1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 296fd7ce5SGreg Kroah-Hartman /* 396fd7ce5SGreg Kroah-Hartman * Tty port functions 496fd7ce5SGreg Kroah-Hartman */ 596fd7ce5SGreg Kroah-Hartman 696fd7ce5SGreg Kroah-Hartman #include <linux/types.h> 796fd7ce5SGreg Kroah-Hartman #include <linux/errno.h> 896fd7ce5SGreg Kroah-Hartman #include <linux/tty.h> 996fd7ce5SGreg Kroah-Hartman #include <linux/tty_driver.h> 1096fd7ce5SGreg Kroah-Hartman #include <linux/tty_flip.h> 1196fd7ce5SGreg Kroah-Hartman #include <linux/serial.h> 1296fd7ce5SGreg Kroah-Hartman #include <linux/timer.h> 1396fd7ce5SGreg Kroah-Hartman #include <linux/string.h> 1496fd7ce5SGreg Kroah-Hartman #include <linux/slab.h> 15174cd4b1SIngo Molnar #include <linux/sched/signal.h> 1696fd7ce5SGreg Kroah-Hartman #include <linux/wait.h> 1796fd7ce5SGreg Kroah-Hartman #include <linux/bitops.h> 1896fd7ce5SGreg Kroah-Hartman #include <linux/delay.h> 1996fd7ce5SGreg Kroah-Hartman #include <linux/module.h> 208cde11b2SJohan Hovold #include <linux/serdev.h> 2198602c01SGreg Kroah-Hartman #include "tty.h" 2296fd7ce5SGreg Kroah-Hartman 23c3485ee0SRob Herring static int tty_port_default_receive_buf(struct tty_port *port, 24c3485ee0SRob Herring const unsigned char *p, 25c3485ee0SRob Herring const unsigned char *f, size_t count) 26c3485ee0SRob Herring { 27c3485ee0SRob Herring int ret; 28c3485ee0SRob Herring struct tty_struct *tty; 29c3485ee0SRob Herring struct tty_ldisc *disc; 30c3485ee0SRob Herring 31c3485ee0SRob Herring tty = READ_ONCE(port->itty); 32c3485ee0SRob Herring if (!tty) 33c3485ee0SRob Herring return 0; 34c3485ee0SRob Herring 35c3485ee0SRob Herring disc = tty_ldisc_ref(tty); 36c3485ee0SRob Herring if (!disc) 37c3485ee0SRob Herring return 0; 38c3485ee0SRob Herring 39c3485ee0SRob Herring ret = tty_ldisc_receive_buf(disc, p, (char *)f, count); 40c3485ee0SRob Herring 41c3485ee0SRob Herring tty_ldisc_deref(disc); 42c3485ee0SRob Herring 43c3485ee0SRob Herring return ret; 44c3485ee0SRob Herring } 45c3485ee0SRob Herring 46c3485ee0SRob Herring static void tty_port_default_wakeup(struct tty_port *port) 47c3485ee0SRob Herring { 48c3485ee0SRob Herring struct tty_struct *tty = tty_port_tty_get(port); 49c3485ee0SRob Herring 50c3485ee0SRob Herring if (tty) { 51c3485ee0SRob Herring tty_wakeup(tty); 52c3485ee0SRob Herring tty_kref_put(tty); 53c3485ee0SRob Herring } 54c3485ee0SRob Herring } 55c3485ee0SRob Herring 560c5aae59SJohan Hovold const struct tty_port_client_operations tty_port_default_client_ops = { 57c3485ee0SRob Herring .receive_buf = tty_port_default_receive_buf, 58c3485ee0SRob Herring .write_wakeup = tty_port_default_wakeup, 59c3485ee0SRob Herring }; 600c5aae59SJohan Hovold EXPORT_SYMBOL_GPL(tty_port_default_client_ops); 61c3485ee0SRob Herring 6296fd7ce5SGreg Kroah-Hartman void tty_port_init(struct tty_port *port) 6396fd7ce5SGreg Kroah-Hartman { 6496fd7ce5SGreg Kroah-Hartman memset(port, 0, sizeof(*port)); 65ecbbfd44SJiri Slaby tty_buffer_init(port); 6696fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&port->open_wait); 6796fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&port->delta_msr_wait); 6896fd7ce5SGreg Kroah-Hartman mutex_init(&port->mutex); 6996fd7ce5SGreg Kroah-Hartman mutex_init(&port->buf_mutex); 7096fd7ce5SGreg Kroah-Hartman spin_lock_init(&port->lock); 7196fd7ce5SGreg Kroah-Hartman port->close_delay = (50 * HZ) / 100; 7296fd7ce5SGreg Kroah-Hartman port->closing_wait = (3000 * HZ) / 100; 730c5aae59SJohan Hovold port->client_ops = &tty_port_default_client_ops; 7496fd7ce5SGreg Kroah-Hartman kref_init(&port->kref); 7596fd7ce5SGreg Kroah-Hartman } 7696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_init); 7796fd7ce5SGreg Kroah-Hartman 7872a33bf5SJiri Slaby /** 792cb4ca02SJiri Slaby * tty_port_link_device - link tty and tty_port 802cb4ca02SJiri Slaby * @port: tty_port of the device 812cb4ca02SJiri Slaby * @driver: tty_driver for this device 822cb4ca02SJiri Slaby * @index: index of the tty 832cb4ca02SJiri Slaby * 842cb4ca02SJiri Slaby * Provide the tty layer with a link from a tty (specified by @index) to a 85*cb6f6f98SJiri Slaby * tty_port (@port). Use this only if neither tty_port_register_device() nor 86*cb6f6f98SJiri Slaby * tty_port_install() is used in the driver. If used, this has to be called 87*cb6f6f98SJiri Slaby * before tty_register_driver(). 882cb4ca02SJiri Slaby */ 892cb4ca02SJiri Slaby void tty_port_link_device(struct tty_port *port, 902cb4ca02SJiri Slaby struct tty_driver *driver, unsigned index) 912cb4ca02SJiri Slaby { 922cb4ca02SJiri Slaby if (WARN_ON(index >= driver->num)) 932cb4ca02SJiri Slaby return; 942cb4ca02SJiri Slaby driver->ports[index] = port; 952cb4ca02SJiri Slaby } 962cb4ca02SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_link_device); 972cb4ca02SJiri Slaby 982cb4ca02SJiri Slaby /** 9972a33bf5SJiri Slaby * tty_port_register_device - register tty device 10072a33bf5SJiri Slaby * @port: tty_port of the device 10172a33bf5SJiri Slaby * @driver: tty_driver for this device 10272a33bf5SJiri Slaby * @index: index of the tty 10372a33bf5SJiri Slaby * @device: parent if exists, otherwise NULL 10472a33bf5SJiri Slaby * 105*cb6f6f98SJiri Slaby * It is the same as tty_register_device() except the provided @port is linked 106*cb6f6f98SJiri Slaby * to a concrete tty specified by @index. Use this or tty_port_install() (or 107*cb6f6f98SJiri Slaby * both). Call tty_port_link_device() as a last resort. 10872a33bf5SJiri Slaby */ 109057eb856SJiri Slaby struct device *tty_port_register_device(struct tty_port *port, 110057eb856SJiri Slaby struct tty_driver *driver, unsigned index, 111057eb856SJiri Slaby struct device *device) 112057eb856SJiri Slaby { 1133086365dSRob Herring return tty_port_register_device_attr(port, driver, index, device, NULL, NULL); 114057eb856SJiri Slaby } 115057eb856SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_register_device); 116057eb856SJiri Slaby 117b1b79916STomas Hlavacek /** 118b1b79916STomas Hlavacek * tty_port_register_device_attr - register tty device 119b1b79916STomas Hlavacek * @port: tty_port of the device 120b1b79916STomas Hlavacek * @driver: tty_driver for this device 121b1b79916STomas Hlavacek * @index: index of the tty 122b1b79916STomas Hlavacek * @device: parent if exists, otherwise NULL 123b1b79916STomas Hlavacek * @drvdata: Driver data to be set to device. 124b1b79916STomas Hlavacek * @attr_grp: Attribute group to be set on device. 125b1b79916STomas Hlavacek * 126*cb6f6f98SJiri Slaby * It is the same as tty_register_device_attr() except the provided @port is 127*cb6f6f98SJiri Slaby * linked to a concrete tty specified by @index. Use this or tty_port_install() 128*cb6f6f98SJiri Slaby * (or both). Call tty_port_link_device() as a last resort. 129b1b79916STomas Hlavacek */ 130b1b79916STomas Hlavacek struct device *tty_port_register_device_attr(struct tty_port *port, 131b1b79916STomas Hlavacek struct tty_driver *driver, unsigned index, 132b1b79916STomas Hlavacek struct device *device, void *drvdata, 133b1b79916STomas Hlavacek const struct attribute_group **attr_grp) 134b1b79916STomas Hlavacek { 135b1b79916STomas Hlavacek tty_port_link_device(port, driver, index); 136b1b79916STomas Hlavacek return tty_register_device_attr(driver, index, device, drvdata, 137b1b79916STomas Hlavacek attr_grp); 138b1b79916STomas Hlavacek } 139b1b79916STomas Hlavacek EXPORT_SYMBOL_GPL(tty_port_register_device_attr); 140b1b79916STomas Hlavacek 1418cde11b2SJohan Hovold /** 1428cde11b2SJohan Hovold * tty_port_register_device_attr_serdev - register tty or serdev device 1438cde11b2SJohan Hovold * @port: tty_port of the device 1448cde11b2SJohan Hovold * @driver: tty_driver for this device 1458cde11b2SJohan Hovold * @index: index of the tty 1468cde11b2SJohan Hovold * @device: parent if exists, otherwise NULL 1478cde11b2SJohan Hovold * @drvdata: driver data for the device 1488cde11b2SJohan Hovold * @attr_grp: attribute group for the device 1498cde11b2SJohan Hovold * 1508cde11b2SJohan Hovold * Register a serdev or tty device depending on if the parent device has any 1518cde11b2SJohan Hovold * defined serdev clients or not. 1528cde11b2SJohan Hovold */ 1538cde11b2SJohan Hovold struct device *tty_port_register_device_attr_serdev(struct tty_port *port, 1548cde11b2SJohan Hovold struct tty_driver *driver, unsigned index, 1558cde11b2SJohan Hovold struct device *device, void *drvdata, 1568cde11b2SJohan Hovold const struct attribute_group **attr_grp) 1578cde11b2SJohan Hovold { 1588cde11b2SJohan Hovold struct device *dev; 1598cde11b2SJohan Hovold 1608cde11b2SJohan Hovold tty_port_link_device(port, driver, index); 1618cde11b2SJohan Hovold 1628cde11b2SJohan Hovold dev = serdev_tty_port_register(port, device, driver, index); 1638cde11b2SJohan Hovold if (PTR_ERR(dev) != -ENODEV) { 1648cde11b2SJohan Hovold /* Skip creating cdev if we registered a serdev device */ 1658cde11b2SJohan Hovold return dev; 1668cde11b2SJohan Hovold } 1678cde11b2SJohan Hovold 1688cde11b2SJohan Hovold return tty_register_device_attr(driver, index, device, drvdata, 1698cde11b2SJohan Hovold attr_grp); 1708cde11b2SJohan Hovold } 1718cde11b2SJohan Hovold EXPORT_SYMBOL_GPL(tty_port_register_device_attr_serdev); 1728cde11b2SJohan Hovold 1738cde11b2SJohan Hovold /** 1748cde11b2SJohan Hovold * tty_port_register_device_serdev - register tty or serdev device 1758cde11b2SJohan Hovold * @port: tty_port of the device 1768cde11b2SJohan Hovold * @driver: tty_driver for this device 1778cde11b2SJohan Hovold * @index: index of the tty 1788cde11b2SJohan Hovold * @device: parent if exists, otherwise NULL 1798cde11b2SJohan Hovold * 1808cde11b2SJohan Hovold * Register a serdev or tty device depending on if the parent device has any 1818cde11b2SJohan Hovold * defined serdev clients or not. 1828cde11b2SJohan Hovold */ 1838cde11b2SJohan Hovold struct device *tty_port_register_device_serdev(struct tty_port *port, 1848cde11b2SJohan Hovold struct tty_driver *driver, unsigned index, 1858cde11b2SJohan Hovold struct device *device) 1868cde11b2SJohan Hovold { 1878cde11b2SJohan Hovold return tty_port_register_device_attr_serdev(port, driver, index, 1888cde11b2SJohan Hovold device, NULL, NULL); 1898cde11b2SJohan Hovold } 1908cde11b2SJohan Hovold EXPORT_SYMBOL_GPL(tty_port_register_device_serdev); 1918cde11b2SJohan Hovold 1928cde11b2SJohan Hovold /** 1938cde11b2SJohan Hovold * tty_port_unregister_device - deregister a tty or serdev device 1948cde11b2SJohan Hovold * @port: tty_port of the device 1958cde11b2SJohan Hovold * @driver: tty_driver for this device 1968cde11b2SJohan Hovold * @index: index of the tty 1978cde11b2SJohan Hovold * 1988cde11b2SJohan Hovold * If a tty or serdev device is registered with a call to 1998cde11b2SJohan Hovold * tty_port_register_device_serdev() then this function must be called when 2008cde11b2SJohan Hovold * the device is gone. 2018cde11b2SJohan Hovold */ 2028cde11b2SJohan Hovold void tty_port_unregister_device(struct tty_port *port, 2038cde11b2SJohan Hovold struct tty_driver *driver, unsigned index) 2048cde11b2SJohan Hovold { 2058cde11b2SJohan Hovold int ret; 2068cde11b2SJohan Hovold 2078cde11b2SJohan Hovold ret = serdev_tty_port_unregister(port); 2088cde11b2SJohan Hovold if (ret == 0) 2098cde11b2SJohan Hovold return; 2108cde11b2SJohan Hovold 2118cde11b2SJohan Hovold tty_unregister_device(driver, index); 2128cde11b2SJohan Hovold } 2138cde11b2SJohan Hovold EXPORT_SYMBOL_GPL(tty_port_unregister_device); 2148cde11b2SJohan Hovold 21596fd7ce5SGreg Kroah-Hartman int tty_port_alloc_xmit_buf(struct tty_port *port) 21696fd7ce5SGreg Kroah-Hartman { 21796fd7ce5SGreg Kroah-Hartman /* We may sleep in get_zeroed_page() */ 21896fd7ce5SGreg Kroah-Hartman mutex_lock(&port->buf_mutex); 21996fd7ce5SGreg Kroah-Hartman if (port->xmit_buf == NULL) 22096fd7ce5SGreg Kroah-Hartman port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL); 22196fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->buf_mutex); 22296fd7ce5SGreg Kroah-Hartman if (port->xmit_buf == NULL) 22396fd7ce5SGreg Kroah-Hartman return -ENOMEM; 22496fd7ce5SGreg Kroah-Hartman return 0; 22596fd7ce5SGreg Kroah-Hartman } 22696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_alloc_xmit_buf); 22796fd7ce5SGreg Kroah-Hartman 22896fd7ce5SGreg Kroah-Hartman void tty_port_free_xmit_buf(struct tty_port *port) 22996fd7ce5SGreg Kroah-Hartman { 23096fd7ce5SGreg Kroah-Hartman mutex_lock(&port->buf_mutex); 23196fd7ce5SGreg Kroah-Hartman if (port->xmit_buf != NULL) { 23296fd7ce5SGreg Kroah-Hartman free_page((unsigned long)port->xmit_buf); 23396fd7ce5SGreg Kroah-Hartman port->xmit_buf = NULL; 23496fd7ce5SGreg Kroah-Hartman } 23596fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->buf_mutex); 23696fd7ce5SGreg Kroah-Hartman } 23796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_free_xmit_buf); 23896fd7ce5SGreg Kroah-Hartman 239de274bfeSJiri Slaby /** 240de274bfeSJiri Slaby * tty_port_destroy -- destroy inited port 2411926e5d3SAntonio Borneo * @port: tty port to be destroyed 242de274bfeSJiri Slaby * 243*cb6f6f98SJiri Slaby * When a port was initialized using tty_port_init(), one has to destroy the 244*cb6f6f98SJiri Slaby * port by this function. Either indirectly by using &tty_port refcounting 245*cb6f6f98SJiri Slaby * (tty_port_put()) or directly if refcounting is not used. 246de274bfeSJiri Slaby */ 247de274bfeSJiri Slaby void tty_port_destroy(struct tty_port *port) 248de274bfeSJiri Slaby { 249e176058fSPeter Hurley tty_buffer_cancel_work(port); 250de274bfeSJiri Slaby tty_buffer_free_all(port); 251de274bfeSJiri Slaby } 252de274bfeSJiri Slaby EXPORT_SYMBOL(tty_port_destroy); 253de274bfeSJiri Slaby 25496fd7ce5SGreg Kroah-Hartman static void tty_port_destructor(struct kref *kref) 25596fd7ce5SGreg Kroah-Hartman { 25696fd7ce5SGreg Kroah-Hartman struct tty_port *port = container_of(kref, struct tty_port, kref); 257e3bfea23SPeter Hurley 258e3bfea23SPeter Hurley /* check if last port ref was dropped before tty release */ 259e3bfea23SPeter Hurley if (WARN_ON(port->itty)) 260e3bfea23SPeter Hurley return; 26196fd7ce5SGreg Kroah-Hartman if (port->xmit_buf) 26296fd7ce5SGreg Kroah-Hartman free_page((unsigned long)port->xmit_buf); 263de274bfeSJiri Slaby tty_port_destroy(port); 26481c79838SJiri Slaby if (port->ops && port->ops->destruct) 26596fd7ce5SGreg Kroah-Hartman port->ops->destruct(port); 26696fd7ce5SGreg Kroah-Hartman else 26796fd7ce5SGreg Kroah-Hartman kfree(port); 26896fd7ce5SGreg Kroah-Hartman } 26996fd7ce5SGreg Kroah-Hartman 27096fd7ce5SGreg Kroah-Hartman void tty_port_put(struct tty_port *port) 27196fd7ce5SGreg Kroah-Hartman { 27296fd7ce5SGreg Kroah-Hartman if (port) 27396fd7ce5SGreg Kroah-Hartman kref_put(&port->kref, tty_port_destructor); 27496fd7ce5SGreg Kroah-Hartman } 27596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_put); 27696fd7ce5SGreg Kroah-Hartman 27796fd7ce5SGreg Kroah-Hartman /** 27896fd7ce5SGreg Kroah-Hartman * tty_port_tty_get - get a tty reference 27996fd7ce5SGreg Kroah-Hartman * @port: tty port 28096fd7ce5SGreg Kroah-Hartman * 281*cb6f6f98SJiri Slaby * Return a refcount protected tty instance or %NULL if the port is not 282*cb6f6f98SJiri Slaby * associated with a tty (eg due to close or hangup). 28396fd7ce5SGreg Kroah-Hartman */ 28496fd7ce5SGreg Kroah-Hartman struct tty_struct *tty_port_tty_get(struct tty_port *port) 28596fd7ce5SGreg Kroah-Hartman { 28696fd7ce5SGreg Kroah-Hartman unsigned long flags; 28796fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 28896fd7ce5SGreg Kroah-Hartman 28996fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 29096fd7ce5SGreg Kroah-Hartman tty = tty_kref_get(port->tty); 29196fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 29296fd7ce5SGreg Kroah-Hartman return tty; 29396fd7ce5SGreg Kroah-Hartman } 29496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_get); 29596fd7ce5SGreg Kroah-Hartman 29696fd7ce5SGreg Kroah-Hartman /** 29796fd7ce5SGreg Kroah-Hartman * tty_port_tty_set - set the tty of a port 29896fd7ce5SGreg Kroah-Hartman * @port: tty port 29996fd7ce5SGreg Kroah-Hartman * @tty: the tty 30096fd7ce5SGreg Kroah-Hartman * 301*cb6f6f98SJiri Slaby * Associate the port and tty pair. Manages any internal refcounts. Pass %NULL 302*cb6f6f98SJiri Slaby * to deassociate a port. 30396fd7ce5SGreg Kroah-Hartman */ 30496fd7ce5SGreg Kroah-Hartman void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty) 30596fd7ce5SGreg Kroah-Hartman { 30696fd7ce5SGreg Kroah-Hartman unsigned long flags; 30796fd7ce5SGreg Kroah-Hartman 30896fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 30996fd7ce5SGreg Kroah-Hartman tty_kref_put(port->tty); 31096fd7ce5SGreg Kroah-Hartman port->tty = tty_kref_get(tty); 31196fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 31296fd7ce5SGreg Kroah-Hartman } 31396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_set); 31496fd7ce5SGreg Kroah-Hartman 315957dacaeSJohan Hovold static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty) 31696fd7ce5SGreg Kroah-Hartman { 31796fd7ce5SGreg Kroah-Hartman mutex_lock(&port->mutex); 3188bde9658SJohan Hovold if (port->console) 3198bde9658SJohan Hovold goto out; 3208bde9658SJohan Hovold 321d41861caSPeter Hurley if (tty_port_initialized(port)) { 322d41861caSPeter Hurley tty_port_set_initialized(port, 0); 323957dacaeSJohan Hovold /* 324957dacaeSJohan Hovold * Drop DTR/RTS if HUPCL is set. This causes any attached 325957dacaeSJohan Hovold * modem to hang up the line. 326957dacaeSJohan Hovold */ 327957dacaeSJohan Hovold if (tty && C_HUPCL(tty)) 328957dacaeSJohan Hovold tty_port_lower_dtr_rts(port); 329957dacaeSJohan Hovold 3300d3cb6f6SJohan Hovold if (port->ops->shutdown) 33196fd7ce5SGreg Kroah-Hartman port->ops->shutdown(port); 3328bde9658SJohan Hovold } 3338bde9658SJohan Hovold out: 33496fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->mutex); 33596fd7ce5SGreg Kroah-Hartman } 33696fd7ce5SGreg Kroah-Hartman 33796fd7ce5SGreg Kroah-Hartman /** 33896fd7ce5SGreg Kroah-Hartman * tty_port_hangup - hangup helper 33996fd7ce5SGreg Kroah-Hartman * @port: tty port 34096fd7ce5SGreg Kroah-Hartman * 34196fd7ce5SGreg Kroah-Hartman * Perform port level tty hangup flag and count changes. Drop the tty 34296fd7ce5SGreg Kroah-Hartman * reference. 3439c9928bdSPeter Hurley * 3449c9928bdSPeter Hurley * Caller holds tty lock. 34596fd7ce5SGreg Kroah-Hartman */ 34696fd7ce5SGreg Kroah-Hartman void tty_port_hangup(struct tty_port *port) 34796fd7ce5SGreg Kroah-Hartman { 348957dacaeSJohan Hovold struct tty_struct *tty; 34996fd7ce5SGreg Kroah-Hartman unsigned long flags; 35096fd7ce5SGreg Kroah-Hartman 35196fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 35296fd7ce5SGreg Kroah-Hartman port->count = 0; 353957dacaeSJohan Hovold tty = port->tty; 354957dacaeSJohan Hovold if (tty) 355957dacaeSJohan Hovold set_bit(TTY_IO_ERROR, &tty->flags); 35696fd7ce5SGreg Kroah-Hartman port->tty = NULL; 35796fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 358807c8d81SPeter Hurley tty_port_set_active(port, 0); 359957dacaeSJohan Hovold tty_port_shutdown(port, tty); 360957dacaeSJohan Hovold tty_kref_put(tty); 36196fd7ce5SGreg Kroah-Hartman wake_up_interruptible(&port->open_wait); 36296fd7ce5SGreg Kroah-Hartman wake_up_interruptible(&port->delta_msr_wait); 36396fd7ce5SGreg Kroah-Hartman } 36496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_hangup); 36596fd7ce5SGreg Kroah-Hartman 36696fd7ce5SGreg Kroah-Hartman /** 367aa27a094SJiri Slaby * tty_port_tty_hangup - helper to hang up a tty 368aa27a094SJiri Slaby * @port: tty port 369*cb6f6f98SJiri Slaby * @check_clocal: hang only ttys with %CLOCAL unset? 370aa27a094SJiri Slaby */ 371aa27a094SJiri Slaby void tty_port_tty_hangup(struct tty_port *port, bool check_clocal) 372aa27a094SJiri Slaby { 373aa27a094SJiri Slaby struct tty_struct *tty = tty_port_tty_get(port); 374aa27a094SJiri Slaby 3751d9e689cSGianluca Anzolin if (tty && (!check_clocal || !C_CLOCAL(tty))) 376aa27a094SJiri Slaby tty_hangup(tty); 377aa27a094SJiri Slaby tty_kref_put(tty); 378aa27a094SJiri Slaby } 379aa27a094SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_tty_hangup); 380aa27a094SJiri Slaby 381aa27a094SJiri Slaby /** 3826aad04f2SJiri Slaby * tty_port_tty_wakeup - helper to wake up a tty 3836aad04f2SJiri Slaby * @port: tty port 3846aad04f2SJiri Slaby */ 3856aad04f2SJiri Slaby void tty_port_tty_wakeup(struct tty_port *port) 3866aad04f2SJiri Slaby { 387c3485ee0SRob Herring port->client_ops->write_wakeup(port); 3886aad04f2SJiri Slaby } 3896aad04f2SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_tty_wakeup); 3906aad04f2SJiri Slaby 3916aad04f2SJiri Slaby /** 39296fd7ce5SGreg Kroah-Hartman * tty_port_carrier_raised - carrier raised check 39396fd7ce5SGreg Kroah-Hartman * @port: tty port 39496fd7ce5SGreg Kroah-Hartman * 39596fd7ce5SGreg Kroah-Hartman * Wrapper for the carrier detect logic. For the moment this is used 39696fd7ce5SGreg Kroah-Hartman * to hide some internal details. This will eventually become entirely 39796fd7ce5SGreg Kroah-Hartman * internal to the tty port. 39896fd7ce5SGreg Kroah-Hartman */ 39996fd7ce5SGreg Kroah-Hartman int tty_port_carrier_raised(struct tty_port *port) 40096fd7ce5SGreg Kroah-Hartman { 4010d3cb6f6SJohan Hovold if (port->ops->carrier_raised == NULL) 40296fd7ce5SGreg Kroah-Hartman return 1; 40396fd7ce5SGreg Kroah-Hartman return port->ops->carrier_raised(port); 40496fd7ce5SGreg Kroah-Hartman } 40596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_carrier_raised); 40696fd7ce5SGreg Kroah-Hartman 40796fd7ce5SGreg Kroah-Hartman /** 40896fd7ce5SGreg Kroah-Hartman * tty_port_raise_dtr_rts - Raise DTR/RTS 40996fd7ce5SGreg Kroah-Hartman * @port: tty port 41096fd7ce5SGreg Kroah-Hartman * 411*cb6f6f98SJiri Slaby * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide 412*cb6f6f98SJiri Slaby * some internal details. This will eventually become entirely internal to the 413*cb6f6f98SJiri Slaby * tty port. 41496fd7ce5SGreg Kroah-Hartman */ 41596fd7ce5SGreg Kroah-Hartman void tty_port_raise_dtr_rts(struct tty_port *port) 41696fd7ce5SGreg Kroah-Hartman { 4170d3cb6f6SJohan Hovold if (port->ops->dtr_rts) 41896fd7ce5SGreg Kroah-Hartman port->ops->dtr_rts(port, 1); 41996fd7ce5SGreg Kroah-Hartman } 42096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_raise_dtr_rts); 42196fd7ce5SGreg Kroah-Hartman 42296fd7ce5SGreg Kroah-Hartman /** 42396fd7ce5SGreg Kroah-Hartman * tty_port_lower_dtr_rts - Lower DTR/RTS 42496fd7ce5SGreg Kroah-Hartman * @port: tty port 42596fd7ce5SGreg Kroah-Hartman * 426*cb6f6f98SJiri Slaby * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide 427*cb6f6f98SJiri Slaby * some internal details. This will eventually become entirely internal to the 428*cb6f6f98SJiri Slaby * tty port. 42996fd7ce5SGreg Kroah-Hartman */ 43096fd7ce5SGreg Kroah-Hartman void tty_port_lower_dtr_rts(struct tty_port *port) 43196fd7ce5SGreg Kroah-Hartman { 4320d3cb6f6SJohan Hovold if (port->ops->dtr_rts) 43396fd7ce5SGreg Kroah-Hartman port->ops->dtr_rts(port, 0); 43496fd7ce5SGreg Kroah-Hartman } 43596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_lower_dtr_rts); 43696fd7ce5SGreg Kroah-Hartman 43796fd7ce5SGreg Kroah-Hartman /** 43896fd7ce5SGreg Kroah-Hartman * tty_port_block_til_ready - Waiting logic for tty open 43996fd7ce5SGreg Kroah-Hartman * @port: the tty port being opened 44096fd7ce5SGreg Kroah-Hartman * @tty: the tty device being bound 441*cb6f6f98SJiri Slaby * @filp: the file pointer of the opener or %NULL 44296fd7ce5SGreg Kroah-Hartman * 44396fd7ce5SGreg Kroah-Hartman * Implement the core POSIX/SuS tty behaviour when opening a tty device. 44496fd7ce5SGreg Kroah-Hartman * Handles: 445*cb6f6f98SJiri Slaby * 44696fd7ce5SGreg Kroah-Hartman * - hangup (both before and during) 44796fd7ce5SGreg Kroah-Hartman * - non blocking open 44896fd7ce5SGreg Kroah-Hartman * - rts/dtr/dcd 44996fd7ce5SGreg Kroah-Hartman * - signals 45096fd7ce5SGreg Kroah-Hartman * - port flags and counts 45196fd7ce5SGreg Kroah-Hartman * 452*cb6f6f98SJiri Slaby * The passed @port must implement the @port->ops->carrier_raised method if it 453*cb6f6f98SJiri Slaby * can do carrier detect and the @port->ops->dtr_rts method if it supports 454*cb6f6f98SJiri Slaby * software management of these lines. Note that the dtr/rts raise is done each 45596fd7ce5SGreg Kroah-Hartman * iteration as a hangup may have previously dropped them while we wait. 456c590f6b6SPeter Hurley * 457c590f6b6SPeter Hurley * Caller holds tty lock. 458c590f6b6SPeter Hurley * 459*cb6f6f98SJiri Slaby * Note: May drop and reacquire tty lock when blocking, so @tty and @port may 460*cb6f6f98SJiri Slaby * have changed state (eg., may have been hung up). 46196fd7ce5SGreg Kroah-Hartman */ 46296fd7ce5SGreg Kroah-Hartman int tty_port_block_til_ready(struct tty_port *port, 46396fd7ce5SGreg Kroah-Hartman struct tty_struct *tty, struct file *filp) 46496fd7ce5SGreg Kroah-Hartman { 46596fd7ce5SGreg Kroah-Hartman int do_clocal = 0, retval; 46696fd7ce5SGreg Kroah-Hartman unsigned long flags; 46796fd7ce5SGreg Kroah-Hartman DEFINE_WAIT(wait); 46896fd7ce5SGreg Kroah-Hartman 46996fd7ce5SGreg Kroah-Hartman /* if non-blocking mode is set we can pass directly to open unless 4701df92640SXiaofei Tan * the port has just hung up or is in another error state. 4711df92640SXiaofei Tan */ 47218900ca6SPeter Hurley if (tty_io_error(tty)) { 473807c8d81SPeter Hurley tty_port_set_active(port, 1); 47496fd7ce5SGreg Kroah-Hartman return 0; 47596fd7ce5SGreg Kroah-Hartman } 476ed3f0af8SAlan Cox if (filp == NULL || (filp->f_flags & O_NONBLOCK)) { 47796fd7ce5SGreg Kroah-Hartman /* Indicate we are open */ 4789db276f8SPeter Hurley if (C_BAUD(tty)) 47996fd7ce5SGreg Kroah-Hartman tty_port_raise_dtr_rts(port); 480807c8d81SPeter Hurley tty_port_set_active(port, 1); 48196fd7ce5SGreg Kroah-Hartman return 0; 48296fd7ce5SGreg Kroah-Hartman } 48396fd7ce5SGreg Kroah-Hartman 48496fd7ce5SGreg Kroah-Hartman if (C_CLOCAL(tty)) 48596fd7ce5SGreg Kroah-Hartman do_clocal = 1; 48696fd7ce5SGreg Kroah-Hartman 48796fd7ce5SGreg Kroah-Hartman /* Block waiting until we can proceed. We may need to wait for the 4881df92640SXiaofei Tan * carrier, but we must also wait for any close that is in progress 4891df92640SXiaofei Tan * before the next open may complete. 4901df92640SXiaofei Tan */ 49196fd7ce5SGreg Kroah-Hartman 49296fd7ce5SGreg Kroah-Hartman retval = 0; 49396fd7ce5SGreg Kroah-Hartman 49496fd7ce5SGreg Kroah-Hartman /* The port lock protects the port counts */ 49596fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 49696fd7ce5SGreg Kroah-Hartman port->count--; 49796fd7ce5SGreg Kroah-Hartman port->blocked_open++; 49896fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 49996fd7ce5SGreg Kroah-Hartman 50096fd7ce5SGreg Kroah-Hartman while (1) { 50196fd7ce5SGreg Kroah-Hartman /* Indicate we are open */ 502d41861caSPeter Hurley if (C_BAUD(tty) && tty_port_initialized(port)) 50396fd7ce5SGreg Kroah-Hartman tty_port_raise_dtr_rts(port); 50496fd7ce5SGreg Kroah-Hartman 50596fd7ce5SGreg Kroah-Hartman prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE); 50696fd7ce5SGreg Kroah-Hartman /* Check for a hangup or uninitialised port. 5071df92640SXiaofei Tan * Return accordingly. 5081df92640SXiaofei Tan */ 509d41861caSPeter Hurley if (tty_hung_up_p(filp) || !tty_port_initialized(port)) { 51096fd7ce5SGreg Kroah-Hartman if (port->flags & ASYNC_HUP_NOTIFY) 51196fd7ce5SGreg Kroah-Hartman retval = -EAGAIN; 51296fd7ce5SGreg Kroah-Hartman else 51396fd7ce5SGreg Kroah-Hartman retval = -ERESTARTSYS; 51496fd7ce5SGreg Kroah-Hartman break; 51596fd7ce5SGreg Kroah-Hartman } 5160eee50afSJiri Slaby /* 5170eee50afSJiri Slaby * Probe the carrier. For devices with no carrier detect 5180eee50afSJiri Slaby * tty_port_carrier_raised will always return true. 5190eee50afSJiri Slaby * Never ask drivers if CLOCAL is set, this causes troubles 5200eee50afSJiri Slaby * on some hardware. 5210eee50afSJiri Slaby */ 522fef062cbSPeter Hurley if (do_clocal || tty_port_carrier_raised(port)) 52396fd7ce5SGreg Kroah-Hartman break; 52496fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) { 52596fd7ce5SGreg Kroah-Hartman retval = -ERESTARTSYS; 52696fd7ce5SGreg Kroah-Hartman break; 52796fd7ce5SGreg Kroah-Hartman } 52889c8d91eSAlan Cox tty_unlock(tty); 52996fd7ce5SGreg Kroah-Hartman schedule(); 53089c8d91eSAlan Cox tty_lock(tty); 53196fd7ce5SGreg Kroah-Hartman } 53296fd7ce5SGreg Kroah-Hartman finish_wait(&port->open_wait, &wait); 53396fd7ce5SGreg Kroah-Hartman 53496fd7ce5SGreg Kroah-Hartman /* Update counts. A parallel hangup will have set count to zero and 5351df92640SXiaofei Tan * we must not mess that up further. 5361df92640SXiaofei Tan */ 53796fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 53896fd7ce5SGreg Kroah-Hartman if (!tty_hung_up_p(filp)) 53996fd7ce5SGreg Kroah-Hartman port->count++; 54096fd7ce5SGreg Kroah-Hartman port->blocked_open--; 54196fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 542807c8d81SPeter Hurley if (retval == 0) 543807c8d81SPeter Hurley tty_port_set_active(port, 1); 54496fd7ce5SGreg Kroah-Hartman return retval; 54596fd7ce5SGreg Kroah-Hartman } 54696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_block_til_ready); 54796fd7ce5SGreg Kroah-Hartman 548b74414f5SJohan Hovold static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty) 549b74414f5SJohan Hovold { 550b74414f5SJohan Hovold unsigned int bps = tty_get_baud_rate(tty); 551b74414f5SJohan Hovold long timeout; 552b74414f5SJohan Hovold 553b74414f5SJohan Hovold if (bps > 1200) { 554b74414f5SJohan Hovold timeout = (HZ * 10 * port->drain_delay) / bps; 555b74414f5SJohan Hovold timeout = max_t(long, timeout, HZ / 10); 556b74414f5SJohan Hovold } else { 557b74414f5SJohan Hovold timeout = 2 * HZ; 558b74414f5SJohan Hovold } 559b74414f5SJohan Hovold schedule_timeout_interruptible(timeout); 560b74414f5SJohan Hovold } 561b74414f5SJohan Hovold 56279c1faa4SPeter Hurley /* Caller holds tty lock. */ 56396fd7ce5SGreg Kroah-Hartman int tty_port_close_start(struct tty_port *port, 56496fd7ce5SGreg Kroah-Hartman struct tty_struct *tty, struct file *filp) 56596fd7ce5SGreg Kroah-Hartman { 56696fd7ce5SGreg Kroah-Hartman unsigned long flags; 56796fd7ce5SGreg Kroah-Hartman 568633caba8SPeter Hurley if (tty_hung_up_p(filp)) 56996fd7ce5SGreg Kroah-Hartman return 0; 57096fd7ce5SGreg Kroah-Hartman 571633caba8SPeter Hurley spin_lock_irqsave(&port->lock, flags); 57296fd7ce5SGreg Kroah-Hartman if (tty->count == 1 && port->count != 1) { 573339f36baSPeter Hurley tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__, 57496fd7ce5SGreg Kroah-Hartman port->count); 57596fd7ce5SGreg Kroah-Hartman port->count = 1; 57696fd7ce5SGreg Kroah-Hartman } 57796fd7ce5SGreg Kroah-Hartman if (--port->count < 0) { 578339f36baSPeter Hurley tty_warn(tty, "%s: bad port count (%d)\n", __func__, 57996fd7ce5SGreg Kroah-Hartman port->count); 58096fd7ce5SGreg Kroah-Hartman port->count = 0; 58196fd7ce5SGreg Kroah-Hartman } 58296fd7ce5SGreg Kroah-Hartman 58396fd7ce5SGreg Kroah-Hartman if (port->count) { 58496fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 58596fd7ce5SGreg Kroah-Hartman return 0; 58696fd7ce5SGreg Kroah-Hartman } 58796fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 5880b2588caSJohan Hovold 589ddc7b758SPeter Hurley tty->closing = 1; 590ddc7b758SPeter Hurley 591d41861caSPeter Hurley if (tty_port_initialized(port)) { 59296fd7ce5SGreg Kroah-Hartman /* Don't block on a stalled port, just pull the chain */ 5936e94dbc7SJiri Slaby if (tty->flow.tco_stopped) 59496fd7ce5SGreg Kroah-Hartman tty_driver_flush_buffer(tty); 5950b2588caSJohan Hovold if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE) 59679c1faa4SPeter Hurley tty_wait_until_sent(tty, port->closing_wait); 597b74414f5SJohan Hovold if (port->drain_delay) 598b74414f5SJohan Hovold tty_port_drain_delay(port, tty); 5990b2588caSJohan Hovold } 60096fd7ce5SGreg Kroah-Hartman /* Flush the ldisc buffering */ 60196fd7ce5SGreg Kroah-Hartman tty_ldisc_flush(tty); 60296fd7ce5SGreg Kroah-Hartman 603469d6d06SPeter Hurley /* Report to caller this is the last port reference */ 60496fd7ce5SGreg Kroah-Hartman return 1; 60596fd7ce5SGreg Kroah-Hartman } 60696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_start); 60796fd7ce5SGreg Kroah-Hartman 6080733db91SPeter Hurley /* Caller holds tty lock */ 60996fd7ce5SGreg Kroah-Hartman void tty_port_close_end(struct tty_port *port, struct tty_struct *tty) 61096fd7ce5SGreg Kroah-Hartman { 61196fd7ce5SGreg Kroah-Hartman unsigned long flags; 61296fd7ce5SGreg Kroah-Hartman 6133f40f5b2SPeter Hurley tty_ldisc_flush(tty); 61496fd7ce5SGreg Kroah-Hartman tty->closing = 0; 61596fd7ce5SGreg Kroah-Hartman 616ddc7b758SPeter Hurley spin_lock_irqsave(&port->lock, flags); 617ddc7b758SPeter Hurley 61896fd7ce5SGreg Kroah-Hartman if (port->blocked_open) { 61996fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 6205823323eSPeter Hurley if (port->close_delay) 6215823323eSPeter Hurley msleep_interruptible(jiffies_to_msecs(port->close_delay)); 62296fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 62396fd7ce5SGreg Kroah-Hartman wake_up_interruptible(&port->open_wait); 62496fd7ce5SGreg Kroah-Hartman } 62596fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 626807c8d81SPeter Hurley tty_port_set_active(port, 0); 62796fd7ce5SGreg Kroah-Hartman } 62896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_end); 62996fd7ce5SGreg Kroah-Hartman 630*cb6f6f98SJiri Slaby /** 631*cb6f6f98SJiri Slaby * tty_port_close - generic tty->ops->close handler 632*cb6f6f98SJiri Slaby * @port: tty_port of the device 633*cb6f6f98SJiri Slaby * @tty: tty being closed 634*cb6f6f98SJiri Slaby * @filp: passed file pointer 6350733db91SPeter Hurley * 636*cb6f6f98SJiri Slaby * It is a generic helper to be used in driver's @tty->ops->close. It wraps a 637*cb6f6f98SJiri Slaby * sequence of tty_port_close_start(), tty_port_shutdown(), and 638*cb6f6f98SJiri Slaby * tty_port_close_end(). The latter two are called only if this is the last 639*cb6f6f98SJiri Slaby * close. See the respective functions for the details. 640*cb6f6f98SJiri Slaby * 641*cb6f6f98SJiri Slaby * Locking: Caller holds tty lock 6420733db91SPeter Hurley */ 64396fd7ce5SGreg Kroah-Hartman void tty_port_close(struct tty_port *port, struct tty_struct *tty, 64496fd7ce5SGreg Kroah-Hartman struct file *filp) 64596fd7ce5SGreg Kroah-Hartman { 64696fd7ce5SGreg Kroah-Hartman if (tty_port_close_start(port, tty, filp) == 0) 64796fd7ce5SGreg Kroah-Hartman return; 648957dacaeSJohan Hovold tty_port_shutdown(port, tty); 6492a486026SChanho Park if (!port->console) 65096fd7ce5SGreg Kroah-Hartman set_bit(TTY_IO_ERROR, &tty->flags); 65196fd7ce5SGreg Kroah-Hartman tty_port_close_end(port, tty); 65296fd7ce5SGreg Kroah-Hartman tty_port_tty_set(port, NULL); 65396fd7ce5SGreg Kroah-Hartman } 65496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close); 65596fd7ce5SGreg Kroah-Hartman 65672a33bf5SJiri Slaby /** 65772a33bf5SJiri Slaby * tty_port_install - generic tty->ops->install handler 65872a33bf5SJiri Slaby * @port: tty_port of the device 65972a33bf5SJiri Slaby * @driver: tty_driver for this device 66072a33bf5SJiri Slaby * @tty: tty to be installed 66172a33bf5SJiri Slaby * 662*cb6f6f98SJiri Slaby * It is the same as tty_standard_install() except the provided @port is linked 663*cb6f6f98SJiri Slaby * to a concrete tty specified by @tty. Use this or tty_port_register_device() 664*cb6f6f98SJiri Slaby * (or both). Call tty_port_link_device() as a last resort. 66572a33bf5SJiri Slaby */ 666695586caSJiri Slaby int tty_port_install(struct tty_port *port, struct tty_driver *driver, 667695586caSJiri Slaby struct tty_struct *tty) 668695586caSJiri Slaby { 669695586caSJiri Slaby tty->port = port; 670695586caSJiri Slaby return tty_standard_install(driver, tty); 671695586caSJiri Slaby } 672695586caSJiri Slaby EXPORT_SYMBOL_GPL(tty_port_install); 673695586caSJiri Slaby 674*cb6f6f98SJiri Slaby /** 675*cb6f6f98SJiri Slaby * tty_port_open - generic tty->ops->open handler 676*cb6f6f98SJiri Slaby * @port: tty_port of the device 677*cb6f6f98SJiri Slaby * @tty: tty to be opened 678*cb6f6f98SJiri Slaby * @filp: passed file pointer 679addd4672SPeter Hurley * 680*cb6f6f98SJiri Slaby * It is a generic helper to be used in driver's @tty->ops->open. It activates 681*cb6f6f98SJiri Slaby * the devices using @port->ops->activate if not active already. And waits for 682*cb6f6f98SJiri Slaby * the device to be ready using tty_port_block_til_ready() (e.g. raises 683*cb6f6f98SJiri Slaby * DTR/CTS and waits for carrier). 684addd4672SPeter Hurley * 685*cb6f6f98SJiri Slaby * Locking: Caller holds tty lock. 686*cb6f6f98SJiri Slaby * 687*cb6f6f98SJiri Slaby * Note: may drop and reacquire tty lock (in tty_port_block_til_ready()) so 688*cb6f6f98SJiri Slaby * @tty and @port may have changed state (eg., may be hung up now). 689addd4672SPeter Hurley */ 69096fd7ce5SGreg Kroah-Hartman int tty_port_open(struct tty_port *port, struct tty_struct *tty, 69196fd7ce5SGreg Kroah-Hartman struct file *filp) 69296fd7ce5SGreg Kroah-Hartman { 69396fd7ce5SGreg Kroah-Hartman spin_lock_irq(&port->lock); 69496fd7ce5SGreg Kroah-Hartman ++port->count; 69596fd7ce5SGreg Kroah-Hartman spin_unlock_irq(&port->lock); 69696fd7ce5SGreg Kroah-Hartman tty_port_tty_set(port, tty); 69796fd7ce5SGreg Kroah-Hartman 69896fd7ce5SGreg Kroah-Hartman /* 69996fd7ce5SGreg Kroah-Hartman * Do the device-specific open only if the hardware isn't 70096fd7ce5SGreg Kroah-Hartman * already initialized. Serialize open and shutdown using the 70196fd7ce5SGreg Kroah-Hartman * port mutex. 70296fd7ce5SGreg Kroah-Hartman */ 70396fd7ce5SGreg Kroah-Hartman 70496fd7ce5SGreg Kroah-Hartman mutex_lock(&port->mutex); 70596fd7ce5SGreg Kroah-Hartman 706d41861caSPeter Hurley if (!tty_port_initialized(port)) { 70796fd7ce5SGreg Kroah-Hartman clear_bit(TTY_IO_ERROR, &tty->flags); 7080d3cb6f6SJohan Hovold if (port->ops->activate) { 70996fd7ce5SGreg Kroah-Hartman int retval = port->ops->activate(port, tty); 71054ad59a2SXiaofei Tan 71196fd7ce5SGreg Kroah-Hartman if (retval) { 71296fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->mutex); 71396fd7ce5SGreg Kroah-Hartman return retval; 71496fd7ce5SGreg Kroah-Hartman } 71596fd7ce5SGreg Kroah-Hartman } 716d41861caSPeter Hurley tty_port_set_initialized(port, 1); 71796fd7ce5SGreg Kroah-Hartman } 71896fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->mutex); 71996fd7ce5SGreg Kroah-Hartman return tty_port_block_til_ready(port, tty, filp); 72096fd7ce5SGreg Kroah-Hartman } 72196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_open); 722