Greg Kroah-Hartman | e3b3d0f | 2017-11-06 18:11:51 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Tty port functions |
| 4 | */ |
| 5 | |
| 6 | #include <linux/types.h> |
| 7 | #include <linux/errno.h> |
| 8 | #include <linux/tty.h> |
| 9 | #include <linux/tty_driver.h> |
| 10 | #include <linux/tty_flip.h> |
Alan Cox | 3e61696 | 2009-01-02 13:45:26 +0000 | [diff] [blame] | 11 | #include <linux/serial.h> |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 12 | #include <linux/timer.h> |
| 13 | #include <linux/string.h> |
| 14 | #include <linux/slab.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 15 | #include <linux/sched/signal.h> |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 16 | #include <linux/wait.h> |
| 17 | #include <linux/bitops.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/module.h> |
Johan Hovold | 8cde11b | 2017-05-18 17:33:00 +0200 | [diff] [blame] | 20 | #include <linux/serdev.h> |
Greg Kroah-Hartman | 98602c0 | 2021-04-08 14:51:22 +0200 | [diff] [blame] | 21 | #include "tty.h" |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 22 | |
Rob Herring | c3485ee | 2017-02-02 13:48:05 -0600 | [diff] [blame] | 23 | static int tty_port_default_receive_buf(struct tty_port *port, |
| 24 | const unsigned char *p, |
| 25 | const unsigned char *f, size_t count) |
| 26 | { |
| 27 | int ret; |
| 28 | struct tty_struct *tty; |
| 29 | struct tty_ldisc *disc; |
| 30 | |
| 31 | tty = READ_ONCE(port->itty); |
| 32 | if (!tty) |
| 33 | return 0; |
| 34 | |
| 35 | disc = tty_ldisc_ref(tty); |
| 36 | if (!disc) |
| 37 | return 0; |
| 38 | |
| 39 | ret = tty_ldisc_receive_buf(disc, p, (char *)f, count); |
| 40 | |
| 41 | tty_ldisc_deref(disc); |
| 42 | |
| 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | static void tty_port_default_wakeup(struct tty_port *port) |
| 47 | { |
| 48 | struct tty_struct *tty = tty_port_tty_get(port); |
| 49 | |
| 50 | if (tty) { |
| 51 | tty_wakeup(tty); |
| 52 | tty_kref_put(tty); |
| 53 | } |
| 54 | } |
| 55 | |
Johan Hovold | 0c5aae5 | 2020-02-10 15:57:30 +0100 | [diff] [blame] | 56 | const struct tty_port_client_operations tty_port_default_client_ops = { |
Rob Herring | c3485ee | 2017-02-02 13:48:05 -0600 | [diff] [blame] | 57 | .receive_buf = tty_port_default_receive_buf, |
| 58 | .write_wakeup = tty_port_default_wakeup, |
| 59 | }; |
Johan Hovold | 0c5aae5 | 2020-02-10 15:57:30 +0100 | [diff] [blame] | 60 | EXPORT_SYMBOL_GPL(tty_port_default_client_ops); |
Rob Herring | c3485ee | 2017-02-02 13:48:05 -0600 | [diff] [blame] | 61 | |
Jiri Slaby | 3be491d | 2021-11-26 09:16:06 +0100 | [diff] [blame] | 62 | /** |
| 63 | * tty_port_init -- initialize tty_port |
| 64 | * @port: tty_port to initialize |
| 65 | * |
| 66 | * Initializes the state of struct tty_port. When a port was initialized using |
| 67 | * this function, one has to destroy the port by tty_port_destroy(). Either |
| 68 | * indirectly by using &tty_port refcounting (tty_port_put()) or directly if |
| 69 | * refcounting is not used. |
| 70 | */ |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 71 | void tty_port_init(struct tty_port *port) |
| 72 | { |
| 73 | memset(port, 0, sizeof(*port)); |
Jiri Slaby | ecbbfd4 | 2012-10-18 22:26:47 +0200 | [diff] [blame] | 74 | tty_buffer_init(port); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 75 | init_waitqueue_head(&port->open_wait); |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 76 | init_waitqueue_head(&port->delta_msr_wait); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 77 | mutex_init(&port->mutex); |
Alan Cox | 44e4909 | 2009-11-30 13:16:41 +0000 | [diff] [blame] | 78 | mutex_init(&port->buf_mutex); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 79 | spin_lock_init(&port->lock); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 80 | port->close_delay = (50 * HZ) / 100; |
| 81 | port->closing_wait = (3000 * HZ) / 100; |
Johan Hovold | 0c5aae5 | 2020-02-10 15:57:30 +0100 | [diff] [blame] | 82 | port->client_ops = &tty_port_default_client_ops; |
Alan Cox | 568aafc | 2009-11-30 13:17:14 +0000 | [diff] [blame] | 83 | kref_init(&port->kref); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 84 | } |
| 85 | EXPORT_SYMBOL(tty_port_init); |
| 86 | |
Jiri Slaby | 72a33bf | 2012-08-07 21:47:49 +0200 | [diff] [blame] | 87 | /** |
Jiri Slaby | 2cb4ca0 | 2012-08-07 21:47:50 +0200 | [diff] [blame] | 88 | * tty_port_link_device - link tty and tty_port |
| 89 | * @port: tty_port of the device |
| 90 | * @driver: tty_driver for this device |
| 91 | * @index: index of the tty |
| 92 | * |
Antonio Borneo | 1926e5d | 2017-09-08 08:59:42 +0200 | [diff] [blame] | 93 | * Provide the tty layer with a link from a tty (specified by @index) to a |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 94 | * tty_port (@port). Use this only if neither tty_port_register_device() nor |
| 95 | * tty_port_install() is used in the driver. If used, this has to be called |
| 96 | * before tty_register_driver(). |
Jiri Slaby | 2cb4ca0 | 2012-08-07 21:47:50 +0200 | [diff] [blame] | 97 | */ |
| 98 | void tty_port_link_device(struct tty_port *port, |
| 99 | struct tty_driver *driver, unsigned index) |
| 100 | { |
| 101 | if (WARN_ON(index >= driver->num)) |
| 102 | return; |
Sudip Mukherjee | 273f6329 | 2019-12-27 17:44:34 +0000 | [diff] [blame] | 103 | driver->ports[index] = port; |
Jiri Slaby | 2cb4ca0 | 2012-08-07 21:47:50 +0200 | [diff] [blame] | 104 | } |
| 105 | EXPORT_SYMBOL_GPL(tty_port_link_device); |
| 106 | |
| 107 | /** |
Jiri Slaby | 72a33bf | 2012-08-07 21:47:49 +0200 | [diff] [blame] | 108 | * tty_port_register_device - register tty device |
| 109 | * @port: tty_port of the device |
| 110 | * @driver: tty_driver for this device |
| 111 | * @index: index of the tty |
| 112 | * @device: parent if exists, otherwise NULL |
| 113 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 114 | * It is the same as tty_register_device() except the provided @port is linked |
| 115 | * to a concrete tty specified by @index. Use this or tty_port_install() (or |
| 116 | * both). Call tty_port_link_device() as a last resort. |
Jiri Slaby | 72a33bf | 2012-08-07 21:47:49 +0200 | [diff] [blame] | 117 | */ |
Jiri Slaby | 057eb85 | 2012-06-04 13:35:37 +0200 | [diff] [blame] | 118 | struct device *tty_port_register_device(struct tty_port *port, |
| 119 | struct tty_driver *driver, unsigned index, |
| 120 | struct device *device) |
| 121 | { |
Rob Herring | 3086365 | 2017-01-16 16:54:30 -0600 | [diff] [blame] | 122 | return tty_port_register_device_attr(port, driver, index, device, NULL, NULL); |
Jiri Slaby | 057eb85 | 2012-06-04 13:35:37 +0200 | [diff] [blame] | 123 | } |
| 124 | EXPORT_SYMBOL_GPL(tty_port_register_device); |
| 125 | |
Tomas Hlavacek | b1b7991 | 2012-09-06 23:17:47 +0200 | [diff] [blame] | 126 | /** |
| 127 | * tty_port_register_device_attr - register tty device |
| 128 | * @port: tty_port of the device |
| 129 | * @driver: tty_driver for this device |
| 130 | * @index: index of the tty |
| 131 | * @device: parent if exists, otherwise NULL |
| 132 | * @drvdata: Driver data to be set to device. |
| 133 | * @attr_grp: Attribute group to be set on device. |
| 134 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 135 | * It is the same as tty_register_device_attr() except the provided @port is |
| 136 | * linked to a concrete tty specified by @index. Use this or tty_port_install() |
| 137 | * (or both). Call tty_port_link_device() as a last resort. |
Tomas Hlavacek | b1b7991 | 2012-09-06 23:17:47 +0200 | [diff] [blame] | 138 | */ |
| 139 | struct device *tty_port_register_device_attr(struct tty_port *port, |
| 140 | struct tty_driver *driver, unsigned index, |
| 141 | struct device *device, void *drvdata, |
| 142 | const struct attribute_group **attr_grp) |
| 143 | { |
| 144 | tty_port_link_device(port, driver, index); |
| 145 | return tty_register_device_attr(driver, index, device, drvdata, |
| 146 | attr_grp); |
| 147 | } |
| 148 | EXPORT_SYMBOL_GPL(tty_port_register_device_attr); |
| 149 | |
Johan Hovold | 8cde11b | 2017-05-18 17:33:00 +0200 | [diff] [blame] | 150 | /** |
| 151 | * tty_port_register_device_attr_serdev - register tty or serdev device |
| 152 | * @port: tty_port of the device |
| 153 | * @driver: tty_driver for this device |
| 154 | * @index: index of the tty |
| 155 | * @device: parent if exists, otherwise NULL |
| 156 | * @drvdata: driver data for the device |
| 157 | * @attr_grp: attribute group for the device |
| 158 | * |
| 159 | * Register a serdev or tty device depending on if the parent device has any |
| 160 | * defined serdev clients or not. |
| 161 | */ |
| 162 | struct device *tty_port_register_device_attr_serdev(struct tty_port *port, |
| 163 | struct tty_driver *driver, unsigned index, |
| 164 | struct device *device, void *drvdata, |
| 165 | const struct attribute_group **attr_grp) |
| 166 | { |
| 167 | struct device *dev; |
| 168 | |
| 169 | tty_port_link_device(port, driver, index); |
| 170 | |
| 171 | dev = serdev_tty_port_register(port, device, driver, index); |
| 172 | if (PTR_ERR(dev) != -ENODEV) { |
| 173 | /* Skip creating cdev if we registered a serdev device */ |
| 174 | return dev; |
| 175 | } |
| 176 | |
| 177 | return tty_register_device_attr(driver, index, device, drvdata, |
| 178 | attr_grp); |
| 179 | } |
| 180 | EXPORT_SYMBOL_GPL(tty_port_register_device_attr_serdev); |
| 181 | |
| 182 | /** |
| 183 | * tty_port_register_device_serdev - register tty or serdev device |
| 184 | * @port: tty_port of the device |
| 185 | * @driver: tty_driver for this device |
| 186 | * @index: index of the tty |
| 187 | * @device: parent if exists, otherwise NULL |
| 188 | * |
| 189 | * Register a serdev or tty device depending on if the parent device has any |
| 190 | * defined serdev clients or not. |
| 191 | */ |
| 192 | struct device *tty_port_register_device_serdev(struct tty_port *port, |
| 193 | struct tty_driver *driver, unsigned index, |
| 194 | struct device *device) |
| 195 | { |
| 196 | return tty_port_register_device_attr_serdev(port, driver, index, |
| 197 | device, NULL, NULL); |
| 198 | } |
| 199 | EXPORT_SYMBOL_GPL(tty_port_register_device_serdev); |
| 200 | |
| 201 | /** |
| 202 | * tty_port_unregister_device - deregister a tty or serdev device |
| 203 | * @port: tty_port of the device |
| 204 | * @driver: tty_driver for this device |
| 205 | * @index: index of the tty |
| 206 | * |
| 207 | * If a tty or serdev device is registered with a call to |
| 208 | * tty_port_register_device_serdev() then this function must be called when |
| 209 | * the device is gone. |
| 210 | */ |
| 211 | void tty_port_unregister_device(struct tty_port *port, |
| 212 | struct tty_driver *driver, unsigned index) |
| 213 | { |
| 214 | int ret; |
| 215 | |
| 216 | ret = serdev_tty_port_unregister(port); |
| 217 | if (ret == 0) |
| 218 | return; |
| 219 | |
| 220 | tty_unregister_device(driver, index); |
| 221 | } |
| 222 | EXPORT_SYMBOL_GPL(tty_port_unregister_device); |
| 223 | |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 224 | int tty_port_alloc_xmit_buf(struct tty_port *port) |
| 225 | { |
| 226 | /* We may sleep in get_zeroed_page() */ |
Alan Cox | 44e4909 | 2009-11-30 13:16:41 +0000 | [diff] [blame] | 227 | mutex_lock(&port->buf_mutex); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 228 | if (port->xmit_buf == NULL) |
| 229 | port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL); |
Alan Cox | 44e4909 | 2009-11-30 13:16:41 +0000 | [diff] [blame] | 230 | mutex_unlock(&port->buf_mutex); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 231 | if (port->xmit_buf == NULL) |
| 232 | return -ENOMEM; |
| 233 | return 0; |
| 234 | } |
| 235 | EXPORT_SYMBOL(tty_port_alloc_xmit_buf); |
| 236 | |
| 237 | void tty_port_free_xmit_buf(struct tty_port *port) |
| 238 | { |
Alan Cox | 44e4909 | 2009-11-30 13:16:41 +0000 | [diff] [blame] | 239 | mutex_lock(&port->buf_mutex); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 240 | if (port->xmit_buf != NULL) { |
| 241 | free_page((unsigned long)port->xmit_buf); |
| 242 | port->xmit_buf = NULL; |
| 243 | } |
Alan Cox | 44e4909 | 2009-11-30 13:16:41 +0000 | [diff] [blame] | 244 | mutex_unlock(&port->buf_mutex); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 245 | } |
| 246 | EXPORT_SYMBOL(tty_port_free_xmit_buf); |
| 247 | |
Jiri Slaby | de274bf | 2012-11-15 09:49:54 +0100 | [diff] [blame] | 248 | /** |
| 249 | * tty_port_destroy -- destroy inited port |
Antonio Borneo | 1926e5d | 2017-09-08 08:59:42 +0200 | [diff] [blame] | 250 | * @port: tty port to be destroyed |
Jiri Slaby | de274bf | 2012-11-15 09:49:54 +0100 | [diff] [blame] | 251 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 252 | * When a port was initialized using tty_port_init(), one has to destroy the |
| 253 | * port by this function. Either indirectly by using &tty_port refcounting |
| 254 | * (tty_port_put()) or directly if refcounting is not used. |
Jiri Slaby | de274bf | 2012-11-15 09:49:54 +0100 | [diff] [blame] | 255 | */ |
| 256 | void tty_port_destroy(struct tty_port *port) |
| 257 | { |
Peter Hurley | e176058 | 2015-10-17 16:36:23 -0400 | [diff] [blame] | 258 | tty_buffer_cancel_work(port); |
Jiri Slaby | de274bf | 2012-11-15 09:49:54 +0100 | [diff] [blame] | 259 | tty_buffer_free_all(port); |
| 260 | } |
| 261 | EXPORT_SYMBOL(tty_port_destroy); |
| 262 | |
Alan Cox | 568aafc | 2009-11-30 13:17:14 +0000 | [diff] [blame] | 263 | static void tty_port_destructor(struct kref *kref) |
| 264 | { |
| 265 | struct tty_port *port = container_of(kref, struct tty_port, kref); |
Peter Hurley | e3bfea2 | 2013-09-18 20:42:39 -0400 | [diff] [blame] | 266 | |
| 267 | /* check if last port ref was dropped before tty release */ |
| 268 | if (WARN_ON(port->itty)) |
| 269 | return; |
Alan Cox | 568aafc | 2009-11-30 13:17:14 +0000 | [diff] [blame] | 270 | if (port->xmit_buf) |
| 271 | free_page((unsigned long)port->xmit_buf); |
Jiri Slaby | de274bf | 2012-11-15 09:49:54 +0100 | [diff] [blame] | 272 | tty_port_destroy(port); |
Jiri Slaby | 81c7983 | 2012-11-15 09:49:49 +0100 | [diff] [blame] | 273 | if (port->ops && port->ops->destruct) |
Alan Cox | 568aafc | 2009-11-30 13:17:14 +0000 | [diff] [blame] | 274 | port->ops->destruct(port); |
| 275 | else |
| 276 | kfree(port); |
| 277 | } |
| 278 | |
Jiri Slaby | 3be491d | 2021-11-26 09:16:06 +0100 | [diff] [blame] | 279 | /** |
| 280 | * tty_port_put -- drop a reference to tty_port |
| 281 | * @port: port to drop a reference of (can be NULL) |
| 282 | * |
| 283 | * The final put will destroy and free up the @port using |
| 284 | * @port->ops->destruct() hook, or using kfree() if not provided. |
| 285 | */ |
Alan Cox | 568aafc | 2009-11-30 13:17:14 +0000 | [diff] [blame] | 286 | void tty_port_put(struct tty_port *port) |
| 287 | { |
| 288 | if (port) |
| 289 | kref_put(&port->kref, tty_port_destructor); |
| 290 | } |
| 291 | EXPORT_SYMBOL(tty_port_put); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 292 | |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 293 | /** |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 294 | * tty_port_tty_get - get a tty reference |
| 295 | * @port: tty port |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 296 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 297 | * Return a refcount protected tty instance or %NULL if the port is not |
| 298 | * associated with a tty (eg due to close or hangup). |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 299 | */ |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 300 | struct tty_struct *tty_port_tty_get(struct tty_port *port) |
| 301 | { |
| 302 | unsigned long flags; |
| 303 | struct tty_struct *tty; |
| 304 | |
| 305 | spin_lock_irqsave(&port->lock, flags); |
| 306 | tty = tty_kref_get(port->tty); |
| 307 | spin_unlock_irqrestore(&port->lock, flags); |
| 308 | return tty; |
| 309 | } |
| 310 | EXPORT_SYMBOL(tty_port_tty_get); |
| 311 | |
| 312 | /** |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 313 | * tty_port_tty_set - set the tty of a port |
| 314 | * @port: tty port |
| 315 | * @tty: the tty |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 316 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 317 | * Associate the port and tty pair. Manages any internal refcounts. Pass %NULL |
| 318 | * to deassociate a port. |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 319 | */ |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 320 | void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty) |
| 321 | { |
| 322 | unsigned long flags; |
| 323 | |
| 324 | spin_lock_irqsave(&port->lock, flags); |
Markus Elfring | a211b1a | 2014-11-21 13:42:29 +0100 | [diff] [blame] | 325 | tty_kref_put(port->tty); |
Alan Cox | cb4bca3 | 2008-10-21 13:47:44 +0100 | [diff] [blame] | 326 | port->tty = tty_kref_get(tty); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 327 | spin_unlock_irqrestore(&port->lock, flags); |
| 328 | } |
| 329 | EXPORT_SYMBOL(tty_port_tty_set); |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 330 | |
Jiri Slaby | 3be491d | 2021-11-26 09:16:06 +0100 | [diff] [blame] | 331 | /** |
| 332 | * tty_port_shutdown - internal helper to shutdown the device |
| 333 | * @port: tty port to be shut down |
| 334 | * @tty: the associated tty |
| 335 | * |
| 336 | * It is used by tty_port_hangup() and tty_port_close(). Its task is to |
| 337 | * shutdown the device if it was initialized (note consoles remain |
| 338 | * functioning). It lowers DTR/RTS (if @tty has HUPCL set) and invokes |
| 339 | * @port->ops->shutdown(). |
| 340 | */ |
Johan Hovold | 957daca | 2013-03-07 15:55:51 +0100 | [diff] [blame] | 341 | static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty) |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 342 | { |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 343 | mutex_lock(&port->mutex); |
Johan Hovold | 8bde965 | 2013-03-07 15:55:48 +0100 | [diff] [blame] | 344 | if (port->console) |
| 345 | goto out; |
| 346 | |
Peter Hurley | d41861c | 2016-04-09 17:53:25 -0700 | [diff] [blame] | 347 | if (tty_port_initialized(port)) { |
| 348 | tty_port_set_initialized(port, 0); |
Johan Hovold | 957daca | 2013-03-07 15:55:51 +0100 | [diff] [blame] | 349 | /* |
| 350 | * Drop DTR/RTS if HUPCL is set. This causes any attached |
| 351 | * modem to hang up the line. |
| 352 | */ |
| 353 | if (tty && C_HUPCL(tty)) |
| 354 | tty_port_lower_dtr_rts(port); |
| 355 | |
Johan Hovold | 0d3cb6f | 2019-04-03 09:40:53 +0200 | [diff] [blame] | 356 | if (port->ops->shutdown) |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 357 | port->ops->shutdown(port); |
Johan Hovold | 8bde965 | 2013-03-07 15:55:48 +0100 | [diff] [blame] | 358 | } |
| 359 | out: |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 360 | mutex_unlock(&port->mutex); |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 363 | /** |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 364 | * tty_port_hangup - hangup helper |
| 365 | * @port: tty port |
Alan Cox | 3e61696 | 2009-01-02 13:45:26 +0000 | [diff] [blame] | 366 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 367 | * Perform port level tty hangup flag and count changes. Drop the tty |
| 368 | * reference. |
Peter Hurley | 9c9928b | 2014-06-16 09:17:02 -0400 | [diff] [blame] | 369 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 370 | * Caller holds tty lock. |
Alan Cox | 3e61696 | 2009-01-02 13:45:26 +0000 | [diff] [blame] | 371 | */ |
Alan Cox | 3e61696 | 2009-01-02 13:45:26 +0000 | [diff] [blame] | 372 | void tty_port_hangup(struct tty_port *port) |
| 373 | { |
Johan Hovold | 957daca | 2013-03-07 15:55:51 +0100 | [diff] [blame] | 374 | struct tty_struct *tty; |
Alan Cox | 3e61696 | 2009-01-02 13:45:26 +0000 | [diff] [blame] | 375 | unsigned long flags; |
| 376 | |
| 377 | spin_lock_irqsave(&port->lock, flags); |
| 378 | port->count = 0; |
Johan Hovold | 957daca | 2013-03-07 15:55:51 +0100 | [diff] [blame] | 379 | tty = port->tty; |
| 380 | if (tty) |
| 381 | set_bit(TTY_IO_ERROR, &tty->flags); |
Alan Cox | 3e61696 | 2009-01-02 13:45:26 +0000 | [diff] [blame] | 382 | port->tty = NULL; |
| 383 | spin_unlock_irqrestore(&port->lock, flags); |
Peter Hurley | 807c8d81 | 2016-04-09 17:53:22 -0700 | [diff] [blame] | 384 | tty_port_set_active(port, 0); |
Johan Hovold | 957daca | 2013-03-07 15:55:51 +0100 | [diff] [blame] | 385 | tty_port_shutdown(port, tty); |
| 386 | tty_kref_put(tty); |
Alan Cox | 3e61696 | 2009-01-02 13:45:26 +0000 | [diff] [blame] | 387 | wake_up_interruptible(&port->open_wait); |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 388 | wake_up_interruptible(&port->delta_msr_wait); |
Alan Cox | 3e61696 | 2009-01-02 13:45:26 +0000 | [diff] [blame] | 389 | } |
| 390 | EXPORT_SYMBOL(tty_port_hangup); |
| 391 | |
| 392 | /** |
Jiri Slaby | aa27a09 | 2013-03-07 13:12:30 +0100 | [diff] [blame] | 393 | * tty_port_tty_hangup - helper to hang up a tty |
Jiri Slaby | aa27a09 | 2013-03-07 13:12:30 +0100 | [diff] [blame] | 394 | * @port: tty port |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 395 | * @check_clocal: hang only ttys with %CLOCAL unset? |
Jiri Slaby | aa27a09 | 2013-03-07 13:12:30 +0100 | [diff] [blame] | 396 | */ |
| 397 | void tty_port_tty_hangup(struct tty_port *port, bool check_clocal) |
| 398 | { |
| 399 | struct tty_struct *tty = tty_port_tty_get(port); |
| 400 | |
Gianluca Anzolin | 1d9e689 | 2013-07-25 07:26:16 +0200 | [diff] [blame] | 401 | if (tty && (!check_clocal || !C_CLOCAL(tty))) |
Jiri Slaby | aa27a09 | 2013-03-07 13:12:30 +0100 | [diff] [blame] | 402 | tty_hangup(tty); |
Gianluca Anzolin | 1d9e689 | 2013-07-25 07:26:16 +0200 | [diff] [blame] | 403 | tty_kref_put(tty); |
Jiri Slaby | aa27a09 | 2013-03-07 13:12:30 +0100 | [diff] [blame] | 404 | } |
| 405 | EXPORT_SYMBOL_GPL(tty_port_tty_hangup); |
| 406 | |
| 407 | /** |
Jiri Slaby | 6aad04f | 2013-03-07 13:12:29 +0100 | [diff] [blame] | 408 | * tty_port_tty_wakeup - helper to wake up a tty |
Jiri Slaby | 6aad04f | 2013-03-07 13:12:29 +0100 | [diff] [blame] | 409 | * @port: tty port |
| 410 | */ |
| 411 | void tty_port_tty_wakeup(struct tty_port *port) |
| 412 | { |
Rob Herring | c3485ee | 2017-02-02 13:48:05 -0600 | [diff] [blame] | 413 | port->client_ops->write_wakeup(port); |
Jiri Slaby | 6aad04f | 2013-03-07 13:12:29 +0100 | [diff] [blame] | 414 | } |
| 415 | EXPORT_SYMBOL_GPL(tty_port_tty_wakeup); |
| 416 | |
| 417 | /** |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 418 | * tty_port_carrier_raised - carrier raised check |
| 419 | * @port: tty port |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 420 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 421 | * Wrapper for the carrier detect logic. For the moment this is used |
| 422 | * to hide some internal details. This will eventually become entirely |
| 423 | * internal to the tty port. |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 424 | */ |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 425 | int tty_port_carrier_raised(struct tty_port *port) |
| 426 | { |
Johan Hovold | 0d3cb6f | 2019-04-03 09:40:53 +0200 | [diff] [blame] | 427 | if (port->ops->carrier_raised == NULL) |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 428 | return 1; |
| 429 | return port->ops->carrier_raised(port); |
| 430 | } |
| 431 | EXPORT_SYMBOL(tty_port_carrier_raised); |
Alan Cox | 5d951fb | 2009-01-02 13:45:19 +0000 | [diff] [blame] | 432 | |
| 433 | /** |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 434 | * tty_port_raise_dtr_rts - Raise DTR/RTS |
| 435 | * @port: tty port |
Alan Cox | 5d951fb | 2009-01-02 13:45:19 +0000 | [diff] [blame] | 436 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 437 | * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide |
| 438 | * some internal details. This will eventually become entirely internal to the |
| 439 | * tty port. |
Alan Cox | 5d951fb | 2009-01-02 13:45:19 +0000 | [diff] [blame] | 440 | */ |
Alan Cox | 5d951fb | 2009-01-02 13:45:19 +0000 | [diff] [blame] | 441 | void tty_port_raise_dtr_rts(struct tty_port *port) |
| 442 | { |
Johan Hovold | 0d3cb6f | 2019-04-03 09:40:53 +0200 | [diff] [blame] | 443 | if (port->ops->dtr_rts) |
Alan Cox | fcc8ac1 | 2009-06-11 12:24:17 +0100 | [diff] [blame] | 444 | port->ops->dtr_rts(port, 1); |
Alan Cox | 5d951fb | 2009-01-02 13:45:19 +0000 | [diff] [blame] | 445 | } |
| 446 | EXPORT_SYMBOL(tty_port_raise_dtr_rts); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 447 | |
| 448 | /** |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 449 | * tty_port_lower_dtr_rts - Lower DTR/RTS |
| 450 | * @port: tty port |
Alan Cox | fcc8ac1 | 2009-06-11 12:24:17 +0100 | [diff] [blame] | 451 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 452 | * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide |
| 453 | * some internal details. This will eventually become entirely internal to the |
| 454 | * tty port. |
Alan Cox | fcc8ac1 | 2009-06-11 12:24:17 +0100 | [diff] [blame] | 455 | */ |
Alan Cox | fcc8ac1 | 2009-06-11 12:24:17 +0100 | [diff] [blame] | 456 | void tty_port_lower_dtr_rts(struct tty_port *port) |
| 457 | { |
Johan Hovold | 0d3cb6f | 2019-04-03 09:40:53 +0200 | [diff] [blame] | 458 | if (port->ops->dtr_rts) |
Alan Cox | fcc8ac1 | 2009-06-11 12:24:17 +0100 | [diff] [blame] | 459 | port->ops->dtr_rts(port, 0); |
| 460 | } |
| 461 | EXPORT_SYMBOL(tty_port_lower_dtr_rts); |
| 462 | |
| 463 | /** |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 464 | * tty_port_block_til_ready - Waiting logic for tty open |
| 465 | * @port: the tty port being opened |
| 466 | * @tty: the tty device being bound |
| 467 | * @filp: the file pointer of the opener or %NULL |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 468 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 469 | * Implement the core POSIX/SuS tty behaviour when opening a tty device. |
| 470 | * Handles: |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 471 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 472 | * - hangup (both before and during) |
| 473 | * - non blocking open |
| 474 | * - rts/dtr/dcd |
| 475 | * - signals |
| 476 | * - port flags and counts |
Peter Hurley | c590f6b | 2014-06-16 09:17:01 -0400 | [diff] [blame] | 477 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 478 | * The passed @port must implement the @port->ops->carrier_raised method if it |
| 479 | * can do carrier detect and the @port->ops->dtr_rts method if it supports |
| 480 | * software management of these lines. Note that the dtr/rts raise is done each |
| 481 | * iteration as a hangup may have previously dropped them while we wait. |
Peter Hurley | c590f6b | 2014-06-16 09:17:01 -0400 | [diff] [blame] | 482 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 483 | * Caller holds tty lock. |
| 484 | * |
| 485 | * Note: May drop and reacquire tty lock when blocking, so @tty and @port may |
| 486 | * have changed state (eg., may have been hung up). |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 487 | */ |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 488 | int tty_port_block_til_ready(struct tty_port *port, |
| 489 | struct tty_struct *tty, struct file *filp) |
| 490 | { |
| 491 | int do_clocal = 0, retval; |
| 492 | unsigned long flags; |
Jiri Slaby | 6af9a43 | 2009-06-24 18:35:05 +0100 | [diff] [blame] | 493 | DEFINE_WAIT(wait); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 494 | |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 495 | /* if non-blocking mode is set we can pass directly to open unless |
Xiaofei Tan | 1df9264 | 2021-05-12 17:26:25 +0800 | [diff] [blame] | 496 | * the port has just hung up or is in another error state. |
| 497 | */ |
Peter Hurley | 18900ca | 2016-04-09 17:06:48 -0700 | [diff] [blame] | 498 | if (tty_io_error(tty)) { |
Peter Hurley | 807c8d81 | 2016-04-09 17:53:22 -0700 | [diff] [blame] | 499 | tty_port_set_active(port, 1); |
Alan Cox | 8627b96 | 2009-11-18 14:12:58 +0000 | [diff] [blame] | 500 | return 0; |
| 501 | } |
Alan Cox | ed3f0af | 2017-01-16 16:54:29 -0600 | [diff] [blame] | 502 | if (filp == NULL || (filp->f_flags & O_NONBLOCK)) { |
Alan Cox | 4175f3e | 2009-10-28 21:12:32 +0100 | [diff] [blame] | 503 | /* Indicate we are open */ |
Peter Hurley | 9db276f | 2016-01-10 20:36:15 -0800 | [diff] [blame] | 504 | if (C_BAUD(tty)) |
Alan Cox | 4175f3e | 2009-10-28 21:12:32 +0100 | [diff] [blame] | 505 | tty_port_raise_dtr_rts(port); |
Peter Hurley | 807c8d81 | 2016-04-09 17:53:22 -0700 | [diff] [blame] | 506 | tty_port_set_active(port, 1); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | if (C_CLOCAL(tty)) |
| 511 | do_clocal = 1; |
| 512 | |
| 513 | /* Block waiting until we can proceed. We may need to wait for the |
Xiaofei Tan | 1df9264 | 2021-05-12 17:26:25 +0800 | [diff] [blame] | 514 | * carrier, but we must also wait for any close that is in progress |
| 515 | * before the next open may complete. |
| 516 | */ |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 517 | |
| 518 | retval = 0; |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 519 | |
| 520 | /* The port lock protects the port counts */ |
| 521 | spin_lock_irqsave(&port->lock, flags); |
Peter Hurley | e359a4e | 2014-06-16 09:17:06 -0400 | [diff] [blame] | 522 | port->count--; |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 523 | port->blocked_open++; |
| 524 | spin_unlock_irqrestore(&port->lock, flags); |
| 525 | |
| 526 | while (1) { |
| 527 | /* Indicate we are open */ |
Peter Hurley | d41861c | 2016-04-09 17:53:25 -0700 | [diff] [blame] | 528 | if (C_BAUD(tty) && tty_port_initialized(port)) |
Alan Cox | 7834909 | 2009-01-02 13:46:43 +0000 | [diff] [blame] | 529 | tty_port_raise_dtr_rts(port); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 530 | |
Jiri Slaby | 3e3b5c0 | 2009-06-11 14:33:37 +0100 | [diff] [blame] | 531 | prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE); |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 532 | /* Check for a hangup or uninitialised port. |
Xiaofei Tan | 1df9264 | 2021-05-12 17:26:25 +0800 | [diff] [blame] | 533 | * Return accordingly. |
| 534 | */ |
Peter Hurley | d41861c | 2016-04-09 17:53:25 -0700 | [diff] [blame] | 535 | if (tty_hung_up_p(filp) || !tty_port_initialized(port)) { |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 536 | if (port->flags & ASYNC_HUP_NOTIFY) |
| 537 | retval = -EAGAIN; |
| 538 | else |
| 539 | retval = -ERESTARTSYS; |
| 540 | break; |
| 541 | } |
Jiri Slaby | 0eee50a | 2012-01-12 22:55:15 +0100 | [diff] [blame] | 542 | /* |
| 543 | * Probe the carrier. For devices with no carrier detect |
| 544 | * tty_port_carrier_raised will always return true. |
| 545 | * Never ask drivers if CLOCAL is set, this causes troubles |
| 546 | * on some hardware. |
| 547 | */ |
Peter Hurley | fef062c | 2015-10-10 16:00:52 -0400 | [diff] [blame] | 548 | if (do_clocal || tty_port_carrier_raised(port)) |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 549 | break; |
| 550 | if (signal_pending(current)) { |
| 551 | retval = -ERESTARTSYS; |
| 552 | break; |
| 553 | } |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 554 | tty_unlock(tty); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 555 | schedule(); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 556 | tty_lock(tty); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 557 | } |
Jiri Slaby | 3e3b5c0 | 2009-06-11 14:33:37 +0100 | [diff] [blame] | 558 | finish_wait(&port->open_wait, &wait); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 559 | |
| 560 | /* Update counts. A parallel hangup will have set count to zero and |
Xiaofei Tan | 1df9264 | 2021-05-12 17:26:25 +0800 | [diff] [blame] | 561 | * we must not mess that up further. |
| 562 | */ |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 563 | spin_lock_irqsave(&port->lock, flags); |
| 564 | if (!tty_hung_up_p(filp)) |
| 565 | port->count++; |
| 566 | port->blocked_open--; |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 567 | spin_unlock_irqrestore(&port->lock, flags); |
Peter Hurley | 807c8d81 | 2016-04-09 17:53:22 -0700 | [diff] [blame] | 568 | if (retval == 0) |
| 569 | tty_port_set_active(port, 1); |
Alan Cox | ecc2e05 | 2009-07-17 16:17:26 +0100 | [diff] [blame] | 570 | return retval; |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 571 | } |
| 572 | EXPORT_SYMBOL(tty_port_block_til_ready); |
| 573 | |
Johan Hovold | b74414f | 2013-03-07 15:55:52 +0100 | [diff] [blame] | 574 | static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty) |
| 575 | { |
| 576 | unsigned int bps = tty_get_baud_rate(tty); |
| 577 | long timeout; |
| 578 | |
| 579 | if (bps > 1200) { |
| 580 | timeout = (HZ * 10 * port->drain_delay) / bps; |
| 581 | timeout = max_t(long, timeout, HZ / 10); |
| 582 | } else { |
| 583 | timeout = 2 * HZ; |
| 584 | } |
| 585 | schedule_timeout_interruptible(timeout); |
| 586 | } |
| 587 | |
Jiri Slaby | 3be491d | 2021-11-26 09:16:06 +0100 | [diff] [blame] | 588 | /** |
| 589 | * tty_port_close_start - helper for tty->ops->close, part 1/2 |
| 590 | * @port: tty_port of the device |
| 591 | * @tty: tty being closed |
| 592 | * @filp: passed file pointer |
| 593 | * |
| 594 | * Decrements and checks open count. Flushes the port if this is the last |
| 595 | * close. That means, dropping the data from the outpu buffer on the device and |
| 596 | * waiting for sending logic to finish. The rest of close handling is performed |
| 597 | * in tty_port_close_end(). |
| 598 | * |
| 599 | * Locking: Caller holds tty lock. |
| 600 | * |
| 601 | * Return: 1 if this is the last close, otherwise 0 |
| 602 | */ |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 603 | int tty_port_close_start(struct tty_port *port, |
| 604 | struct tty_struct *tty, struct file *filp) |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 605 | { |
| 606 | unsigned long flags; |
| 607 | |
Peter Hurley | 633caba | 2014-11-05 12:40:03 -0500 | [diff] [blame] | 608 | if (tty_hung_up_p(filp)) |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 609 | return 0; |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 610 | |
Peter Hurley | 633caba | 2014-11-05 12:40:03 -0500 | [diff] [blame] | 611 | spin_lock_irqsave(&port->lock, flags); |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 612 | if (tty->count == 1 && port->count != 1) { |
Peter Hurley | 339f36b | 2015-11-08 13:01:13 -0500 | [diff] [blame] | 613 | tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__, |
| 614 | port->count); |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 615 | port->count = 1; |
| 616 | } |
| 617 | if (--port->count < 0) { |
Peter Hurley | 339f36b | 2015-11-08 13:01:13 -0500 | [diff] [blame] | 618 | tty_warn(tty, "%s: bad port count (%d)\n", __func__, |
| 619 | port->count); |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 620 | port->count = 0; |
| 621 | } |
| 622 | |
| 623 | if (port->count) { |
| 624 | spin_unlock_irqrestore(&port->lock, flags); |
| 625 | return 0; |
| 626 | } |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 627 | spin_unlock_irqrestore(&port->lock, flags); |
Johan Hovold | 0b2588c | 2013-03-07 15:55:53 +0100 | [diff] [blame] | 628 | |
Peter Hurley | ddc7b75 | 2014-06-16 09:17:03 -0400 | [diff] [blame] | 629 | tty->closing = 1; |
| 630 | |
Peter Hurley | d41861c | 2016-04-09 17:53:25 -0700 | [diff] [blame] | 631 | if (tty_port_initialized(port)) { |
Johan Hovold | 0b2588c | 2013-03-07 15:55:53 +0100 | [diff] [blame] | 632 | /* Don't block on a stalled port, just pull the chain */ |
Jiri Slaby | 6e94dbc | 2021-05-05 11:19:05 +0200 | [diff] [blame] | 633 | if (tty->flow.tco_stopped) |
Johan Hovold | 0b2588c | 2013-03-07 15:55:53 +0100 | [diff] [blame] | 634 | tty_driver_flush_buffer(tty); |
| 635 | if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE) |
Peter Hurley | 79c1faa | 2015-10-10 16:00:51 -0400 | [diff] [blame] | 636 | tty_wait_until_sent(tty, port->closing_wait); |
Johan Hovold | 0b2588c | 2013-03-07 15:55:53 +0100 | [diff] [blame] | 637 | if (port->drain_delay) |
| 638 | tty_port_drain_delay(port, tty); |
| 639 | } |
Alan Cox | e707c35 | 2009-11-05 13:27:57 +0000 | [diff] [blame] | 640 | /* Flush the ldisc buffering */ |
| 641 | tty_ldisc_flush(tty); |
| 642 | |
Peter Hurley | 469d6d0 | 2013-09-18 20:47:06 -0400 | [diff] [blame] | 643 | /* Report to caller this is the last port reference */ |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 644 | return 1; |
| 645 | } |
| 646 | EXPORT_SYMBOL(tty_port_close_start); |
| 647 | |
Jiri Slaby | 3be491d | 2021-11-26 09:16:06 +0100 | [diff] [blame] | 648 | /** |
| 649 | * tty_port_close_end - helper for tty->ops->close, part 2/2 |
| 650 | * @port: tty_port of the device |
| 651 | * @tty: tty being closed |
| 652 | * |
| 653 | * This is a continuation of the first part: tty_port_close_start(). This |
| 654 | * should be called after turning off the device. It flushes the data from the |
| 655 | * line discipline and delays the close by @port->close_delay. |
| 656 | * |
| 657 | * Locking: Caller holds tty lock. |
| 658 | */ |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 659 | void tty_port_close_end(struct tty_port *port, struct tty_struct *tty) |
| 660 | { |
| 661 | unsigned long flags; |
| 662 | |
Peter Hurley | 3f40f5b | 2014-11-05 12:40:05 -0500 | [diff] [blame] | 663 | tty_ldisc_flush(tty); |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 664 | tty->closing = 0; |
| 665 | |
Peter Hurley | ddc7b75 | 2014-06-16 09:17:03 -0400 | [diff] [blame] | 666 | spin_lock_irqsave(&port->lock, flags); |
| 667 | |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 668 | if (port->blocked_open) { |
| 669 | spin_unlock_irqrestore(&port->lock, flags); |
Peter Hurley | 5823323e | 2016-01-10 20:36:14 -0800 | [diff] [blame] | 670 | if (port->close_delay) |
| 671 | msleep_interruptible(jiffies_to_msecs(port->close_delay)); |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 672 | spin_lock_irqsave(&port->lock, flags); |
| 673 | wake_up_interruptible(&port->open_wait); |
| 674 | } |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 675 | spin_unlock_irqrestore(&port->lock, flags); |
Peter Hurley | 807c8d81 | 2016-04-09 17:53:22 -0700 | [diff] [blame] | 676 | tty_port_set_active(port, 0); |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 677 | } |
| 678 | EXPORT_SYMBOL(tty_port_close_end); |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 679 | |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 680 | /** |
| 681 | * tty_port_close - generic tty->ops->close handler |
| 682 | * @port: tty_port of the device |
| 683 | * @tty: tty being closed |
| 684 | * @filp: passed file pointer |
Peter Hurley | 0733db91 | 2014-06-16 09:16:59 -0400 | [diff] [blame] | 685 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 686 | * It is a generic helper to be used in driver's @tty->ops->close. It wraps a |
| 687 | * sequence of tty_port_close_start(), tty_port_shutdown(), and |
| 688 | * tty_port_close_end(). The latter two are called only if this is the last |
| 689 | * close. See the respective functions for the details. |
| 690 | * |
| 691 | * Locking: Caller holds tty lock |
Peter Hurley | 0733db91 | 2014-06-16 09:16:59 -0400 | [diff] [blame] | 692 | */ |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 693 | void tty_port_close(struct tty_port *port, struct tty_struct *tty, |
| 694 | struct file *filp) |
| 695 | { |
| 696 | if (tty_port_close_start(port, tty, filp) == 0) |
| 697 | return; |
Johan Hovold | 957daca | 2013-03-07 15:55:51 +0100 | [diff] [blame] | 698 | tty_port_shutdown(port, tty); |
Chanho Park | 2a48602 | 2018-11-22 18:23:47 +0900 | [diff] [blame] | 699 | if (!port->console) |
| 700 | set_bit(TTY_IO_ERROR, &tty->flags); |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 701 | tty_port_close_end(port, tty); |
| 702 | tty_port_tty_set(port, NULL); |
| 703 | } |
| 704 | EXPORT_SYMBOL(tty_port_close); |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 705 | |
Jiri Slaby | 72a33bf | 2012-08-07 21:47:49 +0200 | [diff] [blame] | 706 | /** |
| 707 | * tty_port_install - generic tty->ops->install handler |
| 708 | * @port: tty_port of the device |
| 709 | * @driver: tty_driver for this device |
| 710 | * @tty: tty to be installed |
| 711 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 712 | * It is the same as tty_standard_install() except the provided @port is linked |
| 713 | * to a concrete tty specified by @tty. Use this or tty_port_register_device() |
| 714 | * (or both). Call tty_port_link_device() as a last resort. |
Jiri Slaby | 72a33bf | 2012-08-07 21:47:49 +0200 | [diff] [blame] | 715 | */ |
Jiri Slaby | 695586c | 2012-06-04 13:35:32 +0200 | [diff] [blame] | 716 | int tty_port_install(struct tty_port *port, struct tty_driver *driver, |
| 717 | struct tty_struct *tty) |
| 718 | { |
| 719 | tty->port = port; |
| 720 | return tty_standard_install(driver, tty); |
| 721 | } |
| 722 | EXPORT_SYMBOL_GPL(tty_port_install); |
| 723 | |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 724 | /** |
| 725 | * tty_port_open - generic tty->ops->open handler |
| 726 | * @port: tty_port of the device |
| 727 | * @tty: tty to be opened |
| 728 | * @filp: passed file pointer |
Peter Hurley | addd467 | 2014-06-16 09:17:00 -0400 | [diff] [blame] | 729 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 730 | * It is a generic helper to be used in driver's @tty->ops->open. It activates |
| 731 | * the devices using @port->ops->activate if not active already. And waits for |
| 732 | * the device to be ready using tty_port_block_til_ready() (e.g. raises |
| 733 | * DTR/CTS and waits for carrier). |
Peter Hurley | addd467 | 2014-06-16 09:17:00 -0400 | [diff] [blame] | 734 | * |
Jiri Slaby | cb6f6f9 | 2021-11-26 09:15:59 +0100 | [diff] [blame] | 735 | * Locking: Caller holds tty lock. |
| 736 | * |
| 737 | * Note: may drop and reacquire tty lock (in tty_port_block_til_ready()) so |
| 738 | * @tty and @port may have changed state (eg., may be hung up now). |
Peter Hurley | addd467 | 2014-06-16 09:17:00 -0400 | [diff] [blame] | 739 | */ |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 740 | int tty_port_open(struct tty_port *port, struct tty_struct *tty, |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 741 | struct file *filp) |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 742 | { |
| 743 | spin_lock_irq(&port->lock); |
Peter Hurley | e359a4e | 2014-06-16 09:17:06 -0400 | [diff] [blame] | 744 | ++port->count; |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 745 | spin_unlock_irq(&port->lock); |
| 746 | tty_port_tty_set(port, tty); |
| 747 | |
| 748 | /* |
| 749 | * Do the device-specific open only if the hardware isn't |
| 750 | * already initialized. Serialize open and shutdown using the |
| 751 | * port mutex. |
| 752 | */ |
| 753 | |
| 754 | mutex_lock(&port->mutex); |
| 755 | |
Peter Hurley | d41861c | 2016-04-09 17:53:25 -0700 | [diff] [blame] | 756 | if (!tty_port_initialized(port)) { |
Alan Cox | a9a37ec | 2009-11-30 13:16:57 +0000 | [diff] [blame] | 757 | clear_bit(TTY_IO_ERROR, &tty->flags); |
Johan Hovold | 0d3cb6f | 2019-04-03 09:40:53 +0200 | [diff] [blame] | 758 | if (port->ops->activate) { |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 759 | int retval = port->ops->activate(port, tty); |
Xiaofei Tan | 54ad59a | 2021-05-12 17:26:24 +0800 | [diff] [blame] | 760 | |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 761 | if (retval) { |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 762 | mutex_unlock(&port->mutex); |
| 763 | return retval; |
| 764 | } |
| 765 | } |
Peter Hurley | d41861c | 2016-04-09 17:53:25 -0700 | [diff] [blame] | 766 | tty_port_set_initialized(port, 1); |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 767 | } |
| 768 | mutex_unlock(&port->mutex); |
| 769 | return tty_port_block_til_ready(port, tty, filp); |
| 770 | } |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 771 | EXPORT_SYMBOL(tty_port_open); |