Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Tty port functions
0004  */
0005 
0006 #include <linux/types.h>
0007 #include <linux/errno.h>
0008 #include <linux/tty.h>
0009 #include <linux/tty_driver.h>
0010 #include <linux/tty_flip.h>
0011 #include <linux/serial.h>
0012 #include <linux/timer.h>
0013 #include <linux/string.h>
0014 #include <linux/slab.h>
0015 #include <linux/sched/signal.h>
0016 #include <linux/wait.h>
0017 #include <linux/bitops.h>
0018 #include <linux/delay.h>
0019 #include <linux/module.h>
0020 #include <linux/serdev.h>
0021 #include "tty.h"
0022 
0023 static int tty_port_default_receive_buf(struct tty_port *port,
0024                     const unsigned char *p,
0025                     const unsigned char *f, size_t count)
0026 {
0027     int ret;
0028     struct tty_struct *tty;
0029     struct tty_ldisc *disc;
0030 
0031     tty = READ_ONCE(port->itty);
0032     if (!tty)
0033         return 0;
0034 
0035     disc = tty_ldisc_ref(tty);
0036     if (!disc)
0037         return 0;
0038 
0039     ret = tty_ldisc_receive_buf(disc, p, (char *)f, count);
0040 
0041     tty_ldisc_deref(disc);
0042 
0043     return ret;
0044 }
0045 
0046 static void tty_port_default_lookahead_buf(struct tty_port *port, const unsigned char *p,
0047                        const unsigned char *f, unsigned int count)
0048 {
0049     struct tty_struct *tty;
0050     struct tty_ldisc *disc;
0051 
0052     tty = READ_ONCE(port->itty);
0053     if (!tty)
0054         return;
0055 
0056     disc = tty_ldisc_ref(tty);
0057     if (!disc)
0058         return;
0059 
0060     if (disc->ops->lookahead_buf)
0061         disc->ops->lookahead_buf(disc->tty, p, f, count);
0062 
0063     tty_ldisc_deref(disc);
0064 }
0065 
0066 static void tty_port_default_wakeup(struct tty_port *port)
0067 {
0068     struct tty_struct *tty = tty_port_tty_get(port);
0069 
0070     if (tty) {
0071         tty_wakeup(tty);
0072         tty_kref_put(tty);
0073     }
0074 }
0075 
0076 const struct tty_port_client_operations tty_port_default_client_ops = {
0077     .receive_buf = tty_port_default_receive_buf,
0078     .lookahead_buf = tty_port_default_lookahead_buf,
0079     .write_wakeup = tty_port_default_wakeup,
0080 };
0081 EXPORT_SYMBOL_GPL(tty_port_default_client_ops);
0082 
0083 /**
0084  * tty_port_init -- initialize tty_port
0085  * @port: tty_port to initialize
0086  *
0087  * Initializes the state of struct tty_port. When a port was initialized using
0088  * this function, one has to destroy the port by tty_port_destroy(). Either
0089  * indirectly by using &tty_port refcounting (tty_port_put()) or directly if
0090  * refcounting is not used.
0091  */
0092 void tty_port_init(struct tty_port *port)
0093 {
0094     memset(port, 0, sizeof(*port));
0095     tty_buffer_init(port);
0096     init_waitqueue_head(&port->open_wait);
0097     init_waitqueue_head(&port->delta_msr_wait);
0098     mutex_init(&port->mutex);
0099     mutex_init(&port->buf_mutex);
0100     spin_lock_init(&port->lock);
0101     port->close_delay = (50 * HZ) / 100;
0102     port->closing_wait = (3000 * HZ) / 100;
0103     port->client_ops = &tty_port_default_client_ops;
0104     kref_init(&port->kref);
0105 }
0106 EXPORT_SYMBOL(tty_port_init);
0107 
0108 /**
0109  * tty_port_link_device - link tty and tty_port
0110  * @port: tty_port of the device
0111  * @driver: tty_driver for this device
0112  * @index: index of the tty
0113  *
0114  * Provide the tty layer with a link from a tty (specified by @index) to a
0115  * tty_port (@port). Use this only if neither tty_port_register_device() nor
0116  * tty_port_install() is used in the driver. If used, this has to be called
0117  * before tty_register_driver().
0118  */
0119 void tty_port_link_device(struct tty_port *port,
0120         struct tty_driver *driver, unsigned index)
0121 {
0122     if (WARN_ON(index >= driver->num))
0123         return;
0124     driver->ports[index] = port;
0125 }
0126 EXPORT_SYMBOL_GPL(tty_port_link_device);
0127 
0128 /**
0129  * tty_port_register_device - register tty device
0130  * @port: tty_port of the device
0131  * @driver: tty_driver for this device
0132  * @index: index of the tty
0133  * @device: parent if exists, otherwise NULL
0134  *
0135  * It is the same as tty_register_device() except the provided @port is linked
0136  * to a concrete tty specified by @index. Use this or tty_port_install() (or
0137  * both). Call tty_port_link_device() as a last resort.
0138  */
0139 struct device *tty_port_register_device(struct tty_port *port,
0140         struct tty_driver *driver, unsigned index,
0141         struct device *device)
0142 {
0143     return tty_port_register_device_attr(port, driver, index, device, NULL, NULL);
0144 }
0145 EXPORT_SYMBOL_GPL(tty_port_register_device);
0146 
0147 /**
0148  * tty_port_register_device_attr - register tty device
0149  * @port: tty_port of the device
0150  * @driver: tty_driver for this device
0151  * @index: index of the tty
0152  * @device: parent if exists, otherwise NULL
0153  * @drvdata: Driver data to be set to device.
0154  * @attr_grp: Attribute group to be set on device.
0155  *
0156  * It is the same as tty_register_device_attr() except the provided @port is
0157  * linked to a concrete tty specified by @index. Use this or tty_port_install()
0158  * (or both). Call tty_port_link_device() as a last resort.
0159  */
0160 struct device *tty_port_register_device_attr(struct tty_port *port,
0161         struct tty_driver *driver, unsigned index,
0162         struct device *device, void *drvdata,
0163         const struct attribute_group **attr_grp)
0164 {
0165     tty_port_link_device(port, driver, index);
0166     return tty_register_device_attr(driver, index, device, drvdata,
0167             attr_grp);
0168 }
0169 EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
0170 
0171 /**
0172  * tty_port_register_device_attr_serdev - register tty or serdev device
0173  * @port: tty_port of the device
0174  * @driver: tty_driver for this device
0175  * @index: index of the tty
0176  * @device: parent if exists, otherwise NULL
0177  * @drvdata: driver data for the device
0178  * @attr_grp: attribute group for the device
0179  *
0180  * Register a serdev or tty device depending on if the parent device has any
0181  * defined serdev clients or not.
0182  */
0183 struct device *tty_port_register_device_attr_serdev(struct tty_port *port,
0184         struct tty_driver *driver, unsigned index,
0185         struct device *device, void *drvdata,
0186         const struct attribute_group **attr_grp)
0187 {
0188     struct device *dev;
0189 
0190     tty_port_link_device(port, driver, index);
0191 
0192     dev = serdev_tty_port_register(port, device, driver, index);
0193     if (PTR_ERR(dev) != -ENODEV) {
0194         /* Skip creating cdev if we registered a serdev device */
0195         return dev;
0196     }
0197 
0198     return tty_register_device_attr(driver, index, device, drvdata,
0199             attr_grp);
0200 }
0201 EXPORT_SYMBOL_GPL(tty_port_register_device_attr_serdev);
0202 
0203 /**
0204  * tty_port_register_device_serdev - register tty or serdev device
0205  * @port: tty_port of the device
0206  * @driver: tty_driver for this device
0207  * @index: index of the tty
0208  * @device: parent if exists, otherwise NULL
0209  *
0210  * Register a serdev or tty device depending on if the parent device has any
0211  * defined serdev clients or not.
0212  */
0213 struct device *tty_port_register_device_serdev(struct tty_port *port,
0214         struct tty_driver *driver, unsigned index,
0215         struct device *device)
0216 {
0217     return tty_port_register_device_attr_serdev(port, driver, index,
0218             device, NULL, NULL);
0219 }
0220 EXPORT_SYMBOL_GPL(tty_port_register_device_serdev);
0221 
0222 /**
0223  * tty_port_unregister_device - deregister a tty or serdev device
0224  * @port: tty_port of the device
0225  * @driver: tty_driver for this device
0226  * @index: index of the tty
0227  *
0228  * If a tty or serdev device is registered with a call to
0229  * tty_port_register_device_serdev() then this function must be called when
0230  * the device is gone.
0231  */
0232 void tty_port_unregister_device(struct tty_port *port,
0233         struct tty_driver *driver, unsigned index)
0234 {
0235     int ret;
0236 
0237     ret = serdev_tty_port_unregister(port);
0238     if (ret == 0)
0239         return;
0240 
0241     tty_unregister_device(driver, index);
0242 }
0243 EXPORT_SYMBOL_GPL(tty_port_unregister_device);
0244 
0245 int tty_port_alloc_xmit_buf(struct tty_port *port)
0246 {
0247     /* We may sleep in get_zeroed_page() */
0248     mutex_lock(&port->buf_mutex);
0249     if (port->xmit_buf == NULL) {
0250         port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
0251         if (port->xmit_buf)
0252             kfifo_init(&port->xmit_fifo, port->xmit_buf, PAGE_SIZE);
0253     }
0254     mutex_unlock(&port->buf_mutex);
0255     if (port->xmit_buf == NULL)
0256         return -ENOMEM;
0257     return 0;
0258 }
0259 EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
0260 
0261 void tty_port_free_xmit_buf(struct tty_port *port)
0262 {
0263     mutex_lock(&port->buf_mutex);
0264     free_page((unsigned long)port->xmit_buf);
0265     port->xmit_buf = NULL;
0266     INIT_KFIFO(port->xmit_fifo);
0267     mutex_unlock(&port->buf_mutex);
0268 }
0269 EXPORT_SYMBOL(tty_port_free_xmit_buf);
0270 
0271 /**
0272  * tty_port_destroy -- destroy inited port
0273  * @port: tty port to be destroyed
0274  *
0275  * When a port was initialized using tty_port_init(), one has to destroy the
0276  * port by this function. Either indirectly by using &tty_port refcounting
0277  * (tty_port_put()) or directly if refcounting is not used.
0278  */
0279 void tty_port_destroy(struct tty_port *port)
0280 {
0281     tty_buffer_cancel_work(port);
0282     tty_buffer_free_all(port);
0283 }
0284 EXPORT_SYMBOL(tty_port_destroy);
0285 
0286 static void tty_port_destructor(struct kref *kref)
0287 {
0288     struct tty_port *port = container_of(kref, struct tty_port, kref);
0289 
0290     /* check if last port ref was dropped before tty release */
0291     if (WARN_ON(port->itty))
0292         return;
0293     free_page((unsigned long)port->xmit_buf);
0294     tty_port_destroy(port);
0295     if (port->ops && port->ops->destruct)
0296         port->ops->destruct(port);
0297     else
0298         kfree(port);
0299 }
0300 
0301 /**
0302  * tty_port_put -- drop a reference to tty_port
0303  * @port: port to drop a reference of (can be NULL)
0304  *
0305  * The final put will destroy and free up the @port using
0306  * @port->ops->destruct() hook, or using kfree() if not provided.
0307  */
0308 void tty_port_put(struct tty_port *port)
0309 {
0310     if (port)
0311         kref_put(&port->kref, tty_port_destructor);
0312 }
0313 EXPORT_SYMBOL(tty_port_put);
0314 
0315 /**
0316  * tty_port_tty_get -   get a tty reference
0317  * @port: tty port
0318  *
0319  * Return a refcount protected tty instance or %NULL if the port is not
0320  * associated with a tty (eg due to close or hangup).
0321  */
0322 struct tty_struct *tty_port_tty_get(struct tty_port *port)
0323 {
0324     unsigned long flags;
0325     struct tty_struct *tty;
0326 
0327     spin_lock_irqsave(&port->lock, flags);
0328     tty = tty_kref_get(port->tty);
0329     spin_unlock_irqrestore(&port->lock, flags);
0330     return tty;
0331 }
0332 EXPORT_SYMBOL(tty_port_tty_get);
0333 
0334 /**
0335  * tty_port_tty_set -   set the tty of a port
0336  * @port: tty port
0337  * @tty: the tty
0338  *
0339  * Associate the port and tty pair. Manages any internal refcounts. Pass %NULL
0340  * to deassociate a port.
0341  */
0342 void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
0343 {
0344     unsigned long flags;
0345 
0346     spin_lock_irqsave(&port->lock, flags);
0347     tty_kref_put(port->tty);
0348     port->tty = tty_kref_get(tty);
0349     spin_unlock_irqrestore(&port->lock, flags);
0350 }
0351 EXPORT_SYMBOL(tty_port_tty_set);
0352 
0353 /**
0354  * tty_port_shutdown - internal helper to shutdown the device
0355  * @port: tty port to be shut down
0356  * @tty: the associated tty
0357  *
0358  * It is used by tty_port_hangup() and tty_port_close(). Its task is to
0359  * shutdown the device if it was initialized (note consoles remain
0360  * functioning). It lowers DTR/RTS (if @tty has HUPCL set) and invokes
0361  * @port->ops->shutdown().
0362  */
0363 static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
0364 {
0365     mutex_lock(&port->mutex);
0366     if (port->console)
0367         goto out;
0368 
0369     if (tty_port_initialized(port)) {
0370         tty_port_set_initialized(port, 0);
0371         /*
0372          * Drop DTR/RTS if HUPCL is set. This causes any attached
0373          * modem to hang up the line.
0374          */
0375         if (tty && C_HUPCL(tty))
0376             tty_port_lower_dtr_rts(port);
0377 
0378         if (port->ops->shutdown)
0379             port->ops->shutdown(port);
0380     }
0381 out:
0382     mutex_unlock(&port->mutex);
0383 }
0384 
0385 /**
0386  * tty_port_hangup      -   hangup helper
0387  * @port: tty port
0388  *
0389  * Perform port level tty hangup flag and count changes. Drop the tty
0390  * reference.
0391  *
0392  * Caller holds tty lock.
0393  */
0394 void tty_port_hangup(struct tty_port *port)
0395 {
0396     struct tty_struct *tty;
0397     unsigned long flags;
0398 
0399     spin_lock_irqsave(&port->lock, flags);
0400     port->count = 0;
0401     tty = port->tty;
0402     if (tty)
0403         set_bit(TTY_IO_ERROR, &tty->flags);
0404     port->tty = NULL;
0405     spin_unlock_irqrestore(&port->lock, flags);
0406     tty_port_set_active(port, 0);
0407     tty_port_shutdown(port, tty);
0408     tty_kref_put(tty);
0409     wake_up_interruptible(&port->open_wait);
0410     wake_up_interruptible(&port->delta_msr_wait);
0411 }
0412 EXPORT_SYMBOL(tty_port_hangup);
0413 
0414 /**
0415  * tty_port_tty_hangup - helper to hang up a tty
0416  * @port: tty port
0417  * @check_clocal: hang only ttys with %CLOCAL unset?
0418  */
0419 void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
0420 {
0421     struct tty_struct *tty = tty_port_tty_get(port);
0422 
0423     if (tty && (!check_clocal || !C_CLOCAL(tty)))
0424         tty_hangup(tty);
0425     tty_kref_put(tty);
0426 }
0427 EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
0428 
0429 /**
0430  * tty_port_tty_wakeup - helper to wake up a tty
0431  * @port: tty port
0432  */
0433 void tty_port_tty_wakeup(struct tty_port *port)
0434 {
0435     port->client_ops->write_wakeup(port);
0436 }
0437 EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
0438 
0439 /**
0440  * tty_port_carrier_raised  -   carrier raised check
0441  * @port: tty port
0442  *
0443  * Wrapper for the carrier detect logic. For the moment this is used
0444  * to hide some internal details. This will eventually become entirely
0445  * internal to the tty port.
0446  */
0447 int tty_port_carrier_raised(struct tty_port *port)
0448 {
0449     if (port->ops->carrier_raised == NULL)
0450         return 1;
0451     return port->ops->carrier_raised(port);
0452 }
0453 EXPORT_SYMBOL(tty_port_carrier_raised);
0454 
0455 /**
0456  * tty_port_raise_dtr_rts   -   Raise DTR/RTS
0457  * @port: tty port
0458  *
0459  * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide
0460  * some internal details. This will eventually become entirely internal to the
0461  * tty port.
0462  */
0463 void tty_port_raise_dtr_rts(struct tty_port *port)
0464 {
0465     if (port->ops->dtr_rts)
0466         port->ops->dtr_rts(port, 1);
0467 }
0468 EXPORT_SYMBOL(tty_port_raise_dtr_rts);
0469 
0470 /**
0471  * tty_port_lower_dtr_rts   -   Lower DTR/RTS
0472  * @port: tty port
0473  *
0474  * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide
0475  * some internal details. This will eventually become entirely internal to the
0476  * tty port.
0477  */
0478 void tty_port_lower_dtr_rts(struct tty_port *port)
0479 {
0480     if (port->ops->dtr_rts)
0481         port->ops->dtr_rts(port, 0);
0482 }
0483 EXPORT_SYMBOL(tty_port_lower_dtr_rts);
0484 
0485 /**
0486  * tty_port_block_til_ready -   Waiting logic for tty open
0487  * @port: the tty port being opened
0488  * @tty: the tty device being bound
0489  * @filp: the file pointer of the opener or %NULL
0490  *
0491  * Implement the core POSIX/SuS tty behaviour when opening a tty device.
0492  * Handles:
0493  *
0494  *  - hangup (both before and during)
0495  *  - non blocking open
0496  *  - rts/dtr/dcd
0497  *  - signals
0498  *  - port flags and counts
0499  *
0500  * The passed @port must implement the @port->ops->carrier_raised method if it
0501  * can do carrier detect and the @port->ops->dtr_rts method if it supports
0502  * software management of these lines. Note that the dtr/rts raise is done each
0503  * iteration as a hangup may have previously dropped them while we wait.
0504  *
0505  * Caller holds tty lock.
0506  *
0507  * Note: May drop and reacquire tty lock when blocking, so @tty and @port may
0508  * have changed state (eg., may have been hung up).
0509  */
0510 int tty_port_block_til_ready(struct tty_port *port,
0511                 struct tty_struct *tty, struct file *filp)
0512 {
0513     int do_clocal = 0, retval;
0514     unsigned long flags;
0515     DEFINE_WAIT(wait);
0516 
0517     /* if non-blocking mode is set we can pass directly to open unless
0518      * the port has just hung up or is in another error state.
0519      */
0520     if (tty_io_error(tty)) {
0521         tty_port_set_active(port, 1);
0522         return 0;
0523     }
0524     if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
0525         /* Indicate we are open */
0526         if (C_BAUD(tty))
0527             tty_port_raise_dtr_rts(port);
0528         tty_port_set_active(port, 1);
0529         return 0;
0530     }
0531 
0532     if (C_CLOCAL(tty))
0533         do_clocal = 1;
0534 
0535     /* Block waiting until we can proceed. We may need to wait for the
0536      * carrier, but we must also wait for any close that is in progress
0537      * before the next open may complete.
0538      */
0539 
0540     retval = 0;
0541 
0542     /* The port lock protects the port counts */
0543     spin_lock_irqsave(&port->lock, flags);
0544     port->count--;
0545     port->blocked_open++;
0546     spin_unlock_irqrestore(&port->lock, flags);
0547 
0548     while (1) {
0549         /* Indicate we are open */
0550         if (C_BAUD(tty) && tty_port_initialized(port))
0551             tty_port_raise_dtr_rts(port);
0552 
0553         prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
0554         /* Check for a hangup or uninitialised port.
0555          * Return accordingly.
0556          */
0557         if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
0558             if (port->flags & ASYNC_HUP_NOTIFY)
0559                 retval = -EAGAIN;
0560             else
0561                 retval = -ERESTARTSYS;
0562             break;
0563         }
0564         /*
0565          * Probe the carrier. For devices with no carrier detect
0566          * tty_port_carrier_raised will always return true.
0567          * Never ask drivers if CLOCAL is set, this causes troubles
0568          * on some hardware.
0569          */
0570         if (do_clocal || tty_port_carrier_raised(port))
0571             break;
0572         if (signal_pending(current)) {
0573             retval = -ERESTARTSYS;
0574             break;
0575         }
0576         tty_unlock(tty);
0577         schedule();
0578         tty_lock(tty);
0579     }
0580     finish_wait(&port->open_wait, &wait);
0581 
0582     /* Update counts. A parallel hangup will have set count to zero and
0583      * we must not mess that up further.
0584      */
0585     spin_lock_irqsave(&port->lock, flags);
0586     if (!tty_hung_up_p(filp))
0587         port->count++;
0588     port->blocked_open--;
0589     spin_unlock_irqrestore(&port->lock, flags);
0590     if (retval == 0)
0591         tty_port_set_active(port, 1);
0592     return retval;
0593 }
0594 EXPORT_SYMBOL(tty_port_block_til_ready);
0595 
0596 static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
0597 {
0598     unsigned int bps = tty_get_baud_rate(tty);
0599     long timeout;
0600 
0601     if (bps > 1200) {
0602         timeout = (HZ * 10 * port->drain_delay) / bps;
0603         timeout = max_t(long, timeout, HZ / 10);
0604     } else {
0605         timeout = 2 * HZ;
0606     }
0607     schedule_timeout_interruptible(timeout);
0608 }
0609 
0610 /**
0611  * tty_port_close_start - helper for tty->ops->close, part 1/2
0612  * @port: tty_port of the device
0613  * @tty: tty being closed
0614  * @filp: passed file pointer
0615  *
0616  * Decrements and checks open count. Flushes the port if this is the last
0617  * close. That means, dropping the data from the outpu buffer on the device and
0618  * waiting for sending logic to finish. The rest of close handling is performed
0619  * in tty_port_close_end().
0620  *
0621  * Locking: Caller holds tty lock.
0622  *
0623  * Return: 1 if this is the last close, otherwise 0
0624  */
0625 int tty_port_close_start(struct tty_port *port,
0626                 struct tty_struct *tty, struct file *filp)
0627 {
0628     unsigned long flags;
0629 
0630     if (tty_hung_up_p(filp))
0631         return 0;
0632 
0633     spin_lock_irqsave(&port->lock, flags);
0634     if (tty->count == 1 && port->count != 1) {
0635         tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
0636              port->count);
0637         port->count = 1;
0638     }
0639     if (--port->count < 0) {
0640         tty_warn(tty, "%s: bad port count (%d)\n", __func__,
0641              port->count);
0642         port->count = 0;
0643     }
0644 
0645     if (port->count) {
0646         spin_unlock_irqrestore(&port->lock, flags);
0647         return 0;
0648     }
0649     spin_unlock_irqrestore(&port->lock, flags);
0650 
0651     tty->closing = 1;
0652 
0653     if (tty_port_initialized(port)) {
0654         /* Don't block on a stalled port, just pull the chain */
0655         if (tty->flow.tco_stopped)
0656             tty_driver_flush_buffer(tty);
0657         if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
0658             tty_wait_until_sent(tty, port->closing_wait);
0659         if (port->drain_delay)
0660             tty_port_drain_delay(port, tty);
0661     }
0662     /* Flush the ldisc buffering */
0663     tty_ldisc_flush(tty);
0664 
0665     /* Report to caller this is the last port reference */
0666     return 1;
0667 }
0668 EXPORT_SYMBOL(tty_port_close_start);
0669 
0670 /**
0671  * tty_port_close_end - helper for tty->ops->close, part 2/2
0672  * @port: tty_port of the device
0673  * @tty: tty being closed
0674  *
0675  * This is a continuation of the first part: tty_port_close_start(). This
0676  * should be called after turning off the device. It flushes the data from the
0677  * line discipline and delays the close by @port->close_delay.
0678  *
0679  * Locking: Caller holds tty lock.
0680  */
0681 void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
0682 {
0683     unsigned long flags;
0684 
0685     tty_ldisc_flush(tty);
0686     tty->closing = 0;
0687 
0688     spin_lock_irqsave(&port->lock, flags);
0689 
0690     if (port->blocked_open) {
0691         spin_unlock_irqrestore(&port->lock, flags);
0692         if (port->close_delay)
0693             msleep_interruptible(jiffies_to_msecs(port->close_delay));
0694         spin_lock_irqsave(&port->lock, flags);
0695         wake_up_interruptible(&port->open_wait);
0696     }
0697     spin_unlock_irqrestore(&port->lock, flags);
0698     tty_port_set_active(port, 0);
0699 }
0700 EXPORT_SYMBOL(tty_port_close_end);
0701 
0702 /**
0703  * tty_port_close - generic tty->ops->close handler
0704  * @port: tty_port of the device
0705  * @tty: tty being closed
0706  * @filp: passed file pointer
0707  *
0708  * It is a generic helper to be used in driver's @tty->ops->close. It wraps a
0709  * sequence of tty_port_close_start(), tty_port_shutdown(), and
0710  * tty_port_close_end(). The latter two are called only if this is the last
0711  * close. See the respective functions for the details.
0712  *
0713  * Locking: Caller holds tty lock
0714  */
0715 void tty_port_close(struct tty_port *port, struct tty_struct *tty,
0716                             struct file *filp)
0717 {
0718     if (tty_port_close_start(port, tty, filp) == 0)
0719         return;
0720     tty_port_shutdown(port, tty);
0721     if (!port->console)
0722         set_bit(TTY_IO_ERROR, &tty->flags);
0723     tty_port_close_end(port, tty);
0724     tty_port_tty_set(port, NULL);
0725 }
0726 EXPORT_SYMBOL(tty_port_close);
0727 
0728 /**
0729  * tty_port_install - generic tty->ops->install handler
0730  * @port: tty_port of the device
0731  * @driver: tty_driver for this device
0732  * @tty: tty to be installed
0733  *
0734  * It is the same as tty_standard_install() except the provided @port is linked
0735  * to a concrete tty specified by @tty. Use this or tty_port_register_device()
0736  * (or both). Call tty_port_link_device() as a last resort.
0737  */
0738 int tty_port_install(struct tty_port *port, struct tty_driver *driver,
0739         struct tty_struct *tty)
0740 {
0741     tty->port = port;
0742     return tty_standard_install(driver, tty);
0743 }
0744 EXPORT_SYMBOL_GPL(tty_port_install);
0745 
0746 /**
0747  * tty_port_open - generic tty->ops->open handler
0748  * @port: tty_port of the device
0749  * @tty: tty to be opened
0750  * @filp: passed file pointer
0751  *
0752  * It is a generic helper to be used in driver's @tty->ops->open. It activates
0753  * the devices using @port->ops->activate if not active already. And waits for
0754  * the device to be ready using tty_port_block_til_ready() (e.g.  raises
0755  * DTR/CTS and waits for carrier).
0756  *
0757  * Note that @port->ops->shutdown is not called when @port->ops->activate
0758  * returns an error (on the contrary, @tty->ops->close is).
0759  *
0760  * Locking: Caller holds tty lock.
0761  *
0762  * Note: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
0763  * @tty and @port may have changed state (eg., may be hung up now).
0764  */
0765 int tty_port_open(struct tty_port *port, struct tty_struct *tty,
0766                             struct file *filp)
0767 {
0768     spin_lock_irq(&port->lock);
0769     ++port->count;
0770     spin_unlock_irq(&port->lock);
0771     tty_port_tty_set(port, tty);
0772 
0773     /*
0774      * Do the device-specific open only if the hardware isn't
0775      * already initialized. Serialize open and shutdown using the
0776      * port mutex.
0777      */
0778 
0779     mutex_lock(&port->mutex);
0780 
0781     if (!tty_port_initialized(port)) {
0782         clear_bit(TTY_IO_ERROR, &tty->flags);
0783         if (port->ops->activate) {
0784             int retval = port->ops->activate(port, tty);
0785 
0786             if (retval) {
0787                 mutex_unlock(&port->mutex);
0788                 return retval;
0789             }
0790         }
0791         tty_port_set_initialized(port, 1);
0792     }
0793     mutex_unlock(&port->mutex);
0794     return tty_port_block_til_ready(port, tty, filp);
0795 }
0796 EXPORT_SYMBOL(tty_port_open);