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