tty_port.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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);