196fd7ce5SGreg Kroah-Hartman /* 296fd7ce5SGreg Kroah-Hartman * Tty port functions 396fd7ce5SGreg Kroah-Hartman */ 496fd7ce5SGreg Kroah-Hartman 596fd7ce5SGreg Kroah-Hartman #include <linux/types.h> 696fd7ce5SGreg Kroah-Hartman #include <linux/errno.h> 796fd7ce5SGreg Kroah-Hartman #include <linux/tty.h> 896fd7ce5SGreg Kroah-Hartman #include <linux/tty_driver.h> 996fd7ce5SGreg Kroah-Hartman #include <linux/tty_flip.h> 1096fd7ce5SGreg Kroah-Hartman #include <linux/serial.h> 1196fd7ce5SGreg Kroah-Hartman #include <linux/timer.h> 1296fd7ce5SGreg Kroah-Hartman #include <linux/string.h> 1396fd7ce5SGreg Kroah-Hartman #include <linux/slab.h> 14*174cd4b1SIngo Molnar #include <linux/sched/signal.h> 1596fd7ce5SGreg Kroah-Hartman #include <linux/wait.h> 1696fd7ce5SGreg Kroah-Hartman #include <linux/bitops.h> 1796fd7ce5SGreg Kroah-Hartman #include <linux/delay.h> 1896fd7ce5SGreg Kroah-Hartman #include <linux/module.h> 198ee3fde0SRob Herring #include <linux/serdev.h> 2096fd7ce5SGreg Kroah-Hartman 21c3485ee0SRob Herring static int tty_port_default_receive_buf(struct tty_port *port, 22c3485ee0SRob Herring const unsigned char *p, 23c3485ee0SRob Herring const unsigned char *f, size_t count) 24c3485ee0SRob Herring { 25c3485ee0SRob Herring int ret; 26c3485ee0SRob Herring struct tty_struct *tty; 27c3485ee0SRob Herring struct tty_ldisc *disc; 28c3485ee0SRob Herring 29c3485ee0SRob Herring tty = READ_ONCE(port->itty); 30c3485ee0SRob Herring if (!tty) 31c3485ee0SRob Herring return 0; 32c3485ee0SRob Herring 33c3485ee0SRob Herring disc = tty_ldisc_ref(tty); 34c3485ee0SRob Herring if (!disc) 35c3485ee0SRob Herring return 0; 36c3485ee0SRob Herring 37c3485ee0SRob Herring ret = tty_ldisc_receive_buf(disc, p, (char *)f, count); 38c3485ee0SRob Herring 39c3485ee0SRob Herring tty_ldisc_deref(disc); 40c3485ee0SRob Herring 41c3485ee0SRob Herring return ret; 42c3485ee0SRob Herring } 43c3485ee0SRob Herring 44c3485ee0SRob Herring static void tty_port_default_wakeup(struct tty_port *port) 45c3485ee0SRob Herring { 46c3485ee0SRob Herring struct tty_struct *tty = tty_port_tty_get(port); 47c3485ee0SRob Herring 48c3485ee0SRob Herring if (tty) { 49c3485ee0SRob Herring tty_wakeup(tty); 50c3485ee0SRob Herring tty_kref_put(tty); 51c3485ee0SRob Herring } 52c3485ee0SRob Herring } 53c3485ee0SRob Herring 54c3485ee0SRob Herring static const struct tty_port_client_operations default_client_ops = { 55c3485ee0SRob Herring .receive_buf = tty_port_default_receive_buf, 56c3485ee0SRob Herring .write_wakeup = tty_port_default_wakeup, 57c3485ee0SRob Herring }; 58c3485ee0SRob Herring 5996fd7ce5SGreg Kroah-Hartman void tty_port_init(struct tty_port *port) 6096fd7ce5SGreg Kroah-Hartman { 6196fd7ce5SGreg Kroah-Hartman memset(port, 0, sizeof(*port)); 62ecbbfd44SJiri Slaby tty_buffer_init(port); 6396fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&port->open_wait); 6496fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&port->delta_msr_wait); 6596fd7ce5SGreg Kroah-Hartman mutex_init(&port->mutex); 6696fd7ce5SGreg Kroah-Hartman mutex_init(&port->buf_mutex); 6796fd7ce5SGreg Kroah-Hartman spin_lock_init(&port->lock); 6896fd7ce5SGreg Kroah-Hartman port->close_delay = (50 * HZ) / 100; 6996fd7ce5SGreg Kroah-Hartman port->closing_wait = (3000 * HZ) / 100; 70c3485ee0SRob Herring port->client_ops = &default_client_ops; 7196fd7ce5SGreg Kroah-Hartman kref_init(&port->kref); 7296fd7ce5SGreg Kroah-Hartman } 7396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_init); 7496fd7ce5SGreg Kroah-Hartman 7572a33bf5SJiri Slaby /** 762cb4ca02SJiri Slaby * tty_port_link_device - link tty and tty_port 772cb4ca02SJiri Slaby * @port: tty_port of the device 782cb4ca02SJiri Slaby * @driver: tty_driver for this device 792cb4ca02SJiri Slaby * @index: index of the tty 802cb4ca02SJiri Slaby * 812cb4ca02SJiri Slaby * Provide the tty layer wit ha link from a tty (specified by @index) to a 822cb4ca02SJiri Slaby * tty_port (@port). Use this only if neither tty_port_register_device nor 832cb4ca02SJiri Slaby * tty_port_install is used in the driver. If used, this has to be called before 842cb4ca02SJiri Slaby * tty_register_driver. 852cb4ca02SJiri Slaby */ 862cb4ca02SJiri Slaby void tty_port_link_device(struct tty_port *port, 872cb4ca02SJiri Slaby struct tty_driver *driver, unsigned index) 882cb4ca02SJiri Slaby { 892cb4ca02SJiri Slaby if (WARN_ON(index >= driver->num)) 902cb4ca02SJiri Slaby return; 912cb4ca02SJiri Slaby driver->ports[index] = port; 922cb4ca02SJiri Slaby } 932cb4ca02SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_link_device); 942cb4ca02SJiri Slaby 952cb4ca02SJiri Slaby /** 9672a33bf5SJiri Slaby * tty_port_register_device - register tty device 9772a33bf5SJiri Slaby * @port: tty_port of the device 9872a33bf5SJiri Slaby * @driver: tty_driver for this device 9972a33bf5SJiri Slaby * @index: index of the tty 10072a33bf5SJiri Slaby * @device: parent if exists, otherwise NULL 10172a33bf5SJiri Slaby * 10272a33bf5SJiri Slaby * It is the same as tty_register_device except the provided @port is linked to 10372a33bf5SJiri Slaby * a concrete tty specified by @index. Use this or tty_port_install (or both). 10472a33bf5SJiri Slaby * Call tty_port_link_device as a last resort. 10572a33bf5SJiri Slaby */ 106057eb856SJiri Slaby struct device *tty_port_register_device(struct tty_port *port, 107057eb856SJiri Slaby struct tty_driver *driver, unsigned index, 108057eb856SJiri Slaby struct device *device) 109057eb856SJiri Slaby { 1103086365dSRob Herring return tty_port_register_device_attr(port, driver, index, device, NULL, NULL); 111057eb856SJiri Slaby } 112057eb856SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_register_device); 113057eb856SJiri Slaby 114b1b79916STomas Hlavacek /** 115b1b79916STomas Hlavacek * tty_port_register_device_attr - register tty device 116b1b79916STomas Hlavacek * @port: tty_port of the device 117b1b79916STomas Hlavacek * @driver: tty_driver for this device 118b1b79916STomas Hlavacek * @index: index of the tty 119b1b79916STomas Hlavacek * @device: parent if exists, otherwise NULL 120b1b79916STomas Hlavacek * @drvdata: Driver data to be set to device. 121b1b79916STomas Hlavacek * @attr_grp: Attribute group to be set on device. 122b1b79916STomas Hlavacek * 123b1b79916STomas Hlavacek * It is the same as tty_register_device_attr except the provided @port is 124b1b79916STomas Hlavacek * linked to a concrete tty specified by @index. Use this or tty_port_install 125b1b79916STomas Hlavacek * (or both). Call tty_port_link_device as a last resort. 126b1b79916STomas Hlavacek */ 127b1b79916STomas Hlavacek struct device *tty_port_register_device_attr(struct tty_port *port, 128b1b79916STomas Hlavacek struct tty_driver *driver, unsigned index, 129b1b79916STomas Hlavacek struct device *device, void *drvdata, 130b1b79916STomas Hlavacek const struct attribute_group **attr_grp) 131b1b79916STomas Hlavacek { 1328ee3fde0SRob Herring struct device *dev; 1338ee3fde0SRob Herring 134b1b79916STomas Hlavacek tty_port_link_device(port, driver, index); 1358ee3fde0SRob Herring 1368ee3fde0SRob Herring dev = serdev_tty_port_register(port, device, driver, index); 1378ee3fde0SRob Herring if (PTR_ERR(dev) != -ENODEV) 1388ee3fde0SRob Herring /* Skip creating cdev if we registered a serdev device */ 1398ee3fde0SRob Herring return dev; 1408ee3fde0SRob Herring 141b1b79916STomas Hlavacek return tty_register_device_attr(driver, index, device, drvdata, 142b1b79916STomas Hlavacek attr_grp); 143b1b79916STomas Hlavacek } 144b1b79916STomas Hlavacek EXPORT_SYMBOL_GPL(tty_port_register_device_attr); 145b1b79916STomas Hlavacek 14696fd7ce5SGreg Kroah-Hartman int tty_port_alloc_xmit_buf(struct tty_port *port) 14796fd7ce5SGreg Kroah-Hartman { 14896fd7ce5SGreg Kroah-Hartman /* We may sleep in get_zeroed_page() */ 14996fd7ce5SGreg Kroah-Hartman mutex_lock(&port->buf_mutex); 15096fd7ce5SGreg Kroah-Hartman if (port->xmit_buf == NULL) 15196fd7ce5SGreg Kroah-Hartman port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL); 15296fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->buf_mutex); 15396fd7ce5SGreg Kroah-Hartman if (port->xmit_buf == NULL) 15496fd7ce5SGreg Kroah-Hartman return -ENOMEM; 15596fd7ce5SGreg Kroah-Hartman return 0; 15696fd7ce5SGreg Kroah-Hartman } 15796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_alloc_xmit_buf); 15896fd7ce5SGreg Kroah-Hartman 15996fd7ce5SGreg Kroah-Hartman void tty_port_free_xmit_buf(struct tty_port *port) 16096fd7ce5SGreg Kroah-Hartman { 16196fd7ce5SGreg Kroah-Hartman mutex_lock(&port->buf_mutex); 16296fd7ce5SGreg Kroah-Hartman if (port->xmit_buf != NULL) { 16396fd7ce5SGreg Kroah-Hartman free_page((unsigned long)port->xmit_buf); 16496fd7ce5SGreg Kroah-Hartman port->xmit_buf = NULL; 16596fd7ce5SGreg Kroah-Hartman } 16696fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->buf_mutex); 16796fd7ce5SGreg Kroah-Hartman } 16896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_free_xmit_buf); 16996fd7ce5SGreg Kroah-Hartman 170de274bfeSJiri Slaby /** 171de274bfeSJiri Slaby * tty_port_destroy -- destroy inited port 172de274bfeSJiri Slaby * @port: tty port to be doestroyed 173de274bfeSJiri Slaby * 174de274bfeSJiri Slaby * When a port was initialized using tty_port_init, one has to destroy the 175de274bfeSJiri Slaby * port by this function. Either indirectly by using tty_port refcounting 176de274bfeSJiri Slaby * (tty_port_put) or directly if refcounting is not used. 177de274bfeSJiri Slaby */ 178de274bfeSJiri Slaby void tty_port_destroy(struct tty_port *port) 179de274bfeSJiri Slaby { 180e176058fSPeter Hurley tty_buffer_cancel_work(port); 181de274bfeSJiri Slaby tty_buffer_free_all(port); 182de274bfeSJiri Slaby } 183de274bfeSJiri Slaby EXPORT_SYMBOL(tty_port_destroy); 184de274bfeSJiri Slaby 18596fd7ce5SGreg Kroah-Hartman static void tty_port_destructor(struct kref *kref) 18696fd7ce5SGreg Kroah-Hartman { 18796fd7ce5SGreg Kroah-Hartman struct tty_port *port = container_of(kref, struct tty_port, kref); 188e3bfea23SPeter Hurley 189e3bfea23SPeter Hurley /* check if last port ref was dropped before tty release */ 190e3bfea23SPeter Hurley if (WARN_ON(port->itty)) 191e3bfea23SPeter Hurley return; 1928ee3fde0SRob Herring 1938ee3fde0SRob Herring serdev_tty_port_unregister(port); 1948ee3fde0SRob Herring 19596fd7ce5SGreg Kroah-Hartman if (port->xmit_buf) 19696fd7ce5SGreg Kroah-Hartman free_page((unsigned long)port->xmit_buf); 197de274bfeSJiri Slaby tty_port_destroy(port); 19881c79838SJiri Slaby if (port->ops && port->ops->destruct) 19996fd7ce5SGreg Kroah-Hartman port->ops->destruct(port); 20096fd7ce5SGreg Kroah-Hartman else 20196fd7ce5SGreg Kroah-Hartman kfree(port); 20296fd7ce5SGreg Kroah-Hartman } 20396fd7ce5SGreg Kroah-Hartman 20496fd7ce5SGreg Kroah-Hartman void tty_port_put(struct tty_port *port) 20596fd7ce5SGreg Kroah-Hartman { 20696fd7ce5SGreg Kroah-Hartman if (port) 20796fd7ce5SGreg Kroah-Hartman kref_put(&port->kref, tty_port_destructor); 20896fd7ce5SGreg Kroah-Hartman } 20996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_put); 21096fd7ce5SGreg Kroah-Hartman 21196fd7ce5SGreg Kroah-Hartman /** 21296fd7ce5SGreg Kroah-Hartman * tty_port_tty_get - get a tty reference 21396fd7ce5SGreg Kroah-Hartman * @port: tty port 21496fd7ce5SGreg Kroah-Hartman * 21596fd7ce5SGreg Kroah-Hartman * Return a refcount protected tty instance or NULL if the port is not 21696fd7ce5SGreg Kroah-Hartman * associated with a tty (eg due to close or hangup) 21796fd7ce5SGreg Kroah-Hartman */ 21896fd7ce5SGreg Kroah-Hartman 21996fd7ce5SGreg Kroah-Hartman struct tty_struct *tty_port_tty_get(struct tty_port *port) 22096fd7ce5SGreg Kroah-Hartman { 22196fd7ce5SGreg Kroah-Hartman unsigned long flags; 22296fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 22396fd7ce5SGreg Kroah-Hartman 22496fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 22596fd7ce5SGreg Kroah-Hartman tty = tty_kref_get(port->tty); 22696fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 22796fd7ce5SGreg Kroah-Hartman return tty; 22896fd7ce5SGreg Kroah-Hartman } 22996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_get); 23096fd7ce5SGreg Kroah-Hartman 23196fd7ce5SGreg Kroah-Hartman /** 23296fd7ce5SGreg Kroah-Hartman * tty_port_tty_set - set the tty of a port 23396fd7ce5SGreg Kroah-Hartman * @port: tty port 23496fd7ce5SGreg Kroah-Hartman * @tty: the tty 23596fd7ce5SGreg Kroah-Hartman * 23696fd7ce5SGreg Kroah-Hartman * Associate the port and tty pair. Manages any internal refcounts. 23796fd7ce5SGreg Kroah-Hartman * Pass NULL to deassociate a port 23896fd7ce5SGreg Kroah-Hartman */ 23996fd7ce5SGreg Kroah-Hartman 24096fd7ce5SGreg Kroah-Hartman void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty) 24196fd7ce5SGreg Kroah-Hartman { 24296fd7ce5SGreg Kroah-Hartman unsigned long flags; 24396fd7ce5SGreg Kroah-Hartman 24496fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 24596fd7ce5SGreg Kroah-Hartman tty_kref_put(port->tty); 24696fd7ce5SGreg Kroah-Hartman port->tty = tty_kref_get(tty); 24796fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 24896fd7ce5SGreg Kroah-Hartman } 24996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_set); 25096fd7ce5SGreg Kroah-Hartman 251957dacaeSJohan Hovold static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty) 25296fd7ce5SGreg Kroah-Hartman { 25396fd7ce5SGreg Kroah-Hartman mutex_lock(&port->mutex); 2548bde9658SJohan Hovold if (port->console) 2558bde9658SJohan Hovold goto out; 2568bde9658SJohan Hovold 257d41861caSPeter Hurley if (tty_port_initialized(port)) { 258d41861caSPeter Hurley tty_port_set_initialized(port, 0); 259957dacaeSJohan Hovold /* 260957dacaeSJohan Hovold * Drop DTR/RTS if HUPCL is set. This causes any attached 261957dacaeSJohan Hovold * modem to hang up the line. 262957dacaeSJohan Hovold */ 263957dacaeSJohan Hovold if (tty && C_HUPCL(tty)) 264957dacaeSJohan Hovold tty_port_lower_dtr_rts(port); 265957dacaeSJohan Hovold 2668bde9658SJohan Hovold if (port->ops->shutdown) 26796fd7ce5SGreg Kroah-Hartman port->ops->shutdown(port); 2688bde9658SJohan Hovold } 2698bde9658SJohan Hovold out: 27096fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->mutex); 27196fd7ce5SGreg Kroah-Hartman } 27296fd7ce5SGreg Kroah-Hartman 27396fd7ce5SGreg Kroah-Hartman /** 27496fd7ce5SGreg Kroah-Hartman * tty_port_hangup - hangup helper 27596fd7ce5SGreg Kroah-Hartman * @port: tty port 27696fd7ce5SGreg Kroah-Hartman * 27796fd7ce5SGreg Kroah-Hartman * Perform port level tty hangup flag and count changes. Drop the tty 27896fd7ce5SGreg Kroah-Hartman * reference. 2799c9928bdSPeter Hurley * 2809c9928bdSPeter Hurley * Caller holds tty lock. 28196fd7ce5SGreg Kroah-Hartman */ 28296fd7ce5SGreg Kroah-Hartman 28396fd7ce5SGreg Kroah-Hartman void tty_port_hangup(struct tty_port *port) 28496fd7ce5SGreg Kroah-Hartman { 285957dacaeSJohan Hovold struct tty_struct *tty; 28696fd7ce5SGreg Kroah-Hartman unsigned long flags; 28796fd7ce5SGreg Kroah-Hartman 28896fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 28996fd7ce5SGreg Kroah-Hartman port->count = 0; 290957dacaeSJohan Hovold tty = port->tty; 291957dacaeSJohan Hovold if (tty) 292957dacaeSJohan Hovold set_bit(TTY_IO_ERROR, &tty->flags); 29396fd7ce5SGreg Kroah-Hartman port->tty = NULL; 29496fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 295807c8d81SPeter Hurley tty_port_set_active(port, 0); 296957dacaeSJohan Hovold tty_port_shutdown(port, tty); 297957dacaeSJohan Hovold tty_kref_put(tty); 29896fd7ce5SGreg Kroah-Hartman wake_up_interruptible(&port->open_wait); 29996fd7ce5SGreg Kroah-Hartman wake_up_interruptible(&port->delta_msr_wait); 30096fd7ce5SGreg Kroah-Hartman } 30196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_hangup); 30296fd7ce5SGreg Kroah-Hartman 30396fd7ce5SGreg Kroah-Hartman /** 304aa27a094SJiri Slaby * tty_port_tty_hangup - helper to hang up a tty 305aa27a094SJiri Slaby * 306aa27a094SJiri Slaby * @port: tty port 307aa27a094SJiri Slaby * @check_clocal: hang only ttys with CLOCAL unset? 308aa27a094SJiri Slaby */ 309aa27a094SJiri Slaby void tty_port_tty_hangup(struct tty_port *port, bool check_clocal) 310aa27a094SJiri Slaby { 311aa27a094SJiri Slaby struct tty_struct *tty = tty_port_tty_get(port); 312aa27a094SJiri Slaby 3131d9e689cSGianluca Anzolin if (tty && (!check_clocal || !C_CLOCAL(tty))) 314aa27a094SJiri Slaby tty_hangup(tty); 315aa27a094SJiri Slaby tty_kref_put(tty); 316aa27a094SJiri Slaby } 317aa27a094SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_tty_hangup); 318aa27a094SJiri Slaby 319aa27a094SJiri Slaby /** 3206aad04f2SJiri Slaby * tty_port_tty_wakeup - helper to wake up a tty 3216aad04f2SJiri Slaby * 3226aad04f2SJiri Slaby * @port: tty port 3236aad04f2SJiri Slaby */ 3246aad04f2SJiri Slaby void tty_port_tty_wakeup(struct tty_port *port) 3256aad04f2SJiri Slaby { 326c3485ee0SRob Herring port->client_ops->write_wakeup(port); 3276aad04f2SJiri Slaby } 3286aad04f2SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_tty_wakeup); 3296aad04f2SJiri Slaby 3306aad04f2SJiri Slaby /** 33196fd7ce5SGreg Kroah-Hartman * tty_port_carrier_raised - carrier raised check 33296fd7ce5SGreg Kroah-Hartman * @port: tty port 33396fd7ce5SGreg Kroah-Hartman * 33496fd7ce5SGreg Kroah-Hartman * Wrapper for the carrier detect logic. For the moment this is used 33596fd7ce5SGreg Kroah-Hartman * to hide some internal details. This will eventually become entirely 33696fd7ce5SGreg Kroah-Hartman * internal to the tty port. 33796fd7ce5SGreg Kroah-Hartman */ 33896fd7ce5SGreg Kroah-Hartman 33996fd7ce5SGreg Kroah-Hartman int tty_port_carrier_raised(struct tty_port *port) 34096fd7ce5SGreg Kroah-Hartman { 34196fd7ce5SGreg Kroah-Hartman if (port->ops->carrier_raised == NULL) 34296fd7ce5SGreg Kroah-Hartman return 1; 34396fd7ce5SGreg Kroah-Hartman return port->ops->carrier_raised(port); 34496fd7ce5SGreg Kroah-Hartman } 34596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_carrier_raised); 34696fd7ce5SGreg Kroah-Hartman 34796fd7ce5SGreg Kroah-Hartman /** 34896fd7ce5SGreg Kroah-Hartman * tty_port_raise_dtr_rts - Raise DTR/RTS 34996fd7ce5SGreg Kroah-Hartman * @port: tty port 35096fd7ce5SGreg Kroah-Hartman * 35196fd7ce5SGreg Kroah-Hartman * Wrapper for the DTR/RTS raise logic. For the moment this is used 35296fd7ce5SGreg Kroah-Hartman * to hide some internal details. This will eventually become entirely 35396fd7ce5SGreg Kroah-Hartman * internal to the tty port. 35496fd7ce5SGreg Kroah-Hartman */ 35596fd7ce5SGreg Kroah-Hartman 35696fd7ce5SGreg Kroah-Hartman void tty_port_raise_dtr_rts(struct tty_port *port) 35796fd7ce5SGreg Kroah-Hartman { 35896fd7ce5SGreg Kroah-Hartman if (port->ops->dtr_rts) 35996fd7ce5SGreg Kroah-Hartman port->ops->dtr_rts(port, 1); 36096fd7ce5SGreg Kroah-Hartman } 36196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_raise_dtr_rts); 36296fd7ce5SGreg Kroah-Hartman 36396fd7ce5SGreg Kroah-Hartman /** 36496fd7ce5SGreg Kroah-Hartman * tty_port_lower_dtr_rts - Lower DTR/RTS 36596fd7ce5SGreg Kroah-Hartman * @port: tty port 36696fd7ce5SGreg Kroah-Hartman * 36796fd7ce5SGreg Kroah-Hartman * Wrapper for the DTR/RTS raise logic. For the moment this is used 36896fd7ce5SGreg Kroah-Hartman * to hide some internal details. This will eventually become entirely 36996fd7ce5SGreg Kroah-Hartman * internal to the tty port. 37096fd7ce5SGreg Kroah-Hartman */ 37196fd7ce5SGreg Kroah-Hartman 37296fd7ce5SGreg Kroah-Hartman void tty_port_lower_dtr_rts(struct tty_port *port) 37396fd7ce5SGreg Kroah-Hartman { 37496fd7ce5SGreg Kroah-Hartman if (port->ops->dtr_rts) 37596fd7ce5SGreg Kroah-Hartman port->ops->dtr_rts(port, 0); 37696fd7ce5SGreg Kroah-Hartman } 37796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_lower_dtr_rts); 37896fd7ce5SGreg Kroah-Hartman 37996fd7ce5SGreg Kroah-Hartman /** 38096fd7ce5SGreg Kroah-Hartman * tty_port_block_til_ready - Waiting logic for tty open 38196fd7ce5SGreg Kroah-Hartman * @port: the tty port being opened 38296fd7ce5SGreg Kroah-Hartman * @tty: the tty device being bound 383ed3f0af8SAlan Cox * @filp: the file pointer of the opener or NULL 38496fd7ce5SGreg Kroah-Hartman * 38596fd7ce5SGreg Kroah-Hartman * Implement the core POSIX/SuS tty behaviour when opening a tty device. 38696fd7ce5SGreg Kroah-Hartman * Handles: 38796fd7ce5SGreg Kroah-Hartman * - hangup (both before and during) 38896fd7ce5SGreg Kroah-Hartman * - non blocking open 38996fd7ce5SGreg Kroah-Hartman * - rts/dtr/dcd 39096fd7ce5SGreg Kroah-Hartman * - signals 39196fd7ce5SGreg Kroah-Hartman * - port flags and counts 39296fd7ce5SGreg Kroah-Hartman * 39396fd7ce5SGreg Kroah-Hartman * The passed tty_port must implement the carrier_raised method if it can 39496fd7ce5SGreg Kroah-Hartman * do carrier detect and the dtr_rts method if it supports software 39596fd7ce5SGreg Kroah-Hartman * management of these lines. Note that the dtr/rts raise is done each 39696fd7ce5SGreg Kroah-Hartman * iteration as a hangup may have previously dropped them while we wait. 397c590f6b6SPeter Hurley * 398c590f6b6SPeter Hurley * Caller holds tty lock. 399c590f6b6SPeter Hurley * 400c590f6b6SPeter Hurley * NB: May drop and reacquire tty lock when blocking, so tty and tty_port 401c590f6b6SPeter Hurley * may have changed state (eg., may have been hung up). 40296fd7ce5SGreg Kroah-Hartman */ 40396fd7ce5SGreg Kroah-Hartman 40496fd7ce5SGreg Kroah-Hartman int tty_port_block_til_ready(struct tty_port *port, 40596fd7ce5SGreg Kroah-Hartman struct tty_struct *tty, struct file *filp) 40696fd7ce5SGreg Kroah-Hartman { 40796fd7ce5SGreg Kroah-Hartman int do_clocal = 0, retval; 40896fd7ce5SGreg Kroah-Hartman unsigned long flags; 40996fd7ce5SGreg Kroah-Hartman DEFINE_WAIT(wait); 41096fd7ce5SGreg Kroah-Hartman 41196fd7ce5SGreg Kroah-Hartman /* if non-blocking mode is set we can pass directly to open unless 41296fd7ce5SGreg Kroah-Hartman the port has just hung up or is in another error state */ 41318900ca6SPeter Hurley if (tty_io_error(tty)) { 414807c8d81SPeter Hurley tty_port_set_active(port, 1); 41596fd7ce5SGreg Kroah-Hartman return 0; 41696fd7ce5SGreg Kroah-Hartman } 417ed3f0af8SAlan Cox if (filp == NULL || (filp->f_flags & O_NONBLOCK)) { 41896fd7ce5SGreg Kroah-Hartman /* Indicate we are open */ 4199db276f8SPeter Hurley if (C_BAUD(tty)) 42096fd7ce5SGreg Kroah-Hartman tty_port_raise_dtr_rts(port); 421807c8d81SPeter Hurley tty_port_set_active(port, 1); 42296fd7ce5SGreg Kroah-Hartman return 0; 42396fd7ce5SGreg Kroah-Hartman } 42496fd7ce5SGreg Kroah-Hartman 42596fd7ce5SGreg Kroah-Hartman if (C_CLOCAL(tty)) 42696fd7ce5SGreg Kroah-Hartman do_clocal = 1; 42796fd7ce5SGreg Kroah-Hartman 42896fd7ce5SGreg Kroah-Hartman /* Block waiting until we can proceed. We may need to wait for the 42996fd7ce5SGreg Kroah-Hartman carrier, but we must also wait for any close that is in progress 43096fd7ce5SGreg Kroah-Hartman before the next open may complete */ 43196fd7ce5SGreg Kroah-Hartman 43296fd7ce5SGreg Kroah-Hartman retval = 0; 43396fd7ce5SGreg Kroah-Hartman 43496fd7ce5SGreg Kroah-Hartman /* The port lock protects the port counts */ 43596fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 43696fd7ce5SGreg Kroah-Hartman port->count--; 43796fd7ce5SGreg Kroah-Hartman port->blocked_open++; 43896fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 43996fd7ce5SGreg Kroah-Hartman 44096fd7ce5SGreg Kroah-Hartman while (1) { 44196fd7ce5SGreg Kroah-Hartman /* Indicate we are open */ 442d41861caSPeter Hurley if (C_BAUD(tty) && tty_port_initialized(port)) 44396fd7ce5SGreg Kroah-Hartman tty_port_raise_dtr_rts(port); 44496fd7ce5SGreg Kroah-Hartman 44596fd7ce5SGreg Kroah-Hartman prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE); 44696fd7ce5SGreg Kroah-Hartman /* Check for a hangup or uninitialised port. 44796fd7ce5SGreg Kroah-Hartman Return accordingly */ 448d41861caSPeter Hurley if (tty_hung_up_p(filp) || !tty_port_initialized(port)) { 44996fd7ce5SGreg Kroah-Hartman if (port->flags & ASYNC_HUP_NOTIFY) 45096fd7ce5SGreg Kroah-Hartman retval = -EAGAIN; 45196fd7ce5SGreg Kroah-Hartman else 45296fd7ce5SGreg Kroah-Hartman retval = -ERESTARTSYS; 45396fd7ce5SGreg Kroah-Hartman break; 45496fd7ce5SGreg Kroah-Hartman } 4550eee50afSJiri Slaby /* 4560eee50afSJiri Slaby * Probe the carrier. For devices with no carrier detect 4570eee50afSJiri Slaby * tty_port_carrier_raised will always return true. 4580eee50afSJiri Slaby * Never ask drivers if CLOCAL is set, this causes troubles 4590eee50afSJiri Slaby * on some hardware. 4600eee50afSJiri Slaby */ 461fef062cbSPeter Hurley if (do_clocal || tty_port_carrier_raised(port)) 46296fd7ce5SGreg Kroah-Hartman break; 46396fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) { 46496fd7ce5SGreg Kroah-Hartman retval = -ERESTARTSYS; 46596fd7ce5SGreg Kroah-Hartman break; 46696fd7ce5SGreg Kroah-Hartman } 46789c8d91eSAlan Cox tty_unlock(tty); 46896fd7ce5SGreg Kroah-Hartman schedule(); 46989c8d91eSAlan Cox tty_lock(tty); 47096fd7ce5SGreg Kroah-Hartman } 47196fd7ce5SGreg Kroah-Hartman finish_wait(&port->open_wait, &wait); 47296fd7ce5SGreg Kroah-Hartman 47396fd7ce5SGreg Kroah-Hartman /* Update counts. A parallel hangup will have set count to zero and 47496fd7ce5SGreg Kroah-Hartman we must not mess that up further */ 47596fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 47696fd7ce5SGreg Kroah-Hartman if (!tty_hung_up_p(filp)) 47796fd7ce5SGreg Kroah-Hartman port->count++; 47896fd7ce5SGreg Kroah-Hartman port->blocked_open--; 47996fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 480807c8d81SPeter Hurley if (retval == 0) 481807c8d81SPeter Hurley tty_port_set_active(port, 1); 48296fd7ce5SGreg Kroah-Hartman return retval; 48396fd7ce5SGreg Kroah-Hartman } 48496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_block_til_ready); 48596fd7ce5SGreg Kroah-Hartman 486b74414f5SJohan Hovold static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty) 487b74414f5SJohan Hovold { 488b74414f5SJohan Hovold unsigned int bps = tty_get_baud_rate(tty); 489b74414f5SJohan Hovold long timeout; 490b74414f5SJohan Hovold 491b74414f5SJohan Hovold if (bps > 1200) { 492b74414f5SJohan Hovold timeout = (HZ * 10 * port->drain_delay) / bps; 493b74414f5SJohan Hovold timeout = max_t(long, timeout, HZ / 10); 494b74414f5SJohan Hovold } else { 495b74414f5SJohan Hovold timeout = 2 * HZ; 496b74414f5SJohan Hovold } 497b74414f5SJohan Hovold schedule_timeout_interruptible(timeout); 498b74414f5SJohan Hovold } 499b74414f5SJohan Hovold 50079c1faa4SPeter Hurley /* Caller holds tty lock. */ 50196fd7ce5SGreg Kroah-Hartman int tty_port_close_start(struct tty_port *port, 50296fd7ce5SGreg Kroah-Hartman struct tty_struct *tty, struct file *filp) 50396fd7ce5SGreg Kroah-Hartman { 50496fd7ce5SGreg Kroah-Hartman unsigned long flags; 50596fd7ce5SGreg Kroah-Hartman 506633caba8SPeter Hurley if (tty_hung_up_p(filp)) 50796fd7ce5SGreg Kroah-Hartman return 0; 50896fd7ce5SGreg Kroah-Hartman 509633caba8SPeter Hurley spin_lock_irqsave(&port->lock, flags); 51096fd7ce5SGreg Kroah-Hartman if (tty->count == 1 && port->count != 1) { 511339f36baSPeter Hurley tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__, 51296fd7ce5SGreg Kroah-Hartman port->count); 51396fd7ce5SGreg Kroah-Hartman port->count = 1; 51496fd7ce5SGreg Kroah-Hartman } 51596fd7ce5SGreg Kroah-Hartman if (--port->count < 0) { 516339f36baSPeter Hurley tty_warn(tty, "%s: bad port count (%d)\n", __func__, 51796fd7ce5SGreg Kroah-Hartman port->count); 51896fd7ce5SGreg Kroah-Hartman port->count = 0; 51996fd7ce5SGreg Kroah-Hartman } 52096fd7ce5SGreg Kroah-Hartman 52196fd7ce5SGreg Kroah-Hartman if (port->count) { 52296fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 52396fd7ce5SGreg Kroah-Hartman return 0; 52496fd7ce5SGreg Kroah-Hartman } 52596fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 5260b2588caSJohan Hovold 527ddc7b758SPeter Hurley tty->closing = 1; 528ddc7b758SPeter Hurley 529d41861caSPeter Hurley if (tty_port_initialized(port)) { 53096fd7ce5SGreg Kroah-Hartman /* Don't block on a stalled port, just pull the chain */ 53196fd7ce5SGreg Kroah-Hartman if (tty->flow_stopped) 53296fd7ce5SGreg Kroah-Hartman tty_driver_flush_buffer(tty); 5330b2588caSJohan Hovold if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE) 53479c1faa4SPeter Hurley tty_wait_until_sent(tty, port->closing_wait); 535b74414f5SJohan Hovold if (port->drain_delay) 536b74414f5SJohan Hovold tty_port_drain_delay(port, tty); 5370b2588caSJohan Hovold } 53896fd7ce5SGreg Kroah-Hartman /* Flush the ldisc buffering */ 53996fd7ce5SGreg Kroah-Hartman tty_ldisc_flush(tty); 54096fd7ce5SGreg Kroah-Hartman 541469d6d06SPeter Hurley /* Report to caller this is the last port reference */ 54296fd7ce5SGreg Kroah-Hartman return 1; 54396fd7ce5SGreg Kroah-Hartman } 54496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_start); 54596fd7ce5SGreg Kroah-Hartman 5460733db91SPeter Hurley /* Caller holds tty lock */ 54796fd7ce5SGreg Kroah-Hartman void tty_port_close_end(struct tty_port *port, struct tty_struct *tty) 54896fd7ce5SGreg Kroah-Hartman { 54996fd7ce5SGreg Kroah-Hartman unsigned long flags; 55096fd7ce5SGreg Kroah-Hartman 5513f40f5b2SPeter Hurley tty_ldisc_flush(tty); 55296fd7ce5SGreg Kroah-Hartman tty->closing = 0; 55396fd7ce5SGreg Kroah-Hartman 554ddc7b758SPeter Hurley spin_lock_irqsave(&port->lock, flags); 555ddc7b758SPeter Hurley 55696fd7ce5SGreg Kroah-Hartman if (port->blocked_open) { 55796fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 5585823323eSPeter Hurley if (port->close_delay) 5595823323eSPeter Hurley msleep_interruptible(jiffies_to_msecs(port->close_delay)); 56096fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 56196fd7ce5SGreg Kroah-Hartman wake_up_interruptible(&port->open_wait); 56296fd7ce5SGreg Kroah-Hartman } 56396fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 564807c8d81SPeter Hurley tty_port_set_active(port, 0); 56596fd7ce5SGreg Kroah-Hartman } 56696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_end); 56796fd7ce5SGreg Kroah-Hartman 5680733db91SPeter Hurley /** 5690733db91SPeter Hurley * tty_port_close 5700733db91SPeter Hurley * 5710733db91SPeter Hurley * Caller holds tty lock 5720733db91SPeter Hurley */ 57396fd7ce5SGreg Kroah-Hartman void tty_port_close(struct tty_port *port, struct tty_struct *tty, 57496fd7ce5SGreg Kroah-Hartman struct file *filp) 57596fd7ce5SGreg Kroah-Hartman { 57696fd7ce5SGreg Kroah-Hartman if (tty_port_close_start(port, tty, filp) == 0) 57796fd7ce5SGreg Kroah-Hartman return; 578957dacaeSJohan Hovold tty_port_shutdown(port, tty); 57996fd7ce5SGreg Kroah-Hartman set_bit(TTY_IO_ERROR, &tty->flags); 58096fd7ce5SGreg Kroah-Hartman tty_port_close_end(port, tty); 58196fd7ce5SGreg Kroah-Hartman tty_port_tty_set(port, NULL); 58296fd7ce5SGreg Kroah-Hartman } 58396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close); 58496fd7ce5SGreg Kroah-Hartman 58572a33bf5SJiri Slaby /** 58672a33bf5SJiri Slaby * tty_port_install - generic tty->ops->install handler 58772a33bf5SJiri Slaby * @port: tty_port of the device 58872a33bf5SJiri Slaby * @driver: tty_driver for this device 58972a33bf5SJiri Slaby * @tty: tty to be installed 59072a33bf5SJiri Slaby * 59172a33bf5SJiri Slaby * It is the same as tty_standard_install except the provided @port is linked 59272a33bf5SJiri Slaby * to a concrete tty specified by @tty. Use this or tty_port_register_device 59372a33bf5SJiri Slaby * (or both). Call tty_port_link_device as a last resort. 59472a33bf5SJiri Slaby */ 595695586caSJiri Slaby int tty_port_install(struct tty_port *port, struct tty_driver *driver, 596695586caSJiri Slaby struct tty_struct *tty) 597695586caSJiri Slaby { 598695586caSJiri Slaby tty->port = port; 599695586caSJiri Slaby return tty_standard_install(driver, tty); 600695586caSJiri Slaby } 601695586caSJiri Slaby EXPORT_SYMBOL_GPL(tty_port_install); 602695586caSJiri Slaby 603addd4672SPeter Hurley /** 604addd4672SPeter Hurley * tty_port_open 605addd4672SPeter Hurley * 606addd4672SPeter Hurley * Caller holds tty lock. 607addd4672SPeter Hurley * 608addd4672SPeter Hurley * NB: may drop and reacquire tty lock (in tty_port_block_til_ready()) so 609addd4672SPeter Hurley * tty and tty_port may have changed state (eg., may be hung up now) 610addd4672SPeter Hurley */ 61196fd7ce5SGreg Kroah-Hartman int tty_port_open(struct tty_port *port, struct tty_struct *tty, 61296fd7ce5SGreg Kroah-Hartman struct file *filp) 61396fd7ce5SGreg Kroah-Hartman { 61496fd7ce5SGreg Kroah-Hartman spin_lock_irq(&port->lock); 61596fd7ce5SGreg Kroah-Hartman ++port->count; 61696fd7ce5SGreg Kroah-Hartman spin_unlock_irq(&port->lock); 61796fd7ce5SGreg Kroah-Hartman tty_port_tty_set(port, tty); 61896fd7ce5SGreg Kroah-Hartman 61996fd7ce5SGreg Kroah-Hartman /* 62096fd7ce5SGreg Kroah-Hartman * Do the device-specific open only if the hardware isn't 62196fd7ce5SGreg Kroah-Hartman * already initialized. Serialize open and shutdown using the 62296fd7ce5SGreg Kroah-Hartman * port mutex. 62396fd7ce5SGreg Kroah-Hartman */ 62496fd7ce5SGreg Kroah-Hartman 62596fd7ce5SGreg Kroah-Hartman mutex_lock(&port->mutex); 62696fd7ce5SGreg Kroah-Hartman 627d41861caSPeter Hurley if (!tty_port_initialized(port)) { 62896fd7ce5SGreg Kroah-Hartman clear_bit(TTY_IO_ERROR, &tty->flags); 62996fd7ce5SGreg Kroah-Hartman if (port->ops->activate) { 63096fd7ce5SGreg Kroah-Hartman int retval = port->ops->activate(port, tty); 63196fd7ce5SGreg Kroah-Hartman if (retval) { 63296fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->mutex); 63396fd7ce5SGreg Kroah-Hartman return retval; 63496fd7ce5SGreg Kroah-Hartman } 63596fd7ce5SGreg Kroah-Hartman } 636d41861caSPeter Hurley tty_port_set_initialized(port, 1); 63796fd7ce5SGreg Kroah-Hartman } 63896fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->mutex); 63996fd7ce5SGreg Kroah-Hartman return tty_port_block_til_ready(port, tty, filp); 64096fd7ce5SGreg Kroah-Hartman } 64196fd7ce5SGreg Kroah-Hartman 64296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_open); 643