tty: Implement a drain delay in the tty port
[linux-2.6/kvm.git] / drivers / char / tty_port.c
blob4d08b6d27c28b5f81589367c9d810bc33c53bd36
1 /*
2 * Tty port functions
3 */
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/serial.h>
11 #include <linux/timer.h>
12 #include <linux/string.h>
13 #include <linux/slab.h>
14 #include <linux/sched.h>
15 #include <linux/init.h>
16 #include <linux/wait.h>
17 #include <linux/bitops.h>
18 #include <linux/delay.h>
19 #include <linux/module.h>
21 void tty_port_init(struct tty_port *port)
23 memset(port, 0, sizeof(*port));
24 init_waitqueue_head(&port->open_wait);
25 init_waitqueue_head(&port->close_wait);
26 mutex_init(&port->mutex);
27 spin_lock_init(&port->lock);
28 port->close_delay = (50 * HZ) / 100;
29 port->closing_wait = (3000 * HZ) / 100;
31 EXPORT_SYMBOL(tty_port_init);
33 int tty_port_alloc_xmit_buf(struct tty_port *port)
35 /* We may sleep in get_zeroed_page() */
36 mutex_lock(&port->mutex);
37 if (port->xmit_buf == NULL)
38 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
39 mutex_unlock(&port->mutex);
40 if (port->xmit_buf == NULL)
41 return -ENOMEM;
42 return 0;
44 EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
46 void tty_port_free_xmit_buf(struct tty_port *port)
48 mutex_lock(&port->mutex);
49 if (port->xmit_buf != NULL) {
50 free_page((unsigned long)port->xmit_buf);
51 port->xmit_buf = NULL;
53 mutex_unlock(&port->mutex);
55 EXPORT_SYMBOL(tty_port_free_xmit_buf);
58 /**
59 * tty_port_tty_get - get a tty reference
60 * @port: tty port
62 * Return a refcount protected tty instance or NULL if the port is not
63 * associated with a tty (eg due to close or hangup)
66 struct tty_struct *tty_port_tty_get(struct tty_port *port)
68 unsigned long flags;
69 struct tty_struct *tty;
71 spin_lock_irqsave(&port->lock, flags);
72 tty = tty_kref_get(port->tty);
73 spin_unlock_irqrestore(&port->lock, flags);
74 return tty;
76 EXPORT_SYMBOL(tty_port_tty_get);
78 /**
79 * tty_port_tty_set - set the tty of a port
80 * @port: tty port
81 * @tty: the tty
83 * Associate the port and tty pair. Manages any internal refcounts.
84 * Pass NULL to deassociate a port
87 void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
89 unsigned long flags;
91 spin_lock_irqsave(&port->lock, flags);
92 if (port->tty)
93 tty_kref_put(port->tty);
94 port->tty = tty_kref_get(tty);
95 spin_unlock_irqrestore(&port->lock, flags);
97 EXPORT_SYMBOL(tty_port_tty_set);
99 /**
100 * tty_port_hangup - hangup helper
101 * @port: tty port
103 * Perform port level tty hangup flag and count changes. Drop the tty
104 * reference.
107 void tty_port_hangup(struct tty_port *port)
109 unsigned long flags;
111 spin_lock_irqsave(&port->lock, flags);
112 port->count = 0;
113 port->flags &= ~ASYNC_NORMAL_ACTIVE;
114 if (port->tty)
115 tty_kref_put(port->tty);
116 port->tty = NULL;
117 spin_unlock_irqrestore(&port->lock, flags);
118 wake_up_interruptible(&port->open_wait);
120 EXPORT_SYMBOL(tty_port_hangup);
123 * tty_port_carrier_raised - carrier raised check
124 * @port: tty port
126 * Wrapper for the carrier detect logic. For the moment this is used
127 * to hide some internal details. This will eventually become entirely
128 * internal to the tty port.
131 int tty_port_carrier_raised(struct tty_port *port)
133 if (port->ops->carrier_raised == NULL)
134 return 1;
135 return port->ops->carrier_raised(port);
137 EXPORT_SYMBOL(tty_port_carrier_raised);
140 * tty_port_raise_dtr_rts - Raise DTR/RTS
141 * @port: tty port
143 * Wrapper for the DTR/RTS raise logic. For the moment this is used
144 * to hide some internal details. This will eventually become entirely
145 * internal to the tty port.
148 void tty_port_raise_dtr_rts(struct tty_port *port)
150 if (port->ops->dtr_rts)
151 port->ops->dtr_rts(port, 1);
153 EXPORT_SYMBOL(tty_port_raise_dtr_rts);
156 * tty_port_lower_dtr_rts - Lower DTR/RTS
157 * @port: tty port
159 * Wrapper for the DTR/RTS raise logic. For the moment this is used
160 * to hide some internal details. This will eventually become entirely
161 * internal to the tty port.
164 void tty_port_lower_dtr_rts(struct tty_port *port)
166 if (port->ops->dtr_rts)
167 port->ops->dtr_rts(port, 0);
169 EXPORT_SYMBOL(tty_port_lower_dtr_rts);
172 * tty_port_block_til_ready - Waiting logic for tty open
173 * @port: the tty port being opened
174 * @tty: the tty device being bound
175 * @filp: the file pointer of the opener
177 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
178 * Handles:
179 * - hangup (both before and during)
180 * - non blocking open
181 * - rts/dtr/dcd
182 * - signals
183 * - port flags and counts
185 * The passed tty_port must implement the carrier_raised method if it can
186 * do carrier detect and the dtr_rts method if it supports software
187 * management of these lines. Note that the dtr/rts raise is done each
188 * iteration as a hangup may have previously dropped them while we wait.
191 int tty_port_block_til_ready(struct tty_port *port,
192 struct tty_struct *tty, struct file *filp)
194 int do_clocal = 0, retval;
195 unsigned long flags;
196 DECLARE_WAITQUEUE(wait, current);
197 int cd;
199 /* block if port is in the process of being closed */
200 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
201 interruptible_sleep_on(&port->close_wait);
202 if (port->flags & ASYNC_HUP_NOTIFY)
203 return -EAGAIN;
204 else
205 return -ERESTARTSYS;
208 /* if non-blocking mode is set we can pass directly to open unless
209 the port has just hung up or is in another error state */
210 if ((filp->f_flags & O_NONBLOCK) ||
211 (tty->flags & (1 << TTY_IO_ERROR))) {
212 port->flags |= ASYNC_NORMAL_ACTIVE;
213 return 0;
216 if (C_CLOCAL(tty))
217 do_clocal = 1;
219 /* Block waiting until we can proceed. We may need to wait for the
220 carrier, but we must also wait for any close that is in progress
221 before the next open may complete */
223 retval = 0;
224 add_wait_queue(&port->open_wait, &wait);
226 /* The port lock protects the port counts */
227 spin_lock_irqsave(&port->lock, flags);
228 if (!tty_hung_up_p(filp))
229 port->count--;
230 port->blocked_open++;
231 spin_unlock_irqrestore(&port->lock, flags);
233 while (1) {
234 /* Indicate we are open */
235 if (tty->termios->c_cflag & CBAUD)
236 tty_port_raise_dtr_rts(port);
238 set_current_state(TASK_INTERRUPTIBLE);
239 /* Check for a hangup or uninitialised port. Return accordingly */
240 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
241 if (port->flags & ASYNC_HUP_NOTIFY)
242 retval = -EAGAIN;
243 else
244 retval = -ERESTARTSYS;
245 break;
247 /* Probe the carrier. For devices with no carrier detect this
248 will always return true */
249 cd = tty_port_carrier_raised(port);
250 if (!(port->flags & ASYNC_CLOSING) &&
251 (do_clocal || cd))
252 break;
253 if (signal_pending(current)) {
254 retval = -ERESTARTSYS;
255 break;
257 schedule();
259 set_current_state(TASK_RUNNING);
260 remove_wait_queue(&port->open_wait, &wait);
262 /* Update counts. A parallel hangup will have set count to zero and
263 we must not mess that up further */
264 spin_lock_irqsave(&port->lock, flags);
265 if (!tty_hung_up_p(filp))
266 port->count++;
267 port->blocked_open--;
268 if (retval == 0)
269 port->flags |= ASYNC_NORMAL_ACTIVE;
270 spin_unlock_irqrestore(&port->lock, flags);
271 return 0;
274 EXPORT_SYMBOL(tty_port_block_til_ready);
276 int tty_port_close_start(struct tty_port *port, struct tty_struct *tty, struct file *filp)
278 unsigned long flags;
280 spin_lock_irqsave(&port->lock, flags);
281 if (tty_hung_up_p(filp)) {
282 spin_unlock_irqrestore(&port->lock, flags);
283 return 0;
286 if( tty->count == 1 && port->count != 1) {
287 printk(KERN_WARNING
288 "tty_port_close_start: tty->count = 1 port count = %d.\n",
289 port->count);
290 port->count = 1;
292 if (--port->count < 0) {
293 printk(KERN_WARNING "tty_port_close_start: count = %d\n",
294 port->count);
295 port->count = 0;
298 if (port->count) {
299 spin_unlock_irqrestore(&port->lock, flags);
300 return 0;
302 port->flags |= ASYNC_CLOSING;
303 tty->closing = 1;
304 spin_unlock_irqrestore(&port->lock, flags);
305 /* Don't block on a stalled port, just pull the chain */
306 if (tty->flow_stopped)
307 tty_driver_flush_buffer(tty);
308 if (port->flags & ASYNC_INITIALIZED &&
309 port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
310 tty_wait_until_sent(tty, port->closing_wait);
311 if (port->drain_delay) {
312 unsigned int bps = tty_get_baud_rate(tty);
313 long timeout;
315 if (bps > 1200)
316 timeout = max_t(long, (HZ * 10 * port->drain_delay) / bps,
317 HZ / 10);
318 else
319 timeout = 2 * HZ;
320 schedule_timeout_interruptible(timeout);
322 return 1;
324 EXPORT_SYMBOL(tty_port_close_start);
326 void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
328 unsigned long flags;
330 tty_ldisc_flush(tty);
332 if (tty->termios->c_cflag & HUPCL)
333 tty_port_lower_dtr_rts(port);
335 spin_lock_irqsave(&port->lock, flags);
336 tty->closing = 0;
338 if (port->blocked_open) {
339 spin_unlock_irqrestore(&port->lock, flags);
340 if (port->close_delay) {
341 msleep_interruptible(
342 jiffies_to_msecs(port->close_delay));
344 spin_lock_irqsave(&port->lock, flags);
345 wake_up_interruptible(&port->open_wait);
347 port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
348 wake_up_interruptible(&port->close_wait);
349 spin_unlock_irqrestore(&port->lock, flags);
351 EXPORT_SYMBOL(tty_port_close_end);