]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/tty/tty_port.c
Merge git://git.infradead.org/users/eparis/audit
[mirror_ubuntu-artful-kernel.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{
4f98d467 135 cancel_work_sync(&port->buf.work);
de274bfe
JS
136 tty_buffer_free_all(port);
137}
138EXPORT_SYMBOL(tty_port_destroy);
139
568aafc6
AC
140static void tty_port_destructor(struct kref *kref)
141{
142 struct tty_port *port = container_of(kref, struct tty_port, kref);
e3bfea23
PH
143
144 /* check if last port ref was dropped before tty release */
145 if (WARN_ON(port->itty))
146 return;
568aafc6
AC
147 if (port->xmit_buf)
148 free_page((unsigned long)port->xmit_buf);
de274bfe 149 tty_port_destroy(port);
81c79838 150 if (port->ops && port->ops->destruct)
568aafc6
AC
151 port->ops->destruct(port);
152 else
153 kfree(port);
154}
155
156void tty_port_put(struct tty_port *port)
157{
158 if (port)
159 kref_put(&port->kref, tty_port_destructor);
160}
161EXPORT_SYMBOL(tty_port_put);
9e48565d 162
4a90f09b
AC
163/**
164 * tty_port_tty_get - get a tty reference
165 * @port: tty port
166 *
167 * Return a refcount protected tty instance or NULL if the port is not
168 * associated with a tty (eg due to close or hangup)
169 */
170
171struct tty_struct *tty_port_tty_get(struct tty_port *port)
172{
173 unsigned long flags;
174 struct tty_struct *tty;
175
176 spin_lock_irqsave(&port->lock, flags);
177 tty = tty_kref_get(port->tty);
178 spin_unlock_irqrestore(&port->lock, flags);
179 return tty;
180}
181EXPORT_SYMBOL(tty_port_tty_get);
182
183/**
184 * tty_port_tty_set - set the tty of a port
185 * @port: tty port
186 * @tty: the tty
187 *
188 * Associate the port and tty pair. Manages any internal refcounts.
189 * Pass NULL to deassociate a port
190 */
191
192void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
193{
194 unsigned long flags;
195
196 spin_lock_irqsave(&port->lock, flags);
197 if (port->tty)
198 tty_kref_put(port->tty);
cb4bca35 199 port->tty = tty_kref_get(tty);
4a90f09b
AC
200 spin_unlock_irqrestore(&port->lock, flags);
201}
202EXPORT_SYMBOL(tty_port_tty_set);
31f35939 203
957dacae 204static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
7ca0ff9a 205{
64bc3979 206 mutex_lock(&port->mutex);
8bde9658
JH
207 if (port->console)
208 goto out;
209
210 if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
957dacae
JH
211 /*
212 * Drop DTR/RTS if HUPCL is set. This causes any attached
213 * modem to hang up the line.
214 */
215 if (tty && C_HUPCL(tty))
216 tty_port_lower_dtr_rts(port);
217
8bde9658 218 if (port->ops->shutdown)
7ca0ff9a 219 port->ops->shutdown(port);
8bde9658
JH
220 }
221out:
64bc3979 222 mutex_unlock(&port->mutex);
7ca0ff9a
AC
223}
224
3e61696b
AC
225/**
226 * tty_port_hangup - hangup helper
227 * @port: tty port
228 *
229 * Perform port level tty hangup flag and count changes. Drop the tty
230 * reference.
231 */
232
233void tty_port_hangup(struct tty_port *port)
234{
957dacae 235 struct tty_struct *tty;
3e61696b
AC
236 unsigned long flags;
237
238 spin_lock_irqsave(&port->lock, flags);
239 port->count = 0;
240 port->flags &= ~ASYNC_NORMAL_ACTIVE;
957dacae
JH
241 tty = port->tty;
242 if (tty)
243 set_bit(TTY_IO_ERROR, &tty->flags);
3e61696b
AC
244 port->tty = NULL;
245 spin_unlock_irqrestore(&port->lock, flags);
957dacae
JH
246 tty_port_shutdown(port, tty);
247 tty_kref_put(tty);
3e61696b 248 wake_up_interruptible(&port->open_wait);
bdc04e31 249 wake_up_interruptible(&port->delta_msr_wait);
3e61696b
AC
250}
251EXPORT_SYMBOL(tty_port_hangup);
252
aa27a094
JS
253/**
254 * tty_port_tty_hangup - helper to hang up a tty
255 *
256 * @port: tty port
257 * @check_clocal: hang only ttys with CLOCAL unset?
258 */
259void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
260{
261 struct tty_struct *tty = tty_port_tty_get(port);
262
1d9e689c 263 if (tty && (!check_clocal || !C_CLOCAL(tty)))
aa27a094 264 tty_hangup(tty);
1d9e689c 265 tty_kref_put(tty);
aa27a094
JS
266}
267EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
268
6aad04f2
JS
269/**
270 * tty_port_tty_wakeup - helper to wake up a tty
271 *
272 * @port: tty port
273 */
274void tty_port_tty_wakeup(struct tty_port *port)
275{
276 struct tty_struct *tty = tty_port_tty_get(port);
277
278 if (tty) {
279 tty_wakeup(tty);
280 tty_kref_put(tty);
281 }
282}
283EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
284
31f35939
AC
285/**
286 * tty_port_carrier_raised - carrier raised check
287 * @port: tty port
288 *
289 * Wrapper for the carrier detect logic. For the moment this is used
290 * to hide some internal details. This will eventually become entirely
291 * internal to the tty port.
292 */
293
294int tty_port_carrier_raised(struct tty_port *port)
295{
296 if (port->ops->carrier_raised == NULL)
297 return 1;
298 return port->ops->carrier_raised(port);
299}
300EXPORT_SYMBOL(tty_port_carrier_raised);
5d951fb4
AC
301
302/**
fcc8ac18 303 * tty_port_raise_dtr_rts - Raise DTR/RTS
5d951fb4
AC
304 * @port: tty port
305 *
306 * Wrapper for the DTR/RTS raise logic. For the moment this is used
307 * to hide some internal details. This will eventually become entirely
308 * internal to the tty port.
309 */
310
311void tty_port_raise_dtr_rts(struct tty_port *port)
312{
fcc8ac18
AC
313 if (port->ops->dtr_rts)
314 port->ops->dtr_rts(port, 1);
5d951fb4
AC
315}
316EXPORT_SYMBOL(tty_port_raise_dtr_rts);
36c621d8 317
fcc8ac18
AC
318/**
319 * tty_port_lower_dtr_rts - Lower DTR/RTS
320 * @port: tty port
321 *
322 * Wrapper for the DTR/RTS raise logic. For the moment this is used
323 * to hide some internal details. This will eventually become entirely
324 * internal to the tty port.
325 */
326
327void tty_port_lower_dtr_rts(struct tty_port *port)
328{
329 if (port->ops->dtr_rts)
330 port->ops->dtr_rts(port, 0);
331}
332EXPORT_SYMBOL(tty_port_lower_dtr_rts);
333
36c621d8
AC
334/**
335 * tty_port_block_til_ready - Waiting logic for tty open
336 * @port: the tty port being opened
337 * @tty: the tty device being bound
338 * @filp: the file pointer of the opener
339 *
340 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
341 * Handles:
342 * - hangup (both before and during)
343 * - non blocking open
344 * - rts/dtr/dcd
345 * - signals
346 * - port flags and counts
347 *
348 * The passed tty_port must implement the carrier_raised method if it can
fcc8ac18 349 * do carrier detect and the dtr_rts method if it supports software
36c621d8
AC
350 * management of these lines. Note that the dtr/rts raise is done each
351 * iteration as a hangup may have previously dropped them while we wait.
352 */
d774a56d 353
36c621d8
AC
354int tty_port_block_til_ready(struct tty_port *port,
355 struct tty_struct *tty, struct file *filp)
356{
357 int do_clocal = 0, retval;
358 unsigned long flags;
6af9a43d 359 DEFINE_WAIT(wait);
36c621d8
AC
360
361 /* block if port is in the process of being closed */
362 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
89c8d91e 363 wait_event_interruptible_tty(tty, port->close_wait,
5fc5b42a 364 !(port->flags & ASYNC_CLOSING));
36c621d8
AC
365 if (port->flags & ASYNC_HUP_NOTIFY)
366 return -EAGAIN;
367 else
368 return -ERESTARTSYS;
369 }
370
371 /* if non-blocking mode is set we can pass directly to open unless
372 the port has just hung up or is in another error state */
8627b96d
AC
373 if (tty->flags & (1 << TTY_IO_ERROR)) {
374 port->flags |= ASYNC_NORMAL_ACTIVE;
375 return 0;
376 }
377 if (filp->f_flags & O_NONBLOCK) {
4175f3e3 378 /* Indicate we are open */
adc8d746 379 if (tty->termios.c_cflag & CBAUD)
4175f3e3 380 tty_port_raise_dtr_rts(port);
36c621d8
AC
381 port->flags |= ASYNC_NORMAL_ACTIVE;
382 return 0;
383 }
384
385 if (C_CLOCAL(tty))
386 do_clocal = 1;
387
388 /* Block waiting until we can proceed. We may need to wait for the
389 carrier, but we must also wait for any close that is in progress
390 before the next open may complete */
391
392 retval = 0;
36c621d8
AC
393
394 /* The port lock protects the port counts */
395 spin_lock_irqsave(&port->lock, flags);
396 if (!tty_hung_up_p(filp))
397 port->count--;
398 port->blocked_open++;
399 spin_unlock_irqrestore(&port->lock, flags);
400
401 while (1) {
402 /* Indicate we are open */
e584a02c 403 if (C_BAUD(tty) && test_bit(ASYNCB_INITIALIZED, &port->flags))
7834909f 404 tty_port_raise_dtr_rts(port);
36c621d8 405
3e3b5c08 406 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
d774a56d
AC
407 /* Check for a hangup or uninitialised port.
408 Return accordingly */
36c621d8
AC
409 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
410 if (port->flags & ASYNC_HUP_NOTIFY)
411 retval = -EAGAIN;
412 else
413 retval = -ERESTARTSYS;
414 break;
415 }
0eee50af
JS
416 /*
417 * Probe the carrier. For devices with no carrier detect
418 * tty_port_carrier_raised will always return true.
419 * Never ask drivers if CLOCAL is set, this causes troubles
420 * on some hardware.
421 */
36c621d8 422 if (!(port->flags & ASYNC_CLOSING) &&
0eee50af 423 (do_clocal || tty_port_carrier_raised(port)))
36c621d8
AC
424 break;
425 if (signal_pending(current)) {
426 retval = -ERESTARTSYS;
427 break;
428 }
89c8d91e 429 tty_unlock(tty);
36c621d8 430 schedule();
89c8d91e 431 tty_lock(tty);
36c621d8 432 }
3e3b5c08 433 finish_wait(&port->open_wait, &wait);
36c621d8
AC
434
435 /* Update counts. A parallel hangup will have set count to zero and
436 we must not mess that up further */
437 spin_lock_irqsave(&port->lock, flags);
438 if (!tty_hung_up_p(filp))
439 port->count++;
440 port->blocked_open--;
441 if (retval == 0)
442 port->flags |= ASYNC_NORMAL_ACTIVE;
443 spin_unlock_irqrestore(&port->lock, flags);
ecc2e05e 444 return retval;
36c621d8
AC
445}
446EXPORT_SYMBOL(tty_port_block_til_ready);
447
b74414f5
JH
448static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
449{
450 unsigned int bps = tty_get_baud_rate(tty);
451 long timeout;
452
453 if (bps > 1200) {
454 timeout = (HZ * 10 * port->drain_delay) / bps;
455 timeout = max_t(long, timeout, HZ / 10);
456 } else {
457 timeout = 2 * HZ;
458 }
459 schedule_timeout_interruptible(timeout);
460}
461
d774a56d
AC
462int tty_port_close_start(struct tty_port *port,
463 struct tty_struct *tty, struct file *filp)
a6614999
AC
464{
465 unsigned long flags;
466
467 spin_lock_irqsave(&port->lock, flags);
468 if (tty_hung_up_p(filp)) {
469 spin_unlock_irqrestore(&port->lock, flags);
470 return 0;
471 }
472
d774a56d 473 if (tty->count == 1 && port->count != 1) {
a6614999
AC
474 printk(KERN_WARNING
475 "tty_port_close_start: tty->count = 1 port count = %d.\n",
476 port->count);
477 port->count = 1;
478 }
479 if (--port->count < 0) {
480 printk(KERN_WARNING "tty_port_close_start: count = %d\n",
481 port->count);
482 port->count = 0;
483 }
484
485 if (port->count) {
486 spin_unlock_irqrestore(&port->lock, flags);
487 return 0;
488 }
1f5c13fa 489 set_bit(ASYNCB_CLOSING, &port->flags);
a6614999
AC
490 tty->closing = 1;
491 spin_unlock_irqrestore(&port->lock, flags);
0b2588ca
JH
492
493 if (test_bit(ASYNCB_INITIALIZED, &port->flags)) {
494 /* Don't block on a stalled port, just pull the chain */
495 if (tty->flow_stopped)
496 tty_driver_flush_buffer(tty);
497 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
498 tty_wait_until_sent_from_close(tty, port->closing_wait);
499 if (port->drain_delay)
500 tty_port_drain_delay(port, tty);
501 }
e707c35c
AC
502 /* Flush the ldisc buffering */
503 tty_ldisc_flush(tty);
504
469d6d06 505 /* Report to caller this is the last port reference */
a6614999
AC
506 return 1;
507}
508EXPORT_SYMBOL(tty_port_close_start);
509
510void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
511{
512 unsigned long flags;
513
a6614999
AC
514 spin_lock_irqsave(&port->lock, flags);
515 tty->closing = 0;
516
517 if (port->blocked_open) {
518 spin_unlock_irqrestore(&port->lock, flags);
519 if (port->close_delay) {
520 msleep_interruptible(
521 jiffies_to_msecs(port->close_delay));
522 }
523 spin_lock_irqsave(&port->lock, flags);
524 wake_up_interruptible(&port->open_wait);
525 }
526 port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
527 wake_up_interruptible(&port->close_wait);
528 spin_unlock_irqrestore(&port->lock, flags);
529}
530EXPORT_SYMBOL(tty_port_close_end);
7ca0ff9a
AC
531
532void tty_port_close(struct tty_port *port, struct tty_struct *tty,
533 struct file *filp)
534{
535 if (tty_port_close_start(port, tty, filp) == 0)
536 return;
957dacae 537 tty_port_shutdown(port, tty);
d74e8286 538 set_bit(TTY_IO_ERROR, &tty->flags);
7ca0ff9a
AC
539 tty_port_close_end(port, tty);
540 tty_port_tty_set(port, NULL);
541}
542EXPORT_SYMBOL(tty_port_close);
64bc3979 543
72a33bf5
JS
544/**
545 * tty_port_install - generic tty->ops->install handler
546 * @port: tty_port of the device
547 * @driver: tty_driver for this device
548 * @tty: tty to be installed
549 *
550 * It is the same as tty_standard_install except the provided @port is linked
551 * to a concrete tty specified by @tty. Use this or tty_port_register_device
552 * (or both). Call tty_port_link_device as a last resort.
553 */
695586ca
JS
554int tty_port_install(struct tty_port *port, struct tty_driver *driver,
555 struct tty_struct *tty)
556{
557 tty->port = port;
558 return tty_standard_install(driver, tty);
559}
560EXPORT_SYMBOL_GPL(tty_port_install);
561
64bc3979 562int tty_port_open(struct tty_port *port, struct tty_struct *tty,
d774a56d 563 struct file *filp)
64bc3979
AC
564{
565 spin_lock_irq(&port->lock);
566 if (!tty_hung_up_p(filp))
567 ++port->count;
568 spin_unlock_irq(&port->lock);
569 tty_port_tty_set(port, tty);
570
571 /*
572 * Do the device-specific open only if the hardware isn't
573 * already initialized. Serialize open and shutdown using the
574 * port mutex.
575 */
576
577 mutex_lock(&port->mutex);
578
579 if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
a9a37ec3 580 clear_bit(TTY_IO_ERROR, &tty->flags);
64bc3979
AC
581 if (port->ops->activate) {
582 int retval = port->ops->activate(port, tty);
583 if (retval) {
d774a56d
AC
584 mutex_unlock(&port->mutex);
585 return retval;
586 }
587 }
64bc3979
AC
588 set_bit(ASYNCB_INITIALIZED, &port->flags);
589 }
590 mutex_unlock(&port->mutex);
591 return tty_port_block_til_ready(port, tty, filp);
592}
593
594EXPORT_SYMBOL(tty_port_open);