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