]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/tty/tty_port.c
Merge tag 'sunxi-fixes-for-4.13-3' of https://git.kernel.org/pub/scm/linux/kernel...
[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>
174cd4b1 14#include <linux/sched/signal.h>
9e48565d
AC
15#include <linux/wait.h>
16#include <linux/bitops.h>
17#include <linux/delay.h>
18#include <linux/module.h>
8cde11b2 19#include <linux/serdev.h>
9e48565d 20
c3485ee0
RH
21static int tty_port_default_receive_buf(struct tty_port *port,
22 const unsigned char *p,
23 const unsigned char *f, size_t count)
24{
25 int ret;
26 struct tty_struct *tty;
27 struct tty_ldisc *disc;
28
29 tty = READ_ONCE(port->itty);
30 if (!tty)
31 return 0;
32
33 disc = tty_ldisc_ref(tty);
34 if (!disc)
35 return 0;
36
37 ret = tty_ldisc_receive_buf(disc, p, (char *)f, count);
38
39 tty_ldisc_deref(disc);
40
41 return ret;
42}
43
44static void tty_port_default_wakeup(struct tty_port *port)
45{
46 struct tty_struct *tty = tty_port_tty_get(port);
47
48 if (tty) {
49 tty_wakeup(tty);
50 tty_kref_put(tty);
51 }
52}
53
54static const struct tty_port_client_operations default_client_ops = {
55 .receive_buf = tty_port_default_receive_buf,
56 .write_wakeup = tty_port_default_wakeup,
57};
58
9e48565d
AC
59void tty_port_init(struct tty_port *port)
60{
61 memset(port, 0, sizeof(*port));
ecbbfd44 62 tty_buffer_init(port);
9e48565d 63 init_waitqueue_head(&port->open_wait);
bdc04e31 64 init_waitqueue_head(&port->delta_msr_wait);
9e48565d 65 mutex_init(&port->mutex);
44e4909e 66 mutex_init(&port->buf_mutex);
4a90f09b 67 spin_lock_init(&port->lock);
9e48565d
AC
68 port->close_delay = (50 * HZ) / 100;
69 port->closing_wait = (3000 * HZ) / 100;
c3485ee0 70 port->client_ops = &default_client_ops;
568aafc6 71 kref_init(&port->kref);
9e48565d
AC
72}
73EXPORT_SYMBOL(tty_port_init);
74
2cb4ca02
JS
75/**
76 * tty_port_link_device - link tty and tty_port
77 * @port: tty_port of the device
78 * @driver: tty_driver for this device
79 * @index: index of the tty
80 *
81 * Provide the tty layer wit ha link from a tty (specified by @index) to a
82 * tty_port (@port). Use this only if neither tty_port_register_device nor
83 * tty_port_install is used in the driver. If used, this has to be called before
84 * tty_register_driver.
85 */
86void tty_port_link_device(struct tty_port *port,
87 struct tty_driver *driver, unsigned index)
88{
89 if (WARN_ON(index >= driver->num))
90 return;
91 driver->ports[index] = port;
92}
93EXPORT_SYMBOL_GPL(tty_port_link_device);
94
72a33bf5
JS
95/**
96 * tty_port_register_device - register tty device
97 * @port: tty_port of the device
98 * @driver: tty_driver for this device
99 * @index: index of the tty
100 * @device: parent if exists, otherwise NULL
101 *
102 * It is the same as tty_register_device except the provided @port is linked to
103 * a concrete tty specified by @index. Use this or tty_port_install (or both).
104 * Call tty_port_link_device as a last resort.
105 */
057eb856
JS
106struct device *tty_port_register_device(struct tty_port *port,
107 struct tty_driver *driver, unsigned index,
108 struct device *device)
109{
3086365d 110 return tty_port_register_device_attr(port, driver, index, device, NULL, NULL);
057eb856
JS
111}
112EXPORT_SYMBOL_GPL(tty_port_register_device);
113
b1b79916
TH
114/**
115 * tty_port_register_device_attr - register tty device
116 * @port: tty_port of the device
117 * @driver: tty_driver for this device
118 * @index: index of the tty
119 * @device: parent if exists, otherwise NULL
120 * @drvdata: Driver data to be set to device.
121 * @attr_grp: Attribute group to be set on device.
122 *
123 * It is the same as tty_register_device_attr except the provided @port is
124 * linked to a concrete tty specified by @index. Use this or tty_port_install
125 * (or both). Call tty_port_link_device as a last resort.
126 */
127struct device *tty_port_register_device_attr(struct tty_port *port,
128 struct tty_driver *driver, unsigned index,
129 struct device *device, void *drvdata,
130 const struct attribute_group **attr_grp)
131{
132 tty_port_link_device(port, driver, index);
133 return tty_register_device_attr(driver, index, device, drvdata,
134 attr_grp);
135}
136EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
137
8cde11b2
JH
138/**
139 * tty_port_register_device_attr_serdev - register tty or serdev device
140 * @port: tty_port of the device
141 * @driver: tty_driver for this device
142 * @index: index of the tty
143 * @device: parent if exists, otherwise NULL
144 * @drvdata: driver data for the device
145 * @attr_grp: attribute group for the device
146 *
147 * Register a serdev or tty device depending on if the parent device has any
148 * defined serdev clients or not.
149 */
150struct device *tty_port_register_device_attr_serdev(struct tty_port *port,
151 struct tty_driver *driver, unsigned index,
152 struct device *device, void *drvdata,
153 const struct attribute_group **attr_grp)
154{
155 struct device *dev;
156
157 tty_port_link_device(port, driver, index);
158
159 dev = serdev_tty_port_register(port, device, driver, index);
160 if (PTR_ERR(dev) != -ENODEV) {
161 /* Skip creating cdev if we registered a serdev device */
162 return dev;
163 }
164
165 return tty_register_device_attr(driver, index, device, drvdata,
166 attr_grp);
167}
168EXPORT_SYMBOL_GPL(tty_port_register_device_attr_serdev);
169
170/**
171 * tty_port_register_device_serdev - register tty or serdev device
172 * @port: tty_port of the device
173 * @driver: tty_driver for this device
174 * @index: index of the tty
175 * @device: parent if exists, otherwise NULL
176 *
177 * Register a serdev or tty device depending on if the parent device has any
178 * defined serdev clients or not.
179 */
180struct device *tty_port_register_device_serdev(struct tty_port *port,
181 struct tty_driver *driver, unsigned index,
182 struct device *device)
183{
184 return tty_port_register_device_attr_serdev(port, driver, index,
185 device, NULL, NULL);
186}
187EXPORT_SYMBOL_GPL(tty_port_register_device_serdev);
188
189/**
190 * tty_port_unregister_device - deregister a tty or serdev device
191 * @port: tty_port of the device
192 * @driver: tty_driver for this device
193 * @index: index of the tty
194 *
195 * If a tty or serdev device is registered with a call to
196 * tty_port_register_device_serdev() then this function must be called when
197 * the device is gone.
198 */
199void tty_port_unregister_device(struct tty_port *port,
200 struct tty_driver *driver, unsigned index)
201{
202 int ret;
203
204 ret = serdev_tty_port_unregister(port);
205 if (ret == 0)
206 return;
207
208 tty_unregister_device(driver, index);
209}
210EXPORT_SYMBOL_GPL(tty_port_unregister_device);
211
9e48565d
AC
212int tty_port_alloc_xmit_buf(struct tty_port *port)
213{
214 /* We may sleep in get_zeroed_page() */
44e4909e 215 mutex_lock(&port->buf_mutex);
9e48565d
AC
216 if (port->xmit_buf == NULL)
217 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
44e4909e 218 mutex_unlock(&port->buf_mutex);
9e48565d
AC
219 if (port->xmit_buf == NULL)
220 return -ENOMEM;
221 return 0;
222}
223EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
224
225void tty_port_free_xmit_buf(struct tty_port *port)
226{
44e4909e 227 mutex_lock(&port->buf_mutex);
9e48565d
AC
228 if (port->xmit_buf != NULL) {
229 free_page((unsigned long)port->xmit_buf);
230 port->xmit_buf = NULL;
231 }
44e4909e 232 mutex_unlock(&port->buf_mutex);
9e48565d
AC
233}
234EXPORT_SYMBOL(tty_port_free_xmit_buf);
235
de274bfe
JS
236/**
237 * tty_port_destroy -- destroy inited port
238 * @port: tty port to be doestroyed
239 *
240 * When a port was initialized using tty_port_init, one has to destroy the
241 * port by this function. Either indirectly by using tty_port refcounting
242 * (tty_port_put) or directly if refcounting is not used.
243 */
244void tty_port_destroy(struct tty_port *port)
245{
e176058f 246 tty_buffer_cancel_work(port);
de274bfe
JS
247 tty_buffer_free_all(port);
248}
249EXPORT_SYMBOL(tty_port_destroy);
250
568aafc6
AC
251static void tty_port_destructor(struct kref *kref)
252{
253 struct tty_port *port = container_of(kref, struct tty_port, kref);
e3bfea23
PH
254
255 /* check if last port ref was dropped before tty release */
256 if (WARN_ON(port->itty))
257 return;
568aafc6
AC
258 if (port->xmit_buf)
259 free_page((unsigned long)port->xmit_buf);
de274bfe 260 tty_port_destroy(port);
81c79838 261 if (port->ops && port->ops->destruct)
568aafc6
AC
262 port->ops->destruct(port);
263 else
264 kfree(port);
265}
266
267void tty_port_put(struct tty_port *port)
268{
269 if (port)
270 kref_put(&port->kref, tty_port_destructor);
271}
272EXPORT_SYMBOL(tty_port_put);
9e48565d 273
4a90f09b
AC
274/**
275 * tty_port_tty_get - get a tty reference
276 * @port: tty port
277 *
278 * Return a refcount protected tty instance or NULL if the port is not
279 * associated with a tty (eg due to close or hangup)
280 */
281
282struct tty_struct *tty_port_tty_get(struct tty_port *port)
283{
284 unsigned long flags;
285 struct tty_struct *tty;
286
287 spin_lock_irqsave(&port->lock, flags);
288 tty = tty_kref_get(port->tty);
289 spin_unlock_irqrestore(&port->lock, flags);
290 return tty;
291}
292EXPORT_SYMBOL(tty_port_tty_get);
293
294/**
295 * tty_port_tty_set - set the tty of a port
296 * @port: tty port
297 * @tty: the tty
298 *
299 * Associate the port and tty pair. Manages any internal refcounts.
300 * Pass NULL to deassociate a port
301 */
302
303void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
304{
305 unsigned long flags;
306
307 spin_lock_irqsave(&port->lock, flags);
a211b1af 308 tty_kref_put(port->tty);
cb4bca35 309 port->tty = tty_kref_get(tty);
4a90f09b
AC
310 spin_unlock_irqrestore(&port->lock, flags);
311}
312EXPORT_SYMBOL(tty_port_tty_set);
31f35939 313
957dacae 314static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
7ca0ff9a 315{
64bc3979 316 mutex_lock(&port->mutex);
8bde9658
JH
317 if (port->console)
318 goto out;
319
d41861ca
PH
320 if (tty_port_initialized(port)) {
321 tty_port_set_initialized(port, 0);
957dacae
JH
322 /*
323 * Drop DTR/RTS if HUPCL is set. This causes any attached
324 * modem to hang up the line.
325 */
326 if (tty && C_HUPCL(tty))
327 tty_port_lower_dtr_rts(port);
328
8bde9658 329 if (port->ops->shutdown)
7ca0ff9a 330 port->ops->shutdown(port);
8bde9658
JH
331 }
332out:
64bc3979 333 mutex_unlock(&port->mutex);
7ca0ff9a
AC
334}
335
3e61696b
AC
336/**
337 * tty_port_hangup - hangup helper
338 * @port: tty port
339 *
340 * Perform port level tty hangup flag and count changes. Drop the tty
341 * reference.
9c9928bd
PH
342 *
343 * Caller holds tty lock.
3e61696b
AC
344 */
345
346void tty_port_hangup(struct tty_port *port)
347{
957dacae 348 struct tty_struct *tty;
3e61696b
AC
349 unsigned long flags;
350
351 spin_lock_irqsave(&port->lock, flags);
352 port->count = 0;
957dacae
JH
353 tty = port->tty;
354 if (tty)
355 set_bit(TTY_IO_ERROR, &tty->flags);
3e61696b
AC
356 port->tty = NULL;
357 spin_unlock_irqrestore(&port->lock, flags);
807c8d81 358 tty_port_set_active(port, 0);
957dacae
JH
359 tty_port_shutdown(port, tty);
360 tty_kref_put(tty);
3e61696b 361 wake_up_interruptible(&port->open_wait);
bdc04e31 362 wake_up_interruptible(&port->delta_msr_wait);
3e61696b
AC
363}
364EXPORT_SYMBOL(tty_port_hangup);
365
aa27a094
JS
366/**
367 * tty_port_tty_hangup - helper to hang up a tty
368 *
369 * @port: tty port
370 * @check_clocal: hang only ttys with CLOCAL unset?
371 */
372void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
373{
374 struct tty_struct *tty = tty_port_tty_get(port);
375
1d9e689c 376 if (tty && (!check_clocal || !C_CLOCAL(tty)))
aa27a094 377 tty_hangup(tty);
1d9e689c 378 tty_kref_put(tty);
aa27a094
JS
379}
380EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
381
6aad04f2
JS
382/**
383 * tty_port_tty_wakeup - helper to wake up a tty
384 *
385 * @port: tty port
386 */
387void tty_port_tty_wakeup(struct tty_port *port)
388{
c3485ee0 389 port->client_ops->write_wakeup(port);
6aad04f2
JS
390}
391EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
392
31f35939
AC
393/**
394 * tty_port_carrier_raised - carrier raised check
395 * @port: tty port
396 *
397 * Wrapper for the carrier detect logic. For the moment this is used
398 * to hide some internal details. This will eventually become entirely
399 * internal to the tty port.
400 */
401
402int tty_port_carrier_raised(struct tty_port *port)
403{
404 if (port->ops->carrier_raised == NULL)
405 return 1;
406 return port->ops->carrier_raised(port);
407}
408EXPORT_SYMBOL(tty_port_carrier_raised);
5d951fb4
AC
409
410/**
fcc8ac18 411 * tty_port_raise_dtr_rts - Raise DTR/RTS
5d951fb4
AC
412 * @port: tty port
413 *
414 * Wrapper for the DTR/RTS raise logic. For the moment this is used
415 * to hide some internal details. This will eventually become entirely
416 * internal to the tty port.
417 */
418
419void tty_port_raise_dtr_rts(struct tty_port *port)
420{
fcc8ac18
AC
421 if (port->ops->dtr_rts)
422 port->ops->dtr_rts(port, 1);
5d951fb4
AC
423}
424EXPORT_SYMBOL(tty_port_raise_dtr_rts);
36c621d8 425
fcc8ac18
AC
426/**
427 * tty_port_lower_dtr_rts - Lower DTR/RTS
428 * @port: tty port
429 *
430 * Wrapper for the DTR/RTS raise logic. For the moment this is used
431 * to hide some internal details. This will eventually become entirely
432 * internal to the tty port.
433 */
434
435void tty_port_lower_dtr_rts(struct tty_port *port)
436{
437 if (port->ops->dtr_rts)
438 port->ops->dtr_rts(port, 0);
439}
440EXPORT_SYMBOL(tty_port_lower_dtr_rts);
441
36c621d8
AC
442/**
443 * tty_port_block_til_ready - Waiting logic for tty open
444 * @port: the tty port being opened
445 * @tty: the tty device being bound
ed3f0af8 446 * @filp: the file pointer of the opener or NULL
36c621d8
AC
447 *
448 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
449 * Handles:
450 * - hangup (both before and during)
451 * - non blocking open
452 * - rts/dtr/dcd
453 * - signals
454 * - port flags and counts
455 *
456 * The passed tty_port must implement the carrier_raised method if it can
fcc8ac18 457 * do carrier detect and the dtr_rts method if it supports software
36c621d8
AC
458 * management of these lines. Note that the dtr/rts raise is done each
459 * iteration as a hangup may have previously dropped them while we wait.
c590f6b6
PH
460 *
461 * Caller holds tty lock.
462 *
463 * NB: May drop and reacquire tty lock when blocking, so tty and tty_port
464 * may have changed state (eg., may have been hung up).
36c621d8 465 */
d774a56d 466
36c621d8
AC
467int tty_port_block_til_ready(struct tty_port *port,
468 struct tty_struct *tty, struct file *filp)
469{
470 int do_clocal = 0, retval;
471 unsigned long flags;
6af9a43d 472 DEFINE_WAIT(wait);
36c621d8 473
36c621d8
AC
474 /* if non-blocking mode is set we can pass directly to open unless
475 the port has just hung up or is in another error state */
18900ca6 476 if (tty_io_error(tty)) {
807c8d81 477 tty_port_set_active(port, 1);
8627b96d
AC
478 return 0;
479 }
ed3f0af8 480 if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
4175f3e3 481 /* Indicate we are open */
9db276f8 482 if (C_BAUD(tty))
4175f3e3 483 tty_port_raise_dtr_rts(port);
807c8d81 484 tty_port_set_active(port, 1);
36c621d8
AC
485 return 0;
486 }
487
488 if (C_CLOCAL(tty))
489 do_clocal = 1;
490
491 /* Block waiting until we can proceed. We may need to wait for the
492 carrier, but we must also wait for any close that is in progress
493 before the next open may complete */
494
495 retval = 0;
36c621d8
AC
496
497 /* The port lock protects the port counts */
498 spin_lock_irqsave(&port->lock, flags);
e359a4e3 499 port->count--;
36c621d8
AC
500 port->blocked_open++;
501 spin_unlock_irqrestore(&port->lock, flags);
502
503 while (1) {
504 /* Indicate we are open */
d41861ca 505 if (C_BAUD(tty) && tty_port_initialized(port))
7834909f 506 tty_port_raise_dtr_rts(port);
36c621d8 507
3e3b5c08 508 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
d774a56d
AC
509 /* Check for a hangup or uninitialised port.
510 Return accordingly */
d41861ca 511 if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
36c621d8
AC
512 if (port->flags & ASYNC_HUP_NOTIFY)
513 retval = -EAGAIN;
514 else
515 retval = -ERESTARTSYS;
516 break;
517 }
0eee50af
JS
518 /*
519 * Probe the carrier. For devices with no carrier detect
520 * tty_port_carrier_raised will always return true.
521 * Never ask drivers if CLOCAL is set, this causes troubles
522 * on some hardware.
523 */
fef062cb 524 if (do_clocal || tty_port_carrier_raised(port))
36c621d8
AC
525 break;
526 if (signal_pending(current)) {
527 retval = -ERESTARTSYS;
528 break;
529 }
89c8d91e 530 tty_unlock(tty);
36c621d8 531 schedule();
89c8d91e 532 tty_lock(tty);
36c621d8 533 }
3e3b5c08 534 finish_wait(&port->open_wait, &wait);
36c621d8
AC
535
536 /* Update counts. A parallel hangup will have set count to zero and
537 we must not mess that up further */
538 spin_lock_irqsave(&port->lock, flags);
539 if (!tty_hung_up_p(filp))
540 port->count++;
541 port->blocked_open--;
36c621d8 542 spin_unlock_irqrestore(&port->lock, flags);
807c8d81
PH
543 if (retval == 0)
544 tty_port_set_active(port, 1);
ecc2e05e 545 return retval;
36c621d8
AC
546}
547EXPORT_SYMBOL(tty_port_block_til_ready);
548
b74414f5
JH
549static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
550{
551 unsigned int bps = tty_get_baud_rate(tty);
552 long timeout;
553
554 if (bps > 1200) {
555 timeout = (HZ * 10 * port->drain_delay) / bps;
556 timeout = max_t(long, timeout, HZ / 10);
557 } else {
558 timeout = 2 * HZ;
559 }
560 schedule_timeout_interruptible(timeout);
561}
562
79c1faa4 563/* Caller holds tty lock. */
d774a56d
AC
564int tty_port_close_start(struct tty_port *port,
565 struct tty_struct *tty, struct file *filp)
a6614999
AC
566{
567 unsigned long flags;
568
633caba8 569 if (tty_hung_up_p(filp))
a6614999 570 return 0;
a6614999 571
633caba8 572 spin_lock_irqsave(&port->lock, flags);
d774a56d 573 if (tty->count == 1 && port->count != 1) {
339f36ba
PH
574 tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
575 port->count);
a6614999
AC
576 port->count = 1;
577 }
578 if (--port->count < 0) {
339f36ba
PH
579 tty_warn(tty, "%s: bad port count (%d)\n", __func__,
580 port->count);
a6614999
AC
581 port->count = 0;
582 }
583
584 if (port->count) {
585 spin_unlock_irqrestore(&port->lock, flags);
586 return 0;
587 }
a6614999 588 spin_unlock_irqrestore(&port->lock, flags);
0b2588ca 589
ddc7b758
PH
590 tty->closing = 1;
591
d41861ca 592 if (tty_port_initialized(port)) {
0b2588ca
JH
593 /* Don't block on a stalled port, just pull the chain */
594 if (tty->flow_stopped)
595 tty_driver_flush_buffer(tty);
596 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
79c1faa4 597 tty_wait_until_sent(tty, port->closing_wait);
0b2588ca
JH
598 if (port->drain_delay)
599 tty_port_drain_delay(port, tty);
600 }
e707c35c
AC
601 /* Flush the ldisc buffering */
602 tty_ldisc_flush(tty);
603
469d6d06 604 /* Report to caller this is the last port reference */
a6614999
AC
605 return 1;
606}
607EXPORT_SYMBOL(tty_port_close_start);
608
0733db91 609/* Caller holds tty lock */
a6614999
AC
610void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
611{
612 unsigned long flags;
613
3f40f5b2 614 tty_ldisc_flush(tty);
a6614999
AC
615 tty->closing = 0;
616
ddc7b758
PH
617 spin_lock_irqsave(&port->lock, flags);
618
a6614999
AC
619 if (port->blocked_open) {
620 spin_unlock_irqrestore(&port->lock, flags);
5823323e
PH
621 if (port->close_delay)
622 msleep_interruptible(jiffies_to_msecs(port->close_delay));
a6614999
AC
623 spin_lock_irqsave(&port->lock, flags);
624 wake_up_interruptible(&port->open_wait);
625 }
a6614999 626 spin_unlock_irqrestore(&port->lock, flags);
807c8d81 627 tty_port_set_active(port, 0);
a6614999
AC
628}
629EXPORT_SYMBOL(tty_port_close_end);
7ca0ff9a 630
0733db91
PH
631/**
632 * tty_port_close
633 *
634 * Caller holds tty lock
0733db91 635 */
7ca0ff9a
AC
636void tty_port_close(struct tty_port *port, struct tty_struct *tty,
637 struct file *filp)
638{
639 if (tty_port_close_start(port, tty, filp) == 0)
640 return;
957dacae 641 tty_port_shutdown(port, tty);
d74e8286 642 set_bit(TTY_IO_ERROR, &tty->flags);
7ca0ff9a
AC
643 tty_port_close_end(port, tty);
644 tty_port_tty_set(port, NULL);
645}
646EXPORT_SYMBOL(tty_port_close);
64bc3979 647
72a33bf5
JS
648/**
649 * tty_port_install - generic tty->ops->install handler
650 * @port: tty_port of the device
651 * @driver: tty_driver for this device
652 * @tty: tty to be installed
653 *
654 * It is the same as tty_standard_install except the provided @port is linked
655 * to a concrete tty specified by @tty. Use this or tty_port_register_device
656 * (or both). Call tty_port_link_device as a last resort.
657 */
695586ca
JS
658int tty_port_install(struct tty_port *port, struct tty_driver *driver,
659 struct tty_struct *tty)
660{
661 tty->port = port;
662 return tty_standard_install(driver, tty);
663}
664EXPORT_SYMBOL_GPL(tty_port_install);
665
addd4672
PH
666/**
667 * tty_port_open
668 *
669 * Caller holds tty lock.
670 *
671 * NB: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
672 * tty and tty_port may have changed state (eg., may be hung up now)
673 */
64bc3979 674int tty_port_open(struct tty_port *port, struct tty_struct *tty,
d774a56d 675 struct file *filp)
64bc3979
AC
676{
677 spin_lock_irq(&port->lock);
e359a4e3 678 ++port->count;
64bc3979
AC
679 spin_unlock_irq(&port->lock);
680 tty_port_tty_set(port, tty);
681
682 /*
683 * Do the device-specific open only if the hardware isn't
684 * already initialized. Serialize open and shutdown using the
685 * port mutex.
686 */
687
688 mutex_lock(&port->mutex);
689
d41861ca 690 if (!tty_port_initialized(port)) {
a9a37ec3 691 clear_bit(TTY_IO_ERROR, &tty->flags);
64bc3979
AC
692 if (port->ops->activate) {
693 int retval = port->ops->activate(port, tty);
694 if (retval) {
d774a56d
AC
695 mutex_unlock(&port->mutex);
696 return retval;
697 }
698 }
d41861ca 699 tty_port_set_initialized(port, 1);
64bc3979
AC
700 }
701 mutex_unlock(&port->mutex);
702 return tty_port_block_til_ready(port, tty, filp);
703}
704
705EXPORT_SYMBOL(tty_port_open);