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/init.h> 1696fd7ce5SGreg Kroah-Hartman #include <linux/wait.h> 1796fd7ce5SGreg Kroah-Hartman #include <linux/bitops.h> 1896fd7ce5SGreg Kroah-Hartman #include <linux/delay.h> 1996fd7ce5SGreg Kroah-Hartman #include <linux/module.h> 2096fd7ce5SGreg Kroah-Hartman 2196fd7ce5SGreg Kroah-Hartman void tty_port_init(struct tty_port *port) 2296fd7ce5SGreg Kroah-Hartman { 2396fd7ce5SGreg Kroah-Hartman memset(port, 0, sizeof(*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 36057eb856SJiri Slaby struct device *tty_port_register_device(struct tty_port *port, 37057eb856SJiri Slaby struct tty_driver *driver, unsigned index, 38057eb856SJiri Slaby struct device *device) 39057eb856SJiri Slaby { 40057eb856SJiri Slaby driver->ports[index] = port; 41057eb856SJiri Slaby return tty_register_device(driver, index, device); 42057eb856SJiri Slaby } 43057eb856SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_register_device); 44057eb856SJiri Slaby 4596fd7ce5SGreg Kroah-Hartman int tty_port_alloc_xmit_buf(struct tty_port *port) 4696fd7ce5SGreg Kroah-Hartman { 4796fd7ce5SGreg Kroah-Hartman /* We may sleep in get_zeroed_page() */ 4896fd7ce5SGreg Kroah-Hartman mutex_lock(&port->buf_mutex); 4996fd7ce5SGreg Kroah-Hartman if (port->xmit_buf == NULL) 5096fd7ce5SGreg Kroah-Hartman port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL); 5196fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->buf_mutex); 5296fd7ce5SGreg Kroah-Hartman if (port->xmit_buf == NULL) 5396fd7ce5SGreg Kroah-Hartman return -ENOMEM; 5496fd7ce5SGreg Kroah-Hartman return 0; 5596fd7ce5SGreg Kroah-Hartman } 5696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_alloc_xmit_buf); 5796fd7ce5SGreg Kroah-Hartman 5896fd7ce5SGreg Kroah-Hartman void tty_port_free_xmit_buf(struct tty_port *port) 5996fd7ce5SGreg Kroah-Hartman { 6096fd7ce5SGreg Kroah-Hartman mutex_lock(&port->buf_mutex); 6196fd7ce5SGreg Kroah-Hartman if (port->xmit_buf != NULL) { 6296fd7ce5SGreg Kroah-Hartman free_page((unsigned long)port->xmit_buf); 6396fd7ce5SGreg Kroah-Hartman port->xmit_buf = NULL; 6496fd7ce5SGreg Kroah-Hartman } 6596fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->buf_mutex); 6696fd7ce5SGreg Kroah-Hartman } 6796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_free_xmit_buf); 6896fd7ce5SGreg Kroah-Hartman 6996fd7ce5SGreg Kroah-Hartman static void tty_port_destructor(struct kref *kref) 7096fd7ce5SGreg Kroah-Hartman { 7196fd7ce5SGreg Kroah-Hartman struct tty_port *port = container_of(kref, struct tty_port, kref); 7296fd7ce5SGreg Kroah-Hartman if (port->xmit_buf) 7396fd7ce5SGreg Kroah-Hartman free_page((unsigned long)port->xmit_buf); 7496fd7ce5SGreg Kroah-Hartman if (port->ops->destruct) 7596fd7ce5SGreg Kroah-Hartman port->ops->destruct(port); 7696fd7ce5SGreg Kroah-Hartman else 7796fd7ce5SGreg Kroah-Hartman kfree(port); 7896fd7ce5SGreg Kroah-Hartman } 7996fd7ce5SGreg Kroah-Hartman 8096fd7ce5SGreg Kroah-Hartman void tty_port_put(struct tty_port *port) 8196fd7ce5SGreg Kroah-Hartman { 8296fd7ce5SGreg Kroah-Hartman if (port) 8396fd7ce5SGreg Kroah-Hartman kref_put(&port->kref, tty_port_destructor); 8496fd7ce5SGreg Kroah-Hartman } 8596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_put); 8696fd7ce5SGreg Kroah-Hartman 8796fd7ce5SGreg Kroah-Hartman /** 8896fd7ce5SGreg Kroah-Hartman * tty_port_tty_get - get a tty reference 8996fd7ce5SGreg Kroah-Hartman * @port: tty port 9096fd7ce5SGreg Kroah-Hartman * 9196fd7ce5SGreg Kroah-Hartman * Return a refcount protected tty instance or NULL if the port is not 9296fd7ce5SGreg Kroah-Hartman * associated with a tty (eg due to close or hangup) 9396fd7ce5SGreg Kroah-Hartman */ 9496fd7ce5SGreg Kroah-Hartman 9596fd7ce5SGreg Kroah-Hartman struct tty_struct *tty_port_tty_get(struct tty_port *port) 9696fd7ce5SGreg Kroah-Hartman { 9796fd7ce5SGreg Kroah-Hartman unsigned long flags; 9896fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 9996fd7ce5SGreg Kroah-Hartman 10096fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 10196fd7ce5SGreg Kroah-Hartman tty = tty_kref_get(port->tty); 10296fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 10396fd7ce5SGreg Kroah-Hartman return tty; 10496fd7ce5SGreg Kroah-Hartman } 10596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_get); 10696fd7ce5SGreg Kroah-Hartman 10796fd7ce5SGreg Kroah-Hartman /** 10896fd7ce5SGreg Kroah-Hartman * tty_port_tty_set - set the tty of a port 10996fd7ce5SGreg Kroah-Hartman * @port: tty port 11096fd7ce5SGreg Kroah-Hartman * @tty: the tty 11196fd7ce5SGreg Kroah-Hartman * 11296fd7ce5SGreg Kroah-Hartman * Associate the port and tty pair. Manages any internal refcounts. 11396fd7ce5SGreg Kroah-Hartman * Pass NULL to deassociate a port 11496fd7ce5SGreg Kroah-Hartman */ 11596fd7ce5SGreg Kroah-Hartman 11696fd7ce5SGreg Kroah-Hartman void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty) 11796fd7ce5SGreg Kroah-Hartman { 11896fd7ce5SGreg Kroah-Hartman unsigned long flags; 11996fd7ce5SGreg Kroah-Hartman 12096fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 12196fd7ce5SGreg Kroah-Hartman if (port->tty) 12296fd7ce5SGreg Kroah-Hartman tty_kref_put(port->tty); 12396fd7ce5SGreg Kroah-Hartman port->tty = tty_kref_get(tty); 12496fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 12596fd7ce5SGreg Kroah-Hartman } 12696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_set); 12796fd7ce5SGreg Kroah-Hartman 12896fd7ce5SGreg Kroah-Hartman static void tty_port_shutdown(struct tty_port *port) 12996fd7ce5SGreg Kroah-Hartman { 13096fd7ce5SGreg Kroah-Hartman mutex_lock(&port->mutex); 13196fd7ce5SGreg Kroah-Hartman if (port->ops->shutdown && !port->console && 13296fd7ce5SGreg Kroah-Hartman test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) 13396fd7ce5SGreg Kroah-Hartman port->ops->shutdown(port); 13496fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->mutex); 13596fd7ce5SGreg Kroah-Hartman } 13696fd7ce5SGreg Kroah-Hartman 13796fd7ce5SGreg Kroah-Hartman /** 13896fd7ce5SGreg Kroah-Hartman * tty_port_hangup - hangup helper 13996fd7ce5SGreg Kroah-Hartman * @port: tty port 14096fd7ce5SGreg Kroah-Hartman * 14196fd7ce5SGreg Kroah-Hartman * Perform port level tty hangup flag and count changes. Drop the tty 14296fd7ce5SGreg Kroah-Hartman * reference. 14396fd7ce5SGreg Kroah-Hartman */ 14496fd7ce5SGreg Kroah-Hartman 14596fd7ce5SGreg Kroah-Hartman void tty_port_hangup(struct tty_port *port) 14696fd7ce5SGreg Kroah-Hartman { 14796fd7ce5SGreg Kroah-Hartman unsigned long flags; 14896fd7ce5SGreg Kroah-Hartman 14996fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 15096fd7ce5SGreg Kroah-Hartman port->count = 0; 15196fd7ce5SGreg Kroah-Hartman port->flags &= ~ASYNC_NORMAL_ACTIVE; 15296fd7ce5SGreg Kroah-Hartman if (port->tty) { 15396fd7ce5SGreg Kroah-Hartman set_bit(TTY_IO_ERROR, &port->tty->flags); 15496fd7ce5SGreg Kroah-Hartman tty_kref_put(port->tty); 15596fd7ce5SGreg Kroah-Hartman } 15696fd7ce5SGreg Kroah-Hartman port->tty = NULL; 15796fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 15896fd7ce5SGreg Kroah-Hartman wake_up_interruptible(&port->open_wait); 15996fd7ce5SGreg Kroah-Hartman wake_up_interruptible(&port->delta_msr_wait); 16096fd7ce5SGreg Kroah-Hartman tty_port_shutdown(port); 16196fd7ce5SGreg Kroah-Hartman } 16296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_hangup); 16396fd7ce5SGreg Kroah-Hartman 16496fd7ce5SGreg Kroah-Hartman /** 16596fd7ce5SGreg Kroah-Hartman * tty_port_carrier_raised - carrier raised check 16696fd7ce5SGreg Kroah-Hartman * @port: tty port 16796fd7ce5SGreg Kroah-Hartman * 16896fd7ce5SGreg Kroah-Hartman * Wrapper for the carrier detect logic. For the moment this is used 16996fd7ce5SGreg Kroah-Hartman * to hide some internal details. This will eventually become entirely 17096fd7ce5SGreg Kroah-Hartman * internal to the tty port. 17196fd7ce5SGreg Kroah-Hartman */ 17296fd7ce5SGreg Kroah-Hartman 17396fd7ce5SGreg Kroah-Hartman int tty_port_carrier_raised(struct tty_port *port) 17496fd7ce5SGreg Kroah-Hartman { 17596fd7ce5SGreg Kroah-Hartman if (port->ops->carrier_raised == NULL) 17696fd7ce5SGreg Kroah-Hartman return 1; 17796fd7ce5SGreg Kroah-Hartman return port->ops->carrier_raised(port); 17896fd7ce5SGreg Kroah-Hartman } 17996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_carrier_raised); 18096fd7ce5SGreg Kroah-Hartman 18196fd7ce5SGreg Kroah-Hartman /** 18296fd7ce5SGreg Kroah-Hartman * tty_port_raise_dtr_rts - Raise DTR/RTS 18396fd7ce5SGreg Kroah-Hartman * @port: tty port 18496fd7ce5SGreg Kroah-Hartman * 18596fd7ce5SGreg Kroah-Hartman * Wrapper for the DTR/RTS raise logic. For the moment this is used 18696fd7ce5SGreg Kroah-Hartman * to hide some internal details. This will eventually become entirely 18796fd7ce5SGreg Kroah-Hartman * internal to the tty port. 18896fd7ce5SGreg Kroah-Hartman */ 18996fd7ce5SGreg Kroah-Hartman 19096fd7ce5SGreg Kroah-Hartman void tty_port_raise_dtr_rts(struct tty_port *port) 19196fd7ce5SGreg Kroah-Hartman { 19296fd7ce5SGreg Kroah-Hartman if (port->ops->dtr_rts) 19396fd7ce5SGreg Kroah-Hartman port->ops->dtr_rts(port, 1); 19496fd7ce5SGreg Kroah-Hartman } 19596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_raise_dtr_rts); 19696fd7ce5SGreg Kroah-Hartman 19796fd7ce5SGreg Kroah-Hartman /** 19896fd7ce5SGreg Kroah-Hartman * tty_port_lower_dtr_rts - Lower DTR/RTS 19996fd7ce5SGreg Kroah-Hartman * @port: tty port 20096fd7ce5SGreg Kroah-Hartman * 20196fd7ce5SGreg Kroah-Hartman * Wrapper for the DTR/RTS raise logic. For the moment this is used 20296fd7ce5SGreg Kroah-Hartman * to hide some internal details. This will eventually become entirely 20396fd7ce5SGreg Kroah-Hartman * internal to the tty port. 20496fd7ce5SGreg Kroah-Hartman */ 20596fd7ce5SGreg Kroah-Hartman 20696fd7ce5SGreg Kroah-Hartman void tty_port_lower_dtr_rts(struct tty_port *port) 20796fd7ce5SGreg Kroah-Hartman { 20896fd7ce5SGreg Kroah-Hartman if (port->ops->dtr_rts) 20996fd7ce5SGreg Kroah-Hartman port->ops->dtr_rts(port, 0); 21096fd7ce5SGreg Kroah-Hartman } 21196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_lower_dtr_rts); 21296fd7ce5SGreg Kroah-Hartman 21396fd7ce5SGreg Kroah-Hartman /** 21496fd7ce5SGreg Kroah-Hartman * tty_port_block_til_ready - Waiting logic for tty open 21596fd7ce5SGreg Kroah-Hartman * @port: the tty port being opened 21696fd7ce5SGreg Kroah-Hartman * @tty: the tty device being bound 21796fd7ce5SGreg Kroah-Hartman * @filp: the file pointer of the opener 21896fd7ce5SGreg Kroah-Hartman * 21996fd7ce5SGreg Kroah-Hartman * Implement the core POSIX/SuS tty behaviour when opening a tty device. 22096fd7ce5SGreg Kroah-Hartman * Handles: 22196fd7ce5SGreg Kroah-Hartman * - hangup (both before and during) 22296fd7ce5SGreg Kroah-Hartman * - non blocking open 22396fd7ce5SGreg Kroah-Hartman * - rts/dtr/dcd 22496fd7ce5SGreg Kroah-Hartman * - signals 22596fd7ce5SGreg Kroah-Hartman * - port flags and counts 22696fd7ce5SGreg Kroah-Hartman * 22796fd7ce5SGreg Kroah-Hartman * The passed tty_port must implement the carrier_raised method if it can 22896fd7ce5SGreg Kroah-Hartman * do carrier detect and the dtr_rts method if it supports software 22996fd7ce5SGreg Kroah-Hartman * management of these lines. Note that the dtr/rts raise is done each 23096fd7ce5SGreg Kroah-Hartman * iteration as a hangup may have previously dropped them while we wait. 23196fd7ce5SGreg Kroah-Hartman */ 23296fd7ce5SGreg Kroah-Hartman 23396fd7ce5SGreg Kroah-Hartman int tty_port_block_til_ready(struct tty_port *port, 23496fd7ce5SGreg Kroah-Hartman struct tty_struct *tty, struct file *filp) 23596fd7ce5SGreg Kroah-Hartman { 23696fd7ce5SGreg Kroah-Hartman int do_clocal = 0, retval; 23796fd7ce5SGreg Kroah-Hartman unsigned long flags; 23896fd7ce5SGreg Kroah-Hartman DEFINE_WAIT(wait); 23996fd7ce5SGreg Kroah-Hartman 24096fd7ce5SGreg Kroah-Hartman /* block if port is in the process of being closed */ 24196fd7ce5SGreg Kroah-Hartman if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) { 2426d31a88cSAlan Cox wait_event_interruptible_tty(port->close_wait, 24396fd7ce5SGreg Kroah-Hartman !(port->flags & ASYNC_CLOSING)); 24496fd7ce5SGreg Kroah-Hartman if (port->flags & ASYNC_HUP_NOTIFY) 24596fd7ce5SGreg Kroah-Hartman return -EAGAIN; 24696fd7ce5SGreg Kroah-Hartman else 24796fd7ce5SGreg Kroah-Hartman return -ERESTARTSYS; 24896fd7ce5SGreg Kroah-Hartman } 24996fd7ce5SGreg Kroah-Hartman 25096fd7ce5SGreg Kroah-Hartman /* if non-blocking mode is set we can pass directly to open unless 25196fd7ce5SGreg Kroah-Hartman the port has just hung up or is in another error state */ 25296fd7ce5SGreg Kroah-Hartman if (tty->flags & (1 << TTY_IO_ERROR)) { 25396fd7ce5SGreg Kroah-Hartman port->flags |= ASYNC_NORMAL_ACTIVE; 25496fd7ce5SGreg Kroah-Hartman return 0; 25596fd7ce5SGreg Kroah-Hartman } 25696fd7ce5SGreg Kroah-Hartman if (filp->f_flags & O_NONBLOCK) { 25796fd7ce5SGreg Kroah-Hartman /* Indicate we are open */ 258*adc8d746SAlan Cox if (tty->termios.c_cflag & CBAUD) 25996fd7ce5SGreg Kroah-Hartman tty_port_raise_dtr_rts(port); 26096fd7ce5SGreg Kroah-Hartman port->flags |= ASYNC_NORMAL_ACTIVE; 26196fd7ce5SGreg Kroah-Hartman return 0; 26296fd7ce5SGreg Kroah-Hartman } 26396fd7ce5SGreg Kroah-Hartman 26496fd7ce5SGreg Kroah-Hartman if (C_CLOCAL(tty)) 26596fd7ce5SGreg Kroah-Hartman do_clocal = 1; 26696fd7ce5SGreg Kroah-Hartman 26796fd7ce5SGreg Kroah-Hartman /* Block waiting until we can proceed. We may need to wait for the 26896fd7ce5SGreg Kroah-Hartman carrier, but we must also wait for any close that is in progress 26996fd7ce5SGreg Kroah-Hartman before the next open may complete */ 27096fd7ce5SGreg Kroah-Hartman 27196fd7ce5SGreg Kroah-Hartman retval = 0; 27296fd7ce5SGreg Kroah-Hartman 27396fd7ce5SGreg Kroah-Hartman /* The port lock protects the port counts */ 27496fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 27596fd7ce5SGreg Kroah-Hartman if (!tty_hung_up_p(filp)) 27696fd7ce5SGreg Kroah-Hartman port->count--; 27796fd7ce5SGreg Kroah-Hartman port->blocked_open++; 27896fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 27996fd7ce5SGreg Kroah-Hartman 28096fd7ce5SGreg Kroah-Hartman while (1) { 28196fd7ce5SGreg Kroah-Hartman /* Indicate we are open */ 282*adc8d746SAlan Cox if (tty->termios.c_cflag & CBAUD) 28396fd7ce5SGreg Kroah-Hartman tty_port_raise_dtr_rts(port); 28496fd7ce5SGreg Kroah-Hartman 28596fd7ce5SGreg Kroah-Hartman prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE); 28696fd7ce5SGreg Kroah-Hartman /* Check for a hangup or uninitialised port. 28796fd7ce5SGreg Kroah-Hartman Return accordingly */ 28896fd7ce5SGreg Kroah-Hartman if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) { 28996fd7ce5SGreg Kroah-Hartman if (port->flags & ASYNC_HUP_NOTIFY) 29096fd7ce5SGreg Kroah-Hartman retval = -EAGAIN; 29196fd7ce5SGreg Kroah-Hartman else 29296fd7ce5SGreg Kroah-Hartman retval = -ERESTARTSYS; 29396fd7ce5SGreg Kroah-Hartman break; 29496fd7ce5SGreg Kroah-Hartman } 2950eee50afSJiri Slaby /* 2960eee50afSJiri Slaby * Probe the carrier. For devices with no carrier detect 2970eee50afSJiri Slaby * tty_port_carrier_raised will always return true. 2980eee50afSJiri Slaby * Never ask drivers if CLOCAL is set, this causes troubles 2990eee50afSJiri Slaby * on some hardware. 3000eee50afSJiri Slaby */ 30196fd7ce5SGreg Kroah-Hartman if (!(port->flags & ASYNC_CLOSING) && 3020eee50afSJiri Slaby (do_clocal || tty_port_carrier_raised(port))) 30396fd7ce5SGreg Kroah-Hartman break; 30496fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) { 30596fd7ce5SGreg Kroah-Hartman retval = -ERESTARTSYS; 30696fd7ce5SGreg Kroah-Hartman break; 30796fd7ce5SGreg Kroah-Hartman } 3086d31a88cSAlan Cox tty_unlock(); 30996fd7ce5SGreg Kroah-Hartman schedule(); 3106d31a88cSAlan Cox tty_lock(); 31196fd7ce5SGreg Kroah-Hartman } 31296fd7ce5SGreg Kroah-Hartman finish_wait(&port->open_wait, &wait); 31396fd7ce5SGreg Kroah-Hartman 31496fd7ce5SGreg Kroah-Hartman /* Update counts. A parallel hangup will have set count to zero and 31596fd7ce5SGreg Kroah-Hartman we must not mess that up further */ 31696fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 31796fd7ce5SGreg Kroah-Hartman if (!tty_hung_up_p(filp)) 31896fd7ce5SGreg Kroah-Hartman port->count++; 31996fd7ce5SGreg Kroah-Hartman port->blocked_open--; 32096fd7ce5SGreg Kroah-Hartman if (retval == 0) 32196fd7ce5SGreg Kroah-Hartman port->flags |= ASYNC_NORMAL_ACTIVE; 32296fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 32396fd7ce5SGreg Kroah-Hartman return retval; 32496fd7ce5SGreg Kroah-Hartman } 32596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_block_til_ready); 32696fd7ce5SGreg Kroah-Hartman 32796fd7ce5SGreg Kroah-Hartman int tty_port_close_start(struct tty_port *port, 32896fd7ce5SGreg Kroah-Hartman struct tty_struct *tty, struct file *filp) 32996fd7ce5SGreg Kroah-Hartman { 33096fd7ce5SGreg Kroah-Hartman unsigned long flags; 33196fd7ce5SGreg Kroah-Hartman 33296fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 33396fd7ce5SGreg Kroah-Hartman if (tty_hung_up_p(filp)) { 33496fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 33596fd7ce5SGreg Kroah-Hartman return 0; 33696fd7ce5SGreg Kroah-Hartman } 33796fd7ce5SGreg Kroah-Hartman 33896fd7ce5SGreg Kroah-Hartman if (tty->count == 1 && port->count != 1) { 33996fd7ce5SGreg Kroah-Hartman printk(KERN_WARNING 34096fd7ce5SGreg Kroah-Hartman "tty_port_close_start: tty->count = 1 port count = %d.\n", 34196fd7ce5SGreg Kroah-Hartman port->count); 34296fd7ce5SGreg Kroah-Hartman port->count = 1; 34396fd7ce5SGreg Kroah-Hartman } 34496fd7ce5SGreg Kroah-Hartman if (--port->count < 0) { 34596fd7ce5SGreg Kroah-Hartman printk(KERN_WARNING "tty_port_close_start: count = %d\n", 34696fd7ce5SGreg Kroah-Hartman port->count); 34796fd7ce5SGreg Kroah-Hartman port->count = 0; 34896fd7ce5SGreg Kroah-Hartman } 34996fd7ce5SGreg Kroah-Hartman 35096fd7ce5SGreg Kroah-Hartman if (port->count) { 35196fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 35296fd7ce5SGreg Kroah-Hartman if (port->ops->drop) 35396fd7ce5SGreg Kroah-Hartman port->ops->drop(port); 35496fd7ce5SGreg Kroah-Hartman return 0; 35596fd7ce5SGreg Kroah-Hartman } 35696fd7ce5SGreg Kroah-Hartman set_bit(ASYNCB_CLOSING, &port->flags); 35796fd7ce5SGreg Kroah-Hartman tty->closing = 1; 35896fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 35996fd7ce5SGreg Kroah-Hartman /* Don't block on a stalled port, just pull the chain */ 36096fd7ce5SGreg Kroah-Hartman if (tty->flow_stopped) 36196fd7ce5SGreg Kroah-Hartman tty_driver_flush_buffer(tty); 36296fd7ce5SGreg Kroah-Hartman if (test_bit(ASYNCB_INITIALIZED, &port->flags) && 36396fd7ce5SGreg Kroah-Hartman port->closing_wait != ASYNC_CLOSING_WAIT_NONE) 364424cc039SJiri Slaby tty_wait_until_sent_from_close(tty, port->closing_wait); 36596fd7ce5SGreg Kroah-Hartman if (port->drain_delay) { 36696fd7ce5SGreg Kroah-Hartman unsigned int bps = tty_get_baud_rate(tty); 36796fd7ce5SGreg Kroah-Hartman long timeout; 36896fd7ce5SGreg Kroah-Hartman 36996fd7ce5SGreg Kroah-Hartman if (bps > 1200) 37096fd7ce5SGreg Kroah-Hartman timeout = max_t(long, 37196fd7ce5SGreg Kroah-Hartman (HZ * 10 * port->drain_delay) / bps, HZ / 10); 37296fd7ce5SGreg Kroah-Hartman else 37396fd7ce5SGreg Kroah-Hartman timeout = 2 * HZ; 37496fd7ce5SGreg Kroah-Hartman schedule_timeout_interruptible(timeout); 37596fd7ce5SGreg Kroah-Hartman } 37696fd7ce5SGreg Kroah-Hartman /* Flush the ldisc buffering */ 37796fd7ce5SGreg Kroah-Hartman tty_ldisc_flush(tty); 37896fd7ce5SGreg Kroah-Hartman 37996fd7ce5SGreg Kroah-Hartman /* Drop DTR/RTS if HUPCL is set. This causes any attached modem to 38096fd7ce5SGreg Kroah-Hartman hang up the line */ 381*adc8d746SAlan Cox if (tty->termios.c_cflag & HUPCL) 38296fd7ce5SGreg Kroah-Hartman tty_port_lower_dtr_rts(port); 38396fd7ce5SGreg Kroah-Hartman 38496fd7ce5SGreg Kroah-Hartman /* Don't call port->drop for the last reference. Callers will want 38596fd7ce5SGreg Kroah-Hartman to drop the last active reference in ->shutdown() or the tty 38696fd7ce5SGreg Kroah-Hartman shutdown path */ 38796fd7ce5SGreg Kroah-Hartman return 1; 38896fd7ce5SGreg Kroah-Hartman } 38996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_start); 39096fd7ce5SGreg Kroah-Hartman 39196fd7ce5SGreg Kroah-Hartman void tty_port_close_end(struct tty_port *port, struct tty_struct *tty) 39296fd7ce5SGreg Kroah-Hartman { 39396fd7ce5SGreg Kroah-Hartman unsigned long flags; 39496fd7ce5SGreg Kroah-Hartman 39596fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 39696fd7ce5SGreg Kroah-Hartman tty->closing = 0; 39796fd7ce5SGreg Kroah-Hartman 39896fd7ce5SGreg Kroah-Hartman if (port->blocked_open) { 39996fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 40096fd7ce5SGreg Kroah-Hartman if (port->close_delay) { 40196fd7ce5SGreg Kroah-Hartman msleep_interruptible( 40296fd7ce5SGreg Kroah-Hartman jiffies_to_msecs(port->close_delay)); 40396fd7ce5SGreg Kroah-Hartman } 40496fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 40596fd7ce5SGreg Kroah-Hartman wake_up_interruptible(&port->open_wait); 40696fd7ce5SGreg Kroah-Hartman } 40796fd7ce5SGreg Kroah-Hartman port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING); 40896fd7ce5SGreg Kroah-Hartman wake_up_interruptible(&port->close_wait); 40996fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 41096fd7ce5SGreg Kroah-Hartman } 41196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_end); 41296fd7ce5SGreg Kroah-Hartman 41396fd7ce5SGreg Kroah-Hartman void tty_port_close(struct tty_port *port, struct tty_struct *tty, 41496fd7ce5SGreg Kroah-Hartman struct file *filp) 41596fd7ce5SGreg Kroah-Hartman { 41696fd7ce5SGreg Kroah-Hartman if (tty_port_close_start(port, tty, filp) == 0) 41796fd7ce5SGreg Kroah-Hartman return; 41896fd7ce5SGreg Kroah-Hartman tty_port_shutdown(port); 41996fd7ce5SGreg Kroah-Hartman set_bit(TTY_IO_ERROR, &tty->flags); 42096fd7ce5SGreg Kroah-Hartman tty_port_close_end(port, tty); 42196fd7ce5SGreg Kroah-Hartman tty_port_tty_set(port, NULL); 42296fd7ce5SGreg Kroah-Hartman } 42396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close); 42496fd7ce5SGreg Kroah-Hartman 425695586caSJiri Slaby int tty_port_install(struct tty_port *port, struct tty_driver *driver, 426695586caSJiri Slaby struct tty_struct *tty) 427695586caSJiri Slaby { 428695586caSJiri Slaby tty->port = port; 429695586caSJiri Slaby return tty_standard_install(driver, tty); 430695586caSJiri Slaby } 431695586caSJiri Slaby EXPORT_SYMBOL_GPL(tty_port_install); 432695586caSJiri Slaby 43396fd7ce5SGreg Kroah-Hartman int tty_port_open(struct tty_port *port, struct tty_struct *tty, 43496fd7ce5SGreg Kroah-Hartman struct file *filp) 43596fd7ce5SGreg Kroah-Hartman { 43696fd7ce5SGreg Kroah-Hartman spin_lock_irq(&port->lock); 43796fd7ce5SGreg Kroah-Hartman if (!tty_hung_up_p(filp)) 43896fd7ce5SGreg Kroah-Hartman ++port->count; 43996fd7ce5SGreg Kroah-Hartman spin_unlock_irq(&port->lock); 44096fd7ce5SGreg Kroah-Hartman tty_port_tty_set(port, tty); 44196fd7ce5SGreg Kroah-Hartman 44296fd7ce5SGreg Kroah-Hartman /* 44396fd7ce5SGreg Kroah-Hartman * Do the device-specific open only if the hardware isn't 44496fd7ce5SGreg Kroah-Hartman * already initialized. Serialize open and shutdown using the 44596fd7ce5SGreg Kroah-Hartman * port mutex. 44696fd7ce5SGreg Kroah-Hartman */ 44796fd7ce5SGreg Kroah-Hartman 44896fd7ce5SGreg Kroah-Hartman mutex_lock(&port->mutex); 44996fd7ce5SGreg Kroah-Hartman 45096fd7ce5SGreg Kroah-Hartman if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) { 45196fd7ce5SGreg Kroah-Hartman clear_bit(TTY_IO_ERROR, &tty->flags); 45296fd7ce5SGreg Kroah-Hartman if (port->ops->activate) { 45396fd7ce5SGreg Kroah-Hartman int retval = port->ops->activate(port, tty); 45496fd7ce5SGreg Kroah-Hartman if (retval) { 45596fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->mutex); 45696fd7ce5SGreg Kroah-Hartman return retval; 45796fd7ce5SGreg Kroah-Hartman } 45896fd7ce5SGreg Kroah-Hartman } 45996fd7ce5SGreg Kroah-Hartman set_bit(ASYNCB_INITIALIZED, &port->flags); 46096fd7ce5SGreg Kroah-Hartman } 46196fd7ce5SGreg Kroah-Hartman mutex_unlock(&port->mutex); 46296fd7ce5SGreg Kroah-Hartman return tty_port_block_til_ready(port, tty, filp); 46396fd7ce5SGreg Kroah-Hartman } 46496fd7ce5SGreg Kroah-Hartman 46596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_open); 466