blob: 553b0e9d8d17a5513747337ddf5bcf49caaba95b [file] [log] [blame]
Alan Cox9e485652008-10-13 10:37:07 +01001/*
2 * Tty port functions
3 */
4
5#include <linux/types.h>
6#include <linux/errno.h>
7#include <linux/tty.h>
8#include <linux/tty_driver.h>
9#include <linux/tty_flip.h>
10#include <linux/timer.h>
11#include <linux/string.h>
12#include <linux/slab.h>
13#include <linux/sched.h>
14#include <linux/init.h>
15#include <linux/wait.h>
16#include <linux/bitops.h>
17#include <linux/delay.h>
18#include <linux/module.h>
19
20void tty_port_init(struct tty_port *port)
21{
22 memset(port, 0, sizeof(*port));
23 init_waitqueue_head(&port->open_wait);
24 init_waitqueue_head(&port->close_wait);
25 mutex_init(&port->mutex);
Alan Cox4a90f092008-10-13 10:39:46 +010026 spin_lock_init(&port->lock);
Alan Cox9e485652008-10-13 10:37:07 +010027 port->close_delay = (50 * HZ) / 100;
28 port->closing_wait = (3000 * HZ) / 100;
29}
30EXPORT_SYMBOL(tty_port_init);
31
32int tty_port_alloc_xmit_buf(struct tty_port *port)
33{
34 /* We may sleep in get_zeroed_page() */
35 mutex_lock(&port->mutex);
36 if (port->xmit_buf == NULL)
37 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
38 mutex_unlock(&port->mutex);
39 if (port->xmit_buf == NULL)
40 return -ENOMEM;
41 return 0;
42}
43EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
44
45void tty_port_free_xmit_buf(struct tty_port *port)
46{
47 mutex_lock(&port->mutex);
48 if (port->xmit_buf != NULL) {
49 free_page((unsigned long)port->xmit_buf);
50 port->xmit_buf = NULL;
51 }
52 mutex_unlock(&port->mutex);
53}
54EXPORT_SYMBOL(tty_port_free_xmit_buf);
55
56
Alan Cox4a90f092008-10-13 10:39:46 +010057/**
58 * tty_port_tty_get - get a tty reference
59 * @port: tty port
60 *
61 * Return a refcount protected tty instance or NULL if the port is not
62 * associated with a tty (eg due to close or hangup)
63 */
64
65struct tty_struct *tty_port_tty_get(struct tty_port *port)
66{
67 unsigned long flags;
68 struct tty_struct *tty;
69
70 spin_lock_irqsave(&port->lock, flags);
71 tty = tty_kref_get(port->tty);
72 spin_unlock_irqrestore(&port->lock, flags);
73 return tty;
74}
75EXPORT_SYMBOL(tty_port_tty_get);
76
77/**
78 * tty_port_tty_set - set the tty of a port
79 * @port: tty port
80 * @tty: the tty
81 *
82 * Associate the port and tty pair. Manages any internal refcounts.
83 * Pass NULL to deassociate a port
84 */
85
86void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
87{
88 unsigned long flags;
89
90 spin_lock_irqsave(&port->lock, flags);
91 if (port->tty)
92 tty_kref_put(port->tty);
93 port->tty = tty;
94 spin_unlock_irqrestore(&port->lock, flags);
95}
96EXPORT_SYMBOL(tty_port_tty_set);