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