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