Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * generic_serial.c |
| 3 | * |
| 4 | * Copyright (C) 1998/1999 R.E.Wolff@BitWizard.nl |
| 5 | * |
| 6 | * written for the SX serial driver. |
| 7 | * Contains the code that should be shared over all the serial drivers. |
| 8 | * |
| 9 | * Credit for the idea to do it this way might go to Alan Cox. |
| 10 | * |
| 11 | * |
| 12 | * Version 0.1 -- December, 1998. Initial version. |
| 13 | * Version 0.2 -- March, 1999. Some more routines. Bugfixes. Etc. |
| 14 | * Version 0.5 -- August, 1999. Some more fixes. Reformat for Linus. |
| 15 | * |
| 16 | * BitWizard is actively maintaining this file. We sometimes find |
| 17 | * that someone submitted changes to this file. We really appreciate |
| 18 | * your help, but please submit changes through us. We're doing our |
| 19 | * best to be responsive. -- REW |
| 20 | * */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/tty.h> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 25 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/serial.h> |
| 27 | #include <linux/mm.h> |
| 28 | #include <linux/generic_serial.h> |
| 29 | #include <linux/interrupt.h> |
| 30 | #include <linux/tty_flip.h> |
| 31 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <asm/uaccess.h> |
| 33 | |
| 34 | #define DEBUG |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | static int gs_debug; |
| 37 | |
| 38 | #ifdef DEBUG |
| 39 | #define gs_dprintk(f, str...) if (gs_debug & f) printk (str) |
| 40 | #else |
| 41 | #define gs_dprintk(f, str...) /* nothing */ |
| 42 | #endif |
| 43 | |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 44 | #define func_enter() gs_dprintk (GS_DEBUG_FLOW, "gs: enter %s\n", __func__) |
| 45 | #define func_exit() gs_dprintk (GS_DEBUG_FLOW, "gs: exit %s\n", __func__) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | #define RS_EVENT_WRITE_WAKEUP 1 |
| 48 | |
| 49 | module_param(gs_debug, int, 0644); |
| 50 | |
| 51 | |
Alan Cox | 76b25a5 | 2008-04-30 00:54:03 -0700 | [diff] [blame] | 52 | int gs_put_char(struct tty_struct * tty, unsigned char ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { |
| 54 | struct gs_port *port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | func_enter (); |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | port = tty->driver_data; |
| 59 | |
Alan Cox | 76b25a5 | 2008-04-30 00:54:03 -0700 | [diff] [blame] | 60 | if (!port) return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 62 | if (! (port->port.flags & ASYNC_INITIALIZED)) return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | /* Take a lock on the serial tranmit buffer! */ |
Alan Cox | 3542612 | 2007-07-15 23:41:47 -0700 | [diff] [blame] | 65 | mutex_lock(& port->port_write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) { |
| 68 | /* Sorry, buffer is full, drop character. Update statistics???? -- REW */ |
Alan Cox | 3542612 | 2007-07-15 23:41:47 -0700 | [diff] [blame] | 69 | mutex_unlock(&port->port_write_mutex); |
Alan Cox | 76b25a5 | 2008-04-30 00:54:03 -0700 | [diff] [blame] | 70 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | port->xmit_buf[port->xmit_head++] = ch; |
| 74 | port->xmit_head &= SERIAL_XMIT_SIZE - 1; |
| 75 | port->xmit_cnt++; /* Characters in buffer */ |
| 76 | |
Alan Cox | 3542612 | 2007-07-15 23:41:47 -0700 | [diff] [blame] | 77 | mutex_unlock(&port->port_write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | func_exit (); |
Alan Cox | 76b25a5 | 2008-04-30 00:54:03 -0700 | [diff] [blame] | 79 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* |
| 84 | > Problems to take into account are: |
| 85 | > -1- Interrupts that empty part of the buffer. |
| 86 | > -2- page faults on the access to userspace. |
| 87 | > -3- Other processes that are also trying to do a "write". |
| 88 | */ |
| 89 | |
| 90 | int gs_write(struct tty_struct * tty, |
| 91 | const unsigned char *buf, int count) |
| 92 | { |
| 93 | struct gs_port *port; |
| 94 | int c, total = 0; |
| 95 | int t; |
| 96 | |
| 97 | func_enter (); |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | port = tty->driver_data; |
| 100 | |
| 101 | if (!port) return 0; |
| 102 | |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 103 | if (! (port->port.flags & ASYNC_INITIALIZED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | return 0; |
| 105 | |
| 106 | /* get exclusive "write" access to this port (problem 3) */ |
| 107 | /* This is not a spinlock because we can have a disk access (page |
| 108 | fault) in copy_from_user */ |
Ingo Molnar | 81861d7 | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 109 | mutex_lock(& port->port_write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
| 111 | while (1) { |
| 112 | |
| 113 | c = count; |
| 114 | |
| 115 | /* This is safe because we "OWN" the "head". Noone else can |
Ingo Molnar | 81861d7 | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 116 | change the "head": we own the port_write_mutex. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | /* Don't overrun the end of the buffer */ |
| 118 | t = SERIAL_XMIT_SIZE - port->xmit_head; |
| 119 | if (t < c) c = t; |
| 120 | |
| 121 | /* This is safe because the xmit_cnt can only decrease. This |
| 122 | would increase "t", so we might copy too little chars. */ |
| 123 | /* Don't copy past the "head" of the buffer */ |
| 124 | t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt; |
| 125 | if (t < c) c = t; |
| 126 | |
| 127 | /* Can't copy more? break out! */ |
| 128 | if (c <= 0) break; |
| 129 | |
| 130 | memcpy (port->xmit_buf + port->xmit_head, buf, c); |
| 131 | |
| 132 | port -> xmit_cnt += c; |
| 133 | port -> xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE -1); |
| 134 | buf += c; |
| 135 | count -= c; |
| 136 | total += c; |
| 137 | } |
Ingo Molnar | 81861d7 | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 138 | mutex_unlock(& port->port_write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | gs_dprintk (GS_DEBUG_WRITE, "write: interrupts are %s\n", |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 141 | (port->port.flags & GS_TX_INTEN)?"enabled": "disabled"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | if (port->xmit_cnt && |
| 144 | !tty->stopped && |
| 145 | !tty->hw_stopped && |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 146 | !(port->port.flags & GS_TX_INTEN)) { |
| 147 | port->port.flags |= GS_TX_INTEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | port->rd->enable_tx_interrupts (port); |
| 149 | } |
| 150 | func_exit (); |
| 151 | return total; |
| 152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | |
| 155 | |
| 156 | int gs_write_room(struct tty_struct * tty) |
| 157 | { |
| 158 | struct gs_port *port = tty->driver_data; |
| 159 | int ret; |
| 160 | |
| 161 | func_enter (); |
| 162 | ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1; |
| 163 | if (ret < 0) |
| 164 | ret = 0; |
| 165 | func_exit (); |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | int gs_chars_in_buffer(struct tty_struct *tty) |
| 171 | { |
| 172 | struct gs_port *port = tty->driver_data; |
| 173 | func_enter (); |
| 174 | |
| 175 | func_exit (); |
| 176 | return port->xmit_cnt; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | static int gs_real_chars_in_buffer(struct tty_struct *tty) |
| 181 | { |
| 182 | struct gs_port *port; |
| 183 | func_enter (); |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | port = tty->driver_data; |
| 186 | |
| 187 | if (!port->rd) return 0; |
| 188 | if (!port->rd->chars_in_buffer) return 0; |
| 189 | |
| 190 | func_exit (); |
| 191 | return port->xmit_cnt + port->rd->chars_in_buffer (port); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | static int gs_wait_tx_flushed (void * ptr, unsigned long timeout) |
| 196 | { |
| 197 | struct gs_port *port = ptr; |
| 198 | unsigned long end_jiffies; |
| 199 | int jiffies_to_transmit, charsleft = 0, rv = 0; |
| 200 | int rcib; |
| 201 | |
| 202 | func_enter(); |
| 203 | |
| 204 | gs_dprintk (GS_DEBUG_FLUSH, "port=%p.\n", port); |
| 205 | if (port) { |
| 206 | gs_dprintk (GS_DEBUG_FLUSH, "xmit_cnt=%x, xmit_buf=%p, tty=%p.\n", |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 207 | port->xmit_cnt, port->xmit_buf, port->port.tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | if (!port || port->xmit_cnt < 0 || !port->xmit_buf) { |
| 211 | gs_dprintk (GS_DEBUG_FLUSH, "ERROR: !port, !port->xmit_buf or prot->xmit_cnt < 0.\n"); |
| 212 | func_exit(); |
| 213 | return -EINVAL; /* This is an error which we don't know how to handle. */ |
| 214 | } |
| 215 | |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 216 | rcib = gs_real_chars_in_buffer(port->port.tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
| 218 | if(rcib <= 0) { |
| 219 | gs_dprintk (GS_DEBUG_FLUSH, "nothing to wait for.\n"); |
| 220 | func_exit(); |
| 221 | return rv; |
| 222 | } |
| 223 | /* stop trying: now + twice the time it would normally take + seconds */ |
| 224 | if (timeout == 0) timeout = MAX_SCHEDULE_TIMEOUT; |
| 225 | end_jiffies = jiffies; |
| 226 | if (timeout != MAX_SCHEDULE_TIMEOUT) |
| 227 | end_jiffies += port->baud?(2 * rcib * 10 * HZ / port->baud):0; |
| 228 | end_jiffies += timeout; |
| 229 | |
| 230 | gs_dprintk (GS_DEBUG_FLUSH, "now=%lx, end=%lx (%ld).\n", |
| 231 | jiffies, end_jiffies, end_jiffies-jiffies); |
| 232 | |
| 233 | /* the expression is actually jiffies < end_jiffies, but that won't |
| 234 | work around the wraparound. Tricky eh? */ |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 235 | while ((charsleft = gs_real_chars_in_buffer (port->port.tty)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | time_after (end_jiffies, jiffies)) { |
| 237 | /* Units check: |
| 238 | chars * (bits/char) * (jiffies /sec) / (bits/sec) = jiffies! |
| 239 | check! */ |
| 240 | |
| 241 | charsleft += 16; /* Allow 16 chars more to be transmitted ... */ |
| 242 | jiffies_to_transmit = port->baud?(1 + charsleft * 10 * HZ / port->baud):0; |
| 243 | /* ^^^ Round up.... */ |
| 244 | if (jiffies_to_transmit <= 0) jiffies_to_transmit = 1; |
| 245 | |
| 246 | gs_dprintk (GS_DEBUG_FLUSH, "Expect to finish in %d jiffies " |
| 247 | "(%d chars).\n", jiffies_to_transmit, charsleft); |
| 248 | |
| 249 | msleep_interruptible(jiffies_to_msecs(jiffies_to_transmit)); |
| 250 | if (signal_pending (current)) { |
| 251 | gs_dprintk (GS_DEBUG_FLUSH, "Signal pending. Bombing out: "); |
| 252 | rv = -EINTR; |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | gs_dprintk (GS_DEBUG_FLUSH, "charsleft = %d.\n", charsleft); |
| 258 | set_current_state (TASK_RUNNING); |
| 259 | |
| 260 | func_exit(); |
| 261 | return rv; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | |
| 266 | void gs_flush_buffer(struct tty_struct *tty) |
| 267 | { |
| 268 | struct gs_port *port; |
| 269 | unsigned long flags; |
| 270 | |
| 271 | func_enter (); |
| 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | port = tty->driver_data; |
| 274 | |
| 275 | if (!port) return; |
| 276 | |
| 277 | /* XXX Would the write semaphore do? */ |
| 278 | spin_lock_irqsave (&port->driver_lock, flags); |
| 279 | port->xmit_cnt = port->xmit_head = port->xmit_tail = 0; |
| 280 | spin_unlock_irqrestore (&port->driver_lock, flags); |
| 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | tty_wakeup(tty); |
| 283 | func_exit (); |
| 284 | } |
| 285 | |
| 286 | |
| 287 | void gs_flush_chars(struct tty_struct * tty) |
| 288 | { |
| 289 | struct gs_port *port; |
| 290 | |
| 291 | func_enter (); |
| 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | port = tty->driver_data; |
| 294 | |
| 295 | if (!port) return; |
| 296 | |
| 297 | if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped || |
| 298 | !port->xmit_buf) { |
| 299 | func_exit (); |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | /* Beats me -- REW */ |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 304 | port->port.flags |= GS_TX_INTEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | port->rd->enable_tx_interrupts (port); |
| 306 | func_exit (); |
| 307 | } |
| 308 | |
| 309 | |
| 310 | void gs_stop(struct tty_struct * tty) |
| 311 | { |
| 312 | struct gs_port *port; |
| 313 | |
| 314 | func_enter (); |
| 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | port = tty->driver_data; |
| 317 | |
| 318 | if (!port) return; |
| 319 | |
| 320 | if (port->xmit_cnt && |
| 321 | port->xmit_buf && |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 322 | (port->port.flags & GS_TX_INTEN) ) { |
| 323 | port->port.flags &= ~GS_TX_INTEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | port->rd->disable_tx_interrupts (port); |
| 325 | } |
| 326 | func_exit (); |
| 327 | } |
| 328 | |
| 329 | |
| 330 | void gs_start(struct tty_struct * tty) |
| 331 | { |
| 332 | struct gs_port *port; |
| 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | port = tty->driver_data; |
| 335 | |
| 336 | if (!port) return; |
| 337 | |
| 338 | if (port->xmit_cnt && |
| 339 | port->xmit_buf && |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 340 | !(port->port.flags & GS_TX_INTEN) ) { |
| 341 | port->port.flags |= GS_TX_INTEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | port->rd->enable_tx_interrupts (port); |
| 343 | } |
| 344 | func_exit (); |
| 345 | } |
| 346 | |
| 347 | |
| 348 | static void gs_shutdown_port (struct gs_port *port) |
| 349 | { |
| 350 | unsigned long flags; |
| 351 | |
| 352 | func_enter(); |
| 353 | |
| 354 | if (!port) return; |
| 355 | |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 356 | if (!(port->port.flags & ASYNC_INITIALIZED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | return; |
| 358 | |
| 359 | spin_lock_irqsave(&port->driver_lock, flags); |
| 360 | |
| 361 | if (port->xmit_buf) { |
| 362 | free_page((unsigned long) port->xmit_buf); |
| 363 | port->xmit_buf = NULL; |
| 364 | } |
| 365 | |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 366 | if (port->port.tty) |
| 367 | set_bit(TTY_IO_ERROR, &port->port.tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | port->rd->shutdown_port (port); |
| 370 | |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 371 | port->port.flags &= ~ASYNC_INITIALIZED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | spin_unlock_irqrestore(&port->driver_lock, flags); |
| 373 | |
| 374 | func_exit(); |
| 375 | } |
| 376 | |
| 377 | |
| 378 | void gs_hangup(struct tty_struct *tty) |
| 379 | { |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 380 | struct gs_port *port; |
| 381 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
| 383 | func_enter (); |
| 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | port = tty->driver_data; |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 386 | tty = port->port.tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | if (!tty) |
| 388 | return; |
| 389 | |
| 390 | gs_shutdown_port (port); |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 391 | spin_lock_irqsave(&port->port.lock, flags); |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 392 | port->port.flags &= ~(ASYNC_NORMAL_ACTIVE|GS_ACTIVE); |
| 393 | port->port.tty = NULL; |
| 394 | port->port.count = 0; |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 395 | spin_unlock_irqrestore(&port->port.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 397 | wake_up_interruptible(&port->port.open_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | func_exit (); |
| 399 | } |
| 400 | |
| 401 | |
| 402 | int gs_block_til_ready(void *port_, struct file * filp) |
| 403 | { |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 404 | struct gs_port *gp = port_; |
| 405 | struct tty_port *port = &gp->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | DECLARE_WAITQUEUE(wait, current); |
| 407 | int retval; |
| 408 | int do_clocal = 0; |
| 409 | int CD; |
| 410 | struct tty_struct *tty; |
| 411 | unsigned long flags; |
| 412 | |
| 413 | func_enter (); |
| 414 | |
| 415 | if (!port) return 0; |
| 416 | |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 417 | tty = port->tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | gs_dprintk (GS_DEBUG_BTR, "Entering gs_block_till_ready.\n"); |
| 420 | /* |
| 421 | * If the device is in the middle of being closed, then block |
| 422 | * until it's done, and then try again. |
| 423 | */ |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 424 | if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) { |
| 425 | interruptible_sleep_on(&port->close_wait); |
| 426 | if (port->flags & ASYNC_HUP_NOTIFY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | return -EAGAIN; |
| 428 | else |
| 429 | return -ERESTARTSYS; |
| 430 | } |
| 431 | |
| 432 | gs_dprintk (GS_DEBUG_BTR, "after hung up\n"); |
| 433 | |
| 434 | /* |
| 435 | * If non-blocking mode is set, or the port is not enabled, |
| 436 | * then make the check up front and then exit. |
| 437 | */ |
| 438 | if ((filp->f_flags & O_NONBLOCK) || |
| 439 | (tty->flags & (1 << TTY_IO_ERROR))) { |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 440 | port->flags |= ASYNC_NORMAL_ACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | gs_dprintk (GS_DEBUG_BTR, "after nonblock\n"); |
| 445 | |
| 446 | if (C_CLOCAL(tty)) |
| 447 | do_clocal = 1; |
| 448 | |
| 449 | /* |
| 450 | * Block waiting for the carrier detect and the line to become |
| 451 | * free (i.e., not in use by the callout). While we are in |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 452 | * this loop, port->count is dropped by one, so that |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | * rs_close() knows when to free things. We restore it upon |
| 454 | * exit, either normal or abnormal. |
| 455 | */ |
| 456 | retval = 0; |
| 457 | |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 458 | add_wait_queue(&port->open_wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
| 460 | gs_dprintk (GS_DEBUG_BTR, "after add waitq.\n"); |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 461 | spin_lock_irqsave(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | if (!tty_hung_up_p(filp)) { |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 463 | port->count--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 465 | port->blocked_open++; |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 466 | spin_unlock_irqrestore(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | while (1) { |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 468 | CD = tty_port_carrier_raised(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | gs_dprintk (GS_DEBUG_BTR, "CD is now %d.\n", CD); |
| 470 | set_current_state (TASK_INTERRUPTIBLE); |
| 471 | if (tty_hung_up_p(filp) || |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 472 | !(port->flags & ASYNC_INITIALIZED)) { |
| 473 | if (port->flags & ASYNC_HUP_NOTIFY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | retval = -EAGAIN; |
| 475 | else |
| 476 | retval = -ERESTARTSYS; |
| 477 | break; |
| 478 | } |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 479 | if (!(port->flags & ASYNC_CLOSING) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | (do_clocal || CD)) |
| 481 | break; |
| 482 | gs_dprintk (GS_DEBUG_BTR, "signal_pending is now: %d (%lx)\n", |
| 483 | (int)signal_pending (current), *(long*)(¤t->blocked)); |
| 484 | if (signal_pending(current)) { |
| 485 | retval = -ERESTARTSYS; |
| 486 | break; |
| 487 | } |
| 488 | schedule(); |
| 489 | } |
| 490 | gs_dprintk (GS_DEBUG_BTR, "Got out of the loop. (%d)\n", |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 491 | port->blocked_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | set_current_state (TASK_RUNNING); |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 493 | remove_wait_queue(&port->open_wait, &wait); |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 494 | |
| 495 | spin_lock_irqsave(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | if (!tty_hung_up_p(filp)) { |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 497 | port->count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
Alan Cox | 31f3593 | 2009-01-02 13:45:05 +0000 | [diff] [blame] | 499 | port->blocked_open--; |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 500 | if (retval == 0) |
| 501 | port->flags |= ASYNC_NORMAL_ACTIVE; |
| 502 | spin_unlock_irqrestore(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | func_exit (); |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 504 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | |
| 508 | void gs_close(struct tty_struct * tty, struct file * filp) |
| 509 | { |
| 510 | unsigned long flags; |
| 511 | struct gs_port *port; |
| 512 | |
| 513 | func_enter (); |
| 514 | |
Alan Cox | c9f19e9 | 2009-01-02 13:47:26 +0000 | [diff] [blame] | 515 | port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
| 517 | if (!port) return; |
| 518 | |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 519 | if (!port->port.tty) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | /* This seems to happen when this is called from vhangup. */ |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 521 | gs_dprintk (GS_DEBUG_CLOSE, "gs: Odd: port->port.tty is NULL\n"); |
| 522 | port->port.tty = tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 525 | spin_lock_irqsave(&port->port.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
| 527 | if (tty_hung_up_p(filp)) { |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 528 | spin_unlock_irqrestore(&port->port.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | if (port->rd->hungup) |
| 530 | port->rd->hungup (port); |
| 531 | func_exit (); |
| 532 | return; |
| 533 | } |
| 534 | |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 535 | if ((tty->count == 1) && (port->port.count != 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | printk(KERN_ERR "gs: gs_close port %p: bad port count;" |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 537 | " tty->count is 1, port count is %d\n", port, port->port.count); |
| 538 | port->port.count = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 540 | if (--port->port.count < 0) { |
| 541 | printk(KERN_ERR "gs: gs_close port %p: bad port count: %d\n", port, port->port.count); |
| 542 | port->port.count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 545 | if (port->port.count) { |
| 546 | gs_dprintk(GS_DEBUG_CLOSE, "gs_close port %p: count: %d\n", port, port->port.count); |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 547 | spin_unlock_irqrestore(&port->port.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | func_exit (); |
| 549 | return; |
| 550 | } |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 551 | port->port.flags |= ASYNC_CLOSING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
| 553 | /* |
| 554 | * Now we wait for the transmit buffer to clear; and we notify |
| 555 | * the line discipline to only process XON/XOFF characters. |
| 556 | */ |
| 557 | tty->closing = 1; |
| 558 | /* if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE) |
| 559 | tty_wait_until_sent(tty, port->closing_wait); */ |
| 560 | |
| 561 | /* |
| 562 | * At this point we stop accepting input. To do this, we |
| 563 | * disable the receive line status interrupts, and tell the |
| 564 | * interrupt driver to stop checking the data ready bit in the |
| 565 | * line status register. |
| 566 | */ |
| 567 | |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 568 | spin_lock_irqsave(&port->driver_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | port->rd->disable_rx_interrupts (port); |
| 570 | spin_unlock_irqrestore(&port->driver_lock, flags); |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 571 | spin_unlock_irqrestore(&port->port.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
| 573 | /* close has no way of returning "EINTR", so discard return value */ |
| 574 | if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE) |
| 575 | gs_wait_tx_flushed (port, port->closing_wait); |
| 576 | |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 577 | port->port.flags &= ~GS_ACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
Alan Cox | 978e595 | 2008-04-30 00:53:59 -0700 | [diff] [blame] | 579 | gs_flush_buffer(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
| 581 | tty_ldisc_flush(tty); |
| 582 | tty->closing = 0; |
| 583 | |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 584 | spin_lock_irqsave(&port->driver_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | port->event = 0; |
| 586 | port->rd->close (port); |
| 587 | port->rd->shutdown_port (port); |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 588 | spin_unlock_irqrestore(&port->driver_lock, flags); |
| 589 | |
| 590 | spin_lock_irqsave(&port->port.lock, flags); |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 591 | port->port.tty = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 593 | if (port->port.blocked_open) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | if (port->close_delay) { |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 595 | spin_unlock_irqrestore(&port->port.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | msleep_interruptible(jiffies_to_msecs(port->close_delay)); |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 597 | spin_lock_irqsave(&port->port.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 599 | wake_up_interruptible(&port->port.open_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | } |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 601 | port->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING | ASYNC_INITIALIZED); |
Alan Cox | 510a304 | 2009-01-02 13:45:36 +0000 | [diff] [blame] | 602 | spin_unlock_irqrestore(&port->port.lock, flags); |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 603 | wake_up_interruptible(&port->port.close_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
| 605 | func_exit (); |
| 606 | } |
| 607 | |
| 608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | void gs_set_termios (struct tty_struct * tty, |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 610 | struct ktermios * old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | { |
| 612 | struct gs_port *port; |
| 613 | int baudrate, tmp, rv; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 614 | struct ktermios *tiosp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
| 616 | func_enter(); |
| 617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | port = tty->driver_data; |
| 619 | |
| 620 | if (!port) return; |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 621 | if (!port->port.tty) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | /* This seems to happen when this is called after gs_close. */ |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 623 | gs_dprintk (GS_DEBUG_TERMIOS, "gs: Odd: port->port.tty is NULL\n"); |
| 624 | port->port.tty = tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | |
| 628 | tiosp = tty->termios; |
| 629 | |
| 630 | if (gs_debug & GS_DEBUG_TERMIOS) { |
| 631 | gs_dprintk (GS_DEBUG_TERMIOS, "termios structure (%p):\n", tiosp); |
| 632 | } |
| 633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | if(old_termios && (gs_debug & GS_DEBUG_TERMIOS)) { |
| 635 | if(tiosp->c_iflag != old_termios->c_iflag) printk("c_iflag changed\n"); |
| 636 | if(tiosp->c_oflag != old_termios->c_oflag) printk("c_oflag changed\n"); |
| 637 | if(tiosp->c_cflag != old_termios->c_cflag) printk("c_cflag changed\n"); |
| 638 | if(tiosp->c_lflag != old_termios->c_lflag) printk("c_lflag changed\n"); |
| 639 | if(tiosp->c_line != old_termios->c_line) printk("c_line changed\n"); |
| 640 | if(!memcmp(tiosp->c_cc, old_termios->c_cc, NCC)) printk("c_cc changed\n"); |
| 641 | } |
| 642 | |
Alan Cox | d720bc4 | 2006-09-29 02:01:38 -0700 | [diff] [blame] | 643 | baudrate = tty_get_baud_rate(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | if ((tiosp->c_cflag & CBAUD) == B38400) { |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 646 | if ( (port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | baudrate = 57600; |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 648 | else if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | baudrate = 115200; |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 650 | else if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | baudrate = 230400; |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 652 | else if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | baudrate = 460800; |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 654 | else if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | baudrate = (port->baud_base / port->custom_divisor); |
| 656 | } |
| 657 | |
| 658 | /* I recommend using THIS instead of the mess in termios (and |
| 659 | duplicating the above code). Next we should create a clean |
| 660 | interface towards this variable. If your card supports arbitrary |
| 661 | baud rates, (e.g. CD1400 or 16550 based cards) then everything |
| 662 | will be very easy..... */ |
| 663 | port->baud = baudrate; |
| 664 | |
| 665 | /* Two timer ticks seems enough to wakeup something like SLIP driver */ |
| 666 | /* Baudrate/10 is cps. Divide by HZ to get chars per tick. */ |
| 667 | tmp = (baudrate / 10 / HZ) * 2; |
| 668 | |
| 669 | if (tmp < 0) tmp = 0; |
| 670 | if (tmp >= SERIAL_XMIT_SIZE) tmp = SERIAL_XMIT_SIZE-1; |
| 671 | |
| 672 | port->wakeup_chars = tmp; |
| 673 | |
| 674 | /* We should really wait for the characters to be all sent before |
| 675 | changing the settings. -- CAL */ |
| 676 | rv = gs_wait_tx_flushed (port, MAX_SCHEDULE_TIMEOUT); |
| 677 | if (rv < 0) return /* rv */; |
| 678 | |
| 679 | rv = port->rd->set_real_termios(port); |
| 680 | if (rv < 0) return /* rv */; |
| 681 | |
| 682 | if ((!old_termios || |
| 683 | (old_termios->c_cflag & CRTSCTS)) && |
| 684 | !( tiosp->c_cflag & CRTSCTS)) { |
| 685 | tty->stopped = 0; |
| 686 | gs_start(tty); |
| 687 | } |
| 688 | |
| 689 | #ifdef tytso_patch_94Nov25_1726 |
| 690 | /* This "makes sense", Why is it commented out? */ |
| 691 | |
| 692 | if (!(old_termios->c_cflag & CLOCAL) && |
| 693 | (tty->termios->c_cflag & CLOCAL)) |
| 694 | wake_up_interruptible(&port->gs.open_wait); |
| 695 | #endif |
| 696 | |
| 697 | func_exit(); |
| 698 | return /* 0 */; |
| 699 | } |
| 700 | |
| 701 | |
| 702 | |
| 703 | /* Must be called with interrupts enabled */ |
| 704 | int gs_init_port(struct gs_port *port) |
| 705 | { |
| 706 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | |
| 708 | func_enter (); |
| 709 | |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 710 | if (port->port.flags & ASYNC_INITIALIZED) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | func_exit (); |
| 712 | return 0; |
| 713 | } |
| 714 | if (!port->xmit_buf) { |
| 715 | /* We may sleep in get_zeroed_page() */ |
| 716 | unsigned long tmp; |
| 717 | |
| 718 | tmp = get_zeroed_page(GFP_KERNEL); |
| 719 | spin_lock_irqsave (&port->driver_lock, flags); |
| 720 | if (port->xmit_buf) |
| 721 | free_page (tmp); |
| 722 | else |
| 723 | port->xmit_buf = (unsigned char *) tmp; |
| 724 | spin_unlock_irqrestore(&port->driver_lock, flags); |
| 725 | if (!port->xmit_buf) { |
| 726 | func_exit (); |
| 727 | return -ENOMEM; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | spin_lock_irqsave (&port->driver_lock, flags); |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 732 | if (port->port.tty) |
| 733 | clear_bit(TTY_IO_ERROR, &port->port.tty->flags); |
Ingo Molnar | 81861d7 | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 734 | mutex_init(&port->port_write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | port->xmit_cnt = port->xmit_head = port->xmit_tail = 0; |
| 736 | spin_unlock_irqrestore(&port->driver_lock, flags); |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 737 | gs_set_termios(port->port.tty, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | spin_lock_irqsave (&port->driver_lock, flags); |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 739 | port->port.flags |= ASYNC_INITIALIZED; |
| 740 | port->port.flags &= ~GS_TX_INTEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
| 742 | spin_unlock_irqrestore(&port->driver_lock, flags); |
| 743 | func_exit (); |
| 744 | return 0; |
| 745 | } |
| 746 | |
| 747 | |
| 748 | int gs_setserial(struct gs_port *port, struct serial_struct __user *sp) |
| 749 | { |
| 750 | struct serial_struct sio; |
| 751 | |
| 752 | if (copy_from_user(&sio, sp, sizeof(struct serial_struct))) |
| 753 | return(-EFAULT); |
| 754 | |
| 755 | if (!capable(CAP_SYS_ADMIN)) { |
| 756 | if ((sio.baud_base != port->baud_base) || |
| 757 | (sio.close_delay != port->close_delay) || |
| 758 | ((sio.flags & ~ASYNC_USR_MASK) != |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 759 | (port->port.flags & ~ASYNC_USR_MASK))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | return(-EPERM); |
| 761 | } |
| 762 | |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 763 | port->port.flags = (port->port.flags & ~ASYNC_USR_MASK) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | (sio.flags & ASYNC_USR_MASK); |
| 765 | |
| 766 | port->baud_base = sio.baud_base; |
| 767 | port->close_delay = sio.close_delay; |
| 768 | port->closing_wait = sio.closing_wait; |
| 769 | port->custom_divisor = sio.custom_divisor; |
| 770 | |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 771 | gs_set_termios (port->port.tty, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | |
| 777 | /*****************************************************************************/ |
| 778 | |
| 779 | /* |
| 780 | * Generate the serial struct info. |
| 781 | */ |
| 782 | |
| 783 | int gs_getserial(struct gs_port *port, struct serial_struct __user *sp) |
| 784 | { |
| 785 | struct serial_struct sio; |
| 786 | |
| 787 | memset(&sio, 0, sizeof(struct serial_struct)); |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 788 | sio.flags = port->port.flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | sio.baud_base = port->baud_base; |
| 790 | sio.close_delay = port->close_delay; |
| 791 | sio.closing_wait = port->closing_wait; |
| 792 | sio.custom_divisor = port->custom_divisor; |
| 793 | sio.hub6 = 0; |
| 794 | |
| 795 | /* If you want you can override these. */ |
| 796 | sio.type = PORT_UNKNOWN; |
| 797 | sio.xmit_fifo_size = -1; |
| 798 | sio.line = -1; |
| 799 | sio.port = -1; |
| 800 | sio.irq = -1; |
| 801 | |
| 802 | if (port->rd->getserial) |
| 803 | port->rd->getserial (port, &sio); |
| 804 | |
| 805 | if (copy_to_user(sp, &sio, sizeof(struct serial_struct))) |
| 806 | return -EFAULT; |
| 807 | return 0; |
| 808 | |
| 809 | } |
| 810 | |
| 811 | |
| 812 | void gs_got_break(struct gs_port *port) |
| 813 | { |
| 814 | func_enter (); |
| 815 | |
Alan Cox | b5391e2 | 2008-07-16 21:55:20 +0100 | [diff] [blame] | 816 | tty_insert_flip_char(port->port.tty, 0, TTY_BREAK); |
| 817 | tty_schedule_flip(port->port.tty); |
| 818 | if (port->port.flags & ASYNC_SAK) { |
| 819 | do_SAK (port->port.tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | func_exit (); |
| 823 | } |
| 824 | |
| 825 | |
| 826 | EXPORT_SYMBOL(gs_put_char); |
| 827 | EXPORT_SYMBOL(gs_write); |
| 828 | EXPORT_SYMBOL(gs_write_room); |
| 829 | EXPORT_SYMBOL(gs_chars_in_buffer); |
| 830 | EXPORT_SYMBOL(gs_flush_buffer); |
| 831 | EXPORT_SYMBOL(gs_flush_chars); |
| 832 | EXPORT_SYMBOL(gs_stop); |
| 833 | EXPORT_SYMBOL(gs_start); |
| 834 | EXPORT_SYMBOL(gs_hangup); |
| 835 | EXPORT_SYMBOL(gs_block_til_ready); |
| 836 | EXPORT_SYMBOL(gs_close); |
| 837 | EXPORT_SYMBOL(gs_set_termios); |
| 838 | EXPORT_SYMBOL(gs_init_port); |
| 839 | EXPORT_SYMBOL(gs_setserial); |
| 840 | EXPORT_SYMBOL(gs_getserial); |
| 841 | EXPORT_SYMBOL(gs_got_break); |
| 842 | |
| 843 | MODULE_LICENSE("GPL"); |