tty_ldisc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. #include <linux/types.h>
  2. #include <linux/errno.h>
  3. #include <linux/kmod.h>
  4. #include <linux/sched.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/tty.h>
  7. #include <linux/tty_driver.h>
  8. #include <linux/file.h>
  9. #include <linux/mm.h>
  10. #include <linux/string.h>
  11. #include <linux/slab.h>
  12. #include <linux/poll.h>
  13. #include <linux/proc_fs.h>
  14. #include <linux/module.h>
  15. #include <linux/device.h>
  16. #include <linux/wait.h>
  17. #include <linux/bitops.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/ratelimit.h>
  21. #undef LDISC_DEBUG_HANGUP
  22. #ifdef LDISC_DEBUG_HANGUP
  23. #define tty_ldisc_debug(tty, f, args...) ({ \
  24. printk(KERN_DEBUG "%s: %s: " f, __func__, tty_name(tty), ##args); \
  25. })
  26. #else
  27. #define tty_ldisc_debug(tty, f, args...)
  28. #endif
  29. /* lockdep nested classes for tty->ldisc_sem */
  30. enum {
  31. LDISC_SEM_NORMAL,
  32. LDISC_SEM_OTHER,
  33. };
  34. /*
  35. * This guards the refcounted line discipline lists. The lock
  36. * must be taken with irqs off because there are hangup path
  37. * callers who will do ldisc lookups and cannot sleep.
  38. */
  39. static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
  40. /* Line disc dispatch table */
  41. static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
  42. /**
  43. * tty_register_ldisc - install a line discipline
  44. * @disc: ldisc number
  45. * @new_ldisc: pointer to the ldisc object
  46. *
  47. * Installs a new line discipline into the kernel. The discipline
  48. * is set up as unreferenced and then made available to the kernel
  49. * from this point onwards.
  50. *
  51. * Locking:
  52. * takes tty_ldiscs_lock to guard against ldisc races
  53. */
  54. int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
  55. {
  56. unsigned long flags;
  57. int ret = 0;
  58. if (disc < N_TTY || disc >= NR_LDISCS)
  59. return -EINVAL;
  60. raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
  61. tty_ldiscs[disc] = new_ldisc;
  62. new_ldisc->num = disc;
  63. new_ldisc->refcount = 0;
  64. raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
  65. return ret;
  66. }
  67. EXPORT_SYMBOL(tty_register_ldisc);
  68. /**
  69. * tty_unregister_ldisc - unload a line discipline
  70. * @disc: ldisc number
  71. * @new_ldisc: pointer to the ldisc object
  72. *
  73. * Remove a line discipline from the kernel providing it is not
  74. * currently in use.
  75. *
  76. * Locking:
  77. * takes tty_ldiscs_lock to guard against ldisc races
  78. */
  79. int tty_unregister_ldisc(int disc)
  80. {
  81. unsigned long flags;
  82. int ret = 0;
  83. if (disc < N_TTY || disc >= NR_LDISCS)
  84. return -EINVAL;
  85. raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
  86. if (tty_ldiscs[disc]->refcount)
  87. ret = -EBUSY;
  88. else
  89. tty_ldiscs[disc] = NULL;
  90. raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
  91. return ret;
  92. }
  93. EXPORT_SYMBOL(tty_unregister_ldisc);
  94. static struct tty_ldisc_ops *get_ldops(int disc)
  95. {
  96. unsigned long flags;
  97. struct tty_ldisc_ops *ldops, *ret;
  98. raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
  99. ret = ERR_PTR(-EINVAL);
  100. ldops = tty_ldiscs[disc];
  101. if (ldops) {
  102. ret = ERR_PTR(-EAGAIN);
  103. if (try_module_get(ldops->owner)) {
  104. ldops->refcount++;
  105. ret = ldops;
  106. }
  107. }
  108. raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
  109. return ret;
  110. }
  111. static void put_ldops(struct tty_ldisc_ops *ldops)
  112. {
  113. unsigned long flags;
  114. raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
  115. ldops->refcount--;
  116. module_put(ldops->owner);
  117. raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
  118. }
  119. /**
  120. * tty_ldisc_get - take a reference to an ldisc
  121. * @disc: ldisc number
  122. *
  123. * Takes a reference to a line discipline. Deals with refcounts and
  124. * module locking counts. Returns NULL if the discipline is not available.
  125. * Returns a pointer to the discipline and bumps the ref count if it is
  126. * available
  127. *
  128. * Locking:
  129. * takes tty_ldiscs_lock to guard against ldisc races
  130. */
  131. static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
  132. {
  133. struct tty_ldisc *ld;
  134. struct tty_ldisc_ops *ldops;
  135. if (disc < N_TTY || disc >= NR_LDISCS)
  136. return ERR_PTR(-EINVAL);
  137. /*
  138. * Get the ldisc ops - we may need to request them to be loaded
  139. * dynamically and try again.
  140. */
  141. ldops = get_ldops(disc);
  142. if (IS_ERR(ldops)) {
  143. request_module("tty-ldisc-%d", disc);
  144. ldops = get_ldops(disc);
  145. if (IS_ERR(ldops))
  146. return ERR_CAST(ldops);
  147. }
  148. ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
  149. if (ld == NULL) {
  150. put_ldops(ldops);
  151. return ERR_PTR(-ENOMEM);
  152. }
  153. ld->ops = ldops;
  154. ld->tty = tty;
  155. return ld;
  156. }
  157. /**
  158. * tty_ldisc_put - release the ldisc
  159. *
  160. * Complement of tty_ldisc_get().
  161. */
  162. static inline void tty_ldisc_put(struct tty_ldisc *ld)
  163. {
  164. if (WARN_ON_ONCE(!ld))
  165. return;
  166. put_ldops(ld->ops);
  167. kfree(ld);
  168. }
  169. static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
  170. {
  171. return (*pos < NR_LDISCS) ? pos : NULL;
  172. }
  173. static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
  174. {
  175. (*pos)++;
  176. return (*pos < NR_LDISCS) ? pos : NULL;
  177. }
  178. static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
  179. {
  180. }
  181. static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
  182. {
  183. int i = *(loff_t *)v;
  184. struct tty_ldisc_ops *ldops;
  185. ldops = get_ldops(i);
  186. if (IS_ERR(ldops))
  187. return 0;
  188. seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
  189. put_ldops(ldops);
  190. return 0;
  191. }
  192. static const struct seq_operations tty_ldiscs_seq_ops = {
  193. .start = tty_ldiscs_seq_start,
  194. .next = tty_ldiscs_seq_next,
  195. .stop = tty_ldiscs_seq_stop,
  196. .show = tty_ldiscs_seq_show,
  197. };
  198. static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
  199. {
  200. return seq_open(file, &tty_ldiscs_seq_ops);
  201. }
  202. const struct file_operations tty_ldiscs_proc_fops = {
  203. .owner = THIS_MODULE,
  204. .open = proc_tty_ldiscs_open,
  205. .read = seq_read,
  206. .llseek = seq_lseek,
  207. .release = seq_release,
  208. };
  209. /**
  210. * tty_ldisc_ref_wait - wait for the tty ldisc
  211. * @tty: tty device
  212. *
  213. * Dereference the line discipline for the terminal and take a
  214. * reference to it. If the line discipline is in flux then
  215. * wait patiently until it changes.
  216. *
  217. * Note: Must not be called from an IRQ/timer context. The caller
  218. * must also be careful not to hold other locks that will deadlock
  219. * against a discipline change, such as an existing ldisc reference
  220. * (which we check for)
  221. *
  222. * Note: only callable from a file_operations routine (which
  223. * guarantees tty->ldisc != NULL when the lock is acquired).
  224. */
  225. struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
  226. {
  227. ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
  228. WARN_ON(!tty->ldisc);
  229. return tty->ldisc;
  230. }
  231. EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
  232. /**
  233. * tty_ldisc_ref - get the tty ldisc
  234. * @tty: tty device
  235. *
  236. * Dereference the line discipline for the terminal and take a
  237. * reference to it. If the line discipline is in flux then
  238. * return NULL. Can be called from IRQ and timer functions.
  239. */
  240. struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
  241. {
  242. struct tty_ldisc *ld = NULL;
  243. if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
  244. ld = tty->ldisc;
  245. if (!ld)
  246. ldsem_up_read(&tty->ldisc_sem);
  247. }
  248. return ld;
  249. }
  250. EXPORT_SYMBOL_GPL(tty_ldisc_ref);
  251. /**
  252. * tty_ldisc_deref - free a tty ldisc reference
  253. * @ld: reference to free up
  254. *
  255. * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
  256. * be called in IRQ context.
  257. */
  258. void tty_ldisc_deref(struct tty_ldisc *ld)
  259. {
  260. ldsem_up_read(&ld->tty->ldisc_sem);
  261. }
  262. EXPORT_SYMBOL_GPL(tty_ldisc_deref);
  263. static inline int __lockfunc
  264. __tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
  265. {
  266. return ldsem_down_write(&tty->ldisc_sem, timeout);
  267. }
  268. static inline int __lockfunc
  269. __tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
  270. {
  271. return ldsem_down_write_nested(&tty->ldisc_sem,
  272. LDISC_SEM_OTHER, timeout);
  273. }
  274. static inline void __tty_ldisc_unlock(struct tty_struct *tty)
  275. {
  276. return ldsem_up_write(&tty->ldisc_sem);
  277. }
  278. static int __lockfunc
  279. tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
  280. {
  281. int ret;
  282. ret = __tty_ldisc_lock(tty, timeout);
  283. if (!ret)
  284. return -EBUSY;
  285. set_bit(TTY_LDISC_HALTED, &tty->flags);
  286. return 0;
  287. }
  288. static void tty_ldisc_unlock(struct tty_struct *tty)
  289. {
  290. clear_bit(TTY_LDISC_HALTED, &tty->flags);
  291. __tty_ldisc_unlock(tty);
  292. }
  293. static int __lockfunc
  294. tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
  295. unsigned long timeout)
  296. {
  297. int ret;
  298. if (tty < tty2) {
  299. ret = __tty_ldisc_lock(tty, timeout);
  300. if (ret) {
  301. ret = __tty_ldisc_lock_nested(tty2, timeout);
  302. if (!ret)
  303. __tty_ldisc_unlock(tty);
  304. }
  305. } else {
  306. /* if this is possible, it has lots of implications */
  307. WARN_ON_ONCE(tty == tty2);
  308. if (tty2 && tty != tty2) {
  309. ret = __tty_ldisc_lock(tty2, timeout);
  310. if (ret) {
  311. ret = __tty_ldisc_lock_nested(tty, timeout);
  312. if (!ret)
  313. __tty_ldisc_unlock(tty2);
  314. }
  315. } else
  316. ret = __tty_ldisc_lock(tty, timeout);
  317. }
  318. if (!ret)
  319. return -EBUSY;
  320. set_bit(TTY_LDISC_HALTED, &tty->flags);
  321. if (tty2)
  322. set_bit(TTY_LDISC_HALTED, &tty2->flags);
  323. return 0;
  324. }
  325. static void __lockfunc
  326. tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
  327. {
  328. tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
  329. }
  330. static void __lockfunc tty_ldisc_unlock_pair(struct tty_struct *tty,
  331. struct tty_struct *tty2)
  332. {
  333. __tty_ldisc_unlock(tty);
  334. if (tty2)
  335. __tty_ldisc_unlock(tty2);
  336. }
  337. /**
  338. * tty_ldisc_flush - flush line discipline queue
  339. * @tty: tty
  340. *
  341. * Flush the line discipline queue (if any) and the tty flip buffers
  342. * for this tty.
  343. */
  344. void tty_ldisc_flush(struct tty_struct *tty)
  345. {
  346. struct tty_ldisc *ld = tty_ldisc_ref(tty);
  347. tty_buffer_flush(tty, ld);
  348. if (ld)
  349. tty_ldisc_deref(ld);
  350. }
  351. EXPORT_SYMBOL_GPL(tty_ldisc_flush);
  352. /**
  353. * tty_set_termios_ldisc - set ldisc field
  354. * @tty: tty structure
  355. * @num: line discipline number
  356. *
  357. * This is probably overkill for real world processors but
  358. * they are not on hot paths so a little discipline won't do
  359. * any harm.
  360. *
  361. * Locking: takes termios_rwsem
  362. */
  363. static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
  364. {
  365. down_write(&tty->termios_rwsem);
  366. tty->termios.c_line = num;
  367. up_write(&tty->termios_rwsem);
  368. }
  369. /**
  370. * tty_ldisc_open - open a line discipline
  371. * @tty: tty we are opening the ldisc on
  372. * @ld: discipline to open
  373. *
  374. * A helper opening method. Also a convenient debugging and check
  375. * point.
  376. *
  377. * Locking: always called with BTM already held.
  378. */
  379. static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
  380. {
  381. WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
  382. if (ld->ops->open) {
  383. int ret;
  384. /* BTM here locks versus a hangup event */
  385. ret = ld->ops->open(tty);
  386. if (ret)
  387. clear_bit(TTY_LDISC_OPEN, &tty->flags);
  388. return ret;
  389. }
  390. return 0;
  391. }
  392. /**
  393. * tty_ldisc_close - close a line discipline
  394. * @tty: tty we are opening the ldisc on
  395. * @ld: discipline to close
  396. *
  397. * A helper close method. Also a convenient debugging and check
  398. * point.
  399. */
  400. static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
  401. {
  402. WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
  403. clear_bit(TTY_LDISC_OPEN, &tty->flags);
  404. if (ld->ops->close)
  405. ld->ops->close(tty);
  406. }
  407. /**
  408. * tty_ldisc_restore - helper for tty ldisc change
  409. * @tty: tty to recover
  410. * @old: previous ldisc
  411. *
  412. * Restore the previous line discipline or N_TTY when a line discipline
  413. * change fails due to an open error
  414. */
  415. static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
  416. {
  417. struct tty_ldisc *new_ldisc;
  418. int r;
  419. /* There is an outstanding reference here so this is safe */
  420. old = tty_ldisc_get(tty, old->ops->num);
  421. WARN_ON(IS_ERR(old));
  422. tty->ldisc = old;
  423. tty_set_termios_ldisc(tty, old->ops->num);
  424. if (tty_ldisc_open(tty, old) < 0) {
  425. tty_ldisc_put(old);
  426. /* This driver is always present */
  427. new_ldisc = tty_ldisc_get(tty, N_TTY);
  428. if (IS_ERR(new_ldisc))
  429. panic("n_tty: get");
  430. tty->ldisc = new_ldisc;
  431. tty_set_termios_ldisc(tty, N_TTY);
  432. r = tty_ldisc_open(tty, new_ldisc);
  433. if (r < 0)
  434. panic("Couldn't open N_TTY ldisc for "
  435. "%s --- error %d.",
  436. tty_name(tty), r);
  437. }
  438. }
  439. /**
  440. * tty_set_ldisc - set line discipline
  441. * @tty: the terminal to set
  442. * @ldisc: the line discipline
  443. *
  444. * Set the discipline of a tty line. Must be called from a process
  445. * context. The ldisc change logic has to protect itself against any
  446. * overlapping ldisc change (including on the other end of pty pairs),
  447. * the close of one side of a tty/pty pair, and eventually hangup.
  448. */
  449. int tty_set_ldisc(struct tty_struct *tty, int ldisc)
  450. {
  451. int retval;
  452. struct tty_ldisc *old_ldisc, *new_ldisc;
  453. new_ldisc = tty_ldisc_get(tty, ldisc);
  454. if (IS_ERR(new_ldisc))
  455. return PTR_ERR(new_ldisc);
  456. tty_lock(tty);
  457. retval = tty_ldisc_lock(tty, 5 * HZ);
  458. if (retval) {
  459. tty_ldisc_put(new_ldisc);
  460. tty_unlock(tty);
  461. return retval;
  462. }
  463. /*
  464. * Check the no-op case
  465. */
  466. if (tty->ldisc->ops->num == ldisc) {
  467. tty_ldisc_unlock(tty);
  468. tty_ldisc_put(new_ldisc);
  469. tty_unlock(tty);
  470. return 0;
  471. }
  472. old_ldisc = tty->ldisc;
  473. if (test_bit(TTY_HUPPED, &tty->flags)) {
  474. /* We were raced by the hangup method. It will have stomped
  475. the ldisc data and closed the ldisc down */
  476. tty_ldisc_unlock(tty);
  477. tty_ldisc_put(new_ldisc);
  478. tty_unlock(tty);
  479. return -EIO;
  480. }
  481. /* Shutdown the old discipline. */
  482. tty_ldisc_close(tty, old_ldisc);
  483. /* Now set up the new line discipline. */
  484. tty->ldisc = new_ldisc;
  485. tty_set_termios_ldisc(tty, ldisc);
  486. retval = tty_ldisc_open(tty, new_ldisc);
  487. if (retval < 0) {
  488. /* Back to the old one or N_TTY if we can't */
  489. tty_ldisc_put(new_ldisc);
  490. tty_ldisc_restore(tty, old_ldisc);
  491. }
  492. if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
  493. down_read(&tty->termios_rwsem);
  494. tty->ops->set_ldisc(tty);
  495. up_read(&tty->termios_rwsem);
  496. }
  497. /* At this point we hold a reference to the new ldisc and a
  498. reference to the old ldisc, or we hold two references to
  499. the old ldisc (if it was restored as part of error cleanup
  500. above). In either case, releasing a single reference from
  501. the old ldisc is correct. */
  502. tty_ldisc_put(old_ldisc);
  503. /*
  504. * Allow ldisc referencing to occur again
  505. */
  506. tty_ldisc_unlock(tty);
  507. /* Restart the work queue in case no characters kick it off. Safe if
  508. already running */
  509. schedule_work(&tty->port->buf.work);
  510. tty_unlock(tty);
  511. return retval;
  512. }
  513. /**
  514. * tty_reset_termios - reset terminal state
  515. * @tty: tty to reset
  516. *
  517. * Restore a terminal to the driver default state.
  518. */
  519. static void tty_reset_termios(struct tty_struct *tty)
  520. {
  521. down_write(&tty->termios_rwsem);
  522. tty->termios = tty->driver->init_termios;
  523. tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
  524. tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
  525. up_write(&tty->termios_rwsem);
  526. }
  527. /**
  528. * tty_ldisc_reinit - reinitialise the tty ldisc
  529. * @tty: tty to reinit
  530. * @ldisc: line discipline to reinitialize
  531. *
  532. * Switch the tty to a line discipline and leave the ldisc
  533. * state closed
  534. */
  535. static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
  536. {
  537. struct tty_ldisc *ld = tty_ldisc_get(tty, ldisc);
  538. if (IS_ERR(ld))
  539. return -1;
  540. tty_ldisc_close(tty, tty->ldisc);
  541. tty_ldisc_put(tty->ldisc);
  542. /*
  543. * Switch the line discipline back
  544. */
  545. tty->ldisc = ld;
  546. tty_set_termios_ldisc(tty, ldisc);
  547. return 0;
  548. }
  549. /**
  550. * tty_ldisc_hangup - hangup ldisc reset
  551. * @tty: tty being hung up
  552. *
  553. * Some tty devices reset their termios when they receive a hangup
  554. * event. In that situation we must also switch back to N_TTY properly
  555. * before we reset the termios data.
  556. *
  557. * Locking: We can take the ldisc mutex as the rest of the code is
  558. * careful to allow for this.
  559. *
  560. * In the pty pair case this occurs in the close() path of the
  561. * tty itself so we must be careful about locking rules.
  562. */
  563. void tty_ldisc_hangup(struct tty_struct *tty)
  564. {
  565. struct tty_ldisc *ld;
  566. int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
  567. int err = 0;
  568. tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
  569. ld = tty_ldisc_ref(tty);
  570. if (ld != NULL) {
  571. if (ld->ops->flush_buffer)
  572. ld->ops->flush_buffer(tty);
  573. tty_driver_flush_buffer(tty);
  574. if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
  575. ld->ops->write_wakeup)
  576. ld->ops->write_wakeup(tty);
  577. if (ld->ops->hangup)
  578. ld->ops->hangup(tty);
  579. tty_ldisc_deref(ld);
  580. }
  581. wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
  582. wake_up_interruptible_poll(&tty->read_wait, POLLIN);
  583. /*
  584. * Shutdown the current line discipline, and reset it to
  585. * N_TTY if need be.
  586. *
  587. * Avoid racing set_ldisc or tty_ldisc_release
  588. */
  589. tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
  590. if (tty->ldisc) {
  591. /* At this point we have a halted ldisc; we want to close it and
  592. reopen a new ldisc. We could defer the reopen to the next
  593. open but it means auditing a lot of other paths so this is
  594. a FIXME */
  595. if (reset == 0) {
  596. if (!tty_ldisc_reinit(tty, tty->termios.c_line))
  597. err = tty_ldisc_open(tty, tty->ldisc);
  598. else
  599. err = 1;
  600. }
  601. /* If the re-open fails or we reset then go to N_TTY. The
  602. N_TTY open cannot fail */
  603. if (reset || err) {
  604. BUG_ON(tty_ldisc_reinit(tty, N_TTY));
  605. WARN_ON(tty_ldisc_open(tty, tty->ldisc));
  606. }
  607. }
  608. tty_ldisc_unlock(tty);
  609. if (reset)
  610. tty_reset_termios(tty);
  611. tty_ldisc_debug(tty, "re-opened ldisc: %p\n", tty->ldisc);
  612. }
  613. /**
  614. * tty_ldisc_setup - open line discipline
  615. * @tty: tty being shut down
  616. * @o_tty: pair tty for pty/tty pairs
  617. *
  618. * Called during the initial open of a tty/pty pair in order to set up the
  619. * line disciplines and bind them to the tty. This has no locking issues
  620. * as the device isn't yet active.
  621. */
  622. int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
  623. {
  624. struct tty_ldisc *ld = tty->ldisc;
  625. int retval;
  626. retval = tty_ldisc_open(tty, ld);
  627. if (retval)
  628. return retval;
  629. if (o_tty) {
  630. retval = tty_ldisc_open(o_tty, o_tty->ldisc);
  631. if (retval) {
  632. tty_ldisc_close(tty, ld);
  633. return retval;
  634. }
  635. }
  636. return 0;
  637. }
  638. static void tty_ldisc_kill(struct tty_struct *tty)
  639. {
  640. /*
  641. * Now kill off the ldisc
  642. */
  643. tty_ldisc_close(tty, tty->ldisc);
  644. tty_ldisc_put(tty->ldisc);
  645. /* Force an oops if we mess this up */
  646. tty->ldisc = NULL;
  647. /* Ensure the next open requests the N_TTY ldisc */
  648. tty_set_termios_ldisc(tty, N_TTY);
  649. }
  650. /**
  651. * tty_ldisc_release - release line discipline
  652. * @tty: tty being shut down (or one end of pty pair)
  653. *
  654. * Called during the final close of a tty or a pty pair in order to shut
  655. * down the line discpline layer. On exit, each ldisc assigned is N_TTY and
  656. * each ldisc has not been opened.
  657. */
  658. void tty_ldisc_release(struct tty_struct *tty)
  659. {
  660. struct tty_struct *o_tty = tty->link;
  661. /*
  662. * Shutdown this line discipline. As this is the final close,
  663. * it does not race with the set_ldisc code path.
  664. */
  665. tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
  666. tty_ldisc_lock_pair(tty, o_tty);
  667. tty_ldisc_kill(tty);
  668. if (o_tty)
  669. tty_ldisc_kill(o_tty);
  670. tty_ldisc_unlock_pair(tty, o_tty);
  671. /* And the memory resources remaining (buffers, termios) will be
  672. disposed of when the kref hits zero */
  673. tty_ldisc_debug(tty, "ldisc closed\n");
  674. }
  675. /**
  676. * tty_ldisc_init - ldisc setup for new tty
  677. * @tty: tty being allocated
  678. *
  679. * Set up the line discipline objects for a newly allocated tty. Note that
  680. * the tty structure is not completely set up when this call is made.
  681. */
  682. void tty_ldisc_init(struct tty_struct *tty)
  683. {
  684. struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
  685. if (IS_ERR(ld))
  686. panic("n_tty: init_tty");
  687. tty->ldisc = ld;
  688. }
  689. /**
  690. * tty_ldisc_init - ldisc cleanup for new tty
  691. * @tty: tty that was allocated recently
  692. *
  693. * The tty structure must not becompletely set up (tty_ldisc_setup) when
  694. * this call is made.
  695. */
  696. void tty_ldisc_deinit(struct tty_struct *tty)
  697. {
  698. tty_ldisc_put(tty->ldisc);
  699. tty->ldisc = NULL;
  700. }
  701. void tty_ldisc_begin(void)
  702. {
  703. /* Setup the default TTY line discipline. */
  704. (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
  705. }