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