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