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)); |
| 24 | init_waitqueue_head(&port->open_wait); |
| 25 | init_waitqueue_head(&port->close_wait); |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 26 | init_waitqueue_head(&port->delta_msr_wait); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 27 | mutex_init(&port->mutex); |
Alan Cox | 44e4909 | 2009-11-30 13:16:41 +0000 | [diff] [blame] | 28 | mutex_init(&port->buf_mutex); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 29 | spin_lock_init(&port->lock); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 30 | port->close_delay = (50 * HZ) / 100; |
| 31 | port->closing_wait = (3000 * HZ) / 100; |
| 32 | } |
| 33 | EXPORT_SYMBOL(tty_port_init); |
| 34 | |
| 35 | int tty_port_alloc_xmit_buf(struct tty_port *port) |
| 36 | { |
| 37 | /* We may sleep in get_zeroed_page() */ |
Alan Cox | 44e4909 | 2009-11-30 13:16:41 +0000 | [diff] [blame] | 38 | mutex_lock(&port->buf_mutex); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 39 | if (port->xmit_buf == NULL) |
| 40 | port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL); |
Alan Cox | 44e4909 | 2009-11-30 13:16:41 +0000 | [diff] [blame] | 41 | mutex_unlock(&port->buf_mutex); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 42 | if (port->xmit_buf == NULL) |
| 43 | return -ENOMEM; |
| 44 | return 0; |
| 45 | } |
| 46 | EXPORT_SYMBOL(tty_port_alloc_xmit_buf); |
| 47 | |
| 48 | void tty_port_free_xmit_buf(struct tty_port *port) |
| 49 | { |
Alan Cox | 44e4909 | 2009-11-30 13:16:41 +0000 | [diff] [blame] | 50 | mutex_lock(&port->buf_mutex); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 51 | if (port->xmit_buf != NULL) { |
| 52 | free_page((unsigned long)port->xmit_buf); |
| 53 | port->xmit_buf = NULL; |
| 54 | } |
Alan Cox | 44e4909 | 2009-11-30 13:16:41 +0000 | [diff] [blame] | 55 | mutex_unlock(&port->buf_mutex); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 56 | } |
| 57 | EXPORT_SYMBOL(tty_port_free_xmit_buf); |
| 58 | |
| 59 | |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 60 | /** |
| 61 | * tty_port_tty_get - get a tty reference |
| 62 | * @port: tty port |
| 63 | * |
| 64 | * Return a refcount protected tty instance or NULL if the port is not |
| 65 | * associated with a tty (eg due to close or hangup) |
| 66 | */ |
| 67 | |
| 68 | struct tty_struct *tty_port_tty_get(struct tty_port *port) |
| 69 | { |
| 70 | unsigned long flags; |
| 71 | struct tty_struct *tty; |
| 72 | |
| 73 | spin_lock_irqsave(&port->lock, flags); |
| 74 | tty = tty_kref_get(port->tty); |
| 75 | spin_unlock_irqrestore(&port->lock, flags); |
| 76 | return tty; |
| 77 | } |
| 78 | EXPORT_SYMBOL(tty_port_tty_get); |
| 79 | |
| 80 | /** |
| 81 | * tty_port_tty_set - set the tty of a port |
| 82 | * @port: tty port |
| 83 | * @tty: the tty |
| 84 | * |
| 85 | * Associate the port and tty pair. Manages any internal refcounts. |
| 86 | * Pass NULL to deassociate a port |
| 87 | */ |
| 88 | |
| 89 | void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty) |
| 90 | { |
| 91 | unsigned long flags; |
| 92 | |
| 93 | spin_lock_irqsave(&port->lock, flags); |
| 94 | if (port->tty) |
| 95 | tty_kref_put(port->tty); |
Alan Cox | cb4bca3 | 2008-10-21 13:47:44 +0100 | [diff] [blame] | 96 | port->tty = tty_kref_get(tty); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 97 | spin_unlock_irqrestore(&port->lock, flags); |
| 98 | } |
| 99 | EXPORT_SYMBOL(tty_port_tty_set); |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 100 | |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 101 | static void tty_port_shutdown(struct tty_port *port) |
| 102 | { |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 103 | mutex_lock(&port->mutex); |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 104 | if (port->ops->shutdown && |
Alan Stern | 1f5c13f | 2009-08-20 15:23:47 -0400 | [diff] [blame] | 105 | test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 106 | port->ops->shutdown(port); |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 107 | mutex_unlock(&port->mutex); |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 110 | /** |
Alan Cox | 3e61696 | 2009-01-02 13:45:26 +0000 | [diff] [blame] | 111 | * tty_port_hangup - hangup helper |
| 112 | * @port: tty port |
| 113 | * |
| 114 | * Perform port level tty hangup flag and count changes. Drop the tty |
| 115 | * reference. |
| 116 | */ |
| 117 | |
| 118 | void tty_port_hangup(struct tty_port *port) |
| 119 | { |
| 120 | unsigned long flags; |
| 121 | |
| 122 | spin_lock_irqsave(&port->lock, flags); |
| 123 | port->count = 0; |
| 124 | port->flags &= ~ASYNC_NORMAL_ACTIVE; |
Alan Cox | d74e828 | 2009-11-30 13:16:52 +0000 | [diff] [blame^] | 125 | if (port->tty) { |
| 126 | set_bit(TTY_IO_ERROR, &port->tty->flags); |
Alan Cox | 3e61696 | 2009-01-02 13:45:26 +0000 | [diff] [blame] | 127 | tty_kref_put(port->tty); |
Alan Cox | d74e828 | 2009-11-30 13:16:52 +0000 | [diff] [blame^] | 128 | } |
Alan Cox | 3e61696 | 2009-01-02 13:45:26 +0000 | [diff] [blame] | 129 | port->tty = NULL; |
| 130 | spin_unlock_irqrestore(&port->lock, flags); |
| 131 | wake_up_interruptible(&port->open_wait); |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 132 | wake_up_interruptible(&port->delta_msr_wait); |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 133 | tty_port_shutdown(port); |
Alan Cox | 3e61696 | 2009-01-02 13:45:26 +0000 | [diff] [blame] | 134 | } |
| 135 | EXPORT_SYMBOL(tty_port_hangup); |
| 136 | |
| 137 | /** |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 138 | * tty_port_carrier_raised - carrier raised check |
| 139 | * @port: tty port |
| 140 | * |
| 141 | * Wrapper for the carrier detect logic. For the moment this is used |
| 142 | * to hide some internal details. This will eventually become entirely |
| 143 | * internal to the tty port. |
| 144 | */ |
| 145 | |
| 146 | int tty_port_carrier_raised(struct tty_port *port) |
| 147 | { |
| 148 | if (port->ops->carrier_raised == NULL) |
| 149 | return 1; |
| 150 | return port->ops->carrier_raised(port); |
| 151 | } |
| 152 | EXPORT_SYMBOL(tty_port_carrier_raised); |
Alan Cox | 5d951fb | 2009-01-02 13:45:19 +0000 | [diff] [blame] | 153 | |
| 154 | /** |
Alan Cox | fcc8ac1 | 2009-06-11 12:24:17 +0100 | [diff] [blame] | 155 | * tty_port_raise_dtr_rts - Raise DTR/RTS |
Alan Cox | 5d951fb | 2009-01-02 13:45:19 +0000 | [diff] [blame] | 156 | * @port: tty port |
| 157 | * |
| 158 | * Wrapper for the DTR/RTS raise logic. For the moment this is used |
| 159 | * to hide some internal details. This will eventually become entirely |
| 160 | * internal to the tty port. |
| 161 | */ |
| 162 | |
| 163 | void tty_port_raise_dtr_rts(struct tty_port *port) |
| 164 | { |
Alan Cox | fcc8ac1 | 2009-06-11 12:24:17 +0100 | [diff] [blame] | 165 | if (port->ops->dtr_rts) |
| 166 | port->ops->dtr_rts(port, 1); |
Alan Cox | 5d951fb | 2009-01-02 13:45:19 +0000 | [diff] [blame] | 167 | } |
| 168 | EXPORT_SYMBOL(tty_port_raise_dtr_rts); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 169 | |
| 170 | /** |
Alan Cox | fcc8ac1 | 2009-06-11 12:24:17 +0100 | [diff] [blame] | 171 | * tty_port_lower_dtr_rts - Lower DTR/RTS |
| 172 | * @port: tty port |
| 173 | * |
| 174 | * Wrapper for the DTR/RTS raise logic. For the moment this is used |
| 175 | * to hide some internal details. This will eventually become entirely |
| 176 | * internal to the tty port. |
| 177 | */ |
| 178 | |
| 179 | void tty_port_lower_dtr_rts(struct tty_port *port) |
| 180 | { |
| 181 | if (port->ops->dtr_rts) |
| 182 | port->ops->dtr_rts(port, 0); |
| 183 | } |
| 184 | EXPORT_SYMBOL(tty_port_lower_dtr_rts); |
| 185 | |
| 186 | /** |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 187 | * tty_port_block_til_ready - Waiting logic for tty open |
| 188 | * @port: the tty port being opened |
| 189 | * @tty: the tty device being bound |
| 190 | * @filp: the file pointer of the opener |
| 191 | * |
| 192 | * Implement the core POSIX/SuS tty behaviour when opening a tty device. |
| 193 | * Handles: |
| 194 | * - hangup (both before and during) |
| 195 | * - non blocking open |
| 196 | * - rts/dtr/dcd |
| 197 | * - signals |
| 198 | * - port flags and counts |
| 199 | * |
| 200 | * 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] | 201 | * do carrier detect and the dtr_rts method if it supports software |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 202 | * management of these lines. Note that the dtr/rts raise is done each |
| 203 | * iteration as a hangup may have previously dropped them while we wait. |
| 204 | */ |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 205 | |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 206 | int tty_port_block_til_ready(struct tty_port *port, |
| 207 | struct tty_struct *tty, struct file *filp) |
| 208 | { |
| 209 | int do_clocal = 0, retval; |
| 210 | unsigned long flags; |
Jiri Slaby | 6af9a43 | 2009-06-24 18:35:05 +0100 | [diff] [blame] | 211 | DEFINE_WAIT(wait); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 212 | int cd; |
| 213 | |
| 214 | /* block if port is in the process of being closed */ |
| 215 | if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) { |
Jiri Slaby | 5fc5b42 | 2009-06-11 14:32:42 +0100 | [diff] [blame] | 216 | wait_event_interruptible(port->close_wait, |
| 217 | !(port->flags & ASYNC_CLOSING)); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 218 | if (port->flags & ASYNC_HUP_NOTIFY) |
| 219 | return -EAGAIN; |
| 220 | else |
| 221 | return -ERESTARTSYS; |
| 222 | } |
| 223 | |
| 224 | /* if non-blocking mode is set we can pass directly to open unless |
| 225 | the port has just hung up or is in another error state */ |
Alan Cox | 8627b96 | 2009-11-18 14:12:58 +0000 | [diff] [blame] | 226 | if (tty->flags & (1 << TTY_IO_ERROR)) { |
| 227 | port->flags |= ASYNC_NORMAL_ACTIVE; |
| 228 | return 0; |
| 229 | } |
| 230 | if (filp->f_flags & O_NONBLOCK) { |
Alan Cox | 4175f3e | 2009-10-28 21:12:32 +0100 | [diff] [blame] | 231 | /* Indicate we are open */ |
| 232 | if (tty->termios->c_cflag & CBAUD) |
| 233 | tty_port_raise_dtr_rts(port); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 234 | port->flags |= ASYNC_NORMAL_ACTIVE; |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | if (C_CLOCAL(tty)) |
| 239 | do_clocal = 1; |
| 240 | |
| 241 | /* Block waiting until we can proceed. We may need to wait for the |
| 242 | carrier, but we must also wait for any close that is in progress |
| 243 | before the next open may complete */ |
| 244 | |
| 245 | retval = 0; |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 246 | |
| 247 | /* The port lock protects the port counts */ |
| 248 | spin_lock_irqsave(&port->lock, flags); |
| 249 | if (!tty_hung_up_p(filp)) |
| 250 | port->count--; |
| 251 | port->blocked_open++; |
| 252 | spin_unlock_irqrestore(&port->lock, flags); |
| 253 | |
| 254 | while (1) { |
| 255 | /* Indicate we are open */ |
Alan Cox | 7834909 | 2009-01-02 13:46:43 +0000 | [diff] [blame] | 256 | if (tty->termios->c_cflag & CBAUD) |
| 257 | tty_port_raise_dtr_rts(port); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 258 | |
Jiri Slaby | 3e3b5c0 | 2009-06-11 14:33:37 +0100 | [diff] [blame] | 259 | prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE); |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 260 | /* Check for a hangup or uninitialised port. |
| 261 | Return accordingly */ |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 262 | if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) { |
| 263 | if (port->flags & ASYNC_HUP_NOTIFY) |
| 264 | retval = -EAGAIN; |
| 265 | else |
| 266 | retval = -ERESTARTSYS; |
| 267 | break; |
| 268 | } |
| 269 | /* Probe the carrier. For devices with no carrier detect this |
| 270 | will always return true */ |
| 271 | cd = tty_port_carrier_raised(port); |
| 272 | if (!(port->flags & ASYNC_CLOSING) && |
| 273 | (do_clocal || cd)) |
| 274 | break; |
| 275 | if (signal_pending(current)) { |
| 276 | retval = -ERESTARTSYS; |
| 277 | break; |
| 278 | } |
| 279 | schedule(); |
| 280 | } |
Jiri Slaby | 3e3b5c0 | 2009-06-11 14:33:37 +0100 | [diff] [blame] | 281 | finish_wait(&port->open_wait, &wait); |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 282 | |
| 283 | /* Update counts. A parallel hangup will have set count to zero and |
| 284 | we must not mess that up further */ |
| 285 | spin_lock_irqsave(&port->lock, flags); |
| 286 | if (!tty_hung_up_p(filp)) |
| 287 | port->count++; |
| 288 | port->blocked_open--; |
| 289 | if (retval == 0) |
| 290 | port->flags |= ASYNC_NORMAL_ACTIVE; |
| 291 | spin_unlock_irqrestore(&port->lock, flags); |
Alan Cox | ecc2e05 | 2009-07-17 16:17:26 +0100 | [diff] [blame] | 292 | return retval; |
Alan Cox | 36c621d | 2009-01-02 13:46:10 +0000 | [diff] [blame] | 293 | } |
| 294 | EXPORT_SYMBOL(tty_port_block_til_ready); |
| 295 | |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 296 | int tty_port_close_start(struct tty_port *port, |
| 297 | struct tty_struct *tty, struct file *filp) |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 298 | { |
| 299 | unsigned long flags; |
| 300 | |
| 301 | spin_lock_irqsave(&port->lock, flags); |
| 302 | if (tty_hung_up_p(filp)) { |
| 303 | spin_unlock_irqrestore(&port->lock, flags); |
| 304 | return 0; |
| 305 | } |
| 306 | |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 307 | if (tty->count == 1 && port->count != 1) { |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 308 | printk(KERN_WARNING |
| 309 | "tty_port_close_start: tty->count = 1 port count = %d.\n", |
| 310 | port->count); |
| 311 | port->count = 1; |
| 312 | } |
| 313 | if (--port->count < 0) { |
| 314 | printk(KERN_WARNING "tty_port_close_start: count = %d\n", |
| 315 | port->count); |
| 316 | port->count = 0; |
| 317 | } |
| 318 | |
| 319 | if (port->count) { |
| 320 | spin_unlock_irqrestore(&port->lock, flags); |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 321 | if (port->ops->drop) |
| 322 | port->ops->drop(port); |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 323 | return 0; |
| 324 | } |
Alan Stern | 1f5c13f | 2009-08-20 15:23:47 -0400 | [diff] [blame] | 325 | set_bit(ASYNCB_CLOSING, &port->flags); |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 326 | tty->closing = 1; |
| 327 | spin_unlock_irqrestore(&port->lock, flags); |
Alan Cox | fba85e0 | 2009-01-02 13:48:39 +0000 | [diff] [blame] | 328 | /* Don't block on a stalled port, just pull the chain */ |
| 329 | if (tty->flow_stopped) |
| 330 | tty_driver_flush_buffer(tty); |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 331 | if (test_bit(ASYNCB_INITIALIZED, &port->flags) && |
Alan Cox | 6ed1dba | 2009-01-02 13:48:11 +0000 | [diff] [blame] | 332 | port->closing_wait != ASYNC_CLOSING_WAIT_NONE) |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 333 | tty_wait_until_sent(tty, port->closing_wait); |
Alan Cox | 1ec739b | 2009-06-11 12:25:25 +0100 | [diff] [blame] | 334 | if (port->drain_delay) { |
| 335 | unsigned int bps = tty_get_baud_rate(tty); |
| 336 | long timeout; |
| 337 | |
| 338 | if (bps > 1200) |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 339 | timeout = max_t(long, |
| 340 | (HZ * 10 * port->drain_delay) / bps, HZ / 10); |
Alan Cox | 1ec739b | 2009-06-11 12:25:25 +0100 | [diff] [blame] | 341 | else |
| 342 | timeout = 2 * HZ; |
| 343 | schedule_timeout_interruptible(timeout); |
| 344 | } |
Alan Cox | e707c35 | 2009-11-05 13:27:57 +0000 | [diff] [blame] | 345 | /* Flush the ldisc buffering */ |
| 346 | tty_ldisc_flush(tty); |
| 347 | |
| 348 | /* Drop DTR/RTS if HUPCL is set. This causes any attached modem to |
| 349 | hang up the line */ |
| 350 | if (tty->termios->c_cflag & HUPCL) |
| 351 | tty_port_lower_dtr_rts(port); |
| 352 | |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 353 | /* Don't call port->drop for the last reference. Callers will want |
| 354 | to drop the last active reference in ->shutdown() or the tty |
| 355 | shutdown path */ |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 356 | return 1; |
| 357 | } |
| 358 | EXPORT_SYMBOL(tty_port_close_start); |
| 359 | |
| 360 | void tty_port_close_end(struct tty_port *port, struct tty_struct *tty) |
| 361 | { |
| 362 | unsigned long flags; |
| 363 | |
Alan Cox | a661499 | 2009-01-02 13:46:50 +0000 | [diff] [blame] | 364 | spin_lock_irqsave(&port->lock, flags); |
| 365 | tty->closing = 0; |
| 366 | |
| 367 | if (port->blocked_open) { |
| 368 | spin_unlock_irqrestore(&port->lock, flags); |
| 369 | if (port->close_delay) { |
| 370 | msleep_interruptible( |
| 371 | jiffies_to_msecs(port->close_delay)); |
| 372 | } |
| 373 | spin_lock_irqsave(&port->lock, flags); |
| 374 | wake_up_interruptible(&port->open_wait); |
| 375 | } |
| 376 | port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING); |
| 377 | wake_up_interruptible(&port->close_wait); |
| 378 | spin_unlock_irqrestore(&port->lock, flags); |
| 379 | } |
| 380 | EXPORT_SYMBOL(tty_port_close_end); |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 381 | |
| 382 | void tty_port_close(struct tty_port *port, struct tty_struct *tty, |
| 383 | struct file *filp) |
| 384 | { |
| 385 | if (tty_port_close_start(port, tty, filp) == 0) |
| 386 | return; |
| 387 | tty_port_shutdown(port); |
Alan Cox | d74e828 | 2009-11-30 13:16:52 +0000 | [diff] [blame^] | 388 | set_bit(TTY_IO_ERROR, &tty->flags); |
Alan Cox | 7ca0ff9 | 2009-09-19 13:13:20 -0700 | [diff] [blame] | 389 | tty_port_close_end(port, tty); |
| 390 | tty_port_tty_set(port, NULL); |
| 391 | } |
| 392 | EXPORT_SYMBOL(tty_port_close); |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 393 | |
| 394 | int tty_port_open(struct tty_port *port, struct tty_struct *tty, |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 395 | struct file *filp) |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 396 | { |
| 397 | spin_lock_irq(&port->lock); |
| 398 | if (!tty_hung_up_p(filp)) |
| 399 | ++port->count; |
| 400 | spin_unlock_irq(&port->lock); |
| 401 | tty_port_tty_set(port, tty); |
| 402 | |
| 403 | /* |
| 404 | * Do the device-specific open only if the hardware isn't |
| 405 | * already initialized. Serialize open and shutdown using the |
| 406 | * port mutex. |
| 407 | */ |
| 408 | |
| 409 | mutex_lock(&port->mutex); |
| 410 | |
| 411 | if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) { |
| 412 | if (port->ops->activate) { |
| 413 | int retval = port->ops->activate(port, tty); |
| 414 | if (retval) { |
Alan Cox | d774a56 | 2009-10-06 16:06:21 +0100 | [diff] [blame] | 415 | mutex_unlock(&port->mutex); |
| 416 | return retval; |
| 417 | } |
| 418 | } |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 419 | set_bit(ASYNCB_INITIALIZED, &port->flags); |
Alan Cox | d74e828 | 2009-11-30 13:16:52 +0000 | [diff] [blame^] | 420 | clear_bit(TTY_IO_ERROR, &tty->flags); |
Alan Cox | 64bc397 | 2009-10-06 16:06:11 +0100 | [diff] [blame] | 421 | } |
| 422 | mutex_unlock(&port->mutex); |
| 423 | return tty_port_block_til_ready(port, tty, filp); |
| 424 | } |
| 425 | |
| 426 | EXPORT_SYMBOL(tty_port_open); |