tty_port.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Tty port functions
  3. */
  4. #include <linux/types.h>
  5. #include <linux/errno.h>
  6. #include <linux/tty.h>
  7. #include <linux/tty_driver.h>
  8. #include <linux/tty_flip.h>
  9. #include <linux/timer.h>
  10. #include <linux/string.h>
  11. #include <linux/slab.h>
  12. #include <linux/sched.h>
  13. #include <linux/init.h>
  14. #include <linux/wait.h>
  15. #include <linux/bitops.h>
  16. #include <linux/delay.h>
  17. #include <linux/module.h>
  18. void tty_port_init(struct tty_port *port)
  19. {
  20. memset(port, 0, sizeof(*port));
  21. init_waitqueue_head(&port->open_wait);
  22. init_waitqueue_head(&port->close_wait);
  23. mutex_init(&port->mutex);
  24. spin_lock_init(&port->lock);
  25. port->close_delay = (50 * HZ) / 100;
  26. port->closing_wait = (3000 * HZ) / 100;
  27. }
  28. EXPORT_SYMBOL(tty_port_init);
  29. int tty_port_alloc_xmit_buf(struct tty_port *port)
  30. {
  31. /* We may sleep in get_zeroed_page() */
  32. mutex_lock(&port->mutex);
  33. if (port->xmit_buf == NULL)
  34. port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
  35. mutex_unlock(&port->mutex);
  36. if (port->xmit_buf == NULL)
  37. return -ENOMEM;
  38. return 0;
  39. }
  40. EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
  41. void tty_port_free_xmit_buf(struct tty_port *port)
  42. {
  43. mutex_lock(&port->mutex);
  44. if (port->xmit_buf != NULL) {
  45. free_page((unsigned long)port->xmit_buf);
  46. port->xmit_buf = NULL;
  47. }
  48. mutex_unlock(&port->mutex);
  49. }
  50. EXPORT_SYMBOL(tty_port_free_xmit_buf);
  51. /**
  52. * tty_port_tty_get - get a tty reference
  53. * @port: tty port
  54. *
  55. * Return a refcount protected tty instance or NULL if the port is not
  56. * associated with a tty (eg due to close or hangup)
  57. */
  58. struct tty_struct *tty_port_tty_get(struct tty_port *port)
  59. {
  60. unsigned long flags;
  61. struct tty_struct *tty;
  62. spin_lock_irqsave(&port->lock, flags);
  63. tty = tty_kref_get(port->tty);
  64. spin_unlock_irqrestore(&port->lock, flags);
  65. return tty;
  66. }
  67. EXPORT_SYMBOL(tty_port_tty_get);
  68. /**
  69. * tty_port_tty_set - set the tty of a port
  70. * @port: tty port
  71. * @tty: the tty
  72. *
  73. * Associate the port and tty pair. Manages any internal refcounts.
  74. * Pass NULL to deassociate a port
  75. */
  76. void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
  77. {
  78. unsigned long flags;
  79. spin_lock_irqsave(&port->lock, flags);
  80. if (port->tty)
  81. tty_kref_put(port->tty);
  82. port->tty = tty_kref_get(tty);
  83. spin_unlock_irqrestore(&port->lock, flags);
  84. }
  85. EXPORT_SYMBOL(tty_port_tty_set);
  86. /**
  87. * tty_port_carrier_raised - carrier raised check
  88. * @port: tty port
  89. *
  90. * Wrapper for the carrier detect logic. For the moment this is used
  91. * to hide some internal details. This will eventually become entirely
  92. * internal to the tty port.
  93. */
  94. int tty_port_carrier_raised(struct tty_port *port)
  95. {
  96. if (port->ops->carrier_raised == NULL)
  97. return 1;
  98. return port->ops->carrier_raised(port);
  99. }
  100. EXPORT_SYMBOL(tty_port_carrier_raised);
  101. /**
  102. * tty_port_raise_dtr_rts - Riase DTR/RTS
  103. * @port: tty port
  104. *
  105. * Wrapper for the DTR/RTS raise logic. For the moment this is used
  106. * to hide some internal details. This will eventually become entirely
  107. * internal to the tty port.
  108. */
  109. void tty_port_raise_dtr_rts(struct tty_port *port)
  110. {
  111. if (port->ops->raise_dtr_rts)
  112. port->ops->raise_dtr_rts(port);
  113. }
  114. EXPORT_SYMBOL(tty_port_raise_dtr_rts);