tty_ldisc.c 19 KB

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