ia64/linux-2.6.18-xen.hg

annotate drivers/serial/serial_core.c @ 897:329ea0ccb344

balloon: try harder to balloon up under memory pressure.

Currently if the balloon driver is unable to increase the guest's
reservation it assumes the failure was due to reaching its full
allocation, gives up on the ballooning operation and records the limit
it reached as the "hard limit". The driver will not try again until
the target is set again (even to the same value).

However it is possible that ballooning has in fact failed due to
memory pressure in the host and therefore it is desirable to keep
attempting to reach the target in case memory becomes available. The
most likely scenario is that some guests are ballooning down while
others are ballooning up and therefore there is temporary memory
pressure while things stabilise. You would not expect a well behaved
toolstack to ask a domain to balloon to more than its allocation nor
would you expect it to deliberately over-commit memory by setting
balloon targets which exceed the total host memory.

This patch drops the concept of a hard limit and causes the balloon
driver to retry increasing the reservation on a timer in the same
manner as when decreasing the reservation.

Also if we partially succeed in increasing the reservation
(i.e. receive less pages than we asked for) then we may as well keep
those pages rather than returning them to Xen.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Jun 05 14:01:20 2009 +0100 (2009-06-05)
parents 3e8752eb6d9c
children
rev   line source
ian@0 1 /*
ian@0 2 * linux/drivers/char/core.c
ian@0 3 *
ian@0 4 * Driver core for serial ports
ian@0 5 *
ian@0 6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
ian@0 7 *
ian@0 8 * Copyright 1999 ARM Limited
ian@0 9 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
ian@0 10 *
ian@0 11 * This program is free software; you can redistribute it and/or modify
ian@0 12 * it under the terms of the GNU General Public License as published by
ian@0 13 * the Free Software Foundation; either version 2 of the License, or
ian@0 14 * (at your option) any later version.
ian@0 15 *
ian@0 16 * This program is distributed in the hope that it will be useful,
ian@0 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ian@0 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ian@0 19 * GNU General Public License for more details.
ian@0 20 *
ian@0 21 * You should have received a copy of the GNU General Public License
ian@0 22 * along with this program; if not, write to the Free Software
ian@0 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
ian@0 24 */
ian@0 25 #include <linux/module.h>
ian@0 26 #include <linux/tty.h>
ian@0 27 #include <linux/slab.h>
ian@0 28 #include <linux/init.h>
ian@0 29 #include <linux/console.h>
ian@0 30 #include <linux/serial_core.h>
ian@0 31 #include <linux/smp_lock.h>
ian@0 32 #include <linux/device.h>
ian@0 33 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
ian@0 34 #include <linux/delay.h>
ian@0 35 #include <linux/mutex.h>
ian@0 36
ian@0 37 #include <asm/irq.h>
ian@0 38 #include <asm/uaccess.h>
ian@0 39
ian@0 40 #undef DEBUG
ian@0 41 #ifdef DEBUG
ian@0 42 #define DPRINTK(x...) printk(x)
ian@0 43 #else
ian@0 44 #define DPRINTK(x...) do { } while (0)
ian@0 45 #endif
ian@0 46
ian@0 47 /*
ian@0 48 * This is used to lock changes in serial line configuration.
ian@0 49 */
ian@0 50 static DEFINE_MUTEX(port_mutex);
ian@0 51
ian@0 52 /*
ian@0 53 * lockdep: port->lock is initialized in two places, but we
ian@0 54 * want only one lock-class:
ian@0 55 */
ian@0 56 static struct lock_class_key port_lock_key;
ian@0 57
ian@0 58 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
ian@0 59
ian@0 60 #define uart_users(state) ((state)->count + ((state)->info ? (state)->info->blocked_open : 0))
ian@0 61
ian@0 62 #ifdef CONFIG_SERIAL_CORE_CONSOLE
ian@0 63 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
ian@0 64 #else
ian@0 65 #define uart_console(port) (0)
ian@0 66 #endif
ian@0 67
ian@0 68 static void uart_change_speed(struct uart_state *state, struct termios *old_termios);
ian@0 69 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
ian@0 70 static void uart_change_pm(struct uart_state *state, int pm_state);
ian@0 71
ian@0 72 /*
ian@0 73 * This routine is used by the interrupt handler to schedule processing in
ian@0 74 * the software interrupt portion of the driver.
ian@0 75 */
ian@0 76 void uart_write_wakeup(struct uart_port *port)
ian@0 77 {
ian@0 78 struct uart_info *info = port->info;
ian@0 79 /*
ian@0 80 * This means you called this function _after_ the port was
ian@0 81 * closed. No cookie for you.
ian@0 82 */
ian@0 83 BUG_ON(!info);
ian@0 84 tasklet_schedule(&info->tlet);
ian@0 85 }
ian@0 86
ian@0 87 static void uart_stop(struct tty_struct *tty)
ian@0 88 {
ian@0 89 struct uart_state *state = tty->driver_data;
ian@0 90 struct uart_port *port = state->port;
ian@0 91 unsigned long flags;
ian@0 92
ian@0 93 spin_lock_irqsave(&port->lock, flags);
ian@0 94 port->ops->stop_tx(port);
ian@0 95 spin_unlock_irqrestore(&port->lock, flags);
ian@0 96 }
ian@0 97
ian@0 98 static void __uart_start(struct tty_struct *tty)
ian@0 99 {
ian@0 100 struct uart_state *state = tty->driver_data;
ian@0 101 struct uart_port *port = state->port;
ian@0 102
ian@0 103 if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&
ian@0 104 !tty->stopped && !tty->hw_stopped)
ian@0 105 port->ops->start_tx(port);
ian@0 106 }
ian@0 107
ian@0 108 static void uart_start(struct tty_struct *tty)
ian@0 109 {
ian@0 110 struct uart_state *state = tty->driver_data;
ian@0 111 struct uart_port *port = state->port;
ian@0 112 unsigned long flags;
ian@0 113
ian@0 114 spin_lock_irqsave(&port->lock, flags);
ian@0 115 __uart_start(tty);
ian@0 116 spin_unlock_irqrestore(&port->lock, flags);
ian@0 117 }
ian@0 118
ian@0 119 static void uart_tasklet_action(unsigned long data)
ian@0 120 {
ian@0 121 struct uart_state *state = (struct uart_state *)data;
ian@0 122 tty_wakeup(state->info->tty);
ian@0 123 }
ian@0 124
ian@0 125 static inline void
ian@0 126 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
ian@0 127 {
ian@0 128 unsigned long flags;
ian@0 129 unsigned int old;
ian@0 130
ian@0 131 spin_lock_irqsave(&port->lock, flags);
ian@0 132 old = port->mctrl;
ian@0 133 port->mctrl = (old & ~clear) | set;
ian@0 134 if (old != port->mctrl)
ian@0 135 port->ops->set_mctrl(port, port->mctrl);
ian@0 136 spin_unlock_irqrestore(&port->lock, flags);
ian@0 137 }
ian@0 138
ian@0 139 #define uart_set_mctrl(port,set) uart_update_mctrl(port,set,0)
ian@0 140 #define uart_clear_mctrl(port,clear) uart_update_mctrl(port,0,clear)
ian@0 141
ian@0 142 /*
ian@0 143 * Startup the port. This will be called once per open. All calls
ian@0 144 * will be serialised by the per-port semaphore.
ian@0 145 */
ian@0 146 static int uart_startup(struct uart_state *state, int init_hw)
ian@0 147 {
ian@0 148 struct uart_info *info = state->info;
ian@0 149 struct uart_port *port = state->port;
ian@0 150 unsigned long page;
ian@0 151 int retval = 0;
ian@0 152
ian@0 153 if (info->flags & UIF_INITIALIZED)
ian@0 154 return 0;
ian@0 155
ian@0 156 /*
ian@0 157 * Set the TTY IO error marker - we will only clear this
ian@0 158 * once we have successfully opened the port. Also set
ian@0 159 * up the tty->alt_speed kludge
ian@0 160 */
ian@0 161 set_bit(TTY_IO_ERROR, &info->tty->flags);
ian@0 162
ian@0 163 if (port->type == PORT_UNKNOWN)
ian@0 164 return 0;
ian@0 165
ian@0 166 /*
ian@0 167 * Initialise and allocate the transmit and temporary
ian@0 168 * buffer.
ian@0 169 */
ian@0 170 if (!info->xmit.buf) {
ian@0 171 page = get_zeroed_page(GFP_KERNEL);
ian@0 172 if (!page)
ian@0 173 return -ENOMEM;
ian@0 174
ian@0 175 info->xmit.buf = (unsigned char *) page;
ian@0 176 uart_circ_clear(&info->xmit);
ian@0 177 }
ian@0 178
ian@0 179 retval = port->ops->startup(port);
ian@0 180 if (retval == 0) {
ian@0 181 if (init_hw) {
ian@0 182 /*
ian@0 183 * Initialise the hardware port settings.
ian@0 184 */
ian@0 185 uart_change_speed(state, NULL);
ian@0 186
ian@0 187 /*
ian@0 188 * Setup the RTS and DTR signals once the
ian@0 189 * port is open and ready to respond.
ian@0 190 */
ian@0 191 if (info->tty->termios->c_cflag & CBAUD)
ian@0 192 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
ian@0 193 }
ian@0 194
ian@0 195 if (info->flags & UIF_CTS_FLOW) {
ian@0 196 spin_lock_irq(&port->lock);
ian@0 197 if (!(port->ops->get_mctrl(port) & TIOCM_CTS))
ian@0 198 info->tty->hw_stopped = 1;
ian@0 199 spin_unlock_irq(&port->lock);
ian@0 200 }
ian@0 201
ian@0 202 info->flags |= UIF_INITIALIZED;
ian@0 203
ian@0 204 clear_bit(TTY_IO_ERROR, &info->tty->flags);
ian@0 205 }
ian@0 206
ian@0 207 if (retval && capable(CAP_SYS_ADMIN))
ian@0 208 retval = 0;
ian@0 209
ian@0 210 return retval;
ian@0 211 }
ian@0 212
ian@0 213 /*
ian@0 214 * This routine will shutdown a serial port; interrupts are disabled, and
ian@0 215 * DTR is dropped if the hangup on close termio flag is on. Calls to
ian@0 216 * uart_shutdown are serialised by the per-port semaphore.
ian@0 217 */
ian@0 218 static void uart_shutdown(struct uart_state *state)
ian@0 219 {
ian@0 220 struct uart_info *info = state->info;
ian@0 221 struct uart_port *port = state->port;
ian@0 222
ian@0 223 /*
ian@0 224 * Set the TTY IO error marker
ian@0 225 */
ian@0 226 if (info->tty)
ian@0 227 set_bit(TTY_IO_ERROR, &info->tty->flags);
ian@0 228
ian@0 229 if (info->flags & UIF_INITIALIZED) {
ian@0 230 info->flags &= ~UIF_INITIALIZED;
ian@0 231
ian@0 232 /*
ian@0 233 * Turn off DTR and RTS early.
ian@0 234 */
ian@0 235 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
ian@0 236 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
ian@0 237
ian@0 238 /*
ian@0 239 * clear delta_msr_wait queue to avoid mem leaks: we may free
ian@0 240 * the irq here so the queue might never be woken up. Note
ian@0 241 * that we won't end up waiting on delta_msr_wait again since
ian@0 242 * any outstanding file descriptors should be pointing at
ian@0 243 * hung_up_tty_fops now.
ian@0 244 */
ian@0 245 wake_up_interruptible(&info->delta_msr_wait);
ian@0 246
ian@0 247 /*
ian@0 248 * Free the IRQ and disable the port.
ian@0 249 */
ian@0 250 port->ops->shutdown(port);
ian@0 251
ian@0 252 /*
ian@0 253 * Ensure that the IRQ handler isn't running on another CPU.
ian@0 254 */
ian@0 255 synchronize_irq(port->irq);
ian@0 256 }
ian@0 257
ian@0 258 /*
ian@0 259 * kill off our tasklet
ian@0 260 */
ian@0 261 tasklet_kill(&info->tlet);
ian@0 262
ian@0 263 /*
ian@0 264 * Free the transmit buffer page.
ian@0 265 */
ian@0 266 if (info->xmit.buf) {
ian@0 267 free_page((unsigned long)info->xmit.buf);
ian@0 268 info->xmit.buf = NULL;
ian@0 269 }
ian@0 270 }
ian@0 271
ian@0 272 /**
ian@0 273 * uart_update_timeout - update per-port FIFO timeout.
ian@0 274 * @port: uart_port structure describing the port
ian@0 275 * @cflag: termios cflag value
ian@0 276 * @baud: speed of the port
ian@0 277 *
ian@0 278 * Set the port FIFO timeout value. The @cflag value should
ian@0 279 * reflect the actual hardware settings.
ian@0 280 */
ian@0 281 void
ian@0 282 uart_update_timeout(struct uart_port *port, unsigned int cflag,
ian@0 283 unsigned int baud)
ian@0 284 {
ian@0 285 unsigned int bits;
ian@0 286
ian@0 287 /* byte size and parity */
ian@0 288 switch (cflag & CSIZE) {
ian@0 289 case CS5:
ian@0 290 bits = 7;
ian@0 291 break;
ian@0 292 case CS6:
ian@0 293 bits = 8;
ian@0 294 break;
ian@0 295 case CS7:
ian@0 296 bits = 9;
ian@0 297 break;
ian@0 298 default:
ian@0 299 bits = 10;
ian@0 300 break; // CS8
ian@0 301 }
ian@0 302
ian@0 303 if (cflag & CSTOPB)
ian@0 304 bits++;
ian@0 305 if (cflag & PARENB)
ian@0 306 bits++;
ian@0 307
ian@0 308 /*
ian@0 309 * The total number of bits to be transmitted in the fifo.
ian@0 310 */
ian@0 311 bits = bits * port->fifosize;
ian@0 312
ian@0 313 /*
ian@0 314 * Figure the timeout to send the above number of bits.
ian@0 315 * Add .02 seconds of slop
ian@0 316 */
ian@0 317 port->timeout = (HZ * bits) / baud + HZ/50;
ian@0 318 }
ian@0 319
ian@0 320 EXPORT_SYMBOL(uart_update_timeout);
ian@0 321
ian@0 322 /**
ian@0 323 * uart_get_baud_rate - return baud rate for a particular port
ian@0 324 * @port: uart_port structure describing the port in question.
ian@0 325 * @termios: desired termios settings.
ian@0 326 * @old: old termios (or NULL)
ian@0 327 * @min: minimum acceptable baud rate
ian@0 328 * @max: maximum acceptable baud rate
ian@0 329 *
ian@0 330 * Decode the termios structure into a numeric baud rate,
ian@0 331 * taking account of the magic 38400 baud rate (with spd_*
ian@0 332 * flags), and mapping the %B0 rate to 9600 baud.
ian@0 333 *
ian@0 334 * If the new baud rate is invalid, try the old termios setting.
ian@0 335 * If it's still invalid, we try 9600 baud.
ian@0 336 *
ian@0 337 * Update the @termios structure to reflect the baud rate
ian@0 338 * we're actually going to be using.
ian@0 339 */
ian@0 340 unsigned int
ian@0 341 uart_get_baud_rate(struct uart_port *port, struct termios *termios,
ian@0 342 struct termios *old, unsigned int min, unsigned int max)
ian@0 343 {
ian@0 344 unsigned int try, baud, altbaud = 38400;
ian@0 345 upf_t flags = port->flags & UPF_SPD_MASK;
ian@0 346
ian@0 347 if (flags == UPF_SPD_HI)
ian@0 348 altbaud = 57600;
ian@0 349 if (flags == UPF_SPD_VHI)
ian@0 350 altbaud = 115200;
ian@0 351 if (flags == UPF_SPD_SHI)
ian@0 352 altbaud = 230400;
ian@0 353 if (flags == UPF_SPD_WARP)
ian@0 354 altbaud = 460800;
ian@0 355
ian@0 356 for (try = 0; try < 2; try++) {
ian@0 357 baud = tty_termios_baud_rate(termios);
ian@0 358
ian@0 359 /*
ian@0 360 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
ian@0 361 * Die! Die! Die!
ian@0 362 */
ian@0 363 if (baud == 38400)
ian@0 364 baud = altbaud;
ian@0 365
ian@0 366 /*
ian@0 367 * Special case: B0 rate.
ian@0 368 */
ian@0 369 if (baud == 0)
ian@0 370 baud = 9600;
ian@0 371
ian@0 372 if (baud >= min && baud <= max)
ian@0 373 return baud;
ian@0 374
ian@0 375 /*
ian@0 376 * Oops, the quotient was zero. Try again with
ian@0 377 * the old baud rate if possible.
ian@0 378 */
ian@0 379 termios->c_cflag &= ~CBAUD;
ian@0 380 if (old) {
ian@0 381 termios->c_cflag |= old->c_cflag & CBAUD;
ian@0 382 old = NULL;
ian@0 383 continue;
ian@0 384 }
ian@0 385
ian@0 386 /*
ian@0 387 * As a last resort, if the quotient is zero,
ian@0 388 * default to 9600 bps
ian@0 389 */
ian@0 390 termios->c_cflag |= B9600;
ian@0 391 }
ian@0 392
ian@0 393 return 0;
ian@0 394 }
ian@0 395
ian@0 396 EXPORT_SYMBOL(uart_get_baud_rate);
ian@0 397
ian@0 398 /**
ian@0 399 * uart_get_divisor - return uart clock divisor
ian@0 400 * @port: uart_port structure describing the port.
ian@0 401 * @baud: desired baud rate
ian@0 402 *
ian@0 403 * Calculate the uart clock divisor for the port.
ian@0 404 */
ian@0 405 unsigned int
ian@0 406 uart_get_divisor(struct uart_port *port, unsigned int baud)
ian@0 407 {
ian@0 408 unsigned int quot;
ian@0 409
ian@0 410 /*
ian@0 411 * Old custom speed handling.
ian@0 412 */
ian@0 413 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
ian@0 414 quot = port->custom_divisor;
ian@0 415 else
ian@0 416 quot = (port->uartclk + (8 * baud)) / (16 * baud);
ian@0 417
ian@0 418 return quot;
ian@0 419 }
ian@0 420
ian@0 421 EXPORT_SYMBOL(uart_get_divisor);
ian@0 422
ian@0 423 static void
ian@0 424 uart_change_speed(struct uart_state *state, struct termios *old_termios)
ian@0 425 {
ian@0 426 struct tty_struct *tty = state->info->tty;
ian@0 427 struct uart_port *port = state->port;
ian@0 428 struct termios *termios;
ian@0 429
ian@0 430 /*
ian@0 431 * If we have no tty, termios, or the port does not exist,
ian@0 432 * then we can't set the parameters for this port.
ian@0 433 */
ian@0 434 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
ian@0 435 return;
ian@0 436
ian@0 437 termios = tty->termios;
ian@0 438
ian@0 439 /*
ian@0 440 * Set flags based on termios cflag
ian@0 441 */
ian@0 442 if (termios->c_cflag & CRTSCTS)
ian@0 443 state->info->flags |= UIF_CTS_FLOW;
ian@0 444 else
ian@0 445 state->info->flags &= ~UIF_CTS_FLOW;
ian@0 446
ian@0 447 if (termios->c_cflag & CLOCAL)
ian@0 448 state->info->flags &= ~UIF_CHECK_CD;
ian@0 449 else
ian@0 450 state->info->flags |= UIF_CHECK_CD;
ian@0 451
ian@0 452 port->ops->set_termios(port, termios, old_termios);
ian@0 453 }
ian@0 454
ian@0 455 static inline void
ian@0 456 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
ian@0 457 {
ian@0 458 unsigned long flags;
ian@0 459
ian@0 460 if (!circ->buf)
ian@0 461 return;
ian@0 462
ian@0 463 spin_lock_irqsave(&port->lock, flags);
ian@0 464 if (uart_circ_chars_free(circ) != 0) {
ian@0 465 circ->buf[circ->head] = c;
ian@0 466 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
ian@0 467 }
ian@0 468 spin_unlock_irqrestore(&port->lock, flags);
ian@0 469 }
ian@0 470
ian@0 471 static void uart_put_char(struct tty_struct *tty, unsigned char ch)
ian@0 472 {
ian@0 473 struct uart_state *state = tty->driver_data;
ian@0 474
ian@0 475 __uart_put_char(state->port, &state->info->xmit, ch);
ian@0 476 }
ian@0 477
ian@0 478 static void uart_flush_chars(struct tty_struct *tty)
ian@0 479 {
ian@0 480 uart_start(tty);
ian@0 481 }
ian@0 482
ian@0 483 static int
ian@0 484 uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
ian@0 485 {
ian@0 486 struct uart_state *state = tty->driver_data;
ian@0 487 struct uart_port *port;
ian@0 488 struct circ_buf *circ;
ian@0 489 unsigned long flags;
ian@0 490 int c, ret = 0;
ian@0 491
ian@0 492 /*
ian@0 493 * This means you called this function _after_ the port was
ian@0 494 * closed. No cookie for you.
ian@0 495 */
ian@0 496 if (!state || !state->info) {
ian@0 497 WARN_ON(1);
ian@0 498 return -EL3HLT;
ian@0 499 }
ian@0 500
ian@0 501 port = state->port;
ian@0 502 circ = &state->info->xmit;
ian@0 503
ian@0 504 if (!circ->buf)
ian@0 505 return 0;
ian@0 506
ian@0 507 spin_lock_irqsave(&port->lock, flags);
ian@0 508 while (1) {
ian@0 509 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
ian@0 510 if (count < c)
ian@0 511 c = count;
ian@0 512 if (c <= 0)
ian@0 513 break;
ian@0 514 memcpy(circ->buf + circ->head, buf, c);
ian@0 515 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
ian@0 516 buf += c;
ian@0 517 count -= c;
ian@0 518 ret += c;
ian@0 519 }
ian@0 520 spin_unlock_irqrestore(&port->lock, flags);
ian@0 521
ian@0 522 uart_start(tty);
ian@0 523 return ret;
ian@0 524 }
ian@0 525
ian@0 526 static int uart_write_room(struct tty_struct *tty)
ian@0 527 {
ian@0 528 struct uart_state *state = tty->driver_data;
ian@0 529
ian@0 530 return uart_circ_chars_free(&state->info->xmit);
ian@0 531 }
ian@0 532
ian@0 533 static int uart_chars_in_buffer(struct tty_struct *tty)
ian@0 534 {
ian@0 535 struct uart_state *state = tty->driver_data;
ian@0 536
ian@0 537 return uart_circ_chars_pending(&state->info->xmit);
ian@0 538 }
ian@0 539
ian@0 540 static void uart_flush_buffer(struct tty_struct *tty)
ian@0 541 {
ian@0 542 struct uart_state *state = tty->driver_data;
ian@0 543 struct uart_port *port = state->port;
ian@0 544 unsigned long flags;
ian@0 545
ian@0 546 /*
ian@0 547 * This means you called this function _after_ the port was
ian@0 548 * closed. No cookie for you.
ian@0 549 */
ian@0 550 if (!state || !state->info) {
ian@0 551 WARN_ON(1);
ian@0 552 return;
ian@0 553 }
ian@0 554
ian@0 555 DPRINTK("uart_flush_buffer(%d) called\n", tty->index);
ian@0 556
ian@0 557 spin_lock_irqsave(&port->lock, flags);
ian@0 558 uart_circ_clear(&state->info->xmit);
ian@0 559 spin_unlock_irqrestore(&port->lock, flags);
ian@0 560 tty_wakeup(tty);
ian@0 561 }
ian@0 562
ian@0 563 /*
ian@0 564 * This function is used to send a high-priority XON/XOFF character to
ian@0 565 * the device
ian@0 566 */
ian@0 567 static void uart_send_xchar(struct tty_struct *tty, char ch)
ian@0 568 {
ian@0 569 struct uart_state *state = tty->driver_data;
ian@0 570 struct uart_port *port = state->port;
ian@0 571 unsigned long flags;
ian@0 572
ian@0 573 if (port->ops->send_xchar)
ian@0 574 port->ops->send_xchar(port, ch);
ian@0 575 else {
ian@0 576 port->x_char = ch;
ian@0 577 if (ch) {
ian@0 578 spin_lock_irqsave(&port->lock, flags);
ian@0 579 port->ops->start_tx(port);
ian@0 580 spin_unlock_irqrestore(&port->lock, flags);
ian@0 581 }
ian@0 582 }
ian@0 583 }
ian@0 584
ian@0 585 static void uart_throttle(struct tty_struct *tty)
ian@0 586 {
ian@0 587 struct uart_state *state = tty->driver_data;
ian@0 588
ian@0 589 if (I_IXOFF(tty))
ian@0 590 uart_send_xchar(tty, STOP_CHAR(tty));
ian@0 591
ian@0 592 if (tty->termios->c_cflag & CRTSCTS)
ian@0 593 uart_clear_mctrl(state->port, TIOCM_RTS);
ian@0 594 }
ian@0 595
ian@0 596 static void uart_unthrottle(struct tty_struct *tty)
ian@0 597 {
ian@0 598 struct uart_state *state = tty->driver_data;
ian@0 599 struct uart_port *port = state->port;
ian@0 600
ian@0 601 if (I_IXOFF(tty)) {
ian@0 602 if (port->x_char)
ian@0 603 port->x_char = 0;
ian@0 604 else
ian@0 605 uart_send_xchar(tty, START_CHAR(tty));
ian@0 606 }
ian@0 607
ian@0 608 if (tty->termios->c_cflag & CRTSCTS)
ian@0 609 uart_set_mctrl(port, TIOCM_RTS);
ian@0 610 }
ian@0 611
ian@0 612 static int uart_get_info(struct uart_state *state,
ian@0 613 struct serial_struct __user *retinfo)
ian@0 614 {
ian@0 615 struct uart_port *port = state->port;
ian@0 616 struct serial_struct tmp;
ian@0 617
ian@0 618 memset(&tmp, 0, sizeof(tmp));
ian@0 619 tmp.type = port->type;
ian@0 620 tmp.line = port->line;
ian@0 621 tmp.port = port->iobase;
ian@0 622 if (HIGH_BITS_OFFSET)
ian@0 623 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
ian@0 624 tmp.irq = port->irq;
ian@0 625 tmp.flags = port->flags;
ian@0 626 tmp.xmit_fifo_size = port->fifosize;
ian@0 627 tmp.baud_base = port->uartclk / 16;
ian@0 628 tmp.close_delay = state->close_delay / 10;
ian@0 629 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
ian@0 630 ASYNC_CLOSING_WAIT_NONE :
ian@0 631 state->closing_wait / 10;
ian@0 632 tmp.custom_divisor = port->custom_divisor;
ian@0 633 tmp.hub6 = port->hub6;
ian@0 634 tmp.io_type = port->iotype;
ian@0 635 tmp.iomem_reg_shift = port->regshift;
ian@0 636 tmp.iomem_base = (void *)port->mapbase;
ian@0 637
ian@0 638 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
ian@0 639 return -EFAULT;
ian@0 640 return 0;
ian@0 641 }
ian@0 642
ian@0 643 static int uart_set_info(struct uart_state *state,
ian@0 644 struct serial_struct __user *newinfo)
ian@0 645 {
ian@0 646 struct serial_struct new_serial;
ian@0 647 struct uart_port *port = state->port;
ian@0 648 unsigned long new_port;
ian@0 649 unsigned int change_irq, change_port, closing_wait;
ian@0 650 unsigned int old_custom_divisor, close_delay;
ian@0 651 upf_t old_flags, new_flags;
ian@0 652 int retval = 0;
ian@0 653
ian@0 654 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
ian@0 655 return -EFAULT;
ian@0 656
ian@0 657 new_port = new_serial.port;
ian@0 658 if (HIGH_BITS_OFFSET)
ian@0 659 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
ian@0 660
ian@0 661 new_serial.irq = irq_canonicalize(new_serial.irq);
ian@0 662 close_delay = new_serial.close_delay * 10;
ian@0 663 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
ian@0 664 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
ian@0 665
ian@0 666 /*
ian@0 667 * This semaphore protects state->count. It is also
ian@0 668 * very useful to prevent opens. Also, take the
ian@0 669 * port configuration semaphore to make sure that a
ian@0 670 * module insertion/removal doesn't change anything
ian@0 671 * under us.
ian@0 672 */
ian@0 673 mutex_lock(&state->mutex);
ian@0 674
ian@0 675 change_irq = new_serial.irq != port->irq;
ian@0 676
ian@0 677 /*
ian@0 678 * Since changing the 'type' of the port changes its resource
ian@0 679 * allocations, we should treat type changes the same as
ian@0 680 * IO port changes.
ian@0 681 */
ian@0 682 change_port = new_port != port->iobase ||
ian@0 683 (unsigned long)new_serial.iomem_base != port->mapbase ||
ian@0 684 new_serial.hub6 != port->hub6 ||
ian@0 685 new_serial.io_type != port->iotype ||
ian@0 686 new_serial.iomem_reg_shift != port->regshift ||
ian@0 687 new_serial.type != port->type;
ian@0 688
ian@0 689 old_flags = port->flags;
ian@0 690 new_flags = new_serial.flags;
ian@0 691 old_custom_divisor = port->custom_divisor;
ian@0 692
ian@0 693 if (!capable(CAP_SYS_ADMIN)) {
ian@0 694 retval = -EPERM;
ian@0 695 if (change_irq || change_port ||
ian@0 696 (new_serial.baud_base != port->uartclk / 16) ||
ian@0 697 (close_delay != state->close_delay) ||
ian@0 698 (closing_wait != state->closing_wait) ||
ian@0 699 (new_serial.xmit_fifo_size &&
ian@0 700 new_serial.xmit_fifo_size != port->fifosize) ||
ian@0 701 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
ian@0 702 goto exit;
ian@0 703 port->flags = ((port->flags & ~UPF_USR_MASK) |
ian@0 704 (new_flags & UPF_USR_MASK));
ian@0 705 port->custom_divisor = new_serial.custom_divisor;
ian@0 706 goto check_and_exit;
ian@0 707 }
ian@0 708
ian@0 709 /*
ian@0 710 * Ask the low level driver to verify the settings.
ian@0 711 */
ian@0 712 if (port->ops->verify_port)
ian@0 713 retval = port->ops->verify_port(port, &new_serial);
ian@0 714
ian@0 715 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
ian@0 716 (new_serial.baud_base < 9600))
ian@0 717 retval = -EINVAL;
ian@0 718
ian@0 719 if (retval)
ian@0 720 goto exit;
ian@0 721
ian@0 722 if (change_port || change_irq) {
ian@0 723 retval = -EBUSY;
ian@0 724
ian@0 725 /*
ian@0 726 * Make sure that we are the sole user of this port.
ian@0 727 */
ian@0 728 if (uart_users(state) > 1)
ian@0 729 goto exit;
ian@0 730
ian@0 731 /*
ian@0 732 * We need to shutdown the serial port at the old
ian@0 733 * port/type/irq combination.
ian@0 734 */
ian@0 735 uart_shutdown(state);
ian@0 736 }
ian@0 737
ian@0 738 if (change_port) {
ian@0 739 unsigned long old_iobase, old_mapbase;
ian@0 740 unsigned int old_type, old_iotype, old_hub6, old_shift;
ian@0 741
ian@0 742 old_iobase = port->iobase;
ian@0 743 old_mapbase = port->mapbase;
ian@0 744 old_type = port->type;
ian@0 745 old_hub6 = port->hub6;
ian@0 746 old_iotype = port->iotype;
ian@0 747 old_shift = port->regshift;
ian@0 748
ian@0 749 /*
ian@0 750 * Free and release old regions
ian@0 751 */
ian@0 752 if (old_type != PORT_UNKNOWN)
ian@0 753 port->ops->release_port(port);
ian@0 754
ian@0 755 port->iobase = new_port;
ian@0 756 port->type = new_serial.type;
ian@0 757 port->hub6 = new_serial.hub6;
ian@0 758 port->iotype = new_serial.io_type;
ian@0 759 port->regshift = new_serial.iomem_reg_shift;
ian@0 760 port->mapbase = (unsigned long)new_serial.iomem_base;
ian@0 761
ian@0 762 /*
ian@0 763 * Claim and map the new regions
ian@0 764 */
ian@0 765 if (port->type != PORT_UNKNOWN) {
ian@0 766 retval = port->ops->request_port(port);
ian@0 767 } else {
ian@0 768 /* Always success - Jean II */
ian@0 769 retval = 0;
ian@0 770 }
ian@0 771
ian@0 772 /*
ian@0 773 * If we fail to request resources for the
ian@0 774 * new port, try to restore the old settings.
ian@0 775 */
ian@0 776 if (retval && old_type != PORT_UNKNOWN) {
ian@0 777 port->iobase = old_iobase;
ian@0 778 port->type = old_type;
ian@0 779 port->hub6 = old_hub6;
ian@0 780 port->iotype = old_iotype;
ian@0 781 port->regshift = old_shift;
ian@0 782 port->mapbase = old_mapbase;
ian@0 783 retval = port->ops->request_port(port);
ian@0 784 /*
ian@0 785 * If we failed to restore the old settings,
ian@0 786 * we fail like this.
ian@0 787 */
ian@0 788 if (retval)
ian@0 789 port->type = PORT_UNKNOWN;
ian@0 790
ian@0 791 /*
ian@0 792 * We failed anyway.
ian@0 793 */
ian@0 794 retval = -EBUSY;
ian@0 795 }
ian@0 796 }
ian@0 797
ian@0 798 port->irq = new_serial.irq;
ian@0 799 port->uartclk = new_serial.baud_base * 16;
ian@0 800 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
ian@0 801 (new_flags & UPF_CHANGE_MASK);
ian@0 802 port->custom_divisor = new_serial.custom_divisor;
ian@0 803 state->close_delay = close_delay;
ian@0 804 state->closing_wait = closing_wait;
ian@0 805 if (new_serial.xmit_fifo_size)
ian@0 806 port->fifosize = new_serial.xmit_fifo_size;
ian@0 807 if (state->info->tty)
ian@0 808 state->info->tty->low_latency =
ian@0 809 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
ian@0 810
ian@0 811 check_and_exit:
ian@0 812 retval = 0;
ian@0 813 if (port->type == PORT_UNKNOWN)
ian@0 814 goto exit;
ian@0 815 if (state->info->flags & UIF_INITIALIZED) {
ian@0 816 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
ian@0 817 old_custom_divisor != port->custom_divisor) {
ian@0 818 /*
ian@0 819 * If they're setting up a custom divisor or speed,
ian@0 820 * instead of clearing it, then bitch about it. No
ian@0 821 * need to rate-limit; it's CAP_SYS_ADMIN only.
ian@0 822 */
ian@0 823 if (port->flags & UPF_SPD_MASK) {
ian@0 824 char buf[64];
ian@0 825 printk(KERN_NOTICE
ian@0 826 "%s sets custom speed on %s. This "
ian@0 827 "is deprecated.\n", current->comm,
ian@0 828 tty_name(state->info->tty, buf));
ian@0 829 }
ian@0 830 uart_change_speed(state, NULL);
ian@0 831 }
ian@0 832 } else
ian@0 833 retval = uart_startup(state, 1);
ian@0 834 exit:
ian@0 835 mutex_unlock(&state->mutex);
ian@0 836 return retval;
ian@0 837 }
ian@0 838
ian@0 839
ian@0 840 /*
ian@0 841 * uart_get_lsr_info - get line status register info.
ian@0 842 * Note: uart_ioctl protects us against hangups.
ian@0 843 */
ian@0 844 static int uart_get_lsr_info(struct uart_state *state,
ian@0 845 unsigned int __user *value)
ian@0 846 {
ian@0 847 struct uart_port *port = state->port;
ian@0 848 unsigned int result;
ian@0 849
ian@0 850 result = port->ops->tx_empty(port);
ian@0 851
ian@0 852 /*
ian@0 853 * If we're about to load something into the transmit
ian@0 854 * register, we'll pretend the transmitter isn't empty to
ian@0 855 * avoid a race condition (depending on when the transmit
ian@0 856 * interrupt happens).
ian@0 857 */
ian@0 858 if (port->x_char ||
ian@0 859 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
ian@0 860 !state->info->tty->stopped && !state->info->tty->hw_stopped))
ian@0 861 result &= ~TIOCSER_TEMT;
ian@0 862
ian@0 863 return put_user(result, value);
ian@0 864 }
ian@0 865
ian@0 866 static int uart_tiocmget(struct tty_struct *tty, struct file *file)
ian@0 867 {
ian@0 868 struct uart_state *state = tty->driver_data;
ian@0 869 struct uart_port *port = state->port;
ian@0 870 int result = -EIO;
ian@0 871
ian@0 872 mutex_lock(&state->mutex);
ian@0 873 if ((!file || !tty_hung_up_p(file)) &&
ian@0 874 !(tty->flags & (1 << TTY_IO_ERROR))) {
ian@0 875 result = port->mctrl;
ian@0 876
ian@0 877 spin_lock_irq(&port->lock);
ian@0 878 result |= port->ops->get_mctrl(port);
ian@0 879 spin_unlock_irq(&port->lock);
ian@0 880 }
ian@0 881 mutex_unlock(&state->mutex);
ian@0 882
ian@0 883 return result;
ian@0 884 }
ian@0 885
ian@0 886 static int
ian@0 887 uart_tiocmset(struct tty_struct *tty, struct file *file,
ian@0 888 unsigned int set, unsigned int clear)
ian@0 889 {
ian@0 890 struct uart_state *state = tty->driver_data;
ian@0 891 struct uart_port *port = state->port;
ian@0 892 int ret = -EIO;
ian@0 893
ian@0 894 mutex_lock(&state->mutex);
ian@0 895 if ((!file || !tty_hung_up_p(file)) &&
ian@0 896 !(tty->flags & (1 << TTY_IO_ERROR))) {
ian@0 897 uart_update_mctrl(port, set, clear);
ian@0 898 ret = 0;
ian@0 899 }
ian@0 900 mutex_unlock(&state->mutex);
ian@0 901 return ret;
ian@0 902 }
ian@0 903
ian@0 904 static void uart_break_ctl(struct tty_struct *tty, int break_state)
ian@0 905 {
ian@0 906 struct uart_state *state = tty->driver_data;
ian@0 907 struct uart_port *port = state->port;
ian@0 908
ian@0 909 BUG_ON(!kernel_locked());
ian@0 910
ian@0 911 mutex_lock(&state->mutex);
ian@0 912
ian@0 913 if (port->type != PORT_UNKNOWN)
ian@0 914 port->ops->break_ctl(port, break_state);
ian@0 915
ian@0 916 mutex_unlock(&state->mutex);
ian@0 917 }
ian@0 918
ian@0 919 static int uart_do_autoconfig(struct uart_state *state)
ian@0 920 {
ian@0 921 struct uart_port *port = state->port;
ian@0 922 int flags, ret;
ian@0 923
ian@0 924 if (!capable(CAP_SYS_ADMIN))
ian@0 925 return -EPERM;
ian@0 926
ian@0 927 /*
ian@0 928 * Take the per-port semaphore. This prevents count from
ian@0 929 * changing, and hence any extra opens of the port while
ian@0 930 * we're auto-configuring.
ian@0 931 */
ian@0 932 if (mutex_lock_interruptible(&state->mutex))
ian@0 933 return -ERESTARTSYS;
ian@0 934
ian@0 935 ret = -EBUSY;
ian@0 936 if (uart_users(state) == 1) {
ian@0 937 uart_shutdown(state);
ian@0 938
ian@0 939 /*
ian@0 940 * If we already have a port type configured,
ian@0 941 * we must release its resources.
ian@0 942 */
ian@0 943 if (port->type != PORT_UNKNOWN)
ian@0 944 port->ops->release_port(port);
ian@0 945
ian@0 946 flags = UART_CONFIG_TYPE;
ian@0 947 if (port->flags & UPF_AUTO_IRQ)
ian@0 948 flags |= UART_CONFIG_IRQ;
ian@0 949
ian@0 950 /*
ian@0 951 * This will claim the ports resources if
ian@0 952 * a port is found.
ian@0 953 */
ian@0 954 port->ops->config_port(port, flags);
ian@0 955
ian@0 956 ret = uart_startup(state, 1);
ian@0 957 }
ian@0 958 mutex_unlock(&state->mutex);
ian@0 959 return ret;
ian@0 960 }
ian@0 961
ian@0 962 /*
ian@0 963 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
ian@0 964 * - mask passed in arg for lines of interest
ian@0 965 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
ian@0 966 * Caller should use TIOCGICOUNT to see which one it was
ian@0 967 */
ian@0 968 static int
ian@0 969 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
ian@0 970 {
ian@0 971 struct uart_port *port = state->port;
ian@0 972 DECLARE_WAITQUEUE(wait, current);
ian@0 973 struct uart_icount cprev, cnow;
ian@0 974 int ret;
ian@0 975
ian@0 976 /*
ian@0 977 * note the counters on entry
ian@0 978 */
ian@0 979 spin_lock_irq(&port->lock);
ian@0 980 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
ian@0 981
ian@0 982 /*
ian@0 983 * Force modem status interrupts on
ian@0 984 */
ian@0 985 port->ops->enable_ms(port);
ian@0 986 spin_unlock_irq(&port->lock);
ian@0 987
ian@0 988 add_wait_queue(&state->info->delta_msr_wait, &wait);
ian@0 989 for (;;) {
ian@0 990 spin_lock_irq(&port->lock);
ian@0 991 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
ian@0 992 spin_unlock_irq(&port->lock);
ian@0 993
ian@0 994 set_current_state(TASK_INTERRUPTIBLE);
ian@0 995
ian@0 996 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
ian@0 997 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
ian@0 998 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
ian@0 999 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
ian@0 1000 ret = 0;
ian@0 1001 break;
ian@0 1002 }
ian@0 1003
ian@0 1004 schedule();
ian@0 1005
ian@0 1006 /* see if a signal did it */
ian@0 1007 if (signal_pending(current)) {
ian@0 1008 ret = -ERESTARTSYS;
ian@0 1009 break;
ian@0 1010 }
ian@0 1011
ian@0 1012 cprev = cnow;
ian@0 1013 }
ian@0 1014
ian@0 1015 current->state = TASK_RUNNING;
ian@0 1016 remove_wait_queue(&state->info->delta_msr_wait, &wait);
ian@0 1017
ian@0 1018 return ret;
ian@0 1019 }
ian@0 1020
ian@0 1021 /*
ian@0 1022 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
ian@0 1023 * Return: write counters to the user passed counter struct
ian@0 1024 * NB: both 1->0 and 0->1 transitions are counted except for
ian@0 1025 * RI where only 0->1 is counted.
ian@0 1026 */
ian@0 1027 static int uart_get_count(struct uart_state *state,
ian@0 1028 struct serial_icounter_struct __user *icnt)
ian@0 1029 {
ian@0 1030 struct serial_icounter_struct icount;
ian@0 1031 struct uart_icount cnow;
ian@0 1032 struct uart_port *port = state->port;
ian@0 1033
ian@0 1034 spin_lock_irq(&port->lock);
ian@0 1035 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
ian@0 1036 spin_unlock_irq(&port->lock);
ian@0 1037
ian@0 1038 icount.cts = cnow.cts;
ian@0 1039 icount.dsr = cnow.dsr;
ian@0 1040 icount.rng = cnow.rng;
ian@0 1041 icount.dcd = cnow.dcd;
ian@0 1042 icount.rx = cnow.rx;
ian@0 1043 icount.tx = cnow.tx;
ian@0 1044 icount.frame = cnow.frame;
ian@0 1045 icount.overrun = cnow.overrun;
ian@0 1046 icount.parity = cnow.parity;
ian@0 1047 icount.brk = cnow.brk;
ian@0 1048 icount.buf_overrun = cnow.buf_overrun;
ian@0 1049
ian@0 1050 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
ian@0 1051 }
ian@0 1052
ian@0 1053 /*
ian@0 1054 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
ian@0 1055 */
ian@0 1056 static int
ian@0 1057 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
ian@0 1058 unsigned long arg)
ian@0 1059 {
ian@0 1060 struct uart_state *state = tty->driver_data;
ian@0 1061 void __user *uarg = (void __user *)arg;
ian@0 1062 int ret = -ENOIOCTLCMD;
ian@0 1063
ian@0 1064 BUG_ON(!kernel_locked());
ian@0 1065
ian@0 1066 /*
ian@0 1067 * These ioctls don't rely on the hardware to be present.
ian@0 1068 */
ian@0 1069 switch (cmd) {
ian@0 1070 case TIOCGSERIAL:
ian@0 1071 ret = uart_get_info(state, uarg);
ian@0 1072 break;
ian@0 1073
ian@0 1074 case TIOCSSERIAL:
ian@0 1075 ret = uart_set_info(state, uarg);
ian@0 1076 break;
ian@0 1077
ian@0 1078 case TIOCSERCONFIG:
ian@0 1079 ret = uart_do_autoconfig(state);
ian@0 1080 break;
ian@0 1081
ian@0 1082 case TIOCSERGWILD: /* obsolete */
ian@0 1083 case TIOCSERSWILD: /* obsolete */
ian@0 1084 ret = 0;
ian@0 1085 break;
ian@0 1086 }
ian@0 1087
ian@0 1088 if (ret != -ENOIOCTLCMD)
ian@0 1089 goto out;
ian@0 1090
ian@0 1091 if (tty->flags & (1 << TTY_IO_ERROR)) {
ian@0 1092 ret = -EIO;
ian@0 1093 goto out;
ian@0 1094 }
ian@0 1095
ian@0 1096 /*
ian@0 1097 * The following should only be used when hardware is present.
ian@0 1098 */
ian@0 1099 switch (cmd) {
ian@0 1100 case TIOCMIWAIT:
ian@0 1101 ret = uart_wait_modem_status(state, arg);
ian@0 1102 break;
ian@0 1103
ian@0 1104 case TIOCGICOUNT:
ian@0 1105 ret = uart_get_count(state, uarg);
ian@0 1106 break;
ian@0 1107 }
ian@0 1108
ian@0 1109 if (ret != -ENOIOCTLCMD)
ian@0 1110 goto out;
ian@0 1111
ian@0 1112 mutex_lock(&state->mutex);
ian@0 1113
ian@0 1114 if (tty_hung_up_p(filp)) {
ian@0 1115 ret = -EIO;
ian@0 1116 goto out_up;
ian@0 1117 }
ian@0 1118
ian@0 1119 /*
ian@0 1120 * All these rely on hardware being present and need to be
ian@0 1121 * protected against the tty being hung up.
ian@0 1122 */
ian@0 1123 switch (cmd) {
ian@0 1124 case TIOCSERGETLSR: /* Get line status register */
ian@0 1125 ret = uart_get_lsr_info(state, uarg);
ian@0 1126 break;
ian@0 1127
ian@0 1128 default: {
ian@0 1129 struct uart_port *port = state->port;
ian@0 1130 if (port->ops->ioctl)
ian@0 1131 ret = port->ops->ioctl(port, cmd, arg);
ian@0 1132 break;
ian@0 1133 }
ian@0 1134 }
ian@0 1135 out_up:
ian@0 1136 mutex_unlock(&state->mutex);
ian@0 1137 out:
ian@0 1138 return ret;
ian@0 1139 }
ian@0 1140
ian@0 1141 static void uart_set_termios(struct tty_struct *tty, struct termios *old_termios)
ian@0 1142 {
ian@0 1143 struct uart_state *state = tty->driver_data;
ian@0 1144 unsigned long flags;
ian@0 1145 unsigned int cflag = tty->termios->c_cflag;
ian@0 1146
ian@0 1147 BUG_ON(!kernel_locked());
ian@0 1148
ian@0 1149 /*
ian@0 1150 * These are the bits that are used to setup various
ian@0 1151 * flags in the low level driver.
ian@0 1152 */
ian@0 1153 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
ian@0 1154
ian@0 1155 if ((cflag ^ old_termios->c_cflag) == 0 &&
ian@0 1156 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
ian@0 1157 return;
ian@0 1158
ian@0 1159 uart_change_speed(state, old_termios);
ian@0 1160
ian@0 1161 /* Handle transition to B0 status */
ian@0 1162 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
ian@0 1163 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
ian@0 1164
ian@0 1165 /* Handle transition away from B0 status */
ian@0 1166 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
ian@0 1167 unsigned int mask = TIOCM_DTR;
ian@0 1168 if (!(cflag & CRTSCTS) ||
ian@0 1169 !test_bit(TTY_THROTTLED, &tty->flags))
ian@0 1170 mask |= TIOCM_RTS;
ian@0 1171 uart_set_mctrl(state->port, mask);
ian@0 1172 }
ian@0 1173
ian@0 1174 /* Handle turning off CRTSCTS */
ian@0 1175 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
ian@0 1176 spin_lock_irqsave(&state->port->lock, flags);
ian@0 1177 tty->hw_stopped = 0;
ian@0 1178 __uart_start(tty);
ian@0 1179 spin_unlock_irqrestore(&state->port->lock, flags);
ian@0 1180 }
ian@0 1181
ian@0 1182 /* Handle turning on CRTSCTS */
ian@0 1183 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
ian@0 1184 spin_lock_irqsave(&state->port->lock, flags);
ian@0 1185 if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
ian@0 1186 tty->hw_stopped = 1;
ian@0 1187 state->port->ops->stop_tx(state->port);
ian@0 1188 }
ian@0 1189 spin_unlock_irqrestore(&state->port->lock, flags);
ian@0 1190 }
ian@0 1191
ian@0 1192 #if 0
ian@0 1193 /*
ian@0 1194 * No need to wake up processes in open wait, since they
ian@0 1195 * sample the CLOCAL flag once, and don't recheck it.
ian@0 1196 * XXX It's not clear whether the current behavior is correct
ian@0 1197 * or not. Hence, this may change.....
ian@0 1198 */
ian@0 1199 if (!(old_termios->c_cflag & CLOCAL) &&
ian@0 1200 (tty->termios->c_cflag & CLOCAL))
ian@0 1201 wake_up_interruptible(&state->info->open_wait);
ian@0 1202 #endif
ian@0 1203 }
ian@0 1204
ian@0 1205 /*
ian@0 1206 * In 2.4.5, calls to this will be serialized via the BKL in
ian@0 1207 * linux/drivers/char/tty_io.c:tty_release()
ian@0 1208 * linux/drivers/char/tty_io.c:do_tty_handup()
ian@0 1209 */
ian@0 1210 static void uart_close(struct tty_struct *tty, struct file *filp)
ian@0 1211 {
ian@0 1212 struct uart_state *state = tty->driver_data;
ian@0 1213 struct uart_port *port;
ian@0 1214
ian@0 1215 BUG_ON(!kernel_locked());
ian@0 1216
ian@0 1217 if (!state || !state->port)
ian@0 1218 return;
ian@0 1219
ian@0 1220 port = state->port;
ian@0 1221
ian@0 1222 DPRINTK("uart_close(%d) called\n", port->line);
ian@0 1223
ian@0 1224 mutex_lock(&state->mutex);
ian@0 1225
ian@0 1226 if (tty_hung_up_p(filp))
ian@0 1227 goto done;
ian@0 1228
ian@0 1229 if ((tty->count == 1) && (state->count != 1)) {
ian@0 1230 /*
ian@0 1231 * Uh, oh. tty->count is 1, which means that the tty
ian@0 1232 * structure will be freed. state->count should always
ian@0 1233 * be one in these conditions. If it's greater than
ian@0 1234 * one, we've got real problems, since it means the
ian@0 1235 * serial port won't be shutdown.
ian@0 1236 */
ian@0 1237 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
ian@0 1238 "state->count is %d\n", state->count);
ian@0 1239 state->count = 1;
ian@0 1240 }
ian@0 1241 if (--state->count < 0) {
ian@0 1242 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
ian@0 1243 tty->name, state->count);
ian@0 1244 state->count = 0;
ian@0 1245 }
ian@0 1246 if (state->count)
ian@0 1247 goto done;
ian@0 1248
ian@0 1249 /*
ian@0 1250 * Now we wait for the transmit buffer to clear; and we notify
ian@0 1251 * the line discipline to only process XON/XOFF characters by
ian@0 1252 * setting tty->closing.
ian@0 1253 */
ian@0 1254 tty->closing = 1;
ian@0 1255
ian@0 1256 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
ian@0 1257 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
ian@0 1258
ian@0 1259 /*
ian@0 1260 * At this point, we stop accepting input. To do this, we
ian@0 1261 * disable the receive line status interrupts.
ian@0 1262 */
ian@0 1263 if (state->info->flags & UIF_INITIALIZED) {
ian@0 1264 unsigned long flags;
ian@0 1265 spin_lock_irqsave(&port->lock, flags);
ian@0 1266 port->ops->stop_rx(port);
ian@0 1267 spin_unlock_irqrestore(&port->lock, flags);
ian@0 1268 /*
ian@0 1269 * Before we drop DTR, make sure the UART transmitter
ian@0 1270 * has completely drained; this is especially
ian@0 1271 * important if there is a transmit FIFO!
ian@0 1272 */
ian@0 1273 uart_wait_until_sent(tty, port->timeout);
ian@0 1274 }
ian@0 1275
ian@0 1276 uart_shutdown(state);
ian@0 1277 uart_flush_buffer(tty);
ian@0 1278
ian@0 1279 tty_ldisc_flush(tty);
ian@0 1280
ian@0 1281 tty->closing = 0;
ian@0 1282 state->info->tty = NULL;
ian@0 1283
ian@0 1284 if (state->info->blocked_open) {
ian@0 1285 if (state->close_delay)
ian@0 1286 msleep_interruptible(state->close_delay);
ian@0 1287 } else if (!uart_console(port)) {
ian@0 1288 uart_change_pm(state, 3);
ian@0 1289 }
ian@0 1290
ian@0 1291 /*
ian@0 1292 * Wake up anyone trying to open this port.
ian@0 1293 */
ian@0 1294 state->info->flags &= ~UIF_NORMAL_ACTIVE;
ian@0 1295 wake_up_interruptible(&state->info->open_wait);
ian@0 1296
ian@0 1297 done:
ian@0 1298 mutex_unlock(&state->mutex);
ian@0 1299 }
ian@0 1300
ian@0 1301 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
ian@0 1302 {
ian@0 1303 struct uart_state *state = tty->driver_data;
ian@0 1304 struct uart_port *port = state->port;
ian@0 1305 unsigned long char_time, expire;
ian@0 1306
ian@0 1307 BUG_ON(!kernel_locked());
ian@0 1308
ian@0 1309 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
ian@0 1310 return;
ian@0 1311
ian@0 1312 /*
ian@0 1313 * Set the check interval to be 1/5 of the estimated time to
ian@0 1314 * send a single character, and make it at least 1. The check
ian@0 1315 * interval should also be less than the timeout.
ian@0 1316 *
ian@0 1317 * Note: we have to use pretty tight timings here to satisfy
ian@0 1318 * the NIST-PCTS.
ian@0 1319 */
ian@0 1320 char_time = (port->timeout - HZ/50) / port->fifosize;
ian@0 1321 char_time = char_time / 5;
ian@0 1322 if (char_time == 0)
ian@0 1323 char_time = 1;
ian@0 1324 if (timeout && timeout < char_time)
ian@0 1325 char_time = timeout;
ian@0 1326
ian@0 1327 /*
ian@0 1328 * If the transmitter hasn't cleared in twice the approximate
ian@0 1329 * amount of time to send the entire FIFO, it probably won't
ian@0 1330 * ever clear. This assumes the UART isn't doing flow
ian@0 1331 * control, which is currently the case. Hence, if it ever
ian@0 1332 * takes longer than port->timeout, this is probably due to a
ian@0 1333 * UART bug of some kind. So, we clamp the timeout parameter at
ian@0 1334 * 2*port->timeout.
ian@0 1335 */
ian@0 1336 if (timeout == 0 || timeout > 2 * port->timeout)
ian@0 1337 timeout = 2 * port->timeout;
ian@0 1338
ian@0 1339 expire = jiffies + timeout;
ian@0 1340
ian@0 1341 DPRINTK("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
ian@0 1342 port->line, jiffies, expire);
ian@0 1343
ian@0 1344 /*
ian@0 1345 * Check whether the transmitter is empty every 'char_time'.
ian@0 1346 * 'timeout' / 'expire' give us the maximum amount of time
ian@0 1347 * we wait.
ian@0 1348 */
ian@0 1349 while (!port->ops->tx_empty(port)) {
ian@0 1350 msleep_interruptible(jiffies_to_msecs(char_time));
ian@0 1351 if (signal_pending(current))
ian@0 1352 break;
ian@0 1353 if (time_after(jiffies, expire))
ian@0 1354 break;
ian@0 1355 }
ian@0 1356 set_current_state(TASK_RUNNING); /* might not be needed */
ian@0 1357 }
ian@0 1358
ian@0 1359 /*
ian@0 1360 * This is called with the BKL held in
ian@0 1361 * linux/drivers/char/tty_io.c:do_tty_hangup()
ian@0 1362 * We're called from the eventd thread, so we can sleep for
ian@0 1363 * a _short_ time only.
ian@0 1364 */
ian@0 1365 static void uart_hangup(struct tty_struct *tty)
ian@0 1366 {
ian@0 1367 struct uart_state *state = tty->driver_data;
ian@0 1368
ian@0 1369 BUG_ON(!kernel_locked());
ian@0 1370 DPRINTK("uart_hangup(%d)\n", state->port->line);
ian@0 1371
ian@0 1372 mutex_lock(&state->mutex);
ian@0 1373 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
ian@0 1374 uart_flush_buffer(tty);
ian@0 1375 uart_shutdown(state);
ian@0 1376 state->count = 0;
ian@0 1377 state->info->flags &= ~UIF_NORMAL_ACTIVE;
ian@0 1378 state->info->tty = NULL;
ian@0 1379 wake_up_interruptible(&state->info->open_wait);
ian@0 1380 wake_up_interruptible(&state->info->delta_msr_wait);
ian@0 1381 }
ian@0 1382 mutex_unlock(&state->mutex);
ian@0 1383 }
ian@0 1384
ian@0 1385 /*
ian@0 1386 * Copy across the serial console cflag setting into the termios settings
ian@0 1387 * for the initial open of the port. This allows continuity between the
ian@0 1388 * kernel settings, and the settings init adopts when it opens the port
ian@0 1389 * for the first time.
ian@0 1390 */
ian@0 1391 static void uart_update_termios(struct uart_state *state)
ian@0 1392 {
ian@0 1393 struct tty_struct *tty = state->info->tty;
ian@0 1394 struct uart_port *port = state->port;
ian@0 1395
ian@0 1396 if (uart_console(port) && port->cons->cflag) {
ian@0 1397 tty->termios->c_cflag = port->cons->cflag;
ian@0 1398 port->cons->cflag = 0;
ian@0 1399 }
ian@0 1400
ian@0 1401 /*
ian@0 1402 * If the device failed to grab its irq resources,
ian@0 1403 * or some other error occurred, don't try to talk
ian@0 1404 * to the port hardware.
ian@0 1405 */
ian@0 1406 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
ian@0 1407 /*
ian@0 1408 * Make termios settings take effect.
ian@0 1409 */
ian@0 1410 uart_change_speed(state, NULL);
ian@0 1411
ian@0 1412 /*
ian@0 1413 * And finally enable the RTS and DTR signals.
ian@0 1414 */
ian@0 1415 if (tty->termios->c_cflag & CBAUD)
ian@0 1416 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
ian@0 1417 }
ian@0 1418 }
ian@0 1419
ian@0 1420 /*
ian@0 1421 * Block the open until the port is ready. We must be called with
ian@0 1422 * the per-port semaphore held.
ian@0 1423 */
ian@0 1424 static int
ian@0 1425 uart_block_til_ready(struct file *filp, struct uart_state *state)
ian@0 1426 {
ian@0 1427 DECLARE_WAITQUEUE(wait, current);
ian@0 1428 struct uart_info *info = state->info;
ian@0 1429 struct uart_port *port = state->port;
ian@0 1430 unsigned int mctrl;
ian@0 1431
ian@0 1432 info->blocked_open++;
ian@0 1433 state->count--;
ian@0 1434
ian@0 1435 add_wait_queue(&info->open_wait, &wait);
ian@0 1436 while (1) {
ian@0 1437 set_current_state(TASK_INTERRUPTIBLE);
ian@0 1438
ian@0 1439 /*
ian@0 1440 * If we have been hung up, tell userspace/restart open.
ian@0 1441 */
ian@0 1442 if (tty_hung_up_p(filp) || info->tty == NULL)
ian@0 1443 break;
ian@0 1444
ian@0 1445 /*
ian@0 1446 * If the port has been closed, tell userspace/restart open.
ian@0 1447 */
ian@0 1448 if (!(info->flags & UIF_INITIALIZED))
ian@0 1449 break;
ian@0 1450
ian@0 1451 /*
ian@0 1452 * If non-blocking mode is set, or CLOCAL mode is set,
ian@0 1453 * we don't want to wait for the modem status lines to
ian@0 1454 * indicate that the port is ready.
ian@0 1455 *
ian@0 1456 * Also, if the port is not enabled/configured, we want
ian@0 1457 * to allow the open to succeed here. Note that we will
ian@0 1458 * have set TTY_IO_ERROR for a non-existant port.
ian@0 1459 */
ian@0 1460 if ((filp->f_flags & O_NONBLOCK) ||
ian@0 1461 (info->tty->termios->c_cflag & CLOCAL) ||
ian@0 1462 (info->tty->flags & (1 << TTY_IO_ERROR))) {
ian@0 1463 break;
ian@0 1464 }
ian@0 1465
ian@0 1466 /*
ian@0 1467 * Set DTR to allow modem to know we're waiting. Do
ian@0 1468 * not set RTS here - we want to make sure we catch
ian@0 1469 * the data from the modem.
ian@0 1470 */
ian@0 1471 if (info->tty->termios->c_cflag & CBAUD)
ian@0 1472 uart_set_mctrl(port, TIOCM_DTR);
ian@0 1473
ian@0 1474 /*
ian@0 1475 * and wait for the carrier to indicate that the
ian@0 1476 * modem is ready for us.
ian@0 1477 */
ian@0 1478 spin_lock_irq(&port->lock);
ian@0 1479 port->ops->enable_ms(port);
ian@0 1480 mctrl = port->ops->get_mctrl(port);
ian@0 1481 spin_unlock_irq(&port->lock);
ian@0 1482 if (mctrl & TIOCM_CAR)
ian@0 1483 break;
ian@0 1484
ian@0 1485 mutex_unlock(&state->mutex);
ian@0 1486 schedule();
ian@0 1487 mutex_lock(&state->mutex);
ian@0 1488
ian@0 1489 if (signal_pending(current))
ian@0 1490 break;
ian@0 1491 }
ian@0 1492 set_current_state(TASK_RUNNING);
ian@0 1493 remove_wait_queue(&info->open_wait, &wait);
ian@0 1494
ian@0 1495 state->count++;
ian@0 1496 info->blocked_open--;
ian@0 1497
ian@0 1498 if (signal_pending(current))
ian@0 1499 return -ERESTARTSYS;
ian@0 1500
ian@0 1501 if (!info->tty || tty_hung_up_p(filp))
ian@0 1502 return -EAGAIN;
ian@0 1503
ian@0 1504 return 0;
ian@0 1505 }
ian@0 1506
ian@0 1507 static struct uart_state *uart_get(struct uart_driver *drv, int line)
ian@0 1508 {
ian@0 1509 struct uart_state *state;
ian@0 1510 int ret = 0;
ian@0 1511
ian@0 1512 state = drv->state + line;
ian@0 1513 if (mutex_lock_interruptible(&state->mutex)) {
ian@0 1514 ret = -ERESTARTSYS;
ian@0 1515 goto err;
ian@0 1516 }
ian@0 1517
ian@0 1518 state->count++;
ian@0 1519 if (!state->port || state->port->flags & UPF_DEAD) {
ian@0 1520 ret = -ENXIO;
ian@0 1521 goto err_unlock;
ian@0 1522 }
ian@0 1523
ian@0 1524 if (!state->info) {
ian@0 1525 state->info = kmalloc(sizeof(struct uart_info), GFP_KERNEL);
ian@0 1526 if (state->info) {
ian@0 1527 memset(state->info, 0, sizeof(struct uart_info));
ian@0 1528 init_waitqueue_head(&state->info->open_wait);
ian@0 1529 init_waitqueue_head(&state->info->delta_msr_wait);
ian@0 1530
ian@0 1531 /*
ian@0 1532 * Link the info into the other structures.
ian@0 1533 */
ian@0 1534 state->port->info = state->info;
ian@0 1535
ian@0 1536 tasklet_init(&state->info->tlet, uart_tasklet_action,
ian@0 1537 (unsigned long)state);
ian@0 1538 } else {
ian@0 1539 ret = -ENOMEM;
ian@0 1540 goto err_unlock;
ian@0 1541 }
ian@0 1542 }
ian@0 1543 return state;
ian@0 1544
ian@0 1545 err_unlock:
ian@0 1546 state->count--;
ian@0 1547 mutex_unlock(&state->mutex);
ian@0 1548 err:
ian@0 1549 return ERR_PTR(ret);
ian@0 1550 }
ian@0 1551
ian@0 1552 /*
ian@0 1553 * In 2.4.5, calls to uart_open are serialised by the BKL in
ian@0 1554 * linux/fs/devices.c:chrdev_open()
ian@0 1555 * Note that if this fails, then uart_close() _will_ be called.
ian@0 1556 *
ian@0 1557 * In time, we want to scrap the "opening nonpresent ports"
ian@0 1558 * behaviour and implement an alternative way for setserial
ian@0 1559 * to set base addresses/ports/types. This will allow us to
ian@0 1560 * get rid of a certain amount of extra tests.
ian@0 1561 */
ian@0 1562 static int uart_open(struct tty_struct *tty, struct file *filp)
ian@0 1563 {
ian@0 1564 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
ian@0 1565 struct uart_state *state;
ian@0 1566 int retval, line = tty->index;
ian@0 1567
ian@0 1568 BUG_ON(!kernel_locked());
ian@0 1569 DPRINTK("uart_open(%d) called\n", line);
ian@0 1570
ian@0 1571 /*
ian@0 1572 * tty->driver->num won't change, so we won't fail here with
ian@0 1573 * tty->driver_data set to something non-NULL (and therefore
ian@0 1574 * we won't get caught by uart_close()).
ian@0 1575 */
ian@0 1576 retval = -ENODEV;
ian@0 1577 if (line >= tty->driver->num)
ian@0 1578 goto fail;
ian@0 1579
ian@0 1580 /*
ian@0 1581 * We take the semaphore inside uart_get to guarantee that we won't
ian@0 1582 * be re-entered while allocating the info structure, or while we
ian@0 1583 * request any IRQs that the driver may need. This also has the nice
ian@0 1584 * side-effect that it delays the action of uart_hangup, so we can
ian@0 1585 * guarantee that info->tty will always contain something reasonable.
ian@0 1586 */
ian@0 1587 state = uart_get(drv, line);
ian@0 1588 if (IS_ERR(state)) {
ian@0 1589 retval = PTR_ERR(state);
ian@0 1590 goto fail;
ian@0 1591 }
ian@0 1592
ian@0 1593 /*
ian@0 1594 * Once we set tty->driver_data here, we are guaranteed that
ian@0 1595 * uart_close() will decrement the driver module use count.
ian@0 1596 * Any failures from here onwards should not touch the count.
ian@0 1597 */
ian@0 1598 tty->driver_data = state;
ian@0 1599 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
ian@0 1600 tty->alt_speed = 0;
ian@0 1601 state->info->tty = tty;
ian@0 1602
ian@0 1603 /*
ian@0 1604 * If the port is in the middle of closing, bail out now.
ian@0 1605 */
ian@0 1606 if (tty_hung_up_p(filp)) {
ian@0 1607 retval = -EAGAIN;
ian@0 1608 state->count--;
ian@0 1609 mutex_unlock(&state->mutex);
ian@0 1610 goto fail;
ian@0 1611 }
ian@0 1612
ian@0 1613 /*
ian@0 1614 * Make sure the device is in D0 state.
ian@0 1615 */
ian@0 1616 if (state->count == 1)
ian@0 1617 uart_change_pm(state, 0);
ian@0 1618
ian@0 1619 /*
ian@0 1620 * Start up the serial port.
ian@0 1621 */
ian@0 1622 retval = uart_startup(state, 0);
ian@0 1623
ian@0 1624 /*
ian@0 1625 * If we succeeded, wait until the port is ready.
ian@0 1626 */
ian@0 1627 if (retval == 0)
ian@0 1628 retval = uart_block_til_ready(filp, state);
ian@0 1629 mutex_unlock(&state->mutex);
ian@0 1630
ian@0 1631 /*
ian@0 1632 * If this is the first open to succeed, adjust things to suit.
ian@0 1633 */
ian@0 1634 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
ian@0 1635 state->info->flags |= UIF_NORMAL_ACTIVE;
ian@0 1636
ian@0 1637 uart_update_termios(state);
ian@0 1638 }
ian@0 1639
ian@0 1640 fail:
ian@0 1641 return retval;
ian@0 1642 }
ian@0 1643
ian@0 1644 static const char *uart_type(struct uart_port *port)
ian@0 1645 {
ian@0 1646 const char *str = NULL;
ian@0 1647
ian@0 1648 if (port->ops->type)
ian@0 1649 str = port->ops->type(port);
ian@0 1650
ian@0 1651 if (!str)
ian@0 1652 str = "unknown";
ian@0 1653
ian@0 1654 return str;
ian@0 1655 }
ian@0 1656
ian@0 1657 #ifdef CONFIG_PROC_FS
ian@0 1658
ian@0 1659 static int uart_line_info(char *buf, struct uart_driver *drv, int i)
ian@0 1660 {
ian@0 1661 struct uart_state *state = drv->state + i;
ian@0 1662 struct uart_port *port = state->port;
ian@0 1663 char stat_buf[32];
ian@0 1664 unsigned int status;
ian@0 1665 int ret;
ian@0 1666
ian@0 1667 if (!port)
ian@0 1668 return 0;
ian@0 1669
ian@0 1670 ret = sprintf(buf, "%d: uart:%s %s%08lX irq:%d",
ian@0 1671 port->line, uart_type(port),
ian@0 1672 port->iotype == UPIO_MEM ? "mmio:0x" : "port:",
ian@0 1673 port->iotype == UPIO_MEM ? port->mapbase :
ian@0 1674 (unsigned long) port->iobase,
ian@0 1675 port->irq);
ian@0 1676
ian@0 1677 if (port->type == PORT_UNKNOWN) {
ian@0 1678 strcat(buf, "\n");
ian@0 1679 return ret + 1;
ian@0 1680 }
ian@0 1681
ian@0 1682 if(capable(CAP_SYS_ADMIN))
ian@0 1683 {
ian@0 1684 spin_lock_irq(&port->lock);
ian@0 1685 status = port->ops->get_mctrl(port);
ian@0 1686 spin_unlock_irq(&port->lock);
ian@0 1687
ian@0 1688 ret += sprintf(buf + ret, " tx:%d rx:%d",
ian@0 1689 port->icount.tx, port->icount.rx);
ian@0 1690 if (port->icount.frame)
ian@0 1691 ret += sprintf(buf + ret, " fe:%d",
ian@0 1692 port->icount.frame);
ian@0 1693 if (port->icount.parity)
ian@0 1694 ret += sprintf(buf + ret, " pe:%d",
ian@0 1695 port->icount.parity);
ian@0 1696 if (port->icount.brk)
ian@0 1697 ret += sprintf(buf + ret, " brk:%d",
ian@0 1698 port->icount.brk);
ian@0 1699 if (port->icount.overrun)
ian@0 1700 ret += sprintf(buf + ret, " oe:%d",
ian@0 1701 port->icount.overrun);
ian@0 1702
ian@0 1703 #define INFOBIT(bit,str) \
ian@0 1704 if (port->mctrl & (bit)) \
ian@0 1705 strncat(stat_buf, (str), sizeof(stat_buf) - \
ian@0 1706 strlen(stat_buf) - 2)
ian@0 1707 #define STATBIT(bit,str) \
ian@0 1708 if (status & (bit)) \
ian@0 1709 strncat(stat_buf, (str), sizeof(stat_buf) - \
ian@0 1710 strlen(stat_buf) - 2)
ian@0 1711
ian@0 1712 stat_buf[0] = '\0';
ian@0 1713 stat_buf[1] = '\0';
ian@0 1714 INFOBIT(TIOCM_RTS, "|RTS");
ian@0 1715 STATBIT(TIOCM_CTS, "|CTS");
ian@0 1716 INFOBIT(TIOCM_DTR, "|DTR");
ian@0 1717 STATBIT(TIOCM_DSR, "|DSR");
ian@0 1718 STATBIT(TIOCM_CAR, "|CD");
ian@0 1719 STATBIT(TIOCM_RNG, "|RI");
ian@0 1720 if (stat_buf[0])
ian@0 1721 stat_buf[0] = ' ';
ian@0 1722 strcat(stat_buf, "\n");
ian@0 1723
ian@0 1724 ret += sprintf(buf + ret, stat_buf);
ian@0 1725 } else {
ian@0 1726 strcat(buf, "\n");
ian@0 1727 ret++;
ian@0 1728 }
ian@0 1729 #undef STATBIT
ian@0 1730 #undef INFOBIT
ian@0 1731 return ret;
ian@0 1732 }
ian@0 1733
ian@0 1734 static int uart_read_proc(char *page, char **start, off_t off,
ian@0 1735 int count, int *eof, void *data)
ian@0 1736 {
ian@0 1737 struct tty_driver *ttydrv = data;
ian@0 1738 struct uart_driver *drv = ttydrv->driver_state;
ian@0 1739 int i, len = 0, l;
ian@0 1740 off_t begin = 0;
ian@0 1741
ian@0 1742 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
ian@0 1743 "", "", "");
ian@0 1744 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
ian@0 1745 l = uart_line_info(page + len, drv, i);
ian@0 1746 len += l;
ian@0 1747 if (len + begin > off + count)
ian@0 1748 goto done;
ian@0 1749 if (len + begin < off) {
ian@0 1750 begin += len;
ian@0 1751 len = 0;
ian@0 1752 }
ian@0 1753 }
ian@0 1754 *eof = 1;
ian@0 1755 done:
ian@0 1756 if (off >= len + begin)
ian@0 1757 return 0;
ian@0 1758 *start = page + (off - begin);
ian@0 1759 return (count < begin + len - off) ? count : (begin + len - off);
ian@0 1760 }
ian@0 1761 #endif
ian@0 1762
ian@0 1763 #ifdef CONFIG_SERIAL_CORE_CONSOLE
ian@0 1764 /*
ian@0 1765 * uart_console_write - write a console message to a serial port
ian@0 1766 * @port: the port to write the message
ian@0 1767 * @s: array of characters
ian@0 1768 * @count: number of characters in string to write
ian@0 1769 * @write: function to write character to port
ian@0 1770 */
ian@0 1771 void uart_console_write(struct uart_port *port, const char *s,
ian@0 1772 unsigned int count,
ian@0 1773 void (*putchar)(struct uart_port *, int))
ian@0 1774 {
ian@0 1775 unsigned int i;
ian@0 1776
ian@0 1777 for (i = 0; i < count; i++, s++) {
ian@0 1778 if (*s == '\n')
ian@0 1779 putchar(port, '\r');
ian@0 1780 putchar(port, *s);
ian@0 1781 }
ian@0 1782 }
ian@0 1783 EXPORT_SYMBOL_GPL(uart_console_write);
ian@0 1784
ian@0 1785 /*
ian@0 1786 * Check whether an invalid uart number has been specified, and
ian@0 1787 * if so, search for the first available port that does have
ian@0 1788 * console support.
ian@0 1789 */
ian@0 1790 struct uart_port * __init
ian@0 1791 uart_get_console(struct uart_port *ports, int nr, struct console *co)
ian@0 1792 {
ian@0 1793 int idx = co->index;
ian@0 1794
ian@0 1795 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
ian@0 1796 ports[idx].membase == NULL))
ian@0 1797 for (idx = 0; idx < nr; idx++)
ian@0 1798 if (ports[idx].iobase != 0 ||
ian@0 1799 ports[idx].membase != NULL)
ian@0 1800 break;
ian@0 1801
ian@0 1802 co->index = idx;
ian@0 1803
ian@0 1804 return ports + idx;
ian@0 1805 }
ian@0 1806
ian@0 1807 /**
ian@0 1808 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
ian@0 1809 * @options: pointer to option string
ian@0 1810 * @baud: pointer to an 'int' variable for the baud rate.
ian@0 1811 * @parity: pointer to an 'int' variable for the parity.
ian@0 1812 * @bits: pointer to an 'int' variable for the number of data bits.
ian@0 1813 * @flow: pointer to an 'int' variable for the flow control character.
ian@0 1814 *
ian@0 1815 * uart_parse_options decodes a string containing the serial console
ian@0 1816 * options. The format of the string is <baud><parity><bits><flow>,
ian@0 1817 * eg: 115200n8r
ian@0 1818 */
ian@0 1819 void __init
ian@0 1820 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
ian@0 1821 {
ian@0 1822 char *s = options;
ian@0 1823
ian@0 1824 *baud = simple_strtoul(s, NULL, 10);
ian@0 1825 while (*s >= '0' && *s <= '9')
ian@0 1826 s++;
ian@0 1827 if (*s)
ian@0 1828 *parity = *s++;
ian@0 1829 if (*s)
ian@0 1830 *bits = *s++ - '0';
ian@0 1831 if (*s)
ian@0 1832 *flow = *s;
ian@0 1833 }
ian@0 1834
ian@0 1835 struct baud_rates {
ian@0 1836 unsigned int rate;
ian@0 1837 unsigned int cflag;
ian@0 1838 };
ian@0 1839
ian@0 1840 static const struct baud_rates baud_rates[] = {
ian@0 1841 { 921600, B921600 },
ian@0 1842 { 460800, B460800 },
ian@0 1843 { 230400, B230400 },
ian@0 1844 { 115200, B115200 },
ian@0 1845 { 57600, B57600 },
ian@0 1846 { 38400, B38400 },
ian@0 1847 { 19200, B19200 },
ian@0 1848 { 9600, B9600 },
ian@0 1849 { 4800, B4800 },
ian@0 1850 { 2400, B2400 },
ian@0 1851 { 1200, B1200 },
ian@0 1852 { 0, B38400 }
ian@0 1853 };
ian@0 1854
ian@0 1855 /**
ian@0 1856 * uart_set_options - setup the serial console parameters
ian@0 1857 * @port: pointer to the serial ports uart_port structure
ian@0 1858 * @co: console pointer
ian@0 1859 * @baud: baud rate
ian@0 1860 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
ian@0 1861 * @bits: number of data bits
ian@0 1862 * @flow: flow control character - 'r' (rts)
ian@0 1863 */
ian@0 1864 int __init
ian@0 1865 uart_set_options(struct uart_port *port, struct console *co,
ian@0 1866 int baud, int parity, int bits, int flow)
ian@0 1867 {
ian@0 1868 struct termios termios;
ian@0 1869 int i;
ian@0 1870
ian@0 1871 /*
ian@0 1872 * Ensure that the serial console lock is initialised
ian@0 1873 * early.
ian@0 1874 */
ian@0 1875 spin_lock_init(&port->lock);
ian@0 1876 lockdep_set_class(&port->lock, &port_lock_key);
ian@0 1877
ian@0 1878 memset(&termios, 0, sizeof(struct termios));
ian@0 1879
ian@0 1880 termios.c_cflag = CREAD | HUPCL | CLOCAL;
ian@0 1881
ian@0 1882 /*
ian@0 1883 * Construct a cflag setting.
ian@0 1884 */
ian@0 1885 for (i = 0; baud_rates[i].rate; i++)
ian@0 1886 if (baud_rates[i].rate <= baud)
ian@0 1887 break;
ian@0 1888
ian@0 1889 termios.c_cflag |= baud_rates[i].cflag;
ian@0 1890
ian@0 1891 if (bits == 7)
ian@0 1892 termios.c_cflag |= CS7;
ian@0 1893 else
ian@0 1894 termios.c_cflag |= CS8;
ian@0 1895
ian@0 1896 switch (parity) {
ian@0 1897 case 'o': case 'O':
ian@0 1898 termios.c_cflag |= PARODD;
ian@0 1899 /*fall through*/
ian@0 1900 case 'e': case 'E':
ian@0 1901 termios.c_cflag |= PARENB;
ian@0 1902 break;
ian@0 1903 }
ian@0 1904
ian@0 1905 if (flow == 'r')
ian@0 1906 termios.c_cflag |= CRTSCTS;
ian@0 1907
ian@0 1908 port->ops->set_termios(port, &termios, NULL);
ian@0 1909 co->cflag = termios.c_cflag;
ian@0 1910
ian@0 1911 return 0;
ian@0 1912 }
ian@0 1913 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
ian@0 1914
ian@0 1915 static void uart_change_pm(struct uart_state *state, int pm_state)
ian@0 1916 {
ian@0 1917 struct uart_port *port = state->port;
ian@0 1918
ian@0 1919 if (state->pm_state != pm_state) {
ian@0 1920 if (port->ops->pm)
ian@0 1921 port->ops->pm(port, pm_state, state->pm_state);
ian@0 1922 state->pm_state = pm_state;
ian@0 1923 }
ian@0 1924 }
ian@0 1925
ian@0 1926 int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
ian@0 1927 {
ian@0 1928 struct uart_state *state = drv->state + port->line;
ian@0 1929
ian@0 1930 mutex_lock(&state->mutex);
ian@0 1931
ian@0 1932 if (state->info && state->info->flags & UIF_INITIALIZED) {
ian@0 1933 const struct uart_ops *ops = port->ops;
ian@0 1934
ian@240 1935 state->info->flags = (state->info->flags & ~UIF_INITIALIZED)
ian@240 1936 | UIF_SUSPENDED;
ian@240 1937
ian@0 1938 spin_lock_irq(&port->lock);
ian@0 1939 ops->stop_tx(port);
ian@0 1940 ops->set_mctrl(port, 0);
ian@0 1941 ops->stop_rx(port);
ian@0 1942 spin_unlock_irq(&port->lock);
ian@0 1943
ian@0 1944 /*
ian@0 1945 * Wait for the transmitter to empty.
ian@0 1946 */
ian@0 1947 while (!ops->tx_empty(port)) {
ian@0 1948 msleep(10);
ian@0 1949 }
ian@0 1950
ian@0 1951 ops->shutdown(port);
ian@0 1952 }
ian@0 1953
ian@0 1954 /*
ian@0 1955 * Disable the console device before suspending.
ian@0 1956 */
ian@0 1957 if (uart_console(port))
ian@0 1958 console_stop(port->cons);
ian@0 1959
ian@0 1960 uart_change_pm(state, 3);
ian@0 1961
ian@0 1962 mutex_unlock(&state->mutex);
ian@0 1963
ian@0 1964 return 0;
ian@0 1965 }
ian@0 1966
ian@0 1967 int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
ian@0 1968 {
ian@0 1969 struct uart_state *state = drv->state + port->line;
ian@0 1970
ian@0 1971 mutex_lock(&state->mutex);
ian@0 1972
ian@0 1973 uart_change_pm(state, 0);
ian@0 1974
ian@0 1975 /*
ian@0 1976 * Re-enable the console device after suspending.
ian@0 1977 */
ian@0 1978 if (uart_console(port)) {
ian@0 1979 struct termios termios;
ian@0 1980
ian@0 1981 /*
ian@0 1982 * First try to use the console cflag setting.
ian@0 1983 */
ian@0 1984 memset(&termios, 0, sizeof(struct termios));
ian@0 1985 termios.c_cflag = port->cons->cflag;
ian@0 1986
ian@0 1987 /*
ian@0 1988 * If that's unset, use the tty termios setting.
ian@0 1989 */
ian@0 1990 if (state->info && state->info->tty && termios.c_cflag == 0)
ian@0 1991 termios = *state->info->tty->termios;
ian@0 1992
ian@0 1993 port->ops->set_termios(port, &termios, NULL);
ian@0 1994 console_start(port->cons);
ian@0 1995 }
ian@0 1996
ian@240 1997 if (state->info && state->info->flags & UIF_SUSPENDED) {
ian@0 1998 const struct uart_ops *ops = port->ops;
ian@0 1999 int ret;
ian@0 2000
ian@0 2001 ops->set_mctrl(port, 0);
ian@0 2002 ret = ops->startup(port);
ian@0 2003 if (ret == 0) {
ian@0 2004 uart_change_speed(state, NULL);
ian@0 2005 spin_lock_irq(&port->lock);
ian@0 2006 ops->set_mctrl(port, port->mctrl);
ian@0 2007 ops->start_tx(port);
ian@0 2008 spin_unlock_irq(&port->lock);
ian@240 2009 state->info->flags |= UIF_INITIALIZED;
ian@0 2010 } else {
ian@0 2011 /*
ian@0 2012 * Failed to resume - maybe hardware went away?
ian@0 2013 * Clear the "initialized" flag so we won't try
ian@0 2014 * to call the low level drivers shutdown method.
ian@0 2015 */
ian@0 2016 uart_shutdown(state);
ian@0 2017 }
ian@240 2018
ian@240 2019 state->info->flags &= ~UIF_SUSPENDED;
ian@0 2020 }
ian@0 2021
ian@0 2022 mutex_unlock(&state->mutex);
ian@0 2023
ian@0 2024 return 0;
ian@0 2025 }
ian@0 2026
ian@0 2027 static inline void
ian@0 2028 uart_report_port(struct uart_driver *drv, struct uart_port *port)
ian@0 2029 {
ian@0 2030 char address[64];
ian@0 2031
ian@0 2032 switch (port->iotype) {
ian@0 2033 case UPIO_PORT:
ian@0 2034 snprintf(address, sizeof(address),
ian@0 2035 "I/O 0x%x", port->iobase);
ian@0 2036 break;
ian@0 2037 case UPIO_HUB6:
ian@0 2038 snprintf(address, sizeof(address),
ian@0 2039 "I/O 0x%x offset 0x%x", port->iobase, port->hub6);
ian@0 2040 break;
ian@0 2041 case UPIO_MEM:
ian@0 2042 case UPIO_MEM32:
ian@0 2043 case UPIO_AU:
ian@0 2044 case UPIO_TSI:
ian@0 2045 snprintf(address, sizeof(address),
ian@0 2046 "MMIO 0x%lx", port->mapbase);
ian@0 2047 break;
ian@0 2048 default:
ian@0 2049 strlcpy(address, "*unknown*", sizeof(address));
ian@0 2050 break;
ian@0 2051 }
ian@0 2052
ian@0 2053 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
ian@0 2054 port->dev ? port->dev->bus_id : "",
ian@0 2055 port->dev ? ": " : "",
ian@0 2056 drv->dev_name, port->line, address, port->irq, uart_type(port));
ian@0 2057 }
ian@0 2058
ian@0 2059 static void
ian@0 2060 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
ian@0 2061 struct uart_port *port)
ian@0 2062 {
ian@0 2063 unsigned int flags;
ian@0 2064
ian@0 2065 /*
ian@0 2066 * If there isn't a port here, don't do anything further.
ian@0 2067 */
ian@0 2068 if (!port->iobase && !port->mapbase && !port->membase)
ian@0 2069 return;
ian@0 2070
ian@0 2071 /*
ian@0 2072 * Now do the auto configuration stuff. Note that config_port
ian@0 2073 * is expected to claim the resources and map the port for us.
ian@0 2074 */
ian@0 2075 flags = UART_CONFIG_TYPE;
ian@0 2076 if (port->flags & UPF_AUTO_IRQ)
ian@0 2077 flags |= UART_CONFIG_IRQ;
ian@0 2078 if (port->flags & UPF_BOOT_AUTOCONF) {
ian@0 2079 port->type = PORT_UNKNOWN;
ian@0 2080 port->ops->config_port(port, flags);
ian@0 2081 }
ian@0 2082
ian@0 2083 if (port->type != PORT_UNKNOWN) {
ian@0 2084 unsigned long flags;
ian@0 2085
ian@0 2086 uart_report_port(drv, port);
ian@0 2087
ian@0 2088 /*
ian@0 2089 * Ensure that the modem control lines are de-activated.
ian@0 2090 * We probably don't need a spinlock around this, but
ian@0 2091 */
ian@0 2092 spin_lock_irqsave(&port->lock, flags);
ian@0 2093 port->ops->set_mctrl(port, 0);
ian@0 2094 spin_unlock_irqrestore(&port->lock, flags);
ian@0 2095
ian@0 2096 /*
ian@0 2097 * Power down all ports by default, except the
ian@0 2098 * console if we have one.
ian@0 2099 */
ian@0 2100 if (!uart_console(port))
ian@0 2101 uart_change_pm(state, 3);
ian@0 2102 }
ian@0 2103 }
ian@0 2104
ian@0 2105 static struct tty_operations uart_ops = {
ian@0 2106 .open = uart_open,
ian@0 2107 .close = uart_close,
ian@0 2108 .write = uart_write,
ian@0 2109 .put_char = uart_put_char,
ian@0 2110 .flush_chars = uart_flush_chars,
ian@0 2111 .write_room = uart_write_room,
ian@0 2112 .chars_in_buffer= uart_chars_in_buffer,
ian@0 2113 .flush_buffer = uart_flush_buffer,
ian@0 2114 .ioctl = uart_ioctl,
ian@0 2115 .throttle = uart_throttle,
ian@0 2116 .unthrottle = uart_unthrottle,
ian@0 2117 .send_xchar = uart_send_xchar,
ian@0 2118 .set_termios = uart_set_termios,
ian@0 2119 .stop = uart_stop,
ian@0 2120 .start = uart_start,
ian@0 2121 .hangup = uart_hangup,
ian@0 2122 .break_ctl = uart_break_ctl,
ian@0 2123 .wait_until_sent= uart_wait_until_sent,
ian@0 2124 #ifdef CONFIG_PROC_FS
ian@0 2125 .read_proc = uart_read_proc,
ian@0 2126 #endif
ian@0 2127 .tiocmget = uart_tiocmget,
ian@0 2128 .tiocmset = uart_tiocmset,
ian@0 2129 };
ian@0 2130
ian@0 2131 /**
ian@0 2132 * uart_register_driver - register a driver with the uart core layer
ian@0 2133 * @drv: low level driver structure
ian@0 2134 *
ian@0 2135 * Register a uart driver with the core driver. We in turn register
ian@0 2136 * with the tty layer, and initialise the core driver per-port state.
ian@0 2137 *
ian@0 2138 * We have a proc file in /proc/tty/driver which is named after the
ian@0 2139 * normal driver.
ian@0 2140 *
ian@0 2141 * drv->port should be NULL, and the per-port structures should be
ian@0 2142 * registered using uart_add_one_port after this call has succeeded.
ian@0 2143 */
ian@0 2144 int uart_register_driver(struct uart_driver *drv)
ian@0 2145 {
ian@0 2146 struct tty_driver *normal = NULL;
ian@0 2147 int i, retval;
ian@0 2148
ian@0 2149 BUG_ON(drv->state);
ian@0 2150
ian@0 2151 /*
ian@0 2152 * Maybe we should be using a slab cache for this, especially if
ian@0 2153 * we have a large number of ports to handle.
ian@0 2154 */
ian@0 2155 drv->state = kmalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
ian@0 2156 retval = -ENOMEM;
ian@0 2157 if (!drv->state)
ian@0 2158 goto out;
ian@0 2159
ian@0 2160 memset(drv->state, 0, sizeof(struct uart_state) * drv->nr);
ian@0 2161
ian@0 2162 normal = alloc_tty_driver(drv->nr);
ian@0 2163 if (!normal)
ian@0 2164 goto out;
ian@0 2165
ian@0 2166 drv->tty_driver = normal;
ian@0 2167
ian@0 2168 normal->owner = drv->owner;
ian@0 2169 normal->driver_name = drv->driver_name;
ian@0 2170 normal->name = drv->dev_name;
ian@0 2171 normal->major = drv->major;
ian@0 2172 normal->minor_start = drv->minor;
ian@0 2173 normal->type = TTY_DRIVER_TYPE_SERIAL;
ian@0 2174 normal->subtype = SERIAL_TYPE_NORMAL;
ian@0 2175 normal->init_termios = tty_std_termios;
ian@0 2176 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
ian@0 2177 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
ian@0 2178 normal->driver_state = drv;
ian@0 2179 tty_set_operations(normal, &uart_ops);
ian@0 2180
ian@0 2181 /*
ian@0 2182 * Initialise the UART state(s).
ian@0 2183 */
ian@0 2184 for (i = 0; i < drv->nr; i++) {
ian@0 2185 struct uart_state *state = drv->state + i;
ian@0 2186
ian@0 2187 state->close_delay = 500; /* .5 seconds */
ian@0 2188 state->closing_wait = 30000; /* 30 seconds */
ian@0 2189
ian@0 2190 mutex_init(&state->mutex);
ian@0 2191 }
ian@0 2192
ian@0 2193 retval = tty_register_driver(normal);
ian@0 2194 out:
ian@0 2195 if (retval < 0) {
ian@0 2196 put_tty_driver(normal);
ian@0 2197 kfree(drv->state);
ian@0 2198 }
ian@0 2199 return retval;
ian@0 2200 }
ian@0 2201
ian@0 2202 /**
ian@0 2203 * uart_unregister_driver - remove a driver from the uart core layer
ian@0 2204 * @drv: low level driver structure
ian@0 2205 *
ian@0 2206 * Remove all references to a driver from the core driver. The low
ian@0 2207 * level driver must have removed all its ports via the
ian@0 2208 * uart_remove_one_port() if it registered them with uart_add_one_port().
ian@0 2209 * (ie, drv->port == NULL)
ian@0 2210 */
ian@0 2211 void uart_unregister_driver(struct uart_driver *drv)
ian@0 2212 {
ian@0 2213 struct tty_driver *p = drv->tty_driver;
ian@0 2214 tty_unregister_driver(p);
ian@0 2215 put_tty_driver(p);
ian@0 2216 kfree(drv->state);
ian@0 2217 drv->tty_driver = NULL;
ian@0 2218 }
ian@0 2219
ian@0 2220 struct tty_driver *uart_console_device(struct console *co, int *index)
ian@0 2221 {
ian@0 2222 struct uart_driver *p = co->data;
ian@0 2223 *index = co->index;
ian@0 2224 return p->tty_driver;
ian@0 2225 }
ian@0 2226
ian@0 2227 /**
ian@0 2228 * uart_add_one_port - attach a driver-defined port structure
ian@0 2229 * @drv: pointer to the uart low level driver structure for this port
ian@0 2230 * @port: uart port structure to use for this port.
ian@0 2231 *
ian@0 2232 * This allows the driver to register its own uart_port structure
ian@0 2233 * with the core driver. The main purpose is to allow the low
ian@0 2234 * level uart drivers to expand uart_port, rather than having yet
ian@0 2235 * more levels of structures.
ian@0 2236 */
ian@0 2237 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
ian@0 2238 {
ian@0 2239 struct uart_state *state;
ian@0 2240 int ret = 0;
ian@0 2241
ian@0 2242 BUG_ON(in_interrupt());
ian@0 2243
ian@0 2244 if (port->line >= drv->nr)
ian@0 2245 return -EINVAL;
ian@0 2246
ian@0 2247 state = drv->state + port->line;
ian@0 2248
ian@0 2249 mutex_lock(&port_mutex);
ian@0 2250 mutex_lock(&state->mutex);
ian@0 2251 if (state->port) {
ian@0 2252 ret = -EINVAL;
ian@0 2253 goto out;
ian@0 2254 }
ian@0 2255
ian@0 2256 state->port = port;
ian@0 2257
ian@0 2258 port->cons = drv->cons;
ian@0 2259 port->info = state->info;
ian@0 2260
ian@0 2261 /*
ian@0 2262 * If this port is a console, then the spinlock is already
ian@0 2263 * initialised.
ian@0 2264 */
ian@0 2265 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
ian@0 2266 spin_lock_init(&port->lock);
ian@0 2267 lockdep_set_class(&port->lock, &port_lock_key);
ian@0 2268 }
ian@0 2269
ian@0 2270 uart_configure_port(drv, state, port);
ian@0 2271
ian@0 2272 /*
ian@0 2273 * Register the port whether it's detected or not. This allows
ian@0 2274 * setserial to be used to alter this ports parameters.
ian@0 2275 */
ian@0 2276 tty_register_device(drv->tty_driver, port->line, port->dev);
ian@0 2277
ian@0 2278 /*
ian@0 2279 * If this driver supports console, and it hasn't been
ian@0 2280 * successfully registered yet, try to re-register it.
ian@0 2281 * It may be that the port was not available.
ian@0 2282 */
ian@0 2283 if (port->type != PORT_UNKNOWN &&
ian@0 2284 port->cons && !(port->cons->flags & CON_ENABLED))
ian@0 2285 register_console(port->cons);
ian@0 2286
ian@0 2287 /*
ian@0 2288 * Ensure UPF_DEAD is not set.
ian@0 2289 */
ian@0 2290 port->flags &= ~UPF_DEAD;
ian@0 2291
ian@0 2292 out:
ian@0 2293 mutex_unlock(&state->mutex);
ian@0 2294 mutex_unlock(&port_mutex);
ian@0 2295
ian@0 2296 return ret;
ian@0 2297 }
ian@0 2298
ian@0 2299 /**
ian@0 2300 * uart_remove_one_port - detach a driver defined port structure
ian@0 2301 * @drv: pointer to the uart low level driver structure for this port
ian@0 2302 * @port: uart port structure for this port
ian@0 2303 *
ian@0 2304 * This unhooks (and hangs up) the specified port structure from the
ian@0 2305 * core driver. No further calls will be made to the low-level code
ian@0 2306 * for this port.
ian@0 2307 */
ian@0 2308 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
ian@0 2309 {
ian@0 2310 struct uart_state *state = drv->state + port->line;
ian@0 2311 struct uart_info *info;
ian@0 2312
ian@0 2313 BUG_ON(in_interrupt());
ian@0 2314
ian@0 2315 if (state->port != port)
ian@0 2316 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
ian@0 2317 state->port, port);
ian@0 2318
ian@0 2319 mutex_lock(&port_mutex);
ian@0 2320
ian@0 2321 /*
ian@0 2322 * Mark the port "dead" - this prevents any opens from
ian@0 2323 * succeeding while we shut down the port.
ian@0 2324 */
ian@0 2325 mutex_lock(&state->mutex);
ian@0 2326 port->flags |= UPF_DEAD;
ian@0 2327 mutex_unlock(&state->mutex);
ian@0 2328
ian@0 2329 /*
ian@0 2330 * Remove the devices from the tty layer
ian@0 2331 */
ian@0 2332 tty_unregister_device(drv->tty_driver, port->line);
ian@0 2333
ian@0 2334 info = state->info;
ian@0 2335 if (info && info->tty)
ian@0 2336 tty_vhangup(info->tty);
ian@0 2337
ian@0 2338 /*
ian@0 2339 * All users of this port should now be disconnected from
ian@0 2340 * this driver, and the port shut down. We should be the
ian@0 2341 * only thread fiddling with this port from now on.
ian@0 2342 */
ian@0 2343 state->info = NULL;
ian@0 2344
ian@0 2345 /*
ian@0 2346 * Free the port IO and memory resources, if any.
ian@0 2347 */
ian@0 2348 if (port->type != PORT_UNKNOWN)
ian@0 2349 port->ops->release_port(port);
ian@0 2350
ian@0 2351 /*
ian@0 2352 * Indicate that there isn't a port here anymore.
ian@0 2353 */
ian@0 2354 port->type = PORT_UNKNOWN;
ian@0 2355
ian@0 2356 /*
ian@0 2357 * Kill the tasklet, and free resources.
ian@0 2358 */
ian@0 2359 if (info) {
ian@0 2360 tasklet_kill(&info->tlet);
ian@0 2361 kfree(info);
ian@0 2362 }
ian@0 2363
ian@0 2364 state->port = NULL;
ian@0 2365 mutex_unlock(&port_mutex);
ian@0 2366
ian@0 2367 return 0;
ian@0 2368 }
ian@0 2369
ian@0 2370 /*
ian@0 2371 * Are the two ports equivalent?
ian@0 2372 */
ian@0 2373 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
ian@0 2374 {
ian@0 2375 if (port1->iotype != port2->iotype)
ian@0 2376 return 0;
ian@0 2377
ian@0 2378 switch (port1->iotype) {
ian@0 2379 case UPIO_PORT:
ian@0 2380 return (port1->iobase == port2->iobase);
ian@0 2381 case UPIO_HUB6:
ian@0 2382 return (port1->iobase == port2->iobase) &&
ian@0 2383 (port1->hub6 == port2->hub6);
ian@0 2384 case UPIO_MEM:
ian@0 2385 case UPIO_MEM32:
ian@0 2386 case UPIO_AU:
ian@0 2387 case UPIO_TSI:
ian@0 2388 return (port1->mapbase == port2->mapbase);
ian@0 2389 }
ian@0 2390 return 0;
ian@0 2391 }
ian@0 2392 EXPORT_SYMBOL(uart_match_port);
ian@0 2393
ian@0 2394 EXPORT_SYMBOL(uart_write_wakeup);
ian@0 2395 EXPORT_SYMBOL(uart_register_driver);
ian@0 2396 EXPORT_SYMBOL(uart_unregister_driver);
ian@0 2397 EXPORT_SYMBOL(uart_suspend_port);
ian@0 2398 EXPORT_SYMBOL(uart_resume_port);
ian@0 2399 EXPORT_SYMBOL(uart_add_one_port);
ian@0 2400 EXPORT_SYMBOL(uart_remove_one_port);
ian@0 2401
ian@0 2402 MODULE_DESCRIPTION("Serial driver core");
ian@0 2403 MODULE_LICENSE("GPL");