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> 1496fd7ce5SGreg Kroah-Hartman #include <linux/sched.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> 1996fd7ce5SGreg Kroah-Hartman 2096fd7ce5SGreg Kroah-Hartman void tty_port_init(struct tty_port *port) 2196fd7ce5SGreg Kroah-Hartman { 2296fd7ce5SGreg Kroah-Hartman memset(port, 0, sizeof(*port)); 23ecbbfd44SJiri Slaby tty_buffer_init(port); 2496fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&port->open_wait); 2596fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&port->close_wait); 2696fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&port->delta_msr_wait); 2796fd7ce5SGreg Kroah-Hartman mutex_init(&port->mutex); 2896fd7ce5SGreg Kroah-Hartman mutex_init(&port->buf_mutex); 2996fd7ce5SGreg Kroah-Hartman spin_lock_init(&port->lock); 3096fd7ce5SGreg Kroah-Hartman port->close_delay = (50 * HZ) / 100; 3196fd7ce5SGreg Kroah-Hartman port->closing_wait = (3000 * HZ) / 100; 3296fd7ce5SGreg Kroah-Hartman kref_init(&port->kref); 3396fd7ce5SGreg Kroah-Hartman } 3496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_init); 3596fd7ce5SGreg Kroah-Hartman 3672a33bf5SJiri Slaby /** 372cb4ca02SJiri Slaby * tty_port_link_device - link tty and tty_port 382cb4ca02SJiri Slaby * @port: tty_port of the device 392cb4ca02SJiri Slaby * @driver: tty_driver for this device 402cb4ca02SJiri Slaby * @index: index of the tty 412cb4ca02SJiri Slaby * 422cb4ca02SJiri Slaby * Provide the tty layer wit ha link from a tty (specified by @index) to a 432cb4ca02SJiri Slaby * tty_port (@port). Use this only if neither tty_port_register_device nor 442cb4ca02SJiri Slaby * tty_port_install is used in the driver. If used, this has to be called before 452cb4ca02SJiri Slaby * tty_register_driver. 462cb4ca02SJiri Slaby */ 472cb4ca02SJiri Slaby void tty_port_link_device(struct tty_port *port, 482cb4ca02SJiri Slaby struct tty_driver *driver, unsigned index) 492cb4ca02SJiri Slaby { 502cb4ca02SJiri Slaby if (WARN_ON(index >= driver->num)) 512cb4ca02SJiri Slaby return; 522cb4ca02SJiri Slaby driver->ports[index] = port; 532cb4ca02SJiri Slaby } 542cb4ca02SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_link_device); 552cb4ca02SJiri Slaby 562cb4ca02SJiri Slaby /** 5772a33bf5SJiri Slaby * tty_port_register_device - register tty device 5872a33bf5SJiri Slaby * @port: tty_port of the device 5972a33bf5SJiri Slaby * @driver: tty_driver for this device 6072a33bf5SJiri Slaby * @index: index of the tty 6172a33bf5SJiri Slaby * @device: parent if exists, otherwise NULL 6272a33bf5SJiri Slaby * 6372a33bf5SJiri Slaby * It is the same as tty_register_device except the provided @port is linked to 6472a33bf5SJiri Slaby * a concrete tty specified by @index. Use this or tty_port_install (or both). 6572a33bf5SJiri Slaby * Call tty_port_link_device as a last resort. 6672a33bf5SJiri Slaby */ 67057eb856SJiri Slaby struct device *tty_port_register_device(struct tty_port *port, 68057eb856SJiri Slaby struct tty_driver *driver, unsigned index, 69057eb856SJiri Slaby struct device *device) 70057eb856SJiri Slaby { 712cb4ca02SJiri Slaby tty_port_link_device(port, driver, index); 72057eb856SJiri Slaby return tty_register_device(driver, index, device); 73057eb856SJiri Slaby } 74057eb856SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_register_device); 75057eb856SJiri Slaby 76b1b79916STomas Hlavacek /** 77b1b79916STomas Hlavacek * tty_port_register_device_attr - register tty device 78b1b79916STomas Hlavacek * @port: tty_port of the device 79b1b79916STomas Hlavacek * @driver: tty_driver for this device 80b1b79916STomas Hlavacek * @index: index of the tty 81b1b79916STomas Hlavacek * @device: parent if exists, otherwise NULL 82b1b79916STomas Hlavacek * @drvdata: Driver data to be set to device. 83b1b79916STomas Hlavacek * @attr_grp: Attribute group to be set on device. 84b1b79916STomas Hlavacek * 85b1b79916STomas Hlavacek * It is the same as tty_register_device_attr except the provided @port is 86b1b79916STomas Hlavacek * linked to a concrete tty specified by @index. Use this or tty_port_install 87b1b79916STomas Hlavacek * (or both). Call tty_port_link_device as a last resort. 88b1b79916STomas Hlavacek */ 89b1b79916STomas Hlavacek struct device *tty_port_register_device_attr(struct tty_port *port, 90b1b79916STomas Hlavacek struct tty_driver *driver, unsigned index, 91b1b79916STomas Hlavacek struct device *device, void *drvdata, 92b1b79916STomas Hlavacek const struct attribute_group **attr_grp) 93b1b79916STomas Hlavacek { 94b1b79916STomas Hlavacek tty_port_link_device(port, driver, index); 95b1b79916STomas Hlavacek return tty_register_device_attr(driver, index, device, drvdata, 96b1b79916STomas Hlavacek attr_grp); 97b1b79916STomas Hlavacek } 98b1b79916STomas Hlavacek EXPORT_SYMBOL_GPL(tty_port_register_device_attr); 99b1b79916STomas Hlavacek 10096fd7ce5SGreg Kroah-Hartman int tty_port_alloc_xmit_buf(struct tty_port *port) 10196fd7ce5SGreg Kroah-Hartman { 10296fd7ce5SGreg Kroah-Hartman /* We may sleep in get_zeroed_page() */ 10396fd7ce5SGreg Kroah-Hartman mutex_lock(&port->buf_mutex); 10496fd7ce5SGreg Kroah-Hartman if (port->xmit_buf == NULL) 10596fd7ce5SGreg Kroah-Hartman port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL); 10696fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->buf_mutex); 10796fd7ce5SGreg Kroah-Hartman if (port->xmit_buf == NULL) 10896fd7ce5SGreg Kroah-Hartman return -ENOMEM; 10996fd7ce5SGreg Kroah-Hartman return 0; 11096fd7ce5SGreg Kroah-Hartman } 11196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_alloc_xmit_buf); 11296fd7ce5SGreg Kroah-Hartman 11396fd7ce5SGreg Kroah-Hartman void tty_port_free_xmit_buf(struct tty_port *port) 11496fd7ce5SGreg Kroah-Hartman { 11596fd7ce5SGreg Kroah-Hartman mutex_lock(&port->buf_mutex); 11696fd7ce5SGreg Kroah-Hartman if (port->xmit_buf != NULL) { 11796fd7ce5SGreg Kroah-Hartman free_page((unsigned long)port->xmit_buf); 11896fd7ce5SGreg Kroah-Hartman port->xmit_buf = NULL; 11996fd7ce5SGreg Kroah-Hartman } 12096fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->buf_mutex); 12196fd7ce5SGreg Kroah-Hartman } 12296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_free_xmit_buf); 12396fd7ce5SGreg Kroah-Hartman 124de274bfeSJiri Slaby /** 125de274bfeSJiri Slaby * tty_port_destroy -- destroy inited port 126de274bfeSJiri Slaby * @port: tty port to be doestroyed 127de274bfeSJiri Slaby * 128de274bfeSJiri Slaby * When a port was initialized using tty_port_init, one has to destroy the 129de274bfeSJiri Slaby * port by this function. Either indirectly by using tty_port refcounting 130de274bfeSJiri Slaby * (tty_port_put) or directly if refcounting is not used. 131de274bfeSJiri Slaby */ 132de274bfeSJiri Slaby void tty_port_destroy(struct tty_port *port) 133de274bfeSJiri Slaby { 1344f98d467SPeter Hurley cancel_work_sync(&port->buf.work); 135de274bfeSJiri Slaby tty_buffer_free_all(port); 136de274bfeSJiri Slaby } 137de274bfeSJiri Slaby EXPORT_SYMBOL(tty_port_destroy); 138de274bfeSJiri Slaby 13996fd7ce5SGreg Kroah-Hartman static void tty_port_destructor(struct kref *kref) 14096fd7ce5SGreg Kroah-Hartman { 14196fd7ce5SGreg Kroah-Hartman struct tty_port *port = container_of(kref, struct tty_port, kref); 142e3bfea23SPeter Hurley 143e3bfea23SPeter Hurley /* check if last port ref was dropped before tty release */ 144e3bfea23SPeter Hurley if (WARN_ON(port->itty)) 145e3bfea23SPeter Hurley return; 14696fd7ce5SGreg Kroah-Hartman if (port->xmit_buf) 14796fd7ce5SGreg Kroah-Hartman free_page((unsigned long)port->xmit_buf); 148de274bfeSJiri Slaby tty_port_destroy(port); 14981c79838SJiri Slaby if (port->ops && port->ops->destruct) 15096fd7ce5SGreg Kroah-Hartman port->ops->destruct(port); 15196fd7ce5SGreg Kroah-Hartman else 15296fd7ce5SGreg Kroah-Hartman kfree(port); 15396fd7ce5SGreg Kroah-Hartman } 15496fd7ce5SGreg Kroah-Hartman 15596fd7ce5SGreg Kroah-Hartman void tty_port_put(struct tty_port *port) 15696fd7ce5SGreg Kroah-Hartman { 15796fd7ce5SGreg Kroah-Hartman if (port) 15896fd7ce5SGreg Kroah-Hartman kref_put(&port->kref, tty_port_destructor); 15996fd7ce5SGreg Kroah-Hartman } 16096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_put); 16196fd7ce5SGreg Kroah-Hartman 16296fd7ce5SGreg Kroah-Hartman /** 16396fd7ce5SGreg Kroah-Hartman * tty_port_tty_get - get a tty reference 16496fd7ce5SGreg Kroah-Hartman * @port: tty port 16596fd7ce5SGreg Kroah-Hartman * 16696fd7ce5SGreg Kroah-Hartman * Return a refcount protected tty instance or NULL if the port is not 16796fd7ce5SGreg Kroah-Hartman * associated with a tty (eg due to close or hangup) 16896fd7ce5SGreg Kroah-Hartman */ 16996fd7ce5SGreg Kroah-Hartman 17096fd7ce5SGreg Kroah-Hartman struct tty_struct *tty_port_tty_get(struct tty_port *port) 17196fd7ce5SGreg Kroah-Hartman { 17296fd7ce5SGreg Kroah-Hartman unsigned long flags; 17396fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 17496fd7ce5SGreg Kroah-Hartman 17596fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 17696fd7ce5SGreg Kroah-Hartman tty = tty_kref_get(port->tty); 17796fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 17896fd7ce5SGreg Kroah-Hartman return tty; 17996fd7ce5SGreg Kroah-Hartman } 18096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_get); 18196fd7ce5SGreg Kroah-Hartman 18296fd7ce5SGreg Kroah-Hartman /** 18396fd7ce5SGreg Kroah-Hartman * tty_port_tty_set - set the tty of a port 18496fd7ce5SGreg Kroah-Hartman * @port: tty port 18596fd7ce5SGreg Kroah-Hartman * @tty: the tty 18696fd7ce5SGreg Kroah-Hartman * 18796fd7ce5SGreg Kroah-Hartman * Associate the port and tty pair. Manages any internal refcounts. 18896fd7ce5SGreg Kroah-Hartman * Pass NULL to deassociate a port 18996fd7ce5SGreg Kroah-Hartman */ 19096fd7ce5SGreg Kroah-Hartman 19196fd7ce5SGreg Kroah-Hartman void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty) 19296fd7ce5SGreg Kroah-Hartman { 19396fd7ce5SGreg Kroah-Hartman unsigned long flags; 19496fd7ce5SGreg Kroah-Hartman 19596fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 19696fd7ce5SGreg Kroah-Hartman if (port->tty) 19796fd7ce5SGreg Kroah-Hartman tty_kref_put(port->tty); 19896fd7ce5SGreg Kroah-Hartman port->tty = tty_kref_get(tty); 19996fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 20096fd7ce5SGreg Kroah-Hartman } 20196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_set); 20296fd7ce5SGreg Kroah-Hartman 203957dacaeSJohan Hovold static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty) 20496fd7ce5SGreg Kroah-Hartman { 20596fd7ce5SGreg Kroah-Hartman mutex_lock(&port->mutex); 2068bde9658SJohan Hovold if (port->console) 2078bde9658SJohan Hovold goto out; 2088bde9658SJohan Hovold 2098bde9658SJohan Hovold if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) { 210957dacaeSJohan Hovold /* 211957dacaeSJohan Hovold * Drop DTR/RTS if HUPCL is set. This causes any attached 212957dacaeSJohan Hovold * modem to hang up the line. 213957dacaeSJohan Hovold */ 214957dacaeSJohan Hovold if (tty && C_HUPCL(tty)) 215957dacaeSJohan Hovold tty_port_lower_dtr_rts(port); 216957dacaeSJohan Hovold 2178bde9658SJohan Hovold if (port->ops->shutdown) 21896fd7ce5SGreg Kroah-Hartman port->ops->shutdown(port); 2198bde9658SJohan Hovold } 2208bde9658SJohan Hovold out: 22196fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->mutex); 22296fd7ce5SGreg Kroah-Hartman } 22396fd7ce5SGreg Kroah-Hartman 22496fd7ce5SGreg Kroah-Hartman /** 22596fd7ce5SGreg Kroah-Hartman * tty_port_hangup - hangup helper 22696fd7ce5SGreg Kroah-Hartman * @port: tty port 22796fd7ce5SGreg Kroah-Hartman * 22896fd7ce5SGreg Kroah-Hartman * Perform port level tty hangup flag and count changes. Drop the tty 22996fd7ce5SGreg Kroah-Hartman * reference. 23096fd7ce5SGreg Kroah-Hartman */ 23196fd7ce5SGreg Kroah-Hartman 23296fd7ce5SGreg Kroah-Hartman void tty_port_hangup(struct tty_port *port) 23396fd7ce5SGreg Kroah-Hartman { 234957dacaeSJohan Hovold struct tty_struct *tty; 23596fd7ce5SGreg Kroah-Hartman unsigned long flags; 23696fd7ce5SGreg Kroah-Hartman 23796fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 23896fd7ce5SGreg Kroah-Hartman port->count = 0; 23996fd7ce5SGreg Kroah-Hartman port->flags &= ~ASYNC_NORMAL_ACTIVE; 240957dacaeSJohan Hovold tty = port->tty; 241957dacaeSJohan Hovold if (tty) 242957dacaeSJohan Hovold set_bit(TTY_IO_ERROR, &tty->flags); 24396fd7ce5SGreg Kroah-Hartman port->tty = NULL; 24496fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 245957dacaeSJohan Hovold tty_port_shutdown(port, tty); 246957dacaeSJohan Hovold tty_kref_put(tty); 24796fd7ce5SGreg Kroah-Hartman wake_up_interruptible(&port->open_wait); 24896fd7ce5SGreg Kroah-Hartman wake_up_interruptible(&port->delta_msr_wait); 24996fd7ce5SGreg Kroah-Hartman } 25096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_hangup); 25196fd7ce5SGreg Kroah-Hartman 25296fd7ce5SGreg Kroah-Hartman /** 253aa27a094SJiri Slaby * tty_port_tty_hangup - helper to hang up a tty 254aa27a094SJiri Slaby * 255aa27a094SJiri Slaby * @port: tty port 256aa27a094SJiri Slaby * @check_clocal: hang only ttys with CLOCAL unset? 257aa27a094SJiri Slaby */ 258aa27a094SJiri Slaby void tty_port_tty_hangup(struct tty_port *port, bool check_clocal) 259aa27a094SJiri Slaby { 260aa27a094SJiri Slaby struct tty_struct *tty = tty_port_tty_get(port); 261aa27a094SJiri Slaby 2621d9e689cSGianluca Anzolin if (tty && (!check_clocal || !C_CLOCAL(tty))) 263aa27a094SJiri Slaby tty_hangup(tty); 264aa27a094SJiri Slaby tty_kref_put(tty); 265aa27a094SJiri Slaby } 266aa27a094SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_tty_hangup); 267aa27a094SJiri Slaby 268aa27a094SJiri Slaby /** 2696aad04f2SJiri Slaby * tty_port_tty_wakeup - helper to wake up a tty 2706aad04f2SJiri Slaby * 2716aad04f2SJiri Slaby * @port: tty port 2726aad04f2SJiri Slaby */ 2736aad04f2SJiri Slaby void tty_port_tty_wakeup(struct tty_port *port) 2746aad04f2SJiri Slaby { 2756aad04f2SJiri Slaby struct tty_struct *tty = tty_port_tty_get(port); 2766aad04f2SJiri Slaby 2776aad04f2SJiri Slaby if (tty) { 2786aad04f2SJiri Slaby tty_wakeup(tty); 2796aad04f2SJiri Slaby tty_kref_put(tty); 2806aad04f2SJiri Slaby } 2816aad04f2SJiri Slaby } 2826aad04f2SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_tty_wakeup); 2836aad04f2SJiri Slaby 2846aad04f2SJiri Slaby /** 28596fd7ce5SGreg Kroah-Hartman * tty_port_carrier_raised - carrier raised check 28696fd7ce5SGreg Kroah-Hartman * @port: tty port 28796fd7ce5SGreg Kroah-Hartman * 28896fd7ce5SGreg Kroah-Hartman * Wrapper for the carrier detect logic. For the moment this is used 28996fd7ce5SGreg Kroah-Hartman * to hide some internal details. This will eventually become entirely 29096fd7ce5SGreg Kroah-Hartman * internal to the tty port. 29196fd7ce5SGreg Kroah-Hartman */ 29296fd7ce5SGreg Kroah-Hartman 29396fd7ce5SGreg Kroah-Hartman int tty_port_carrier_raised(struct tty_port *port) 29496fd7ce5SGreg Kroah-Hartman { 29596fd7ce5SGreg Kroah-Hartman if (port->ops->carrier_raised == NULL) 29696fd7ce5SGreg Kroah-Hartman return 1; 29796fd7ce5SGreg Kroah-Hartman return port->ops->carrier_raised(port); 29896fd7ce5SGreg Kroah-Hartman } 29996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_carrier_raised); 30096fd7ce5SGreg Kroah-Hartman 30196fd7ce5SGreg Kroah-Hartman /** 30296fd7ce5SGreg Kroah-Hartman * tty_port_raise_dtr_rts - Raise DTR/RTS 30396fd7ce5SGreg Kroah-Hartman * @port: tty port 30496fd7ce5SGreg Kroah-Hartman * 30596fd7ce5SGreg Kroah-Hartman * Wrapper for the DTR/RTS raise logic. For the moment this is used 30696fd7ce5SGreg Kroah-Hartman * to hide some internal details. This will eventually become entirely 30796fd7ce5SGreg Kroah-Hartman * internal to the tty port. 30896fd7ce5SGreg Kroah-Hartman */ 30996fd7ce5SGreg Kroah-Hartman 31096fd7ce5SGreg Kroah-Hartman void tty_port_raise_dtr_rts(struct tty_port *port) 31196fd7ce5SGreg Kroah-Hartman { 31296fd7ce5SGreg Kroah-Hartman if (port->ops->dtr_rts) 31396fd7ce5SGreg Kroah-Hartman port->ops->dtr_rts(port, 1); 31496fd7ce5SGreg Kroah-Hartman } 31596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_raise_dtr_rts); 31696fd7ce5SGreg Kroah-Hartman 31796fd7ce5SGreg Kroah-Hartman /** 31896fd7ce5SGreg Kroah-Hartman * tty_port_lower_dtr_rts - Lower DTR/RTS 31996fd7ce5SGreg Kroah-Hartman * @port: tty port 32096fd7ce5SGreg Kroah-Hartman * 32196fd7ce5SGreg Kroah-Hartman * Wrapper for the DTR/RTS raise logic. For the moment this is used 32296fd7ce5SGreg Kroah-Hartman * to hide some internal details. This will eventually become entirely 32396fd7ce5SGreg Kroah-Hartman * internal to the tty port. 32496fd7ce5SGreg Kroah-Hartman */ 32596fd7ce5SGreg Kroah-Hartman 32696fd7ce5SGreg Kroah-Hartman void tty_port_lower_dtr_rts(struct tty_port *port) 32796fd7ce5SGreg Kroah-Hartman { 32896fd7ce5SGreg Kroah-Hartman if (port->ops->dtr_rts) 32996fd7ce5SGreg Kroah-Hartman port->ops->dtr_rts(port, 0); 33096fd7ce5SGreg Kroah-Hartman } 33196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_lower_dtr_rts); 33296fd7ce5SGreg Kroah-Hartman 33396fd7ce5SGreg Kroah-Hartman /** 33496fd7ce5SGreg Kroah-Hartman * tty_port_block_til_ready - Waiting logic for tty open 33596fd7ce5SGreg Kroah-Hartman * @port: the tty port being opened 33696fd7ce5SGreg Kroah-Hartman * @tty: the tty device being bound 33796fd7ce5SGreg Kroah-Hartman * @filp: the file pointer of the opener 33896fd7ce5SGreg Kroah-Hartman * 33996fd7ce5SGreg Kroah-Hartman * Implement the core POSIX/SuS tty behaviour when opening a tty device. 34096fd7ce5SGreg Kroah-Hartman * Handles: 34196fd7ce5SGreg Kroah-Hartman * - hangup (both before and during) 34296fd7ce5SGreg Kroah-Hartman * - non blocking open 34396fd7ce5SGreg Kroah-Hartman * - rts/dtr/dcd 34496fd7ce5SGreg Kroah-Hartman * - signals 34596fd7ce5SGreg Kroah-Hartman * - port flags and counts 34696fd7ce5SGreg Kroah-Hartman * 34796fd7ce5SGreg Kroah-Hartman * The passed tty_port must implement the carrier_raised method if it can 34896fd7ce5SGreg Kroah-Hartman * do carrier detect and the dtr_rts method if it supports software 34996fd7ce5SGreg Kroah-Hartman * management of these lines. Note that the dtr/rts raise is done each 35096fd7ce5SGreg Kroah-Hartman * iteration as a hangup may have previously dropped them while we wait. 351*c590f6b6SPeter Hurley * 352*c590f6b6SPeter Hurley * Caller holds tty lock. 353*c590f6b6SPeter Hurley * 354*c590f6b6SPeter Hurley * NB: May drop and reacquire tty lock when blocking, so tty and tty_port 355*c590f6b6SPeter Hurley * may have changed state (eg., may have been hung up). 35696fd7ce5SGreg Kroah-Hartman */ 35796fd7ce5SGreg Kroah-Hartman 35896fd7ce5SGreg Kroah-Hartman int tty_port_block_til_ready(struct tty_port *port, 35996fd7ce5SGreg Kroah-Hartman struct tty_struct *tty, struct file *filp) 36096fd7ce5SGreg Kroah-Hartman { 36196fd7ce5SGreg Kroah-Hartman int do_clocal = 0, retval; 36296fd7ce5SGreg Kroah-Hartman unsigned long flags; 36396fd7ce5SGreg Kroah-Hartman DEFINE_WAIT(wait); 36496fd7ce5SGreg Kroah-Hartman 36596fd7ce5SGreg Kroah-Hartman /* block if port is in the process of being closed */ 36696fd7ce5SGreg Kroah-Hartman if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) { 36789c8d91eSAlan Cox wait_event_interruptible_tty(tty, port->close_wait, 36896fd7ce5SGreg Kroah-Hartman !(port->flags & ASYNC_CLOSING)); 36996fd7ce5SGreg Kroah-Hartman if (port->flags & ASYNC_HUP_NOTIFY) 37096fd7ce5SGreg Kroah-Hartman return -EAGAIN; 37196fd7ce5SGreg Kroah-Hartman else 37296fd7ce5SGreg Kroah-Hartman return -ERESTARTSYS; 37396fd7ce5SGreg Kroah-Hartman } 37496fd7ce5SGreg Kroah-Hartman 37596fd7ce5SGreg Kroah-Hartman /* if non-blocking mode is set we can pass directly to open unless 37696fd7ce5SGreg Kroah-Hartman the port has just hung up or is in another error state */ 37796fd7ce5SGreg Kroah-Hartman if (tty->flags & (1 << TTY_IO_ERROR)) { 37896fd7ce5SGreg Kroah-Hartman port->flags |= ASYNC_NORMAL_ACTIVE; 37996fd7ce5SGreg Kroah-Hartman return 0; 38096fd7ce5SGreg Kroah-Hartman } 38196fd7ce5SGreg Kroah-Hartman if (filp->f_flags & O_NONBLOCK) { 38296fd7ce5SGreg Kroah-Hartman /* Indicate we are open */ 383adc8d746SAlan Cox if (tty->termios.c_cflag & CBAUD) 38496fd7ce5SGreg Kroah-Hartman tty_port_raise_dtr_rts(port); 38596fd7ce5SGreg Kroah-Hartman port->flags |= ASYNC_NORMAL_ACTIVE; 38696fd7ce5SGreg Kroah-Hartman return 0; 38796fd7ce5SGreg Kroah-Hartman } 38896fd7ce5SGreg Kroah-Hartman 38996fd7ce5SGreg Kroah-Hartman if (C_CLOCAL(tty)) 39096fd7ce5SGreg Kroah-Hartman do_clocal = 1; 39196fd7ce5SGreg Kroah-Hartman 39296fd7ce5SGreg Kroah-Hartman /* Block waiting until we can proceed. We may need to wait for the 39396fd7ce5SGreg Kroah-Hartman carrier, but we must also wait for any close that is in progress 39496fd7ce5SGreg Kroah-Hartman before the next open may complete */ 39596fd7ce5SGreg Kroah-Hartman 39696fd7ce5SGreg Kroah-Hartman retval = 0; 39796fd7ce5SGreg Kroah-Hartman 39896fd7ce5SGreg Kroah-Hartman /* The port lock protects the port counts */ 39996fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 40096fd7ce5SGreg Kroah-Hartman if (!tty_hung_up_p(filp)) 40196fd7ce5SGreg Kroah-Hartman port->count--; 40296fd7ce5SGreg Kroah-Hartman port->blocked_open++; 40396fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 40496fd7ce5SGreg Kroah-Hartman 40596fd7ce5SGreg Kroah-Hartman while (1) { 40696fd7ce5SGreg Kroah-Hartman /* Indicate we are open */ 407e584a02cSJohan Hovold if (C_BAUD(tty) && test_bit(ASYNCB_INITIALIZED, &port->flags)) 40896fd7ce5SGreg Kroah-Hartman tty_port_raise_dtr_rts(port); 40996fd7ce5SGreg Kroah-Hartman 41096fd7ce5SGreg Kroah-Hartman prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE); 41196fd7ce5SGreg Kroah-Hartman /* Check for a hangup or uninitialised port. 41296fd7ce5SGreg Kroah-Hartman Return accordingly */ 41396fd7ce5SGreg Kroah-Hartman if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) { 41496fd7ce5SGreg Kroah-Hartman if (port->flags & ASYNC_HUP_NOTIFY) 41596fd7ce5SGreg Kroah-Hartman retval = -EAGAIN; 41696fd7ce5SGreg Kroah-Hartman else 41796fd7ce5SGreg Kroah-Hartman retval = -ERESTARTSYS; 41896fd7ce5SGreg Kroah-Hartman break; 41996fd7ce5SGreg Kroah-Hartman } 4200eee50afSJiri Slaby /* 4210eee50afSJiri Slaby * Probe the carrier. For devices with no carrier detect 4220eee50afSJiri Slaby * tty_port_carrier_raised will always return true. 4230eee50afSJiri Slaby * Never ask drivers if CLOCAL is set, this causes troubles 4240eee50afSJiri Slaby * on some hardware. 4250eee50afSJiri Slaby */ 42696fd7ce5SGreg Kroah-Hartman if (!(port->flags & ASYNC_CLOSING) && 4270eee50afSJiri Slaby (do_clocal || tty_port_carrier_raised(port))) 42896fd7ce5SGreg Kroah-Hartman break; 42996fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) { 43096fd7ce5SGreg Kroah-Hartman retval = -ERESTARTSYS; 43196fd7ce5SGreg Kroah-Hartman break; 43296fd7ce5SGreg Kroah-Hartman } 43389c8d91eSAlan Cox tty_unlock(tty); 43496fd7ce5SGreg Kroah-Hartman schedule(); 43589c8d91eSAlan Cox tty_lock(tty); 43696fd7ce5SGreg Kroah-Hartman } 43796fd7ce5SGreg Kroah-Hartman finish_wait(&port->open_wait, &wait); 43896fd7ce5SGreg Kroah-Hartman 43996fd7ce5SGreg Kroah-Hartman /* Update counts. A parallel hangup will have set count to zero and 44096fd7ce5SGreg Kroah-Hartman we must not mess that up further */ 44196fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 44296fd7ce5SGreg Kroah-Hartman if (!tty_hung_up_p(filp)) 44396fd7ce5SGreg Kroah-Hartman port->count++; 44496fd7ce5SGreg Kroah-Hartman port->blocked_open--; 44596fd7ce5SGreg Kroah-Hartman if (retval == 0) 44696fd7ce5SGreg Kroah-Hartman port->flags |= ASYNC_NORMAL_ACTIVE; 44796fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 44896fd7ce5SGreg Kroah-Hartman return retval; 44996fd7ce5SGreg Kroah-Hartman } 45096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_block_til_ready); 45196fd7ce5SGreg Kroah-Hartman 452b74414f5SJohan Hovold static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty) 453b74414f5SJohan Hovold { 454b74414f5SJohan Hovold unsigned int bps = tty_get_baud_rate(tty); 455b74414f5SJohan Hovold long timeout; 456b74414f5SJohan Hovold 457b74414f5SJohan Hovold if (bps > 1200) { 458b74414f5SJohan Hovold timeout = (HZ * 10 * port->drain_delay) / bps; 459b74414f5SJohan Hovold timeout = max_t(long, timeout, HZ / 10); 460b74414f5SJohan Hovold } else { 461b74414f5SJohan Hovold timeout = 2 * HZ; 462b74414f5SJohan Hovold } 463b74414f5SJohan Hovold schedule_timeout_interruptible(timeout); 464b74414f5SJohan Hovold } 465b74414f5SJohan Hovold 4660733db91SPeter Hurley /* Caller holds tty lock. 4670733db91SPeter Hurley * NB: may drop and reacquire tty lock (in tty_wait_until_sent_from_close()) 4680733db91SPeter Hurley * so tty and tty port may have changed state (but not hung up or reopened). 4690733db91SPeter Hurley */ 47096fd7ce5SGreg Kroah-Hartman int tty_port_close_start(struct tty_port *port, 47196fd7ce5SGreg Kroah-Hartman struct tty_struct *tty, struct file *filp) 47296fd7ce5SGreg Kroah-Hartman { 47396fd7ce5SGreg Kroah-Hartman unsigned long flags; 47496fd7ce5SGreg Kroah-Hartman 47596fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 47696fd7ce5SGreg Kroah-Hartman if (tty_hung_up_p(filp)) { 47796fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 47896fd7ce5SGreg Kroah-Hartman return 0; 47996fd7ce5SGreg Kroah-Hartman } 48096fd7ce5SGreg Kroah-Hartman 48196fd7ce5SGreg Kroah-Hartman if (tty->count == 1 && port->count != 1) { 48296fd7ce5SGreg Kroah-Hartman printk(KERN_WARNING 48396fd7ce5SGreg Kroah-Hartman "tty_port_close_start: tty->count = 1 port count = %d.\n", 48496fd7ce5SGreg Kroah-Hartman port->count); 48596fd7ce5SGreg Kroah-Hartman port->count = 1; 48696fd7ce5SGreg Kroah-Hartman } 48796fd7ce5SGreg Kroah-Hartman if (--port->count < 0) { 48896fd7ce5SGreg Kroah-Hartman printk(KERN_WARNING "tty_port_close_start: count = %d\n", 48996fd7ce5SGreg Kroah-Hartman port->count); 49096fd7ce5SGreg Kroah-Hartman port->count = 0; 49196fd7ce5SGreg Kroah-Hartman } 49296fd7ce5SGreg Kroah-Hartman 49396fd7ce5SGreg Kroah-Hartman if (port->count) { 49496fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 49596fd7ce5SGreg Kroah-Hartman return 0; 49696fd7ce5SGreg Kroah-Hartman } 49796fd7ce5SGreg Kroah-Hartman set_bit(ASYNCB_CLOSING, &port->flags); 49896fd7ce5SGreg Kroah-Hartman tty->closing = 1; 49996fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 5000b2588caSJohan Hovold 5010b2588caSJohan Hovold if (test_bit(ASYNCB_INITIALIZED, &port->flags)) { 50296fd7ce5SGreg Kroah-Hartman /* Don't block on a stalled port, just pull the chain */ 50396fd7ce5SGreg Kroah-Hartman if (tty->flow_stopped) 50496fd7ce5SGreg Kroah-Hartman tty_driver_flush_buffer(tty); 5050b2588caSJohan Hovold if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE) 506424cc039SJiri Slaby tty_wait_until_sent_from_close(tty, port->closing_wait); 507b74414f5SJohan Hovold if (port->drain_delay) 508b74414f5SJohan Hovold tty_port_drain_delay(port, tty); 5090b2588caSJohan Hovold } 51096fd7ce5SGreg Kroah-Hartman /* Flush the ldisc buffering */ 51196fd7ce5SGreg Kroah-Hartman tty_ldisc_flush(tty); 51296fd7ce5SGreg Kroah-Hartman 513469d6d06SPeter Hurley /* Report to caller this is the last port reference */ 51496fd7ce5SGreg Kroah-Hartman return 1; 51596fd7ce5SGreg Kroah-Hartman } 51696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_start); 51796fd7ce5SGreg Kroah-Hartman 5180733db91SPeter Hurley /* Caller holds tty lock */ 51996fd7ce5SGreg Kroah-Hartman void tty_port_close_end(struct tty_port *port, struct tty_struct *tty) 52096fd7ce5SGreg Kroah-Hartman { 52196fd7ce5SGreg Kroah-Hartman unsigned long flags; 52296fd7ce5SGreg Kroah-Hartman 52396fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 52496fd7ce5SGreg Kroah-Hartman tty->closing = 0; 52596fd7ce5SGreg Kroah-Hartman 52696fd7ce5SGreg Kroah-Hartman if (port->blocked_open) { 52796fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 52896fd7ce5SGreg Kroah-Hartman if (port->close_delay) { 52996fd7ce5SGreg Kroah-Hartman msleep_interruptible( 53096fd7ce5SGreg Kroah-Hartman jiffies_to_msecs(port->close_delay)); 53196fd7ce5SGreg Kroah-Hartman } 53296fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 53396fd7ce5SGreg Kroah-Hartman wake_up_interruptible(&port->open_wait); 53496fd7ce5SGreg Kroah-Hartman } 53596fd7ce5SGreg Kroah-Hartman port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING); 53696fd7ce5SGreg Kroah-Hartman wake_up_interruptible(&port->close_wait); 53796fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 53896fd7ce5SGreg Kroah-Hartman } 53996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_end); 54096fd7ce5SGreg Kroah-Hartman 5410733db91SPeter Hurley /** 5420733db91SPeter Hurley * tty_port_close 5430733db91SPeter Hurley * 5440733db91SPeter Hurley * Caller holds tty lock 5450733db91SPeter Hurley * 5460733db91SPeter Hurley * NB: may drop and reacquire tty lock (in tty_port_close_start()-> 5470733db91SPeter Hurley * tty_wait_until_sent_from_close()) so tty and tty_port may have changed 5480733db91SPeter Hurley * state (but not hung up or reopened). 5490733db91SPeter Hurley */ 55096fd7ce5SGreg Kroah-Hartman void tty_port_close(struct tty_port *port, struct tty_struct *tty, 55196fd7ce5SGreg Kroah-Hartman struct file *filp) 55296fd7ce5SGreg Kroah-Hartman { 55396fd7ce5SGreg Kroah-Hartman if (tty_port_close_start(port, tty, filp) == 0) 55496fd7ce5SGreg Kroah-Hartman return; 555957dacaeSJohan Hovold tty_port_shutdown(port, tty); 55696fd7ce5SGreg Kroah-Hartman set_bit(TTY_IO_ERROR, &tty->flags); 55796fd7ce5SGreg Kroah-Hartman tty_port_close_end(port, tty); 55896fd7ce5SGreg Kroah-Hartman tty_port_tty_set(port, NULL); 55996fd7ce5SGreg Kroah-Hartman } 56096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close); 56196fd7ce5SGreg Kroah-Hartman 56272a33bf5SJiri Slaby /** 56372a33bf5SJiri Slaby * tty_port_install - generic tty->ops->install handler 56472a33bf5SJiri Slaby * @port: tty_port of the device 56572a33bf5SJiri Slaby * @driver: tty_driver for this device 56672a33bf5SJiri Slaby * @tty: tty to be installed 56772a33bf5SJiri Slaby * 56872a33bf5SJiri Slaby * It is the same as tty_standard_install except the provided @port is linked 56972a33bf5SJiri Slaby * to a concrete tty specified by @tty. Use this or tty_port_register_device 57072a33bf5SJiri Slaby * (or both). Call tty_port_link_device as a last resort. 57172a33bf5SJiri Slaby */ 572695586caSJiri Slaby int tty_port_install(struct tty_port *port, struct tty_driver *driver, 573695586caSJiri Slaby struct tty_struct *tty) 574695586caSJiri Slaby { 575695586caSJiri Slaby tty->port = port; 576695586caSJiri Slaby return tty_standard_install(driver, tty); 577695586caSJiri Slaby } 578695586caSJiri Slaby EXPORT_SYMBOL_GPL(tty_port_install); 579695586caSJiri Slaby 580addd4672SPeter Hurley /** 581addd4672SPeter Hurley * tty_port_open 582addd4672SPeter Hurley * 583addd4672SPeter Hurley * Caller holds tty lock. 584addd4672SPeter Hurley * 585addd4672SPeter Hurley * NB: may drop and reacquire tty lock (in tty_port_block_til_ready()) so 586addd4672SPeter Hurley * tty and tty_port may have changed state (eg., may be hung up now) 587addd4672SPeter Hurley */ 58896fd7ce5SGreg Kroah-Hartman int tty_port_open(struct tty_port *port, struct tty_struct *tty, 58996fd7ce5SGreg Kroah-Hartman struct file *filp) 59096fd7ce5SGreg Kroah-Hartman { 59196fd7ce5SGreg Kroah-Hartman spin_lock_irq(&port->lock); 59296fd7ce5SGreg Kroah-Hartman if (!tty_hung_up_p(filp)) 59396fd7ce5SGreg Kroah-Hartman ++port->count; 59496fd7ce5SGreg Kroah-Hartman spin_unlock_irq(&port->lock); 59596fd7ce5SGreg Kroah-Hartman tty_port_tty_set(port, tty); 59696fd7ce5SGreg Kroah-Hartman 59796fd7ce5SGreg Kroah-Hartman /* 59896fd7ce5SGreg Kroah-Hartman * Do the device-specific open only if the hardware isn't 59996fd7ce5SGreg Kroah-Hartman * already initialized. Serialize open and shutdown using the 60096fd7ce5SGreg Kroah-Hartman * port mutex. 60196fd7ce5SGreg Kroah-Hartman */ 60296fd7ce5SGreg Kroah-Hartman 60396fd7ce5SGreg Kroah-Hartman mutex_lock(&port->mutex); 60496fd7ce5SGreg Kroah-Hartman 60596fd7ce5SGreg Kroah-Hartman if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) { 60696fd7ce5SGreg Kroah-Hartman clear_bit(TTY_IO_ERROR, &tty->flags); 60796fd7ce5SGreg Kroah-Hartman if (port->ops->activate) { 60896fd7ce5SGreg Kroah-Hartman int retval = port->ops->activate(port, tty); 60996fd7ce5SGreg Kroah-Hartman if (retval) { 61096fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->mutex); 61196fd7ce5SGreg Kroah-Hartman return retval; 61296fd7ce5SGreg Kroah-Hartman } 61396fd7ce5SGreg Kroah-Hartman } 61496fd7ce5SGreg Kroah-Hartman set_bit(ASYNCB_INITIALIZED, &port->flags); 61596fd7ce5SGreg Kroah-Hartman } 61696fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->mutex); 61796fd7ce5SGreg Kroah-Hartman return tty_port_block_til_ready(port, tty, filp); 61896fd7ce5SGreg Kroah-Hartman } 61996fd7ce5SGreg Kroah-Hartman 62096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_open); 621