1ab4382d2SGreg Kroah-Hartman /* 2ab4382d2SGreg Kroah-Hartman * Driver core for serial ports 3ab4382d2SGreg Kroah-Hartman * 4ab4382d2SGreg Kroah-Hartman * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. 5ab4382d2SGreg Kroah-Hartman * 6ab4382d2SGreg Kroah-Hartman * Copyright 1999 ARM Limited 7ab4382d2SGreg Kroah-Hartman * Copyright (C) 2000-2001 Deep Blue Solutions Ltd. 8ab4382d2SGreg Kroah-Hartman * 9ab4382d2SGreg Kroah-Hartman * This program is free software; you can redistribute it and/or modify 10ab4382d2SGreg Kroah-Hartman * it under the terms of the GNU General Public License as published by 11ab4382d2SGreg Kroah-Hartman * the Free Software Foundation; either version 2 of the License, or 12ab4382d2SGreg Kroah-Hartman * (at your option) any later version. 13ab4382d2SGreg Kroah-Hartman * 14ab4382d2SGreg Kroah-Hartman * This program is distributed in the hope that it will be useful, 15ab4382d2SGreg Kroah-Hartman * but WITHOUT ANY WARRANTY; without even the implied warranty of 16ab4382d2SGreg Kroah-Hartman * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17ab4382d2SGreg Kroah-Hartman * GNU General Public License for more details. 18ab4382d2SGreg Kroah-Hartman * 19ab4382d2SGreg Kroah-Hartman * You should have received a copy of the GNU General Public License 20ab4382d2SGreg Kroah-Hartman * along with this program; if not, write to the Free Software 21ab4382d2SGreg Kroah-Hartman * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22ab4382d2SGreg Kroah-Hartman */ 23ab4382d2SGreg Kroah-Hartman #include <linux/module.h> 24ab4382d2SGreg Kroah-Hartman #include <linux/tty.h> 25027d7dacSJiri Slaby #include <linux/tty_flip.h> 26ab4382d2SGreg Kroah-Hartman #include <linux/slab.h> 27ab4382d2SGreg Kroah-Hartman #include <linux/init.h> 28ab4382d2SGreg Kroah-Hartman #include <linux/console.h> 29a208ffd2SGrant Likely #include <linux/of.h> 30ab4382d2SGreg Kroah-Hartman #include <linux/proc_fs.h> 31ab4382d2SGreg Kroah-Hartman #include <linux/seq_file.h> 32ab4382d2SGreg Kroah-Hartman #include <linux/device.h> 33ab4382d2SGreg Kroah-Hartman #include <linux/serial.h> /* for serial_state and serial_icounter_struct */ 34ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h> 35ab4382d2SGreg Kroah-Hartman #include <linux/delay.h> 36ab4382d2SGreg Kroah-Hartman #include <linux/mutex.h> 37ab4382d2SGreg Kroah-Hartman 38ab4382d2SGreg Kroah-Hartman #include <asm/irq.h> 39ab4382d2SGreg Kroah-Hartman #include <asm/uaccess.h> 40ab4382d2SGreg Kroah-Hartman 41ab4382d2SGreg Kroah-Hartman /* 42ab4382d2SGreg Kroah-Hartman * This is used to lock changes in serial line configuration. 43ab4382d2SGreg Kroah-Hartman */ 44ab4382d2SGreg Kroah-Hartman static DEFINE_MUTEX(port_mutex); 45ab4382d2SGreg Kroah-Hartman 46ab4382d2SGreg Kroah-Hartman /* 47ab4382d2SGreg Kroah-Hartman * lockdep: port->lock is initialized in two places, but we 48ab4382d2SGreg Kroah-Hartman * want only one lock-class: 49ab4382d2SGreg Kroah-Hartman */ 50ab4382d2SGreg Kroah-Hartman static struct lock_class_key port_lock_key; 51ab4382d2SGreg Kroah-Hartman 52ab4382d2SGreg Kroah-Hartman #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8) 53ab4382d2SGreg Kroah-Hartman 54ab4382d2SGreg Kroah-Hartman static void uart_change_speed(struct tty_struct *tty, struct uart_state *state, 55ab4382d2SGreg Kroah-Hartman struct ktermios *old_termios); 561f33a51dSJiri Slaby static void uart_wait_until_sent(struct tty_struct *tty, int timeout); 576f538fe3SLinus Walleij static void uart_change_pm(struct uart_state *state, 586f538fe3SLinus Walleij enum uart_pm_state pm_state); 59ab4382d2SGreg Kroah-Hartman 60b922e19dSJiri Slaby static void uart_port_shutdown(struct tty_port *port); 61b922e19dSJiri Slaby 62299245a1SPeter Hurley static int uart_dcd_enabled(struct uart_port *uport) 63299245a1SPeter Hurley { 64d4260b51SPeter Hurley return !!(uport->status & UPSTAT_DCD_ENABLE); 65299245a1SPeter Hurley } 66299245a1SPeter Hurley 679ed19428SPeter Hurley static inline struct uart_port *uart_port_ref(struct uart_state *state) 689ed19428SPeter Hurley { 699ed19428SPeter Hurley if (atomic_add_unless(&state->refcount, 1, 0)) 709ed19428SPeter Hurley return state->uart_port; 719ed19428SPeter Hurley return NULL; 729ed19428SPeter Hurley } 739ed19428SPeter Hurley 749ed19428SPeter Hurley static inline void uart_port_deref(struct uart_port *uport) 759ed19428SPeter Hurley { 769ed19428SPeter Hurley if (uport && atomic_dec_and_test(&uport->state->refcount)) 779ed19428SPeter Hurley wake_up(&uport->state->remove_wait); 789ed19428SPeter Hurley } 799ed19428SPeter Hurley 809ed19428SPeter Hurley #define uart_port_lock(state, flags) \ 819ed19428SPeter Hurley ({ \ 829ed19428SPeter Hurley struct uart_port *__uport = uart_port_ref(state); \ 839ed19428SPeter Hurley if (__uport) \ 849ed19428SPeter Hurley spin_lock_irqsave(&__uport->lock, flags); \ 859ed19428SPeter Hurley __uport; \ 869ed19428SPeter Hurley }) 879ed19428SPeter Hurley 889ed19428SPeter Hurley #define uart_port_unlock(uport, flags) \ 899ed19428SPeter Hurley ({ \ 909ed19428SPeter Hurley struct uart_port *__uport = uport; \ 919ed19428SPeter Hurley if (__uport) \ 929ed19428SPeter Hurley spin_unlock_irqrestore(&__uport->lock, flags); \ 939ed19428SPeter Hurley uart_port_deref(__uport); \ 949ed19428SPeter Hurley }) 959ed19428SPeter Hurley 964047b371SPeter Hurley static inline struct uart_port *uart_port_check(struct uart_state *state) 974047b371SPeter Hurley { 987da4b8b7SPeter Hurley lockdep_assert_held(&state->port.mutex); 994047b371SPeter Hurley return state->uart_port; 1004047b371SPeter Hurley } 1014047b371SPeter Hurley 102ab4382d2SGreg Kroah-Hartman /* 103ab4382d2SGreg Kroah-Hartman * This routine is used by the interrupt handler to schedule processing in 104ab4382d2SGreg Kroah-Hartman * the software interrupt portion of the driver. 105ab4382d2SGreg Kroah-Hartman */ 106ab4382d2SGreg Kroah-Hartman void uart_write_wakeup(struct uart_port *port) 107ab4382d2SGreg Kroah-Hartman { 108ab4382d2SGreg Kroah-Hartman struct uart_state *state = port->state; 109ab4382d2SGreg Kroah-Hartman /* 110ab4382d2SGreg Kroah-Hartman * This means you called this function _after_ the port was 111ab4382d2SGreg Kroah-Hartman * closed. No cookie for you. 112ab4382d2SGreg Kroah-Hartman */ 113ab4382d2SGreg Kroah-Hartman BUG_ON(!state); 1146a3e492bSJiri Slaby tty_wakeup(state->port.tty); 115ab4382d2SGreg Kroah-Hartman } 116ab4382d2SGreg Kroah-Hartman 117ab4382d2SGreg Kroah-Hartman static void uart_stop(struct tty_struct *tty) 118ab4382d2SGreg Kroah-Hartman { 119ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1209ed19428SPeter Hurley struct uart_port *port; 121ab4382d2SGreg Kroah-Hartman unsigned long flags; 122ab4382d2SGreg Kroah-Hartman 1239ed19428SPeter Hurley port = uart_port_lock(state, flags); 1249ed19428SPeter Hurley if (port) 125ab4382d2SGreg Kroah-Hartman port->ops->stop_tx(port); 1269ed19428SPeter Hurley uart_port_unlock(port, flags); 127ab4382d2SGreg Kroah-Hartman } 128ab4382d2SGreg Kroah-Hartman 129ab4382d2SGreg Kroah-Hartman static void __uart_start(struct tty_struct *tty) 130ab4382d2SGreg Kroah-Hartman { 131ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 132ab4382d2SGreg Kroah-Hartman struct uart_port *port = state->uart_port; 133ab4382d2SGreg Kroah-Hartman 1349ed19428SPeter Hurley if (port && !uart_tx_stopped(port)) 135ab4382d2SGreg Kroah-Hartman port->ops->start_tx(port); 136ab4382d2SGreg Kroah-Hartman } 137ab4382d2SGreg Kroah-Hartman 138ab4382d2SGreg Kroah-Hartman static void uart_start(struct tty_struct *tty) 139ab4382d2SGreg Kroah-Hartman { 140ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1419ed19428SPeter Hurley struct uart_port *port; 142ab4382d2SGreg Kroah-Hartman unsigned long flags; 143ab4382d2SGreg Kroah-Hartman 1449ed19428SPeter Hurley port = uart_port_lock(state, flags); 145ab4382d2SGreg Kroah-Hartman __uart_start(tty); 1469ed19428SPeter Hurley uart_port_unlock(port, flags); 147ab4382d2SGreg Kroah-Hartman } 148ab4382d2SGreg Kroah-Hartman 149f4581cabSDenys Vlasenko static void 150ab4382d2SGreg Kroah-Hartman uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear) 151ab4382d2SGreg Kroah-Hartman { 152ab4382d2SGreg Kroah-Hartman unsigned long flags; 153ab4382d2SGreg Kroah-Hartman unsigned int old; 154ab4382d2SGreg Kroah-Hartman 155ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 156ab4382d2SGreg Kroah-Hartman old = port->mctrl; 157ab4382d2SGreg Kroah-Hartman port->mctrl = (old & ~clear) | set; 158ab4382d2SGreg Kroah-Hartman if (old != port->mctrl) 159ab4382d2SGreg Kroah-Hartman port->ops->set_mctrl(port, port->mctrl); 160ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 161ab4382d2SGreg Kroah-Hartman } 162ab4382d2SGreg Kroah-Hartman 163ab4382d2SGreg Kroah-Hartman #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0) 164ab4382d2SGreg Kroah-Hartman #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear) 165ab4382d2SGreg Kroah-Hartman 166ab4382d2SGreg Kroah-Hartman /* 167ab4382d2SGreg Kroah-Hartman * Startup the port. This will be called once per open. All calls 168ab4382d2SGreg Kroah-Hartman * will be serialised by the per-port mutex. 169ab4382d2SGreg Kroah-Hartman */ 170c0d92be6SJiri Slaby static int uart_port_startup(struct tty_struct *tty, struct uart_state *state, 171c0d92be6SJiri Slaby int init_hw) 172ab4382d2SGreg Kroah-Hartman { 1734047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 174ab4382d2SGreg Kroah-Hartman unsigned long page; 175ab4382d2SGreg Kroah-Hartman int retval = 0; 176ab4382d2SGreg Kroah-Hartman 177ab4382d2SGreg Kroah-Hartman if (uport->type == PORT_UNKNOWN) 178c0d92be6SJiri Slaby return 1; 179ab4382d2SGreg Kroah-Hartman 180ab4382d2SGreg Kroah-Hartman /* 1817deb39edSThomas Pfaff * Make sure the device is in D0 state. 1827deb39edSThomas Pfaff */ 1837deb39edSThomas Pfaff uart_change_pm(state, UART_PM_STATE_ON); 1847deb39edSThomas Pfaff 1857deb39edSThomas Pfaff /* 186ab4382d2SGreg Kroah-Hartman * Initialise and allocate the transmit and temporary 187ab4382d2SGreg Kroah-Hartman * buffer. 188ab4382d2SGreg Kroah-Hartman */ 189ab4382d2SGreg Kroah-Hartman if (!state->xmit.buf) { 190ab4382d2SGreg Kroah-Hartman /* This is protected by the per port mutex */ 191ab4382d2SGreg Kroah-Hartman page = get_zeroed_page(GFP_KERNEL); 192ab4382d2SGreg Kroah-Hartman if (!page) 193ab4382d2SGreg Kroah-Hartman return -ENOMEM; 194ab4382d2SGreg Kroah-Hartman 195ab4382d2SGreg Kroah-Hartman state->xmit.buf = (unsigned char *) page; 196ab4382d2SGreg Kroah-Hartman uart_circ_clear(&state->xmit); 197ab4382d2SGreg Kroah-Hartman } 198ab4382d2SGreg Kroah-Hartman 199ab4382d2SGreg Kroah-Hartman retval = uport->ops->startup(uport); 200ab4382d2SGreg Kroah-Hartman if (retval == 0) { 201c7d7abffSJiri Slaby if (uart_console(uport) && uport->cons->cflag) { 202adc8d746SAlan Cox tty->termios.c_cflag = uport->cons->cflag; 203c7d7abffSJiri Slaby uport->cons->cflag = 0; 204c7d7abffSJiri Slaby } 205ab4382d2SGreg Kroah-Hartman /* 206ab4382d2SGreg Kroah-Hartman * Initialise the hardware port settings. 207ab4382d2SGreg Kroah-Hartman */ 208ab4382d2SGreg Kroah-Hartman uart_change_speed(tty, state, NULL); 209ab4382d2SGreg Kroah-Hartman 210ab4382d2SGreg Kroah-Hartman /* 211ab4382d2SGreg Kroah-Hartman * Setup the RTS and DTR signals once the 212ab4382d2SGreg Kroah-Hartman * port is open and ready to respond. 213ab4382d2SGreg Kroah-Hartman */ 2149db276f8SPeter Hurley if (init_hw && C_BAUD(tty)) 215ab4382d2SGreg Kroah-Hartman uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR); 216ab4382d2SGreg Kroah-Hartman } 217ab4382d2SGreg Kroah-Hartman 2180055197eSJiri Slaby /* 2190055197eSJiri Slaby * This is to allow setserial on this port. People may want to set 2200055197eSJiri Slaby * port/irq/type and then reconfigure the port properly if it failed 2210055197eSJiri Slaby * now. 2220055197eSJiri Slaby */ 223ab4382d2SGreg Kroah-Hartman if (retval && capable(CAP_SYS_ADMIN)) 224c0d92be6SJiri Slaby return 1; 225c0d92be6SJiri Slaby 226c0d92be6SJiri Slaby return retval; 227c0d92be6SJiri Slaby } 228c0d92be6SJiri Slaby 229c0d92be6SJiri Slaby static int uart_startup(struct tty_struct *tty, struct uart_state *state, 230c0d92be6SJiri Slaby int init_hw) 231c0d92be6SJiri Slaby { 232c0d92be6SJiri Slaby struct tty_port *port = &state->port; 233c0d92be6SJiri Slaby int retval; 234c0d92be6SJiri Slaby 235d41861caSPeter Hurley if (tty_port_initialized(port)) 236c0d92be6SJiri Slaby return 0; 237c0d92be6SJiri Slaby 238c0d92be6SJiri Slaby retval = uart_port_startup(tty, state, init_hw); 239b3b57646SRob Herring if (retval) 240b3b57646SRob Herring set_bit(TTY_IO_ERROR, &tty->flags); 241ab4382d2SGreg Kroah-Hartman 242ab4382d2SGreg Kroah-Hartman return retval; 243ab4382d2SGreg Kroah-Hartman } 244ab4382d2SGreg Kroah-Hartman 245ab4382d2SGreg Kroah-Hartman /* 246ab4382d2SGreg Kroah-Hartman * This routine will shutdown a serial port; interrupts are disabled, and 247ab4382d2SGreg Kroah-Hartman * DTR is dropped if the hangup on close termio flag is on. Calls to 248ab4382d2SGreg Kroah-Hartman * uart_shutdown are serialised by the per-port semaphore. 249af224ca2SPeter Hurley * 250af224ca2SPeter Hurley * uport == NULL if uart_port has already been removed 251ab4382d2SGreg Kroah-Hartman */ 252ab4382d2SGreg Kroah-Hartman static void uart_shutdown(struct tty_struct *tty, struct uart_state *state) 253ab4382d2SGreg Kroah-Hartman { 2544047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 255ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 256ab4382d2SGreg Kroah-Hartman 257ab4382d2SGreg Kroah-Hartman /* 258ab4382d2SGreg Kroah-Hartman * Set the TTY IO error marker 259ab4382d2SGreg Kroah-Hartman */ 260ab4382d2SGreg Kroah-Hartman if (tty) 261ab4382d2SGreg Kroah-Hartman set_bit(TTY_IO_ERROR, &tty->flags); 262ab4382d2SGreg Kroah-Hartman 263d41861caSPeter Hurley if (tty_port_initialized(port)) { 264d41861caSPeter Hurley tty_port_set_initialized(port, 0); 265d41861caSPeter Hurley 266ab4382d2SGreg Kroah-Hartman /* 267ab4382d2SGreg Kroah-Hartman * Turn off DTR and RTS early. 268ab4382d2SGreg Kroah-Hartman */ 269af224ca2SPeter Hurley if (uport && uart_console(uport) && tty) 270ae84db96SPeter Hurley uport->cons->cflag = tty->termios.c_cflag; 271ae84db96SPeter Hurley 2729db276f8SPeter Hurley if (!tty || C_HUPCL(tty)) 273ab4382d2SGreg Kroah-Hartman uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS); 274ab4382d2SGreg Kroah-Hartman 275b922e19dSJiri Slaby uart_port_shutdown(port); 276ab4382d2SGreg Kroah-Hartman } 277ab4382d2SGreg Kroah-Hartman 278ab4382d2SGreg Kroah-Hartman /* 279d208a3bfSDoug Anderson * It's possible for shutdown to be called after suspend if we get 280d208a3bfSDoug Anderson * a DCD drop (hangup) at just the right time. Clear suspended bit so 281d208a3bfSDoug Anderson * we don't try to resume a port that has been shutdown. 282ab4382d2SGreg Kroah-Hartman */ 28380f02d54SPeter Hurley tty_port_set_suspended(port, 0); 284ab4382d2SGreg Kroah-Hartman 285ab4382d2SGreg Kroah-Hartman /* 286ab4382d2SGreg Kroah-Hartman * Free the transmit buffer page. 287ab4382d2SGreg Kroah-Hartman */ 288ab4382d2SGreg Kroah-Hartman if (state->xmit.buf) { 289ab4382d2SGreg Kroah-Hartman free_page((unsigned long)state->xmit.buf); 290ab4382d2SGreg Kroah-Hartman state->xmit.buf = NULL; 291ab4382d2SGreg Kroah-Hartman } 292ab4382d2SGreg Kroah-Hartman } 293ab4382d2SGreg Kroah-Hartman 294ab4382d2SGreg Kroah-Hartman /** 295ab4382d2SGreg Kroah-Hartman * uart_update_timeout - update per-port FIFO timeout. 296ab4382d2SGreg Kroah-Hartman * @port: uart_port structure describing the port 297ab4382d2SGreg Kroah-Hartman * @cflag: termios cflag value 298ab4382d2SGreg Kroah-Hartman * @baud: speed of the port 299ab4382d2SGreg Kroah-Hartman * 300ab4382d2SGreg Kroah-Hartman * Set the port FIFO timeout value. The @cflag value should 301ab4382d2SGreg Kroah-Hartman * reflect the actual hardware settings. 302ab4382d2SGreg Kroah-Hartman */ 303ab4382d2SGreg Kroah-Hartman void 304ab4382d2SGreg Kroah-Hartman uart_update_timeout(struct uart_port *port, unsigned int cflag, 305ab4382d2SGreg Kroah-Hartman unsigned int baud) 306ab4382d2SGreg Kroah-Hartman { 307ab4382d2SGreg Kroah-Hartman unsigned int bits; 308ab4382d2SGreg Kroah-Hartman 309ab4382d2SGreg Kroah-Hartman /* byte size and parity */ 310ab4382d2SGreg Kroah-Hartman switch (cflag & CSIZE) { 311ab4382d2SGreg Kroah-Hartman case CS5: 312ab4382d2SGreg Kroah-Hartman bits = 7; 313ab4382d2SGreg Kroah-Hartman break; 314ab4382d2SGreg Kroah-Hartman case CS6: 315ab4382d2SGreg Kroah-Hartman bits = 8; 316ab4382d2SGreg Kroah-Hartman break; 317ab4382d2SGreg Kroah-Hartman case CS7: 318ab4382d2SGreg Kroah-Hartman bits = 9; 319ab4382d2SGreg Kroah-Hartman break; 320ab4382d2SGreg Kroah-Hartman default: 321ab4382d2SGreg Kroah-Hartman bits = 10; 322ab4382d2SGreg Kroah-Hartman break; /* CS8 */ 323ab4382d2SGreg Kroah-Hartman } 324ab4382d2SGreg Kroah-Hartman 325ab4382d2SGreg Kroah-Hartman if (cflag & CSTOPB) 326ab4382d2SGreg Kroah-Hartman bits++; 327ab4382d2SGreg Kroah-Hartman if (cflag & PARENB) 328ab4382d2SGreg Kroah-Hartman bits++; 329ab4382d2SGreg Kroah-Hartman 330ab4382d2SGreg Kroah-Hartman /* 331ab4382d2SGreg Kroah-Hartman * The total number of bits to be transmitted in the fifo. 332ab4382d2SGreg Kroah-Hartman */ 333ab4382d2SGreg Kroah-Hartman bits = bits * port->fifosize; 334ab4382d2SGreg Kroah-Hartman 335ab4382d2SGreg Kroah-Hartman /* 336ab4382d2SGreg Kroah-Hartman * Figure the timeout to send the above number of bits. 337ab4382d2SGreg Kroah-Hartman * Add .02 seconds of slop 338ab4382d2SGreg Kroah-Hartman */ 339ab4382d2SGreg Kroah-Hartman port->timeout = (HZ * bits) / baud + HZ/50; 340ab4382d2SGreg Kroah-Hartman } 341ab4382d2SGreg Kroah-Hartman 342ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_update_timeout); 343ab4382d2SGreg Kroah-Hartman 344ab4382d2SGreg Kroah-Hartman /** 345ab4382d2SGreg Kroah-Hartman * uart_get_baud_rate - return baud rate for a particular port 346ab4382d2SGreg Kroah-Hartman * @port: uart_port structure describing the port in question. 347ab4382d2SGreg Kroah-Hartman * @termios: desired termios settings. 348ab4382d2SGreg Kroah-Hartman * @old: old termios (or NULL) 349ab4382d2SGreg Kroah-Hartman * @min: minimum acceptable baud rate 350ab4382d2SGreg Kroah-Hartman * @max: maximum acceptable baud rate 351ab4382d2SGreg Kroah-Hartman * 352ab4382d2SGreg Kroah-Hartman * Decode the termios structure into a numeric baud rate, 353ab4382d2SGreg Kroah-Hartman * taking account of the magic 38400 baud rate (with spd_* 354ab4382d2SGreg Kroah-Hartman * flags), and mapping the %B0 rate to 9600 baud. 355ab4382d2SGreg Kroah-Hartman * 356ab4382d2SGreg Kroah-Hartman * If the new baud rate is invalid, try the old termios setting. 357ab4382d2SGreg Kroah-Hartman * If it's still invalid, we try 9600 baud. 358ab4382d2SGreg Kroah-Hartman * 359ab4382d2SGreg Kroah-Hartman * Update the @termios structure to reflect the baud rate 360ab4382d2SGreg Kroah-Hartman * we're actually going to be using. Don't do this for the case 361ab4382d2SGreg Kroah-Hartman * where B0 is requested ("hang up"). 362ab4382d2SGreg Kroah-Hartman */ 363ab4382d2SGreg Kroah-Hartman unsigned int 364ab4382d2SGreg Kroah-Hartman uart_get_baud_rate(struct uart_port *port, struct ktermios *termios, 365ab4382d2SGreg Kroah-Hartman struct ktermios *old, unsigned int min, unsigned int max) 366ab4382d2SGreg Kroah-Hartman { 367f10a2233SJoakim Nordell unsigned int try; 368f10a2233SJoakim Nordell unsigned int baud; 369f10a2233SJoakim Nordell unsigned int altbaud; 370ab4382d2SGreg Kroah-Hartman int hung_up = 0; 371ab4382d2SGreg Kroah-Hartman upf_t flags = port->flags & UPF_SPD_MASK; 372ab4382d2SGreg Kroah-Hartman 373f10a2233SJoakim Nordell switch (flags) { 374f10a2233SJoakim Nordell case UPF_SPD_HI: 375ab4382d2SGreg Kroah-Hartman altbaud = 57600; 376f10a2233SJoakim Nordell break; 377f10a2233SJoakim Nordell case UPF_SPD_VHI: 378ab4382d2SGreg Kroah-Hartman altbaud = 115200; 379f10a2233SJoakim Nordell break; 380f10a2233SJoakim Nordell case UPF_SPD_SHI: 381ab4382d2SGreg Kroah-Hartman altbaud = 230400; 382f10a2233SJoakim Nordell break; 383f10a2233SJoakim Nordell case UPF_SPD_WARP: 384ab4382d2SGreg Kroah-Hartman altbaud = 460800; 385f10a2233SJoakim Nordell break; 386f10a2233SJoakim Nordell default: 387f10a2233SJoakim Nordell altbaud = 38400; 388f10a2233SJoakim Nordell break; 389f10a2233SJoakim Nordell } 390ab4382d2SGreg Kroah-Hartman 391ab4382d2SGreg Kroah-Hartman for (try = 0; try < 2; try++) { 392ab4382d2SGreg Kroah-Hartman baud = tty_termios_baud_rate(termios); 393ab4382d2SGreg Kroah-Hartman 394ab4382d2SGreg Kroah-Hartman /* 395ab4382d2SGreg Kroah-Hartman * The spd_hi, spd_vhi, spd_shi, spd_warp kludge... 396ab4382d2SGreg Kroah-Hartman * Die! Die! Die! 397ab4382d2SGreg Kroah-Hartman */ 398547039ecSPeter Hurley if (try == 0 && baud == 38400) 399ab4382d2SGreg Kroah-Hartman baud = altbaud; 400ab4382d2SGreg Kroah-Hartman 401ab4382d2SGreg Kroah-Hartman /* 402ab4382d2SGreg Kroah-Hartman * Special case: B0 rate. 403ab4382d2SGreg Kroah-Hartman */ 404ab4382d2SGreg Kroah-Hartman if (baud == 0) { 405ab4382d2SGreg Kroah-Hartman hung_up = 1; 406ab4382d2SGreg Kroah-Hartman baud = 9600; 407ab4382d2SGreg Kroah-Hartman } 408ab4382d2SGreg Kroah-Hartman 409ab4382d2SGreg Kroah-Hartman if (baud >= min && baud <= max) 410ab4382d2SGreg Kroah-Hartman return baud; 411ab4382d2SGreg Kroah-Hartman 412ab4382d2SGreg Kroah-Hartman /* 413ab4382d2SGreg Kroah-Hartman * Oops, the quotient was zero. Try again with 414ab4382d2SGreg Kroah-Hartman * the old baud rate if possible. 415ab4382d2SGreg Kroah-Hartman */ 416ab4382d2SGreg Kroah-Hartman termios->c_cflag &= ~CBAUD; 417ab4382d2SGreg Kroah-Hartman if (old) { 418ab4382d2SGreg Kroah-Hartman baud = tty_termios_baud_rate(old); 419ab4382d2SGreg Kroah-Hartman if (!hung_up) 420ab4382d2SGreg Kroah-Hartman tty_termios_encode_baud_rate(termios, 421ab4382d2SGreg Kroah-Hartman baud, baud); 422ab4382d2SGreg Kroah-Hartman old = NULL; 423ab4382d2SGreg Kroah-Hartman continue; 424ab4382d2SGreg Kroah-Hartman } 425ab4382d2SGreg Kroah-Hartman 426ab4382d2SGreg Kroah-Hartman /* 427ab4382d2SGreg Kroah-Hartman * As a last resort, if the range cannot be met then clip to 428ab4382d2SGreg Kroah-Hartman * the nearest chip supported rate. 429ab4382d2SGreg Kroah-Hartman */ 430ab4382d2SGreg Kroah-Hartman if (!hung_up) { 431ab4382d2SGreg Kroah-Hartman if (baud <= min) 432ab4382d2SGreg Kroah-Hartman tty_termios_encode_baud_rate(termios, 433ab4382d2SGreg Kroah-Hartman min + 1, min + 1); 434ab4382d2SGreg Kroah-Hartman else 435ab4382d2SGreg Kroah-Hartman tty_termios_encode_baud_rate(termios, 436ab4382d2SGreg Kroah-Hartman max - 1, max - 1); 437ab4382d2SGreg Kroah-Hartman } 438ab4382d2SGreg Kroah-Hartman } 439ab4382d2SGreg Kroah-Hartman /* Should never happen */ 440ab4382d2SGreg Kroah-Hartman WARN_ON(1); 441ab4382d2SGreg Kroah-Hartman return 0; 442ab4382d2SGreg Kroah-Hartman } 443ab4382d2SGreg Kroah-Hartman 444ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_get_baud_rate); 445ab4382d2SGreg Kroah-Hartman 446ab4382d2SGreg Kroah-Hartman /** 447ab4382d2SGreg Kroah-Hartman * uart_get_divisor - return uart clock divisor 448ab4382d2SGreg Kroah-Hartman * @port: uart_port structure describing the port. 449ab4382d2SGreg Kroah-Hartman * @baud: desired baud rate 450ab4382d2SGreg Kroah-Hartman * 451ab4382d2SGreg Kroah-Hartman * Calculate the uart clock divisor for the port. 452ab4382d2SGreg Kroah-Hartman */ 453ab4382d2SGreg Kroah-Hartman unsigned int 454ab4382d2SGreg Kroah-Hartman uart_get_divisor(struct uart_port *port, unsigned int baud) 455ab4382d2SGreg Kroah-Hartman { 456ab4382d2SGreg Kroah-Hartman unsigned int quot; 457ab4382d2SGreg Kroah-Hartman 458ab4382d2SGreg Kroah-Hartman /* 459ab4382d2SGreg Kroah-Hartman * Old custom speed handling. 460ab4382d2SGreg Kroah-Hartman */ 461ab4382d2SGreg Kroah-Hartman if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST) 462ab4382d2SGreg Kroah-Hartman quot = port->custom_divisor; 463ab4382d2SGreg Kroah-Hartman else 46497d24634SUwe Kleine-König quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud); 465ab4382d2SGreg Kroah-Hartman 466ab4382d2SGreg Kroah-Hartman return quot; 467ab4382d2SGreg Kroah-Hartman } 468ab4382d2SGreg Kroah-Hartman 469ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_get_divisor); 470ab4382d2SGreg Kroah-Hartman 4717c8ab967SPeter Hurley /* Caller holds port mutex */ 472ab4382d2SGreg Kroah-Hartman static void uart_change_speed(struct tty_struct *tty, struct uart_state *state, 473ab4382d2SGreg Kroah-Hartman struct ktermios *old_termios) 474ab4382d2SGreg Kroah-Hartman { 4754047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 476ab4382d2SGreg Kroah-Hartman struct ktermios *termios; 477391f93f2SPeter Hurley int hw_stopped; 478ab4382d2SGreg Kroah-Hartman 479ab4382d2SGreg Kroah-Hartman /* 480ab4382d2SGreg Kroah-Hartman * If we have no tty, termios, or the port does not exist, 481ab4382d2SGreg Kroah-Hartman * then we can't set the parameters for this port. 482ab4382d2SGreg Kroah-Hartman */ 483adc8d746SAlan Cox if (!tty || uport->type == PORT_UNKNOWN) 484ab4382d2SGreg Kroah-Hartman return; 485ab4382d2SGreg Kroah-Hartman 486adc8d746SAlan Cox termios = &tty->termios; 487c18b55fdSPeter Hurley uport->ops->set_termios(uport, termios, old_termios); 488ab4382d2SGreg Kroah-Hartman 489ab4382d2SGreg Kroah-Hartman /* 490299245a1SPeter Hurley * Set modem status enables based on termios cflag 491ab4382d2SGreg Kroah-Hartman */ 492299245a1SPeter Hurley spin_lock_irq(&uport->lock); 493ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CRTSCTS) 494299245a1SPeter Hurley uport->status |= UPSTAT_CTS_ENABLE; 495ab4382d2SGreg Kroah-Hartman else 496299245a1SPeter Hurley uport->status &= ~UPSTAT_CTS_ENABLE; 497ab4382d2SGreg Kroah-Hartman 498ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CLOCAL) 499299245a1SPeter Hurley uport->status &= ~UPSTAT_DCD_ENABLE; 500ab4382d2SGreg Kroah-Hartman else 501299245a1SPeter Hurley uport->status |= UPSTAT_DCD_ENABLE; 502391f93f2SPeter Hurley 503391f93f2SPeter Hurley /* reset sw-assisted CTS flow control based on (possibly) new mode */ 504391f93f2SPeter Hurley hw_stopped = uport->hw_stopped; 505391f93f2SPeter Hurley uport->hw_stopped = uart_softcts_mode(uport) && 506391f93f2SPeter Hurley !(uport->ops->get_mctrl(uport) & TIOCM_CTS); 507391f93f2SPeter Hurley if (uport->hw_stopped) { 508391f93f2SPeter Hurley if (!hw_stopped) 509391f93f2SPeter Hurley uport->ops->stop_tx(uport); 510391f93f2SPeter Hurley } else { 511391f93f2SPeter Hurley if (hw_stopped) 512391f93f2SPeter Hurley __uart_start(tty); 513391f93f2SPeter Hurley } 514299245a1SPeter Hurley spin_unlock_irq(&uport->lock); 515ab4382d2SGreg Kroah-Hartman } 516ab4382d2SGreg Kroah-Hartman 517f5291eccSPeter Hurley static int uart_put_char(struct tty_struct *tty, unsigned char c) 518ab4382d2SGreg Kroah-Hartman { 519f5291eccSPeter Hurley struct uart_state *state = tty->driver_data; 5209ed19428SPeter Hurley struct uart_port *port; 521f5291eccSPeter Hurley struct circ_buf *circ; 522ab4382d2SGreg Kroah-Hartman unsigned long flags; 523ab4382d2SGreg Kroah-Hartman int ret = 0; 524ab4382d2SGreg Kroah-Hartman 525f5291eccSPeter Hurley circ = &state->xmit; 526ab4382d2SGreg Kroah-Hartman if (!circ->buf) 527ab4382d2SGreg Kroah-Hartman return 0; 528ab4382d2SGreg Kroah-Hartman 5299ed19428SPeter Hurley port = uart_port_lock(state, flags); 5309ed19428SPeter Hurley if (port && uart_circ_chars_free(circ) != 0) { 531ab4382d2SGreg Kroah-Hartman circ->buf[circ->head] = c; 532ab4382d2SGreg Kroah-Hartman circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1); 533ab4382d2SGreg Kroah-Hartman ret = 1; 534ab4382d2SGreg Kroah-Hartman } 5359ed19428SPeter Hurley uart_port_unlock(port, flags); 536ab4382d2SGreg Kroah-Hartman return ret; 537ab4382d2SGreg Kroah-Hartman } 538ab4382d2SGreg Kroah-Hartman 539ab4382d2SGreg Kroah-Hartman static void uart_flush_chars(struct tty_struct *tty) 540ab4382d2SGreg Kroah-Hartman { 541ab4382d2SGreg Kroah-Hartman uart_start(tty); 542ab4382d2SGreg Kroah-Hartman } 543ab4382d2SGreg Kroah-Hartman 544ab4382d2SGreg Kroah-Hartman static int uart_write(struct tty_struct *tty, 545ab4382d2SGreg Kroah-Hartman const unsigned char *buf, int count) 546ab4382d2SGreg Kroah-Hartman { 547ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 548ab4382d2SGreg Kroah-Hartman struct uart_port *port; 549ab4382d2SGreg Kroah-Hartman struct circ_buf *circ; 550ab4382d2SGreg Kroah-Hartman unsigned long flags; 551ab4382d2SGreg Kroah-Hartman int c, ret = 0; 552ab4382d2SGreg Kroah-Hartman 553ab4382d2SGreg Kroah-Hartman /* 554ab4382d2SGreg Kroah-Hartman * This means you called this function _after_ the port was 555ab4382d2SGreg Kroah-Hartman * closed. No cookie for you. 556ab4382d2SGreg Kroah-Hartman */ 557ab4382d2SGreg Kroah-Hartman if (!state) { 558ab4382d2SGreg Kroah-Hartman WARN_ON(1); 559ab4382d2SGreg Kroah-Hartman return -EL3HLT; 560ab4382d2SGreg Kroah-Hartman } 561ab4382d2SGreg Kroah-Hartman 562ab4382d2SGreg Kroah-Hartman circ = &state->xmit; 563ab4382d2SGreg Kroah-Hartman if (!circ->buf) 564ab4382d2SGreg Kroah-Hartman return 0; 565ab4382d2SGreg Kroah-Hartman 5669ed19428SPeter Hurley port = uart_port_lock(state, flags); 5679ed19428SPeter Hurley while (port) { 568ab4382d2SGreg Kroah-Hartman c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE); 569ab4382d2SGreg Kroah-Hartman if (count < c) 570ab4382d2SGreg Kroah-Hartman c = count; 571ab4382d2SGreg Kroah-Hartman if (c <= 0) 572ab4382d2SGreg Kroah-Hartman break; 573ab4382d2SGreg Kroah-Hartman memcpy(circ->buf + circ->head, buf, c); 574ab4382d2SGreg Kroah-Hartman circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1); 575ab4382d2SGreg Kroah-Hartman buf += c; 576ab4382d2SGreg Kroah-Hartman count -= c; 577ab4382d2SGreg Kroah-Hartman ret += c; 578ab4382d2SGreg Kroah-Hartman } 57964dbee31SPeter Hurley 58064dbee31SPeter Hurley __uart_start(tty); 5819ed19428SPeter Hurley uart_port_unlock(port, flags); 582ab4382d2SGreg Kroah-Hartman return ret; 583ab4382d2SGreg Kroah-Hartman } 584ab4382d2SGreg Kroah-Hartman 585ab4382d2SGreg Kroah-Hartman static int uart_write_room(struct tty_struct *tty) 586ab4382d2SGreg Kroah-Hartman { 587ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 5889ed19428SPeter Hurley struct uart_port *port; 589ab4382d2SGreg Kroah-Hartman unsigned long flags; 590ab4382d2SGreg Kroah-Hartman int ret; 591ab4382d2SGreg Kroah-Hartman 5929ed19428SPeter Hurley port = uart_port_lock(state, flags); 593ab4382d2SGreg Kroah-Hartman ret = uart_circ_chars_free(&state->xmit); 5949ed19428SPeter Hurley uart_port_unlock(port, flags); 595ab4382d2SGreg Kroah-Hartman return ret; 596ab4382d2SGreg Kroah-Hartman } 597ab4382d2SGreg Kroah-Hartman 598ab4382d2SGreg Kroah-Hartman static int uart_chars_in_buffer(struct tty_struct *tty) 599ab4382d2SGreg Kroah-Hartman { 600ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 6019ed19428SPeter Hurley struct uart_port *port; 602ab4382d2SGreg Kroah-Hartman unsigned long flags; 603ab4382d2SGreg Kroah-Hartman int ret; 604ab4382d2SGreg Kroah-Hartman 6059ed19428SPeter Hurley port = uart_port_lock(state, flags); 606ab4382d2SGreg Kroah-Hartman ret = uart_circ_chars_pending(&state->xmit); 6079ed19428SPeter Hurley uart_port_unlock(port, flags); 608ab4382d2SGreg Kroah-Hartman return ret; 609ab4382d2SGreg Kroah-Hartman } 610ab4382d2SGreg Kroah-Hartman 611ab4382d2SGreg Kroah-Hartman static void uart_flush_buffer(struct tty_struct *tty) 612ab4382d2SGreg Kroah-Hartman { 613ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 614ab4382d2SGreg Kroah-Hartman struct uart_port *port; 615ab4382d2SGreg Kroah-Hartman unsigned long flags; 616ab4382d2SGreg Kroah-Hartman 617ab4382d2SGreg Kroah-Hartman /* 618ab4382d2SGreg Kroah-Hartman * This means you called this function _after_ the port was 619ab4382d2SGreg Kroah-Hartman * closed. No cookie for you. 620ab4382d2SGreg Kroah-Hartman */ 621ab4382d2SGreg Kroah-Hartman if (!state) { 622ab4382d2SGreg Kroah-Hartman WARN_ON(1); 623ab4382d2SGreg Kroah-Hartman return; 624ab4382d2SGreg Kroah-Hartman } 625ab4382d2SGreg Kroah-Hartman 626ab4382d2SGreg Kroah-Hartman pr_debug("uart_flush_buffer(%d) called\n", tty->index); 627ab4382d2SGreg Kroah-Hartman 6289ed19428SPeter Hurley port = uart_port_lock(state, flags); 6299ed19428SPeter Hurley if (!port) 6309ed19428SPeter Hurley return; 631ab4382d2SGreg Kroah-Hartman uart_circ_clear(&state->xmit); 632ab4382d2SGreg Kroah-Hartman if (port->ops->flush_buffer) 633ab4382d2SGreg Kroah-Hartman port->ops->flush_buffer(port); 6349ed19428SPeter Hurley uart_port_unlock(port, flags); 635ab4382d2SGreg Kroah-Hartman tty_wakeup(tty); 636ab4382d2SGreg Kroah-Hartman } 637ab4382d2SGreg Kroah-Hartman 638ab4382d2SGreg Kroah-Hartman /* 639ab4382d2SGreg Kroah-Hartman * This function is used to send a high-priority XON/XOFF character to 640ab4382d2SGreg Kroah-Hartman * the device 641ab4382d2SGreg Kroah-Hartman */ 642ab4382d2SGreg Kroah-Hartman static void uart_send_xchar(struct tty_struct *tty, char ch) 643ab4382d2SGreg Kroah-Hartman { 644ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 6459ed19428SPeter Hurley struct uart_port *port; 646ab4382d2SGreg Kroah-Hartman unsigned long flags; 647ab4382d2SGreg Kroah-Hartman 6489ed19428SPeter Hurley port = uart_port_ref(state); 6499ed19428SPeter Hurley if (!port) 6509ed19428SPeter Hurley return; 6519ed19428SPeter Hurley 652ab4382d2SGreg Kroah-Hartman if (port->ops->send_xchar) 653ab4382d2SGreg Kroah-Hartman port->ops->send_xchar(port, ch); 654ab4382d2SGreg Kroah-Hartman else { 655ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 656c235ccc1SPeter Hurley port->x_char = ch; 657c235ccc1SPeter Hurley if (ch) 658ab4382d2SGreg Kroah-Hartman port->ops->start_tx(port); 659ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 660ab4382d2SGreg Kroah-Hartman } 6619ed19428SPeter Hurley uart_port_deref(port); 662ab4382d2SGreg Kroah-Hartman } 663ab4382d2SGreg Kroah-Hartman 664ab4382d2SGreg Kroah-Hartman static void uart_throttle(struct tty_struct *tty) 665ab4382d2SGreg Kroah-Hartman { 666ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 6679ed19428SPeter Hurley struct uart_port *port; 668391f93f2SPeter Hurley upstat_t mask = 0; 669ab4382d2SGreg Kroah-Hartman 6709ed19428SPeter Hurley port = uart_port_ref(state); 6719ed19428SPeter Hurley if (!port) 6729ed19428SPeter Hurley return; 6739ed19428SPeter Hurley 674ab4382d2SGreg Kroah-Hartman if (I_IXOFF(tty)) 675391f93f2SPeter Hurley mask |= UPSTAT_AUTOXOFF; 6769db276f8SPeter Hurley if (C_CRTSCTS(tty)) 677391f93f2SPeter Hurley mask |= UPSTAT_AUTORTS; 6789aba8d5bSRussell King 679391f93f2SPeter Hurley if (port->status & mask) { 6809aba8d5bSRussell King port->ops->throttle(port); 681391f93f2SPeter Hurley mask &= ~port->status; 6829aba8d5bSRussell King } 6839aba8d5bSRussell King 684391f93f2SPeter Hurley if (mask & UPSTAT_AUTORTS) 6859aba8d5bSRussell King uart_clear_mctrl(port, TIOCM_RTS); 686b4749b97SPeter Hurley 687b4749b97SPeter Hurley if (mask & UPSTAT_AUTOXOFF) 688b4749b97SPeter Hurley uart_send_xchar(tty, STOP_CHAR(tty)); 6899ed19428SPeter Hurley 6909ed19428SPeter Hurley uart_port_deref(port); 691ab4382d2SGreg Kroah-Hartman } 692ab4382d2SGreg Kroah-Hartman 693ab4382d2SGreg Kroah-Hartman static void uart_unthrottle(struct tty_struct *tty) 694ab4382d2SGreg Kroah-Hartman { 695ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 6969ed19428SPeter Hurley struct uart_port *port; 697391f93f2SPeter Hurley upstat_t mask = 0; 698ab4382d2SGreg Kroah-Hartman 6999ed19428SPeter Hurley port = uart_port_ref(state); 7009ed19428SPeter Hurley if (!port) 7019ed19428SPeter Hurley return; 7029ed19428SPeter Hurley 7039aba8d5bSRussell King if (I_IXOFF(tty)) 704391f93f2SPeter Hurley mask |= UPSTAT_AUTOXOFF; 7059db276f8SPeter Hurley if (C_CRTSCTS(tty)) 706391f93f2SPeter Hurley mask |= UPSTAT_AUTORTS; 7079aba8d5bSRussell King 708391f93f2SPeter Hurley if (port->status & mask) { 7099aba8d5bSRussell King port->ops->unthrottle(port); 710391f93f2SPeter Hurley mask &= ~port->status; 7119aba8d5bSRussell King } 7129aba8d5bSRussell King 713391f93f2SPeter Hurley if (mask & UPSTAT_AUTORTS) 714ab4382d2SGreg Kroah-Hartman uart_set_mctrl(port, TIOCM_RTS); 715b4749b97SPeter Hurley 716b4749b97SPeter Hurley if (mask & UPSTAT_AUTOXOFF) 717b4749b97SPeter Hurley uart_send_xchar(tty, START_CHAR(tty)); 7189ed19428SPeter Hurley 7199ed19428SPeter Hurley uart_port_deref(port); 720ab4382d2SGreg Kroah-Hartman } 721ab4382d2SGreg Kroah-Hartman 7224047b371SPeter Hurley static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo) 723ab4382d2SGreg Kroah-Hartman { 7249f109694SAlan Cox struct uart_state *state = container_of(port, struct uart_state, port); 7254047b371SPeter Hurley struct uart_port *uport; 7264047b371SPeter Hurley int ret = -ENODEV; 7277ba2e769SAlan Cox 72837cd0c99SFengguang Wu memset(retinfo, 0, sizeof(*retinfo)); 7297ba2e769SAlan Cox 7303abe8c76SPeter Hurley /* 7313abe8c76SPeter Hurley * Ensure the state we copy is consistent and no hardware changes 7323abe8c76SPeter Hurley * occur as we go 7333abe8c76SPeter Hurley */ 7343abe8c76SPeter Hurley mutex_lock(&port->mutex); 7354047b371SPeter Hurley uport = uart_port_check(state); 7364047b371SPeter Hurley if (!uport) 7374047b371SPeter Hurley goto out; 7384047b371SPeter Hurley 7397ba2e769SAlan Cox retinfo->type = uport->type; 7407ba2e769SAlan Cox retinfo->line = uport->line; 7417ba2e769SAlan Cox retinfo->port = uport->iobase; 7427ba2e769SAlan Cox if (HIGH_BITS_OFFSET) 7437ba2e769SAlan Cox retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET; 7447ba2e769SAlan Cox retinfo->irq = uport->irq; 7457ba2e769SAlan Cox retinfo->flags = uport->flags; 7467ba2e769SAlan Cox retinfo->xmit_fifo_size = uport->fifosize; 7477ba2e769SAlan Cox retinfo->baud_base = uport->uartclk / 16; 7487ba2e769SAlan Cox retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10; 7497ba2e769SAlan Cox retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ? 7507ba2e769SAlan Cox ASYNC_CLOSING_WAIT_NONE : 7517ba2e769SAlan Cox jiffies_to_msecs(port->closing_wait) / 10; 7527ba2e769SAlan Cox retinfo->custom_divisor = uport->custom_divisor; 7537ba2e769SAlan Cox retinfo->hub6 = uport->hub6; 7547ba2e769SAlan Cox retinfo->io_type = uport->iotype; 7557ba2e769SAlan Cox retinfo->iomem_reg_shift = uport->regshift; 7567ba2e769SAlan Cox retinfo->iomem_base = (void *)(unsigned long)uport->mapbase; 7574047b371SPeter Hurley 7584047b371SPeter Hurley ret = 0; 7594047b371SPeter Hurley out: 760ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 7614047b371SPeter Hurley return ret; 7629f109694SAlan Cox } 7639f109694SAlan Cox 7649f109694SAlan Cox static int uart_get_info_user(struct tty_port *port, 7659f109694SAlan Cox struct serial_struct __user *retinfo) 7669f109694SAlan Cox { 7679f109694SAlan Cox struct serial_struct tmp; 7683abe8c76SPeter Hurley 7694047b371SPeter Hurley if (uart_get_info(port, &tmp) < 0) 7704047b371SPeter Hurley return -EIO; 771ab4382d2SGreg Kroah-Hartman 772ab4382d2SGreg Kroah-Hartman if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 773ab4382d2SGreg Kroah-Hartman return -EFAULT; 774ab4382d2SGreg Kroah-Hartman return 0; 775ab4382d2SGreg Kroah-Hartman } 776ab4382d2SGreg Kroah-Hartman 7777ba2e769SAlan Cox static int uart_set_info(struct tty_struct *tty, struct tty_port *port, 7787ba2e769SAlan Cox struct uart_state *state, 7797ba2e769SAlan Cox struct serial_struct *new_info) 780ab4382d2SGreg Kroah-Hartman { 7814047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 782ab4382d2SGreg Kroah-Hartman unsigned long new_port; 783ab4382d2SGreg Kroah-Hartman unsigned int change_irq, change_port, closing_wait; 784ab4382d2SGreg Kroah-Hartman unsigned int old_custom_divisor, close_delay; 785ab4382d2SGreg Kroah-Hartman upf_t old_flags, new_flags; 786ab4382d2SGreg Kroah-Hartman int retval = 0; 787ab4382d2SGreg Kroah-Hartman 7884047b371SPeter Hurley if (!uport) 7894047b371SPeter Hurley return -EIO; 7904047b371SPeter Hurley 7917ba2e769SAlan Cox new_port = new_info->port; 792ab4382d2SGreg Kroah-Hartman if (HIGH_BITS_OFFSET) 7937ba2e769SAlan Cox new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET; 794ab4382d2SGreg Kroah-Hartman 7957ba2e769SAlan Cox new_info->irq = irq_canonicalize(new_info->irq); 7967ba2e769SAlan Cox close_delay = msecs_to_jiffies(new_info->close_delay * 10); 7977ba2e769SAlan Cox closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ? 7984cb0fbfdSJiri Slaby ASYNC_CLOSING_WAIT_NONE : 7997ba2e769SAlan Cox msecs_to_jiffies(new_info->closing_wait * 10); 800ab4382d2SGreg Kroah-Hartman 801ab4382d2SGreg Kroah-Hartman 802ab4382d2SGreg Kroah-Hartman change_irq = !(uport->flags & UPF_FIXED_PORT) 8037ba2e769SAlan Cox && new_info->irq != uport->irq; 804ab4382d2SGreg Kroah-Hartman 805ab4382d2SGreg Kroah-Hartman /* 806ab4382d2SGreg Kroah-Hartman * Since changing the 'type' of the port changes its resource 807ab4382d2SGreg Kroah-Hartman * allocations, we should treat type changes the same as 808ab4382d2SGreg Kroah-Hartman * IO port changes. 809ab4382d2SGreg Kroah-Hartman */ 810ab4382d2SGreg Kroah-Hartman change_port = !(uport->flags & UPF_FIXED_PORT) 811ab4382d2SGreg Kroah-Hartman && (new_port != uport->iobase || 8127ba2e769SAlan Cox (unsigned long)new_info->iomem_base != uport->mapbase || 8137ba2e769SAlan Cox new_info->hub6 != uport->hub6 || 8147ba2e769SAlan Cox new_info->io_type != uport->iotype || 8157ba2e769SAlan Cox new_info->iomem_reg_shift != uport->regshift || 8167ba2e769SAlan Cox new_info->type != uport->type); 817ab4382d2SGreg Kroah-Hartman 818ab4382d2SGreg Kroah-Hartman old_flags = uport->flags; 8197ba2e769SAlan Cox new_flags = new_info->flags; 820ab4382d2SGreg Kroah-Hartman old_custom_divisor = uport->custom_divisor; 821ab4382d2SGreg Kroah-Hartman 822ab4382d2SGreg Kroah-Hartman if (!capable(CAP_SYS_ADMIN)) { 823ab4382d2SGreg Kroah-Hartman retval = -EPERM; 824ab4382d2SGreg Kroah-Hartman if (change_irq || change_port || 8257ba2e769SAlan Cox (new_info->baud_base != uport->uartclk / 16) || 826ab4382d2SGreg Kroah-Hartman (close_delay != port->close_delay) || 827ab4382d2SGreg Kroah-Hartman (closing_wait != port->closing_wait) || 8287ba2e769SAlan Cox (new_info->xmit_fifo_size && 8297ba2e769SAlan Cox new_info->xmit_fifo_size != uport->fifosize) || 830ab4382d2SGreg Kroah-Hartman (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0)) 831ab4382d2SGreg Kroah-Hartman goto exit; 832ab4382d2SGreg Kroah-Hartman uport->flags = ((uport->flags & ~UPF_USR_MASK) | 833ab4382d2SGreg Kroah-Hartman (new_flags & UPF_USR_MASK)); 8347ba2e769SAlan Cox uport->custom_divisor = new_info->custom_divisor; 835ab4382d2SGreg Kroah-Hartman goto check_and_exit; 836ab4382d2SGreg Kroah-Hartman } 837ab4382d2SGreg Kroah-Hartman 838ab4382d2SGreg Kroah-Hartman /* 839ab4382d2SGreg Kroah-Hartman * Ask the low level driver to verify the settings. 840ab4382d2SGreg Kroah-Hartman */ 841ab4382d2SGreg Kroah-Hartman if (uport->ops->verify_port) 8427ba2e769SAlan Cox retval = uport->ops->verify_port(uport, new_info); 843ab4382d2SGreg Kroah-Hartman 8447ba2e769SAlan Cox if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) || 8457ba2e769SAlan Cox (new_info->baud_base < 9600)) 846ab4382d2SGreg Kroah-Hartman retval = -EINVAL; 847ab4382d2SGreg Kroah-Hartman 848ab4382d2SGreg Kroah-Hartman if (retval) 849ab4382d2SGreg Kroah-Hartman goto exit; 850ab4382d2SGreg Kroah-Hartman 851ab4382d2SGreg Kroah-Hartman if (change_port || change_irq) { 852ab4382d2SGreg Kroah-Hartman retval = -EBUSY; 853ab4382d2SGreg Kroah-Hartman 854ab4382d2SGreg Kroah-Hartman /* 855ab4382d2SGreg Kroah-Hartman * Make sure that we are the sole user of this port. 856ab4382d2SGreg Kroah-Hartman */ 857ab4382d2SGreg Kroah-Hartman if (tty_port_users(port) > 1) 858ab4382d2SGreg Kroah-Hartman goto exit; 859ab4382d2SGreg Kroah-Hartman 860ab4382d2SGreg Kroah-Hartman /* 861ab4382d2SGreg Kroah-Hartman * We need to shutdown the serial port at the old 862ab4382d2SGreg Kroah-Hartman * port/type/irq combination. 863ab4382d2SGreg Kroah-Hartman */ 864ab4382d2SGreg Kroah-Hartman uart_shutdown(tty, state); 865ab4382d2SGreg Kroah-Hartman } 866ab4382d2SGreg Kroah-Hartman 867ab4382d2SGreg Kroah-Hartman if (change_port) { 868ab4382d2SGreg Kroah-Hartman unsigned long old_iobase, old_mapbase; 869ab4382d2SGreg Kroah-Hartman unsigned int old_type, old_iotype, old_hub6, old_shift; 870ab4382d2SGreg Kroah-Hartman 871ab4382d2SGreg Kroah-Hartman old_iobase = uport->iobase; 872ab4382d2SGreg Kroah-Hartman old_mapbase = uport->mapbase; 873ab4382d2SGreg Kroah-Hartman old_type = uport->type; 874ab4382d2SGreg Kroah-Hartman old_hub6 = uport->hub6; 875ab4382d2SGreg Kroah-Hartman old_iotype = uport->iotype; 876ab4382d2SGreg Kroah-Hartman old_shift = uport->regshift; 877ab4382d2SGreg Kroah-Hartman 878ab4382d2SGreg Kroah-Hartman /* 879ab4382d2SGreg Kroah-Hartman * Free and release old regions 880ab4382d2SGreg Kroah-Hartman */ 881a7cfaf16SFabio Estevam if (old_type != PORT_UNKNOWN && uport->ops->release_port) 882ab4382d2SGreg Kroah-Hartman uport->ops->release_port(uport); 883ab4382d2SGreg Kroah-Hartman 884ab4382d2SGreg Kroah-Hartman uport->iobase = new_port; 8857ba2e769SAlan Cox uport->type = new_info->type; 8867ba2e769SAlan Cox uport->hub6 = new_info->hub6; 8877ba2e769SAlan Cox uport->iotype = new_info->io_type; 8887ba2e769SAlan Cox uport->regshift = new_info->iomem_reg_shift; 8897ba2e769SAlan Cox uport->mapbase = (unsigned long)new_info->iomem_base; 890ab4382d2SGreg Kroah-Hartman 891ab4382d2SGreg Kroah-Hartman /* 892ab4382d2SGreg Kroah-Hartman * Claim and map the new regions 893ab4382d2SGreg Kroah-Hartman */ 894a7cfaf16SFabio Estevam if (uport->type != PORT_UNKNOWN && uport->ops->request_port) { 895ab4382d2SGreg Kroah-Hartman retval = uport->ops->request_port(uport); 896ab4382d2SGreg Kroah-Hartman } else { 897ab4382d2SGreg Kroah-Hartman /* Always success - Jean II */ 898ab4382d2SGreg Kroah-Hartman retval = 0; 899ab4382d2SGreg Kroah-Hartman } 900ab4382d2SGreg Kroah-Hartman 901ab4382d2SGreg Kroah-Hartman /* 902ab4382d2SGreg Kroah-Hartman * If we fail to request resources for the 903ab4382d2SGreg Kroah-Hartman * new port, try to restore the old settings. 904ab4382d2SGreg Kroah-Hartman */ 9057deb39edSThomas Pfaff if (retval) { 906ab4382d2SGreg Kroah-Hartman uport->iobase = old_iobase; 907ab4382d2SGreg Kroah-Hartman uport->type = old_type; 908ab4382d2SGreg Kroah-Hartman uport->hub6 = old_hub6; 909ab4382d2SGreg Kroah-Hartman uport->iotype = old_iotype; 910ab4382d2SGreg Kroah-Hartman uport->regshift = old_shift; 911ab4382d2SGreg Kroah-Hartman uport->mapbase = old_mapbase; 9127deb39edSThomas Pfaff 9137deb39edSThomas Pfaff if (old_type != PORT_UNKNOWN) { 914ab4382d2SGreg Kroah-Hartman retval = uport->ops->request_port(uport); 915ab4382d2SGreg Kroah-Hartman /* 916ab4382d2SGreg Kroah-Hartman * If we failed to restore the old settings, 917ab4382d2SGreg Kroah-Hartman * we fail like this. 918ab4382d2SGreg Kroah-Hartman */ 919ab4382d2SGreg Kroah-Hartman if (retval) 920ab4382d2SGreg Kroah-Hartman uport->type = PORT_UNKNOWN; 921ab4382d2SGreg Kroah-Hartman 922ab4382d2SGreg Kroah-Hartman /* 923ab4382d2SGreg Kroah-Hartman * We failed anyway. 924ab4382d2SGreg Kroah-Hartman */ 925ab4382d2SGreg Kroah-Hartman retval = -EBUSY; 9267deb39edSThomas Pfaff } 9277deb39edSThomas Pfaff 928ab4382d2SGreg Kroah-Hartman /* Added to return the correct error -Ram Gupta */ 929ab4382d2SGreg Kroah-Hartman goto exit; 930ab4382d2SGreg Kroah-Hartman } 931ab4382d2SGreg Kroah-Hartman } 932ab4382d2SGreg Kroah-Hartman 933ab4382d2SGreg Kroah-Hartman if (change_irq) 9347ba2e769SAlan Cox uport->irq = new_info->irq; 935ab4382d2SGreg Kroah-Hartman if (!(uport->flags & UPF_FIXED_PORT)) 9367ba2e769SAlan Cox uport->uartclk = new_info->baud_base * 16; 937ab4382d2SGreg Kroah-Hartman uport->flags = (uport->flags & ~UPF_CHANGE_MASK) | 938ab4382d2SGreg Kroah-Hartman (new_flags & UPF_CHANGE_MASK); 9397ba2e769SAlan Cox uport->custom_divisor = new_info->custom_divisor; 940ab4382d2SGreg Kroah-Hartman port->close_delay = close_delay; 941ab4382d2SGreg Kroah-Hartman port->closing_wait = closing_wait; 9427ba2e769SAlan Cox if (new_info->xmit_fifo_size) 9437ba2e769SAlan Cox uport->fifosize = new_info->xmit_fifo_size; 944d6c53c0eSJiri Slaby port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0; 945ab4382d2SGreg Kroah-Hartman 946ab4382d2SGreg Kroah-Hartman check_and_exit: 947ab4382d2SGreg Kroah-Hartman retval = 0; 948ab4382d2SGreg Kroah-Hartman if (uport->type == PORT_UNKNOWN) 949ab4382d2SGreg Kroah-Hartman goto exit; 950d41861caSPeter Hurley if (tty_port_initialized(port)) { 951ab4382d2SGreg Kroah-Hartman if (((old_flags ^ uport->flags) & UPF_SPD_MASK) || 952ab4382d2SGreg Kroah-Hartman old_custom_divisor != uport->custom_divisor) { 953ab4382d2SGreg Kroah-Hartman /* 954ab4382d2SGreg Kroah-Hartman * If they're setting up a custom divisor or speed, 955ab4382d2SGreg Kroah-Hartman * instead of clearing it, then bitch about it. No 956ab4382d2SGreg Kroah-Hartman * need to rate-limit; it's CAP_SYS_ADMIN only. 957ab4382d2SGreg Kroah-Hartman */ 958ab4382d2SGreg Kroah-Hartman if (uport->flags & UPF_SPD_MASK) { 9592f2dafe7SSudip Mukherjee dev_notice(uport->dev, 9602f2dafe7SSudip Mukherjee "%s sets custom speed on %s. This is deprecated.\n", 9612f2dafe7SSudip Mukherjee current->comm, 962429b4749SRasmus Villemoes tty_name(port->tty)); 963ab4382d2SGreg Kroah-Hartman } 964ab4382d2SGreg Kroah-Hartman uart_change_speed(tty, state, NULL); 965ab4382d2SGreg Kroah-Hartman } 966b3b57646SRob Herring } else { 967ab4382d2SGreg Kroah-Hartman retval = uart_startup(tty, state, 1); 968b3b57646SRob Herring if (retval > 0) 969b3b57646SRob Herring retval = 0; 970b3b57646SRob Herring } 971ab4382d2SGreg Kroah-Hartman exit: 9727ba2e769SAlan Cox return retval; 9737ba2e769SAlan Cox } 9747ba2e769SAlan Cox 9757ba2e769SAlan Cox static int uart_set_info_user(struct tty_struct *tty, struct uart_state *state, 9767ba2e769SAlan Cox struct serial_struct __user *newinfo) 9777ba2e769SAlan Cox { 9787ba2e769SAlan Cox struct serial_struct new_serial; 9797ba2e769SAlan Cox struct tty_port *port = &state->port; 9807ba2e769SAlan Cox int retval; 9817ba2e769SAlan Cox 9827ba2e769SAlan Cox if (copy_from_user(&new_serial, newinfo, sizeof(new_serial))) 9837ba2e769SAlan Cox return -EFAULT; 9847ba2e769SAlan Cox 9857ba2e769SAlan Cox /* 9867ba2e769SAlan Cox * This semaphore protects port->count. It is also 9877ba2e769SAlan Cox * very useful to prevent opens. Also, take the 9887ba2e769SAlan Cox * port configuration semaphore to make sure that a 9897ba2e769SAlan Cox * module insertion/removal doesn't change anything 9907ba2e769SAlan Cox * under us. 9917ba2e769SAlan Cox */ 9927ba2e769SAlan Cox mutex_lock(&port->mutex); 9937ba2e769SAlan Cox retval = uart_set_info(tty, port, state, &new_serial); 994ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 995ab4382d2SGreg Kroah-Hartman return retval; 996ab4382d2SGreg Kroah-Hartman } 997ab4382d2SGreg Kroah-Hartman 998ab4382d2SGreg Kroah-Hartman /** 999ab4382d2SGreg Kroah-Hartman * uart_get_lsr_info - get line status register info 1000ab4382d2SGreg Kroah-Hartman * @tty: tty associated with the UART 1001ab4382d2SGreg Kroah-Hartman * @state: UART being queried 1002ab4382d2SGreg Kroah-Hartman * @value: returned modem value 1003ab4382d2SGreg Kroah-Hartman */ 1004ab4382d2SGreg Kroah-Hartman static int uart_get_lsr_info(struct tty_struct *tty, 1005ab4382d2SGreg Kroah-Hartman struct uart_state *state, unsigned int __user *value) 1006ab4382d2SGreg Kroah-Hartman { 10074047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 1008ab4382d2SGreg Kroah-Hartman unsigned int result; 1009ab4382d2SGreg Kroah-Hartman 1010ab4382d2SGreg Kroah-Hartman result = uport->ops->tx_empty(uport); 1011ab4382d2SGreg Kroah-Hartman 1012ab4382d2SGreg Kroah-Hartman /* 1013ab4382d2SGreg Kroah-Hartman * If we're about to load something into the transmit 1014ab4382d2SGreg Kroah-Hartman * register, we'll pretend the transmitter isn't empty to 1015ab4382d2SGreg Kroah-Hartman * avoid a race condition (depending on when the transmit 1016ab4382d2SGreg Kroah-Hartman * interrupt happens). 1017ab4382d2SGreg Kroah-Hartman */ 1018ab4382d2SGreg Kroah-Hartman if (uport->x_char || 1019ab4382d2SGreg Kroah-Hartman ((uart_circ_chars_pending(&state->xmit) > 0) && 1020d01f4d18SPeter Hurley !uart_tx_stopped(uport))) 1021ab4382d2SGreg Kroah-Hartman result &= ~TIOCSER_TEMT; 1022ab4382d2SGreg Kroah-Hartman 1023ab4382d2SGreg Kroah-Hartman return put_user(result, value); 1024ab4382d2SGreg Kroah-Hartman } 1025ab4382d2SGreg Kroah-Hartman 102660b33c13SAlan Cox static int uart_tiocmget(struct tty_struct *tty) 1027ab4382d2SGreg Kroah-Hartman { 1028ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1029ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 10304047b371SPeter Hurley struct uart_port *uport; 1031ab4382d2SGreg Kroah-Hartman int result = -EIO; 1032ab4382d2SGreg Kroah-Hartman 1033ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 10344047b371SPeter Hurley uport = uart_port_check(state); 10354047b371SPeter Hurley if (!uport) 10364047b371SPeter Hurley goto out; 10374047b371SPeter Hurley 103818900ca6SPeter Hurley if (!tty_io_error(tty)) { 1039ab4382d2SGreg Kroah-Hartman result = uport->mctrl; 1040ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 1041ab4382d2SGreg Kroah-Hartman result |= uport->ops->get_mctrl(uport); 1042ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 1043ab4382d2SGreg Kroah-Hartman } 10444047b371SPeter Hurley out: 1045ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 1046ab4382d2SGreg Kroah-Hartman return result; 1047ab4382d2SGreg Kroah-Hartman } 1048ab4382d2SGreg Kroah-Hartman 1049ab4382d2SGreg Kroah-Hartman static int 105020b9d177SAlan Cox uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear) 1051ab4382d2SGreg Kroah-Hartman { 1052ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1053ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 10544047b371SPeter Hurley struct uart_port *uport; 1055ab4382d2SGreg Kroah-Hartman int ret = -EIO; 1056ab4382d2SGreg Kroah-Hartman 1057ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 10584047b371SPeter Hurley uport = uart_port_check(state); 10594047b371SPeter Hurley if (!uport) 10604047b371SPeter Hurley goto out; 10614047b371SPeter Hurley 106218900ca6SPeter Hurley if (!tty_io_error(tty)) { 1063ab4382d2SGreg Kroah-Hartman uart_update_mctrl(uport, set, clear); 1064ab4382d2SGreg Kroah-Hartman ret = 0; 1065ab4382d2SGreg Kroah-Hartman } 10664047b371SPeter Hurley out: 1067ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 1068ab4382d2SGreg Kroah-Hartman return ret; 1069ab4382d2SGreg Kroah-Hartman } 1070ab4382d2SGreg Kroah-Hartman 1071ab4382d2SGreg Kroah-Hartman static int uart_break_ctl(struct tty_struct *tty, int break_state) 1072ab4382d2SGreg Kroah-Hartman { 1073ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1074ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 10754047b371SPeter Hurley struct uart_port *uport; 10764047b371SPeter Hurley int ret = -EIO; 1077ab4382d2SGreg Kroah-Hartman 1078ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 10794047b371SPeter Hurley uport = uart_port_check(state); 10804047b371SPeter Hurley if (!uport) 10814047b371SPeter Hurley goto out; 1082ab4382d2SGreg Kroah-Hartman 1083ab4382d2SGreg Kroah-Hartman if (uport->type != PORT_UNKNOWN) 1084ab4382d2SGreg Kroah-Hartman uport->ops->break_ctl(uport, break_state); 10854047b371SPeter Hurley ret = 0; 10864047b371SPeter Hurley out: 1087ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 10884047b371SPeter Hurley return ret; 1089ab4382d2SGreg Kroah-Hartman } 1090ab4382d2SGreg Kroah-Hartman 1091ab4382d2SGreg Kroah-Hartman static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state) 1092ab4382d2SGreg Kroah-Hartman { 1093ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 10944047b371SPeter Hurley struct uart_port *uport; 1095ab4382d2SGreg Kroah-Hartman int flags, ret; 1096ab4382d2SGreg Kroah-Hartman 1097ab4382d2SGreg Kroah-Hartman if (!capable(CAP_SYS_ADMIN)) 1098ab4382d2SGreg Kroah-Hartman return -EPERM; 1099ab4382d2SGreg Kroah-Hartman 1100ab4382d2SGreg Kroah-Hartman /* 1101ab4382d2SGreg Kroah-Hartman * Take the per-port semaphore. This prevents count from 1102ab4382d2SGreg Kroah-Hartman * changing, and hence any extra opens of the port while 1103ab4382d2SGreg Kroah-Hartman * we're auto-configuring. 1104ab4382d2SGreg Kroah-Hartman */ 1105ab4382d2SGreg Kroah-Hartman if (mutex_lock_interruptible(&port->mutex)) 1106ab4382d2SGreg Kroah-Hartman return -ERESTARTSYS; 1107ab4382d2SGreg Kroah-Hartman 11084047b371SPeter Hurley uport = uart_port_check(state); 11094047b371SPeter Hurley if (!uport) { 11104047b371SPeter Hurley ret = -EIO; 11114047b371SPeter Hurley goto out; 11124047b371SPeter Hurley } 11134047b371SPeter Hurley 1114ab4382d2SGreg Kroah-Hartman ret = -EBUSY; 1115ab4382d2SGreg Kroah-Hartman if (tty_port_users(port) == 1) { 1116ab4382d2SGreg Kroah-Hartman uart_shutdown(tty, state); 1117ab4382d2SGreg Kroah-Hartman 1118ab4382d2SGreg Kroah-Hartman /* 1119ab4382d2SGreg Kroah-Hartman * If we already have a port type configured, 1120ab4382d2SGreg Kroah-Hartman * we must release its resources. 1121ab4382d2SGreg Kroah-Hartman */ 1122a7cfaf16SFabio Estevam if (uport->type != PORT_UNKNOWN && uport->ops->release_port) 1123ab4382d2SGreg Kroah-Hartman uport->ops->release_port(uport); 1124ab4382d2SGreg Kroah-Hartman 1125ab4382d2SGreg Kroah-Hartman flags = UART_CONFIG_TYPE; 1126ab4382d2SGreg Kroah-Hartman if (uport->flags & UPF_AUTO_IRQ) 1127ab4382d2SGreg Kroah-Hartman flags |= UART_CONFIG_IRQ; 1128ab4382d2SGreg Kroah-Hartman 1129ab4382d2SGreg Kroah-Hartman /* 1130ab4382d2SGreg Kroah-Hartman * This will claim the ports resources if 1131ab4382d2SGreg Kroah-Hartman * a port is found. 1132ab4382d2SGreg Kroah-Hartman */ 1133ab4382d2SGreg Kroah-Hartman uport->ops->config_port(uport, flags); 1134ab4382d2SGreg Kroah-Hartman 1135ab4382d2SGreg Kroah-Hartman ret = uart_startup(tty, state, 1); 1136b3b57646SRob Herring if (ret > 0) 1137b3b57646SRob Herring ret = 0; 1138ab4382d2SGreg Kroah-Hartman } 11394047b371SPeter Hurley out: 1140ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 1141ab4382d2SGreg Kroah-Hartman return ret; 1142ab4382d2SGreg Kroah-Hartman } 1143ab4382d2SGreg Kroah-Hartman 11441fdc3106SAlexander Shiyan static void uart_enable_ms(struct uart_port *uport) 11451fdc3106SAlexander Shiyan { 11461fdc3106SAlexander Shiyan /* 11471fdc3106SAlexander Shiyan * Force modem status interrupts on 11481fdc3106SAlexander Shiyan */ 11491fdc3106SAlexander Shiyan if (uport->ops->enable_ms) 11501fdc3106SAlexander Shiyan uport->ops->enable_ms(uport); 11511fdc3106SAlexander Shiyan } 11521fdc3106SAlexander Shiyan 1153ab4382d2SGreg Kroah-Hartman /* 1154ab4382d2SGreg Kroah-Hartman * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change 1155ab4382d2SGreg Kroah-Hartman * - mask passed in arg for lines of interest 1156ab4382d2SGreg Kroah-Hartman * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking) 1157ab4382d2SGreg Kroah-Hartman * Caller should use TIOCGICOUNT to see which one it was 1158ab4382d2SGreg Kroah-Hartman * 1159ab4382d2SGreg Kroah-Hartman * FIXME: This wants extracting into a common all driver implementation 1160ab4382d2SGreg Kroah-Hartman * of TIOCMWAIT using tty_port. 1161ab4382d2SGreg Kroah-Hartman */ 11629ed19428SPeter Hurley static int uart_wait_modem_status(struct uart_state *state, unsigned long arg) 1163ab4382d2SGreg Kroah-Hartman { 11649ed19428SPeter Hurley struct uart_port *uport; 1165ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 1166ab4382d2SGreg Kroah-Hartman DECLARE_WAITQUEUE(wait, current); 1167ab4382d2SGreg Kroah-Hartman struct uart_icount cprev, cnow; 1168ab4382d2SGreg Kroah-Hartman int ret; 1169ab4382d2SGreg Kroah-Hartman 1170ab4382d2SGreg Kroah-Hartman /* 1171ab4382d2SGreg Kroah-Hartman * note the counters on entry 1172ab4382d2SGreg Kroah-Hartman */ 11739ed19428SPeter Hurley uport = uart_port_ref(state); 11749ed19428SPeter Hurley if (!uport) 11759ed19428SPeter Hurley return -EIO; 1176ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 1177ab4382d2SGreg Kroah-Hartman memcpy(&cprev, &uport->icount, sizeof(struct uart_icount)); 11781fdc3106SAlexander Shiyan uart_enable_ms(uport); 1179ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 1180ab4382d2SGreg Kroah-Hartman 1181ab4382d2SGreg Kroah-Hartman add_wait_queue(&port->delta_msr_wait, &wait); 1182ab4382d2SGreg Kroah-Hartman for (;;) { 1183ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 1184ab4382d2SGreg Kroah-Hartman memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); 1185ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 1186ab4382d2SGreg Kroah-Hartman 1187ab4382d2SGreg Kroah-Hartman set_current_state(TASK_INTERRUPTIBLE); 1188ab4382d2SGreg Kroah-Hartman 1189ab4382d2SGreg Kroah-Hartman if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 1190ab4382d2SGreg Kroah-Hartman ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 1191ab4382d2SGreg Kroah-Hartman ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || 1192ab4382d2SGreg Kroah-Hartman ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { 1193ab4382d2SGreg Kroah-Hartman ret = 0; 1194ab4382d2SGreg Kroah-Hartman break; 1195ab4382d2SGreg Kroah-Hartman } 1196ab4382d2SGreg Kroah-Hartman 1197ab4382d2SGreg Kroah-Hartman schedule(); 1198ab4382d2SGreg Kroah-Hartman 1199ab4382d2SGreg Kroah-Hartman /* see if a signal did it */ 1200ab4382d2SGreg Kroah-Hartman if (signal_pending(current)) { 1201ab4382d2SGreg Kroah-Hartman ret = -ERESTARTSYS; 1202ab4382d2SGreg Kroah-Hartman break; 1203ab4382d2SGreg Kroah-Hartman } 1204ab4382d2SGreg Kroah-Hartman 1205ab4382d2SGreg Kroah-Hartman cprev = cnow; 1206ab4382d2SGreg Kroah-Hartman } 120797f9f707SFabian Frederick __set_current_state(TASK_RUNNING); 1208ab4382d2SGreg Kroah-Hartman remove_wait_queue(&port->delta_msr_wait, &wait); 12099ed19428SPeter Hurley uart_port_deref(uport); 1210ab4382d2SGreg Kroah-Hartman 1211ab4382d2SGreg Kroah-Hartman return ret; 1212ab4382d2SGreg Kroah-Hartman } 1213ab4382d2SGreg Kroah-Hartman 1214ab4382d2SGreg Kroah-Hartman /* 1215ab4382d2SGreg Kroah-Hartman * Get counter of input serial line interrupts (DCD,RI,DSR,CTS) 1216ab4382d2SGreg Kroah-Hartman * Return: write counters to the user passed counter struct 1217ab4382d2SGreg Kroah-Hartman * NB: both 1->0 and 0->1 transitions are counted except for 1218ab4382d2SGreg Kroah-Hartman * RI where only 0->1 is counted. 1219ab4382d2SGreg Kroah-Hartman */ 1220ab4382d2SGreg Kroah-Hartman static int uart_get_icount(struct tty_struct *tty, 1221ab4382d2SGreg Kroah-Hartman struct serial_icounter_struct *icount) 1222ab4382d2SGreg Kroah-Hartman { 1223ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1224ab4382d2SGreg Kroah-Hartman struct uart_icount cnow; 12259ed19428SPeter Hurley struct uart_port *uport; 1226ab4382d2SGreg Kroah-Hartman 12279ed19428SPeter Hurley uport = uart_port_ref(state); 12289ed19428SPeter Hurley if (!uport) 12299ed19428SPeter Hurley return -EIO; 1230ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 1231ab4382d2SGreg Kroah-Hartman memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); 1232ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 12339ed19428SPeter Hurley uart_port_deref(uport); 1234ab4382d2SGreg Kroah-Hartman 1235ab4382d2SGreg Kroah-Hartman icount->cts = cnow.cts; 1236ab4382d2SGreg Kroah-Hartman icount->dsr = cnow.dsr; 1237ab4382d2SGreg Kroah-Hartman icount->rng = cnow.rng; 1238ab4382d2SGreg Kroah-Hartman icount->dcd = cnow.dcd; 1239ab4382d2SGreg Kroah-Hartman icount->rx = cnow.rx; 1240ab4382d2SGreg Kroah-Hartman icount->tx = cnow.tx; 1241ab4382d2SGreg Kroah-Hartman icount->frame = cnow.frame; 1242ab4382d2SGreg Kroah-Hartman icount->overrun = cnow.overrun; 1243ab4382d2SGreg Kroah-Hartman icount->parity = cnow.parity; 1244ab4382d2SGreg Kroah-Hartman icount->brk = cnow.brk; 1245ab4382d2SGreg Kroah-Hartman icount->buf_overrun = cnow.buf_overrun; 1246ab4382d2SGreg Kroah-Hartman 1247ab4382d2SGreg Kroah-Hartman return 0; 1248ab4382d2SGreg Kroah-Hartman } 1249ab4382d2SGreg Kroah-Hartman 1250a5f276f1SRicardo Ribalda Delgado static int uart_get_rs485_config(struct uart_port *port, 1251a5f276f1SRicardo Ribalda Delgado struct serial_rs485 __user *rs485) 1252a5f276f1SRicardo Ribalda Delgado { 1253bd737f87SRicardo Ribalda Delgado unsigned long flags; 1254bd737f87SRicardo Ribalda Delgado struct serial_rs485 aux; 1255bd737f87SRicardo Ribalda Delgado 1256bd737f87SRicardo Ribalda Delgado spin_lock_irqsave(&port->lock, flags); 1257bd737f87SRicardo Ribalda Delgado aux = port->rs485; 1258bd737f87SRicardo Ribalda Delgado spin_unlock_irqrestore(&port->lock, flags); 1259bd737f87SRicardo Ribalda Delgado 1260bd737f87SRicardo Ribalda Delgado if (copy_to_user(rs485, &aux, sizeof(aux))) 1261a5f276f1SRicardo Ribalda Delgado return -EFAULT; 1262bd737f87SRicardo Ribalda Delgado 1263a5f276f1SRicardo Ribalda Delgado return 0; 1264a5f276f1SRicardo Ribalda Delgado } 1265a5f276f1SRicardo Ribalda Delgado 1266a5f276f1SRicardo Ribalda Delgado static int uart_set_rs485_config(struct uart_port *port, 1267a5f276f1SRicardo Ribalda Delgado struct serial_rs485 __user *rs485_user) 1268a5f276f1SRicardo Ribalda Delgado { 1269a5f276f1SRicardo Ribalda Delgado struct serial_rs485 rs485; 1270a5f276f1SRicardo Ribalda Delgado int ret; 1271bd737f87SRicardo Ribalda Delgado unsigned long flags; 1272a5f276f1SRicardo Ribalda Delgado 1273a5f276f1SRicardo Ribalda Delgado if (!port->rs485_config) 1274a5f276f1SRicardo Ribalda Delgado return -ENOIOCTLCMD; 1275a5f276f1SRicardo Ribalda Delgado 1276a5f276f1SRicardo Ribalda Delgado if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user))) 1277a5f276f1SRicardo Ribalda Delgado return -EFAULT; 1278a5f276f1SRicardo Ribalda Delgado 1279bd737f87SRicardo Ribalda Delgado spin_lock_irqsave(&port->lock, flags); 1280a5f276f1SRicardo Ribalda Delgado ret = port->rs485_config(port, &rs485); 1281bd737f87SRicardo Ribalda Delgado spin_unlock_irqrestore(&port->lock, flags); 1282a5f276f1SRicardo Ribalda Delgado if (ret) 1283a5f276f1SRicardo Ribalda Delgado return ret; 1284a5f276f1SRicardo Ribalda Delgado 1285a5f276f1SRicardo Ribalda Delgado if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485))) 1286a5f276f1SRicardo Ribalda Delgado return -EFAULT; 1287a5f276f1SRicardo Ribalda Delgado 1288a5f276f1SRicardo Ribalda Delgado return 0; 1289a5f276f1SRicardo Ribalda Delgado } 1290a5f276f1SRicardo Ribalda Delgado 1291ab4382d2SGreg Kroah-Hartman /* 1292ab4382d2SGreg Kroah-Hartman * Called via sys_ioctl. We can use spin_lock_irq() here. 1293ab4382d2SGreg Kroah-Hartman */ 1294ab4382d2SGreg Kroah-Hartman static int 12954047b371SPeter Hurley uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg) 1296ab4382d2SGreg Kroah-Hartman { 1297ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1298ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 12994047b371SPeter Hurley struct uart_port *uport; 1300ab4382d2SGreg Kroah-Hartman void __user *uarg = (void __user *)arg; 1301ab4382d2SGreg Kroah-Hartman int ret = -ENOIOCTLCMD; 1302ab4382d2SGreg Kroah-Hartman 1303ab4382d2SGreg Kroah-Hartman 1304ab4382d2SGreg Kroah-Hartman /* 1305ab4382d2SGreg Kroah-Hartman * These ioctls don't rely on the hardware to be present. 1306ab4382d2SGreg Kroah-Hartman */ 1307ab4382d2SGreg Kroah-Hartman switch (cmd) { 1308ab4382d2SGreg Kroah-Hartman case TIOCGSERIAL: 13099f109694SAlan Cox ret = uart_get_info_user(port, uarg); 1310ab4382d2SGreg Kroah-Hartman break; 1311ab4382d2SGreg Kroah-Hartman 1312ab4382d2SGreg Kroah-Hartman case TIOCSSERIAL: 13137c8ab967SPeter Hurley down_write(&tty->termios_rwsem); 13147ba2e769SAlan Cox ret = uart_set_info_user(tty, state, uarg); 13157c8ab967SPeter Hurley up_write(&tty->termios_rwsem); 1316ab4382d2SGreg Kroah-Hartman break; 1317ab4382d2SGreg Kroah-Hartman 1318ab4382d2SGreg Kroah-Hartman case TIOCSERCONFIG: 13197c8ab967SPeter Hurley down_write(&tty->termios_rwsem); 1320ab4382d2SGreg Kroah-Hartman ret = uart_do_autoconfig(tty, state); 13217c8ab967SPeter Hurley up_write(&tty->termios_rwsem); 1322ab4382d2SGreg Kroah-Hartman break; 1323ab4382d2SGreg Kroah-Hartman 1324ab4382d2SGreg Kroah-Hartman case TIOCSERGWILD: /* obsolete */ 1325ab4382d2SGreg Kroah-Hartman case TIOCSERSWILD: /* obsolete */ 1326ab4382d2SGreg Kroah-Hartman ret = 0; 1327ab4382d2SGreg Kroah-Hartman break; 1328ab4382d2SGreg Kroah-Hartman } 1329ab4382d2SGreg Kroah-Hartman 1330ab4382d2SGreg Kroah-Hartman if (ret != -ENOIOCTLCMD) 1331ab4382d2SGreg Kroah-Hartman goto out; 1332ab4382d2SGreg Kroah-Hartman 133318900ca6SPeter Hurley if (tty_io_error(tty)) { 1334ab4382d2SGreg Kroah-Hartman ret = -EIO; 1335ab4382d2SGreg Kroah-Hartman goto out; 1336ab4382d2SGreg Kroah-Hartman } 1337ab4382d2SGreg Kroah-Hartman 1338ab4382d2SGreg Kroah-Hartman /* 1339ab4382d2SGreg Kroah-Hartman * The following should only be used when hardware is present. 1340ab4382d2SGreg Kroah-Hartman */ 1341ab4382d2SGreg Kroah-Hartman switch (cmd) { 1342ab4382d2SGreg Kroah-Hartman case TIOCMIWAIT: 1343ab4382d2SGreg Kroah-Hartman ret = uart_wait_modem_status(state, arg); 1344ab4382d2SGreg Kroah-Hartman break; 1345ab4382d2SGreg Kroah-Hartman } 1346ab4382d2SGreg Kroah-Hartman 1347ab4382d2SGreg Kroah-Hartman if (ret != -ENOIOCTLCMD) 1348ab4382d2SGreg Kroah-Hartman goto out; 1349ab4382d2SGreg Kroah-Hartman 1350ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 13514047b371SPeter Hurley uport = uart_port_check(state); 1352ab4382d2SGreg Kroah-Hartman 13534047b371SPeter Hurley if (!uport || tty_io_error(tty)) { 1354ab4382d2SGreg Kroah-Hartman ret = -EIO; 1355ab4382d2SGreg Kroah-Hartman goto out_up; 1356ab4382d2SGreg Kroah-Hartman } 1357ab4382d2SGreg Kroah-Hartman 1358ab4382d2SGreg Kroah-Hartman /* 1359ab4382d2SGreg Kroah-Hartman * All these rely on hardware being present and need to be 1360ab4382d2SGreg Kroah-Hartman * protected against the tty being hung up. 1361ab4382d2SGreg Kroah-Hartman */ 1362a9c20a9cSRicardo Ribalda Delgado 1363ab4382d2SGreg Kroah-Hartman switch (cmd) { 1364a9c20a9cSRicardo Ribalda Delgado case TIOCSERGETLSR: /* Get line status register */ 1365a9c20a9cSRicardo Ribalda Delgado ret = uart_get_lsr_info(tty, state, uarg); 1366a9c20a9cSRicardo Ribalda Delgado break; 1367a9c20a9cSRicardo Ribalda Delgado 1368a5f276f1SRicardo Ribalda Delgado case TIOCGRS485: 13694047b371SPeter Hurley ret = uart_get_rs485_config(uport, uarg); 1370a5f276f1SRicardo Ribalda Delgado break; 1371a5f276f1SRicardo Ribalda Delgado 1372a5f276f1SRicardo Ribalda Delgado case TIOCSRS485: 13734047b371SPeter Hurley ret = uart_set_rs485_config(uport, uarg); 1374a5f276f1SRicardo Ribalda Delgado break; 13754047b371SPeter Hurley default: 1376ab4382d2SGreg Kroah-Hartman if (uport->ops->ioctl) 1377ab4382d2SGreg Kroah-Hartman ret = uport->ops->ioctl(uport, cmd, arg); 1378ab4382d2SGreg Kroah-Hartman break; 1379ab4382d2SGreg Kroah-Hartman } 1380ab4382d2SGreg Kroah-Hartman out_up: 1381ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 1382ab4382d2SGreg Kroah-Hartman out: 1383ab4382d2SGreg Kroah-Hartman return ret; 1384ab4382d2SGreg Kroah-Hartman } 1385ab4382d2SGreg Kroah-Hartman 1386ab4382d2SGreg Kroah-Hartman static void uart_set_ldisc(struct tty_struct *tty) 1387ab4382d2SGreg Kroah-Hartman { 1388ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 13894047b371SPeter Hurley struct uart_port *uport; 1390ab4382d2SGreg Kroah-Hartman 1391db1b9dfcSPeter Hurley mutex_lock(&state->port.mutex); 13924047b371SPeter Hurley uport = uart_port_check(state); 13934047b371SPeter Hurley if (uport && uport->ops->set_ldisc) 1394732a84a0SPeter Hurley uport->ops->set_ldisc(uport, &tty->termios); 1395db1b9dfcSPeter Hurley mutex_unlock(&state->port.mutex); 1396db1b9dfcSPeter Hurley } 1397ab4382d2SGreg Kroah-Hartman 1398ab4382d2SGreg Kroah-Hartman static void uart_set_termios(struct tty_struct *tty, 1399ab4382d2SGreg Kroah-Hartman struct ktermios *old_termios) 1400ab4382d2SGreg Kroah-Hartman { 1401ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 14024047b371SPeter Hurley struct uart_port *uport; 1403adc8d746SAlan Cox unsigned int cflag = tty->termios.c_cflag; 14042cbacafdSRussell King unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK; 14052cbacafdSRussell King bool sw_changed = false; 1406ab4382d2SGreg Kroah-Hartman 14074047b371SPeter Hurley mutex_lock(&state->port.mutex); 14084047b371SPeter Hurley uport = uart_port_check(state); 14094047b371SPeter Hurley if (!uport) 14104047b371SPeter Hurley goto out; 14114047b371SPeter Hurley 14122cbacafdSRussell King /* 14132cbacafdSRussell King * Drivers doing software flow control also need to know 14142cbacafdSRussell King * about changes to these input settings. 14152cbacafdSRussell King */ 14162cbacafdSRussell King if (uport->flags & UPF_SOFT_FLOW) { 14172cbacafdSRussell King iflag_mask |= IXANY|IXON|IXOFF; 14182cbacafdSRussell King sw_changed = 14192cbacafdSRussell King tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] || 14202cbacafdSRussell King tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP]; 14212cbacafdSRussell King } 1422ab4382d2SGreg Kroah-Hartman 1423ab4382d2SGreg Kroah-Hartman /* 1424ab4382d2SGreg Kroah-Hartman * These are the bits that are used to setup various 1425ab4382d2SGreg Kroah-Hartman * flags in the low level driver. We can ignore the Bfoo 1426ab4382d2SGreg Kroah-Hartman * bits in c_cflag; c_[io]speed will always be set 1427ab4382d2SGreg Kroah-Hartman * appropriately by set_termios() in tty_ioctl.c 1428ab4382d2SGreg Kroah-Hartman */ 1429ab4382d2SGreg Kroah-Hartman if ((cflag ^ old_termios->c_cflag) == 0 && 1430adc8d746SAlan Cox tty->termios.c_ospeed == old_termios->c_ospeed && 1431adc8d746SAlan Cox tty->termios.c_ispeed == old_termios->c_ispeed && 14322cbacafdSRussell King ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 && 14332cbacafdSRussell King !sw_changed) { 14344047b371SPeter Hurley goto out; 1435ab4382d2SGreg Kroah-Hartman } 1436ab4382d2SGreg Kroah-Hartman 1437ab4382d2SGreg Kroah-Hartman uart_change_speed(tty, state, old_termios); 1438c18b55fdSPeter Hurley /* reload cflag from termios; port driver may have overriden flags */ 1439c18b55fdSPeter Hurley cflag = tty->termios.c_cflag; 1440ab4382d2SGreg Kroah-Hartman 1441ab4382d2SGreg Kroah-Hartman /* Handle transition to B0 status */ 1442ab4382d2SGreg Kroah-Hartman if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) 1443dec94e70SRussell King uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR); 1444ab4382d2SGreg Kroah-Hartman /* Handle transition away from B0 status */ 1445ab4382d2SGreg Kroah-Hartman else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) { 1446ab4382d2SGreg Kroah-Hartman unsigned int mask = TIOCM_DTR; 144797ef38b8SPeter Hurley if (!(cflag & CRTSCTS) || !tty_throttled(tty)) 1448ab4382d2SGreg Kroah-Hartman mask |= TIOCM_RTS; 1449dec94e70SRussell King uart_set_mctrl(uport, mask); 1450ab4382d2SGreg Kroah-Hartman } 14514047b371SPeter Hurley out: 14524047b371SPeter Hurley mutex_unlock(&state->port.mutex); 1453ab4382d2SGreg Kroah-Hartman } 1454ab4382d2SGreg Kroah-Hartman 1455ab4382d2SGreg Kroah-Hartman /* 1456ef4f527cSKevin Cernekee * Calls to uart_close() are serialised via the tty_lock in 1457ef4f527cSKevin Cernekee * drivers/tty/tty_io.c:tty_release() 1458ef4f527cSKevin Cernekee * drivers/tty/tty_io.c:do_tty_hangup() 1459ab4382d2SGreg Kroah-Hartman */ 1460ab4382d2SGreg Kroah-Hartman static void uart_close(struct tty_struct *tty, struct file *filp) 1461ab4382d2SGreg Kroah-Hartman { 1462ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1463ab4382d2SGreg Kroah-Hartman struct tty_port *port; 1464ab4382d2SGreg Kroah-Hartman 146591b32f54SPeter Hurley if (!state) { 146691b32f54SPeter Hurley struct uart_driver *drv = tty->driver->driver_state; 146791b32f54SPeter Hurley 146891b32f54SPeter Hurley state = drv->state + tty->index; 146991b32f54SPeter Hurley port = &state->port; 147091b32f54SPeter Hurley spin_lock_irq(&port->lock); 147191b32f54SPeter Hurley --port->count; 147291b32f54SPeter Hurley spin_unlock_irq(&port->lock); 1473ab4382d2SGreg Kroah-Hartman return; 147491b32f54SPeter Hurley } 1475ab4382d2SGreg Kroah-Hartman 1476ab4382d2SGreg Kroah-Hartman port = &state->port; 147739b3d892SPeter Hurley pr_debug("uart_close(%d) called\n", tty->index); 1478ab4382d2SGreg Kroah-Hartman 1479761ed4a9SRob Herring tty_port_close(tty->port, tty, filp); 1480761ed4a9SRob Herring } 1481ab4382d2SGreg Kroah-Hartman 1482761ed4a9SRob Herring static void uart_tty_port_shutdown(struct tty_port *port) 1483761ed4a9SRob Herring { 1484761ed4a9SRob Herring struct uart_state *state = container_of(port, struct uart_state, port); 1485761ed4a9SRob Herring struct uart_port *uport = uart_port_check(state); 1486af224ca2SPeter Hurley 1487ab4382d2SGreg Kroah-Hartman /* 1488ab4382d2SGreg Kroah-Hartman * At this point, we stop accepting input. To do this, we 1489ab4382d2SGreg Kroah-Hartman * disable the receive line status interrupts. 1490ab4382d2SGreg Kroah-Hartman */ 1491a5a2b130SAndy Shevchenko if (WARN(!uport, "detached port still initialized!\n")) 1492a5a2b130SAndy Shevchenko return; 1493761ed4a9SRob Herring 1494a5a2b130SAndy Shevchenko spin_lock_irq(&uport->lock); 1495ab4382d2SGreg Kroah-Hartman uport->ops->stop_rx(uport); 14966fb98fb3SPeter Hurley spin_unlock_irq(&uport->lock); 1497761ed4a9SRob Herring 1498761ed4a9SRob Herring uart_port_shutdown(port); 1499761ed4a9SRob Herring 1500ab4382d2SGreg Kroah-Hartman /* 1501761ed4a9SRob Herring * It's possible for shutdown to be called after suspend if we get 1502761ed4a9SRob Herring * a DCD drop (hangup) at just the right time. Clear suspended bit so 1503761ed4a9SRob Herring * we don't try to resume a port that has been shutdown. 1504ab4382d2SGreg Kroah-Hartman */ 1505761ed4a9SRob Herring tty_port_set_suspended(port, 0); 1506ab4382d2SGreg Kroah-Hartman 15076f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_OFF); 1508ab4382d2SGreg Kroah-Hartman 1509ab4382d2SGreg Kroah-Hartman } 1510ab4382d2SGreg Kroah-Hartman 15111f33a51dSJiri Slaby static void uart_wait_until_sent(struct tty_struct *tty, int timeout) 1512ab4382d2SGreg Kroah-Hartman { 15131f33a51dSJiri Slaby struct uart_state *state = tty->driver_data; 15149ed19428SPeter Hurley struct uart_port *port; 1515ab4382d2SGreg Kroah-Hartman unsigned long char_time, expire; 1516ab4382d2SGreg Kroah-Hartman 15179ed19428SPeter Hurley port = uart_port_ref(state); 15189ed19428SPeter Hurley if (!port || port->type == PORT_UNKNOWN || port->fifosize == 0) { 15199ed19428SPeter Hurley uart_port_deref(port); 1520ab4382d2SGreg Kroah-Hartman return; 15219ed19428SPeter Hurley } 1522ab4382d2SGreg Kroah-Hartman 1523ab4382d2SGreg Kroah-Hartman /* 1524ab4382d2SGreg Kroah-Hartman * Set the check interval to be 1/5 of the estimated time to 1525ab4382d2SGreg Kroah-Hartman * send a single character, and make it at least 1. The check 1526ab4382d2SGreg Kroah-Hartman * interval should also be less than the timeout. 1527ab4382d2SGreg Kroah-Hartman * 1528ab4382d2SGreg Kroah-Hartman * Note: we have to use pretty tight timings here to satisfy 1529ab4382d2SGreg Kroah-Hartman * the NIST-PCTS. 1530ab4382d2SGreg Kroah-Hartman */ 1531ab4382d2SGreg Kroah-Hartman char_time = (port->timeout - HZ/50) / port->fifosize; 1532ab4382d2SGreg Kroah-Hartman char_time = char_time / 5; 1533ab4382d2SGreg Kroah-Hartman if (char_time == 0) 1534ab4382d2SGreg Kroah-Hartman char_time = 1; 1535ab4382d2SGreg Kroah-Hartman if (timeout && timeout < char_time) 1536ab4382d2SGreg Kroah-Hartman char_time = timeout; 1537ab4382d2SGreg Kroah-Hartman 1538ab4382d2SGreg Kroah-Hartman /* 1539ab4382d2SGreg Kroah-Hartman * If the transmitter hasn't cleared in twice the approximate 1540ab4382d2SGreg Kroah-Hartman * amount of time to send the entire FIFO, it probably won't 1541ab4382d2SGreg Kroah-Hartman * ever clear. This assumes the UART isn't doing flow 1542ab4382d2SGreg Kroah-Hartman * control, which is currently the case. Hence, if it ever 1543ab4382d2SGreg Kroah-Hartman * takes longer than port->timeout, this is probably due to a 1544ab4382d2SGreg Kroah-Hartman * UART bug of some kind. So, we clamp the timeout parameter at 1545ab4382d2SGreg Kroah-Hartman * 2*port->timeout. 1546ab4382d2SGreg Kroah-Hartman */ 1547ab4382d2SGreg Kroah-Hartman if (timeout == 0 || timeout > 2 * port->timeout) 1548ab4382d2SGreg Kroah-Hartman timeout = 2 * port->timeout; 1549ab4382d2SGreg Kroah-Hartman 1550ab4382d2SGreg Kroah-Hartman expire = jiffies + timeout; 1551ab4382d2SGreg Kroah-Hartman 1552ab4382d2SGreg Kroah-Hartman pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n", 1553ab4382d2SGreg Kroah-Hartman port->line, jiffies, expire); 1554ab4382d2SGreg Kroah-Hartman 1555ab4382d2SGreg Kroah-Hartman /* 1556ab4382d2SGreg Kroah-Hartman * Check whether the transmitter is empty every 'char_time'. 1557ab4382d2SGreg Kroah-Hartman * 'timeout' / 'expire' give us the maximum amount of time 1558ab4382d2SGreg Kroah-Hartman * we wait. 1559ab4382d2SGreg Kroah-Hartman */ 1560ab4382d2SGreg Kroah-Hartman while (!port->ops->tx_empty(port)) { 1561ab4382d2SGreg Kroah-Hartman msleep_interruptible(jiffies_to_msecs(char_time)); 1562ab4382d2SGreg Kroah-Hartman if (signal_pending(current)) 1563ab4382d2SGreg Kroah-Hartman break; 1564ab4382d2SGreg Kroah-Hartman if (time_after(jiffies, expire)) 1565ab4382d2SGreg Kroah-Hartman break; 1566ab4382d2SGreg Kroah-Hartman } 15679ed19428SPeter Hurley uart_port_deref(port); 1568ab4382d2SGreg Kroah-Hartman } 1569ab4382d2SGreg Kroah-Hartman 1570ab4382d2SGreg Kroah-Hartman /* 1571ef4f527cSKevin Cernekee * Calls to uart_hangup() are serialised by the tty_lock in 1572ef4f527cSKevin Cernekee * drivers/tty/tty_io.c:do_tty_hangup() 1573ef4f527cSKevin Cernekee * This runs from a workqueue and can sleep for a _short_ time only. 1574ab4382d2SGreg Kroah-Hartman */ 1575ab4382d2SGreg Kroah-Hartman static void uart_hangup(struct tty_struct *tty) 1576ab4382d2SGreg Kroah-Hartman { 1577ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1578ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 1579af224ca2SPeter Hurley struct uart_port *uport; 1580ab4382d2SGreg Kroah-Hartman unsigned long flags; 1581ab4382d2SGreg Kroah-Hartman 158239b3d892SPeter Hurley pr_debug("uart_hangup(%d)\n", tty->index); 1583ab4382d2SGreg Kroah-Hartman 1584ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 1585af224ca2SPeter Hurley uport = uart_port_check(state); 1586af224ca2SPeter Hurley WARN(!uport, "hangup of detached port!\n"); 1587af224ca2SPeter Hurley 1588807c8d81SPeter Hurley if (tty_port_active(port)) { 1589ab4382d2SGreg Kroah-Hartman uart_flush_buffer(tty); 1590ab4382d2SGreg Kroah-Hartman uart_shutdown(tty, state); 1591ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 1592ab4382d2SGreg Kroah-Hartman port->count = 0; 1593ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 1594807c8d81SPeter Hurley tty_port_set_active(port, 0); 1595ab4382d2SGreg Kroah-Hartman tty_port_tty_set(port, NULL); 1596af224ca2SPeter Hurley if (uport && !uart_console(uport)) 1597bf903c0cSGeert Uytterhoeven uart_change_pm(state, UART_PM_STATE_OFF); 1598ab4382d2SGreg Kroah-Hartman wake_up_interruptible(&port->open_wait); 1599ab4382d2SGreg Kroah-Hartman wake_up_interruptible(&port->delta_msr_wait); 1600ab4382d2SGreg Kroah-Hartman } 1601ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 1602ab4382d2SGreg Kroah-Hartman } 1603ab4382d2SGreg Kroah-Hartman 1604af224ca2SPeter Hurley /* uport == NULL if uart_port has already been removed */ 16050b1db830SJiri Slaby static void uart_port_shutdown(struct tty_port *port) 16060b1db830SJiri Slaby { 1607b922e19dSJiri Slaby struct uart_state *state = container_of(port, struct uart_state, port); 16084047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 1609b922e19dSJiri Slaby 1610b922e19dSJiri Slaby /* 1611b922e19dSJiri Slaby * clear delta_msr_wait queue to avoid mem leaks: we may free 1612b922e19dSJiri Slaby * the irq here so the queue might never be woken up. Note 1613b922e19dSJiri Slaby * that we won't end up waiting on delta_msr_wait again since 1614b922e19dSJiri Slaby * any outstanding file descriptors should be pointing at 1615b922e19dSJiri Slaby * hung_up_tty_fops now. 1616b922e19dSJiri Slaby */ 1617b922e19dSJiri Slaby wake_up_interruptible(&port->delta_msr_wait); 1618b922e19dSJiri Slaby 1619b922e19dSJiri Slaby /* 1620b922e19dSJiri Slaby * Free the IRQ and disable the port. 1621b922e19dSJiri Slaby */ 1622af224ca2SPeter Hurley if (uport) 1623b922e19dSJiri Slaby uport->ops->shutdown(uport); 1624b922e19dSJiri Slaby 1625b922e19dSJiri Slaby /* 1626b922e19dSJiri Slaby * Ensure that the IRQ handler isn't running on another CPU. 1627b922e19dSJiri Slaby */ 1628af224ca2SPeter Hurley if (uport) 1629b922e19dSJiri Slaby synchronize_irq(uport->irq); 16300b1db830SJiri Slaby } 16310b1db830SJiri Slaby 1632ab4382d2SGreg Kroah-Hartman static int uart_carrier_raised(struct tty_port *port) 1633ab4382d2SGreg Kroah-Hartman { 1634ab4382d2SGreg Kroah-Hartman struct uart_state *state = container_of(port, struct uart_state, port); 16359ed19428SPeter Hurley struct uart_port *uport; 1636ab4382d2SGreg Kroah-Hartman int mctrl; 16379ed19428SPeter Hurley 16389ed19428SPeter Hurley uport = uart_port_ref(state); 16399ed19428SPeter Hurley /* 16409ed19428SPeter Hurley * Should never observe uport == NULL since checks for hangup should 16419ed19428SPeter Hurley * abort the tty_port_block_til_ready() loop before checking for carrier 16429ed19428SPeter Hurley * raised -- but report carrier raised if it does anyway so open will 16439ed19428SPeter Hurley * continue and not sleep 16449ed19428SPeter Hurley */ 16459ed19428SPeter Hurley if (WARN_ON(!uport)) 16469ed19428SPeter Hurley return 1; 1647ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 16481fdc3106SAlexander Shiyan uart_enable_ms(uport); 1649ab4382d2SGreg Kroah-Hartman mctrl = uport->ops->get_mctrl(uport); 1650ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 16519ed19428SPeter Hurley uart_port_deref(uport); 1652ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_CAR) 1653ab4382d2SGreg Kroah-Hartman return 1; 1654ab4382d2SGreg Kroah-Hartman return 0; 1655ab4382d2SGreg Kroah-Hartman } 1656ab4382d2SGreg Kroah-Hartman 1657ab4382d2SGreg Kroah-Hartman static void uart_dtr_rts(struct tty_port *port, int onoff) 1658ab4382d2SGreg Kroah-Hartman { 1659ab4382d2SGreg Kroah-Hartman struct uart_state *state = container_of(port, struct uart_state, port); 16609ed19428SPeter Hurley struct uart_port *uport; 16619ed19428SPeter Hurley 16629ed19428SPeter Hurley uport = uart_port_ref(state); 16639ed19428SPeter Hurley if (!uport) 16649ed19428SPeter Hurley return; 1665ab4382d2SGreg Kroah-Hartman 16666f5c24adSJiri Slaby if (onoff) 1667ab4382d2SGreg Kroah-Hartman uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS); 1668ab4382d2SGreg Kroah-Hartman else 1669ab4382d2SGreg Kroah-Hartman uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS); 16709ed19428SPeter Hurley 16719ed19428SPeter Hurley uart_port_deref(uport); 1672ab4382d2SGreg Kroah-Hartman } 1673ab4382d2SGreg Kroah-Hartman 1674ab4382d2SGreg Kroah-Hartman /* 1675ef4f527cSKevin Cernekee * Calls to uart_open are serialised by the tty_lock in 1676ef4f527cSKevin Cernekee * drivers/tty/tty_io.c:tty_open() 1677ab4382d2SGreg Kroah-Hartman * Note that if this fails, then uart_close() _will_ be called. 1678ab4382d2SGreg Kroah-Hartman * 1679ab4382d2SGreg Kroah-Hartman * In time, we want to scrap the "opening nonpresent ports" 1680ab4382d2SGreg Kroah-Hartman * behaviour and implement an alternative way for setserial 1681ab4382d2SGreg Kroah-Hartman * to set base addresses/ports/types. This will allow us to 1682ab4382d2SGreg Kroah-Hartman * get rid of a certain amount of extra tests. 1683ab4382d2SGreg Kroah-Hartman */ 1684ab4382d2SGreg Kroah-Hartman static int uart_open(struct tty_struct *tty, struct file *filp) 1685ab4382d2SGreg Kroah-Hartman { 16865e388027SPeter Hurley struct uart_driver *drv = tty->driver->driver_state; 1687ab4382d2SGreg Kroah-Hartman int retval, line = tty->index; 16881c7b13c4SJiri Slaby struct uart_state *state = drv->state + line; 1689ab4382d2SGreg Kroah-Hartman 1690ab4382d2SGreg Kroah-Hartman tty->driver_data = state; 1691b3b57646SRob Herring 1692b3b57646SRob Herring retval = tty_port_open(&state->port, tty, filp); 1693b3b57646SRob Herring if (retval > 0) 1694b3b57646SRob Herring retval = 0; 1695b3b57646SRob Herring 1696b3b57646SRob Herring return retval; 1697b3b57646SRob Herring } 1698b3b57646SRob Herring 1699b3b57646SRob Herring static int uart_port_activate(struct tty_port *port, struct tty_struct *tty) 1700b3b57646SRob Herring { 1701b3b57646SRob Herring struct uart_state *state = container_of(port, struct uart_state, port); 1702b3b57646SRob Herring struct uart_port *uport; 1703b3b57646SRob Herring 1704b3b57646SRob Herring uport = uart_port_check(state); 1705b3b57646SRob Herring if (!uport || uport->flags & UPF_DEAD) 1706b3b57646SRob Herring return -ENXIO; 1707b3b57646SRob Herring 17084047b371SPeter Hurley port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0; 1709ab4382d2SGreg Kroah-Hartman 1710ab4382d2SGreg Kroah-Hartman /* 1711ab4382d2SGreg Kroah-Hartman * Start up the serial port. 1712ab4382d2SGreg Kroah-Hartman */ 1713b3b57646SRob Herring return uart_startup(tty, state, 0); 1714ab4382d2SGreg Kroah-Hartman } 1715ab4382d2SGreg Kroah-Hartman 1716ab4382d2SGreg Kroah-Hartman static const char *uart_type(struct uart_port *port) 1717ab4382d2SGreg Kroah-Hartman { 1718ab4382d2SGreg Kroah-Hartman const char *str = NULL; 1719ab4382d2SGreg Kroah-Hartman 1720ab4382d2SGreg Kroah-Hartman if (port->ops->type) 1721ab4382d2SGreg Kroah-Hartman str = port->ops->type(port); 1722ab4382d2SGreg Kroah-Hartman 1723ab4382d2SGreg Kroah-Hartman if (!str) 1724ab4382d2SGreg Kroah-Hartman str = "unknown"; 1725ab4382d2SGreg Kroah-Hartman 1726ab4382d2SGreg Kroah-Hartman return str; 1727ab4382d2SGreg Kroah-Hartman } 1728ab4382d2SGreg Kroah-Hartman 1729ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PROC_FS 1730ab4382d2SGreg Kroah-Hartman 1731ab4382d2SGreg Kroah-Hartman static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i) 1732ab4382d2SGreg Kroah-Hartman { 1733ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + i; 1734ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 17356f538fe3SLinus Walleij enum uart_pm_state pm_state; 17364047b371SPeter Hurley struct uart_port *uport; 1737ab4382d2SGreg Kroah-Hartman char stat_buf[32]; 1738ab4382d2SGreg Kroah-Hartman unsigned int status; 1739ab4382d2SGreg Kroah-Hartman int mmio; 1740ab4382d2SGreg Kroah-Hartman 17414047b371SPeter Hurley mutex_lock(&port->mutex); 17424047b371SPeter Hurley uport = uart_port_check(state); 1743ab4382d2SGreg Kroah-Hartman if (!uport) 17444047b371SPeter Hurley goto out; 1745ab4382d2SGreg Kroah-Hartman 1746ab4382d2SGreg Kroah-Hartman mmio = uport->iotype >= UPIO_MEM; 1747ab4382d2SGreg Kroah-Hartman seq_printf(m, "%d: uart:%s %s%08llX irq:%d", 1748ab4382d2SGreg Kroah-Hartman uport->line, uart_type(uport), 1749ab4382d2SGreg Kroah-Hartman mmio ? "mmio:0x" : "port:", 1750ab4382d2SGreg Kroah-Hartman mmio ? (unsigned long long)uport->mapbase 1751ab4382d2SGreg Kroah-Hartman : (unsigned long long)uport->iobase, 1752ab4382d2SGreg Kroah-Hartman uport->irq); 1753ab4382d2SGreg Kroah-Hartman 1754ab4382d2SGreg Kroah-Hartman if (uport->type == PORT_UNKNOWN) { 1755ab4382d2SGreg Kroah-Hartman seq_putc(m, '\n'); 17564047b371SPeter Hurley goto out; 1757ab4382d2SGreg Kroah-Hartman } 1758ab4382d2SGreg Kroah-Hartman 1759ab4382d2SGreg Kroah-Hartman if (capable(CAP_SYS_ADMIN)) { 1760ab4382d2SGreg Kroah-Hartman pm_state = state->pm_state; 17616f538fe3SLinus Walleij if (pm_state != UART_PM_STATE_ON) 17626f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_ON); 1763ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 1764ab4382d2SGreg Kroah-Hartman status = uport->ops->get_mctrl(uport); 1765ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 17666f538fe3SLinus Walleij if (pm_state != UART_PM_STATE_ON) 1767ab4382d2SGreg Kroah-Hartman uart_change_pm(state, pm_state); 1768ab4382d2SGreg Kroah-Hartman 1769ab4382d2SGreg Kroah-Hartman seq_printf(m, " tx:%d rx:%d", 1770ab4382d2SGreg Kroah-Hartman uport->icount.tx, uport->icount.rx); 1771ab4382d2SGreg Kroah-Hartman if (uport->icount.frame) 1772968af298SPeter Hurley seq_printf(m, " fe:%d", uport->icount.frame); 1773ab4382d2SGreg Kroah-Hartman if (uport->icount.parity) 1774968af298SPeter Hurley seq_printf(m, " pe:%d", uport->icount.parity); 1775ab4382d2SGreg Kroah-Hartman if (uport->icount.brk) 1776968af298SPeter Hurley seq_printf(m, " brk:%d", uport->icount.brk); 1777ab4382d2SGreg Kroah-Hartman if (uport->icount.overrun) 1778968af298SPeter Hurley seq_printf(m, " oe:%d", uport->icount.overrun); 1779ab4382d2SGreg Kroah-Hartman 1780ab4382d2SGreg Kroah-Hartman #define INFOBIT(bit, str) \ 1781ab4382d2SGreg Kroah-Hartman if (uport->mctrl & (bit)) \ 1782ab4382d2SGreg Kroah-Hartman strncat(stat_buf, (str), sizeof(stat_buf) - \ 1783ab4382d2SGreg Kroah-Hartman strlen(stat_buf) - 2) 1784ab4382d2SGreg Kroah-Hartman #define STATBIT(bit, str) \ 1785ab4382d2SGreg Kroah-Hartman if (status & (bit)) \ 1786ab4382d2SGreg Kroah-Hartman strncat(stat_buf, (str), sizeof(stat_buf) - \ 1787ab4382d2SGreg Kroah-Hartman strlen(stat_buf) - 2) 1788ab4382d2SGreg Kroah-Hartman 1789ab4382d2SGreg Kroah-Hartman stat_buf[0] = '\0'; 1790ab4382d2SGreg Kroah-Hartman stat_buf[1] = '\0'; 1791ab4382d2SGreg Kroah-Hartman INFOBIT(TIOCM_RTS, "|RTS"); 1792ab4382d2SGreg Kroah-Hartman STATBIT(TIOCM_CTS, "|CTS"); 1793ab4382d2SGreg Kroah-Hartman INFOBIT(TIOCM_DTR, "|DTR"); 1794ab4382d2SGreg Kroah-Hartman STATBIT(TIOCM_DSR, "|DSR"); 1795ab4382d2SGreg Kroah-Hartman STATBIT(TIOCM_CAR, "|CD"); 1796ab4382d2SGreg Kroah-Hartman STATBIT(TIOCM_RNG, "|RI"); 1797ab4382d2SGreg Kroah-Hartman if (stat_buf[0]) 1798ab4382d2SGreg Kroah-Hartman stat_buf[0] = ' '; 1799ab4382d2SGreg Kroah-Hartman 1800ab4382d2SGreg Kroah-Hartman seq_puts(m, stat_buf); 1801ab4382d2SGreg Kroah-Hartman } 1802ab4382d2SGreg Kroah-Hartman seq_putc(m, '\n'); 1803ab4382d2SGreg Kroah-Hartman #undef STATBIT 1804ab4382d2SGreg Kroah-Hartman #undef INFOBIT 18054047b371SPeter Hurley out: 18064047b371SPeter Hurley mutex_unlock(&port->mutex); 1807ab4382d2SGreg Kroah-Hartman } 1808ab4382d2SGreg Kroah-Hartman 1809ab4382d2SGreg Kroah-Hartman static int uart_proc_show(struct seq_file *m, void *v) 1810ab4382d2SGreg Kroah-Hartman { 1811ab4382d2SGreg Kroah-Hartman struct tty_driver *ttydrv = m->private; 1812ab4382d2SGreg Kroah-Hartman struct uart_driver *drv = ttydrv->driver_state; 1813ab4382d2SGreg Kroah-Hartman int i; 1814ab4382d2SGreg Kroah-Hartman 1815968af298SPeter Hurley seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", ""); 1816ab4382d2SGreg Kroah-Hartman for (i = 0; i < drv->nr; i++) 1817ab4382d2SGreg Kroah-Hartman uart_line_info(m, drv, i); 1818ab4382d2SGreg Kroah-Hartman return 0; 1819ab4382d2SGreg Kroah-Hartman } 1820ab4382d2SGreg Kroah-Hartman 1821ab4382d2SGreg Kroah-Hartman static int uart_proc_open(struct inode *inode, struct file *file) 1822ab4382d2SGreg Kroah-Hartman { 1823d9dda78bSAl Viro return single_open(file, uart_proc_show, PDE_DATA(inode)); 1824ab4382d2SGreg Kroah-Hartman } 1825ab4382d2SGreg Kroah-Hartman 1826ab4382d2SGreg Kroah-Hartman static const struct file_operations uart_proc_fops = { 1827ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE, 1828ab4382d2SGreg Kroah-Hartman .open = uart_proc_open, 1829ab4382d2SGreg Kroah-Hartman .read = seq_read, 1830ab4382d2SGreg Kroah-Hartman .llseek = seq_lseek, 1831ab4382d2SGreg Kroah-Hartman .release = single_release, 1832ab4382d2SGreg Kroah-Hartman }; 1833ab4382d2SGreg Kroah-Hartman #endif 1834ab4382d2SGreg Kroah-Hartman 1835ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL) 18361cfe42b7SPeter Hurley /** 1837ab4382d2SGreg Kroah-Hartman * uart_console_write - write a console message to a serial port 1838ab4382d2SGreg Kroah-Hartman * @port: the port to write the message 1839ab4382d2SGreg Kroah-Hartman * @s: array of characters 1840ab4382d2SGreg Kroah-Hartman * @count: number of characters in string to write 184110afbe34SPeter Hurley * @putchar: function to write character to port 1842ab4382d2SGreg Kroah-Hartman */ 1843ab4382d2SGreg Kroah-Hartman void uart_console_write(struct uart_port *port, const char *s, 1844ab4382d2SGreg Kroah-Hartman unsigned int count, 1845ab4382d2SGreg Kroah-Hartman void (*putchar)(struct uart_port *, int)) 1846ab4382d2SGreg Kroah-Hartman { 1847ab4382d2SGreg Kroah-Hartman unsigned int i; 1848ab4382d2SGreg Kroah-Hartman 1849ab4382d2SGreg Kroah-Hartman for (i = 0; i < count; i++, s++) { 1850ab4382d2SGreg Kroah-Hartman if (*s == '\n') 1851ab4382d2SGreg Kroah-Hartman putchar(port, '\r'); 1852ab4382d2SGreg Kroah-Hartman putchar(port, *s); 1853ab4382d2SGreg Kroah-Hartman } 1854ab4382d2SGreg Kroah-Hartman } 1855ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_console_write); 1856ab4382d2SGreg Kroah-Hartman 1857ab4382d2SGreg Kroah-Hartman /* 1858ab4382d2SGreg Kroah-Hartman * Check whether an invalid uart number has been specified, and 1859ab4382d2SGreg Kroah-Hartman * if so, search for the first available port that does have 1860ab4382d2SGreg Kroah-Hartman * console support. 1861ab4382d2SGreg Kroah-Hartman */ 1862ab4382d2SGreg Kroah-Hartman struct uart_port * __init 1863ab4382d2SGreg Kroah-Hartman uart_get_console(struct uart_port *ports, int nr, struct console *co) 1864ab4382d2SGreg Kroah-Hartman { 1865ab4382d2SGreg Kroah-Hartman int idx = co->index; 1866ab4382d2SGreg Kroah-Hartman 1867ab4382d2SGreg Kroah-Hartman if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 && 1868ab4382d2SGreg Kroah-Hartman ports[idx].membase == NULL)) 1869ab4382d2SGreg Kroah-Hartman for (idx = 0; idx < nr; idx++) 1870ab4382d2SGreg Kroah-Hartman if (ports[idx].iobase != 0 || 1871ab4382d2SGreg Kroah-Hartman ports[idx].membase != NULL) 1872ab4382d2SGreg Kroah-Hartman break; 1873ab4382d2SGreg Kroah-Hartman 1874ab4382d2SGreg Kroah-Hartman co->index = idx; 1875ab4382d2SGreg Kroah-Hartman 1876ab4382d2SGreg Kroah-Hartman return ports + idx; 1877ab4382d2SGreg Kroah-Hartman } 1878ab4382d2SGreg Kroah-Hartman 1879ab4382d2SGreg Kroah-Hartman /** 188073abaf87SPeter Hurley * uart_parse_earlycon - Parse earlycon options 188173abaf87SPeter Hurley * @p: ptr to 2nd field (ie., just beyond '<name>,') 188273abaf87SPeter Hurley * @iotype: ptr for decoded iotype (out) 188373abaf87SPeter Hurley * @addr: ptr for decoded mapbase/iobase (out) 188473abaf87SPeter Hurley * @options: ptr for <options> field; NULL if not present (out) 188573abaf87SPeter Hurley * 188673abaf87SPeter Hurley * Decodes earlycon kernel command line parameters of the form 1887bd94c407SMasahiro Yamada * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options> 1888bd94c407SMasahiro Yamada * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options> 188973abaf87SPeter Hurley * 189073abaf87SPeter Hurley * The optional form 189173abaf87SPeter Hurley * earlycon=<name>,0x<addr>,<options> 189273abaf87SPeter Hurley * console=<name>,0x<addr>,<options> 189373abaf87SPeter Hurley * is also accepted; the returned @iotype will be UPIO_MEM. 189473abaf87SPeter Hurley * 18958b2303deSAlexander Sverdlin * Returns 0 on success or -EINVAL on failure 189673abaf87SPeter Hurley */ 189746e36683SAlexander Sverdlin int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr, 189873abaf87SPeter Hurley char **options) 189973abaf87SPeter Hurley { 190073abaf87SPeter Hurley if (strncmp(p, "mmio,", 5) == 0) { 190173abaf87SPeter Hurley *iotype = UPIO_MEM; 190273abaf87SPeter Hurley p += 5; 1903bd94c407SMasahiro Yamada } else if (strncmp(p, "mmio16,", 7) == 0) { 1904bd94c407SMasahiro Yamada *iotype = UPIO_MEM16; 1905bd94c407SMasahiro Yamada p += 7; 190673abaf87SPeter Hurley } else if (strncmp(p, "mmio32,", 7) == 0) { 190773abaf87SPeter Hurley *iotype = UPIO_MEM32; 190873abaf87SPeter Hurley p += 7; 19096e63be3fSNoam Camus } else if (strncmp(p, "mmio32be,", 9) == 0) { 19106e63be3fSNoam Camus *iotype = UPIO_MEM32BE; 19116e63be3fSNoam Camus p += 9; 1912d215d809SMax Filippov } else if (strncmp(p, "mmio32native,", 13) == 0) { 1913d215d809SMax Filippov *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ? 1914d215d809SMax Filippov UPIO_MEM32BE : UPIO_MEM32; 1915d215d809SMax Filippov p += 13; 191673abaf87SPeter Hurley } else if (strncmp(p, "io,", 3) == 0) { 191773abaf87SPeter Hurley *iotype = UPIO_PORT; 191873abaf87SPeter Hurley p += 3; 191973abaf87SPeter Hurley } else if (strncmp(p, "0x", 2) == 0) { 192073abaf87SPeter Hurley *iotype = UPIO_MEM; 192173abaf87SPeter Hurley } else { 192273abaf87SPeter Hurley return -EINVAL; 192373abaf87SPeter Hurley } 192473abaf87SPeter Hurley 19258b2303deSAlexander Sverdlin /* 19268b2303deSAlexander Sverdlin * Before you replace it with kstrtoull(), think about options separator 19278b2303deSAlexander Sverdlin * (',') it will not tolerate 19288b2303deSAlexander Sverdlin */ 19298b2303deSAlexander Sverdlin *addr = simple_strtoull(p, NULL, 0); 193073abaf87SPeter Hurley p = strchr(p, ','); 193173abaf87SPeter Hurley if (p) 193273abaf87SPeter Hurley p++; 193373abaf87SPeter Hurley 193473abaf87SPeter Hurley *options = p; 193573abaf87SPeter Hurley return 0; 193673abaf87SPeter Hurley } 193773abaf87SPeter Hurley EXPORT_SYMBOL_GPL(uart_parse_earlycon); 193873abaf87SPeter Hurley 193973abaf87SPeter Hurley /** 194002088ca6SGeert Uytterhoeven * uart_parse_options - Parse serial port baud/parity/bits/flow control. 1941ab4382d2SGreg Kroah-Hartman * @options: pointer to option string 1942ab4382d2SGreg Kroah-Hartman * @baud: pointer to an 'int' variable for the baud rate. 1943ab4382d2SGreg Kroah-Hartman * @parity: pointer to an 'int' variable for the parity. 1944ab4382d2SGreg Kroah-Hartman * @bits: pointer to an 'int' variable for the number of data bits. 1945ab4382d2SGreg Kroah-Hartman * @flow: pointer to an 'int' variable for the flow control character. 1946ab4382d2SGreg Kroah-Hartman * 1947ab4382d2SGreg Kroah-Hartman * uart_parse_options decodes a string containing the serial console 1948ab4382d2SGreg Kroah-Hartman * options. The format of the string is <baud><parity><bits><flow>, 1949ab4382d2SGreg Kroah-Hartman * eg: 115200n8r 1950ab4382d2SGreg Kroah-Hartman */ 1951ab4382d2SGreg Kroah-Hartman void 1952ab4382d2SGreg Kroah-Hartman uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow) 1953ab4382d2SGreg Kroah-Hartman { 1954ab4382d2SGreg Kroah-Hartman char *s = options; 1955ab4382d2SGreg Kroah-Hartman 1956ab4382d2SGreg Kroah-Hartman *baud = simple_strtoul(s, NULL, 10); 1957ab4382d2SGreg Kroah-Hartman while (*s >= '0' && *s <= '9') 1958ab4382d2SGreg Kroah-Hartman s++; 1959ab4382d2SGreg Kroah-Hartman if (*s) 1960ab4382d2SGreg Kroah-Hartman *parity = *s++; 1961ab4382d2SGreg Kroah-Hartman if (*s) 1962ab4382d2SGreg Kroah-Hartman *bits = *s++ - '0'; 1963ab4382d2SGreg Kroah-Hartman if (*s) 1964ab4382d2SGreg Kroah-Hartman *flow = *s; 1965ab4382d2SGreg Kroah-Hartman } 1966ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_parse_options); 1967ab4382d2SGreg Kroah-Hartman 1968ab4382d2SGreg Kroah-Hartman /** 1969ab4382d2SGreg Kroah-Hartman * uart_set_options - setup the serial console parameters 1970ab4382d2SGreg Kroah-Hartman * @port: pointer to the serial ports uart_port structure 1971ab4382d2SGreg Kroah-Hartman * @co: console pointer 1972ab4382d2SGreg Kroah-Hartman * @baud: baud rate 1973ab4382d2SGreg Kroah-Hartman * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even) 1974ab4382d2SGreg Kroah-Hartman * @bits: number of data bits 1975ab4382d2SGreg Kroah-Hartman * @flow: flow control character - 'r' (rts) 1976ab4382d2SGreg Kroah-Hartman */ 1977ab4382d2SGreg Kroah-Hartman int 1978ab4382d2SGreg Kroah-Hartman uart_set_options(struct uart_port *port, struct console *co, 1979ab4382d2SGreg Kroah-Hartman int baud, int parity, int bits, int flow) 1980ab4382d2SGreg Kroah-Hartman { 1981ab4382d2SGreg Kroah-Hartman struct ktermios termios; 1982ab4382d2SGreg Kroah-Hartman static struct ktermios dummy; 1983ab4382d2SGreg Kroah-Hartman 1984ab4382d2SGreg Kroah-Hartman /* 1985ab4382d2SGreg Kroah-Hartman * Ensure that the serial console lock is initialised 1986ab4382d2SGreg Kroah-Hartman * early. 198742b6a1baSRandy Witt * If this port is a console, then the spinlock is already 198842b6a1baSRandy Witt * initialised. 1989ab4382d2SGreg Kroah-Hartman */ 199042b6a1baSRandy Witt if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) { 1991ab4382d2SGreg Kroah-Hartman spin_lock_init(&port->lock); 1992ab4382d2SGreg Kroah-Hartman lockdep_set_class(&port->lock, &port_lock_key); 199342b6a1baSRandy Witt } 1994ab4382d2SGreg Kroah-Hartman 1995ab4382d2SGreg Kroah-Hartman memset(&termios, 0, sizeof(struct ktermios)); 1996ab4382d2SGreg Kroah-Hartman 1997ba47f97aSJeffy Chen termios.c_cflag |= CREAD | HUPCL | CLOCAL; 1998ba47f97aSJeffy Chen tty_termios_encode_baud_rate(&termios, baud, baud); 1999ab4382d2SGreg Kroah-Hartman 2000ab4382d2SGreg Kroah-Hartman if (bits == 7) 2001ab4382d2SGreg Kroah-Hartman termios.c_cflag |= CS7; 2002ab4382d2SGreg Kroah-Hartman else 2003ab4382d2SGreg Kroah-Hartman termios.c_cflag |= CS8; 2004ab4382d2SGreg Kroah-Hartman 2005ab4382d2SGreg Kroah-Hartman switch (parity) { 2006ab4382d2SGreg Kroah-Hartman case 'o': case 'O': 2007ab4382d2SGreg Kroah-Hartman termios.c_cflag |= PARODD; 2008ab4382d2SGreg Kroah-Hartman /*fall through*/ 2009ab4382d2SGreg Kroah-Hartman case 'e': case 'E': 2010ab4382d2SGreg Kroah-Hartman termios.c_cflag |= PARENB; 2011ab4382d2SGreg Kroah-Hartman break; 2012ab4382d2SGreg Kroah-Hartman } 2013ab4382d2SGreg Kroah-Hartman 2014ab4382d2SGreg Kroah-Hartman if (flow == 'r') 2015ab4382d2SGreg Kroah-Hartman termios.c_cflag |= CRTSCTS; 2016ab4382d2SGreg Kroah-Hartman 2017ab4382d2SGreg Kroah-Hartman /* 2018ab4382d2SGreg Kroah-Hartman * some uarts on other side don't support no flow control. 2019ab4382d2SGreg Kroah-Hartman * So we set * DTR in host uart to make them happy 2020ab4382d2SGreg Kroah-Hartman */ 2021ab4382d2SGreg Kroah-Hartman port->mctrl |= TIOCM_DTR; 2022ab4382d2SGreg Kroah-Hartman 2023ab4382d2SGreg Kroah-Hartman port->ops->set_termios(port, &termios, &dummy); 2024ab4382d2SGreg Kroah-Hartman /* 2025ab4382d2SGreg Kroah-Hartman * Allow the setting of the UART parameters with a NULL console 2026ab4382d2SGreg Kroah-Hartman * too: 2027ab4382d2SGreg Kroah-Hartman */ 2028ab4382d2SGreg Kroah-Hartman if (co) 2029ab4382d2SGreg Kroah-Hartman co->cflag = termios.c_cflag; 2030ab4382d2SGreg Kroah-Hartman 2031ab4382d2SGreg Kroah-Hartman return 0; 2032ab4382d2SGreg Kroah-Hartman } 2033ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_set_options); 2034ab4382d2SGreg Kroah-Hartman #endif /* CONFIG_SERIAL_CORE_CONSOLE */ 2035ab4382d2SGreg Kroah-Hartman 2036cf75525fSJiri Slaby /** 2037cf75525fSJiri Slaby * uart_change_pm - set power state of the port 2038cf75525fSJiri Slaby * 2039cf75525fSJiri Slaby * @state: port descriptor 2040cf75525fSJiri Slaby * @pm_state: new state 2041cf75525fSJiri Slaby * 2042cf75525fSJiri Slaby * Locking: port->mutex has to be held 2043cf75525fSJiri Slaby */ 20446f538fe3SLinus Walleij static void uart_change_pm(struct uart_state *state, 20456f538fe3SLinus Walleij enum uart_pm_state pm_state) 2046ab4382d2SGreg Kroah-Hartman { 20474047b371SPeter Hurley struct uart_port *port = uart_port_check(state); 2048ab4382d2SGreg Kroah-Hartman 2049ab4382d2SGreg Kroah-Hartman if (state->pm_state != pm_state) { 20504047b371SPeter Hurley if (port && port->ops->pm) 2051ab4382d2SGreg Kroah-Hartman port->ops->pm(port, pm_state, state->pm_state); 2052ab4382d2SGreg Kroah-Hartman state->pm_state = pm_state; 2053ab4382d2SGreg Kroah-Hartman } 2054ab4382d2SGreg Kroah-Hartman } 2055ab4382d2SGreg Kroah-Hartman 2056ab4382d2SGreg Kroah-Hartman struct uart_match { 2057ab4382d2SGreg Kroah-Hartman struct uart_port *port; 2058ab4382d2SGreg Kroah-Hartman struct uart_driver *driver; 2059ab4382d2SGreg Kroah-Hartman }; 2060ab4382d2SGreg Kroah-Hartman 2061ab4382d2SGreg Kroah-Hartman static int serial_match_port(struct device *dev, void *data) 2062ab4382d2SGreg Kroah-Hartman { 2063ab4382d2SGreg Kroah-Hartman struct uart_match *match = data; 2064ab4382d2SGreg Kroah-Hartman struct tty_driver *tty_drv = match->driver->tty_driver; 2065ab4382d2SGreg Kroah-Hartman dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) + 2066ab4382d2SGreg Kroah-Hartman match->port->line; 2067ab4382d2SGreg Kroah-Hartman 2068ab4382d2SGreg Kroah-Hartman return dev->devt == devt; /* Actually, only one tty per port */ 2069ab4382d2SGreg Kroah-Hartman } 2070ab4382d2SGreg Kroah-Hartman 2071ab4382d2SGreg Kroah-Hartman int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport) 2072ab4382d2SGreg Kroah-Hartman { 2073ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + uport->line; 2074ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 2075ab4382d2SGreg Kroah-Hartman struct device *tty_dev; 2076ab4382d2SGreg Kroah-Hartman struct uart_match match = {uport, drv}; 2077ab4382d2SGreg Kroah-Hartman 2078ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 2079ab4382d2SGreg Kroah-Hartman 2080ab4382d2SGreg Kroah-Hartman tty_dev = device_find_child(uport->dev, &match, serial_match_port); 2081ab4382d2SGreg Kroah-Hartman if (device_may_wakeup(tty_dev)) { 2082ab4382d2SGreg Kroah-Hartman if (!enable_irq_wake(uport->irq)) 2083ab4382d2SGreg Kroah-Hartman uport->irq_wake = 1; 2084ab4382d2SGreg Kroah-Hartman put_device(tty_dev); 2085ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2086ab4382d2SGreg Kroah-Hartman return 0; 2087ab4382d2SGreg Kroah-Hartman } 20885a65dcc0SFederico Vaga put_device(tty_dev); 20895a65dcc0SFederico Vaga 2090b164c972SPeter Hurley /* Nothing to do if the console is not suspending */ 2091b164c972SPeter Hurley if (!console_suspend_enabled && uart_console(uport)) 2092b164c972SPeter Hurley goto unlock; 2093b164c972SPeter Hurley 2094ab4382d2SGreg Kroah-Hartman uport->suspended = 1; 2095ab4382d2SGreg Kroah-Hartman 2096d41861caSPeter Hurley if (tty_port_initialized(port)) { 2097ab4382d2SGreg Kroah-Hartman const struct uart_ops *ops = uport->ops; 2098ab4382d2SGreg Kroah-Hartman int tries; 2099ab4382d2SGreg Kroah-Hartman 210080f02d54SPeter Hurley tty_port_set_suspended(port, 1); 2101d41861caSPeter Hurley tty_port_set_initialized(port, 0); 2102ab4382d2SGreg Kroah-Hartman 2103ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 2104ab4382d2SGreg Kroah-Hartman ops->stop_tx(uport); 2105ab4382d2SGreg Kroah-Hartman ops->set_mctrl(uport, 0); 2106ab4382d2SGreg Kroah-Hartman ops->stop_rx(uport); 2107ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 2108ab4382d2SGreg Kroah-Hartman 2109ab4382d2SGreg Kroah-Hartman /* 2110ab4382d2SGreg Kroah-Hartman * Wait for the transmitter to empty. 2111ab4382d2SGreg Kroah-Hartman */ 2112ab4382d2SGreg Kroah-Hartman for (tries = 3; !ops->tx_empty(uport) && tries; tries--) 2113ab4382d2SGreg Kroah-Hartman msleep(10); 2114ab4382d2SGreg Kroah-Hartman if (!tries) 21152f2dafe7SSudip Mukherjee dev_err(uport->dev, "%s%d: Unable to drain transmitter\n", 2116ab4382d2SGreg Kroah-Hartman drv->dev_name, 2117ab4382d2SGreg Kroah-Hartman drv->tty_driver->name_base + uport->line); 2118ab4382d2SGreg Kroah-Hartman 2119ab4382d2SGreg Kroah-Hartman ops->shutdown(uport); 2120ab4382d2SGreg Kroah-Hartman } 2121ab4382d2SGreg Kroah-Hartman 2122ab4382d2SGreg Kroah-Hartman /* 2123ab4382d2SGreg Kroah-Hartman * Disable the console device before suspending. 2124ab4382d2SGreg Kroah-Hartman */ 2125b164c972SPeter Hurley if (uart_console(uport)) 2126ab4382d2SGreg Kroah-Hartman console_stop(uport->cons); 2127ab4382d2SGreg Kroah-Hartman 21286f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_OFF); 2129b164c972SPeter Hurley unlock: 2130ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2131ab4382d2SGreg Kroah-Hartman 2132ab4382d2SGreg Kroah-Hartman return 0; 2133ab4382d2SGreg Kroah-Hartman } 2134ab4382d2SGreg Kroah-Hartman 2135ab4382d2SGreg Kroah-Hartman int uart_resume_port(struct uart_driver *drv, struct uart_port *uport) 2136ab4382d2SGreg Kroah-Hartman { 2137ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + uport->line; 2138ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 2139ab4382d2SGreg Kroah-Hartman struct device *tty_dev; 2140ab4382d2SGreg Kroah-Hartman struct uart_match match = {uport, drv}; 2141ab4382d2SGreg Kroah-Hartman struct ktermios termios; 2142ab4382d2SGreg Kroah-Hartman 2143ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 2144ab4382d2SGreg Kroah-Hartman 2145ab4382d2SGreg Kroah-Hartman tty_dev = device_find_child(uport->dev, &match, serial_match_port); 2146ab4382d2SGreg Kroah-Hartman if (!uport->suspended && device_may_wakeup(tty_dev)) { 2147ab4382d2SGreg Kroah-Hartman if (uport->irq_wake) { 2148ab4382d2SGreg Kroah-Hartman disable_irq_wake(uport->irq); 2149ab4382d2SGreg Kroah-Hartman uport->irq_wake = 0; 2150ab4382d2SGreg Kroah-Hartman } 21515a65dcc0SFederico Vaga put_device(tty_dev); 2152ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2153ab4382d2SGreg Kroah-Hartman return 0; 2154ab4382d2SGreg Kroah-Hartman } 21555a65dcc0SFederico Vaga put_device(tty_dev); 2156ab4382d2SGreg Kroah-Hartman uport->suspended = 0; 2157ab4382d2SGreg Kroah-Hartman 2158ab4382d2SGreg Kroah-Hartman /* 2159ab4382d2SGreg Kroah-Hartman * Re-enable the console device after suspending. 2160ab4382d2SGreg Kroah-Hartman */ 21615933a161SYin Kangkai if (uart_console(uport)) { 2162ab4382d2SGreg Kroah-Hartman /* 2163ab4382d2SGreg Kroah-Hartman * First try to use the console cflag setting. 2164ab4382d2SGreg Kroah-Hartman */ 2165ab4382d2SGreg Kroah-Hartman memset(&termios, 0, sizeof(struct ktermios)); 2166ab4382d2SGreg Kroah-Hartman termios.c_cflag = uport->cons->cflag; 2167ab4382d2SGreg Kroah-Hartman 2168ab4382d2SGreg Kroah-Hartman /* 2169ab4382d2SGreg Kroah-Hartman * If that's unset, use the tty termios setting. 2170ab4382d2SGreg Kroah-Hartman */ 2171adc8d746SAlan Cox if (port->tty && termios.c_cflag == 0) 2172adc8d746SAlan Cox termios = port->tty->termios; 2173ab4382d2SGreg Kroah-Hartman 217494abc56fSNing Jiang if (console_suspend_enabled) 21756f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_ON); 2176ab4382d2SGreg Kroah-Hartman uport->ops->set_termios(uport, &termios, NULL); 21775933a161SYin Kangkai if (console_suspend_enabled) 2178ab4382d2SGreg Kroah-Hartman console_start(uport->cons); 2179ab4382d2SGreg Kroah-Hartman } 2180ab4382d2SGreg Kroah-Hartman 218180f02d54SPeter Hurley if (tty_port_suspended(port)) { 2182ab4382d2SGreg Kroah-Hartman const struct uart_ops *ops = uport->ops; 2183ab4382d2SGreg Kroah-Hartman int ret; 2184ab4382d2SGreg Kroah-Hartman 21856f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_ON); 2186ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 2187ab4382d2SGreg Kroah-Hartman ops->set_mctrl(uport, 0); 2188ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 2189ab4382d2SGreg Kroah-Hartman if (console_suspend_enabled || !uart_console(uport)) { 2190ab4382d2SGreg Kroah-Hartman /* Protected by port mutex for now */ 2191ab4382d2SGreg Kroah-Hartman struct tty_struct *tty = port->tty; 2192ab4382d2SGreg Kroah-Hartman ret = ops->startup(uport); 2193ab4382d2SGreg Kroah-Hartman if (ret == 0) { 2194ab4382d2SGreg Kroah-Hartman if (tty) 2195ab4382d2SGreg Kroah-Hartman uart_change_speed(tty, state, NULL); 2196ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 2197ab4382d2SGreg Kroah-Hartman ops->set_mctrl(uport, uport->mctrl); 2198ab4382d2SGreg Kroah-Hartman ops->start_tx(uport); 2199ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 2200d41861caSPeter Hurley tty_port_set_initialized(port, 1); 2201ab4382d2SGreg Kroah-Hartman } else { 2202ab4382d2SGreg Kroah-Hartman /* 2203ab4382d2SGreg Kroah-Hartman * Failed to resume - maybe hardware went away? 2204ab4382d2SGreg Kroah-Hartman * Clear the "initialized" flag so we won't try 2205ab4382d2SGreg Kroah-Hartman * to call the low level drivers shutdown method. 2206ab4382d2SGreg Kroah-Hartman */ 2207ab4382d2SGreg Kroah-Hartman uart_shutdown(tty, state); 2208ab4382d2SGreg Kroah-Hartman } 2209ab4382d2SGreg Kroah-Hartman } 2210ab4382d2SGreg Kroah-Hartman 221180f02d54SPeter Hurley tty_port_set_suspended(port, 0); 2212ab4382d2SGreg Kroah-Hartman } 2213ab4382d2SGreg Kroah-Hartman 2214ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2215ab4382d2SGreg Kroah-Hartman 2216ab4382d2SGreg Kroah-Hartman return 0; 2217ab4382d2SGreg Kroah-Hartman } 2218ab4382d2SGreg Kroah-Hartman 2219ab4382d2SGreg Kroah-Hartman static inline void 2220ab4382d2SGreg Kroah-Hartman uart_report_port(struct uart_driver *drv, struct uart_port *port) 2221ab4382d2SGreg Kroah-Hartman { 2222ab4382d2SGreg Kroah-Hartman char address[64]; 2223ab4382d2SGreg Kroah-Hartman 2224ab4382d2SGreg Kroah-Hartman switch (port->iotype) { 2225ab4382d2SGreg Kroah-Hartman case UPIO_PORT: 2226ab4382d2SGreg Kroah-Hartman snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase); 2227ab4382d2SGreg Kroah-Hartman break; 2228ab4382d2SGreg Kroah-Hartman case UPIO_HUB6: 2229ab4382d2SGreg Kroah-Hartman snprintf(address, sizeof(address), 2230ab4382d2SGreg Kroah-Hartman "I/O 0x%lx offset 0x%x", port->iobase, port->hub6); 2231ab4382d2SGreg Kroah-Hartman break; 2232ab4382d2SGreg Kroah-Hartman case UPIO_MEM: 2233bd94c407SMasahiro Yamada case UPIO_MEM16: 2234ab4382d2SGreg Kroah-Hartman case UPIO_MEM32: 22353ffb1a81SKevin Cernekee case UPIO_MEM32BE: 2236ab4382d2SGreg Kroah-Hartman case UPIO_AU: 2237ab4382d2SGreg Kroah-Hartman case UPIO_TSI: 2238ab4382d2SGreg Kroah-Hartman snprintf(address, sizeof(address), 2239ab4382d2SGreg Kroah-Hartman "MMIO 0x%llx", (unsigned long long)port->mapbase); 2240ab4382d2SGreg Kroah-Hartman break; 2241ab4382d2SGreg Kroah-Hartman default: 2242ab4382d2SGreg Kroah-Hartman strlcpy(address, "*unknown*", sizeof(address)); 2243ab4382d2SGreg Kroah-Hartman break; 2244ab4382d2SGreg Kroah-Hartman } 2245ab4382d2SGreg Kroah-Hartman 224668ed7e1cSJames Bottomley printk(KERN_INFO "%s%s%s%d at %s (irq = %d, base_baud = %d) is a %s\n", 224768ed7e1cSJames Bottomley port->dev ? dev_name(port->dev) : "", 224868ed7e1cSJames Bottomley port->dev ? ": " : "", 2249ab4382d2SGreg Kroah-Hartman drv->dev_name, 2250ab4382d2SGreg Kroah-Hartman drv->tty_driver->name_base + port->line, 22517d12b976SKees Cook address, port->irq, port->uartclk / 16, uart_type(port)); 2252ab4382d2SGreg Kroah-Hartman } 2253ab4382d2SGreg Kroah-Hartman 2254ab4382d2SGreg Kroah-Hartman static void 2255ab4382d2SGreg Kroah-Hartman uart_configure_port(struct uart_driver *drv, struct uart_state *state, 2256ab4382d2SGreg Kroah-Hartman struct uart_port *port) 2257ab4382d2SGreg Kroah-Hartman { 2258ab4382d2SGreg Kroah-Hartman unsigned int flags; 2259ab4382d2SGreg Kroah-Hartman 2260ab4382d2SGreg Kroah-Hartman /* 2261ab4382d2SGreg Kroah-Hartman * If there isn't a port here, don't do anything further. 2262ab4382d2SGreg Kroah-Hartman */ 2263ab4382d2SGreg Kroah-Hartman if (!port->iobase && !port->mapbase && !port->membase) 2264ab4382d2SGreg Kroah-Hartman return; 2265ab4382d2SGreg Kroah-Hartman 2266ab4382d2SGreg Kroah-Hartman /* 2267ab4382d2SGreg Kroah-Hartman * Now do the auto configuration stuff. Note that config_port 2268ab4382d2SGreg Kroah-Hartman * is expected to claim the resources and map the port for us. 2269ab4382d2SGreg Kroah-Hartman */ 2270ab4382d2SGreg Kroah-Hartman flags = 0; 2271ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_AUTO_IRQ) 2272ab4382d2SGreg Kroah-Hartman flags |= UART_CONFIG_IRQ; 2273ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_BOOT_AUTOCONF) { 2274ab4382d2SGreg Kroah-Hartman if (!(port->flags & UPF_FIXED_TYPE)) { 2275ab4382d2SGreg Kroah-Hartman port->type = PORT_UNKNOWN; 2276ab4382d2SGreg Kroah-Hartman flags |= UART_CONFIG_TYPE; 2277ab4382d2SGreg Kroah-Hartman } 2278ab4382d2SGreg Kroah-Hartman port->ops->config_port(port, flags); 2279ab4382d2SGreg Kroah-Hartman } 2280ab4382d2SGreg Kroah-Hartman 2281ab4382d2SGreg Kroah-Hartman if (port->type != PORT_UNKNOWN) { 2282ab4382d2SGreg Kroah-Hartman unsigned long flags; 2283ab4382d2SGreg Kroah-Hartman 2284ab4382d2SGreg Kroah-Hartman uart_report_port(drv, port); 2285ab4382d2SGreg Kroah-Hartman 2286ab4382d2SGreg Kroah-Hartman /* Power up port for set_mctrl() */ 22876f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_ON); 2288ab4382d2SGreg Kroah-Hartman 2289ab4382d2SGreg Kroah-Hartman /* 2290ab4382d2SGreg Kroah-Hartman * Ensure that the modem control lines are de-activated. 2291ab4382d2SGreg Kroah-Hartman * keep the DTR setting that is set in uart_set_options() 2292ab4382d2SGreg Kroah-Hartman * We probably don't need a spinlock around this, but 2293ab4382d2SGreg Kroah-Hartman */ 2294ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 2295ab4382d2SGreg Kroah-Hartman port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR); 2296ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 2297ab4382d2SGreg Kroah-Hartman 2298ab4382d2SGreg Kroah-Hartman /* 2299ab4382d2SGreg Kroah-Hartman * If this driver supports console, and it hasn't been 2300ab4382d2SGreg Kroah-Hartman * successfully registered yet, try to re-register it. 2301ab4382d2SGreg Kroah-Hartman * It may be that the port was not available. 2302ab4382d2SGreg Kroah-Hartman */ 2303ab4382d2SGreg Kroah-Hartman if (port->cons && !(port->cons->flags & CON_ENABLED)) 2304ab4382d2SGreg Kroah-Hartman register_console(port->cons); 2305ab4382d2SGreg Kroah-Hartman 2306ab4382d2SGreg Kroah-Hartman /* 2307ab4382d2SGreg Kroah-Hartman * Power down all ports by default, except the 2308ab4382d2SGreg Kroah-Hartman * console if we have one. 2309ab4382d2SGreg Kroah-Hartman */ 2310ab4382d2SGreg Kroah-Hartman if (!uart_console(port)) 23116f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_OFF); 2312ab4382d2SGreg Kroah-Hartman } 2313ab4382d2SGreg Kroah-Hartman } 2314ab4382d2SGreg Kroah-Hartman 2315ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2316ab4382d2SGreg Kroah-Hartman 2317ab4382d2SGreg Kroah-Hartman static int uart_poll_init(struct tty_driver *driver, int line, char *options) 2318ab4382d2SGreg Kroah-Hartman { 2319ab4382d2SGreg Kroah-Hartman struct uart_driver *drv = driver->driver_state; 2320ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + line; 232149c02304SPeter Hurley struct tty_port *tport; 2322ab4382d2SGreg Kroah-Hartman struct uart_port *port; 2323ab4382d2SGreg Kroah-Hartman int baud = 9600; 2324ab4382d2SGreg Kroah-Hartman int bits = 8; 2325ab4382d2SGreg Kroah-Hartman int parity = 'n'; 2326ab4382d2SGreg Kroah-Hartman int flow = 'n'; 232749c02304SPeter Hurley int ret = 0; 2328ab4382d2SGreg Kroah-Hartman 232949c02304SPeter Hurley if (!state) 2330ab4382d2SGreg Kroah-Hartman return -1; 2331ab4382d2SGreg Kroah-Hartman 233249c02304SPeter Hurley tport = &state->port; 233349c02304SPeter Hurley mutex_lock(&tport->mutex); 233449c02304SPeter Hurley 23354047b371SPeter Hurley port = uart_port_check(state); 23364047b371SPeter Hurley if (!port || !(port->ops->poll_get_char && port->ops->poll_put_char)) { 233749c02304SPeter Hurley ret = -1; 233849c02304SPeter Hurley goto out; 233949c02304SPeter Hurley } 2340ab4382d2SGreg Kroah-Hartman 2341c7f3e708SAnton Vorontsov if (port->ops->poll_init) { 2342c7f3e708SAnton Vorontsov /* 2343d41861caSPeter Hurley * We don't set initialized as we only initialized the hw, 2344d41861caSPeter Hurley * e.g. state->xmit is still uninitialized. 2345c7f3e708SAnton Vorontsov */ 2346d41861caSPeter Hurley if (!tty_port_initialized(tport)) 2347c7f3e708SAnton Vorontsov ret = port->ops->poll_init(port); 2348c7f3e708SAnton Vorontsov } 2349c7f3e708SAnton Vorontsov 235049c02304SPeter Hurley if (!ret && options) { 2351ab4382d2SGreg Kroah-Hartman uart_parse_options(options, &baud, &parity, &bits, &flow); 235249c02304SPeter Hurley ret = uart_set_options(port, NULL, baud, parity, bits, flow); 2353ab4382d2SGreg Kroah-Hartman } 235449c02304SPeter Hurley out: 235549c02304SPeter Hurley mutex_unlock(&tport->mutex); 235649c02304SPeter Hurley return ret; 2357ab4382d2SGreg Kroah-Hartman } 2358ab4382d2SGreg Kroah-Hartman 2359ab4382d2SGreg Kroah-Hartman static int uart_poll_get_char(struct tty_driver *driver, int line) 2360ab4382d2SGreg Kroah-Hartman { 2361ab4382d2SGreg Kroah-Hartman struct uart_driver *drv = driver->driver_state; 2362ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + line; 2363ab4382d2SGreg Kroah-Hartman struct uart_port *port; 23649ed19428SPeter Hurley int ret = -1; 2365ab4382d2SGreg Kroah-Hartman 23669ed19428SPeter Hurley if (state) { 23679ed19428SPeter Hurley port = uart_port_ref(state); 23689ed19428SPeter Hurley if (port) 23699ed19428SPeter Hurley ret = port->ops->poll_get_char(port); 23709ed19428SPeter Hurley uart_port_deref(port); 23719ed19428SPeter Hurley } 23729ed19428SPeter Hurley return ret; 2373ab4382d2SGreg Kroah-Hartman } 2374ab4382d2SGreg Kroah-Hartman 2375ab4382d2SGreg Kroah-Hartman static void uart_poll_put_char(struct tty_driver *driver, int line, char ch) 2376ab4382d2SGreg Kroah-Hartman { 2377ab4382d2SGreg Kroah-Hartman struct uart_driver *drv = driver->driver_state; 2378ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + line; 2379ab4382d2SGreg Kroah-Hartman struct uart_port *port; 2380ab4382d2SGreg Kroah-Hartman 23819ed19428SPeter Hurley if (!state) 2382ab4382d2SGreg Kroah-Hartman return; 2383ab4382d2SGreg Kroah-Hartman 23849ed19428SPeter Hurley port = uart_port_ref(state); 23859ed19428SPeter Hurley if (!port) 23869ed19428SPeter Hurley return; 2387c7d44a02SDoug Anderson 2388c7d44a02SDoug Anderson if (ch == '\n') 2389c7d44a02SDoug Anderson port->ops->poll_put_char(port, '\r'); 2390ab4382d2SGreg Kroah-Hartman port->ops->poll_put_char(port, ch); 23919ed19428SPeter Hurley uart_port_deref(port); 2392ab4382d2SGreg Kroah-Hartman } 2393ab4382d2SGreg Kroah-Hartman #endif 2394ab4382d2SGreg Kroah-Hartman 2395ab4382d2SGreg Kroah-Hartman static const struct tty_operations uart_ops = { 2396ab4382d2SGreg Kroah-Hartman .open = uart_open, 2397ab4382d2SGreg Kroah-Hartman .close = uart_close, 2398ab4382d2SGreg Kroah-Hartman .write = uart_write, 2399ab4382d2SGreg Kroah-Hartman .put_char = uart_put_char, 2400ab4382d2SGreg Kroah-Hartman .flush_chars = uart_flush_chars, 2401ab4382d2SGreg Kroah-Hartman .write_room = uart_write_room, 2402ab4382d2SGreg Kroah-Hartman .chars_in_buffer= uart_chars_in_buffer, 2403ab4382d2SGreg Kroah-Hartman .flush_buffer = uart_flush_buffer, 2404ab4382d2SGreg Kroah-Hartman .ioctl = uart_ioctl, 2405ab4382d2SGreg Kroah-Hartman .throttle = uart_throttle, 2406ab4382d2SGreg Kroah-Hartman .unthrottle = uart_unthrottle, 2407ab4382d2SGreg Kroah-Hartman .send_xchar = uart_send_xchar, 2408ab4382d2SGreg Kroah-Hartman .set_termios = uart_set_termios, 2409ab4382d2SGreg Kroah-Hartman .set_ldisc = uart_set_ldisc, 2410ab4382d2SGreg Kroah-Hartman .stop = uart_stop, 2411ab4382d2SGreg Kroah-Hartman .start = uart_start, 2412ab4382d2SGreg Kroah-Hartman .hangup = uart_hangup, 2413ab4382d2SGreg Kroah-Hartman .break_ctl = uart_break_ctl, 2414ab4382d2SGreg Kroah-Hartman .wait_until_sent= uart_wait_until_sent, 2415ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PROC_FS 2416ab4382d2SGreg Kroah-Hartman .proc_fops = &uart_proc_fops, 2417ab4382d2SGreg Kroah-Hartman #endif 2418ab4382d2SGreg Kroah-Hartman .tiocmget = uart_tiocmget, 2419ab4382d2SGreg Kroah-Hartman .tiocmset = uart_tiocmset, 2420ab4382d2SGreg Kroah-Hartman .get_icount = uart_get_icount, 2421ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2422ab4382d2SGreg Kroah-Hartman .poll_init = uart_poll_init, 2423ab4382d2SGreg Kroah-Hartman .poll_get_char = uart_poll_get_char, 2424ab4382d2SGreg Kroah-Hartman .poll_put_char = uart_poll_put_char, 2425ab4382d2SGreg Kroah-Hartman #endif 2426ab4382d2SGreg Kroah-Hartman }; 2427ab4382d2SGreg Kroah-Hartman 2428ab4382d2SGreg Kroah-Hartman static const struct tty_port_operations uart_port_ops = { 2429ab4382d2SGreg Kroah-Hartman .carrier_raised = uart_carrier_raised, 2430ab4382d2SGreg Kroah-Hartman .dtr_rts = uart_dtr_rts, 2431b3b57646SRob Herring .activate = uart_port_activate, 2432761ed4a9SRob Herring .shutdown = uart_tty_port_shutdown, 2433ab4382d2SGreg Kroah-Hartman }; 2434ab4382d2SGreg Kroah-Hartman 2435ab4382d2SGreg Kroah-Hartman /** 2436ab4382d2SGreg Kroah-Hartman * uart_register_driver - register a driver with the uart core layer 2437ab4382d2SGreg Kroah-Hartman * @drv: low level driver structure 2438ab4382d2SGreg Kroah-Hartman * 2439ab4382d2SGreg Kroah-Hartman * Register a uart driver with the core driver. We in turn register 2440ab4382d2SGreg Kroah-Hartman * with the tty layer, and initialise the core driver per-port state. 2441ab4382d2SGreg Kroah-Hartman * 2442ab4382d2SGreg Kroah-Hartman * We have a proc file in /proc/tty/driver which is named after the 2443ab4382d2SGreg Kroah-Hartman * normal driver. 2444ab4382d2SGreg Kroah-Hartman * 2445ab4382d2SGreg Kroah-Hartman * drv->port should be NULL, and the per-port structures should be 2446ab4382d2SGreg Kroah-Hartman * registered using uart_add_one_port after this call has succeeded. 2447ab4382d2SGreg Kroah-Hartman */ 2448ab4382d2SGreg Kroah-Hartman int uart_register_driver(struct uart_driver *drv) 2449ab4382d2SGreg Kroah-Hartman { 2450ab4382d2SGreg Kroah-Hartman struct tty_driver *normal; 2451ab4382d2SGreg Kroah-Hartman int i, retval; 2452ab4382d2SGreg Kroah-Hartman 2453ab4382d2SGreg Kroah-Hartman BUG_ON(drv->state); 2454ab4382d2SGreg Kroah-Hartman 2455ab4382d2SGreg Kroah-Hartman /* 2456ab4382d2SGreg Kroah-Hartman * Maybe we should be using a slab cache for this, especially if 2457ab4382d2SGreg Kroah-Hartman * we have a large number of ports to handle. 2458ab4382d2SGreg Kroah-Hartman */ 2459ab4382d2SGreg Kroah-Hartman drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL); 2460ab4382d2SGreg Kroah-Hartman if (!drv->state) 2461ab4382d2SGreg Kroah-Hartman goto out; 2462ab4382d2SGreg Kroah-Hartman 2463ab4382d2SGreg Kroah-Hartman normal = alloc_tty_driver(drv->nr); 2464ab4382d2SGreg Kroah-Hartman if (!normal) 2465ab4382d2SGreg Kroah-Hartman goto out_kfree; 2466ab4382d2SGreg Kroah-Hartman 2467ab4382d2SGreg Kroah-Hartman drv->tty_driver = normal; 2468ab4382d2SGreg Kroah-Hartman 2469ab4382d2SGreg Kroah-Hartman normal->driver_name = drv->driver_name; 2470ab4382d2SGreg Kroah-Hartman normal->name = drv->dev_name; 2471ab4382d2SGreg Kroah-Hartman normal->major = drv->major; 2472ab4382d2SGreg Kroah-Hartman normal->minor_start = drv->minor; 2473ab4382d2SGreg Kroah-Hartman normal->type = TTY_DRIVER_TYPE_SERIAL; 2474ab4382d2SGreg Kroah-Hartman normal->subtype = SERIAL_TYPE_NORMAL; 2475ab4382d2SGreg Kroah-Hartman normal->init_termios = tty_std_termios; 2476ab4382d2SGreg Kroah-Hartman normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; 2477ab4382d2SGreg Kroah-Hartman normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600; 2478ab4382d2SGreg Kroah-Hartman normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 2479ab4382d2SGreg Kroah-Hartman normal->driver_state = drv; 2480ab4382d2SGreg Kroah-Hartman tty_set_operations(normal, &uart_ops); 2481ab4382d2SGreg Kroah-Hartman 2482ab4382d2SGreg Kroah-Hartman /* 2483ab4382d2SGreg Kroah-Hartman * Initialise the UART state(s). 2484ab4382d2SGreg Kroah-Hartman */ 2485ab4382d2SGreg Kroah-Hartman for (i = 0; i < drv->nr; i++) { 2486ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + i; 2487ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 2488ab4382d2SGreg Kroah-Hartman 2489ab4382d2SGreg Kroah-Hartman tty_port_init(port); 2490ab4382d2SGreg Kroah-Hartman port->ops = &uart_port_ops; 2491ab4382d2SGreg Kroah-Hartman } 2492ab4382d2SGreg Kroah-Hartman 2493ab4382d2SGreg Kroah-Hartman retval = tty_register_driver(normal); 2494ab4382d2SGreg Kroah-Hartman if (retval >= 0) 2495ab4382d2SGreg Kroah-Hartman return retval; 2496ab4382d2SGreg Kroah-Hartman 2497191c5f10SJiri Slaby for (i = 0; i < drv->nr; i++) 2498191c5f10SJiri Slaby tty_port_destroy(&drv->state[i].port); 2499ab4382d2SGreg Kroah-Hartman put_tty_driver(normal); 2500ab4382d2SGreg Kroah-Hartman out_kfree: 2501ab4382d2SGreg Kroah-Hartman kfree(drv->state); 2502ab4382d2SGreg Kroah-Hartman out: 2503ab4382d2SGreg Kroah-Hartman return -ENOMEM; 2504ab4382d2SGreg Kroah-Hartman } 2505ab4382d2SGreg Kroah-Hartman 2506ab4382d2SGreg Kroah-Hartman /** 2507ab4382d2SGreg Kroah-Hartman * uart_unregister_driver - remove a driver from the uart core layer 2508ab4382d2SGreg Kroah-Hartman * @drv: low level driver structure 2509ab4382d2SGreg Kroah-Hartman * 2510ab4382d2SGreg Kroah-Hartman * Remove all references to a driver from the core driver. The low 2511ab4382d2SGreg Kroah-Hartman * level driver must have removed all its ports via the 2512ab4382d2SGreg Kroah-Hartman * uart_remove_one_port() if it registered them with uart_add_one_port(). 2513ab4382d2SGreg Kroah-Hartman * (ie, drv->port == NULL) 2514ab4382d2SGreg Kroah-Hartman */ 2515ab4382d2SGreg Kroah-Hartman void uart_unregister_driver(struct uart_driver *drv) 2516ab4382d2SGreg Kroah-Hartman { 2517ab4382d2SGreg Kroah-Hartman struct tty_driver *p = drv->tty_driver; 2518191c5f10SJiri Slaby unsigned int i; 2519191c5f10SJiri Slaby 2520ab4382d2SGreg Kroah-Hartman tty_unregister_driver(p); 2521ab4382d2SGreg Kroah-Hartman put_tty_driver(p); 2522191c5f10SJiri Slaby for (i = 0; i < drv->nr; i++) 2523191c5f10SJiri Slaby tty_port_destroy(&drv->state[i].port); 2524ab4382d2SGreg Kroah-Hartman kfree(drv->state); 25251e66cdedSAlan Cox drv->state = NULL; 2526ab4382d2SGreg Kroah-Hartman drv->tty_driver = NULL; 2527ab4382d2SGreg Kroah-Hartman } 2528ab4382d2SGreg Kroah-Hartman 2529ab4382d2SGreg Kroah-Hartman struct tty_driver *uart_console_device(struct console *co, int *index) 2530ab4382d2SGreg Kroah-Hartman { 2531ab4382d2SGreg Kroah-Hartman struct uart_driver *p = co->data; 2532ab4382d2SGreg Kroah-Hartman *index = co->index; 2533ab4382d2SGreg Kroah-Hartman return p->tty_driver; 2534ab4382d2SGreg Kroah-Hartman } 2535ab4382d2SGreg Kroah-Hartman 25366915c0e4STomas Hlavacek static ssize_t uart_get_attr_uartclk(struct device *dev, 25376915c0e4STomas Hlavacek struct device_attribute *attr, char *buf) 25386915c0e4STomas Hlavacek { 2539bebe73e3SAlan Cox struct serial_struct tmp; 25406915c0e4STomas Hlavacek struct tty_port *port = dev_get_drvdata(dev); 2541b1b79916STomas Hlavacek 25429f109694SAlan Cox uart_get_info(port, &tmp); 2543bebe73e3SAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16); 25446915c0e4STomas Hlavacek } 25456915c0e4STomas Hlavacek 2546373bac4cSAlan Cox static ssize_t uart_get_attr_type(struct device *dev, 2547373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2548373bac4cSAlan Cox { 2549373bac4cSAlan Cox struct serial_struct tmp; 2550373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2551373bac4cSAlan Cox 2552373bac4cSAlan Cox uart_get_info(port, &tmp); 2553373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type); 2554373bac4cSAlan Cox } 2555373bac4cSAlan Cox static ssize_t uart_get_attr_line(struct device *dev, 2556373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2557373bac4cSAlan Cox { 2558373bac4cSAlan Cox struct serial_struct tmp; 2559373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2560373bac4cSAlan Cox 2561373bac4cSAlan Cox uart_get_info(port, &tmp); 2562373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line); 2563373bac4cSAlan Cox } 2564373bac4cSAlan Cox 2565373bac4cSAlan Cox static ssize_t uart_get_attr_port(struct device *dev, 2566373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2567373bac4cSAlan Cox { 2568373bac4cSAlan Cox struct serial_struct tmp; 2569373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2570fd985e1dSAndrew Morton unsigned long ioaddr; 2571373bac4cSAlan Cox 2572373bac4cSAlan Cox uart_get_info(port, &tmp); 2573fd985e1dSAndrew Morton ioaddr = tmp.port; 2574fd985e1dSAndrew Morton if (HIGH_BITS_OFFSET) 2575fd985e1dSAndrew Morton ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET; 2576fd985e1dSAndrew Morton return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr); 2577373bac4cSAlan Cox } 2578373bac4cSAlan Cox 2579373bac4cSAlan Cox static ssize_t uart_get_attr_irq(struct device *dev, 2580373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2581373bac4cSAlan Cox { 2582373bac4cSAlan Cox struct serial_struct tmp; 2583373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2584373bac4cSAlan Cox 2585373bac4cSAlan Cox uart_get_info(port, &tmp); 2586373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq); 2587373bac4cSAlan Cox } 2588373bac4cSAlan Cox 2589373bac4cSAlan Cox static ssize_t uart_get_attr_flags(struct device *dev, 2590373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2591373bac4cSAlan Cox { 2592373bac4cSAlan Cox struct serial_struct tmp; 2593373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2594373bac4cSAlan Cox 2595373bac4cSAlan Cox uart_get_info(port, &tmp); 2596373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags); 2597373bac4cSAlan Cox } 2598373bac4cSAlan Cox 2599373bac4cSAlan Cox static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev, 2600373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2601373bac4cSAlan Cox { 2602373bac4cSAlan Cox struct serial_struct tmp; 2603373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2604373bac4cSAlan Cox 2605373bac4cSAlan Cox uart_get_info(port, &tmp); 2606373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size); 2607373bac4cSAlan Cox } 2608373bac4cSAlan Cox 2609373bac4cSAlan Cox 2610373bac4cSAlan Cox static ssize_t uart_get_attr_close_delay(struct device *dev, 2611373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2612373bac4cSAlan Cox { 2613373bac4cSAlan Cox struct serial_struct tmp; 2614373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2615373bac4cSAlan Cox 2616373bac4cSAlan Cox uart_get_info(port, &tmp); 2617373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay); 2618373bac4cSAlan Cox } 2619373bac4cSAlan Cox 2620373bac4cSAlan Cox 2621373bac4cSAlan Cox static ssize_t uart_get_attr_closing_wait(struct device *dev, 2622373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2623373bac4cSAlan Cox { 2624373bac4cSAlan Cox struct serial_struct tmp; 2625373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2626373bac4cSAlan Cox 2627373bac4cSAlan Cox uart_get_info(port, &tmp); 2628373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait); 2629373bac4cSAlan Cox } 2630373bac4cSAlan Cox 2631373bac4cSAlan Cox static ssize_t uart_get_attr_custom_divisor(struct device *dev, 2632373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2633373bac4cSAlan Cox { 2634373bac4cSAlan Cox struct serial_struct tmp; 2635373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2636373bac4cSAlan Cox 2637373bac4cSAlan Cox uart_get_info(port, &tmp); 2638373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor); 2639373bac4cSAlan Cox } 2640373bac4cSAlan Cox 2641373bac4cSAlan Cox static ssize_t uart_get_attr_io_type(struct device *dev, 2642373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2643373bac4cSAlan Cox { 2644373bac4cSAlan Cox struct serial_struct tmp; 2645373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2646373bac4cSAlan Cox 2647373bac4cSAlan Cox uart_get_info(port, &tmp); 2648373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type); 2649373bac4cSAlan Cox } 2650373bac4cSAlan Cox 2651373bac4cSAlan Cox static ssize_t uart_get_attr_iomem_base(struct device *dev, 2652373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2653373bac4cSAlan Cox { 2654373bac4cSAlan Cox struct serial_struct tmp; 2655373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2656373bac4cSAlan Cox 2657373bac4cSAlan Cox uart_get_info(port, &tmp); 2658373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base); 2659373bac4cSAlan Cox } 2660373bac4cSAlan Cox 2661373bac4cSAlan Cox static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev, 2662373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2663373bac4cSAlan Cox { 2664373bac4cSAlan Cox struct serial_struct tmp; 2665373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2666373bac4cSAlan Cox 2667373bac4cSAlan Cox uart_get_info(port, &tmp); 2668373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift); 2669373bac4cSAlan Cox } 2670373bac4cSAlan Cox 2671373bac4cSAlan Cox static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL); 2672373bac4cSAlan Cox static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL); 2673373bac4cSAlan Cox static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL); 2674373bac4cSAlan Cox static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL); 2675373bac4cSAlan Cox static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL); 2676373bac4cSAlan Cox static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL); 26776915c0e4STomas Hlavacek static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL); 2678373bac4cSAlan Cox static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL); 2679373bac4cSAlan Cox static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL); 2680373bac4cSAlan Cox static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL); 2681373bac4cSAlan Cox static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL); 2682373bac4cSAlan Cox static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL); 2683373bac4cSAlan Cox static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL); 26846915c0e4STomas Hlavacek 26856915c0e4STomas Hlavacek static struct attribute *tty_dev_attrs[] = { 2686373bac4cSAlan Cox &dev_attr_type.attr, 2687373bac4cSAlan Cox &dev_attr_line.attr, 2688373bac4cSAlan Cox &dev_attr_port.attr, 2689373bac4cSAlan Cox &dev_attr_irq.attr, 2690373bac4cSAlan Cox &dev_attr_flags.attr, 2691373bac4cSAlan Cox &dev_attr_xmit_fifo_size.attr, 26926915c0e4STomas Hlavacek &dev_attr_uartclk.attr, 2693373bac4cSAlan Cox &dev_attr_close_delay.attr, 2694373bac4cSAlan Cox &dev_attr_closing_wait.attr, 2695373bac4cSAlan Cox &dev_attr_custom_divisor.attr, 2696373bac4cSAlan Cox &dev_attr_io_type.attr, 2697373bac4cSAlan Cox &dev_attr_iomem_base.attr, 2698373bac4cSAlan Cox &dev_attr_iomem_reg_shift.attr, 26996915c0e4STomas Hlavacek NULL, 27006915c0e4STomas Hlavacek }; 27016915c0e4STomas Hlavacek 2702b1b79916STomas Hlavacek static const struct attribute_group tty_dev_attr_group = { 27036915c0e4STomas Hlavacek .attrs = tty_dev_attrs, 27046915c0e4STomas Hlavacek }; 27056915c0e4STomas Hlavacek 2706ab4382d2SGreg Kroah-Hartman /** 2707ab4382d2SGreg Kroah-Hartman * uart_add_one_port - attach a driver-defined port structure 2708ab4382d2SGreg Kroah-Hartman * @drv: pointer to the uart low level driver structure for this port 2709ab4382d2SGreg Kroah-Hartman * @uport: uart port structure to use for this port. 2710ab4382d2SGreg Kroah-Hartman * 2711ab4382d2SGreg Kroah-Hartman * This allows the driver to register its own uart_port structure 2712ab4382d2SGreg Kroah-Hartman * with the core driver. The main purpose is to allow the low 2713ab4382d2SGreg Kroah-Hartman * level uart drivers to expand uart_port, rather than having yet 2714ab4382d2SGreg Kroah-Hartman * more levels of structures. 2715ab4382d2SGreg Kroah-Hartman */ 2716ab4382d2SGreg Kroah-Hartman int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport) 2717ab4382d2SGreg Kroah-Hartman { 2718ab4382d2SGreg Kroah-Hartman struct uart_state *state; 2719ab4382d2SGreg Kroah-Hartman struct tty_port *port; 2720ab4382d2SGreg Kroah-Hartman int ret = 0; 2721ab4382d2SGreg Kroah-Hartman struct device *tty_dev; 2722266dcff0SGreg Kroah-Hartman int num_groups; 2723ab4382d2SGreg Kroah-Hartman 2724ab4382d2SGreg Kroah-Hartman BUG_ON(in_interrupt()); 2725ab4382d2SGreg Kroah-Hartman 2726ab4382d2SGreg Kroah-Hartman if (uport->line >= drv->nr) 2727ab4382d2SGreg Kroah-Hartman return -EINVAL; 2728ab4382d2SGreg Kroah-Hartman 2729ab4382d2SGreg Kroah-Hartman state = drv->state + uport->line; 2730ab4382d2SGreg Kroah-Hartman port = &state->port; 2731ab4382d2SGreg Kroah-Hartman 2732ab4382d2SGreg Kroah-Hartman mutex_lock(&port_mutex); 2733ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 2734ab4382d2SGreg Kroah-Hartman if (state->uart_port) { 2735ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2736ab4382d2SGreg Kroah-Hartman goto out; 2737ab4382d2SGreg Kroah-Hartman } 2738ab4382d2SGreg Kroah-Hartman 27392b702b9bSPeter Hurley /* Link the port to the driver state table and vice versa */ 27409ed19428SPeter Hurley atomic_set(&state->refcount, 1); 27419ed19428SPeter Hurley init_waitqueue_head(&state->remove_wait); 2742ab4382d2SGreg Kroah-Hartman state->uart_port = uport; 2743ab4382d2SGreg Kroah-Hartman uport->state = state; 2744ab4382d2SGreg Kroah-Hartman 27452b702b9bSPeter Hurley state->pm_state = UART_PM_STATE_UNDEFINED; 27462b702b9bSPeter Hurley uport->cons = drv->cons; 2747959801feSPeter Hurley uport->minor = drv->tty_driver->minor_start + uport->line; 27482b702b9bSPeter Hurley 2749ab4382d2SGreg Kroah-Hartman /* 2750ab4382d2SGreg Kroah-Hartman * If this port is a console, then the spinlock is already 2751ab4382d2SGreg Kroah-Hartman * initialised. 2752ab4382d2SGreg Kroah-Hartman */ 2753ab4382d2SGreg Kroah-Hartman if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) { 2754ab4382d2SGreg Kroah-Hartman spin_lock_init(&uport->lock); 2755ab4382d2SGreg Kroah-Hartman lockdep_set_class(&uport->lock, &port_lock_key); 2756ab4382d2SGreg Kroah-Hartman } 2757a208ffd2SGrant Likely if (uport->cons && uport->dev) 2758a208ffd2SGrant Likely of_console_check(uport->dev->of_node, uport->cons->name, uport->line); 2759ab4382d2SGreg Kroah-Hartman 2760ab4382d2SGreg Kroah-Hartman uart_configure_port(drv, state, uport); 2761ab4382d2SGreg Kroah-Hartman 27624dda864dSGeert Uytterhoeven port->console = uart_console(uport); 27634dda864dSGeert Uytterhoeven 2764266dcff0SGreg Kroah-Hartman num_groups = 2; 2765266dcff0SGreg Kroah-Hartman if (uport->attr_group) 2766266dcff0SGreg Kroah-Hartman num_groups++; 2767266dcff0SGreg Kroah-Hartman 2768c2b703b8SYoshihiro YUNOMAE uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups), 2769266dcff0SGreg Kroah-Hartman GFP_KERNEL); 2770266dcff0SGreg Kroah-Hartman if (!uport->tty_groups) { 2771266dcff0SGreg Kroah-Hartman ret = -ENOMEM; 2772266dcff0SGreg Kroah-Hartman goto out; 2773266dcff0SGreg Kroah-Hartman } 2774266dcff0SGreg Kroah-Hartman uport->tty_groups[0] = &tty_dev_attr_group; 2775266dcff0SGreg Kroah-Hartman if (uport->attr_group) 2776266dcff0SGreg Kroah-Hartman uport->tty_groups[1] = uport->attr_group; 2777266dcff0SGreg Kroah-Hartman 2778ab4382d2SGreg Kroah-Hartman /* 2779ab4382d2SGreg Kroah-Hartman * Register the port whether it's detected or not. This allows 2780015355b7SGeert Uytterhoeven * setserial to be used to alter this port's parameters. 2781ab4382d2SGreg Kroah-Hartman */ 2782b1b79916STomas Hlavacek tty_dev = tty_port_register_device_attr(port, drv->tty_driver, 2783266dcff0SGreg Kroah-Hartman uport->line, uport->dev, port, uport->tty_groups); 2784ab4382d2SGreg Kroah-Hartman if (likely(!IS_ERR(tty_dev))) { 278577359835SSimon Glass device_set_wakeup_capable(tty_dev, 1); 278677359835SSimon Glass } else { 27872f2dafe7SSudip Mukherjee dev_err(uport->dev, "Cannot register tty device on line %d\n", 2788ab4382d2SGreg Kroah-Hartman uport->line); 278977359835SSimon Glass } 2790ab4382d2SGreg Kroah-Hartman 2791ab4382d2SGreg Kroah-Hartman /* 2792ab4382d2SGreg Kroah-Hartman * Ensure UPF_DEAD is not set. 2793ab4382d2SGreg Kroah-Hartman */ 2794ab4382d2SGreg Kroah-Hartman uport->flags &= ~UPF_DEAD; 2795ab4382d2SGreg Kroah-Hartman 2796ab4382d2SGreg Kroah-Hartman out: 2797ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2798ab4382d2SGreg Kroah-Hartman mutex_unlock(&port_mutex); 2799ab4382d2SGreg Kroah-Hartman 2800ab4382d2SGreg Kroah-Hartman return ret; 2801ab4382d2SGreg Kroah-Hartman } 2802ab4382d2SGreg Kroah-Hartman 2803ab4382d2SGreg Kroah-Hartman /** 2804ab4382d2SGreg Kroah-Hartman * uart_remove_one_port - detach a driver defined port structure 2805ab4382d2SGreg Kroah-Hartman * @drv: pointer to the uart low level driver structure for this port 2806ab4382d2SGreg Kroah-Hartman * @uport: uart port structure for this port 2807ab4382d2SGreg Kroah-Hartman * 2808ab4382d2SGreg Kroah-Hartman * This unhooks (and hangs up) the specified port structure from the 2809ab4382d2SGreg Kroah-Hartman * core driver. No further calls will be made to the low-level code 2810ab4382d2SGreg Kroah-Hartman * for this port. 2811ab4382d2SGreg Kroah-Hartman */ 2812ab4382d2SGreg Kroah-Hartman int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport) 2813ab4382d2SGreg Kroah-Hartman { 2814ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + uport->line; 2815ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 28164047b371SPeter Hurley struct uart_port *uart_port; 28174c6d5b4dSGeert Uytterhoeven struct tty_struct *tty; 2818b342dd51SChen Gang int ret = 0; 2819ab4382d2SGreg Kroah-Hartman 2820ab4382d2SGreg Kroah-Hartman BUG_ON(in_interrupt()); 2821ab4382d2SGreg Kroah-Hartman 2822ab4382d2SGreg Kroah-Hartman mutex_lock(&port_mutex); 2823ab4382d2SGreg Kroah-Hartman 2824ab4382d2SGreg Kroah-Hartman /* 2825ab4382d2SGreg Kroah-Hartman * Mark the port "dead" - this prevents any opens from 2826ab4382d2SGreg Kroah-Hartman * succeeding while we shut down the port. 2827ab4382d2SGreg Kroah-Hartman */ 2828ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 28294047b371SPeter Hurley uart_port = uart_port_check(state); 28304047b371SPeter Hurley if (uart_port != uport) 28314047b371SPeter Hurley dev_alert(uport->dev, "Removing wrong port: %p != %p\n", 28324047b371SPeter Hurley uart_port, uport); 28334047b371SPeter Hurley 28344047b371SPeter Hurley if (!uart_port) { 2835b342dd51SChen Gang mutex_unlock(&port->mutex); 2836b342dd51SChen Gang ret = -EINVAL; 2837b342dd51SChen Gang goto out; 2838b342dd51SChen Gang } 2839ab4382d2SGreg Kroah-Hartman uport->flags |= UPF_DEAD; 2840ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2841ab4382d2SGreg Kroah-Hartman 2842ab4382d2SGreg Kroah-Hartman /* 2843ab4382d2SGreg Kroah-Hartman * Remove the devices from the tty layer 2844ab4382d2SGreg Kroah-Hartman */ 2845ab4382d2SGreg Kroah-Hartman tty_unregister_device(drv->tty_driver, uport->line); 2846ab4382d2SGreg Kroah-Hartman 28474c6d5b4dSGeert Uytterhoeven tty = tty_port_tty_get(port); 28484c6d5b4dSGeert Uytterhoeven if (tty) { 2849ab4382d2SGreg Kroah-Hartman tty_vhangup(port->tty); 28504c6d5b4dSGeert Uytterhoeven tty_kref_put(tty); 28514c6d5b4dSGeert Uytterhoeven } 2852ab4382d2SGreg Kroah-Hartman 2853ab4382d2SGreg Kroah-Hartman /* 28545f5c9ae5SGeert Uytterhoeven * If the port is used as a console, unregister it 28555f5c9ae5SGeert Uytterhoeven */ 28565f5c9ae5SGeert Uytterhoeven if (uart_console(uport)) 28575f5c9ae5SGeert Uytterhoeven unregister_console(uport->cons); 28585f5c9ae5SGeert Uytterhoeven 28595f5c9ae5SGeert Uytterhoeven /* 2860ab4382d2SGreg Kroah-Hartman * Free the port IO and memory resources, if any. 2861ab4382d2SGreg Kroah-Hartman */ 2862a7cfaf16SFabio Estevam if (uport->type != PORT_UNKNOWN && uport->ops->release_port) 2863ab4382d2SGreg Kroah-Hartman uport->ops->release_port(uport); 2864266dcff0SGreg Kroah-Hartman kfree(uport->tty_groups); 2865ab4382d2SGreg Kroah-Hartman 2866ab4382d2SGreg Kroah-Hartman /* 2867ab4382d2SGreg Kroah-Hartman * Indicate that there isn't a port here anymore. 2868ab4382d2SGreg Kroah-Hartman */ 2869ab4382d2SGreg Kroah-Hartman uport->type = PORT_UNKNOWN; 2870ab4382d2SGreg Kroah-Hartman 28714047b371SPeter Hurley mutex_lock(&port->mutex); 28729ed19428SPeter Hurley WARN_ON(atomic_dec_return(&state->refcount) < 0); 28739ed19428SPeter Hurley wait_event(state->remove_wait, !atomic_read(&state->refcount)); 2874ab4382d2SGreg Kroah-Hartman state->uart_port = NULL; 28754047b371SPeter Hurley mutex_unlock(&port->mutex); 2876b342dd51SChen Gang out: 2877ab4382d2SGreg Kroah-Hartman mutex_unlock(&port_mutex); 2878ab4382d2SGreg Kroah-Hartman 2879b342dd51SChen Gang return ret; 2880ab4382d2SGreg Kroah-Hartman } 2881ab4382d2SGreg Kroah-Hartman 2882ab4382d2SGreg Kroah-Hartman /* 2883ab4382d2SGreg Kroah-Hartman * Are the two ports equivalent? 2884ab4382d2SGreg Kroah-Hartman */ 2885ab4382d2SGreg Kroah-Hartman int uart_match_port(struct uart_port *port1, struct uart_port *port2) 2886ab4382d2SGreg Kroah-Hartman { 2887ab4382d2SGreg Kroah-Hartman if (port1->iotype != port2->iotype) 2888ab4382d2SGreg Kroah-Hartman return 0; 2889ab4382d2SGreg Kroah-Hartman 2890ab4382d2SGreg Kroah-Hartman switch (port1->iotype) { 2891ab4382d2SGreg Kroah-Hartman case UPIO_PORT: 2892ab4382d2SGreg Kroah-Hartman return (port1->iobase == port2->iobase); 2893ab4382d2SGreg Kroah-Hartman case UPIO_HUB6: 2894ab4382d2SGreg Kroah-Hartman return (port1->iobase == port2->iobase) && 2895ab4382d2SGreg Kroah-Hartman (port1->hub6 == port2->hub6); 2896ab4382d2SGreg Kroah-Hartman case UPIO_MEM: 2897bd94c407SMasahiro Yamada case UPIO_MEM16: 2898ab4382d2SGreg Kroah-Hartman case UPIO_MEM32: 28993ffb1a81SKevin Cernekee case UPIO_MEM32BE: 2900ab4382d2SGreg Kroah-Hartman case UPIO_AU: 2901ab4382d2SGreg Kroah-Hartman case UPIO_TSI: 2902ab4382d2SGreg Kroah-Hartman return (port1->mapbase == port2->mapbase); 2903ab4382d2SGreg Kroah-Hartman } 2904ab4382d2SGreg Kroah-Hartman return 0; 2905ab4382d2SGreg Kroah-Hartman } 2906ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_match_port); 2907ab4382d2SGreg Kroah-Hartman 2908027d7dacSJiri Slaby /** 2909027d7dacSJiri Slaby * uart_handle_dcd_change - handle a change of carrier detect state 2910027d7dacSJiri Slaby * @uport: uart_port structure for the open port 2911027d7dacSJiri Slaby * @status: new carrier detect status, nonzero if active 29124d90bb14SPeter Hurley * 29134d90bb14SPeter Hurley * Caller must hold uport->lock 2914027d7dacSJiri Slaby */ 2915027d7dacSJiri Slaby void uart_handle_dcd_change(struct uart_port *uport, unsigned int status) 2916027d7dacSJiri Slaby { 291742381572SGeorge Spelvin struct tty_port *port = &uport->state->port; 291843eca0aeSAlan Cox struct tty_struct *tty = port->tty; 2919c993257bSPeter Hurley struct tty_ldisc *ld; 2920027d7dacSJiri Slaby 29214d90bb14SPeter Hurley lockdep_assert_held_once(&uport->lock); 29224d90bb14SPeter Hurley 2923c993257bSPeter Hurley if (tty) { 2924c993257bSPeter Hurley ld = tty_ldisc_ref(tty); 292542381572SGeorge Spelvin if (ld) { 292642381572SGeorge Spelvin if (ld->ops->dcd_change) 2927593fb1aeSGeorge Spelvin ld->ops->dcd_change(tty, status); 292842381572SGeorge Spelvin tty_ldisc_deref(ld); 292942381572SGeorge Spelvin } 2930c993257bSPeter Hurley } 2931027d7dacSJiri Slaby 2932027d7dacSJiri Slaby uport->icount.dcd++; 2933027d7dacSJiri Slaby 2934299245a1SPeter Hurley if (uart_dcd_enabled(uport)) { 2935027d7dacSJiri Slaby if (status) 2936027d7dacSJiri Slaby wake_up_interruptible(&port->open_wait); 293743eca0aeSAlan Cox else if (tty) 293843eca0aeSAlan Cox tty_hangup(tty); 2939027d7dacSJiri Slaby } 2940027d7dacSJiri Slaby } 2941027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_handle_dcd_change); 2942027d7dacSJiri Slaby 2943027d7dacSJiri Slaby /** 2944027d7dacSJiri Slaby * uart_handle_cts_change - handle a change of clear-to-send state 2945027d7dacSJiri Slaby * @uport: uart_port structure for the open port 2946027d7dacSJiri Slaby * @status: new clear to send status, nonzero if active 29474d90bb14SPeter Hurley * 29484d90bb14SPeter Hurley * Caller must hold uport->lock 2949027d7dacSJiri Slaby */ 2950027d7dacSJiri Slaby void uart_handle_cts_change(struct uart_port *uport, unsigned int status) 2951027d7dacSJiri Slaby { 29524d90bb14SPeter Hurley lockdep_assert_held_once(&uport->lock); 29534d90bb14SPeter Hurley 2954027d7dacSJiri Slaby uport->icount.cts++; 2955027d7dacSJiri Slaby 2956391f93f2SPeter Hurley if (uart_softcts_mode(uport)) { 2957d01f4d18SPeter Hurley if (uport->hw_stopped) { 2958027d7dacSJiri Slaby if (status) { 2959d01f4d18SPeter Hurley uport->hw_stopped = 0; 2960027d7dacSJiri Slaby uport->ops->start_tx(uport); 2961027d7dacSJiri Slaby uart_write_wakeup(uport); 2962027d7dacSJiri Slaby } 2963027d7dacSJiri Slaby } else { 2964027d7dacSJiri Slaby if (!status) { 2965d01f4d18SPeter Hurley uport->hw_stopped = 1; 2966027d7dacSJiri Slaby uport->ops->stop_tx(uport); 2967027d7dacSJiri Slaby } 2968027d7dacSJiri Slaby } 2969391f93f2SPeter Hurley 2970027d7dacSJiri Slaby } 2971027d7dacSJiri Slaby } 2972027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_handle_cts_change); 2973027d7dacSJiri Slaby 2974cf75525fSJiri Slaby /** 2975cf75525fSJiri Slaby * uart_insert_char - push a char to the uart layer 2976cf75525fSJiri Slaby * 2977cf75525fSJiri Slaby * User is responsible to call tty_flip_buffer_push when they are done with 2978cf75525fSJiri Slaby * insertion. 2979cf75525fSJiri Slaby * 2980cf75525fSJiri Slaby * @port: corresponding port 2981cf75525fSJiri Slaby * @status: state of the serial port RX buffer (LSR for 8250) 2982cf75525fSJiri Slaby * @overrun: mask of overrun bits in @status 2983cf75525fSJiri Slaby * @ch: character to push 2984cf75525fSJiri Slaby * @flag: flag for the character (see TTY_NORMAL and friends) 2985cf75525fSJiri Slaby */ 2986027d7dacSJiri Slaby void uart_insert_char(struct uart_port *port, unsigned int status, 2987027d7dacSJiri Slaby unsigned int overrun, unsigned int ch, unsigned int flag) 2988027d7dacSJiri Slaby { 298992a19f9cSJiri Slaby struct tty_port *tport = &port->state->port; 2990027d7dacSJiri Slaby 2991027d7dacSJiri Slaby if ((status & port->ignore_status_mask & ~overrun) == 0) 299292a19f9cSJiri Slaby if (tty_insert_flip_char(tport, ch, flag) == 0) 2993dabfb351SCorbin ++port->icount.buf_overrun; 2994027d7dacSJiri Slaby 2995027d7dacSJiri Slaby /* 2996027d7dacSJiri Slaby * Overrun is special. Since it's reported immediately, 2997027d7dacSJiri Slaby * it doesn't affect the current character. 2998027d7dacSJiri Slaby */ 2999027d7dacSJiri Slaby if (status & ~port->ignore_status_mask & overrun) 300092a19f9cSJiri Slaby if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0) 3001dabfb351SCorbin ++port->icount.buf_overrun; 3002027d7dacSJiri Slaby } 3003027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_insert_char); 3004027d7dacSJiri Slaby 3005ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_write_wakeup); 3006ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_register_driver); 3007ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_unregister_driver); 3008ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_suspend_port); 3009ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_resume_port); 3010ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_add_one_port); 3011ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_remove_one_port); 3012ab4382d2SGreg Kroah-Hartman 3013ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Serial driver core"); 3014ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL"); 3015