1
0

tty_ldisc.c 3.8 KB

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