tty_ldisc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include <tty.h>
  2. #include <tty_ldisc.h>
  3. #define DBG_TAG "TTY_LDISC"
  4. #ifdef RT_TTY_DEBUG
  5. #define DBG_LVL DBG_LOG
  6. #else
  7. #define DBG_LVL DBG_INFO
  8. #endif /* RT_TTY_DEBUG */
  9. #include <rtdbg.h>
  10. static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
  11. static struct tty_ldisc_ops *get_ldops(int disc)
  12. {
  13. struct tty_ldisc_ops *ldops = RT_NULL;
  14. int level = 0;
  15. level = rt_hw_interrupt_disable();
  16. ldops = tty_ldiscs[disc];
  17. if (ldops)
  18. {
  19. ldops->refcount++;
  20. }
  21. rt_hw_interrupt_enable(level);
  22. return ldops;
  23. }
  24. static void put_ldops(struct tty_ldisc_ops *ldops)
  25. {
  26. int level = 0;
  27. level = rt_hw_interrupt_disable();
  28. ldops->refcount--;
  29. rt_hw_interrupt_enable(level);
  30. }
  31. static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
  32. {
  33. struct tty_ldisc *ld = RT_NULL;
  34. struct tty_ldisc_ops *ldops = RT_NULL;
  35. if (disc < N_TTY || disc >= NR_LDISCS)
  36. {
  37. return RT_NULL;
  38. }
  39. ldops = get_ldops(disc);
  40. if (ldops == RT_NULL)
  41. {
  42. LOG_E("tty ldisc get error\n");
  43. return RT_NULL;
  44. }
  45. ld = rt_malloc(sizeof(struct tty_ldisc));
  46. if (ld == RT_NULL)
  47. {
  48. ldops->refcount--;
  49. return RT_NULL;
  50. }
  51. ld->ops = ldops;
  52. ld->tty = tty;
  53. return ld;
  54. }
  55. /**
  56. * tty_ldisc_put - release the ldisc
  57. *
  58. * Complement of tty_ldisc_get().
  59. */
  60. static void tty_ldisc_put(struct tty_ldisc *ld)
  61. {
  62. if (ld == RT_NULL)
  63. {
  64. return;
  65. }
  66. put_ldops(ld->ops);
  67. rt_free(ld);
  68. }
  69. /**
  70. * tty_ldisc_close - close a line discipline
  71. * @tty: tty we are opening the ldisc on
  72. * @ld: discipline to close
  73. *
  74. * A helper close method. Also a convenient debugging and check
  75. * point.
  76. */
  77. static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
  78. {
  79. if (ld->ops->close)
  80. {
  81. ld->ops->close(tty);
  82. }
  83. }
  84. /**
  85. * tty_ldisc_kill - teardown ldisc
  86. * @tty: tty being released
  87. *
  88. * Perform final close of the ldisc and reset tty->ldisc
  89. */
  90. void tty_ldisc_kill(struct tty_struct *tty)
  91. {
  92. if (!tty->ldisc)
  93. {
  94. return;
  95. }
  96. /*
  97. * Now kill off the ldisc
  98. */
  99. tty_ldisc_close(tty, tty->ldisc);
  100. tty_ldisc_put(tty->ldisc);
  101. /* Force an oops if we mess this up */
  102. tty->ldisc = NULL;
  103. }
  104. int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
  105. {
  106. int ret = 0;
  107. int level = 0;
  108. if (disc < N_TTY || disc >= NR_LDISCS)
  109. {
  110. return -EINVAL;
  111. }
  112. level = rt_hw_interrupt_disable();
  113. tty_ldiscs[disc] = new_ldisc;
  114. new_ldisc->num = disc;
  115. new_ldisc->refcount = 0;
  116. rt_hw_interrupt_enable(level);
  117. return ret;
  118. }
  119. /**
  120. * tty_ldisc_release - release line discipline
  121. * @tty: tty being shut down (or one end of pty pair)
  122. *
  123. * Called during the final close of a tty or a pty pair in order to shut
  124. * down the line discpline layer. On exit, each tty's ldisc is NULL.
  125. */
  126. void tty_ldisc_release(struct tty_struct *tty)
  127. {
  128. int level = 0;
  129. struct tty_struct *o_tty = tty->other_struct;
  130. /*
  131. * Shutdown this line discipline. As this is the final close,
  132. * it does not race with the set_ldisc code path.
  133. */
  134. level = rt_hw_interrupt_disable();
  135. tty_ldisc_kill(tty);
  136. if (o_tty)
  137. {
  138. tty_ldisc_kill(o_tty);
  139. }
  140. rt_hw_interrupt_enable(level);
  141. }
  142. /**
  143. * tty_ldisc_init - ldisc setup for new tty
  144. * @tty: tty being allocated
  145. *
  146. * Set up the line discipline objects for a newly allocated tty. Note that
  147. * the tty structure is not completely set up when this call is made.
  148. */
  149. void tty_ldisc_init(struct tty_struct *tty)
  150. {
  151. struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
  152. RT_ASSERT(ld != RT_NULL);
  153. tty->ldisc = ld;
  154. }