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