blob: 7efbca474689ea93fa776436b2bb319033539056 [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>
Alan Cox3e616962009-01-02 13:45:26 +000010#include <linux/serial.h>
Alan Cox9e485652008-10-13 10:37:07 +010011#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));
Jiri Slabyecbbfd42012-10-18 22:26:47 +020024 tty_buffer_init(port);
Alan Cox9e485652008-10-13 10:37:07 +010025 init_waitqueue_head(&port->open_wait);
26 init_waitqueue_head(&port->close_wait);
Alan Coxbdc04e32009-09-19 13:13:31 -070027 init_waitqueue_head(&port->delta_msr_wait);
Alan Cox9e485652008-10-13 10:37:07 +010028 mutex_init(&port->mutex);
Alan Cox44e49092009-11-30 13:16:41 +000029 mutex_init(&port->buf_mutex);
Alan Cox4a90f092008-10-13 10:39:46 +010030 spin_lock_init(&port->lock);
Alan Cox9e485652008-10-13 10:37:07 +010031 port->close_delay = (50 * HZ) / 100;
32 port->closing_wait = (3000 * HZ) / 100;
Alan Cox568aafc2009-11-30 13:17:14 +000033 kref_init(&port->kref);
Alan Cox9e485652008-10-13 10:37:07 +010034}
35EXPORT_SYMBOL(tty_port_init);
36
Jiri Slaby72a33bf2012-08-07 21:47:49 +020037/**
Jiri Slaby2cb4ca02012-08-07 21:47:50 +020038 * 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
57/**
Jiri Slaby72a33bf2012-08-07 21:47:49 +020058 * 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 */
Jiri Slaby057eb852012-06-04 13:35:37 +020068struct device *tty_port_register_device(struct tty_port *port,
69 struct tty_driver *driver, unsigned index,
70 struct device *device)
71{
Jiri Slaby2cb4ca02012-08-07 21:47:50 +020072 tty_port_link_device(port, driver, index);
Jiri Slaby057eb852012-06-04 13:35:37 +020073 return tty_register_device(driver, index, device);
74}
75EXPORT_SYMBOL_GPL(tty_port_register_device);
76
Tomas Hlavacekb1b79912012-09-06 23:17:47 +020077/**
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
Alan Cox9e485652008-10-13 10:37:07 +0100101int tty_port_alloc_xmit_buf(struct tty_port *port)
102{
103 /* We may sleep in get_zeroed_page() */
Alan Cox44e49092009-11-30 13:16:41 +0000104 mutex_lock(&port->buf_mutex);
Alan Cox9e485652008-10-13 10:37:07 +0100105 if (port->xmit_buf == NULL)
106 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
Alan Cox44e49092009-11-30 13:16:41 +0000107 mutex_unlock(&port->buf_mutex);
Alan Cox9e485652008-10-13 10:37:07 +0100108 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{
Alan Cox44e49092009-11-30 13:16:41 +0000116 mutex_lock(&port->buf_mutex);
Alan Cox9e485652008-10-13 10:37:07 +0100117 if (port->xmit_buf != NULL) {
118 free_page((unsigned long)port->xmit_buf);
119 port->xmit_buf = NULL;
120 }
Alan Cox44e49092009-11-30 13:16:41 +0000121 mutex_unlock(&port->buf_mutex);
Alan Cox9e485652008-10-13 10:37:07 +0100122}
123EXPORT_SYMBOL(tty_port_free_xmit_buf);
124
Jiri Slabyde274bf2012-11-15 09:49:54 +0100125/**
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{
Peter Hurley4f98d462013-03-11 16:44:34 -0400135 cancel_work_sync(&port->buf.work);
Jiri Slabyde274bf2012-11-15 09:49:54 +0100136 tty_buffer_free_all(port);
137}
138EXPORT_SYMBOL(tty_port_destroy);
139
Alan Cox568aafc2009-11-30 13:17:14 +0000140static void tty_port_destructor(struct kref *kref)
141{
142 struct tty_port *port = container_of(kref, struct tty_port, kref);
Peter Hurleye3bfea22013-09-18 20:42:39 -0400143
144 /* check if last port ref was dropped before tty release */
145 if (WARN_ON(port->itty))
146 return;
Alan Cox568aafc2009-11-30 13:17:14 +0000147 if (port->xmit_buf)
148 free_page((unsigned long)port->xmit_buf);
Jiri Slabyde274bf2012-11-15 09:49:54 +0100149 tty_port_destroy(port);
Jiri Slaby81c79832012-11-15 09:49:49 +0100150 if (port->ops && port->ops->destruct)
Alan Cox568aafc2009-11-30 13:17:14 +0000151 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);
Alan Cox9e485652008-10-13 10:37:07 +0100162
Alan Cox4a90f092008-10-13 10:39:46 +0100163/**
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);
Alan Coxcb4bca32008-10-21 13:47:44 +0100199 port->tty = tty_kref_get(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100200 spin_unlock_irqrestore(&port->lock, flags);
201}
202EXPORT_SYMBOL(tty_port_tty_set);
Alan Cox31f35932009-01-02 13:45:05 +0000203
Johan Hovold957daca2013-03-07 15:55:51 +0100204static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
Alan Cox7ca0ff92009-09-19 13:13:20 -0700205{
Alan Cox64bc3972009-10-06 16:06:11 +0100206 mutex_lock(&port->mutex);
Johan Hovold8bde9652013-03-07 15:55:48 +0100207 if (port->console)
208 goto out;
209
210 if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
Johan Hovold957daca2013-03-07 15:55:51 +0100211 /*
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
Johan Hovold8bde9652013-03-07 15:55:48 +0100218 if (port->ops->shutdown)
Alan Cox7ca0ff92009-09-19 13:13:20 -0700219 port->ops->shutdown(port);
Johan Hovold8bde9652013-03-07 15:55:48 +0100220 }
221out:
Alan Cox64bc3972009-10-06 16:06:11 +0100222 mutex_unlock(&port->mutex);
Alan Cox7ca0ff92009-09-19 13:13:20 -0700223}
224
Alan Cox31f35932009-01-02 13:45:05 +0000225/**
Alan Cox3e616962009-01-02 13:45:26 +0000226 * 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{
Johan Hovold957daca2013-03-07 15:55:51 +0100235 struct tty_struct *tty;
Alan Cox3e616962009-01-02 13:45:26 +0000236 unsigned long flags;
237
238 spin_lock_irqsave(&port->lock, flags);
239 port->count = 0;
240 port->flags &= ~ASYNC_NORMAL_ACTIVE;
Johan Hovold957daca2013-03-07 15:55:51 +0100241 tty = port->tty;
242 if (tty)
243 set_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox3e616962009-01-02 13:45:26 +0000244 port->tty = NULL;
245 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovold957daca2013-03-07 15:55:51 +0100246 tty_port_shutdown(port, tty);
247 tty_kref_put(tty);
Alan Cox3e616962009-01-02 13:45:26 +0000248 wake_up_interruptible(&port->open_wait);
Alan Coxbdc04e32009-09-19 13:13:31 -0700249 wake_up_interruptible(&port->delta_msr_wait);
Alan Cox3e616962009-01-02 13:45:26 +0000250}
251EXPORT_SYMBOL(tty_port_hangup);
252
253/**
Jiri Slabyaa27a092013-03-07 13:12:30 +0100254 * 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
Gianluca Anzolin1d9e6892013-07-25 07:26:16 +0200263 if (tty && (!check_clocal || !C_CLOCAL(tty)))
Jiri Slabyaa27a092013-03-07 13:12:30 +0100264 tty_hangup(tty);
Gianluca Anzolin1d9e6892013-07-25 07:26:16 +0200265 tty_kref_put(tty);
Jiri Slabyaa27a092013-03-07 13:12:30 +0100266}
267EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
268
269/**
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100270 * 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
285/**
Alan Cox31f35932009-01-02 13:45:05 +0000286 * 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);
Alan Cox5d951fb2009-01-02 13:45:19 +0000301
302/**
Alan Coxfcc8ac12009-06-11 12:24:17 +0100303 * tty_port_raise_dtr_rts - Raise DTR/RTS
Alan Cox5d951fb2009-01-02 13:45:19 +0000304 * @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{
Alan Coxfcc8ac12009-06-11 12:24:17 +0100313 if (port->ops->dtr_rts)
314 port->ops->dtr_rts(port, 1);
Alan Cox5d951fb2009-01-02 13:45:19 +0000315}
316EXPORT_SYMBOL(tty_port_raise_dtr_rts);
Alan Cox36c621d2009-01-02 13:46:10 +0000317
318/**
Alan Coxfcc8ac12009-06-11 12:24:17 +0100319 * 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
334/**
Alan Cox36c621d2009-01-02 13:46:10 +0000335 * 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
Alan Coxfcc8ac12009-06-11 12:24:17 +0100349 * do carrier detect and the dtr_rts method if it supports software
Alan Cox36c621d2009-01-02 13:46:10 +0000350 * 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 */
Alan Coxd774a562009-10-06 16:06:21 +0100353
Alan Cox36c621d2009-01-02 13:46:10 +0000354int 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;
Jiri Slaby6af9a432009-06-24 18:35:05 +0100359 DEFINE_WAIT(wait);
Alan Cox36c621d2009-01-02 13:46:10 +0000360
361 /* block if port is in the process of being closed */
362 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
Alan Cox89c8d912012-08-08 16:30:13 +0100363 wait_event_interruptible_tty(tty, port->close_wait,
Jiri Slaby5fc5b422009-06-11 14:32:42 +0100364 !(port->flags & ASYNC_CLOSING));
Alan Cox36c621d2009-01-02 13:46:10 +0000365 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 */
Alan Cox8627b962009-11-18 14:12:58 +0000373 if (tty->flags & (1 << TTY_IO_ERROR)) {
374 port->flags |= ASYNC_NORMAL_ACTIVE;
375 return 0;
376 }
377 if (filp->f_flags & O_NONBLOCK) {
Alan Cox4175f3e2009-10-28 21:12:32 +0100378 /* Indicate we are open */
Alan Coxadc8d742012-07-14 15:31:47 +0100379 if (tty->termios.c_cflag & CBAUD)
Alan Cox4175f3e2009-10-28 21:12:32 +0100380 tty_port_raise_dtr_rts(port);
Alan Cox36c621d2009-01-02 13:46:10 +0000381 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;
Alan Cox36c621d2009-01-02 13:46:10 +0000393
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 */
Johan Hovolde584a022013-03-07 15:55:50 +0100403 if (C_BAUD(tty) && test_bit(ASYNCB_INITIALIZED, &port->flags))
Alan Cox78349092009-01-02 13:46:43 +0000404 tty_port_raise_dtr_rts(port);
Alan Cox36c621d2009-01-02 13:46:10 +0000405
Jiri Slaby3e3b5c02009-06-11 14:33:37 +0100406 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
Alan Coxd774a562009-10-06 16:06:21 +0100407 /* Check for a hangup or uninitialised port.
408 Return accordingly */
Alan Cox36c621d2009-01-02 13:46:10 +0000409 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 }
Jiri Slaby0eee50a2012-01-12 22:55:15 +0100416 /*
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 */
Alan Cox36c621d2009-01-02 13:46:10 +0000422 if (!(port->flags & ASYNC_CLOSING) &&
Jiri Slaby0eee50a2012-01-12 22:55:15 +0100423 (do_clocal || tty_port_carrier_raised(port)))
Alan Cox36c621d2009-01-02 13:46:10 +0000424 break;
425 if (signal_pending(current)) {
426 retval = -ERESTARTSYS;
427 break;
428 }
Alan Cox89c8d912012-08-08 16:30:13 +0100429 tty_unlock(tty);
Alan Cox36c621d2009-01-02 13:46:10 +0000430 schedule();
Alan Cox89c8d912012-08-08 16:30:13 +0100431 tty_lock(tty);
Alan Cox36c621d2009-01-02 13:46:10 +0000432 }
Jiri Slaby3e3b5c02009-06-11 14:33:37 +0100433 finish_wait(&port->open_wait, &wait);
Alan Cox36c621d2009-01-02 13:46:10 +0000434
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);
Alan Coxecc2e052009-07-17 16:17:26 +0100444 return retval;
Alan Cox36c621d2009-01-02 13:46:10 +0000445}
446EXPORT_SYMBOL(tty_port_block_til_ready);
447
Johan Hovoldb74414f2013-03-07 15:55:52 +0100448static 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
Alan Coxd774a562009-10-06 16:06:21 +0100462int tty_port_close_start(struct tty_port *port,
463 struct tty_struct *tty, struct file *filp)
Alan Coxa6614992009-01-02 13:46:50 +0000464{
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
Alan Coxd774a562009-10-06 16:06:21 +0100473 if (tty->count == 1 && port->count != 1) {
Alan Coxa6614992009-01-02 13:46:50 +0000474 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);
Alan Cox7ca0ff92009-09-19 13:13:20 -0700487 if (port->ops->drop)
488 port->ops->drop(port);
Alan Coxa6614992009-01-02 13:46:50 +0000489 return 0;
490 }
Alan Stern1f5c13f2009-08-20 15:23:47 -0400491 set_bit(ASYNCB_CLOSING, &port->flags);
Alan Coxa6614992009-01-02 13:46:50 +0000492 tty->closing = 1;
493 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovold0b2588c2013-03-07 15:55:53 +0100494
495 if (test_bit(ASYNCB_INITIALIZED, &port->flags)) {
496 /* Don't block on a stalled port, just pull the chain */
497 if (tty->flow_stopped)
498 tty_driver_flush_buffer(tty);
499 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
500 tty_wait_until_sent_from_close(tty, port->closing_wait);
501 if (port->drain_delay)
502 tty_port_drain_delay(port, tty);
503 }
Alan Coxe707c352009-11-05 13:27:57 +0000504 /* Flush the ldisc buffering */
505 tty_ldisc_flush(tty);
506
Alan Cox7ca0ff92009-09-19 13:13:20 -0700507 /* Don't call port->drop for the last reference. Callers will want
508 to drop the last active reference in ->shutdown() or the tty
509 shutdown path */
Alan Coxa6614992009-01-02 13:46:50 +0000510 return 1;
511}
512EXPORT_SYMBOL(tty_port_close_start);
513
514void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
515{
516 unsigned long flags;
517
Alan Coxa6614992009-01-02 13:46:50 +0000518 spin_lock_irqsave(&port->lock, flags);
519 tty->closing = 0;
520
521 if (port->blocked_open) {
522 spin_unlock_irqrestore(&port->lock, flags);
523 if (port->close_delay) {
524 msleep_interruptible(
525 jiffies_to_msecs(port->close_delay));
526 }
527 spin_lock_irqsave(&port->lock, flags);
528 wake_up_interruptible(&port->open_wait);
529 }
530 port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
531 wake_up_interruptible(&port->close_wait);
532 spin_unlock_irqrestore(&port->lock, flags);
533}
534EXPORT_SYMBOL(tty_port_close_end);
Alan Cox7ca0ff92009-09-19 13:13:20 -0700535
536void tty_port_close(struct tty_port *port, struct tty_struct *tty,
537 struct file *filp)
538{
539 if (tty_port_close_start(port, tty, filp) == 0)
540 return;
Johan Hovold957daca2013-03-07 15:55:51 +0100541 tty_port_shutdown(port, tty);
Alan Coxd74e8282009-11-30 13:16:52 +0000542 set_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox7ca0ff92009-09-19 13:13:20 -0700543 tty_port_close_end(port, tty);
544 tty_port_tty_set(port, NULL);
545}
546EXPORT_SYMBOL(tty_port_close);
Alan Cox64bc3972009-10-06 16:06:11 +0100547
Jiri Slaby72a33bf2012-08-07 21:47:49 +0200548/**
549 * tty_port_install - generic tty->ops->install handler
550 * @port: tty_port of the device
551 * @driver: tty_driver for this device
552 * @tty: tty to be installed
553 *
554 * It is the same as tty_standard_install except the provided @port is linked
555 * to a concrete tty specified by @tty. Use this or tty_port_register_device
556 * (or both). Call tty_port_link_device as a last resort.
557 */
Jiri Slaby695586c2012-06-04 13:35:32 +0200558int tty_port_install(struct tty_port *port, struct tty_driver *driver,
559 struct tty_struct *tty)
560{
561 tty->port = port;
562 return tty_standard_install(driver, tty);
563}
564EXPORT_SYMBOL_GPL(tty_port_install);
565
Alan Cox64bc3972009-10-06 16:06:11 +0100566int tty_port_open(struct tty_port *port, struct tty_struct *tty,
Alan Coxd774a562009-10-06 16:06:21 +0100567 struct file *filp)
Alan Cox64bc3972009-10-06 16:06:11 +0100568{
569 spin_lock_irq(&port->lock);
570 if (!tty_hung_up_p(filp))
571 ++port->count;
572 spin_unlock_irq(&port->lock);
573 tty_port_tty_set(port, tty);
574
575 /*
576 * Do the device-specific open only if the hardware isn't
577 * already initialized. Serialize open and shutdown using the
578 * port mutex.
579 */
580
581 mutex_lock(&port->mutex);
582
583 if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
Alan Coxa9a37ec2009-11-30 13:16:57 +0000584 clear_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox64bc3972009-10-06 16:06:11 +0100585 if (port->ops->activate) {
586 int retval = port->ops->activate(port, tty);
587 if (retval) {
Alan Coxd774a562009-10-06 16:06:21 +0100588 mutex_unlock(&port->mutex);
589 return retval;
590 }
591 }
Alan Cox64bc3972009-10-06 16:06:11 +0100592 set_bit(ASYNCB_INITIALIZED, &port->flags);
593 }
594 mutex_unlock(&port->mutex);
595 return tty_port_block_til_ready(port, tty, filp);
596}
597
598EXPORT_SYMBOL(tty_port_open);