tty_ldisc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
  281. unsigned long timeout)
  282. {
  283. int ret;
  284. if (tty < tty2) {
  285. ret = tty_ldisc_lock(tty, timeout);
  286. if (ret) {
  287. ret = tty_ldisc_lock_nested(tty2, timeout);
  288. if (!ret)
  289. tty_ldisc_unlock(tty);
  290. }
  291. } else {
  292. /* if this is possible, it has lots of implications */
  293. WARN_ON_ONCE(tty == tty2);
  294. if (tty2 && tty != tty2) {
  295. ret = tty_ldisc_lock(tty2, timeout);
  296. if (ret) {
  297. ret = tty_ldisc_lock_nested(tty, timeout);
  298. if (!ret)
  299. tty_ldisc_unlock(tty2);
  300. }
  301. } else
  302. ret = tty_ldisc_lock(tty, timeout);
  303. }
  304. if (!ret)
  305. return -EBUSY;
  306. set_bit(TTY_LDISC_HALTED, &tty->flags);
  307. if (tty2)
  308. set_bit(TTY_LDISC_HALTED, &tty2->flags);
  309. return 0;
  310. }
  311. static void __lockfunc
  312. tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
  313. {
  314. tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
  315. }
  316. static void __lockfunc tty_ldisc_unlock_pair(struct tty_struct *tty,
  317. struct tty_struct *tty2)
  318. {
  319. tty_ldisc_unlock(tty);
  320. if (tty2)
  321. tty_ldisc_unlock(tty2);
  322. }
  323. static void __lockfunc tty_ldisc_enable_pair(struct tty_struct *tty,
  324. struct tty_struct *tty2)
  325. {
  326. clear_bit(TTY_LDISC_HALTED, &tty->flags);
  327. if (tty2)
  328. clear_bit(TTY_LDISC_HALTED, &tty2->flags);
  329. tty_ldisc_unlock_pair(tty, tty2);
  330. }
  331. /**
  332. * tty_ldisc_flush - flush line discipline queue
  333. * @tty: tty
  334. *
  335. * Flush the line discipline queue (if any) for this tty. If there
  336. * is no line discipline active this is a no-op.
  337. */
  338. void tty_ldisc_flush(struct tty_struct *tty)
  339. {
  340. struct tty_ldisc *ld = tty_ldisc_ref(tty);
  341. if (ld) {
  342. if (ld->ops->flush_buffer)
  343. ld->ops->flush_buffer(tty);
  344. tty_ldisc_deref(ld);
  345. }
  346. tty_buffer_flush(tty);
  347. }
  348. EXPORT_SYMBOL_GPL(tty_ldisc_flush);
  349. /**
  350. * tty_set_termios_ldisc - set ldisc field
  351. * @tty: tty structure
  352. * @num: line discipline number
  353. *
  354. * This is probably overkill for real world processors but
  355. * they are not on hot paths so a little discipline won't do
  356. * any harm.
  357. *
  358. * Locking: takes termios_rwsem
  359. */
  360. static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
  361. {
  362. down_write(&tty->termios_rwsem);
  363. tty->termios.c_line = num;
  364. up_write(&tty->termios_rwsem);
  365. }
  366. /**
  367. * tty_ldisc_open - open a line discipline
  368. * @tty: tty we are opening the ldisc on
  369. * @ld: discipline to open
  370. *
  371. * A helper opening method. Also a convenient debugging and check
  372. * point.
  373. *
  374. * Locking: always called with BTM already held.
  375. */
  376. static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
  377. {
  378. WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
  379. if (ld->ops->open) {
  380. int ret;
  381. /* BTM here locks versus a hangup event */
  382. ret = ld->ops->open(tty);
  383. if (ret)
  384. clear_bit(TTY_LDISC_OPEN, &tty->flags);
  385. return ret;
  386. }
  387. return 0;
  388. }
  389. /**
  390. * tty_ldisc_close - close a line discipline
  391. * @tty: tty we are opening the ldisc on
  392. * @ld: discipline to close
  393. *
  394. * A helper close method. Also a convenient debugging and check
  395. * point.
  396. */
  397. static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
  398. {
  399. WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
  400. clear_bit(TTY_LDISC_OPEN, &tty->flags);
  401. if (ld->ops->close)
  402. ld->ops->close(tty);
  403. }
  404. /**
  405. * tty_ldisc_restore - helper for tty ldisc change
  406. * @tty: tty to recover
  407. * @old: previous ldisc
  408. *
  409. * Restore the previous line discipline or N_TTY when a line discipline
  410. * change fails due to an open error
  411. */
  412. static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
  413. {
  414. char buf[64];
  415. struct tty_ldisc *new_ldisc;
  416. int r;
  417. /* There is an outstanding reference here so this is safe */
  418. old = tty_ldisc_get(tty, old->ops->num);
  419. WARN_ON(IS_ERR(old));
  420. tty->ldisc = old;
  421. tty_set_termios_ldisc(tty, old->ops->num);
  422. if (tty_ldisc_open(tty, old) < 0) {
  423. tty_ldisc_put(old);
  424. /* This driver is always present */
  425. new_ldisc = tty_ldisc_get(tty, N_TTY);
  426. if (IS_ERR(new_ldisc))
  427. panic("n_tty: get");
  428. tty->ldisc = new_ldisc;
  429. tty_set_termios_ldisc(tty, N_TTY);
  430. r = tty_ldisc_open(tty, new_ldisc);
  431. if (r < 0)
  432. panic("Couldn't open N_TTY ldisc for "
  433. "%s --- error %d.",
  434. tty_name(tty, buf), r);
  435. }
  436. }
  437. /**
  438. * tty_set_ldisc - set line discipline
  439. * @tty: the terminal to set
  440. * @ldisc: the line discipline
  441. *
  442. * Set the discipline of a tty line. Must be called from a process
  443. * context. The ldisc change logic has to protect itself against any
  444. * overlapping ldisc change (including on the other end of pty pairs),
  445. * the close of one side of a tty/pty pair, and eventually hangup.
  446. */
  447. int tty_set_ldisc(struct tty_struct *tty, int ldisc)
  448. {
  449. int retval;
  450. struct tty_ldisc *old_ldisc, *new_ldisc;
  451. struct tty_struct *o_tty = tty->link;
  452. new_ldisc = tty_ldisc_get(tty, ldisc);
  453. if (IS_ERR(new_ldisc))
  454. return PTR_ERR(new_ldisc);
  455. retval = tty_ldisc_lock_pair_timeout(tty, o_tty, 5 * HZ);
  456. if (retval) {
  457. tty_ldisc_put(new_ldisc);
  458. return retval;
  459. }
  460. /*
  461. * Check the no-op case
  462. */
  463. if (tty->ldisc->ops->num == ldisc) {
  464. tty_ldisc_enable_pair(tty, o_tty);
  465. tty_ldisc_put(new_ldisc);
  466. return 0;
  467. }
  468. old_ldisc = tty->ldisc;
  469. tty_lock(tty);
  470. if (test_bit(TTY_HUPPING, &tty->flags) ||
  471. test_bit(TTY_HUPPED, &tty->flags)) {
  472. /* We were raced by the hangup method. It will have stomped
  473. the ldisc data and closed the ldisc down */
  474. tty_ldisc_enable_pair(tty, o_tty);
  475. tty_ldisc_put(new_ldisc);
  476. tty_unlock(tty);
  477. return -EIO;
  478. }
  479. /* Shutdown the old discipline. */
  480. tty_ldisc_close(tty, old_ldisc);
  481. /* Now set up the new line discipline. */
  482. tty->ldisc = new_ldisc;
  483. tty_set_termios_ldisc(tty, ldisc);
  484. retval = tty_ldisc_open(tty, new_ldisc);
  485. if (retval < 0) {
  486. /* Back to the old one or N_TTY if we can't */
  487. tty_ldisc_put(new_ldisc);
  488. tty_ldisc_restore(tty, old_ldisc);
  489. }
  490. if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc)
  491. tty->ops->set_ldisc(tty);
  492. /* At this point we hold a reference to the new ldisc and a
  493. reference to the old ldisc, or we hold two references to
  494. the old ldisc (if it was restored as part of error cleanup
  495. above). In either case, releasing a single reference from
  496. the old ldisc is correct. */
  497. tty_ldisc_put(old_ldisc);
  498. /*
  499. * Allow ldisc referencing to occur again
  500. */
  501. tty_ldisc_enable_pair(tty, o_tty);
  502. /* Restart the work queue in case no characters kick it off. Safe if
  503. already running */
  504. schedule_work(&tty->port->buf.work);
  505. if (o_tty)
  506. schedule_work(&o_tty->port->buf.work);
  507. tty_unlock(tty);
  508. return retval;
  509. }
  510. /**
  511. * tty_reset_termios - reset terminal state
  512. * @tty: tty to reset
  513. *
  514. * Restore a terminal to the driver default state.
  515. */
  516. static void tty_reset_termios(struct tty_struct *tty)
  517. {
  518. down_write(&tty->termios_rwsem);
  519. tty->termios = tty->driver->init_termios;
  520. tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
  521. tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
  522. up_write(&tty->termios_rwsem);
  523. }
  524. /**
  525. * tty_ldisc_reinit - reinitialise the tty ldisc
  526. * @tty: tty to reinit
  527. * @ldisc: line discipline to reinitialize
  528. *
  529. * Switch the tty to a line discipline and leave the ldisc
  530. * state closed
  531. */
  532. static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
  533. {
  534. struct tty_ldisc *ld = tty_ldisc_get(tty, ldisc);
  535. if (IS_ERR(ld))
  536. return -1;
  537. tty_ldisc_close(tty, tty->ldisc);
  538. tty_ldisc_put(tty->ldisc);
  539. /*
  540. * Switch the line discipline back
  541. */
  542. tty->ldisc = ld;
  543. tty_set_termios_ldisc(tty, ldisc);
  544. return 0;
  545. }
  546. /**
  547. * tty_ldisc_hangup - hangup ldisc reset
  548. * @tty: tty being hung up
  549. *
  550. * Some tty devices reset their termios when they receive a hangup
  551. * event. In that situation we must also switch back to N_TTY properly
  552. * before we reset the termios data.
  553. *
  554. * Locking: We can take the ldisc mutex as the rest of the code is
  555. * careful to allow for this.
  556. *
  557. * In the pty pair case this occurs in the close() path of the
  558. * tty itself so we must be careful about locking rules.
  559. */
  560. void tty_ldisc_hangup(struct tty_struct *tty)
  561. {
  562. struct tty_ldisc *ld;
  563. int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
  564. int err = 0;
  565. tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
  566. ld = tty_ldisc_ref(tty);
  567. if (ld != NULL) {
  568. if (ld->ops->flush_buffer)
  569. ld->ops->flush_buffer(tty);
  570. tty_driver_flush_buffer(tty);
  571. if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
  572. ld->ops->write_wakeup)
  573. ld->ops->write_wakeup(tty);
  574. if (ld->ops->hangup)
  575. ld->ops->hangup(tty);
  576. tty_ldisc_deref(ld);
  577. }
  578. wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
  579. wake_up_interruptible_poll(&tty->read_wait, POLLIN);
  580. tty_unlock(tty);
  581. /*
  582. * Shutdown the current line discipline, and reset it to
  583. * N_TTY if need be.
  584. *
  585. * Avoid racing set_ldisc or tty_ldisc_release
  586. */
  587. tty_ldisc_lock_pair(tty, tty->link);
  588. tty_lock(tty);
  589. if (tty->ldisc) {
  590. /* At this point we have a halted ldisc; we want to close it and
  591. reopen a new ldisc. We could defer the reopen to the next
  592. open but it means auditing a lot of other paths so this is
  593. a FIXME */
  594. if (reset == 0) {
  595. if (!tty_ldisc_reinit(tty, tty->termios.c_line))
  596. err = tty_ldisc_open(tty, tty->ldisc);
  597. else
  598. err = 1;
  599. }
  600. /* If the re-open fails or we reset then go to N_TTY. The
  601. N_TTY open cannot fail */
  602. if (reset || err) {
  603. BUG_ON(tty_ldisc_reinit(tty, N_TTY));
  604. WARN_ON(tty_ldisc_open(tty, tty->ldisc));
  605. }
  606. }
  607. tty_ldisc_enable_pair(tty, tty->link);
  608. if (reset)
  609. tty_reset_termios(tty);
  610. tty_ldisc_debug(tty, "re-opened ldisc: %p\n", tty->ldisc);
  611. }
  612. /**
  613. * tty_ldisc_setup - open line discipline
  614. * @tty: tty being shut down
  615. * @o_tty: pair tty for pty/tty pairs
  616. *
  617. * Called during the initial open of a tty/pty pair in order to set up the
  618. * line disciplines and bind them to the tty. This has no locking issues
  619. * as the device isn't yet active.
  620. */
  621. int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
  622. {
  623. struct tty_ldisc *ld = tty->ldisc;
  624. int retval;
  625. retval = tty_ldisc_open(tty, ld);
  626. if (retval)
  627. return retval;
  628. if (o_tty) {
  629. retval = tty_ldisc_open(o_tty, o_tty->ldisc);
  630. if (retval) {
  631. tty_ldisc_close(tty, ld);
  632. return retval;
  633. }
  634. }
  635. return 0;
  636. }
  637. static void tty_ldisc_kill(struct tty_struct *tty)
  638. {
  639. /*
  640. * Now kill off the ldisc
  641. */
  642. tty_ldisc_close(tty, tty->ldisc);
  643. tty_ldisc_put(tty->ldisc);
  644. /* Force an oops if we mess this up */
  645. tty->ldisc = NULL;
  646. /* Ensure the next open requests the N_TTY ldisc */
  647. tty_set_termios_ldisc(tty, N_TTY);
  648. }
  649. /**
  650. * tty_ldisc_release - release line discipline
  651. * @tty: tty being shut down
  652. * @o_tty: pair tty for pty/tty pairs
  653. *
  654. * Called during the final close of a tty/pty pair in order to shut down
  655. * the line discpline layer. On exit the ldisc assigned is N_TTY and the
  656. * ldisc has not been opened.
  657. */
  658. void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
  659. {
  660. /*
  661. * Shutdown this line discipline. As this is the final close,
  662. * it does not race with the set_ldisc code path.
  663. */
  664. tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
  665. tty_ldisc_lock_pair(tty, o_tty);
  666. tty_lock_pair(tty, o_tty);
  667. tty_ldisc_kill(tty);
  668. if (o_tty)
  669. tty_ldisc_kill(o_tty);
  670. tty_unlock_pair(tty, o_tty);
  671. tty_ldisc_unlock_pair(tty, o_tty);
  672. /* And the memory resources remaining (buffers, termios) will be
  673. disposed of when the kref hits zero */
  674. tty_ldisc_debug(tty, "ldisc closed\n");
  675. }
  676. /**
  677. * tty_ldisc_init - ldisc setup for new tty
  678. * @tty: tty being allocated
  679. *
  680. * Set up the line discipline objects for a newly allocated tty. Note that
  681. * the tty structure is not completely set up when this call is made.
  682. */
  683. void tty_ldisc_init(struct tty_struct *tty)
  684. {
  685. struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
  686. if (IS_ERR(ld))
  687. panic("n_tty: init_tty");
  688. tty->ldisc = ld;
  689. }
  690. /**
  691. * tty_ldisc_init - ldisc cleanup for new tty
  692. * @tty: tty that was allocated recently
  693. *
  694. * The tty structure must not becompletely set up (tty_ldisc_setup) when
  695. * this call is made.
  696. */
  697. void tty_ldisc_deinit(struct tty_struct *tty)
  698. {
  699. tty_ldisc_put(tty->ldisc);
  700. tty->ldisc = NULL;
  701. }
  702. void tty_ldisc_begin(void)
  703. {
  704. /* Setup the default TTY line discipline. */
  705. (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
  706. }