tty_ldisc.c 20 KB

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