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> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/wait.h> |
| 17 | #include <linux/bitops.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/module.h> |
| 20 | |
| 21 | void tty_port_init(struct tty_port *port) |
| 22 | { |
| 23 | memset(port, 0, sizeof(*port)); |
Jiri Slaby | ecbbfd4 | 2012-10-18 22:26:47 +0200 | [diff] [blame] | 24 | tty_buffer_init(port); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 25 | init_waitqueue_head(&port->open_wait); |
| 26 | init_waitqueue_head(&port->close_wait); |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 27 | init_waitqueue_head(&port->delta_msr_wait); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 28 | mutex_init(&port->mutex); |
Alan Cox | 44e4909 | 2009-11-30 13:16:41 +0000 | [diff] [blame] | 29 | mutex_init(&port->buf_mutex); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 30 | spin_lock_init(&port->lock); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 31 | port->close_delay = (50 * HZ) / 100; |
| 32 | port->closing_wait = (3000 * HZ) / 100; |
Alan Cox | 568aafc | 2009-11-30 13:17:14 +0000 | [diff] [blame] | 33 | kref_init(&port->kref); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 34 | } |
| 35 | EXPORT_SYMBOL(tty_port_init); |
| 36 | |
Jiri Slaby | 72a33bf | 2012-08-07 21:47:49 +0200 | [diff] [blame] | 37 | /** |
Jiri Slaby | 2cb4ca0 | 2012-08-07 21:47:50 +0200 | [diff] [blame] | 38 | * tty_port_link_device - link tty and tty_port |
| 39 | * @port: tty_port of the device |
| 40 | * @driver: tty_driver for this device |
| 41 | * @index: index of the tty |
| 42 | * |
| 43 | * Provide the tty layer wit ha link from a tty (specified by @index) to a |
| 44 | * tty_port (@port). Use this only if neither tty_port_register_device nor |
| 45 | * tty_port_install is used in the driver. If used, this has to be called before |
| 46 | * tty_register_driver. |
| 47 | */ |
| 48 | void tty_port_link_device(struct tty_port *port, |
| 49 | struct tty_driver *driver, unsigned index) |
| 50 | { |
| 51 | if (WARN_ON(index >= driver->num)) |
| 52 | return; |
| 53 | driver->ports[index] = port; |
| 54 | } |
| 55 | EXPORT_SYMBOL_GPL(tty_port_link_device); |
| 56 | |
| 57 | /** |
Jiri Slaby | 72a33bf | 2012-08-07 21:47:49 +0200 | [diff] [blame] | 58 | * tty_port_register_device - register tty device |
| 59 | * @port: tty_port of the device |
| 60 | * @driver: tty_driver for this device |
| 61 | * @index: index of the tty |
| 62 | * @device: parent if exists, otherwise NULL |
| 63 | * |
| 64 | * It is the same as tty_register_device except the provided @port is linked to |
| 65 | * a concrete tty specified by @index. Use this or tty_port_install (or both). |
| 66 | * Call tty_port_link_device as a last resort. |
| 67 | */ |
Jiri Slaby | 057eb85 | 2012-06-04 13:35:37 +0200 | [diff] [blame] | 68 | struct device *tty_port_register_device(struct tty_port *port, |
| 69 | struct tty_driver *driver, unsigned index, |
| 70 | struct device *device) |
| 71 | { |
Jiri Slaby | 2cb4ca0 | 2012-08-07 21:47:50 +0200 | [diff] [blame] | 72 | tty_port_link_device(port, driver, index); |
Jiri Slaby | 057eb85 | 2012-06-04 13:35:37 +0200 | [diff] [blame] | 73 | return tty_register_device(driver, index, device); |
| 74 | } |
| 75 | EXPORT_SYMBOL_GPL(tty_port_register_device); |
| 76 | |
Tomas Hlavacek | b1b7991 | 2012-09-06 23:17:47 +0200 | [diff] [blame] | 77 | /** |
| 78 | * tty_port_register_device_attr - register tty device |
| 79 | * @port: tty_port of the device |
| 80 | * @driver: tty_driver for this device |
| 81 | * @index: index of the tty |
| 82 | * @device: parent if exists, otherwise NULL |
| 83 | * @drvdata: Driver data to be set to device. |
| 84 | * @attr_grp: Attribute group to be set on device. |
| 85 | * |
| 86 | * It is the same as tty_register_device_attr except the provided @port is |
| 87 | * linked to a concrete tty specified by @index. Use this or tty_port_install |
| 88 | * (or both). Call tty_port_link_device as a last resort. |
| 89 | */ |
| 90 | struct device *tty_port_register_device_attr(struct tty_port *port, |
| 91 | struct tty_driver *driver, unsigned index, |
| 92 | struct device *device, void *drvdata, |
| 93 | const struct attribute_group **attr_grp) |
| 94 | { |
| 95 | tty_port_link_device(port, driver, index); |
| 96 | return tty_register_device_attr(driver, index, device, drvdata, |
| 97 | attr_grp); |
| 98 | } |
| 99 | EXPORT_SYMBOL_GPL(tty_port_register_device_attr); |
| 100 | |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 101 | int tty_port_alloc_xmit_buf(struct tty_port *port) |
| 102 | { |
| 103 | /* We may sleep in get_zeroed_page() */ |
Alan Cox | 44e4909 | 2009-11-30 13:16:41 +0000 | [diff] [blame] | 104 | mutex_lock(&port->buf_mutex); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 105 | if (port->xmit_buf == NULL) |
| 106 | port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL); |
Alan Cox | 44e4909 | 2009-11-30 13:16:41 +0000 | [diff] [blame] | 107 | mutex_unlock(&port->buf_mutex); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 108 | if (port->xmit_buf == NULL) |
| 109 | return -ENOMEM; |
| 110 | return 0; |
| 111 | } |
| 112 | EXPORT_SYMBOL(tty_port_alloc_xmit_buf); |
| 113 | |
| 114 | void tty_port_free_xmit_buf(struct tty_port *port) |
| 115 | { |
Alan Cox | 44e4909 | 2009-11-30 13:16:41 +0000 | [diff] [blame] | 116 | mutex_lock(&port->buf_mutex); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 117 | if (port->xmit_buf != NULL) { |
| 118 | free_page((unsigned long)port->xmit_buf); |
| 119 | port->xmit_buf = NULL; |
| 120 | } |
Alan Cox | 44e4909 | 2009-11-30 13:16:41 +0000 | [diff] [blame] | 121 | mutex_unlock(&port->buf_mutex); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 122 | } |
| 123 | EXPORT_SYMBOL(tty_port_free_xmit_buf); |
| 124 | |
Jiri Slaby | de274bf | 2012-11-15 09:49:54 +0100 | [diff] [blame] | 125 | /** |
| 126 | * tty_port_destroy -- destroy inited port |
| 127 | * @port: tty port to be doestroyed |
| 128 | * |
| 129 | * When a port was initialized using tty_port_init, one has to destroy the |
| 130 | * port by this function. Either indirectly by using tty_port refcounting |
| 131 | * (tty_port_put) or directly if refcounting is not used. |
| 132 | */ |
| 133 | void tty_port_destroy(struct tty_port *port) |
| 134 | { |
| 135 | tty_buffer_free_all(port); |
| 136 | } |
| 137 | EXPORT_SYMBOL(tty_port_destroy); |
| 138 | |
Alan Cox | 568aafc | 2009-11-30 13:17:14 +0000 | [diff] [blame] | 139 | static void tty_port_destructor(struct kref *kref) |
| 140 | { |
| 141 | struct tty_port *port = container_of(kref, struct tty_port, kref); |
| 142 | if (port->xmit_buf) |
| 143 | free_page((unsigned long)port->xmit_buf); |
Jiri Slaby | de274bf | 2012-11-15 09:49:54 +0100 | [diff] [blame] | 144 | tty_port_destroy(port); |
Jiri Slaby | 81c7983 | 2012-11-15 09:49:49 +0100 | [diff] [blame] | 145 | if (port->ops && port->ops->destruct) |
Alan Cox | 568aafc | 2009-11-30 13:17:14 +0000 | [diff] [blame] | 146 | port->ops->destruct(port); |
| 147 | else |
| 148 | kfree(port); |
| 149 | } |
| 150 | |
| 151 | void tty_port_put(struct tty_port *port) |
| 152 | { |
| 153 | if (port) |
| 154 | kref_put(&port->kref, tty_port_destructor); |
| 155 | } |
| 156 | EXPORT_SYMBOL(tty_port_put); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 157 | |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 158 | /** |
| 159 | * tty_port_tty_get - get a tty reference |
| 160 | * @port: tty port |
| 161 | * |
| 162 | * Return a refcount protected tty instance or NULL if the port is not |
| 163 | * associated with a tty (eg due to close or hangup) |
| 164 | */ |
| 165 | |
| 166 | struct tty_struct *tty_port_tty_get(struct tty_port *port) |
| 167 | { |
| 168 | unsigned long flags; |
| 169 | struct tty_struct *tty; |
| 170 | |
| 171 | spin_lock_irqsave(&port->lock, flags); |
| 172 | tty = tty_kref_get(port->tty); |
| 173 | spin_unlock_irqrestore(&port->lock, flags); |
| 174 | return tty; |
| 175 | } |
| 176 | EXPORT_SYMBOL(tty_port_tty_get); |
| 177 | |
| 178 | /** |
| 179 | * tty_port_tty_set - set the tty of a port |
| 180 | * @port: tty port |
| 181 | * @tty: the tty |
| 182 | * |
| 183 | * Associate the port and tty pair. Manages any internal refcounts. |
| 184 | * Pass NULL to deassociate a port |
| 185 | */ |
| 186 | |
| 187 | void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty) |
| 188 | { |
| 189 | unsigned long flags; |
| 190 | |
| 191 | spin_lock_irqsave(&port->lock, flags); |
| 192 | if (port->tty) |
| 193 | tty_kref_put(port->tty); |
Alan Cox | cb4bca3 | 2008-10-21 13:47:44 +0100 | [diff] [blame] | 194 | port->tty = tty_kref_get(tty); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 195 | spin_unlock_irqrestore(&port->lock, flags); |
| 196 | } |
| 197 | EXPORT_SYMBOL(tty_port_tty_set); |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 198 | |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 199 | static void tty_port_shutdown(struct tty_port *port) |
| 200 | { |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 201 | mutex_lock(&port->mutex); |
Jason Wessel | 336cee4 | 2010-03-08 21:50:11 -0600 | [diff] [blame] | 202 | if (port->ops->shutdown && !port->console && |
Alan Stern | 1f5c13f | 2009-08-20 15:23:47 -0400 | [diff] [blame] | 203 | test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 204 | port->ops->shutdown(port); |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 205 | mutex_unlock(&port->mutex); |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 208 | /** |
Alan Cox | 3e61696 | 2009-01-02 13:45:26 +0000 | [diff] [blame] | 209 | * tty_port_hangup - hangup helper |
| 210 | * @port: tty port |
| 211 | * |
| 212 | * Perform port level tty hangup flag and count changes. Drop the tty |
| 213 | * reference. |
| 214 | */ |
| 215 | |
| 216 | void tty_port_hangup(struct tty_port *port) |
| 217 | { |
| 218 | unsigned long flags; |
| 219 | |
| 220 | spin_lock_irqsave(&port->lock, flags); |
| 221 | port->count = 0; |
| 222 | port->flags &= ~ASYNC_NORMAL_ACTIVE; |
Alan Cox | d74e828 | 2009-11-30 13:16:52 +0000 | [diff] [blame] | 223 | if (port->tty) { |
| 224 | set_bit(TTY_IO_ERROR, &port->tty->flags); |
Alan Cox | 3e61696 | 2009-01-02 13:45:26 +0000 | [diff] [blame] | 225 | tty_kref_put(port->tty); |
Alan Cox | d74e828 | 2009-11-30 13:16:52 +0000 | [diff] [blame] | 226 | } |
Alan Cox | 3e61696 | 2009-01-02 13:45:26 +0000 | [diff] [blame] | 227 | port->tty = NULL; |
| 228 | spin_unlock_irqrestore(&port->lock, flags); |
| 229 | wake_up_interruptible(&port->open_wait); |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 230 | wake_up_interruptible(&port->delta_msr_wait); |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 231 | tty_port_shutdown(port); |
Alan Cox | 3e61696 | 2009-01-02 13:45:26 +0000 | [diff] [blame] | 232 | } |
| 233 | EXPORT_SYMBOL(tty_port_hangup); |
| 234 | |
| 235 | /** |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 236 | * tty_port_carrier_raised - carrier raised check |
| 237 | * @port: tty port |
| 238 | * |
| 239 | * Wrapper for the carrier detect logic. For the moment this is used |
| 240 | * to hide some internal details. This will eventually become entirely |
| 241 | * internal to the tty port. |
| 242 | */ |
| 243 | |
| 244 | int tty_port_carrier_raised(struct tty_port *port) |
| 245 | { |
| 246 | if (port->ops->carrier_raised == NULL) |
| 247 | return 1; |
| 248 | return port->ops->carrier_raised(port); |
| 249 | } |
| 250 | EXPORT_SYMBOL(tty_port_carrier_raised); |
Alan Cox | 5d951fb | 2009-01-02 13:45:19 +0000 | [diff] [blame] | 251 | |
| 252 | /** |
Alan Cox | fcc8ac1 | 2009-06-11 12:24:17 +0100 | [diff] [blame] | 253 | * tty_port_raise_dtr_rts - Raise DTR/RTS |
Alan Cox | 5d951fb | 2009-01-02 13:45:19 +0000 | [diff] [blame] | 254 | * @port: tty port |
| 255 | * |
| 256 | * Wrapper for the DTR/RTS raise logic. For the moment this is used |
| 257 | * to hide some internal details. This will eventually become entirely |
| 258 | * internal to the tty port. |
| 259 | */ |
| 260 | |
| 261 | void tty_port_raise_dtr_rts(struct tty_port *port) |
| 262 | { |
Alan Cox | fcc8ac1 | 2009-06-11 12:24:17 +0100 | [diff] [blame] | 263 | if (port->ops->dtr_rts) |
| 264 | port->ops->dtr_rts(port, 1); |
Alan Cox | 5d951fb | 2009-01-02 13:45:19 +0000 | [diff] [blame] | 265 | } |
| 266 | EXPORT_SYMBOL(tty_port_raise_dtr_rts); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 267 | |
| 268 | /** |
Alan Cox | fcc8ac1 | 2009-06-11 12:24:17 +0100 | [diff] [blame] | 269 | * tty_port_lower_dtr_rts - Lower DTR/RTS |
| 270 | * @port: tty port |
| 271 | * |
| 272 | * Wrapper for the DTR/RTS raise logic. For the moment this is used |
| 273 | * to hide some internal details. This will eventually become entirely |
| 274 | * internal to the tty port. |
| 275 | */ |
| 276 | |
| 277 | void tty_port_lower_dtr_rts(struct tty_port *port) |
| 278 | { |
| 279 | if (port->ops->dtr_rts) |
| 280 | port->ops->dtr_rts(port, 0); |
| 281 | } |
| 282 | EXPORT_SYMBOL(tty_port_lower_dtr_rts); |
| 283 | |
| 284 | /** |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 285 | * tty_port_block_til_ready - Waiting logic for tty open |
| 286 | * @port: the tty port being opened |
| 287 | * @tty: the tty device being bound |
| 288 | * @filp: the file pointer of the opener |
| 289 | * |
| 290 | * Implement the core POSIX/SuS tty behaviour when opening a tty device. |
| 291 | * Handles: |
| 292 | * - hangup (both before and during) |
| 293 | * - non blocking open |
| 294 | * - rts/dtr/dcd |
| 295 | * - signals |
| 296 | * - port flags and counts |
| 297 | * |
| 298 | * 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] | 299 | * do carrier detect and the dtr_rts method if it supports software |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 300 | * management of these lines. Note that the dtr/rts raise is done each |
| 301 | * iteration as a hangup may have previously dropped them while we wait. |
| 302 | */ |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 303 | |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 304 | int tty_port_block_til_ready(struct tty_port *port, |
| 305 | struct tty_struct *tty, struct file *filp) |
| 306 | { |
| 307 | int do_clocal = 0, retval; |
| 308 | unsigned long flags; |
Jiri Slaby | 6af9a43 | 2009-06-24 18:35:05 +0100 | [diff] [blame] | 309 | DEFINE_WAIT(wait); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 310 | |
| 311 | /* block if port is in the process of being closed */ |
| 312 | if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) { |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 313 | wait_event_interruptible_tty(tty, port->close_wait, |
Jiri Slaby | 5fc5b42 | 2009-06-11 14:32:42 +0100 | [diff] [blame] | 314 | !(port->flags & ASYNC_CLOSING)); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 315 | if (port->flags & ASYNC_HUP_NOTIFY) |
| 316 | return -EAGAIN; |
| 317 | else |
| 318 | return -ERESTARTSYS; |
| 319 | } |
| 320 | |
| 321 | /* if non-blocking mode is set we can pass directly to open unless |
| 322 | the port has just hung up or is in another error state */ |
Alan Cox | 8627b96 | 2009-11-18 14:12:58 +0000 | [diff] [blame] | 323 | if (tty->flags & (1 << TTY_IO_ERROR)) { |
| 324 | port->flags |= ASYNC_NORMAL_ACTIVE; |
| 325 | return 0; |
| 326 | } |
| 327 | if (filp->f_flags & O_NONBLOCK) { |
Alan Cox | 4175f3e | 2009-10-28 21:12:32 +0100 | [diff] [blame] | 328 | /* Indicate we are open */ |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 329 | if (tty->termios.c_cflag & CBAUD) |
Alan Cox | 4175f3e | 2009-10-28 21:12:32 +0100 | [diff] [blame] | 330 | tty_port_raise_dtr_rts(port); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 331 | port->flags |= ASYNC_NORMAL_ACTIVE; |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | if (C_CLOCAL(tty)) |
| 336 | do_clocal = 1; |
| 337 | |
| 338 | /* Block waiting until we can proceed. We may need to wait for the |
| 339 | carrier, but we must also wait for any close that is in progress |
| 340 | before the next open may complete */ |
| 341 | |
| 342 | retval = 0; |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 343 | |
| 344 | /* The port lock protects the port counts */ |
| 345 | spin_lock_irqsave(&port->lock, flags); |
| 346 | if (!tty_hung_up_p(filp)) |
| 347 | port->count--; |
| 348 | port->blocked_open++; |
| 349 | spin_unlock_irqrestore(&port->lock, flags); |
| 350 | |
| 351 | while (1) { |
| 352 | /* Indicate we are open */ |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 353 | if (tty->termios.c_cflag & CBAUD) |
Alan Cox | 7834909 | 2009-01-02 13:46:43 +0000 | [diff] [blame] | 354 | tty_port_raise_dtr_rts(port); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 355 | |
Jiri Slaby | 3e3b5c0 | 2009-06-11 14:33:37 +0100 | [diff] [blame] | 356 | prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE); |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 357 | /* Check for a hangup or uninitialised port. |
| 358 | Return accordingly */ |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 359 | if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) { |
| 360 | if (port->flags & ASYNC_HUP_NOTIFY) |
| 361 | retval = -EAGAIN; |
| 362 | else |
| 363 | retval = -ERESTARTSYS; |
| 364 | break; |
| 365 | } |
Jiri Slaby | 0eee50a | 2012-01-12 22:55:15 +0100 | [diff] [blame] | 366 | /* |
| 367 | * Probe the carrier. For devices with no carrier detect |
| 368 | * tty_port_carrier_raised will always return true. |
| 369 | * Never ask drivers if CLOCAL is set, this causes troubles |
| 370 | * on some hardware. |
| 371 | */ |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 372 | if (!(port->flags & ASYNC_CLOSING) && |
Jiri Slaby | 0eee50a | 2012-01-12 22:55:15 +0100 | [diff] [blame] | 373 | (do_clocal || tty_port_carrier_raised(port))) |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 374 | break; |
| 375 | if (signal_pending(current)) { |
| 376 | retval = -ERESTARTSYS; |
| 377 | break; |
| 378 | } |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 379 | tty_unlock(tty); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 380 | schedule(); |
Alan Cox | 89c8d91 | 2012-08-08 16:30:13 +0100 | [diff] [blame] | 381 | tty_lock(tty); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 382 | } |
Jiri Slaby | 3e3b5c0 | 2009-06-11 14:33:37 +0100 | [diff] [blame] | 383 | finish_wait(&port->open_wait, &wait); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 384 | |
| 385 | /* Update counts. A parallel hangup will have set count to zero and |
| 386 | we must not mess that up further */ |
| 387 | spin_lock_irqsave(&port->lock, flags); |
| 388 | if (!tty_hung_up_p(filp)) |
| 389 | port->count++; |
| 390 | port->blocked_open--; |
| 391 | if (retval == 0) |
| 392 | port->flags |= ASYNC_NORMAL_ACTIVE; |
| 393 | spin_unlock_irqrestore(&port->lock, flags); |
Alan Cox | ecc2e05 | 2009-07-17 16:17:26 +0100 | [diff] [blame] | 394 | return retval; |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 395 | } |
| 396 | EXPORT_SYMBOL(tty_port_block_til_ready); |
| 397 | |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 398 | int tty_port_close_start(struct tty_port *port, |
| 399 | struct tty_struct *tty, struct file *filp) |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 400 | { |
| 401 | unsigned long flags; |
| 402 | |
| 403 | spin_lock_irqsave(&port->lock, flags); |
| 404 | if (tty_hung_up_p(filp)) { |
| 405 | spin_unlock_irqrestore(&port->lock, flags); |
| 406 | return 0; |
| 407 | } |
| 408 | |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 409 | if (tty->count == 1 && port->count != 1) { |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 410 | printk(KERN_WARNING |
| 411 | "tty_port_close_start: tty->count = 1 port count = %d.\n", |
| 412 | port->count); |
| 413 | port->count = 1; |
| 414 | } |
| 415 | if (--port->count < 0) { |
| 416 | printk(KERN_WARNING "tty_port_close_start: count = %d\n", |
| 417 | port->count); |
| 418 | port->count = 0; |
| 419 | } |
| 420 | |
| 421 | if (port->count) { |
| 422 | spin_unlock_irqrestore(&port->lock, flags); |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 423 | if (port->ops->drop) |
| 424 | port->ops->drop(port); |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 425 | return 0; |
| 426 | } |
Alan Stern | 1f5c13f | 2009-08-20 15:23:47 -0400 | [diff] [blame] | 427 | set_bit(ASYNCB_CLOSING, &port->flags); |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 428 | tty->closing = 1; |
| 429 | spin_unlock_irqrestore(&port->lock, flags); |
Alan Cox | fba85e0 | 2009-01-02 13:48:39 +0000 | [diff] [blame] | 430 | /* Don't block on a stalled port, just pull the chain */ |
| 431 | if (tty->flow_stopped) |
| 432 | tty_driver_flush_buffer(tty); |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 433 | if (test_bit(ASYNCB_INITIALIZED, &port->flags) && |
Alan Cox | 6ed1dba | 2009-01-02 13:48:11 +0000 | [diff] [blame] | 434 | port->closing_wait != ASYNC_CLOSING_WAIT_NONE) |
Jiri Slaby | 424cc03 | 2011-08-25 15:12:07 +0200 | [diff] [blame] | 435 | tty_wait_until_sent_from_close(tty, port->closing_wait); |
Alan Cox | 1ec739b | 2009-06-11 12:25:25 +0100 | [diff] [blame] | 436 | if (port->drain_delay) { |
| 437 | unsigned int bps = tty_get_baud_rate(tty); |
| 438 | long timeout; |
| 439 | |
| 440 | if (bps > 1200) |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 441 | timeout = max_t(long, |
| 442 | (HZ * 10 * port->drain_delay) / bps, HZ / 10); |
Alan Cox | 1ec739b | 2009-06-11 12:25:25 +0100 | [diff] [blame] | 443 | else |
| 444 | timeout = 2 * HZ; |
| 445 | schedule_timeout_interruptible(timeout); |
| 446 | } |
Alan Cox | e707c35 | 2009-11-05 13:27:57 +0000 | [diff] [blame] | 447 | /* Flush the ldisc buffering */ |
| 448 | tty_ldisc_flush(tty); |
| 449 | |
| 450 | /* Drop DTR/RTS if HUPCL is set. This causes any attached modem to |
| 451 | hang up the line */ |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 452 | if (tty->termios.c_cflag & HUPCL) |
Alan Cox | e707c35 | 2009-11-05 13:27:57 +0000 | [diff] [blame] | 453 | tty_port_lower_dtr_rts(port); |
| 454 | |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 455 | /* Don't call port->drop for the last reference. Callers will want |
| 456 | to drop the last active reference in ->shutdown() or the tty |
| 457 | shutdown path */ |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 458 | return 1; |
| 459 | } |
| 460 | EXPORT_SYMBOL(tty_port_close_start); |
| 461 | |
| 462 | void tty_port_close_end(struct tty_port *port, struct tty_struct *tty) |
| 463 | { |
| 464 | unsigned long flags; |
| 465 | |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 466 | spin_lock_irqsave(&port->lock, flags); |
| 467 | tty->closing = 0; |
| 468 | |
| 469 | if (port->blocked_open) { |
| 470 | spin_unlock_irqrestore(&port->lock, flags); |
| 471 | if (port->close_delay) { |
| 472 | msleep_interruptible( |
| 473 | jiffies_to_msecs(port->close_delay)); |
| 474 | } |
| 475 | spin_lock_irqsave(&port->lock, flags); |
| 476 | wake_up_interruptible(&port->open_wait); |
| 477 | } |
| 478 | port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING); |
| 479 | wake_up_interruptible(&port->close_wait); |
| 480 | spin_unlock_irqrestore(&port->lock, flags); |
| 481 | } |
| 482 | EXPORT_SYMBOL(tty_port_close_end); |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 483 | |
| 484 | void tty_port_close(struct tty_port *port, struct tty_struct *tty, |
| 485 | struct file *filp) |
| 486 | { |
| 487 | if (tty_port_close_start(port, tty, filp) == 0) |
| 488 | return; |
| 489 | tty_port_shutdown(port); |
Alan Cox | d74e828 | 2009-11-30 13:16:52 +0000 | [diff] [blame] | 490 | set_bit(TTY_IO_ERROR, &tty->flags); |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 491 | tty_port_close_end(port, tty); |
| 492 | tty_port_tty_set(port, NULL); |
| 493 | } |
| 494 | EXPORT_SYMBOL(tty_port_close); |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 495 | |
Jiri Slaby | 72a33bf | 2012-08-07 21:47:49 +0200 | [diff] [blame] | 496 | /** |
| 497 | * tty_port_install - generic tty->ops->install handler |
| 498 | * @port: tty_port of the device |
| 499 | * @driver: tty_driver for this device |
| 500 | * @tty: tty to be installed |
| 501 | * |
| 502 | * It is the same as tty_standard_install except the provided @port is linked |
| 503 | * to a concrete tty specified by @tty. Use this or tty_port_register_device |
| 504 | * (or both). Call tty_port_link_device as a last resort. |
| 505 | */ |
Jiri Slaby | 695586c | 2012-06-04 13:35:32 +0200 | [diff] [blame] | 506 | int tty_port_install(struct tty_port *port, struct tty_driver *driver, |
| 507 | struct tty_struct *tty) |
| 508 | { |
| 509 | tty->port = port; |
| 510 | return tty_standard_install(driver, tty); |
| 511 | } |
| 512 | EXPORT_SYMBOL_GPL(tty_port_install); |
| 513 | |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 514 | int tty_port_open(struct tty_port *port, struct tty_struct *tty, |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 515 | struct file *filp) |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 516 | { |
| 517 | spin_lock_irq(&port->lock); |
| 518 | if (!tty_hung_up_p(filp)) |
| 519 | ++port->count; |
| 520 | spin_unlock_irq(&port->lock); |
| 521 | tty_port_tty_set(port, tty); |
| 522 | |
| 523 | /* |
| 524 | * Do the device-specific open only if the hardware isn't |
| 525 | * already initialized. Serialize open and shutdown using the |
| 526 | * port mutex. |
| 527 | */ |
| 528 | |
| 529 | mutex_lock(&port->mutex); |
| 530 | |
| 531 | if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) { |
Alan Cox | a9a37ec | 2009-11-30 13:16:57 +0000 | [diff] [blame] | 532 | clear_bit(TTY_IO_ERROR, &tty->flags); |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 533 | if (port->ops->activate) { |
| 534 | int retval = port->ops->activate(port, tty); |
| 535 | if (retval) { |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 536 | mutex_unlock(&port->mutex); |
| 537 | return retval; |
| 538 | } |
| 539 | } |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 540 | set_bit(ASYNCB_INITIALIZED, &port->flags); |
| 541 | } |
| 542 | mutex_unlock(&port->mutex); |
| 543 | return tty_port_block_til_ready(port, tty, filp); |
| 544 | } |
| 545 | |
| 546 | EXPORT_SYMBOL(tty_port_open); |