2017-11-06 17:11:51 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2008-10-13 09:37:07 +00:00
|
|
|
/*
|
|
|
|
* Tty port functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/tty.h>
|
|
|
|
#include <linux/tty_driver.h>
|
|
|
|
#include <linux/tty_flip.h>
|
2009-01-02 13:45:26 +00:00
|
|
|
#include <linux/serial.h>
|
2008-10-13 09:37:07 +00:00
|
|
|
#include <linux/timer.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/slab.h>
|
2017-02-02 18:15:33 +00:00
|
|
|
#include <linux/sched/signal.h>
|
2008-10-13 09:37:07 +00:00
|
|
|
#include <linux/wait.h>
|
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/module.h>
|
2017-05-18 15:33:00 +00:00
|
|
|
#include <linux/serdev.h>
|
2021-04-08 12:51:22 +00:00
|
|
|
#include "tty.h"
|
2008-10-13 09:37:07 +00:00
|
|
|
|
2017-02-02 19:48:05 +00:00
|
|
|
static int tty_port_default_receive_buf(struct tty_port *port,
|
|
|
|
const unsigned char *p,
|
|
|
|
const unsigned char *f, size_t count)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct tty_struct *tty;
|
|
|
|
struct tty_ldisc *disc;
|
|
|
|
|
|
|
|
tty = READ_ONCE(port->itty);
|
|
|
|
if (!tty)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
disc = tty_ldisc_ref(tty);
|
|
|
|
if (!disc)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = tty_ldisc_receive_buf(disc, p, (char *)f, count);
|
|
|
|
|
|
|
|
tty_ldisc_deref(disc);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
tty: Implement lookahead to process XON/XOFF timely
When tty is not read from, XON/XOFF may get stuck into an
intermediate buffer. As those characters are there to do software
flow-control, it is not very useful. In the case where neither end
reads from ttys, the receiving ends might not be able receive the
XOFF characters and just keep sending more data to the opposite
direction. This problem is almost guaranteed to occur with DMA
which sends data in large chunks.
If TTY is slow to process characters, that is, eats less than given
amount in receive_buf, invoke lookahead for the rest of the chars
to process potential XON/XOFF characters.
We need to keep track of how many characters have been processed by the
lookahead to avoid processing the flow control char again on the normal
path. Bookkeeping occurs parallel on two layers (tty_buffer and n_tty)
to avoid passing the lookahead_count through the whole call chain.
When a flow-control char is processed, two things must occur:
a) it must not be treated as normal char
b) if not yet processed, flow-control actions need to be taken
The return value of n_tty_receive_char_flow_ctrl() tells caller a), and
b) is kept internal to n_tty_receive_char_flow_ctrl().
If characters were previous looked ahead, __receive_buf() makes two
calls to the appropriate n_tty_receive_buf_* function. First call is
made with lookahead_done=true for the characters that were subject to
lookahead earlier and then with lookahead=false for the new characters.
Either of the calls might be skipped when it has no characters to
handle.
Reported-by: Gilles Buloz <gilles.buloz@kontron.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Link: https://lore.kernel.org/r/20220606153652.63554-2-ilpo.jarvinen@linux.intel.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-06-06 15:36:51 +00:00
|
|
|
static void tty_port_default_lookahead_buf(struct tty_port *port, const unsigned char *p,
|
|
|
|
const unsigned char *f, unsigned int count)
|
|
|
|
{
|
|
|
|
struct tty_struct *tty;
|
|
|
|
struct tty_ldisc *disc;
|
|
|
|
|
|
|
|
tty = READ_ONCE(port->itty);
|
|
|
|
if (!tty)
|
|
|
|
return;
|
|
|
|
|
|
|
|
disc = tty_ldisc_ref(tty);
|
|
|
|
if (!disc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (disc->ops->lookahead_buf)
|
|
|
|
disc->ops->lookahead_buf(disc->tty, p, f, count);
|
|
|
|
|
|
|
|
tty_ldisc_deref(disc);
|
|
|
|
}
|
|
|
|
|
2017-02-02 19:48:05 +00:00
|
|
|
static void tty_port_default_wakeup(struct tty_port *port)
|
|
|
|
{
|
|
|
|
struct tty_struct *tty = tty_port_tty_get(port);
|
|
|
|
|
|
|
|
if (tty) {
|
|
|
|
tty_wakeup(tty);
|
|
|
|
tty_kref_put(tty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 14:57:30 +00:00
|
|
|
const struct tty_port_client_operations tty_port_default_client_ops = {
|
2017-02-02 19:48:05 +00:00
|
|
|
.receive_buf = tty_port_default_receive_buf,
|
tty: Implement lookahead to process XON/XOFF timely
When tty is not read from, XON/XOFF may get stuck into an
intermediate buffer. As those characters are there to do software
flow-control, it is not very useful. In the case where neither end
reads from ttys, the receiving ends might not be able receive the
XOFF characters and just keep sending more data to the opposite
direction. This problem is almost guaranteed to occur with DMA
which sends data in large chunks.
If TTY is slow to process characters, that is, eats less than given
amount in receive_buf, invoke lookahead for the rest of the chars
to process potential XON/XOFF characters.
We need to keep track of how many characters have been processed by the
lookahead to avoid processing the flow control char again on the normal
path. Bookkeeping occurs parallel on two layers (tty_buffer and n_tty)
to avoid passing the lookahead_count through the whole call chain.
When a flow-control char is processed, two things must occur:
a) it must not be treated as normal char
b) if not yet processed, flow-control actions need to be taken
The return value of n_tty_receive_char_flow_ctrl() tells caller a), and
b) is kept internal to n_tty_receive_char_flow_ctrl().
If characters were previous looked ahead, __receive_buf() makes two
calls to the appropriate n_tty_receive_buf_* function. First call is
made with lookahead_done=true for the characters that were subject to
lookahead earlier and then with lookahead=false for the new characters.
Either of the calls might be skipped when it has no characters to
handle.
Reported-by: Gilles Buloz <gilles.buloz@kontron.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Link: https://lore.kernel.org/r/20220606153652.63554-2-ilpo.jarvinen@linux.intel.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-06-06 15:36:51 +00:00
|
|
|
.lookahead_buf = tty_port_default_lookahead_buf,
|
2017-02-02 19:48:05 +00:00
|
|
|
.write_wakeup = tty_port_default_wakeup,
|
|
|
|
};
|
2020-02-10 14:57:30 +00:00
|
|
|
EXPORT_SYMBOL_GPL(tty_port_default_client_ops);
|
2017-02-02 19:48:05 +00:00
|
|
|
|
2021-11-26 08:16:06 +00:00
|
|
|
/**
|
|
|
|
* tty_port_init -- initialize tty_port
|
|
|
|
* @port: tty_port to initialize
|
|
|
|
*
|
|
|
|
* Initializes the state of struct tty_port. When a port was initialized using
|
|
|
|
* this function, one has to destroy the port by tty_port_destroy(). Either
|
|
|
|
* indirectly by using &tty_port refcounting (tty_port_put()) or directly if
|
|
|
|
* refcounting is not used.
|
|
|
|
*/
|
2008-10-13 09:37:07 +00:00
|
|
|
void tty_port_init(struct tty_port *port)
|
|
|
|
{
|
|
|
|
memset(port, 0, sizeof(*port));
|
2012-10-18 20:26:47 +00:00
|
|
|
tty_buffer_init(port);
|
2008-10-13 09:37:07 +00:00
|
|
|
init_waitqueue_head(&port->open_wait);
|
2009-09-19 20:13:31 +00:00
|
|
|
init_waitqueue_head(&port->delta_msr_wait);
|
2008-10-13 09:37:07 +00:00
|
|
|
mutex_init(&port->mutex);
|
2009-11-30 13:16:41 +00:00
|
|
|
mutex_init(&port->buf_mutex);
|
2008-10-13 09:39:46 +00:00
|
|
|
spin_lock_init(&port->lock);
|
2008-10-13 09:37:07 +00:00
|
|
|
port->close_delay = (50 * HZ) / 100;
|
|
|
|
port->closing_wait = (3000 * HZ) / 100;
|
2020-02-10 14:57:30 +00:00
|
|
|
port->client_ops = &tty_port_default_client_ops;
|
2009-11-30 13:17:14 +00:00
|
|
|
kref_init(&port->kref);
|
2008-10-13 09:37:07 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tty_port_init);
|
|
|
|
|
2012-08-07 19:47:50 +00:00
|
|
|
/**
|
|
|
|
* tty_port_link_device - link tty and tty_port
|
|
|
|
* @port: tty_port of the device
|
|
|
|
* @driver: tty_driver for this device
|
|
|
|
* @index: index of the tty
|
|
|
|
*
|
2017-09-08 06:59:42 +00:00
|
|
|
* Provide the tty layer with a link from a tty (specified by @index) to a
|
2021-11-26 08:15:59 +00:00
|
|
|
* tty_port (@port). Use this only if neither tty_port_register_device() nor
|
|
|
|
* tty_port_install() is used in the driver. If used, this has to be called
|
|
|
|
* before tty_register_driver().
|
2012-08-07 19:47:50 +00:00
|
|
|
*/
|
|
|
|
void tty_port_link_device(struct tty_port *port,
|
|
|
|
struct tty_driver *driver, unsigned index)
|
|
|
|
{
|
|
|
|
if (WARN_ON(index >= driver->num))
|
|
|
|
return;
|
2019-12-27 17:44:34 +00:00
|
|
|
driver->ports[index] = port;
|
2012-08-07 19:47:50 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(tty_port_link_device);
|
|
|
|
|
2012-08-07 19:47:49 +00:00
|
|
|
/**
|
|
|
|
* tty_port_register_device - register tty device
|
|
|
|
* @port: tty_port of the device
|
|
|
|
* @driver: tty_driver for this device
|
|
|
|
* @index: index of the tty
|
|
|
|
* @device: parent if exists, otherwise NULL
|
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* It is the same as tty_register_device() except the provided @port is linked
|
|
|
|
* to a concrete tty specified by @index. Use this or tty_port_install() (or
|
|
|
|
* both). Call tty_port_link_device() as a last resort.
|
2012-08-07 19:47:49 +00:00
|
|
|
*/
|
2012-06-04 11:35:37 +00:00
|
|
|
struct device *tty_port_register_device(struct tty_port *port,
|
|
|
|
struct tty_driver *driver, unsigned index,
|
|
|
|
struct device *device)
|
|
|
|
{
|
2017-01-16 22:54:30 +00:00
|
|
|
return tty_port_register_device_attr(port, driver, index, device, NULL, NULL);
|
2012-06-04 11:35:37 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(tty_port_register_device);
|
|
|
|
|
2012-09-06 21:17:47 +00:00
|
|
|
/**
|
|
|
|
* tty_port_register_device_attr - register tty device
|
|
|
|
* @port: tty_port of the device
|
|
|
|
* @driver: tty_driver for this device
|
|
|
|
* @index: index of the tty
|
|
|
|
* @device: parent if exists, otherwise NULL
|
|
|
|
* @drvdata: Driver data to be set to device.
|
|
|
|
* @attr_grp: Attribute group to be set on device.
|
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* It is the same as tty_register_device_attr() except the provided @port is
|
|
|
|
* linked to a concrete tty specified by @index. Use this or tty_port_install()
|
|
|
|
* (or both). Call tty_port_link_device() as a last resort.
|
2012-09-06 21:17:47 +00:00
|
|
|
*/
|
|
|
|
struct device *tty_port_register_device_attr(struct tty_port *port,
|
|
|
|
struct tty_driver *driver, unsigned index,
|
|
|
|
struct device *device, void *drvdata,
|
|
|
|
const struct attribute_group **attr_grp)
|
|
|
|
{
|
|
|
|
tty_port_link_device(port, driver, index);
|
|
|
|
return tty_register_device_attr(driver, index, device, drvdata,
|
|
|
|
attr_grp);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
|
|
|
|
|
2017-05-18 15:33:00 +00:00
|
|
|
/**
|
|
|
|
* tty_port_register_device_attr_serdev - register tty or serdev device
|
|
|
|
* @port: tty_port of the device
|
|
|
|
* @driver: tty_driver for this device
|
|
|
|
* @index: index of the tty
|
|
|
|
* @device: parent if exists, otherwise NULL
|
|
|
|
* @drvdata: driver data for the device
|
|
|
|
* @attr_grp: attribute group for the device
|
|
|
|
*
|
|
|
|
* Register a serdev or tty device depending on if the parent device has any
|
|
|
|
* defined serdev clients or not.
|
|
|
|
*/
|
|
|
|
struct device *tty_port_register_device_attr_serdev(struct tty_port *port,
|
|
|
|
struct tty_driver *driver, unsigned index,
|
|
|
|
struct device *device, void *drvdata,
|
|
|
|
const struct attribute_group **attr_grp)
|
|
|
|
{
|
|
|
|
struct device *dev;
|
|
|
|
|
|
|
|
tty_port_link_device(port, driver, index);
|
|
|
|
|
|
|
|
dev = serdev_tty_port_register(port, device, driver, index);
|
|
|
|
if (PTR_ERR(dev) != -ENODEV) {
|
|
|
|
/* Skip creating cdev if we registered a serdev device */
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tty_register_device_attr(driver, index, device, drvdata,
|
|
|
|
attr_grp);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(tty_port_register_device_attr_serdev);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tty_port_register_device_serdev - register tty or serdev device
|
|
|
|
* @port: tty_port of the device
|
|
|
|
* @driver: tty_driver for this device
|
|
|
|
* @index: index of the tty
|
|
|
|
* @device: parent if exists, otherwise NULL
|
|
|
|
*
|
|
|
|
* Register a serdev or tty device depending on if the parent device has any
|
|
|
|
* defined serdev clients or not.
|
|
|
|
*/
|
|
|
|
struct device *tty_port_register_device_serdev(struct tty_port *port,
|
|
|
|
struct tty_driver *driver, unsigned index,
|
|
|
|
struct device *device)
|
|
|
|
{
|
|
|
|
return tty_port_register_device_attr_serdev(port, driver, index,
|
|
|
|
device, NULL, NULL);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(tty_port_register_device_serdev);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tty_port_unregister_device - deregister a tty or serdev device
|
|
|
|
* @port: tty_port of the device
|
|
|
|
* @driver: tty_driver for this device
|
|
|
|
* @index: index of the tty
|
|
|
|
*
|
|
|
|
* If a tty or serdev device is registered with a call to
|
|
|
|
* tty_port_register_device_serdev() then this function must be called when
|
|
|
|
* the device is gone.
|
|
|
|
*/
|
|
|
|
void tty_port_unregister_device(struct tty_port *port,
|
|
|
|
struct tty_driver *driver, unsigned index)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = serdev_tty_port_unregister(port);
|
|
|
|
if (ret == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tty_unregister_device(driver, index);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(tty_port_unregister_device);
|
|
|
|
|
2008-10-13 09:37:07 +00:00
|
|
|
int tty_port_alloc_xmit_buf(struct tty_port *port)
|
|
|
|
{
|
|
|
|
/* We may sleep in get_zeroed_page() */
|
2009-11-30 13:16:41 +00:00
|
|
|
mutex_lock(&port->buf_mutex);
|
2022-01-24 07:14:22 +00:00
|
|
|
if (port->xmit_buf == NULL) {
|
2008-10-13 09:37:07 +00:00
|
|
|
port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
|
2022-01-24 07:14:22 +00:00
|
|
|
if (port->xmit_buf)
|
|
|
|
kfifo_init(&port->xmit_fifo, port->xmit_buf, PAGE_SIZE);
|
|
|
|
}
|
2009-11-30 13:16:41 +00:00
|
|
|
mutex_unlock(&port->buf_mutex);
|
2008-10-13 09:37:07 +00:00
|
|
|
if (port->xmit_buf == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
|
|
|
|
|
|
|
|
void tty_port_free_xmit_buf(struct tty_port *port)
|
|
|
|
{
|
2009-11-30 13:16:41 +00:00
|
|
|
mutex_lock(&port->buf_mutex);
|
2022-02-04 15:32:53 +00:00
|
|
|
free_page((unsigned long)port->xmit_buf);
|
|
|
|
port->xmit_buf = NULL;
|
|
|
|
INIT_KFIFO(port->xmit_fifo);
|
2009-11-30 13:16:41 +00:00
|
|
|
mutex_unlock(&port->buf_mutex);
|
2008-10-13 09:37:07 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tty_port_free_xmit_buf);
|
|
|
|
|
2012-11-15 08:49:54 +00:00
|
|
|
/**
|
|
|
|
* tty_port_destroy -- destroy inited port
|
2017-09-08 06:59:42 +00:00
|
|
|
* @port: tty port to be destroyed
|
2012-11-15 08:49:54 +00:00
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* When a port was initialized using tty_port_init(), one has to destroy the
|
|
|
|
* port by this function. Either indirectly by using &tty_port refcounting
|
|
|
|
* (tty_port_put()) or directly if refcounting is not used.
|
2012-11-15 08:49:54 +00:00
|
|
|
*/
|
|
|
|
void tty_port_destroy(struct tty_port *port)
|
|
|
|
{
|
2015-10-17 20:36:23 +00:00
|
|
|
tty_buffer_cancel_work(port);
|
2012-11-15 08:49:54 +00:00
|
|
|
tty_buffer_free_all(port);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tty_port_destroy);
|
|
|
|
|
2009-11-30 13:17:14 +00:00
|
|
|
static void tty_port_destructor(struct kref *kref)
|
|
|
|
{
|
|
|
|
struct tty_port *port = container_of(kref, struct tty_port, kref);
|
2013-09-19 00:42:39 +00:00
|
|
|
|
|
|
|
/* check if last port ref was dropped before tty release */
|
|
|
|
if (WARN_ON(port->itty))
|
|
|
|
return;
|
2022-02-04 15:32:53 +00:00
|
|
|
free_page((unsigned long)port->xmit_buf);
|
2012-11-15 08:49:54 +00:00
|
|
|
tty_port_destroy(port);
|
2012-11-15 08:49:49 +00:00
|
|
|
if (port->ops && port->ops->destruct)
|
2009-11-30 13:17:14 +00:00
|
|
|
port->ops->destruct(port);
|
|
|
|
else
|
|
|
|
kfree(port);
|
|
|
|
}
|
|
|
|
|
2021-11-26 08:16:06 +00:00
|
|
|
/**
|
|
|
|
* tty_port_put -- drop a reference to tty_port
|
|
|
|
* @port: port to drop a reference of (can be NULL)
|
|
|
|
*
|
|
|
|
* The final put will destroy and free up the @port using
|
|
|
|
* @port->ops->destruct() hook, or using kfree() if not provided.
|
|
|
|
*/
|
2009-11-30 13:17:14 +00:00
|
|
|
void tty_port_put(struct tty_port *port)
|
|
|
|
{
|
|
|
|
if (port)
|
|
|
|
kref_put(&port->kref, tty_port_destructor);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tty_port_put);
|
2008-10-13 09:37:07 +00:00
|
|
|
|
2008-10-13 09:39:46 +00:00
|
|
|
/**
|
2021-11-26 08:15:59 +00:00
|
|
|
* tty_port_tty_get - get a tty reference
|
|
|
|
* @port: tty port
|
2008-10-13 09:39:46 +00:00
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* Return a refcount protected tty instance or %NULL if the port is not
|
|
|
|
* associated with a tty (eg due to close or hangup).
|
2008-10-13 09:39:46 +00:00
|
|
|
*/
|
|
|
|
struct tty_struct *tty_port_tty_get(struct tty_port *port)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
struct tty_struct *tty;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&port->lock, flags);
|
|
|
|
tty = tty_kref_get(port->tty);
|
|
|
|
spin_unlock_irqrestore(&port->lock, flags);
|
|
|
|
return tty;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tty_port_tty_get);
|
|
|
|
|
|
|
|
/**
|
2021-11-26 08:15:59 +00:00
|
|
|
* tty_port_tty_set - set the tty of a port
|
|
|
|
* @port: tty port
|
|
|
|
* @tty: the tty
|
2008-10-13 09:39:46 +00:00
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* Associate the port and tty pair. Manages any internal refcounts. Pass %NULL
|
|
|
|
* to deassociate a port.
|
2008-10-13 09:39:46 +00:00
|
|
|
*/
|
|
|
|
void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&port->lock, flags);
|
2014-11-21 12:42:29 +00:00
|
|
|
tty_kref_put(port->tty);
|
2008-10-21 12:47:44 +00:00
|
|
|
port->tty = tty_kref_get(tty);
|
2008-10-13 09:39:46 +00:00
|
|
|
spin_unlock_irqrestore(&port->lock, flags);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tty_port_tty_set);
|
2009-01-02 13:45:05 +00:00
|
|
|
|
2021-11-26 08:16:06 +00:00
|
|
|
/**
|
|
|
|
* tty_port_shutdown - internal helper to shutdown the device
|
|
|
|
* @port: tty port to be shut down
|
|
|
|
* @tty: the associated tty
|
|
|
|
*
|
|
|
|
* It is used by tty_port_hangup() and tty_port_close(). Its task is to
|
|
|
|
* shutdown the device if it was initialized (note consoles remain
|
|
|
|
* functioning). It lowers DTR/RTS (if @tty has HUPCL set) and invokes
|
|
|
|
* @port->ops->shutdown().
|
|
|
|
*/
|
2013-03-07 14:55:51 +00:00
|
|
|
static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
|
2009-09-19 20:13:20 +00:00
|
|
|
{
|
2009-10-06 15:06:11 +00:00
|
|
|
mutex_lock(&port->mutex);
|
2013-03-07 14:55:48 +00:00
|
|
|
if (port->console)
|
|
|
|
goto out;
|
|
|
|
|
2016-04-10 00:53:25 +00:00
|
|
|
if (tty_port_initialized(port)) {
|
2023-01-17 09:03:47 +00:00
|
|
|
tty_port_set_initialized(port, false);
|
2013-03-07 14:55:51 +00:00
|
|
|
/*
|
|
|
|
* Drop DTR/RTS if HUPCL is set. This causes any attached
|
|
|
|
* modem to hang up the line.
|
|
|
|
*/
|
|
|
|
if (tty && C_HUPCL(tty))
|
|
|
|
tty_port_lower_dtr_rts(port);
|
|
|
|
|
2019-04-03 07:40:53 +00:00
|
|
|
if (port->ops->shutdown)
|
2009-09-19 20:13:20 +00:00
|
|
|
port->ops->shutdown(port);
|
2013-03-07 14:55:48 +00:00
|
|
|
}
|
|
|
|
out:
|
2009-10-06 15:06:11 +00:00
|
|
|
mutex_unlock(&port->mutex);
|
2009-09-19 20:13:20 +00:00
|
|
|
}
|
|
|
|
|
2009-01-02 13:45:26 +00:00
|
|
|
/**
|
2021-11-26 08:15:59 +00:00
|
|
|
* tty_port_hangup - hangup helper
|
|
|
|
* @port: tty port
|
2009-01-02 13:45:26 +00:00
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* Perform port level tty hangup flag and count changes. Drop the tty
|
|
|
|
* reference.
|
2014-06-16 13:17:02 +00:00
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* Caller holds tty lock.
|
2009-01-02 13:45:26 +00:00
|
|
|
*/
|
|
|
|
void tty_port_hangup(struct tty_port *port)
|
|
|
|
{
|
2013-03-07 14:55:51 +00:00
|
|
|
struct tty_struct *tty;
|
2009-01-02 13:45:26 +00:00
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&port->lock, flags);
|
|
|
|
port->count = 0;
|
2013-03-07 14:55:51 +00:00
|
|
|
tty = port->tty;
|
|
|
|
if (tty)
|
|
|
|
set_bit(TTY_IO_ERROR, &tty->flags);
|
2009-01-02 13:45:26 +00:00
|
|
|
port->tty = NULL;
|
|
|
|
spin_unlock_irqrestore(&port->lock, flags);
|
2023-01-17 09:03:49 +00:00
|
|
|
tty_port_set_active(port, false);
|
2013-03-07 14:55:51 +00:00
|
|
|
tty_port_shutdown(port, tty);
|
|
|
|
tty_kref_put(tty);
|
2009-01-02 13:45:26 +00:00
|
|
|
wake_up_interruptible(&port->open_wait);
|
2009-09-19 20:13:31 +00:00
|
|
|
wake_up_interruptible(&port->delta_msr_wait);
|
2009-01-02 13:45:26 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tty_port_hangup);
|
|
|
|
|
2013-03-07 12:12:30 +00:00
|
|
|
/**
|
|
|
|
* tty_port_tty_hangup - helper to hang up a tty
|
|
|
|
* @port: tty port
|
2021-11-26 08:15:59 +00:00
|
|
|
* @check_clocal: hang only ttys with %CLOCAL unset?
|
2013-03-07 12:12:30 +00:00
|
|
|
*/
|
|
|
|
void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
|
|
|
|
{
|
|
|
|
struct tty_struct *tty = tty_port_tty_get(port);
|
|
|
|
|
2013-07-25 05:26:16 +00:00
|
|
|
if (tty && (!check_clocal || !C_CLOCAL(tty)))
|
2013-03-07 12:12:30 +00:00
|
|
|
tty_hangup(tty);
|
2013-07-25 05:26:16 +00:00
|
|
|
tty_kref_put(tty);
|
2013-03-07 12:12:30 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
|
|
|
|
|
2013-03-07 12:12:29 +00:00
|
|
|
/**
|
|
|
|
* tty_port_tty_wakeup - helper to wake up a tty
|
|
|
|
* @port: tty port
|
|
|
|
*/
|
|
|
|
void tty_port_tty_wakeup(struct tty_port *port)
|
|
|
|
{
|
2017-02-02 19:48:05 +00:00
|
|
|
port->client_ops->write_wakeup(port);
|
2013-03-07 12:12:29 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
|
|
|
|
|
2009-01-02 13:45:05 +00:00
|
|
|
/**
|
2021-11-26 08:15:59 +00:00
|
|
|
* tty_port_carrier_raised - carrier raised check
|
|
|
|
* @port: tty port
|
2009-01-02 13:45:05 +00:00
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* Wrapper for the carrier detect logic. For the moment this is used
|
|
|
|
* to hide some internal details. This will eventually become entirely
|
|
|
|
* internal to the tty port.
|
2009-01-02 13:45:05 +00:00
|
|
|
*/
|
2023-01-17 09:03:52 +00:00
|
|
|
bool tty_port_carrier_raised(struct tty_port *port)
|
2009-01-02 13:45:05 +00:00
|
|
|
{
|
2019-04-03 07:40:53 +00:00
|
|
|
if (port->ops->carrier_raised == NULL)
|
2023-01-17 09:03:52 +00:00
|
|
|
return true;
|
2009-01-02 13:45:05 +00:00
|
|
|
return port->ops->carrier_raised(port);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tty_port_carrier_raised);
|
2009-01-02 13:45:19 +00:00
|
|
|
|
|
|
|
/**
|
2021-11-26 08:15:59 +00:00
|
|
|
* tty_port_raise_dtr_rts - Raise DTR/RTS
|
|
|
|
* @port: tty port
|
2009-01-02 13:45:19 +00:00
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* Wrapper for the DTR/RTS raise logic. For the moment this is used to hide
|
|
|
|
* some internal details. This will eventually become entirely internal to the
|
|
|
|
* tty port.
|
2009-01-02 13:45:19 +00:00
|
|
|
*/
|
|
|
|
void tty_port_raise_dtr_rts(struct tty_port *port)
|
|
|
|
{
|
2019-04-03 07:40:53 +00:00
|
|
|
if (port->ops->dtr_rts)
|
2023-01-17 09:03:53 +00:00
|
|
|
port->ops->dtr_rts(port, true);
|
2009-01-02 13:45:19 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tty_port_raise_dtr_rts);
|
2009-01-02 13:46:10 +00:00
|
|
|
|
2009-06-11 11:24:17 +00:00
|
|
|
/**
|
2021-11-26 08:15:59 +00:00
|
|
|
* tty_port_lower_dtr_rts - Lower DTR/RTS
|
|
|
|
* @port: tty port
|
2009-06-11 11:24:17 +00:00
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* Wrapper for the DTR/RTS raise logic. For the moment this is used to hide
|
|
|
|
* some internal details. This will eventually become entirely internal to the
|
|
|
|
* tty port.
|
2009-06-11 11:24:17 +00:00
|
|
|
*/
|
|
|
|
void tty_port_lower_dtr_rts(struct tty_port *port)
|
|
|
|
{
|
2019-04-03 07:40:53 +00:00
|
|
|
if (port->ops->dtr_rts)
|
2023-01-17 09:03:53 +00:00
|
|
|
port->ops->dtr_rts(port, false);
|
2009-06-11 11:24:17 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tty_port_lower_dtr_rts);
|
|
|
|
|
2009-01-02 13:46:10 +00:00
|
|
|
/**
|
2021-11-26 08:15:59 +00:00
|
|
|
* tty_port_block_til_ready - Waiting logic for tty open
|
|
|
|
* @port: the tty port being opened
|
|
|
|
* @tty: the tty device being bound
|
|
|
|
* @filp: the file pointer of the opener or %NULL
|
|
|
|
*
|
|
|
|
* Implement the core POSIX/SuS tty behaviour when opening a tty device.
|
|
|
|
* Handles:
|
2009-01-02 13:46:10 +00:00
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* - hangup (both before and during)
|
|
|
|
* - non blocking open
|
|
|
|
* - rts/dtr/dcd
|
|
|
|
* - signals
|
|
|
|
* - port flags and counts
|
2009-01-02 13:46:10 +00:00
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* The passed @port must implement the @port->ops->carrier_raised method if it
|
|
|
|
* can do carrier detect and the @port->ops->dtr_rts method if it supports
|
|
|
|
* software management of these lines. Note that the dtr/rts raise is done each
|
|
|
|
* iteration as a hangup may have previously dropped them while we wait.
|
2014-06-16 13:17:01 +00:00
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* Caller holds tty lock.
|
2014-06-16 13:17:01 +00:00
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* Note: May drop and reacquire tty lock when blocking, so @tty and @port may
|
|
|
|
* have changed state (eg., may have been hung up).
|
2009-01-02 13:46:10 +00:00
|
|
|
*/
|
|
|
|
int tty_port_block_til_ready(struct tty_port *port,
|
|
|
|
struct tty_struct *tty, struct file *filp)
|
|
|
|
{
|
|
|
|
int do_clocal = 0, retval;
|
|
|
|
unsigned long flags;
|
2009-06-24 17:35:05 +00:00
|
|
|
DEFINE_WAIT(wait);
|
2009-01-02 13:46:10 +00:00
|
|
|
|
|
|
|
/* if non-blocking mode is set we can pass directly to open unless
|
2021-05-12 09:26:25 +00:00
|
|
|
* the port has just hung up or is in another error state.
|
|
|
|
*/
|
2016-04-10 00:06:48 +00:00
|
|
|
if (tty_io_error(tty)) {
|
2023-01-17 09:03:49 +00:00
|
|
|
tty_port_set_active(port, true);
|
2009-11-18 14:12:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-01-16 22:54:29 +00:00
|
|
|
if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
|
2009-10-28 20:12:32 +00:00
|
|
|
/* Indicate we are open */
|
2016-01-11 04:36:15 +00:00
|
|
|
if (C_BAUD(tty))
|
2009-10-28 20:12:32 +00:00
|
|
|
tty_port_raise_dtr_rts(port);
|
2023-01-17 09:03:49 +00:00
|
|
|
tty_port_set_active(port, true);
|
2009-01-02 13:46:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (C_CLOCAL(tty))
|
|
|
|
do_clocal = 1;
|
|
|
|
|
|
|
|
/* Block waiting until we can proceed. We may need to wait for the
|
2021-05-12 09:26:25 +00:00
|
|
|
* carrier, but we must also wait for any close that is in progress
|
|
|
|
* before the next open may complete.
|
|
|
|
*/
|
2009-01-02 13:46:10 +00:00
|
|
|
|
|
|
|
retval = 0;
|
|
|
|
|
|
|
|
/* The port lock protects the port counts */
|
|
|
|
spin_lock_irqsave(&port->lock, flags);
|
tty: Remove tty_hung_up_p() tests from tty drivers' open()
Since at least before 2.6.30, it has not been possible to observe
a hung up file pointer in a tty driver's open() method unless/until
the driver open() releases the tty_lock() (eg., before blocking).
This is because tty_open() adds the file pointer while holding
the tty_lock() _and_ doesn't release the lock until after calling
the tty driver's open() method. [ Before tty_lock(), this was
lock_kernel(). ]
Since __tty_hangup() first waits on the tty_lock() before
enumerating and hanging up the open file pointers, either
__tty_hangup() will wait for the tty_lock() or tty_open() will
not yet have added the file pointer. For example,
CPU 0 | CPU 1
|
tty_open | __tty_hangup
.. | ..
tty_lock | ..
tty_reopen | tty_lock / blocks
.. |
tty_add_file(tty, filp) |
.. |
tty->ops->open(tty, filp) |
tty_port_open |
tty_port_block_til_ready |
.. |
while (1) |
.. |
tty_unlock | / unblocks
schedule | for each filp on tty->tty_files
| f_ops = tty_hung_up_fops;
| ..
| tty_unlock
tty_lock |
.. |
tty_unlock |
Note that since tty_port_block_til_ready() and similar drop
the tty_lock while blocking, when woken, the file pointer
must then be tested for having been hung up.
Also, fix bit-rotted drivers that used extra_count to track the
port->count bump.
CC: Mikael Starvik <starvik@axis.com>
CC: Samuel Ortiz <samuel@sortiz.org>
CC: "David S. Miller" <davem@davemloft.net>
Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Acked-by: Jesper Nilsson <jesper.nilsson@axis.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-06-16 13:17:06 +00:00
|
|
|
port->count--;
|
2009-01-02 13:46:10 +00:00
|
|
|
port->blocked_open++;
|
|
|
|
spin_unlock_irqrestore(&port->lock, flags);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
/* Indicate we are open */
|
2016-04-10 00:53:25 +00:00
|
|
|
if (C_BAUD(tty) && tty_port_initialized(port))
|
2009-01-02 13:46:43 +00:00
|
|
|
tty_port_raise_dtr_rts(port);
|
2009-01-02 13:46:10 +00:00
|
|
|
|
2009-06-11 13:33:37 +00:00
|
|
|
prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
|
2009-10-06 15:06:21 +00:00
|
|
|
/* Check for a hangup or uninitialised port.
|
2021-05-12 09:26:25 +00:00
|
|
|
* Return accordingly.
|
|
|
|
*/
|
2016-04-10 00:53:25 +00:00
|
|
|
if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
|
2009-01-02 13:46:10 +00:00
|
|
|
if (port->flags & ASYNC_HUP_NOTIFY)
|
|
|
|
retval = -EAGAIN;
|
|
|
|
else
|
|
|
|
retval = -ERESTARTSYS;
|
|
|
|
break;
|
|
|
|
}
|
2012-01-12 21:55:15 +00:00
|
|
|
/*
|
|
|
|
* Probe the carrier. For devices with no carrier detect
|
|
|
|
* tty_port_carrier_raised will always return true.
|
|
|
|
* Never ask drivers if CLOCAL is set, this causes troubles
|
|
|
|
* on some hardware.
|
|
|
|
*/
|
tty: Remove ASYNC_CLOSING checks in open()/hangup() methods
Since at least before 2.6.30, tty drivers that do not drop the tty lock
while closing cannot observe ASYNC_CLOSING set while holding the
tty lock; this includes the tty driver's open() and hangup() methods,
since the tty core calls these methods holding the tty lock.
For these drivers, waiting for ASYNC_CLOSING to clear while opening
is not required, since this condition cannot occur. Similarly, even
when the open() method drops and reacquires the tty lock after
blocking, ASYNC_CLOSING cannot be set (again, for drivers that
do not drop the tty lock while closing).
Now that tty port drivers no longer drop the tty lock while closing
(since 'tty: Remove tty_wait_until_sent_from_close()'), the same
conditions apply: waiting for ASYNC_CLOSING to clear while opening
is not required, nor is re-checking ASYNC_CLOSING after dropping and
reacquiring the tty lock while blocking (eg., in *_block_til_ready()).
Note: The ASYNC_CLOSING flag state is still maintained since several
bitrotting drivers use it for (dubious) other purposes.
Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2015-10-10 20:00:52 +00:00
|
|
|
if (do_clocal || tty_port_carrier_raised(port))
|
2009-01-02 13:46:10 +00:00
|
|
|
break;
|
|
|
|
if (signal_pending(current)) {
|
|
|
|
retval = -ERESTARTSYS;
|
|
|
|
break;
|
|
|
|
}
|
2012-08-08 15:30:13 +00:00
|
|
|
tty_unlock(tty);
|
2009-01-02 13:46:10 +00:00
|
|
|
schedule();
|
2012-08-08 15:30:13 +00:00
|
|
|
tty_lock(tty);
|
2009-01-02 13:46:10 +00:00
|
|
|
}
|
2009-06-11 13:33:37 +00:00
|
|
|
finish_wait(&port->open_wait, &wait);
|
2009-01-02 13:46:10 +00:00
|
|
|
|
|
|
|
/* Update counts. A parallel hangup will have set count to zero and
|
2021-05-12 09:26:25 +00:00
|
|
|
* we must not mess that up further.
|
|
|
|
*/
|
2009-01-02 13:46:10 +00:00
|
|
|
spin_lock_irqsave(&port->lock, flags);
|
|
|
|
if (!tty_hung_up_p(filp))
|
|
|
|
port->count++;
|
|
|
|
port->blocked_open--;
|
|
|
|
spin_unlock_irqrestore(&port->lock, flags);
|
2016-04-10 00:53:22 +00:00
|
|
|
if (retval == 0)
|
2023-01-17 09:03:49 +00:00
|
|
|
tty_port_set_active(port, true);
|
2009-07-17 15:17:26 +00:00
|
|
|
return retval;
|
2009-01-02 13:46:10 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tty_port_block_til_ready);
|
|
|
|
|
2013-03-07 14:55:52 +00:00
|
|
|
static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
|
|
|
|
{
|
|
|
|
unsigned int bps = tty_get_baud_rate(tty);
|
|
|
|
long timeout;
|
|
|
|
|
|
|
|
if (bps > 1200) {
|
|
|
|
timeout = (HZ * 10 * port->drain_delay) / bps;
|
|
|
|
timeout = max_t(long, timeout, HZ / 10);
|
|
|
|
} else {
|
|
|
|
timeout = 2 * HZ;
|
|
|
|
}
|
|
|
|
schedule_timeout_interruptible(timeout);
|
|
|
|
}
|
|
|
|
|
2021-11-26 08:16:06 +00:00
|
|
|
/**
|
|
|
|
* tty_port_close_start - helper for tty->ops->close, part 1/2
|
|
|
|
* @port: tty_port of the device
|
|
|
|
* @tty: tty being closed
|
|
|
|
* @filp: passed file pointer
|
|
|
|
*
|
|
|
|
* Decrements and checks open count. Flushes the port if this is the last
|
|
|
|
* close. That means, dropping the data from the outpu buffer on the device and
|
|
|
|
* waiting for sending logic to finish. The rest of close handling is performed
|
|
|
|
* in tty_port_close_end().
|
|
|
|
*
|
|
|
|
* Locking: Caller holds tty lock.
|
|
|
|
*
|
|
|
|
* Return: 1 if this is the last close, otherwise 0
|
|
|
|
*/
|
2009-10-06 15:06:21 +00:00
|
|
|
int tty_port_close_start(struct tty_port *port,
|
|
|
|
struct tty_struct *tty, struct file *filp)
|
2009-01-02 13:46:50 +00:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
2014-11-05 17:40:03 +00:00
|
|
|
if (tty_hung_up_p(filp))
|
2009-01-02 13:46:50 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-11-05 17:40:03 +00:00
|
|
|
spin_lock_irqsave(&port->lock, flags);
|
2009-10-06 15:06:21 +00:00
|
|
|
if (tty->count == 1 && port->count != 1) {
|
2015-11-08 18:01:13 +00:00
|
|
|
tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
|
|
|
|
port->count);
|
2009-01-02 13:46:50 +00:00
|
|
|
port->count = 1;
|
|
|
|
}
|
|
|
|
if (--port->count < 0) {
|
2015-11-08 18:01:13 +00:00
|
|
|
tty_warn(tty, "%s: bad port count (%d)\n", __func__,
|
|
|
|
port->count);
|
2009-01-02 13:46:50 +00:00
|
|
|
port->count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port->count) {
|
|
|
|
spin_unlock_irqrestore(&port->lock, flags);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&port->lock, flags);
|
2013-03-07 14:55:53 +00:00
|
|
|
|
2014-06-16 13:17:03 +00:00
|
|
|
tty->closing = 1;
|
|
|
|
|
2016-04-10 00:53:25 +00:00
|
|
|
if (tty_port_initialized(port)) {
|
2013-03-07 14:55:53 +00:00
|
|
|
/* Don't block on a stalled port, just pull the chain */
|
2021-05-05 09:19:05 +00:00
|
|
|
if (tty->flow.tco_stopped)
|
2013-03-07 14:55:53 +00:00
|
|
|
tty_driver_flush_buffer(tty);
|
|
|
|
if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
|
tty: Remove tty_wait_until_sent_from_close()
tty_wait_until_sent_from_close() drops the tty lock while waiting
for the tty driver to finish sending previously accepted data (ie.,
data remaining in its write buffer and transmit fifo).
tty_wait_until_sent_from_close() was added by commit a57a7bf3fc7e
("TTY: define tty_wait_until_sent_from_close") to prevent the entire
tty subsystem from being unable to open new ttys while waiting for
one tty to close while output drained.
However, since commit 0911261d4cb6 ("tty: Don't take tty_mutex for tty
count changes"), holding a tty lock while closing does not prevent other
ttys from being opened/closed/hung up, but only prevents lifetime event
changes for the tty under lock.
Holding the tty lock while waiting for output to drain does prevent
parallel non-blocking opens (O_NONBLOCK) from advancing or returning
while the tty lock is held. However, all parallel opens _already_
block even if the tty lock is dropped while closing and the parallel
open advances. Blocking in open has been in mainline since at least 2.6.29
(see tty_port_block_til_ready(); note the test for O_NONBLOCK is _after_
the wait while ASYNC_CLOSING).
IOW, before this patch a non-blocking open will sleep anyway for the
_entire_ duration of a parallel hardware shutdown, and when it wakes, the
error return will cause a release of its tty, and it will restart with
a fresh attempt to open. Similarly with a blocking open that is already
waiting; when it's woken, the hardware shutdown has already completed
to ASYNC_INITIALIZED is not set, which forces a release and restart as
well.
So, holding the tty lock across the _entire_ close (which is what this
patch does), even while waiting for output to drain, is equivalent to
the current outcome wrt parallel opens.
Cc: Alan Cox <alan@linux.intel.com>
Cc: David Laight <David.Laight@aculab.com>
CC: Arnd Bergmann <arnd@arndb.de>
CC: Karsten Keil <isdn@linux-pingi.de>
CC: linuxppc-dev@lists.ozlabs.org
Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2015-10-10 20:00:51 +00:00
|
|
|
tty_wait_until_sent(tty, port->closing_wait);
|
2013-03-07 14:55:53 +00:00
|
|
|
if (port->drain_delay)
|
|
|
|
tty_port_drain_delay(port, tty);
|
|
|
|
}
|
2009-11-05 13:27:57 +00:00
|
|
|
/* Flush the ldisc buffering */
|
|
|
|
tty_ldisc_flush(tty);
|
|
|
|
|
2013-09-19 00:47:06 +00:00
|
|
|
/* Report to caller this is the last port reference */
|
2009-01-02 13:46:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tty_port_close_start);
|
|
|
|
|
2021-11-26 08:16:06 +00:00
|
|
|
/**
|
|
|
|
* tty_port_close_end - helper for tty->ops->close, part 2/2
|
|
|
|
* @port: tty_port of the device
|
|
|
|
* @tty: tty being closed
|
|
|
|
*
|
|
|
|
* This is a continuation of the first part: tty_port_close_start(). This
|
|
|
|
* should be called after turning off the device. It flushes the data from the
|
|
|
|
* line discipline and delays the close by @port->close_delay.
|
|
|
|
*
|
|
|
|
* Locking: Caller holds tty lock.
|
|
|
|
*/
|
2009-01-02 13:46:50 +00:00
|
|
|
void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
2014-11-05 17:40:05 +00:00
|
|
|
tty_ldisc_flush(tty);
|
2009-01-02 13:46:50 +00:00
|
|
|
tty->closing = 0;
|
|
|
|
|
2014-06-16 13:17:03 +00:00
|
|
|
spin_lock_irqsave(&port->lock, flags);
|
|
|
|
|
2009-01-02 13:46:50 +00:00
|
|
|
if (port->blocked_open) {
|
|
|
|
spin_unlock_irqrestore(&port->lock, flags);
|
2016-01-11 04:36:14 +00:00
|
|
|
if (port->close_delay)
|
|
|
|
msleep_interruptible(jiffies_to_msecs(port->close_delay));
|
2009-01-02 13:46:50 +00:00
|
|
|
spin_lock_irqsave(&port->lock, flags);
|
|
|
|
wake_up_interruptible(&port->open_wait);
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&port->lock, flags);
|
2023-01-17 09:03:49 +00:00
|
|
|
tty_port_set_active(port, false);
|
2009-01-02 13:46:50 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tty_port_close_end);
|
2009-09-19 20:13:20 +00:00
|
|
|
|
2021-11-26 08:15:59 +00:00
|
|
|
/**
|
|
|
|
* tty_port_close - generic tty->ops->close handler
|
|
|
|
* @port: tty_port of the device
|
|
|
|
* @tty: tty being closed
|
|
|
|
* @filp: passed file pointer
|
|
|
|
*
|
|
|
|
* It is a generic helper to be used in driver's @tty->ops->close. It wraps a
|
|
|
|
* sequence of tty_port_close_start(), tty_port_shutdown(), and
|
|
|
|
* tty_port_close_end(). The latter two are called only if this is the last
|
|
|
|
* close. See the respective functions for the details.
|
tty: Document locking for tty_port_close{,start,end}()
The tty lock is held when the tty driver's .close method is called
(from the two lone call-sites of tty_release() and __tty_hangup()).
The call-tree audit[1] of tty_port_close(), tty_port_close_start,
and tty_port_close_end() is a closed graph of the callers of these
3 functions; ie., all callers originate from only tty_release()
or __tty_hangup().
Of these callers, none drop the tty lock.
Also, document tty_port_close_start() may drop and reacquire the
tty lock in tty_wait_until_sent_from_close(), which means the tty
or tty_port may have changed state (but not reopened or hung up).
[1]
Call-tree audit of tty_port_close, tty_port_close_start, and tty_port_close_end()
tty_release()
tty->ops->close() --+
|
__tty_hangup() |
tty->ops->close() --+
|
+- rp_close():drivers/tty/rocket.c -------------------+
+- uart_close():drivers/tty/serial/serial_core.c -----+
| +- tty_port_close_start()
|
|
+- close():drivers/tty/synclinkmp.c ------------------+
+- rs_close():drivers/tty/amiserial.c ----------------+
+- gsmtty_close():drivers/tty/n_gsm.c ----------------+
+- mxser_close():drivers/tty/mxser.c -----------------+
+- close():drivers/tty/synclink_gt.c -----------------+
+- mgsl_close():drivers/tty/synclink.c ---------------+
+- isdn_tty_close():drivers/isdn/i4l/isdn_tty.c ------+
+- mgslpc_close():drivers/char/pcmcia/synclink_cs.c --+
+- ircomm_tty_close():net/irda/ircomm/ircomm_tty.c ---+
| |
rs_close():arch/ia64/hp/sim/simserial.c |
*line_close():arch/um/drivers/line.c |
gdm_tty_close():drivers/staging/gdm724x/gdm_tty.c
fwtty_close():drivers/staging/fwserial/fwserial.c
acm_tty_close():drivers/usb/class/cdc-acm.c
serial_close():drivers/usb/serial/usb-serial.c
pti_tty_driver_close():drivers/misc/pti.c
ipoctal_close():drivers/ipack/devices/ipoctal.c
cy_close():drivers/tty/cyclades.c
isicom_close():drivers/tty/isicom.c
dashtty_close():drivers/tty/metag_da.c
moxa_close():drivers/tty/moxa.c
goldfish_tty_close():drivers/tty/goldfish.c
ehv_bc_tty_close():drivers/tty/ehv_bytechan.c
kgdb_nmi_tty_close():drivers/tty/serial/kgdb_nmi.c
ifx_spi_close():drivers/tty/serial/ifx6x60.c
smd_tty_close():drivers/tty/serial/msm_smd_tty.c
ntty_close():drivers/tty/nozomi.c
capinc_tty_close():drivers/isdn/capi/capi.c
tpk_close():drivers/char/ttyprintk.c
sdio_uart_close():drivers/mmc/card/sdio_uart.c |
rfcomm_tty_close():net/bluetooth/rfcomm/tty.c |
| |
+- tty_port_close():drivers/tty/tty_port.c -----------+
|
+- tty_port_close_start()
+- tty_port_close_end()
* line_close() is the .close method for 2 um drivers,
declared in ./arch/um/drivers/stdio_console.c and
in ./arch/um/drivers/ssl.c, and not called directly
Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-06-16 13:16:59 +00:00
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* Locking: Caller holds tty lock
|
tty: Document locking for tty_port_close{,start,end}()
The tty lock is held when the tty driver's .close method is called
(from the two lone call-sites of tty_release() and __tty_hangup()).
The call-tree audit[1] of tty_port_close(), tty_port_close_start,
and tty_port_close_end() is a closed graph of the callers of these
3 functions; ie., all callers originate from only tty_release()
or __tty_hangup().
Of these callers, none drop the tty lock.
Also, document tty_port_close_start() may drop and reacquire the
tty lock in tty_wait_until_sent_from_close(), which means the tty
or tty_port may have changed state (but not reopened or hung up).
[1]
Call-tree audit of tty_port_close, tty_port_close_start, and tty_port_close_end()
tty_release()
tty->ops->close() --+
|
__tty_hangup() |
tty->ops->close() --+
|
+- rp_close():drivers/tty/rocket.c -------------------+
+- uart_close():drivers/tty/serial/serial_core.c -----+
| +- tty_port_close_start()
|
|
+- close():drivers/tty/synclinkmp.c ------------------+
+- rs_close():drivers/tty/amiserial.c ----------------+
+- gsmtty_close():drivers/tty/n_gsm.c ----------------+
+- mxser_close():drivers/tty/mxser.c -----------------+
+- close():drivers/tty/synclink_gt.c -----------------+
+- mgsl_close():drivers/tty/synclink.c ---------------+
+- isdn_tty_close():drivers/isdn/i4l/isdn_tty.c ------+
+- mgslpc_close():drivers/char/pcmcia/synclink_cs.c --+
+- ircomm_tty_close():net/irda/ircomm/ircomm_tty.c ---+
| |
rs_close():arch/ia64/hp/sim/simserial.c |
*line_close():arch/um/drivers/line.c |
gdm_tty_close():drivers/staging/gdm724x/gdm_tty.c
fwtty_close():drivers/staging/fwserial/fwserial.c
acm_tty_close():drivers/usb/class/cdc-acm.c
serial_close():drivers/usb/serial/usb-serial.c
pti_tty_driver_close():drivers/misc/pti.c
ipoctal_close():drivers/ipack/devices/ipoctal.c
cy_close():drivers/tty/cyclades.c
isicom_close():drivers/tty/isicom.c
dashtty_close():drivers/tty/metag_da.c
moxa_close():drivers/tty/moxa.c
goldfish_tty_close():drivers/tty/goldfish.c
ehv_bc_tty_close():drivers/tty/ehv_bytechan.c
kgdb_nmi_tty_close():drivers/tty/serial/kgdb_nmi.c
ifx_spi_close():drivers/tty/serial/ifx6x60.c
smd_tty_close():drivers/tty/serial/msm_smd_tty.c
ntty_close():drivers/tty/nozomi.c
capinc_tty_close():drivers/isdn/capi/capi.c
tpk_close():drivers/char/ttyprintk.c
sdio_uart_close():drivers/mmc/card/sdio_uart.c |
rfcomm_tty_close():net/bluetooth/rfcomm/tty.c |
| |
+- tty_port_close():drivers/tty/tty_port.c -----------+
|
+- tty_port_close_start()
+- tty_port_close_end()
* line_close() is the .close method for 2 um drivers,
declared in ./arch/um/drivers/stdio_console.c and
in ./arch/um/drivers/ssl.c, and not called directly
Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-06-16 13:16:59 +00:00
|
|
|
*/
|
2009-09-19 20:13:20 +00:00
|
|
|
void tty_port_close(struct tty_port *port, struct tty_struct *tty,
|
|
|
|
struct file *filp)
|
|
|
|
{
|
|
|
|
if (tty_port_close_start(port, tty, filp) == 0)
|
|
|
|
return;
|
2013-03-07 14:55:51 +00:00
|
|
|
tty_port_shutdown(port, tty);
|
2018-11-22 09:23:47 +00:00
|
|
|
if (!port->console)
|
|
|
|
set_bit(TTY_IO_ERROR, &tty->flags);
|
2009-09-19 20:13:20 +00:00
|
|
|
tty_port_close_end(port, tty);
|
|
|
|
tty_port_tty_set(port, NULL);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tty_port_close);
|
2009-10-06 15:06:11 +00:00
|
|
|
|
2012-08-07 19:47:49 +00:00
|
|
|
/**
|
|
|
|
* tty_port_install - generic tty->ops->install handler
|
|
|
|
* @port: tty_port of the device
|
|
|
|
* @driver: tty_driver for this device
|
|
|
|
* @tty: tty to be installed
|
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* It is the same as tty_standard_install() except the provided @port is linked
|
|
|
|
* to a concrete tty specified by @tty. Use this or tty_port_register_device()
|
|
|
|
* (or both). Call tty_port_link_device() as a last resort.
|
2012-08-07 19:47:49 +00:00
|
|
|
*/
|
2012-06-04 11:35:32 +00:00
|
|
|
int tty_port_install(struct tty_port *port, struct tty_driver *driver,
|
|
|
|
struct tty_struct *tty)
|
|
|
|
{
|
|
|
|
tty->port = port;
|
|
|
|
return tty_standard_install(driver, tty);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(tty_port_install);
|
|
|
|
|
2021-11-26 08:15:59 +00:00
|
|
|
/**
|
|
|
|
* tty_port_open - generic tty->ops->open handler
|
|
|
|
* @port: tty_port of the device
|
|
|
|
* @tty: tty to be opened
|
|
|
|
* @filp: passed file pointer
|
2014-06-16 13:17:00 +00:00
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* It is a generic helper to be used in driver's @tty->ops->open. It activates
|
|
|
|
* the devices using @port->ops->activate if not active already. And waits for
|
|
|
|
* the device to be ready using tty_port_block_til_ready() (e.g. raises
|
|
|
|
* DTR/CTS and waits for carrier).
|
|
|
|
*
|
2022-01-24 07:14:23 +00:00
|
|
|
* Note that @port->ops->shutdown is not called when @port->ops->activate
|
|
|
|
* returns an error (on the contrary, @tty->ops->close is).
|
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* Locking: Caller holds tty lock.
|
2014-06-16 13:17:00 +00:00
|
|
|
*
|
2021-11-26 08:15:59 +00:00
|
|
|
* Note: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
|
|
|
|
* @tty and @port may have changed state (eg., may be hung up now).
|
2014-06-16 13:17:00 +00:00
|
|
|
*/
|
2009-10-06 15:06:11 +00:00
|
|
|
int tty_port_open(struct tty_port *port, struct tty_struct *tty,
|
2009-10-06 15:06:21 +00:00
|
|
|
struct file *filp)
|
2009-10-06 15:06:11 +00:00
|
|
|
{
|
|
|
|
spin_lock_irq(&port->lock);
|
tty: Remove tty_hung_up_p() tests from tty drivers' open()
Since at least before 2.6.30, it has not been possible to observe
a hung up file pointer in a tty driver's open() method unless/until
the driver open() releases the tty_lock() (eg., before blocking).
This is because tty_open() adds the file pointer while holding
the tty_lock() _and_ doesn't release the lock until after calling
the tty driver's open() method. [ Before tty_lock(), this was
lock_kernel(). ]
Since __tty_hangup() first waits on the tty_lock() before
enumerating and hanging up the open file pointers, either
__tty_hangup() will wait for the tty_lock() or tty_open() will
not yet have added the file pointer. For example,
CPU 0 | CPU 1
|
tty_open | __tty_hangup
.. | ..
tty_lock | ..
tty_reopen | tty_lock / blocks
.. |
tty_add_file(tty, filp) |
.. |
tty->ops->open(tty, filp) |
tty_port_open |
tty_port_block_til_ready |
.. |
while (1) |
.. |
tty_unlock | / unblocks
schedule | for each filp on tty->tty_files
| f_ops = tty_hung_up_fops;
| ..
| tty_unlock
tty_lock |
.. |
tty_unlock |
Note that since tty_port_block_til_ready() and similar drop
the tty_lock while blocking, when woken, the file pointer
must then be tested for having been hung up.
Also, fix bit-rotted drivers that used extra_count to track the
port->count bump.
CC: Mikael Starvik <starvik@axis.com>
CC: Samuel Ortiz <samuel@sortiz.org>
CC: "David S. Miller" <davem@davemloft.net>
Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Acked-by: Jesper Nilsson <jesper.nilsson@axis.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-06-16 13:17:06 +00:00
|
|
|
++port->count;
|
2009-10-06 15:06:11 +00:00
|
|
|
spin_unlock_irq(&port->lock);
|
|
|
|
tty_port_tty_set(port, tty);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do the device-specific open only if the hardware isn't
|
|
|
|
* already initialized. Serialize open and shutdown using the
|
|
|
|
* port mutex.
|
|
|
|
*/
|
|
|
|
|
|
|
|
mutex_lock(&port->mutex);
|
|
|
|
|
2016-04-10 00:53:25 +00:00
|
|
|
if (!tty_port_initialized(port)) {
|
2009-11-30 13:16:57 +00:00
|
|
|
clear_bit(TTY_IO_ERROR, &tty->flags);
|
2019-04-03 07:40:53 +00:00
|
|
|
if (port->ops->activate) {
|
2009-10-06 15:06:11 +00:00
|
|
|
int retval = port->ops->activate(port, tty);
|
2021-05-12 09:26:24 +00:00
|
|
|
|
2009-10-06 15:06:11 +00:00
|
|
|
if (retval) {
|
2009-10-06 15:06:21 +00:00
|
|
|
mutex_unlock(&port->mutex);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 09:03:47 +00:00
|
|
|
tty_port_set_initialized(port, true);
|
2009-10-06 15:06:11 +00:00
|
|
|
}
|
|
|
|
mutex_unlock(&port->mutex);
|
|
|
|
return tty_port_block_til_ready(port, tty, filp);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tty_port_open);
|