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 20*c3485ee0SRob Herring static int tty_port_default_receive_buf(struct tty_port *port, 21*c3485ee0SRob Herring const unsigned char *p, 22*c3485ee0SRob Herring const unsigned char *f, size_t count) 23*c3485ee0SRob Herring { 24*c3485ee0SRob Herring int ret; 25*c3485ee0SRob Herring struct tty_struct *tty; 26*c3485ee0SRob Herring struct tty_ldisc *disc; 27*c3485ee0SRob Herring 28*c3485ee0SRob Herring tty = READ_ONCE(port->itty); 29*c3485ee0SRob Herring if (!tty) 30*c3485ee0SRob Herring return 0; 31*c3485ee0SRob Herring 32*c3485ee0SRob Herring disc = tty_ldisc_ref(tty); 33*c3485ee0SRob Herring if (!disc) 34*c3485ee0SRob Herring return 0; 35*c3485ee0SRob Herring 36*c3485ee0SRob Herring ret = tty_ldisc_receive_buf(disc, p, (char *)f, count); 37*c3485ee0SRob Herring 38*c3485ee0SRob Herring tty_ldisc_deref(disc); 39*c3485ee0SRob Herring 40*c3485ee0SRob Herring return ret; 41*c3485ee0SRob Herring } 42*c3485ee0SRob Herring 43*c3485ee0SRob Herring static void tty_port_default_wakeup(struct tty_port *port) 44*c3485ee0SRob Herring { 45*c3485ee0SRob Herring struct tty_struct *tty = tty_port_tty_get(port); 46*c3485ee0SRob Herring 47*c3485ee0SRob Herring if (tty) { 48*c3485ee0SRob Herring tty_wakeup(tty); 49*c3485ee0SRob Herring tty_kref_put(tty); 50*c3485ee0SRob Herring } 51*c3485ee0SRob Herring } 52*c3485ee0SRob Herring 53*c3485ee0SRob Herring static const struct tty_port_client_operations default_client_ops = { 54*c3485ee0SRob Herring .receive_buf = tty_port_default_receive_buf, 55*c3485ee0SRob Herring .write_wakeup = tty_port_default_wakeup, 56*c3485ee0SRob Herring }; 57*c3485ee0SRob Herring 5896fd7ce5SGreg Kroah-Hartman void tty_port_init(struct tty_port *port) 5996fd7ce5SGreg Kroah-Hartman { 6096fd7ce5SGreg Kroah-Hartman memset(port, 0, sizeof(*port)); 61ecbbfd44SJiri Slaby tty_buffer_init(port); 6296fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&port->open_wait); 6396fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&port->delta_msr_wait); 6496fd7ce5SGreg Kroah-Hartman mutex_init(&port->mutex); 6596fd7ce5SGreg Kroah-Hartman mutex_init(&port->buf_mutex); 6696fd7ce5SGreg Kroah-Hartman spin_lock_init(&port->lock); 6796fd7ce5SGreg Kroah-Hartman port->close_delay = (50 * HZ) / 100; 6896fd7ce5SGreg Kroah-Hartman port->closing_wait = (3000 * HZ) / 100; 69*c3485ee0SRob Herring port->client_ops = &default_client_ops; 7096fd7ce5SGreg Kroah-Hartman kref_init(&port->kref); 7196fd7ce5SGreg Kroah-Hartman } 7296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_init); 7396fd7ce5SGreg Kroah-Hartman 7472a33bf5SJiri Slaby /** 752cb4ca02SJiri Slaby * tty_port_link_device - link tty and tty_port 762cb4ca02SJiri Slaby * @port: tty_port of the device 772cb4ca02SJiri Slaby * @driver: tty_driver for this device 782cb4ca02SJiri Slaby * @index: index of the tty 792cb4ca02SJiri Slaby * 802cb4ca02SJiri Slaby * Provide the tty layer wit ha link from a tty (specified by @index) to a 812cb4ca02SJiri Slaby * tty_port (@port). Use this only if neither tty_port_register_device nor 822cb4ca02SJiri Slaby * tty_port_install is used in the driver. If used, this has to be called before 832cb4ca02SJiri Slaby * tty_register_driver. 842cb4ca02SJiri Slaby */ 852cb4ca02SJiri Slaby void tty_port_link_device(struct tty_port *port, 862cb4ca02SJiri Slaby struct tty_driver *driver, unsigned index) 872cb4ca02SJiri Slaby { 882cb4ca02SJiri Slaby if (WARN_ON(index >= driver->num)) 892cb4ca02SJiri Slaby return; 902cb4ca02SJiri Slaby driver->ports[index] = port; 912cb4ca02SJiri Slaby } 922cb4ca02SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_link_device); 932cb4ca02SJiri Slaby 942cb4ca02SJiri Slaby /** 9572a33bf5SJiri Slaby * tty_port_register_device - register tty device 9672a33bf5SJiri Slaby * @port: tty_port of the device 9772a33bf5SJiri Slaby * @driver: tty_driver for this device 9872a33bf5SJiri Slaby * @index: index of the tty 9972a33bf5SJiri Slaby * @device: parent if exists, otherwise NULL 10072a33bf5SJiri Slaby * 10172a33bf5SJiri Slaby * It is the same as tty_register_device except the provided @port is linked to 10272a33bf5SJiri Slaby * a concrete tty specified by @index. Use this or tty_port_install (or both). 10372a33bf5SJiri Slaby * Call tty_port_link_device as a last resort. 10472a33bf5SJiri Slaby */ 105057eb856SJiri Slaby struct device *tty_port_register_device(struct tty_port *port, 106057eb856SJiri Slaby struct tty_driver *driver, unsigned index, 107057eb856SJiri Slaby struct device *device) 108057eb856SJiri Slaby { 1093086365dSRob Herring return tty_port_register_device_attr(port, driver, index, device, NULL, NULL); 110057eb856SJiri Slaby } 111057eb856SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_register_device); 112057eb856SJiri Slaby 113b1b79916STomas Hlavacek /** 114b1b79916STomas Hlavacek * tty_port_register_device_attr - register tty device 115b1b79916STomas Hlavacek * @port: tty_port of the device 116b1b79916STomas Hlavacek * @driver: tty_driver for this device 117b1b79916STomas Hlavacek * @index: index of the tty 118b1b79916STomas Hlavacek * @device: parent if exists, otherwise NULL 119b1b79916STomas Hlavacek * @drvdata: Driver data to be set to device. 120b1b79916STomas Hlavacek * @attr_grp: Attribute group to be set on device. 121b1b79916STomas Hlavacek * 122b1b79916STomas Hlavacek * It is the same as tty_register_device_attr except the provided @port is 123b1b79916STomas Hlavacek * linked to a concrete tty specified by @index. Use this or tty_port_install 124b1b79916STomas Hlavacek * (or both). Call tty_port_link_device as a last resort. 125b1b79916STomas Hlavacek */ 126b1b79916STomas Hlavacek struct device *tty_port_register_device_attr(struct tty_port *port, 127b1b79916STomas Hlavacek struct tty_driver *driver, unsigned index, 128b1b79916STomas Hlavacek struct device *device, void *drvdata, 129b1b79916STomas Hlavacek const struct attribute_group **attr_grp) 130b1b79916STomas Hlavacek { 131b1b79916STomas Hlavacek tty_port_link_device(port, driver, index); 132b1b79916STomas Hlavacek return tty_register_device_attr(driver, index, device, drvdata, 133b1b79916STomas Hlavacek attr_grp); 134b1b79916STomas Hlavacek } 135b1b79916STomas Hlavacek EXPORT_SYMBOL_GPL(tty_port_register_device_attr); 136b1b79916STomas Hlavacek 13796fd7ce5SGreg Kroah-Hartman int tty_port_alloc_xmit_buf(struct tty_port *port) 13896fd7ce5SGreg Kroah-Hartman { 13996fd7ce5SGreg Kroah-Hartman /* We may sleep in get_zeroed_page() */ 14096fd7ce5SGreg Kroah-Hartman mutex_lock(&port->buf_mutex); 14196fd7ce5SGreg Kroah-Hartman if (port->xmit_buf == NULL) 14296fd7ce5SGreg Kroah-Hartman port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL); 14396fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->buf_mutex); 14496fd7ce5SGreg Kroah-Hartman if (port->xmit_buf == NULL) 14596fd7ce5SGreg Kroah-Hartman return -ENOMEM; 14696fd7ce5SGreg Kroah-Hartman return 0; 14796fd7ce5SGreg Kroah-Hartman } 14896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_alloc_xmit_buf); 14996fd7ce5SGreg Kroah-Hartman 15096fd7ce5SGreg Kroah-Hartman void tty_port_free_xmit_buf(struct tty_port *port) 15196fd7ce5SGreg Kroah-Hartman { 15296fd7ce5SGreg Kroah-Hartman mutex_lock(&port->buf_mutex); 15396fd7ce5SGreg Kroah-Hartman if (port->xmit_buf != NULL) { 15496fd7ce5SGreg Kroah-Hartman free_page((unsigned long)port->xmit_buf); 15596fd7ce5SGreg Kroah-Hartman port->xmit_buf = NULL; 15696fd7ce5SGreg Kroah-Hartman } 15796fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->buf_mutex); 15896fd7ce5SGreg Kroah-Hartman } 15996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_free_xmit_buf); 16096fd7ce5SGreg Kroah-Hartman 161de274bfeSJiri Slaby /** 162de274bfeSJiri Slaby * tty_port_destroy -- destroy inited port 163de274bfeSJiri Slaby * @port: tty port to be doestroyed 164de274bfeSJiri Slaby * 165de274bfeSJiri Slaby * When a port was initialized using tty_port_init, one has to destroy the 166de274bfeSJiri Slaby * port by this function. Either indirectly by using tty_port refcounting 167de274bfeSJiri Slaby * (tty_port_put) or directly if refcounting is not used. 168de274bfeSJiri Slaby */ 169de274bfeSJiri Slaby void tty_port_destroy(struct tty_port *port) 170de274bfeSJiri Slaby { 171e176058fSPeter Hurley tty_buffer_cancel_work(port); 172de274bfeSJiri Slaby tty_buffer_free_all(port); 173de274bfeSJiri Slaby } 174de274bfeSJiri Slaby EXPORT_SYMBOL(tty_port_destroy); 175de274bfeSJiri Slaby 17696fd7ce5SGreg Kroah-Hartman static void tty_port_destructor(struct kref *kref) 17796fd7ce5SGreg Kroah-Hartman { 17896fd7ce5SGreg Kroah-Hartman struct tty_port *port = container_of(kref, struct tty_port, kref); 179e3bfea23SPeter Hurley 180e3bfea23SPeter Hurley /* check if last port ref was dropped before tty release */ 181e3bfea23SPeter Hurley if (WARN_ON(port->itty)) 182e3bfea23SPeter Hurley return; 18396fd7ce5SGreg Kroah-Hartman if (port->xmit_buf) 18496fd7ce5SGreg Kroah-Hartman free_page((unsigned long)port->xmit_buf); 185de274bfeSJiri Slaby tty_port_destroy(port); 18681c79838SJiri Slaby if (port->ops && port->ops->destruct) 18796fd7ce5SGreg Kroah-Hartman port->ops->destruct(port); 18896fd7ce5SGreg Kroah-Hartman else 18996fd7ce5SGreg Kroah-Hartman kfree(port); 19096fd7ce5SGreg Kroah-Hartman } 19196fd7ce5SGreg Kroah-Hartman 19296fd7ce5SGreg Kroah-Hartman void tty_port_put(struct tty_port *port) 19396fd7ce5SGreg Kroah-Hartman { 19496fd7ce5SGreg Kroah-Hartman if (port) 19596fd7ce5SGreg Kroah-Hartman kref_put(&port->kref, tty_port_destructor); 19696fd7ce5SGreg Kroah-Hartman } 19796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_put); 19896fd7ce5SGreg Kroah-Hartman 19996fd7ce5SGreg Kroah-Hartman /** 20096fd7ce5SGreg Kroah-Hartman * tty_port_tty_get - get a tty reference 20196fd7ce5SGreg Kroah-Hartman * @port: tty port 20296fd7ce5SGreg Kroah-Hartman * 20396fd7ce5SGreg Kroah-Hartman * Return a refcount protected tty instance or NULL if the port is not 20496fd7ce5SGreg Kroah-Hartman * associated with a tty (eg due to close or hangup) 20596fd7ce5SGreg Kroah-Hartman */ 20696fd7ce5SGreg Kroah-Hartman 20796fd7ce5SGreg Kroah-Hartman struct tty_struct *tty_port_tty_get(struct tty_port *port) 20896fd7ce5SGreg Kroah-Hartman { 20996fd7ce5SGreg Kroah-Hartman unsigned long flags; 21096fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 21196fd7ce5SGreg Kroah-Hartman 21296fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 21396fd7ce5SGreg Kroah-Hartman tty = tty_kref_get(port->tty); 21496fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 21596fd7ce5SGreg Kroah-Hartman return tty; 21696fd7ce5SGreg Kroah-Hartman } 21796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_get); 21896fd7ce5SGreg Kroah-Hartman 21996fd7ce5SGreg Kroah-Hartman /** 22096fd7ce5SGreg Kroah-Hartman * tty_port_tty_set - set the tty of a port 22196fd7ce5SGreg Kroah-Hartman * @port: tty port 22296fd7ce5SGreg Kroah-Hartman * @tty: the tty 22396fd7ce5SGreg Kroah-Hartman * 22496fd7ce5SGreg Kroah-Hartman * Associate the port and tty pair. Manages any internal refcounts. 22596fd7ce5SGreg Kroah-Hartman * Pass NULL to deassociate a port 22696fd7ce5SGreg Kroah-Hartman */ 22796fd7ce5SGreg Kroah-Hartman 22896fd7ce5SGreg Kroah-Hartman void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty) 22996fd7ce5SGreg Kroah-Hartman { 23096fd7ce5SGreg Kroah-Hartman unsigned long flags; 23196fd7ce5SGreg Kroah-Hartman 23296fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 23396fd7ce5SGreg Kroah-Hartman tty_kref_put(port->tty); 23496fd7ce5SGreg Kroah-Hartman port->tty = tty_kref_get(tty); 23596fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 23696fd7ce5SGreg Kroah-Hartman } 23796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_set); 23896fd7ce5SGreg Kroah-Hartman 239957dacaeSJohan Hovold static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty) 24096fd7ce5SGreg Kroah-Hartman { 24196fd7ce5SGreg Kroah-Hartman mutex_lock(&port->mutex); 2428bde9658SJohan Hovold if (port->console) 2438bde9658SJohan Hovold goto out; 2448bde9658SJohan Hovold 245d41861caSPeter Hurley if (tty_port_initialized(port)) { 246d41861caSPeter Hurley tty_port_set_initialized(port, 0); 247957dacaeSJohan Hovold /* 248957dacaeSJohan Hovold * Drop DTR/RTS if HUPCL is set. This causes any attached 249957dacaeSJohan Hovold * modem to hang up the line. 250957dacaeSJohan Hovold */ 251957dacaeSJohan Hovold if (tty && C_HUPCL(tty)) 252957dacaeSJohan Hovold tty_port_lower_dtr_rts(port); 253957dacaeSJohan Hovold 2548bde9658SJohan Hovold if (port->ops->shutdown) 25596fd7ce5SGreg Kroah-Hartman port->ops->shutdown(port); 2568bde9658SJohan Hovold } 2578bde9658SJohan Hovold out: 25896fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->mutex); 25996fd7ce5SGreg Kroah-Hartman } 26096fd7ce5SGreg Kroah-Hartman 26196fd7ce5SGreg Kroah-Hartman /** 26296fd7ce5SGreg Kroah-Hartman * tty_port_hangup - hangup helper 26396fd7ce5SGreg Kroah-Hartman * @port: tty port 26496fd7ce5SGreg Kroah-Hartman * 26596fd7ce5SGreg Kroah-Hartman * Perform port level tty hangup flag and count changes. Drop the tty 26696fd7ce5SGreg Kroah-Hartman * reference. 2679c9928bdSPeter Hurley * 2689c9928bdSPeter Hurley * Caller holds tty lock. 26996fd7ce5SGreg Kroah-Hartman */ 27096fd7ce5SGreg Kroah-Hartman 27196fd7ce5SGreg Kroah-Hartman void tty_port_hangup(struct tty_port *port) 27296fd7ce5SGreg Kroah-Hartman { 273957dacaeSJohan Hovold struct tty_struct *tty; 27496fd7ce5SGreg Kroah-Hartman unsigned long flags; 27596fd7ce5SGreg Kroah-Hartman 27696fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 27796fd7ce5SGreg Kroah-Hartman port->count = 0; 278957dacaeSJohan Hovold tty = port->tty; 279957dacaeSJohan Hovold if (tty) 280957dacaeSJohan Hovold set_bit(TTY_IO_ERROR, &tty->flags); 28196fd7ce5SGreg Kroah-Hartman port->tty = NULL; 28296fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 283807c8d81SPeter Hurley tty_port_set_active(port, 0); 284957dacaeSJohan Hovold tty_port_shutdown(port, tty); 285957dacaeSJohan Hovold tty_kref_put(tty); 28696fd7ce5SGreg Kroah-Hartman wake_up_interruptible(&port->open_wait); 28796fd7ce5SGreg Kroah-Hartman wake_up_interruptible(&port->delta_msr_wait); 28896fd7ce5SGreg Kroah-Hartman } 28996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_hangup); 29096fd7ce5SGreg Kroah-Hartman 29196fd7ce5SGreg Kroah-Hartman /** 292aa27a094SJiri Slaby * tty_port_tty_hangup - helper to hang up a tty 293aa27a094SJiri Slaby * 294aa27a094SJiri Slaby * @port: tty port 295aa27a094SJiri Slaby * @check_clocal: hang only ttys with CLOCAL unset? 296aa27a094SJiri Slaby */ 297aa27a094SJiri Slaby void tty_port_tty_hangup(struct tty_port *port, bool check_clocal) 298aa27a094SJiri Slaby { 299aa27a094SJiri Slaby struct tty_struct *tty = tty_port_tty_get(port); 300aa27a094SJiri Slaby 3011d9e689cSGianluca Anzolin if (tty && (!check_clocal || !C_CLOCAL(tty))) 302aa27a094SJiri Slaby tty_hangup(tty); 303aa27a094SJiri Slaby tty_kref_put(tty); 304aa27a094SJiri Slaby } 305aa27a094SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_tty_hangup); 306aa27a094SJiri Slaby 307aa27a094SJiri Slaby /** 3086aad04f2SJiri Slaby * tty_port_tty_wakeup - helper to wake up a tty 3096aad04f2SJiri Slaby * 3106aad04f2SJiri Slaby * @port: tty port 3116aad04f2SJiri Slaby */ 3126aad04f2SJiri Slaby void tty_port_tty_wakeup(struct tty_port *port) 3136aad04f2SJiri Slaby { 314*c3485ee0SRob Herring port->client_ops->write_wakeup(port); 3156aad04f2SJiri Slaby } 3166aad04f2SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_tty_wakeup); 3176aad04f2SJiri Slaby 3186aad04f2SJiri Slaby /** 31996fd7ce5SGreg Kroah-Hartman * tty_port_carrier_raised - carrier raised check 32096fd7ce5SGreg Kroah-Hartman * @port: tty port 32196fd7ce5SGreg Kroah-Hartman * 32296fd7ce5SGreg Kroah-Hartman * Wrapper for the carrier detect logic. For the moment this is used 32396fd7ce5SGreg Kroah-Hartman * to hide some internal details. This will eventually become entirely 32496fd7ce5SGreg Kroah-Hartman * internal to the tty port. 32596fd7ce5SGreg Kroah-Hartman */ 32696fd7ce5SGreg Kroah-Hartman 32796fd7ce5SGreg Kroah-Hartman int tty_port_carrier_raised(struct tty_port *port) 32896fd7ce5SGreg Kroah-Hartman { 32996fd7ce5SGreg Kroah-Hartman if (port->ops->carrier_raised == NULL) 33096fd7ce5SGreg Kroah-Hartman return 1; 33196fd7ce5SGreg Kroah-Hartman return port->ops->carrier_raised(port); 33296fd7ce5SGreg Kroah-Hartman } 33396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_carrier_raised); 33496fd7ce5SGreg Kroah-Hartman 33596fd7ce5SGreg Kroah-Hartman /** 33696fd7ce5SGreg Kroah-Hartman * tty_port_raise_dtr_rts - Raise DTR/RTS 33796fd7ce5SGreg Kroah-Hartman * @port: tty port 33896fd7ce5SGreg Kroah-Hartman * 33996fd7ce5SGreg Kroah-Hartman * Wrapper for the DTR/RTS raise logic. For the moment this is used 34096fd7ce5SGreg Kroah-Hartman * to hide some internal details. This will eventually become entirely 34196fd7ce5SGreg Kroah-Hartman * internal to the tty port. 34296fd7ce5SGreg Kroah-Hartman */ 34396fd7ce5SGreg Kroah-Hartman 34496fd7ce5SGreg Kroah-Hartman void tty_port_raise_dtr_rts(struct tty_port *port) 34596fd7ce5SGreg Kroah-Hartman { 34696fd7ce5SGreg Kroah-Hartman if (port->ops->dtr_rts) 34796fd7ce5SGreg Kroah-Hartman port->ops->dtr_rts(port, 1); 34896fd7ce5SGreg Kroah-Hartman } 34996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_raise_dtr_rts); 35096fd7ce5SGreg Kroah-Hartman 35196fd7ce5SGreg Kroah-Hartman /** 35296fd7ce5SGreg Kroah-Hartman * tty_port_lower_dtr_rts - Lower DTR/RTS 35396fd7ce5SGreg Kroah-Hartman * @port: tty port 35496fd7ce5SGreg Kroah-Hartman * 35596fd7ce5SGreg Kroah-Hartman * Wrapper for the DTR/RTS raise logic. For the moment this is used 35696fd7ce5SGreg Kroah-Hartman * to hide some internal details. This will eventually become entirely 35796fd7ce5SGreg Kroah-Hartman * internal to the tty port. 35896fd7ce5SGreg Kroah-Hartman */ 35996fd7ce5SGreg Kroah-Hartman 36096fd7ce5SGreg Kroah-Hartman void tty_port_lower_dtr_rts(struct tty_port *port) 36196fd7ce5SGreg Kroah-Hartman { 36296fd7ce5SGreg Kroah-Hartman if (port->ops->dtr_rts) 36396fd7ce5SGreg Kroah-Hartman port->ops->dtr_rts(port, 0); 36496fd7ce5SGreg Kroah-Hartman } 36596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_lower_dtr_rts); 36696fd7ce5SGreg Kroah-Hartman 36796fd7ce5SGreg Kroah-Hartman /** 36896fd7ce5SGreg Kroah-Hartman * tty_port_block_til_ready - Waiting logic for tty open 36996fd7ce5SGreg Kroah-Hartman * @port: the tty port being opened 37096fd7ce5SGreg Kroah-Hartman * @tty: the tty device being bound 371ed3f0af8SAlan Cox * @filp: the file pointer of the opener or NULL 37296fd7ce5SGreg Kroah-Hartman * 37396fd7ce5SGreg Kroah-Hartman * Implement the core POSIX/SuS tty behaviour when opening a tty device. 37496fd7ce5SGreg Kroah-Hartman * Handles: 37596fd7ce5SGreg Kroah-Hartman * - hangup (both before and during) 37696fd7ce5SGreg Kroah-Hartman * - non blocking open 37796fd7ce5SGreg Kroah-Hartman * - rts/dtr/dcd 37896fd7ce5SGreg Kroah-Hartman * - signals 37996fd7ce5SGreg Kroah-Hartman * - port flags and counts 38096fd7ce5SGreg Kroah-Hartman * 38196fd7ce5SGreg Kroah-Hartman * The passed tty_port must implement the carrier_raised method if it can 38296fd7ce5SGreg Kroah-Hartman * do carrier detect and the dtr_rts method if it supports software 38396fd7ce5SGreg Kroah-Hartman * management of these lines. Note that the dtr/rts raise is done each 38496fd7ce5SGreg Kroah-Hartman * iteration as a hangup may have previously dropped them while we wait. 385c590f6b6SPeter Hurley * 386c590f6b6SPeter Hurley * Caller holds tty lock. 387c590f6b6SPeter Hurley * 388c590f6b6SPeter Hurley * NB: May drop and reacquire tty lock when blocking, so tty and tty_port 389c590f6b6SPeter Hurley * may have changed state (eg., may have been hung up). 39096fd7ce5SGreg Kroah-Hartman */ 39196fd7ce5SGreg Kroah-Hartman 39296fd7ce5SGreg Kroah-Hartman int tty_port_block_til_ready(struct tty_port *port, 39396fd7ce5SGreg Kroah-Hartman struct tty_struct *tty, struct file *filp) 39496fd7ce5SGreg Kroah-Hartman { 39596fd7ce5SGreg Kroah-Hartman int do_clocal = 0, retval; 39696fd7ce5SGreg Kroah-Hartman unsigned long flags; 39796fd7ce5SGreg Kroah-Hartman DEFINE_WAIT(wait); 39896fd7ce5SGreg Kroah-Hartman 39996fd7ce5SGreg Kroah-Hartman /* if non-blocking mode is set we can pass directly to open unless 40096fd7ce5SGreg Kroah-Hartman the port has just hung up or is in another error state */ 40118900ca6SPeter Hurley if (tty_io_error(tty)) { 402807c8d81SPeter Hurley tty_port_set_active(port, 1); 40396fd7ce5SGreg Kroah-Hartman return 0; 40496fd7ce5SGreg Kroah-Hartman } 405ed3f0af8SAlan Cox if (filp == NULL || (filp->f_flags & O_NONBLOCK)) { 40696fd7ce5SGreg Kroah-Hartman /* Indicate we are open */ 4079db276f8SPeter Hurley if (C_BAUD(tty)) 40896fd7ce5SGreg Kroah-Hartman tty_port_raise_dtr_rts(port); 409807c8d81SPeter Hurley tty_port_set_active(port, 1); 41096fd7ce5SGreg Kroah-Hartman return 0; 41196fd7ce5SGreg Kroah-Hartman } 41296fd7ce5SGreg Kroah-Hartman 41396fd7ce5SGreg Kroah-Hartman if (C_CLOCAL(tty)) 41496fd7ce5SGreg Kroah-Hartman do_clocal = 1; 41596fd7ce5SGreg Kroah-Hartman 41696fd7ce5SGreg Kroah-Hartman /* Block waiting until we can proceed. We may need to wait for the 41796fd7ce5SGreg Kroah-Hartman carrier, but we must also wait for any close that is in progress 41896fd7ce5SGreg Kroah-Hartman before the next open may complete */ 41996fd7ce5SGreg Kroah-Hartman 42096fd7ce5SGreg Kroah-Hartman retval = 0; 42196fd7ce5SGreg Kroah-Hartman 42296fd7ce5SGreg Kroah-Hartman /* The port lock protects the port counts */ 42396fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 42496fd7ce5SGreg Kroah-Hartman port->count--; 42596fd7ce5SGreg Kroah-Hartman port->blocked_open++; 42696fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 42796fd7ce5SGreg Kroah-Hartman 42896fd7ce5SGreg Kroah-Hartman while (1) { 42996fd7ce5SGreg Kroah-Hartman /* Indicate we are open */ 430d41861caSPeter Hurley if (C_BAUD(tty) && tty_port_initialized(port)) 43196fd7ce5SGreg Kroah-Hartman tty_port_raise_dtr_rts(port); 43296fd7ce5SGreg Kroah-Hartman 43396fd7ce5SGreg Kroah-Hartman prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE); 43496fd7ce5SGreg Kroah-Hartman /* Check for a hangup or uninitialised port. 43596fd7ce5SGreg Kroah-Hartman Return accordingly */ 436d41861caSPeter Hurley if (tty_hung_up_p(filp) || !tty_port_initialized(port)) { 43796fd7ce5SGreg Kroah-Hartman if (port->flags & ASYNC_HUP_NOTIFY) 43896fd7ce5SGreg Kroah-Hartman retval = -EAGAIN; 43996fd7ce5SGreg Kroah-Hartman else 44096fd7ce5SGreg Kroah-Hartman retval = -ERESTARTSYS; 44196fd7ce5SGreg Kroah-Hartman break; 44296fd7ce5SGreg Kroah-Hartman } 4430eee50afSJiri Slaby /* 4440eee50afSJiri Slaby * Probe the carrier. For devices with no carrier detect 4450eee50afSJiri Slaby * tty_port_carrier_raised will always return true. 4460eee50afSJiri Slaby * Never ask drivers if CLOCAL is set, this causes troubles 4470eee50afSJiri Slaby * on some hardware. 4480eee50afSJiri Slaby */ 449fef062cbSPeter Hurley if (do_clocal || tty_port_carrier_raised(port)) 45096fd7ce5SGreg Kroah-Hartman break; 45196fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) { 45296fd7ce5SGreg Kroah-Hartman retval = -ERESTARTSYS; 45396fd7ce5SGreg Kroah-Hartman break; 45496fd7ce5SGreg Kroah-Hartman } 45589c8d91eSAlan Cox tty_unlock(tty); 45696fd7ce5SGreg Kroah-Hartman schedule(); 45789c8d91eSAlan Cox tty_lock(tty); 45896fd7ce5SGreg Kroah-Hartman } 45996fd7ce5SGreg Kroah-Hartman finish_wait(&port->open_wait, &wait); 46096fd7ce5SGreg Kroah-Hartman 46196fd7ce5SGreg Kroah-Hartman /* Update counts. A parallel hangup will have set count to zero and 46296fd7ce5SGreg Kroah-Hartman we must not mess that up further */ 46396fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 46496fd7ce5SGreg Kroah-Hartman if (!tty_hung_up_p(filp)) 46596fd7ce5SGreg Kroah-Hartman port->count++; 46696fd7ce5SGreg Kroah-Hartman port->blocked_open--; 46796fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 468807c8d81SPeter Hurley if (retval == 0) 469807c8d81SPeter Hurley tty_port_set_active(port, 1); 47096fd7ce5SGreg Kroah-Hartman return retval; 47196fd7ce5SGreg Kroah-Hartman } 47296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_block_til_ready); 47396fd7ce5SGreg Kroah-Hartman 474b74414f5SJohan Hovold static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty) 475b74414f5SJohan Hovold { 476b74414f5SJohan Hovold unsigned int bps = tty_get_baud_rate(tty); 477b74414f5SJohan Hovold long timeout; 478b74414f5SJohan Hovold 479b74414f5SJohan Hovold if (bps > 1200) { 480b74414f5SJohan Hovold timeout = (HZ * 10 * port->drain_delay) / bps; 481b74414f5SJohan Hovold timeout = max_t(long, timeout, HZ / 10); 482b74414f5SJohan Hovold } else { 483b74414f5SJohan Hovold timeout = 2 * HZ; 484b74414f5SJohan Hovold } 485b74414f5SJohan Hovold schedule_timeout_interruptible(timeout); 486b74414f5SJohan Hovold } 487b74414f5SJohan Hovold 48879c1faa4SPeter Hurley /* Caller holds tty lock. */ 48996fd7ce5SGreg Kroah-Hartman int tty_port_close_start(struct tty_port *port, 49096fd7ce5SGreg Kroah-Hartman struct tty_struct *tty, struct file *filp) 49196fd7ce5SGreg Kroah-Hartman { 49296fd7ce5SGreg Kroah-Hartman unsigned long flags; 49396fd7ce5SGreg Kroah-Hartman 494633caba8SPeter Hurley if (tty_hung_up_p(filp)) 49596fd7ce5SGreg Kroah-Hartman return 0; 49696fd7ce5SGreg Kroah-Hartman 497633caba8SPeter Hurley spin_lock_irqsave(&port->lock, flags); 49896fd7ce5SGreg Kroah-Hartman if (tty->count == 1 && port->count != 1) { 499339f36baSPeter Hurley tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__, 50096fd7ce5SGreg Kroah-Hartman port->count); 50196fd7ce5SGreg Kroah-Hartman port->count = 1; 50296fd7ce5SGreg Kroah-Hartman } 50396fd7ce5SGreg Kroah-Hartman if (--port->count < 0) { 504339f36baSPeter Hurley tty_warn(tty, "%s: bad port count (%d)\n", __func__, 50596fd7ce5SGreg Kroah-Hartman port->count); 50696fd7ce5SGreg Kroah-Hartman port->count = 0; 50796fd7ce5SGreg Kroah-Hartman } 50896fd7ce5SGreg Kroah-Hartman 50996fd7ce5SGreg Kroah-Hartman if (port->count) { 51096fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 51196fd7ce5SGreg Kroah-Hartman return 0; 51296fd7ce5SGreg Kroah-Hartman } 51396fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 5140b2588caSJohan Hovold 515ddc7b758SPeter Hurley tty->closing = 1; 516ddc7b758SPeter Hurley 517d41861caSPeter Hurley if (tty_port_initialized(port)) { 51896fd7ce5SGreg Kroah-Hartman /* Don't block on a stalled port, just pull the chain */ 51996fd7ce5SGreg Kroah-Hartman if (tty->flow_stopped) 52096fd7ce5SGreg Kroah-Hartman tty_driver_flush_buffer(tty); 5210b2588caSJohan Hovold if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE) 52279c1faa4SPeter Hurley tty_wait_until_sent(tty, port->closing_wait); 523b74414f5SJohan Hovold if (port->drain_delay) 524b74414f5SJohan Hovold tty_port_drain_delay(port, tty); 5250b2588caSJohan Hovold } 52696fd7ce5SGreg Kroah-Hartman /* Flush the ldisc buffering */ 52796fd7ce5SGreg Kroah-Hartman tty_ldisc_flush(tty); 52896fd7ce5SGreg Kroah-Hartman 529469d6d06SPeter Hurley /* Report to caller this is the last port reference */ 53096fd7ce5SGreg Kroah-Hartman return 1; 53196fd7ce5SGreg Kroah-Hartman } 53296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_start); 53396fd7ce5SGreg Kroah-Hartman 5340733db91SPeter Hurley /* Caller holds tty lock */ 53596fd7ce5SGreg Kroah-Hartman void tty_port_close_end(struct tty_port *port, struct tty_struct *tty) 53696fd7ce5SGreg Kroah-Hartman { 53796fd7ce5SGreg Kroah-Hartman unsigned long flags; 53896fd7ce5SGreg Kroah-Hartman 5393f40f5b2SPeter Hurley tty_ldisc_flush(tty); 54096fd7ce5SGreg Kroah-Hartman tty->closing = 0; 54196fd7ce5SGreg Kroah-Hartman 542ddc7b758SPeter Hurley spin_lock_irqsave(&port->lock, flags); 543ddc7b758SPeter Hurley 54496fd7ce5SGreg Kroah-Hartman if (port->blocked_open) { 54596fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 5465823323eSPeter Hurley if (port->close_delay) 5475823323eSPeter Hurley msleep_interruptible(jiffies_to_msecs(port->close_delay)); 54896fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 54996fd7ce5SGreg Kroah-Hartman wake_up_interruptible(&port->open_wait); 55096fd7ce5SGreg Kroah-Hartman } 55196fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 552807c8d81SPeter Hurley tty_port_set_active(port, 0); 55396fd7ce5SGreg Kroah-Hartman } 55496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_end); 55596fd7ce5SGreg Kroah-Hartman 5560733db91SPeter Hurley /** 5570733db91SPeter Hurley * tty_port_close 5580733db91SPeter Hurley * 5590733db91SPeter Hurley * Caller holds tty lock 5600733db91SPeter Hurley */ 56196fd7ce5SGreg Kroah-Hartman void tty_port_close(struct tty_port *port, struct tty_struct *tty, 56296fd7ce5SGreg Kroah-Hartman struct file *filp) 56396fd7ce5SGreg Kroah-Hartman { 56496fd7ce5SGreg Kroah-Hartman if (tty_port_close_start(port, tty, filp) == 0) 56596fd7ce5SGreg Kroah-Hartman return; 566957dacaeSJohan Hovold tty_port_shutdown(port, tty); 56796fd7ce5SGreg Kroah-Hartman set_bit(TTY_IO_ERROR, &tty->flags); 56896fd7ce5SGreg Kroah-Hartman tty_port_close_end(port, tty); 56996fd7ce5SGreg Kroah-Hartman tty_port_tty_set(port, NULL); 57096fd7ce5SGreg Kroah-Hartman } 57196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close); 57296fd7ce5SGreg Kroah-Hartman 57372a33bf5SJiri Slaby /** 57472a33bf5SJiri Slaby * tty_port_install - generic tty->ops->install handler 57572a33bf5SJiri Slaby * @port: tty_port of the device 57672a33bf5SJiri Slaby * @driver: tty_driver for this device 57772a33bf5SJiri Slaby * @tty: tty to be installed 57872a33bf5SJiri Slaby * 57972a33bf5SJiri Slaby * It is the same as tty_standard_install except the provided @port is linked 58072a33bf5SJiri Slaby * to a concrete tty specified by @tty. Use this or tty_port_register_device 58172a33bf5SJiri Slaby * (or both). Call tty_port_link_device as a last resort. 58272a33bf5SJiri Slaby */ 583695586caSJiri Slaby int tty_port_install(struct tty_port *port, struct tty_driver *driver, 584695586caSJiri Slaby struct tty_struct *tty) 585695586caSJiri Slaby { 586695586caSJiri Slaby tty->port = port; 587695586caSJiri Slaby return tty_standard_install(driver, tty); 588695586caSJiri Slaby } 589695586caSJiri Slaby EXPORT_SYMBOL_GPL(tty_port_install); 590695586caSJiri Slaby 591addd4672SPeter Hurley /** 592addd4672SPeter Hurley * tty_port_open 593addd4672SPeter Hurley * 594addd4672SPeter Hurley * Caller holds tty lock. 595addd4672SPeter Hurley * 596addd4672SPeter Hurley * NB: may drop and reacquire tty lock (in tty_port_block_til_ready()) so 597addd4672SPeter Hurley * tty and tty_port may have changed state (eg., may be hung up now) 598addd4672SPeter Hurley */ 59996fd7ce5SGreg Kroah-Hartman int tty_port_open(struct tty_port *port, struct tty_struct *tty, 60096fd7ce5SGreg Kroah-Hartman struct file *filp) 60196fd7ce5SGreg Kroah-Hartman { 60296fd7ce5SGreg Kroah-Hartman spin_lock_irq(&port->lock); 60396fd7ce5SGreg Kroah-Hartman ++port->count; 60496fd7ce5SGreg Kroah-Hartman spin_unlock_irq(&port->lock); 60596fd7ce5SGreg Kroah-Hartman tty_port_tty_set(port, tty); 60696fd7ce5SGreg Kroah-Hartman 60796fd7ce5SGreg Kroah-Hartman /* 60896fd7ce5SGreg Kroah-Hartman * Do the device-specific open only if the hardware isn't 60996fd7ce5SGreg Kroah-Hartman * already initialized. Serialize open and shutdown using the 61096fd7ce5SGreg Kroah-Hartman * port mutex. 61196fd7ce5SGreg Kroah-Hartman */ 61296fd7ce5SGreg Kroah-Hartman 61396fd7ce5SGreg Kroah-Hartman mutex_lock(&port->mutex); 61496fd7ce5SGreg Kroah-Hartman 615d41861caSPeter Hurley if (!tty_port_initialized(port)) { 61696fd7ce5SGreg Kroah-Hartman clear_bit(TTY_IO_ERROR, &tty->flags); 61796fd7ce5SGreg Kroah-Hartman if (port->ops->activate) { 61896fd7ce5SGreg Kroah-Hartman int retval = port->ops->activate(port, tty); 61996fd7ce5SGreg Kroah-Hartman if (retval) { 62096fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->mutex); 62196fd7ce5SGreg Kroah-Hartman return retval; 62296fd7ce5SGreg Kroah-Hartman } 62396fd7ce5SGreg Kroah-Hartman } 624d41861caSPeter Hurley tty_port_set_initialized(port, 1); 62596fd7ce5SGreg Kroah-Hartman } 62696fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->mutex); 62796fd7ce5SGreg Kroah-Hartman return tty_port_block_til_ready(port, tty, filp); 62896fd7ce5SGreg Kroah-Hartman } 62996fd7ce5SGreg Kroah-Hartman 63096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_open); 631