TTY: clean up port drain-delay handling
[deliverable/linux.git] / drivers / tty / tty_port.c
CommitLineData
9e48565d
AC
1/*
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>
3e61696b 10#include <linux/serial.h>
9e48565d
AC
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>
20
21void tty_port_init(struct tty_port *port)
22{
23 memset(port, 0, sizeof(*port));
ecbbfd44 24 tty_buffer_init(port);
9e48565d
AC
25 init_waitqueue_head(&port->open_wait);
26 init_waitqueue_head(&port->close_wait);
bdc04e31 27 init_waitqueue_head(&port->delta_msr_wait);
9e48565d 28 mutex_init(&port->mutex);
44e4909e 29 mutex_init(&port->buf_mutex);
4a90f09b 30 spin_lock_init(&port->lock);
9e48565d
AC
31 port->close_delay = (50 * HZ) / 100;
32 port->closing_wait = (3000 * HZ) / 100;
568aafc6 33 kref_init(&port->kref);
9e48565d
AC
34}
35EXPORT_SYMBOL(tty_port_init);
36
2cb4ca02
JS
37/**
38 * tty_port_link_device - link tty and tty_port
39 * @port: tty_port of the device
40 * @driver: tty_driver for this device
41 * @index: index of the tty
42 *
43 * Provide the tty layer wit ha link from a tty (specified by @index) to a
44 * tty_port (@port). Use this only if neither tty_port_register_device nor
45 * tty_port_install is used in the driver. If used, this has to be called before
46 * tty_register_driver.
47 */
48void tty_port_link_device(struct tty_port *port,
49 struct tty_driver *driver, unsigned index)
50{
51 if (WARN_ON(index >= driver->num))
52 return;
53 driver->ports[index] = port;
54}
55EXPORT_SYMBOL_GPL(tty_port_link_device);
56
72a33bf5
JS
57/**
58 * tty_port_register_device - register tty device
59 * @port: tty_port of the device
60 * @driver: tty_driver for this device
61 * @index: index of the tty
62 * @device: parent if exists, otherwise NULL
63 *
64 * It is the same as tty_register_device except the provided @port is linked to
65 * a concrete tty specified by @index. Use this or tty_port_install (or both).
66 * Call tty_port_link_device as a last resort.
67 */
057eb856
JS
68struct device *tty_port_register_device(struct tty_port *port,
69 struct tty_driver *driver, unsigned index,
70 struct device *device)
71{
2cb4ca02 72 tty_port_link_device(port, driver, index);
057eb856
JS
73 return tty_register_device(driver, index, device);
74}
75EXPORT_SYMBOL_GPL(tty_port_register_device);
76
b1b79916
TH
77/**
78 * tty_port_register_device_attr - register tty device
79 * @port: tty_port of the device
80 * @driver: tty_driver for this device
81 * @index: index of the tty
82 * @device: parent if exists, otherwise NULL
83 * @drvdata: Driver data to be set to device.
84 * @attr_grp: Attribute group to be set on device.
85 *
86 * It is the same as tty_register_device_attr except the provided @port is
87 * linked to a concrete tty specified by @index. Use this or tty_port_install
88 * (or both). Call tty_port_link_device as a last resort.
89 */
90struct device *tty_port_register_device_attr(struct tty_port *port,
91 struct tty_driver *driver, unsigned index,
92 struct device *device, void *drvdata,
93 const struct attribute_group **attr_grp)
94{
95 tty_port_link_device(port, driver, index);
96 return tty_register_device_attr(driver, index, device, drvdata,
97 attr_grp);
98}
99EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
100
9e48565d
AC
101int tty_port_alloc_xmit_buf(struct tty_port *port)
102{
103 /* We may sleep in get_zeroed_page() */
44e4909e 104 mutex_lock(&port->buf_mutex);
9e48565d
AC
105 if (port->xmit_buf == NULL)
106 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
44e4909e 107 mutex_unlock(&port->buf_mutex);
9e48565d
AC
108 if (port->xmit_buf == NULL)
109 return -ENOMEM;
110 return 0;
111}
112EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
113
114void tty_port_free_xmit_buf(struct tty_port *port)
115{
44e4909e 116 mutex_lock(&port->buf_mutex);
9e48565d
AC
117 if (port->xmit_buf != NULL) {
118 free_page((unsigned long)port->xmit_buf);
119 port->xmit_buf = NULL;
120 }
44e4909e 121 mutex_unlock(&port->buf_mutex);
9e48565d
AC
122}
123EXPORT_SYMBOL(tty_port_free_xmit_buf);
124
de274bfe
JS
125/**
126 * tty_port_destroy -- destroy inited port
127 * @port: tty port to be doestroyed
128 *
129 * When a port was initialized using tty_port_init, one has to destroy the
130 * port by this function. Either indirectly by using tty_port refcounting
131 * (tty_port_put) or directly if refcounting is not used.
132 */
133void tty_port_destroy(struct tty_port *port)
134{
135 tty_buffer_free_all(port);
136}
137EXPORT_SYMBOL(tty_port_destroy);
138
568aafc6
AC
139static void tty_port_destructor(struct kref *kref)
140{
141 struct tty_port *port = container_of(kref, struct tty_port, kref);
142 if (port->xmit_buf)
143 free_page((unsigned long)port->xmit_buf);
de274bfe 144 tty_port_destroy(port);
81c79838 145 if (port->ops && port->ops->destruct)
568aafc6
AC
146 port->ops->destruct(port);
147 else
148 kfree(port);
149}
150
151void tty_port_put(struct tty_port *port)
152{
153 if (port)
154 kref_put(&port->kref, tty_port_destructor);
155}
156EXPORT_SYMBOL(tty_port_put);
9e48565d 157
4a90f09b
AC
158/**
159 * tty_port_tty_get - get a tty reference
160 * @port: tty port
161 *
162 * Return a refcount protected tty instance or NULL if the port is not
163 * associated with a tty (eg due to close or hangup)
164 */
165
166struct tty_struct *tty_port_tty_get(struct tty_port *port)
167{
168 unsigned long flags;
169 struct tty_struct *tty;
170
171 spin_lock_irqsave(&port->lock, flags);
172 tty = tty_kref_get(port->tty);
173 spin_unlock_irqrestore(&port->lock, flags);
174 return tty;
175}
176EXPORT_SYMBOL(tty_port_tty_get);
177
178/**
179 * tty_port_tty_set - set the tty of a port
180 * @port: tty port
181 * @tty: the tty
182 *
183 * Associate the port and tty pair. Manages any internal refcounts.
184 * Pass NULL to deassociate a port
185 */
186
187void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
188{
189 unsigned long flags;
190
191 spin_lock_irqsave(&port->lock, flags);
192 if (port->tty)
193 tty_kref_put(port->tty);
cb4bca35 194 port->tty = tty_kref_get(tty);
4a90f09b
AC
195 spin_unlock_irqrestore(&port->lock, flags);
196}
197EXPORT_SYMBOL(tty_port_tty_set);
31f35939 198
957dacae 199static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
7ca0ff9a 200{
64bc3979 201 mutex_lock(&port->mutex);
8bde9658
JH
202 if (port->console)
203 goto out;
204
205 if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
957dacae
JH
206 /*
207 * Drop DTR/RTS if HUPCL is set. This causes any attached
208 * modem to hang up the line.
209 */
210 if (tty && C_HUPCL(tty))
211 tty_port_lower_dtr_rts(port);
212
8bde9658 213 if (port->ops->shutdown)
7ca0ff9a 214 port->ops->shutdown(port);
8bde9658
JH
215 }
216out:
64bc3979 217 mutex_unlock(&port->mutex);
7ca0ff9a
AC
218}
219
3e61696b
AC
220/**
221 * tty_port_hangup - hangup helper
222 * @port: tty port
223 *
224 * Perform port level tty hangup flag and count changes. Drop the tty
225 * reference.
226 */
227
228void tty_port_hangup(struct tty_port *port)
229{
957dacae 230 struct tty_struct *tty;
3e61696b
AC
231 unsigned long flags;
232
233 spin_lock_irqsave(&port->lock, flags);
234 port->count = 0;
235 port->flags &= ~ASYNC_NORMAL_ACTIVE;
957dacae
JH
236 tty = port->tty;
237 if (tty)
238 set_bit(TTY_IO_ERROR, &tty->flags);
3e61696b
AC
239 port->tty = NULL;
240 spin_unlock_irqrestore(&port->lock, flags);
957dacae
JH
241 tty_port_shutdown(port, tty);
242 tty_kref_put(tty);
3e61696b 243 wake_up_interruptible(&port->open_wait);
bdc04e31 244 wake_up_interruptible(&port->delta_msr_wait);
3e61696b
AC
245}
246EXPORT_SYMBOL(tty_port_hangup);
247
aa27a094
JS
248/**
249 * tty_port_tty_hangup - helper to hang up a tty
250 *
251 * @port: tty port
252 * @check_clocal: hang only ttys with CLOCAL unset?
253 */
254void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
255{
256 struct tty_struct *tty = tty_port_tty_get(port);
257
258 if (tty && (!check_clocal || !C_CLOCAL(tty))) {
259 tty_hangup(tty);
260 tty_kref_put(tty);
261 }
262}
263EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
264
6aad04f2
JS
265/**
266 * tty_port_tty_wakeup - helper to wake up a tty
267 *
268 * @port: tty port
269 */
270void tty_port_tty_wakeup(struct tty_port *port)
271{
272 struct tty_struct *tty = tty_port_tty_get(port);
273
274 if (tty) {
275 tty_wakeup(tty);
276 tty_kref_put(tty);
277 }
278}
279EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
280
31f35939
AC
281/**
282 * tty_port_carrier_raised - carrier raised check
283 * @port: tty port
284 *
285 * Wrapper for the carrier detect logic. For the moment this is used
286 * to hide some internal details. This will eventually become entirely
287 * internal to the tty port.
288 */
289
290int tty_port_carrier_raised(struct tty_port *port)
291{
292 if (port->ops->carrier_raised == NULL)
293 return 1;
294 return port->ops->carrier_raised(port);
295}
296EXPORT_SYMBOL(tty_port_carrier_raised);
5d951fb4
AC
297
298/**
fcc8ac18 299 * tty_port_raise_dtr_rts - Raise DTR/RTS
5d951fb4
AC
300 * @port: tty port
301 *
302 * Wrapper for the DTR/RTS raise logic. For the moment this is used
303 * to hide some internal details. This will eventually become entirely
304 * internal to the tty port.
305 */
306
307void tty_port_raise_dtr_rts(struct tty_port *port)
308{
fcc8ac18
AC
309 if (port->ops->dtr_rts)
310 port->ops->dtr_rts(port, 1);
5d951fb4
AC
311}
312EXPORT_SYMBOL(tty_port_raise_dtr_rts);
36c621d8 313
fcc8ac18
AC
314/**
315 * tty_port_lower_dtr_rts - Lower DTR/RTS
316 * @port: tty port
317 *
318 * Wrapper for the DTR/RTS raise logic. For the moment this is used
319 * to hide some internal details. This will eventually become entirely
320 * internal to the tty port.
321 */
322
323void tty_port_lower_dtr_rts(struct tty_port *port)
324{
325 if (port->ops->dtr_rts)
326 port->ops->dtr_rts(port, 0);
327}
328EXPORT_SYMBOL(tty_port_lower_dtr_rts);
329
36c621d8
AC
330/**
331 * tty_port_block_til_ready - Waiting logic for tty open
332 * @port: the tty port being opened
333 * @tty: the tty device being bound
334 * @filp: the file pointer of the opener
335 *
336 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
337 * Handles:
338 * - hangup (both before and during)
339 * - non blocking open
340 * - rts/dtr/dcd
341 * - signals
342 * - port flags and counts
343 *
344 * The passed tty_port must implement the carrier_raised method if it can
fcc8ac18 345 * do carrier detect and the dtr_rts method if it supports software
36c621d8
AC
346 * management of these lines. Note that the dtr/rts raise is done each
347 * iteration as a hangup may have previously dropped them while we wait.
348 */
d774a56d 349
36c621d8
AC
350int tty_port_block_til_ready(struct tty_port *port,
351 struct tty_struct *tty, struct file *filp)
352{
353 int do_clocal = 0, retval;
354 unsigned long flags;
6af9a43d 355 DEFINE_WAIT(wait);
36c621d8
AC
356
357 /* block if port is in the process of being closed */
358 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
89c8d91e 359 wait_event_interruptible_tty(tty, port->close_wait,
5fc5b42a 360 !(port->flags & ASYNC_CLOSING));
36c621d8
AC
361 if (port->flags & ASYNC_HUP_NOTIFY)
362 return -EAGAIN;
363 else
364 return -ERESTARTSYS;
365 }
366
367 /* if non-blocking mode is set we can pass directly to open unless
368 the port has just hung up or is in another error state */
8627b96d
AC
369 if (tty->flags & (1 << TTY_IO_ERROR)) {
370 port->flags |= ASYNC_NORMAL_ACTIVE;
371 return 0;
372 }
373 if (filp->f_flags & O_NONBLOCK) {
4175f3e3 374 /* Indicate we are open */
adc8d746 375 if (tty->termios.c_cflag & CBAUD)
4175f3e3 376 tty_port_raise_dtr_rts(port);
36c621d8
AC
377 port->flags |= ASYNC_NORMAL_ACTIVE;
378 return 0;
379 }
380
381 if (C_CLOCAL(tty))
382 do_clocal = 1;
383
384 /* Block waiting until we can proceed. We may need to wait for the
385 carrier, but we must also wait for any close that is in progress
386 before the next open may complete */
387
388 retval = 0;
36c621d8
AC
389
390 /* The port lock protects the port counts */
391 spin_lock_irqsave(&port->lock, flags);
392 if (!tty_hung_up_p(filp))
393 port->count--;
394 port->blocked_open++;
395 spin_unlock_irqrestore(&port->lock, flags);
396
397 while (1) {
398 /* Indicate we are open */
e584a02c 399 if (C_BAUD(tty) && test_bit(ASYNCB_INITIALIZED, &port->flags))
7834909f 400 tty_port_raise_dtr_rts(port);
36c621d8 401
3e3b5c08 402 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
d774a56d
AC
403 /* Check for a hangup or uninitialised port.
404 Return accordingly */
36c621d8
AC
405 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
406 if (port->flags & ASYNC_HUP_NOTIFY)
407 retval = -EAGAIN;
408 else
409 retval = -ERESTARTSYS;
410 break;
411 }
0eee50af
JS
412 /*
413 * Probe the carrier. For devices with no carrier detect
414 * tty_port_carrier_raised will always return true.
415 * Never ask drivers if CLOCAL is set, this causes troubles
416 * on some hardware.
417 */
36c621d8 418 if (!(port->flags & ASYNC_CLOSING) &&
0eee50af 419 (do_clocal || tty_port_carrier_raised(port)))
36c621d8
AC
420 break;
421 if (signal_pending(current)) {
422 retval = -ERESTARTSYS;
423 break;
424 }
89c8d91e 425 tty_unlock(tty);
36c621d8 426 schedule();
89c8d91e 427 tty_lock(tty);
36c621d8 428 }
3e3b5c08 429 finish_wait(&port->open_wait, &wait);
36c621d8
AC
430
431 /* Update counts. A parallel hangup will have set count to zero and
432 we must not mess that up further */
433 spin_lock_irqsave(&port->lock, flags);
434 if (!tty_hung_up_p(filp))
435 port->count++;
436 port->blocked_open--;
437 if (retval == 0)
438 port->flags |= ASYNC_NORMAL_ACTIVE;
439 spin_unlock_irqrestore(&port->lock, flags);
ecc2e05e 440 return retval;
36c621d8
AC
441}
442EXPORT_SYMBOL(tty_port_block_til_ready);
443
b74414f5
JH
444static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
445{
446 unsigned int bps = tty_get_baud_rate(tty);
447 long timeout;
448
449 if (bps > 1200) {
450 timeout = (HZ * 10 * port->drain_delay) / bps;
451 timeout = max_t(long, timeout, HZ / 10);
452 } else {
453 timeout = 2 * HZ;
454 }
455 schedule_timeout_interruptible(timeout);
456}
457
d774a56d
AC
458int tty_port_close_start(struct tty_port *port,
459 struct tty_struct *tty, struct file *filp)
a6614999
AC
460{
461 unsigned long flags;
462
463 spin_lock_irqsave(&port->lock, flags);
464 if (tty_hung_up_p(filp)) {
465 spin_unlock_irqrestore(&port->lock, flags);
466 return 0;
467 }
468
d774a56d 469 if (tty->count == 1 && port->count != 1) {
a6614999
AC
470 printk(KERN_WARNING
471 "tty_port_close_start: tty->count = 1 port count = %d.\n",
472 port->count);
473 port->count = 1;
474 }
475 if (--port->count < 0) {
476 printk(KERN_WARNING "tty_port_close_start: count = %d\n",
477 port->count);
478 port->count = 0;
479 }
480
481 if (port->count) {
482 spin_unlock_irqrestore(&port->lock, flags);
7ca0ff9a
AC
483 if (port->ops->drop)
484 port->ops->drop(port);
a6614999
AC
485 return 0;
486 }
1f5c13fa 487 set_bit(ASYNCB_CLOSING, &port->flags);
a6614999
AC
488 tty->closing = 1;
489 spin_unlock_irqrestore(&port->lock, flags);
fba85e01
AC
490 /* Don't block on a stalled port, just pull the chain */
491 if (tty->flow_stopped)
492 tty_driver_flush_buffer(tty);
7ca0ff9a 493 if (test_bit(ASYNCB_INITIALIZED, &port->flags) &&
6ed1dbae 494 port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
424cc039 495 tty_wait_until_sent_from_close(tty, port->closing_wait);
b74414f5
JH
496 if (port->drain_delay)
497 tty_port_drain_delay(port, tty);
e707c35c
AC
498 /* Flush the ldisc buffering */
499 tty_ldisc_flush(tty);
500
7ca0ff9a
AC
501 /* Don't call port->drop for the last reference. Callers will want
502 to drop the last active reference in ->shutdown() or the tty
503 shutdown path */
a6614999
AC
504 return 1;
505}
506EXPORT_SYMBOL(tty_port_close_start);
507
508void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
509{
510 unsigned long flags;
511
a6614999
AC
512 spin_lock_irqsave(&port->lock, flags);
513 tty->closing = 0;
514
515 if (port->blocked_open) {
516 spin_unlock_irqrestore(&port->lock, flags);
517 if (port->close_delay) {
518 msleep_interruptible(
519 jiffies_to_msecs(port->close_delay));
520 }
521 spin_lock_irqsave(&port->lock, flags);
522 wake_up_interruptible(&port->open_wait);
523 }
524 port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
525 wake_up_interruptible(&port->close_wait);
526 spin_unlock_irqrestore(&port->lock, flags);
527}
528EXPORT_SYMBOL(tty_port_close_end);
7ca0ff9a
AC
529
530void tty_port_close(struct tty_port *port, struct tty_struct *tty,
531 struct file *filp)
532{
533 if (tty_port_close_start(port, tty, filp) == 0)
534 return;
957dacae 535 tty_port_shutdown(port, tty);
d74e8286 536 set_bit(TTY_IO_ERROR, &tty->flags);
7ca0ff9a
AC
537 tty_port_close_end(port, tty);
538 tty_port_tty_set(port, NULL);
539}
540EXPORT_SYMBOL(tty_port_close);
64bc3979 541
72a33bf5
JS
542/**
543 * tty_port_install - generic tty->ops->install handler
544 * @port: tty_port of the device
545 * @driver: tty_driver for this device
546 * @tty: tty to be installed
547 *
548 * It is the same as tty_standard_install except the provided @port is linked
549 * to a concrete tty specified by @tty. Use this or tty_port_register_device
550 * (or both). Call tty_port_link_device as a last resort.
551 */
695586ca
JS
552int tty_port_install(struct tty_port *port, struct tty_driver *driver,
553 struct tty_struct *tty)
554{
555 tty->port = port;
556 return tty_standard_install(driver, tty);
557}
558EXPORT_SYMBOL_GPL(tty_port_install);
559
64bc3979 560int tty_port_open(struct tty_port *port, struct tty_struct *tty,
d774a56d 561 struct file *filp)
64bc3979
AC
562{
563 spin_lock_irq(&port->lock);
564 if (!tty_hung_up_p(filp))
565 ++port->count;
566 spin_unlock_irq(&port->lock);
567 tty_port_tty_set(port, tty);
568
569 /*
570 * Do the device-specific open only if the hardware isn't
571 * already initialized. Serialize open and shutdown using the
572 * port mutex.
573 */
574
575 mutex_lock(&port->mutex);
576
577 if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
a9a37ec3 578 clear_bit(TTY_IO_ERROR, &tty->flags);
64bc3979
AC
579 if (port->ops->activate) {
580 int retval = port->ops->activate(port, tty);
581 if (retval) {
d774a56d
AC
582 mutex_unlock(&port->mutex);
583 return retval;
584 }
585 }
64bc3979
AC
586 set_bit(ASYNCB_INITIALIZED, &port->flags);
587 }
588 mutex_unlock(&port->mutex);
589 return tty_port_block_til_ready(port, tty, filp);
590}
591
592EXPORT_SYMBOL(tty_port_open);
This page took 0.381428 seconds and 5 git commands to generate.