1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+ 2ab4382d2SGreg Kroah-Hartman /* 3ab4382d2SGreg Kroah-Hartman * Driver core for serial ports 4ab4382d2SGreg Kroah-Hartman * 5ab4382d2SGreg Kroah-Hartman * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. 6ab4382d2SGreg Kroah-Hartman * 7ab4382d2SGreg Kroah-Hartman * Copyright 1999 ARM Limited 8ab4382d2SGreg Kroah-Hartman * Copyright (C) 2000-2001 Deep Blue Solutions Ltd. 9ab4382d2SGreg Kroah-Hartman */ 10ab4382d2SGreg Kroah-Hartman #include <linux/module.h> 11ab4382d2SGreg Kroah-Hartman #include <linux/tty.h> 12027d7dacSJiri Slaby #include <linux/tty_flip.h> 13ab4382d2SGreg Kroah-Hartman #include <linux/slab.h> 14174cd4b1SIngo Molnar #include <linux/sched/signal.h> 15ab4382d2SGreg Kroah-Hartman #include <linux/init.h> 16ab4382d2SGreg Kroah-Hartman #include <linux/console.h> 17167cbce2SJohan Hovold #include <linux/gpio/consumer.h> 18a208ffd2SGrant Likely #include <linux/of.h> 19ab4382d2SGreg Kroah-Hartman #include <linux/proc_fs.h> 20ab4382d2SGreg Kroah-Hartman #include <linux/seq_file.h> 21ab4382d2SGreg Kroah-Hartman #include <linux/device.h> 22ab4382d2SGreg Kroah-Hartman #include <linux/serial.h> /* for serial_state and serial_icounter_struct */ 23ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h> 2468af4317SDmitry Safonov #include <linux/sysrq.h> 25ab4382d2SGreg Kroah-Hartman #include <linux/delay.h> 26ab4382d2SGreg Kroah-Hartman #include <linux/mutex.h> 27794edf30SDavid Howells #include <linux/security.h> 28ab4382d2SGreg Kroah-Hartman 29aef3ad10SAndy Shevchenko #include <linux/irq.h> 307c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 31ab4382d2SGreg Kroah-Hartman 32ab4382d2SGreg Kroah-Hartman /* 33ab4382d2SGreg Kroah-Hartman * This is used to lock changes in serial line configuration. 34ab4382d2SGreg Kroah-Hartman */ 35ab4382d2SGreg Kroah-Hartman static DEFINE_MUTEX(port_mutex); 36ab4382d2SGreg Kroah-Hartman 37ab4382d2SGreg Kroah-Hartman /* 38ab4382d2SGreg Kroah-Hartman * lockdep: port->lock is initialized in two places, but we 39ab4382d2SGreg Kroah-Hartman * want only one lock-class: 40ab4382d2SGreg Kroah-Hartman */ 41ab4382d2SGreg Kroah-Hartman static struct lock_class_key port_lock_key; 42ab4382d2SGreg Kroah-Hartman 43ab4382d2SGreg Kroah-Hartman #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8) 44ab4382d2SGreg Kroah-Hartman 45ab4382d2SGreg Kroah-Hartman static void uart_change_speed(struct tty_struct *tty, struct uart_state *state, 46ab4382d2SGreg Kroah-Hartman struct ktermios *old_termios); 471f33a51dSJiri Slaby static void uart_wait_until_sent(struct tty_struct *tty, int timeout); 486f538fe3SLinus Walleij static void uart_change_pm(struct uart_state *state, 496f538fe3SLinus Walleij enum uart_pm_state pm_state); 50ab4382d2SGreg Kroah-Hartman 51b922e19dSJiri Slaby static void uart_port_shutdown(struct tty_port *port); 52b922e19dSJiri Slaby 53299245a1SPeter Hurley static int uart_dcd_enabled(struct uart_port *uport) 54299245a1SPeter Hurley { 55d4260b51SPeter Hurley return !!(uport->status & UPSTAT_DCD_ENABLE); 56299245a1SPeter Hurley } 57299245a1SPeter Hurley 589ed19428SPeter Hurley static inline struct uart_port *uart_port_ref(struct uart_state *state) 599ed19428SPeter Hurley { 609ed19428SPeter Hurley if (atomic_add_unless(&state->refcount, 1, 0)) 619ed19428SPeter Hurley return state->uart_port; 629ed19428SPeter Hurley return NULL; 639ed19428SPeter Hurley } 649ed19428SPeter Hurley 659ed19428SPeter Hurley static inline void uart_port_deref(struct uart_port *uport) 669ed19428SPeter Hurley { 67ef510beaSAndy Shevchenko if (atomic_dec_and_test(&uport->state->refcount)) 689ed19428SPeter Hurley wake_up(&uport->state->remove_wait); 699ed19428SPeter Hurley } 709ed19428SPeter Hurley 719ed19428SPeter Hurley #define uart_port_lock(state, flags) \ 729ed19428SPeter Hurley ({ \ 739ed19428SPeter Hurley struct uart_port *__uport = uart_port_ref(state); \ 749ed19428SPeter Hurley if (__uport) \ 759ed19428SPeter Hurley spin_lock_irqsave(&__uport->lock, flags); \ 769ed19428SPeter Hurley __uport; \ 779ed19428SPeter Hurley }) 789ed19428SPeter Hurley 799ed19428SPeter Hurley #define uart_port_unlock(uport, flags) \ 809ed19428SPeter Hurley ({ \ 819ed19428SPeter Hurley struct uart_port *__uport = uport; \ 82ef510beaSAndy Shevchenko if (__uport) { \ 839ed19428SPeter Hurley spin_unlock_irqrestore(&__uport->lock, flags); \ 849ed19428SPeter Hurley uart_port_deref(__uport); \ 85ef510beaSAndy Shevchenko } \ 869ed19428SPeter Hurley }) 879ed19428SPeter Hurley 884047b371SPeter Hurley static inline struct uart_port *uart_port_check(struct uart_state *state) 894047b371SPeter Hurley { 907da4b8b7SPeter Hurley lockdep_assert_held(&state->port.mutex); 914047b371SPeter Hurley return state->uart_port; 924047b371SPeter Hurley } 934047b371SPeter Hurley 94ab4382d2SGreg Kroah-Hartman /* 95ab4382d2SGreg Kroah-Hartman * This routine is used by the interrupt handler to schedule processing in 96ab4382d2SGreg Kroah-Hartman * the software interrupt portion of the driver. 97ab4382d2SGreg Kroah-Hartman */ 98ab4382d2SGreg Kroah-Hartman void uart_write_wakeup(struct uart_port *port) 99ab4382d2SGreg Kroah-Hartman { 100ab4382d2SGreg Kroah-Hartman struct uart_state *state = port->state; 101ab4382d2SGreg Kroah-Hartman /* 102ab4382d2SGreg Kroah-Hartman * This means you called this function _after_ the port was 103ab4382d2SGreg Kroah-Hartman * closed. No cookie for you. 104ab4382d2SGreg Kroah-Hartman */ 105ab4382d2SGreg Kroah-Hartman BUG_ON(!state); 106d0f4bce2SRob Herring tty_port_tty_wakeup(&state->port); 107ab4382d2SGreg Kroah-Hartman } 108ab4382d2SGreg Kroah-Hartman 109ab4382d2SGreg Kroah-Hartman static void uart_stop(struct tty_struct *tty) 110ab4382d2SGreg Kroah-Hartman { 111ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1129ed19428SPeter Hurley struct uart_port *port; 113ab4382d2SGreg Kroah-Hartman unsigned long flags; 114ab4382d2SGreg Kroah-Hartman 1159ed19428SPeter Hurley port = uart_port_lock(state, flags); 1169ed19428SPeter Hurley if (port) 117ab4382d2SGreg Kroah-Hartman port->ops->stop_tx(port); 1189ed19428SPeter Hurley uart_port_unlock(port, flags); 119ab4382d2SGreg Kroah-Hartman } 120ab4382d2SGreg Kroah-Hartman 121ab4382d2SGreg Kroah-Hartman static void __uart_start(struct tty_struct *tty) 122ab4382d2SGreg Kroah-Hartman { 123ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 124ab4382d2SGreg Kroah-Hartman struct uart_port *port = state->uart_port; 125ab4382d2SGreg Kroah-Hartman 1269ed19428SPeter Hurley if (port && !uart_tx_stopped(port)) 127ab4382d2SGreg Kroah-Hartman port->ops->start_tx(port); 128ab4382d2SGreg Kroah-Hartman } 129ab4382d2SGreg Kroah-Hartman 130ab4382d2SGreg Kroah-Hartman static void uart_start(struct tty_struct *tty) 131ab4382d2SGreg Kroah-Hartman { 132ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1339ed19428SPeter Hurley struct uart_port *port; 134ab4382d2SGreg Kroah-Hartman unsigned long flags; 135ab4382d2SGreg Kroah-Hartman 1369ed19428SPeter Hurley port = uart_port_lock(state, flags); 137ab4382d2SGreg Kroah-Hartman __uart_start(tty); 1389ed19428SPeter Hurley uart_port_unlock(port, flags); 139ab4382d2SGreg Kroah-Hartman } 140ab4382d2SGreg Kroah-Hartman 141f4581cabSDenys Vlasenko static void 142ab4382d2SGreg Kroah-Hartman uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear) 143ab4382d2SGreg Kroah-Hartman { 144ab4382d2SGreg Kroah-Hartman unsigned long flags; 145ab4382d2SGreg Kroah-Hartman unsigned int old; 146ab4382d2SGreg Kroah-Hartman 147ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 148ab4382d2SGreg Kroah-Hartman old = port->mctrl; 149ab4382d2SGreg Kroah-Hartman port->mctrl = (old & ~clear) | set; 150ab4382d2SGreg Kroah-Hartman if (old != port->mctrl) 151ab4382d2SGreg Kroah-Hartman port->ops->set_mctrl(port, port->mctrl); 152ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 153ab4382d2SGreg Kroah-Hartman } 154ab4382d2SGreg Kroah-Hartman 155ab4382d2SGreg Kroah-Hartman #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0) 156ab4382d2SGreg Kroah-Hartman #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear) 157ab4382d2SGreg Kroah-Hartman 158a6845e1eSRafael Gago static void uart_port_dtr_rts(struct uart_port *uport, int raise) 159a6845e1eSRafael Gago { 160a6845e1eSRafael Gago int rs485_on = uport->rs485_config && 161a6845e1eSRafael Gago (uport->rs485.flags & SER_RS485_ENABLED); 162a6845e1eSRafael Gago int RTS_after_send = !!(uport->rs485.flags & SER_RS485_RTS_AFTER_SEND); 163a6845e1eSRafael Gago 164a6845e1eSRafael Gago if (raise) { 165a6845e1eSRafael Gago if (rs485_on && !RTS_after_send) { 166a6845e1eSRafael Gago uart_set_mctrl(uport, TIOCM_DTR); 167a6845e1eSRafael Gago uart_clear_mctrl(uport, TIOCM_RTS); 168a6845e1eSRafael Gago } else { 169a6845e1eSRafael Gago uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS); 170a6845e1eSRafael Gago } 171a6845e1eSRafael Gago } else { 172a6845e1eSRafael Gago unsigned int clear = TIOCM_DTR; 173a6845e1eSRafael Gago 174a6845e1eSRafael Gago clear |= (!rs485_on || !RTS_after_send) ? TIOCM_RTS : 0; 175a6845e1eSRafael Gago uart_clear_mctrl(uport, clear); 176a6845e1eSRafael Gago } 177a6845e1eSRafael Gago } 178a6845e1eSRafael Gago 179ab4382d2SGreg Kroah-Hartman /* 180ab4382d2SGreg Kroah-Hartman * Startup the port. This will be called once per open. All calls 181ab4382d2SGreg Kroah-Hartman * will be serialised by the per-port mutex. 182ab4382d2SGreg Kroah-Hartman */ 183c0d92be6SJiri Slaby static int uart_port_startup(struct tty_struct *tty, struct uart_state *state, 184c0d92be6SJiri Slaby int init_hw) 185ab4382d2SGreg Kroah-Hartman { 1864047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 187ab4382d2SGreg Kroah-Hartman unsigned long page; 188a5ba1d95STycho Andersen unsigned long flags = 0; 189ab4382d2SGreg Kroah-Hartman int retval = 0; 190ab4382d2SGreg Kroah-Hartman 191ab4382d2SGreg Kroah-Hartman if (uport->type == PORT_UNKNOWN) 192c0d92be6SJiri Slaby return 1; 193ab4382d2SGreg Kroah-Hartman 194ab4382d2SGreg Kroah-Hartman /* 1957deb39edSThomas Pfaff * Make sure the device is in D0 state. 1967deb39edSThomas Pfaff */ 1977deb39edSThomas Pfaff uart_change_pm(state, UART_PM_STATE_ON); 1987deb39edSThomas Pfaff 1997deb39edSThomas Pfaff /* 200ab4382d2SGreg Kroah-Hartman * Initialise and allocate the transmit and temporary 201ab4382d2SGreg Kroah-Hartman * buffer. 202ab4382d2SGreg Kroah-Hartman */ 203ab4382d2SGreg Kroah-Hartman page = get_zeroed_page(GFP_KERNEL); 204ab4382d2SGreg Kroah-Hartman if (!page) 205ab4382d2SGreg Kroah-Hartman return -ENOMEM; 206ab4382d2SGreg Kroah-Hartman 207a5ba1d95STycho Andersen uart_port_lock(state, flags); 208a5ba1d95STycho Andersen if (!state->xmit.buf) { 209ab4382d2SGreg Kroah-Hartman state->xmit.buf = (unsigned char *) page; 210ab4382d2SGreg Kroah-Hartman uart_circ_clear(&state->xmit); 211d7240214SSergey Senozhatsky uart_port_unlock(uport, flags); 212a5ba1d95STycho Andersen } else { 213d7240214SSergey Senozhatsky uart_port_unlock(uport, flags); 214d7240214SSergey Senozhatsky /* 215d7240214SSergey Senozhatsky * Do not free() the page under the port lock, see 216d7240214SSergey Senozhatsky * uart_shutdown(). 217d7240214SSergey Senozhatsky */ 218a5ba1d95STycho Andersen free_page(page); 219ab4382d2SGreg Kroah-Hartman } 220ab4382d2SGreg Kroah-Hartman 221ab4382d2SGreg Kroah-Hartman retval = uport->ops->startup(uport); 222ab4382d2SGreg Kroah-Hartman if (retval == 0) { 223c7d7abffSJiri Slaby if (uart_console(uport) && uport->cons->cflag) { 224adc8d746SAlan Cox tty->termios.c_cflag = uport->cons->cflag; 225c7d7abffSJiri Slaby uport->cons->cflag = 0; 226c7d7abffSJiri Slaby } 227ab4382d2SGreg Kroah-Hartman /* 228ab4382d2SGreg Kroah-Hartman * Initialise the hardware port settings. 229ab4382d2SGreg Kroah-Hartman */ 230ab4382d2SGreg Kroah-Hartman uart_change_speed(tty, state, NULL); 231ab4382d2SGreg Kroah-Hartman 232ab4382d2SGreg Kroah-Hartman /* 233ab4382d2SGreg Kroah-Hartman * Setup the RTS and DTR signals once the 234ab4382d2SGreg Kroah-Hartman * port is open and ready to respond. 235ab4382d2SGreg Kroah-Hartman */ 2369db276f8SPeter Hurley if (init_hw && C_BAUD(tty)) 237a6845e1eSRafael Gago uart_port_dtr_rts(uport, 1); 238ab4382d2SGreg Kroah-Hartman } 239ab4382d2SGreg Kroah-Hartman 2400055197eSJiri Slaby /* 2410055197eSJiri Slaby * This is to allow setserial on this port. People may want to set 2420055197eSJiri Slaby * port/irq/type and then reconfigure the port properly if it failed 2430055197eSJiri Slaby * now. 2440055197eSJiri Slaby */ 245ab4382d2SGreg Kroah-Hartman if (retval && capable(CAP_SYS_ADMIN)) 246c0d92be6SJiri Slaby return 1; 247c0d92be6SJiri Slaby 248c0d92be6SJiri Slaby return retval; 249c0d92be6SJiri Slaby } 250c0d92be6SJiri Slaby 251c0d92be6SJiri Slaby static int uart_startup(struct tty_struct *tty, struct uart_state *state, 252c0d92be6SJiri Slaby int init_hw) 253c0d92be6SJiri Slaby { 254c0d92be6SJiri Slaby struct tty_port *port = &state->port; 255c0d92be6SJiri Slaby int retval; 256c0d92be6SJiri Slaby 257d41861caSPeter Hurley if (tty_port_initialized(port)) 258c0d92be6SJiri Slaby return 0; 259c0d92be6SJiri Slaby 260c0d92be6SJiri Slaby retval = uart_port_startup(tty, state, init_hw); 261b3b57646SRob Herring if (retval) 262b3b57646SRob Herring set_bit(TTY_IO_ERROR, &tty->flags); 263ab4382d2SGreg Kroah-Hartman 264ab4382d2SGreg Kroah-Hartman return retval; 265ab4382d2SGreg Kroah-Hartman } 266ab4382d2SGreg Kroah-Hartman 267ab4382d2SGreg Kroah-Hartman /* 268ab4382d2SGreg Kroah-Hartman * This routine will shutdown a serial port; interrupts are disabled, and 269ab4382d2SGreg Kroah-Hartman * DTR is dropped if the hangup on close termio flag is on. Calls to 270ab4382d2SGreg Kroah-Hartman * uart_shutdown are serialised by the per-port semaphore. 271af224ca2SPeter Hurley * 272af224ca2SPeter Hurley * uport == NULL if uart_port has already been removed 273ab4382d2SGreg Kroah-Hartman */ 274ab4382d2SGreg Kroah-Hartman static void uart_shutdown(struct tty_struct *tty, struct uart_state *state) 275ab4382d2SGreg Kroah-Hartman { 2764047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 277ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 278a5ba1d95STycho Andersen unsigned long flags = 0; 279d7240214SSergey Senozhatsky char *xmit_buf = NULL; 280ab4382d2SGreg Kroah-Hartman 281ab4382d2SGreg Kroah-Hartman /* 282ab4382d2SGreg Kroah-Hartman * Set the TTY IO error marker 283ab4382d2SGreg Kroah-Hartman */ 284ab4382d2SGreg Kroah-Hartman if (tty) 285ab4382d2SGreg Kroah-Hartman set_bit(TTY_IO_ERROR, &tty->flags); 286ab4382d2SGreg Kroah-Hartman 287d41861caSPeter Hurley if (tty_port_initialized(port)) { 288d41861caSPeter Hurley tty_port_set_initialized(port, 0); 289d41861caSPeter Hurley 290ab4382d2SGreg Kroah-Hartman /* 291ab4382d2SGreg Kroah-Hartman * Turn off DTR and RTS early. 292ab4382d2SGreg Kroah-Hartman */ 293af224ca2SPeter Hurley if (uport && uart_console(uport) && tty) 294ae84db96SPeter Hurley uport->cons->cflag = tty->termios.c_cflag; 295ae84db96SPeter Hurley 2969db276f8SPeter Hurley if (!tty || C_HUPCL(tty)) 297a6845e1eSRafael Gago uart_port_dtr_rts(uport, 0); 298ab4382d2SGreg Kroah-Hartman 299b922e19dSJiri Slaby uart_port_shutdown(port); 300ab4382d2SGreg Kroah-Hartman } 301ab4382d2SGreg Kroah-Hartman 302ab4382d2SGreg Kroah-Hartman /* 303d208a3bfSDoug Anderson * It's possible for shutdown to be called after suspend if we get 304d208a3bfSDoug Anderson * a DCD drop (hangup) at just the right time. Clear suspended bit so 305d208a3bfSDoug Anderson * we don't try to resume a port that has been shutdown. 306ab4382d2SGreg Kroah-Hartman */ 30780f02d54SPeter Hurley tty_port_set_suspended(port, 0); 308ab4382d2SGreg Kroah-Hartman 309ab4382d2SGreg Kroah-Hartman /* 310d7240214SSergey Senozhatsky * Do not free() the transmit buffer page under the port lock since 311d7240214SSergey Senozhatsky * this can create various circular locking scenarios. For instance, 312d7240214SSergey Senozhatsky * console driver may need to allocate/free a debug object, which 313d7240214SSergey Senozhatsky * can endup in printk() recursion. 314ab4382d2SGreg Kroah-Hartman */ 315a5ba1d95STycho Andersen uart_port_lock(state, flags); 316d7240214SSergey Senozhatsky xmit_buf = state->xmit.buf; 317ab4382d2SGreg Kroah-Hartman state->xmit.buf = NULL; 318a5ba1d95STycho Andersen uart_port_unlock(uport, flags); 319d7240214SSergey Senozhatsky 320d7240214SSergey Senozhatsky if (xmit_buf) 321d7240214SSergey Senozhatsky free_page((unsigned long)xmit_buf); 322ab4382d2SGreg Kroah-Hartman } 323ab4382d2SGreg Kroah-Hartman 324ab4382d2SGreg Kroah-Hartman /** 325ab4382d2SGreg Kroah-Hartman * uart_update_timeout - update per-port FIFO timeout. 326ab4382d2SGreg Kroah-Hartman * @port: uart_port structure describing the port 327ab4382d2SGreg Kroah-Hartman * @cflag: termios cflag value 328ab4382d2SGreg Kroah-Hartman * @baud: speed of the port 329ab4382d2SGreg Kroah-Hartman * 330ab4382d2SGreg Kroah-Hartman * Set the port FIFO timeout value. The @cflag value should 331ab4382d2SGreg Kroah-Hartman * reflect the actual hardware settings. 332ab4382d2SGreg Kroah-Hartman */ 333ab4382d2SGreg Kroah-Hartman void 334ab4382d2SGreg Kroah-Hartman uart_update_timeout(struct uart_port *port, unsigned int cflag, 335ab4382d2SGreg Kroah-Hartman unsigned int baud) 336ab4382d2SGreg Kroah-Hartman { 337ab4382d2SGreg Kroah-Hartman unsigned int bits; 338ab4382d2SGreg Kroah-Hartman 339ab4382d2SGreg Kroah-Hartman /* byte size and parity */ 340ab4382d2SGreg Kroah-Hartman switch (cflag & CSIZE) { 341ab4382d2SGreg Kroah-Hartman case CS5: 342ab4382d2SGreg Kroah-Hartman bits = 7; 343ab4382d2SGreg Kroah-Hartman break; 344ab4382d2SGreg Kroah-Hartman case CS6: 345ab4382d2SGreg Kroah-Hartman bits = 8; 346ab4382d2SGreg Kroah-Hartman break; 347ab4382d2SGreg Kroah-Hartman case CS7: 348ab4382d2SGreg Kroah-Hartman bits = 9; 349ab4382d2SGreg Kroah-Hartman break; 350ab4382d2SGreg Kroah-Hartman default: 351ab4382d2SGreg Kroah-Hartman bits = 10; 352ab4382d2SGreg Kroah-Hartman break; /* CS8 */ 353ab4382d2SGreg Kroah-Hartman } 354ab4382d2SGreg Kroah-Hartman 355ab4382d2SGreg Kroah-Hartman if (cflag & CSTOPB) 356ab4382d2SGreg Kroah-Hartman bits++; 357ab4382d2SGreg Kroah-Hartman if (cflag & PARENB) 358ab4382d2SGreg Kroah-Hartman bits++; 359ab4382d2SGreg Kroah-Hartman 360ab4382d2SGreg Kroah-Hartman /* 361ab4382d2SGreg Kroah-Hartman * The total number of bits to be transmitted in the fifo. 362ab4382d2SGreg Kroah-Hartman */ 363ab4382d2SGreg Kroah-Hartman bits = bits * port->fifosize; 364ab4382d2SGreg Kroah-Hartman 365ab4382d2SGreg Kroah-Hartman /* 366ab4382d2SGreg Kroah-Hartman * Figure the timeout to send the above number of bits. 367ab4382d2SGreg Kroah-Hartman * Add .02 seconds of slop 368ab4382d2SGreg Kroah-Hartman */ 369ab4382d2SGreg Kroah-Hartman port->timeout = (HZ * bits) / baud + HZ/50; 370ab4382d2SGreg Kroah-Hartman } 371ab4382d2SGreg Kroah-Hartman 372ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_update_timeout); 373ab4382d2SGreg Kroah-Hartman 374ab4382d2SGreg Kroah-Hartman /** 375ab4382d2SGreg Kroah-Hartman * uart_get_baud_rate - return baud rate for a particular port 376ab4382d2SGreg Kroah-Hartman * @port: uart_port structure describing the port in question. 377ab4382d2SGreg Kroah-Hartman * @termios: desired termios settings. 378ab4382d2SGreg Kroah-Hartman * @old: old termios (or NULL) 379ab4382d2SGreg Kroah-Hartman * @min: minimum acceptable baud rate 380ab4382d2SGreg Kroah-Hartman * @max: maximum acceptable baud rate 381ab4382d2SGreg Kroah-Hartman * 382ab4382d2SGreg Kroah-Hartman * Decode the termios structure into a numeric baud rate, 383ab4382d2SGreg Kroah-Hartman * taking account of the magic 38400 baud rate (with spd_* 384ab4382d2SGreg Kroah-Hartman * flags), and mapping the %B0 rate to 9600 baud. 385ab4382d2SGreg Kroah-Hartman * 386ab4382d2SGreg Kroah-Hartman * If the new baud rate is invalid, try the old termios setting. 387ab4382d2SGreg Kroah-Hartman * If it's still invalid, we try 9600 baud. 388ab4382d2SGreg Kroah-Hartman * 389ab4382d2SGreg Kroah-Hartman * Update the @termios structure to reflect the baud rate 390ab4382d2SGreg Kroah-Hartman * we're actually going to be using. Don't do this for the case 391ab4382d2SGreg Kroah-Hartman * where B0 is requested ("hang up"). 392ab4382d2SGreg Kroah-Hartman */ 393ab4382d2SGreg Kroah-Hartman unsigned int 394ab4382d2SGreg Kroah-Hartman uart_get_baud_rate(struct uart_port *port, struct ktermios *termios, 395ab4382d2SGreg Kroah-Hartman struct ktermios *old, unsigned int min, unsigned int max) 396ab4382d2SGreg Kroah-Hartman { 397f10a2233SJoakim Nordell unsigned int try; 398f10a2233SJoakim Nordell unsigned int baud; 399f10a2233SJoakim Nordell unsigned int altbaud; 400ab4382d2SGreg Kroah-Hartman int hung_up = 0; 401ab4382d2SGreg Kroah-Hartman upf_t flags = port->flags & UPF_SPD_MASK; 402ab4382d2SGreg Kroah-Hartman 403f10a2233SJoakim Nordell switch (flags) { 404f10a2233SJoakim Nordell case UPF_SPD_HI: 405ab4382d2SGreg Kroah-Hartman altbaud = 57600; 406f10a2233SJoakim Nordell break; 407f10a2233SJoakim Nordell case UPF_SPD_VHI: 408ab4382d2SGreg Kroah-Hartman altbaud = 115200; 409f10a2233SJoakim Nordell break; 410f10a2233SJoakim Nordell case UPF_SPD_SHI: 411ab4382d2SGreg Kroah-Hartman altbaud = 230400; 412f10a2233SJoakim Nordell break; 413f10a2233SJoakim Nordell case UPF_SPD_WARP: 414ab4382d2SGreg Kroah-Hartman altbaud = 460800; 415f10a2233SJoakim Nordell break; 416f10a2233SJoakim Nordell default: 417f10a2233SJoakim Nordell altbaud = 38400; 418f10a2233SJoakim Nordell break; 419f10a2233SJoakim Nordell } 420ab4382d2SGreg Kroah-Hartman 421ab4382d2SGreg Kroah-Hartman for (try = 0; try < 2; try++) { 422ab4382d2SGreg Kroah-Hartman baud = tty_termios_baud_rate(termios); 423ab4382d2SGreg Kroah-Hartman 424ab4382d2SGreg Kroah-Hartman /* 425ab4382d2SGreg Kroah-Hartman * The spd_hi, spd_vhi, spd_shi, spd_warp kludge... 426ab4382d2SGreg Kroah-Hartman * Die! Die! Die! 427ab4382d2SGreg Kroah-Hartman */ 428547039ecSPeter Hurley if (try == 0 && baud == 38400) 429ab4382d2SGreg Kroah-Hartman baud = altbaud; 430ab4382d2SGreg Kroah-Hartman 431ab4382d2SGreg Kroah-Hartman /* 432ab4382d2SGreg Kroah-Hartman * Special case: B0 rate. 433ab4382d2SGreg Kroah-Hartman */ 434ab4382d2SGreg Kroah-Hartman if (baud == 0) { 435ab4382d2SGreg Kroah-Hartman hung_up = 1; 436ab4382d2SGreg Kroah-Hartman baud = 9600; 437ab4382d2SGreg Kroah-Hartman } 438ab4382d2SGreg Kroah-Hartman 439ab4382d2SGreg Kroah-Hartman if (baud >= min && baud <= max) 440ab4382d2SGreg Kroah-Hartman return baud; 441ab4382d2SGreg Kroah-Hartman 442ab4382d2SGreg Kroah-Hartman /* 443ab4382d2SGreg Kroah-Hartman * Oops, the quotient was zero. Try again with 444ab4382d2SGreg Kroah-Hartman * the old baud rate if possible. 445ab4382d2SGreg Kroah-Hartman */ 446ab4382d2SGreg Kroah-Hartman termios->c_cflag &= ~CBAUD; 447ab4382d2SGreg Kroah-Hartman if (old) { 448ab4382d2SGreg Kroah-Hartman baud = tty_termios_baud_rate(old); 449ab4382d2SGreg Kroah-Hartman if (!hung_up) 450ab4382d2SGreg Kroah-Hartman tty_termios_encode_baud_rate(termios, 451ab4382d2SGreg Kroah-Hartman baud, baud); 452ab4382d2SGreg Kroah-Hartman old = NULL; 453ab4382d2SGreg Kroah-Hartman continue; 454ab4382d2SGreg Kroah-Hartman } 455ab4382d2SGreg Kroah-Hartman 456ab4382d2SGreg Kroah-Hartman /* 457ab4382d2SGreg Kroah-Hartman * As a last resort, if the range cannot be met then clip to 458ab4382d2SGreg Kroah-Hartman * the nearest chip supported rate. 459ab4382d2SGreg Kroah-Hartman */ 460ab4382d2SGreg Kroah-Hartman if (!hung_up) { 461ab4382d2SGreg Kroah-Hartman if (baud <= min) 462ab4382d2SGreg Kroah-Hartman tty_termios_encode_baud_rate(termios, 463ab4382d2SGreg Kroah-Hartman min + 1, min + 1); 464ab4382d2SGreg Kroah-Hartman else 465ab4382d2SGreg Kroah-Hartman tty_termios_encode_baud_rate(termios, 466ab4382d2SGreg Kroah-Hartman max - 1, max - 1); 467ab4382d2SGreg Kroah-Hartman } 468ab4382d2SGreg Kroah-Hartman } 469ab4382d2SGreg Kroah-Hartman /* Should never happen */ 470ab4382d2SGreg Kroah-Hartman WARN_ON(1); 471ab4382d2SGreg Kroah-Hartman return 0; 472ab4382d2SGreg Kroah-Hartman } 473ab4382d2SGreg Kroah-Hartman 474ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_get_baud_rate); 475ab4382d2SGreg Kroah-Hartman 476ab4382d2SGreg Kroah-Hartman /** 477ab4382d2SGreg Kroah-Hartman * uart_get_divisor - return uart clock divisor 478ab4382d2SGreg Kroah-Hartman * @port: uart_port structure describing the port. 479ab4382d2SGreg Kroah-Hartman * @baud: desired baud rate 480ab4382d2SGreg Kroah-Hartman * 481ab4382d2SGreg Kroah-Hartman * Calculate the uart clock divisor for the port. 482ab4382d2SGreg Kroah-Hartman */ 483ab4382d2SGreg Kroah-Hartman unsigned int 484ab4382d2SGreg Kroah-Hartman uart_get_divisor(struct uart_port *port, unsigned int baud) 485ab4382d2SGreg Kroah-Hartman { 486ab4382d2SGreg Kroah-Hartman unsigned int quot; 487ab4382d2SGreg Kroah-Hartman 488ab4382d2SGreg Kroah-Hartman /* 489ab4382d2SGreg Kroah-Hartman * Old custom speed handling. 490ab4382d2SGreg Kroah-Hartman */ 491ab4382d2SGreg Kroah-Hartman if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST) 492ab4382d2SGreg Kroah-Hartman quot = port->custom_divisor; 493ab4382d2SGreg Kroah-Hartman else 49497d24634SUwe Kleine-König quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud); 495ab4382d2SGreg Kroah-Hartman 496ab4382d2SGreg Kroah-Hartman return quot; 497ab4382d2SGreg Kroah-Hartman } 498ab4382d2SGreg Kroah-Hartman 499ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_get_divisor); 500ab4382d2SGreg Kroah-Hartman 5017c8ab967SPeter Hurley /* Caller holds port mutex */ 502ab4382d2SGreg Kroah-Hartman static void uart_change_speed(struct tty_struct *tty, struct uart_state *state, 503ab4382d2SGreg Kroah-Hartman struct ktermios *old_termios) 504ab4382d2SGreg Kroah-Hartman { 5054047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 506ab4382d2SGreg Kroah-Hartman struct ktermios *termios; 507391f93f2SPeter Hurley int hw_stopped; 508ab4382d2SGreg Kroah-Hartman 509ab4382d2SGreg Kroah-Hartman /* 510ab4382d2SGreg Kroah-Hartman * If we have no tty, termios, or the port does not exist, 511ab4382d2SGreg Kroah-Hartman * then we can't set the parameters for this port. 512ab4382d2SGreg Kroah-Hartman */ 513adc8d746SAlan Cox if (!tty || uport->type == PORT_UNKNOWN) 514ab4382d2SGreg Kroah-Hartman return; 515ab4382d2SGreg Kroah-Hartman 516adc8d746SAlan Cox termios = &tty->termios; 517c18b55fdSPeter Hurley uport->ops->set_termios(uport, termios, old_termios); 518ab4382d2SGreg Kroah-Hartman 519ab4382d2SGreg Kroah-Hartman /* 520299245a1SPeter Hurley * Set modem status enables based on termios cflag 521ab4382d2SGreg Kroah-Hartman */ 522299245a1SPeter Hurley spin_lock_irq(&uport->lock); 523ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CRTSCTS) 524299245a1SPeter Hurley uport->status |= UPSTAT_CTS_ENABLE; 525ab4382d2SGreg Kroah-Hartman else 526299245a1SPeter Hurley uport->status &= ~UPSTAT_CTS_ENABLE; 527ab4382d2SGreg Kroah-Hartman 528ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CLOCAL) 529299245a1SPeter Hurley uport->status &= ~UPSTAT_DCD_ENABLE; 530ab4382d2SGreg Kroah-Hartman else 531299245a1SPeter Hurley uport->status |= UPSTAT_DCD_ENABLE; 532391f93f2SPeter Hurley 533391f93f2SPeter Hurley /* reset sw-assisted CTS flow control based on (possibly) new mode */ 534391f93f2SPeter Hurley hw_stopped = uport->hw_stopped; 535391f93f2SPeter Hurley uport->hw_stopped = uart_softcts_mode(uport) && 536391f93f2SPeter Hurley !(uport->ops->get_mctrl(uport) & TIOCM_CTS); 537391f93f2SPeter Hurley if (uport->hw_stopped) { 538391f93f2SPeter Hurley if (!hw_stopped) 539391f93f2SPeter Hurley uport->ops->stop_tx(uport); 540391f93f2SPeter Hurley } else { 541391f93f2SPeter Hurley if (hw_stopped) 542391f93f2SPeter Hurley __uart_start(tty); 543391f93f2SPeter Hurley } 544299245a1SPeter Hurley spin_unlock_irq(&uport->lock); 545ab4382d2SGreg Kroah-Hartman } 546ab4382d2SGreg Kroah-Hartman 547f5291eccSPeter Hurley static int uart_put_char(struct tty_struct *tty, unsigned char c) 548ab4382d2SGreg Kroah-Hartman { 549f5291eccSPeter Hurley struct uart_state *state = tty->driver_data; 5509ed19428SPeter Hurley struct uart_port *port; 551f5291eccSPeter Hurley struct circ_buf *circ; 552ab4382d2SGreg Kroah-Hartman unsigned long flags; 553ab4382d2SGreg Kroah-Hartman int ret = 0; 554ab4382d2SGreg Kroah-Hartman 555f5291eccSPeter Hurley circ = &state->xmit; 5569ed19428SPeter Hurley port = uart_port_lock(state, flags); 557aff9cf59SSamir Virmani if (!circ->buf) { 558aff9cf59SSamir Virmani uart_port_unlock(port, flags); 559aff9cf59SSamir Virmani return 0; 560aff9cf59SSamir Virmani } 561aff9cf59SSamir Virmani 5629ed19428SPeter Hurley if (port && uart_circ_chars_free(circ) != 0) { 563ab4382d2SGreg Kroah-Hartman circ->buf[circ->head] = c; 564ab4382d2SGreg Kroah-Hartman circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1); 565ab4382d2SGreg Kroah-Hartman ret = 1; 566ab4382d2SGreg Kroah-Hartman } 5679ed19428SPeter Hurley uart_port_unlock(port, flags); 568ab4382d2SGreg Kroah-Hartman return ret; 569ab4382d2SGreg Kroah-Hartman } 570ab4382d2SGreg Kroah-Hartman 571ab4382d2SGreg Kroah-Hartman static void uart_flush_chars(struct tty_struct *tty) 572ab4382d2SGreg Kroah-Hartman { 573ab4382d2SGreg Kroah-Hartman uart_start(tty); 574ab4382d2SGreg Kroah-Hartman } 575ab4382d2SGreg Kroah-Hartman 576ab4382d2SGreg Kroah-Hartman static int uart_write(struct tty_struct *tty, 577ab4382d2SGreg Kroah-Hartman const unsigned char *buf, int count) 578ab4382d2SGreg Kroah-Hartman { 579ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 580ab4382d2SGreg Kroah-Hartman struct uart_port *port; 581ab4382d2SGreg Kroah-Hartman struct circ_buf *circ; 582ab4382d2SGreg Kroah-Hartman unsigned long flags; 583ab4382d2SGreg Kroah-Hartman int c, ret = 0; 584ab4382d2SGreg Kroah-Hartman 585ab4382d2SGreg Kroah-Hartman /* 586ab4382d2SGreg Kroah-Hartman * This means you called this function _after_ the port was 587ab4382d2SGreg Kroah-Hartman * closed. No cookie for you. 588ab4382d2SGreg Kroah-Hartman */ 589ab4382d2SGreg Kroah-Hartman if (!state) { 590ab4382d2SGreg Kroah-Hartman WARN_ON(1); 591ab4382d2SGreg Kroah-Hartman return -EL3HLT; 592ab4382d2SGreg Kroah-Hartman } 593ab4382d2SGreg Kroah-Hartman 5949ed19428SPeter Hurley port = uart_port_lock(state, flags); 595aff9cf59SSamir Virmani circ = &state->xmit; 596aff9cf59SSamir Virmani if (!circ->buf) { 597aff9cf59SSamir Virmani uart_port_unlock(port, flags); 598aff9cf59SSamir Virmani return 0; 599aff9cf59SSamir Virmani } 600aff9cf59SSamir Virmani 6019ed19428SPeter Hurley while (port) { 602ab4382d2SGreg Kroah-Hartman c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE); 603ab4382d2SGreg Kroah-Hartman if (count < c) 604ab4382d2SGreg Kroah-Hartman c = count; 605ab4382d2SGreg Kroah-Hartman if (c <= 0) 606ab4382d2SGreg Kroah-Hartman break; 607ab4382d2SGreg Kroah-Hartman memcpy(circ->buf + circ->head, buf, c); 608ab4382d2SGreg Kroah-Hartman circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1); 609ab4382d2SGreg Kroah-Hartman buf += c; 610ab4382d2SGreg Kroah-Hartman count -= c; 611ab4382d2SGreg Kroah-Hartman ret += c; 612ab4382d2SGreg Kroah-Hartman } 61364dbee31SPeter Hurley 61464dbee31SPeter Hurley __uart_start(tty); 6159ed19428SPeter Hurley uart_port_unlock(port, flags); 616ab4382d2SGreg Kroah-Hartman return ret; 617ab4382d2SGreg Kroah-Hartman } 618ab4382d2SGreg Kroah-Hartman 619ab4382d2SGreg Kroah-Hartman static int uart_write_room(struct tty_struct *tty) 620ab4382d2SGreg Kroah-Hartman { 621ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 6229ed19428SPeter Hurley struct uart_port *port; 623ab4382d2SGreg Kroah-Hartman unsigned long flags; 624ab4382d2SGreg Kroah-Hartman int ret; 625ab4382d2SGreg Kroah-Hartman 6269ed19428SPeter Hurley port = uart_port_lock(state, flags); 627ab4382d2SGreg Kroah-Hartman ret = uart_circ_chars_free(&state->xmit); 6289ed19428SPeter Hurley uart_port_unlock(port, flags); 629ab4382d2SGreg Kroah-Hartman return ret; 630ab4382d2SGreg Kroah-Hartman } 631ab4382d2SGreg Kroah-Hartman 632ab4382d2SGreg Kroah-Hartman static int uart_chars_in_buffer(struct tty_struct *tty) 633ab4382d2SGreg Kroah-Hartman { 634ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 6359ed19428SPeter Hurley struct uart_port *port; 636ab4382d2SGreg Kroah-Hartman unsigned long flags; 637ab4382d2SGreg Kroah-Hartman int ret; 638ab4382d2SGreg Kroah-Hartman 6399ed19428SPeter Hurley port = uart_port_lock(state, flags); 640ab4382d2SGreg Kroah-Hartman ret = uart_circ_chars_pending(&state->xmit); 6419ed19428SPeter Hurley uart_port_unlock(port, flags); 642ab4382d2SGreg Kroah-Hartman return ret; 643ab4382d2SGreg Kroah-Hartman } 644ab4382d2SGreg Kroah-Hartman 645ab4382d2SGreg Kroah-Hartman static void uart_flush_buffer(struct tty_struct *tty) 646ab4382d2SGreg Kroah-Hartman { 647ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 648ab4382d2SGreg Kroah-Hartman struct uart_port *port; 649ab4382d2SGreg Kroah-Hartman unsigned long flags; 650ab4382d2SGreg Kroah-Hartman 651ab4382d2SGreg Kroah-Hartman /* 652ab4382d2SGreg Kroah-Hartman * This means you called this function _after_ the port was 653ab4382d2SGreg Kroah-Hartman * closed. No cookie for you. 654ab4382d2SGreg Kroah-Hartman */ 655ab4382d2SGreg Kroah-Hartman if (!state) { 656ab4382d2SGreg Kroah-Hartman WARN_ON(1); 657ab4382d2SGreg Kroah-Hartman return; 658ab4382d2SGreg Kroah-Hartman } 659ab4382d2SGreg Kroah-Hartman 660ab4382d2SGreg Kroah-Hartman pr_debug("uart_flush_buffer(%d) called\n", tty->index); 661ab4382d2SGreg Kroah-Hartman 6629ed19428SPeter Hurley port = uart_port_lock(state, flags); 6639ed19428SPeter Hurley if (!port) 6649ed19428SPeter Hurley return; 665ab4382d2SGreg Kroah-Hartman uart_circ_clear(&state->xmit); 666ab4382d2SGreg Kroah-Hartman if (port->ops->flush_buffer) 667ab4382d2SGreg Kroah-Hartman port->ops->flush_buffer(port); 6689ed19428SPeter Hurley uart_port_unlock(port, flags); 669d0f4bce2SRob Herring tty_port_tty_wakeup(&state->port); 670ab4382d2SGreg Kroah-Hartman } 671ab4382d2SGreg Kroah-Hartman 672ab4382d2SGreg Kroah-Hartman /* 673ab4382d2SGreg Kroah-Hartman * This function is used to send a high-priority XON/XOFF character to 674ab4382d2SGreg Kroah-Hartman * the device 675ab4382d2SGreg Kroah-Hartman */ 676ab4382d2SGreg Kroah-Hartman static void uart_send_xchar(struct tty_struct *tty, char ch) 677ab4382d2SGreg Kroah-Hartman { 678ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 6799ed19428SPeter Hurley struct uart_port *port; 680ab4382d2SGreg Kroah-Hartman unsigned long flags; 681ab4382d2SGreg Kroah-Hartman 6829ed19428SPeter Hurley port = uart_port_ref(state); 6839ed19428SPeter Hurley if (!port) 6849ed19428SPeter Hurley return; 6859ed19428SPeter Hurley 686ab4382d2SGreg Kroah-Hartman if (port->ops->send_xchar) 687ab4382d2SGreg Kroah-Hartman port->ops->send_xchar(port, ch); 688ab4382d2SGreg Kroah-Hartman else { 689ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 690c235ccc1SPeter Hurley port->x_char = ch; 691c235ccc1SPeter Hurley if (ch) 692ab4382d2SGreg Kroah-Hartman port->ops->start_tx(port); 693ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 694ab4382d2SGreg Kroah-Hartman } 6959ed19428SPeter Hurley uart_port_deref(port); 696ab4382d2SGreg Kroah-Hartman } 697ab4382d2SGreg Kroah-Hartman 698ab4382d2SGreg Kroah-Hartman static void uart_throttle(struct tty_struct *tty) 699ab4382d2SGreg Kroah-Hartman { 700ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 701c5f78b1fSJeremy Kerr upstat_t mask = UPSTAT_SYNC_FIFO; 7029ed19428SPeter Hurley struct uart_port *port; 703ab4382d2SGreg Kroah-Hartman 7049ed19428SPeter Hurley port = uart_port_ref(state); 7059ed19428SPeter Hurley if (!port) 7069ed19428SPeter Hurley return; 7079ed19428SPeter Hurley 708ab4382d2SGreg Kroah-Hartman if (I_IXOFF(tty)) 709391f93f2SPeter Hurley mask |= UPSTAT_AUTOXOFF; 7109db276f8SPeter Hurley if (C_CRTSCTS(tty)) 711391f93f2SPeter Hurley mask |= UPSTAT_AUTORTS; 7129aba8d5bSRussell King 713391f93f2SPeter Hurley if (port->status & mask) { 7149aba8d5bSRussell King port->ops->throttle(port); 715391f93f2SPeter Hurley mask &= ~port->status; 7169aba8d5bSRussell King } 7179aba8d5bSRussell King 718391f93f2SPeter Hurley if (mask & UPSTAT_AUTORTS) 7199aba8d5bSRussell King uart_clear_mctrl(port, TIOCM_RTS); 720b4749b97SPeter Hurley 721b4749b97SPeter Hurley if (mask & UPSTAT_AUTOXOFF) 722b4749b97SPeter Hurley uart_send_xchar(tty, STOP_CHAR(tty)); 7239ed19428SPeter Hurley 7249ed19428SPeter Hurley uart_port_deref(port); 725ab4382d2SGreg Kroah-Hartman } 726ab4382d2SGreg Kroah-Hartman 727ab4382d2SGreg Kroah-Hartman static void uart_unthrottle(struct tty_struct *tty) 728ab4382d2SGreg Kroah-Hartman { 729ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 730c5f78b1fSJeremy Kerr upstat_t mask = UPSTAT_SYNC_FIFO; 7319ed19428SPeter Hurley struct uart_port *port; 732ab4382d2SGreg Kroah-Hartman 7339ed19428SPeter Hurley port = uart_port_ref(state); 7349ed19428SPeter Hurley if (!port) 7359ed19428SPeter Hurley return; 7369ed19428SPeter Hurley 7379aba8d5bSRussell King if (I_IXOFF(tty)) 738391f93f2SPeter Hurley mask |= UPSTAT_AUTOXOFF; 7399db276f8SPeter Hurley if (C_CRTSCTS(tty)) 740391f93f2SPeter Hurley mask |= UPSTAT_AUTORTS; 7419aba8d5bSRussell King 742391f93f2SPeter Hurley if (port->status & mask) { 7439aba8d5bSRussell King port->ops->unthrottle(port); 744391f93f2SPeter Hurley mask &= ~port->status; 7459aba8d5bSRussell King } 7469aba8d5bSRussell King 747391f93f2SPeter Hurley if (mask & UPSTAT_AUTORTS) 748ab4382d2SGreg Kroah-Hartman uart_set_mctrl(port, TIOCM_RTS); 749b4749b97SPeter Hurley 750b4749b97SPeter Hurley if (mask & UPSTAT_AUTOXOFF) 751b4749b97SPeter Hurley uart_send_xchar(tty, START_CHAR(tty)); 7529ed19428SPeter Hurley 7539ed19428SPeter Hurley uart_port_deref(port); 754ab4382d2SGreg Kroah-Hartman } 755ab4382d2SGreg Kroah-Hartman 7564047b371SPeter Hurley static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo) 757ab4382d2SGreg Kroah-Hartman { 7589f109694SAlan Cox struct uart_state *state = container_of(port, struct uart_state, port); 7594047b371SPeter Hurley struct uart_port *uport; 7604047b371SPeter Hurley int ret = -ENODEV; 7617ba2e769SAlan Cox 76237cd0c99SFengguang Wu memset(retinfo, 0, sizeof(*retinfo)); 7637ba2e769SAlan Cox 7643abe8c76SPeter Hurley /* 7653abe8c76SPeter Hurley * Ensure the state we copy is consistent and no hardware changes 7663abe8c76SPeter Hurley * occur as we go 7673abe8c76SPeter Hurley */ 7683abe8c76SPeter Hurley mutex_lock(&port->mutex); 7694047b371SPeter Hurley uport = uart_port_check(state); 7704047b371SPeter Hurley if (!uport) 7714047b371SPeter Hurley goto out; 7724047b371SPeter Hurley 7737ba2e769SAlan Cox retinfo->type = uport->type; 7747ba2e769SAlan Cox retinfo->line = uport->line; 7757ba2e769SAlan Cox retinfo->port = uport->iobase; 7767ba2e769SAlan Cox if (HIGH_BITS_OFFSET) 7777ba2e769SAlan Cox retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET; 7787ba2e769SAlan Cox retinfo->irq = uport->irq; 779a17e74c5SAndy Shevchenko retinfo->flags = (__force int)uport->flags; 7807ba2e769SAlan Cox retinfo->xmit_fifo_size = uport->fifosize; 7817ba2e769SAlan Cox retinfo->baud_base = uport->uartclk / 16; 7827ba2e769SAlan Cox retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10; 7837ba2e769SAlan Cox retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ? 7847ba2e769SAlan Cox ASYNC_CLOSING_WAIT_NONE : 7857ba2e769SAlan Cox jiffies_to_msecs(port->closing_wait) / 10; 7867ba2e769SAlan Cox retinfo->custom_divisor = uport->custom_divisor; 7877ba2e769SAlan Cox retinfo->hub6 = uport->hub6; 7887ba2e769SAlan Cox retinfo->io_type = uport->iotype; 7897ba2e769SAlan Cox retinfo->iomem_reg_shift = uport->regshift; 7907ba2e769SAlan Cox retinfo->iomem_base = (void *)(unsigned long)uport->mapbase; 7914047b371SPeter Hurley 7924047b371SPeter Hurley ret = 0; 7934047b371SPeter Hurley out: 794ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 7954047b371SPeter Hurley return ret; 7969f109694SAlan Cox } 7979f109694SAlan Cox 7985099d234SAl Viro static int uart_get_info_user(struct tty_struct *tty, 7995099d234SAl Viro struct serial_struct *ss) 8009f109694SAlan Cox { 8015099d234SAl Viro struct uart_state *state = tty->driver_data; 8025099d234SAl Viro struct tty_port *port = &state->port; 8033abe8c76SPeter Hurley 8045099d234SAl Viro return uart_get_info(port, ss) < 0 ? -EIO : 0; 805ab4382d2SGreg Kroah-Hartman } 806ab4382d2SGreg Kroah-Hartman 8077ba2e769SAlan Cox static int uart_set_info(struct tty_struct *tty, struct tty_port *port, 8087ba2e769SAlan Cox struct uart_state *state, 8097ba2e769SAlan Cox struct serial_struct *new_info) 810ab4382d2SGreg Kroah-Hartman { 8114047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 812ab4382d2SGreg Kroah-Hartman unsigned long new_port; 813ab4382d2SGreg Kroah-Hartman unsigned int change_irq, change_port, closing_wait; 814ab4382d2SGreg Kroah-Hartman unsigned int old_custom_divisor, close_delay; 815ab4382d2SGreg Kroah-Hartman upf_t old_flags, new_flags; 816ab4382d2SGreg Kroah-Hartman int retval = 0; 817ab4382d2SGreg Kroah-Hartman 8184047b371SPeter Hurley if (!uport) 8194047b371SPeter Hurley return -EIO; 8204047b371SPeter Hurley 8217ba2e769SAlan Cox new_port = new_info->port; 822ab4382d2SGreg Kroah-Hartman if (HIGH_BITS_OFFSET) 8237ba2e769SAlan Cox new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET; 824ab4382d2SGreg Kroah-Hartman 8257ba2e769SAlan Cox new_info->irq = irq_canonicalize(new_info->irq); 8267ba2e769SAlan Cox close_delay = msecs_to_jiffies(new_info->close_delay * 10); 8277ba2e769SAlan Cox closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ? 8284cb0fbfdSJiri Slaby ASYNC_CLOSING_WAIT_NONE : 8297ba2e769SAlan Cox msecs_to_jiffies(new_info->closing_wait * 10); 830ab4382d2SGreg Kroah-Hartman 831ab4382d2SGreg Kroah-Hartman 832ab4382d2SGreg Kroah-Hartman change_irq = !(uport->flags & UPF_FIXED_PORT) 8337ba2e769SAlan Cox && new_info->irq != uport->irq; 834ab4382d2SGreg Kroah-Hartman 835ab4382d2SGreg Kroah-Hartman /* 836ab4382d2SGreg Kroah-Hartman * Since changing the 'type' of the port changes its resource 837ab4382d2SGreg Kroah-Hartman * allocations, we should treat type changes the same as 838ab4382d2SGreg Kroah-Hartman * IO port changes. 839ab4382d2SGreg Kroah-Hartman */ 840ab4382d2SGreg Kroah-Hartman change_port = !(uport->flags & UPF_FIXED_PORT) 841ab4382d2SGreg Kroah-Hartman && (new_port != uport->iobase || 8427ba2e769SAlan Cox (unsigned long)new_info->iomem_base != uport->mapbase || 8437ba2e769SAlan Cox new_info->hub6 != uport->hub6 || 8447ba2e769SAlan Cox new_info->io_type != uport->iotype || 8457ba2e769SAlan Cox new_info->iomem_reg_shift != uport->regshift || 8467ba2e769SAlan Cox new_info->type != uport->type); 847ab4382d2SGreg Kroah-Hartman 848ab4382d2SGreg Kroah-Hartman old_flags = uport->flags; 849a17e74c5SAndy Shevchenko new_flags = (__force upf_t)new_info->flags; 850ab4382d2SGreg Kroah-Hartman old_custom_divisor = uport->custom_divisor; 851ab4382d2SGreg Kroah-Hartman 852ab4382d2SGreg Kroah-Hartman if (!capable(CAP_SYS_ADMIN)) { 853ab4382d2SGreg Kroah-Hartman retval = -EPERM; 854ab4382d2SGreg Kroah-Hartman if (change_irq || change_port || 8557ba2e769SAlan Cox (new_info->baud_base != uport->uartclk / 16) || 856ab4382d2SGreg Kroah-Hartman (close_delay != port->close_delay) || 857ab4382d2SGreg Kroah-Hartman (closing_wait != port->closing_wait) || 8587ba2e769SAlan Cox (new_info->xmit_fifo_size && 8597ba2e769SAlan Cox new_info->xmit_fifo_size != uport->fifosize) || 860ab4382d2SGreg Kroah-Hartman (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0)) 861ab4382d2SGreg Kroah-Hartman goto exit; 862ab4382d2SGreg Kroah-Hartman uport->flags = ((uport->flags & ~UPF_USR_MASK) | 863ab4382d2SGreg Kroah-Hartman (new_flags & UPF_USR_MASK)); 8647ba2e769SAlan Cox uport->custom_divisor = new_info->custom_divisor; 865ab4382d2SGreg Kroah-Hartman goto check_and_exit; 866ab4382d2SGreg Kroah-Hartman } 867ab4382d2SGreg Kroah-Hartman 868794edf30SDavid Howells retval = security_locked_down(LOCKDOWN_TIOCSSERIAL); 869794edf30SDavid Howells if (retval && (change_irq || change_port)) 870794edf30SDavid Howells goto exit; 871794edf30SDavid Howells 872ab4382d2SGreg Kroah-Hartman /* 873ab4382d2SGreg Kroah-Hartman * Ask the low level driver to verify the settings. 874ab4382d2SGreg Kroah-Hartman */ 875ab4382d2SGreg Kroah-Hartman if (uport->ops->verify_port) 8767ba2e769SAlan Cox retval = uport->ops->verify_port(uport, new_info); 877ab4382d2SGreg Kroah-Hartman 8787ba2e769SAlan Cox if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) || 8797ba2e769SAlan Cox (new_info->baud_base < 9600)) 880ab4382d2SGreg Kroah-Hartman retval = -EINVAL; 881ab4382d2SGreg Kroah-Hartman 882ab4382d2SGreg Kroah-Hartman if (retval) 883ab4382d2SGreg Kroah-Hartman goto exit; 884ab4382d2SGreg Kroah-Hartman 885ab4382d2SGreg Kroah-Hartman if (change_port || change_irq) { 886ab4382d2SGreg Kroah-Hartman retval = -EBUSY; 887ab4382d2SGreg Kroah-Hartman 888ab4382d2SGreg Kroah-Hartman /* 889ab4382d2SGreg Kroah-Hartman * Make sure that we are the sole user of this port. 890ab4382d2SGreg Kroah-Hartman */ 891ab4382d2SGreg Kroah-Hartman if (tty_port_users(port) > 1) 892ab4382d2SGreg Kroah-Hartman goto exit; 893ab4382d2SGreg Kroah-Hartman 894ab4382d2SGreg Kroah-Hartman /* 895ab4382d2SGreg Kroah-Hartman * We need to shutdown the serial port at the old 896ab4382d2SGreg Kroah-Hartman * port/type/irq combination. 897ab4382d2SGreg Kroah-Hartman */ 898ab4382d2SGreg Kroah-Hartman uart_shutdown(tty, state); 899ab4382d2SGreg Kroah-Hartman } 900ab4382d2SGreg Kroah-Hartman 901ab4382d2SGreg Kroah-Hartman if (change_port) { 902ab4382d2SGreg Kroah-Hartman unsigned long old_iobase, old_mapbase; 903ab4382d2SGreg Kroah-Hartman unsigned int old_type, old_iotype, old_hub6, old_shift; 904ab4382d2SGreg Kroah-Hartman 905ab4382d2SGreg Kroah-Hartman old_iobase = uport->iobase; 906ab4382d2SGreg Kroah-Hartman old_mapbase = uport->mapbase; 907ab4382d2SGreg Kroah-Hartman old_type = uport->type; 908ab4382d2SGreg Kroah-Hartman old_hub6 = uport->hub6; 909ab4382d2SGreg Kroah-Hartman old_iotype = uport->iotype; 910ab4382d2SGreg Kroah-Hartman old_shift = uport->regshift; 911ab4382d2SGreg Kroah-Hartman 912ab4382d2SGreg Kroah-Hartman /* 913ab4382d2SGreg Kroah-Hartman * Free and release old regions 914ab4382d2SGreg Kroah-Hartman */ 915a7cfaf16SFabio Estevam if (old_type != PORT_UNKNOWN && uport->ops->release_port) 916ab4382d2SGreg Kroah-Hartman uport->ops->release_port(uport); 917ab4382d2SGreg Kroah-Hartman 918ab4382d2SGreg Kroah-Hartman uport->iobase = new_port; 9197ba2e769SAlan Cox uport->type = new_info->type; 9207ba2e769SAlan Cox uport->hub6 = new_info->hub6; 9217ba2e769SAlan Cox uport->iotype = new_info->io_type; 9227ba2e769SAlan Cox uport->regshift = new_info->iomem_reg_shift; 9237ba2e769SAlan Cox uport->mapbase = (unsigned long)new_info->iomem_base; 924ab4382d2SGreg Kroah-Hartman 925ab4382d2SGreg Kroah-Hartman /* 926ab4382d2SGreg Kroah-Hartman * Claim and map the new regions 927ab4382d2SGreg Kroah-Hartman */ 928a7cfaf16SFabio Estevam if (uport->type != PORT_UNKNOWN && uport->ops->request_port) { 929ab4382d2SGreg Kroah-Hartman retval = uport->ops->request_port(uport); 930ab4382d2SGreg Kroah-Hartman } else { 931ab4382d2SGreg Kroah-Hartman /* Always success - Jean II */ 932ab4382d2SGreg Kroah-Hartman retval = 0; 933ab4382d2SGreg Kroah-Hartman } 934ab4382d2SGreg Kroah-Hartman 935ab4382d2SGreg Kroah-Hartman /* 936ab4382d2SGreg Kroah-Hartman * If we fail to request resources for the 937ab4382d2SGreg Kroah-Hartman * new port, try to restore the old settings. 938ab4382d2SGreg Kroah-Hartman */ 9397deb39edSThomas Pfaff if (retval) { 940ab4382d2SGreg Kroah-Hartman uport->iobase = old_iobase; 941ab4382d2SGreg Kroah-Hartman uport->type = old_type; 942ab4382d2SGreg Kroah-Hartman uport->hub6 = old_hub6; 943ab4382d2SGreg Kroah-Hartman uport->iotype = old_iotype; 944ab4382d2SGreg Kroah-Hartman uport->regshift = old_shift; 945ab4382d2SGreg Kroah-Hartman uport->mapbase = old_mapbase; 9467deb39edSThomas Pfaff 9477deb39edSThomas Pfaff if (old_type != PORT_UNKNOWN) { 948ab4382d2SGreg Kroah-Hartman retval = uport->ops->request_port(uport); 949ab4382d2SGreg Kroah-Hartman /* 950ab4382d2SGreg Kroah-Hartman * If we failed to restore the old settings, 951ab4382d2SGreg Kroah-Hartman * we fail like this. 952ab4382d2SGreg Kroah-Hartman */ 953ab4382d2SGreg Kroah-Hartman if (retval) 954ab4382d2SGreg Kroah-Hartman uport->type = PORT_UNKNOWN; 955ab4382d2SGreg Kroah-Hartman 956ab4382d2SGreg Kroah-Hartman /* 957ab4382d2SGreg Kroah-Hartman * We failed anyway. 958ab4382d2SGreg Kroah-Hartman */ 959ab4382d2SGreg Kroah-Hartman retval = -EBUSY; 9607deb39edSThomas Pfaff } 9617deb39edSThomas Pfaff 962ab4382d2SGreg Kroah-Hartman /* Added to return the correct error -Ram Gupta */ 963ab4382d2SGreg Kroah-Hartman goto exit; 964ab4382d2SGreg Kroah-Hartman } 965ab4382d2SGreg Kroah-Hartman } 966ab4382d2SGreg Kroah-Hartman 967ab4382d2SGreg Kroah-Hartman if (change_irq) 9687ba2e769SAlan Cox uport->irq = new_info->irq; 969ab4382d2SGreg Kroah-Hartman if (!(uport->flags & UPF_FIXED_PORT)) 9707ba2e769SAlan Cox uport->uartclk = new_info->baud_base * 16; 971ab4382d2SGreg Kroah-Hartman uport->flags = (uport->flags & ~UPF_CHANGE_MASK) | 972ab4382d2SGreg Kroah-Hartman (new_flags & UPF_CHANGE_MASK); 9737ba2e769SAlan Cox uport->custom_divisor = new_info->custom_divisor; 974ab4382d2SGreg Kroah-Hartman port->close_delay = close_delay; 975ab4382d2SGreg Kroah-Hartman port->closing_wait = closing_wait; 9767ba2e769SAlan Cox if (new_info->xmit_fifo_size) 9777ba2e769SAlan Cox uport->fifosize = new_info->xmit_fifo_size; 978d6c53c0eSJiri Slaby port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0; 979ab4382d2SGreg Kroah-Hartman 980ab4382d2SGreg Kroah-Hartman check_and_exit: 981ab4382d2SGreg Kroah-Hartman retval = 0; 982ab4382d2SGreg Kroah-Hartman if (uport->type == PORT_UNKNOWN) 983ab4382d2SGreg Kroah-Hartman goto exit; 984d41861caSPeter Hurley if (tty_port_initialized(port)) { 985ab4382d2SGreg Kroah-Hartman if (((old_flags ^ uport->flags) & UPF_SPD_MASK) || 986ab4382d2SGreg Kroah-Hartman old_custom_divisor != uport->custom_divisor) { 987ab4382d2SGreg Kroah-Hartman /* 988ab4382d2SGreg Kroah-Hartman * If they're setting up a custom divisor or speed, 989dc2f7271SJohan Hovold * instead of clearing it, then bitch about it. 990ab4382d2SGreg Kroah-Hartman */ 991ab4382d2SGreg Kroah-Hartman if (uport->flags & UPF_SPD_MASK) { 992dc2f7271SJohan Hovold dev_notice_ratelimited(uport->dev, 9932f2dafe7SSudip Mukherjee "%s sets custom speed on %s. This is deprecated.\n", 9942f2dafe7SSudip Mukherjee current->comm, 995429b4749SRasmus Villemoes tty_name(port->tty)); 996ab4382d2SGreg Kroah-Hartman } 997ab4382d2SGreg Kroah-Hartman uart_change_speed(tty, state, NULL); 998ab4382d2SGreg Kroah-Hartman } 999b3b57646SRob Herring } else { 1000ab4382d2SGreg Kroah-Hartman retval = uart_startup(tty, state, 1); 100144117a1dSSebastian Andrzej Siewior if (retval == 0) 100244117a1dSSebastian Andrzej Siewior tty_port_set_initialized(port, true); 1003b3b57646SRob Herring if (retval > 0) 1004b3b57646SRob Herring retval = 0; 1005b3b57646SRob Herring } 1006ab4382d2SGreg Kroah-Hartman exit: 10077ba2e769SAlan Cox return retval; 10087ba2e769SAlan Cox } 10097ba2e769SAlan Cox 10105099d234SAl Viro static int uart_set_info_user(struct tty_struct *tty, struct serial_struct *ss) 10117ba2e769SAlan Cox { 10125099d234SAl Viro struct uart_state *state = tty->driver_data; 10137ba2e769SAlan Cox struct tty_port *port = &state->port; 10147ba2e769SAlan Cox int retval; 10157ba2e769SAlan Cox 10165099d234SAl Viro down_write(&tty->termios_rwsem); 10177ba2e769SAlan Cox /* 10187ba2e769SAlan Cox * This semaphore protects port->count. It is also 10197ba2e769SAlan Cox * very useful to prevent opens. Also, take the 10207ba2e769SAlan Cox * port configuration semaphore to make sure that a 10217ba2e769SAlan Cox * module insertion/removal doesn't change anything 10227ba2e769SAlan Cox * under us. 10237ba2e769SAlan Cox */ 10247ba2e769SAlan Cox mutex_lock(&port->mutex); 10255099d234SAl Viro retval = uart_set_info(tty, port, state, ss); 1026ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 10275099d234SAl Viro up_write(&tty->termios_rwsem); 1028ab4382d2SGreg Kroah-Hartman return retval; 1029ab4382d2SGreg Kroah-Hartman } 1030ab4382d2SGreg Kroah-Hartman 1031ab4382d2SGreg Kroah-Hartman /** 1032ab4382d2SGreg Kroah-Hartman * uart_get_lsr_info - get line status register info 1033ab4382d2SGreg Kroah-Hartman * @tty: tty associated with the UART 1034ab4382d2SGreg Kroah-Hartman * @state: UART being queried 1035ab4382d2SGreg Kroah-Hartman * @value: returned modem value 1036ab4382d2SGreg Kroah-Hartman */ 1037ab4382d2SGreg Kroah-Hartman static int uart_get_lsr_info(struct tty_struct *tty, 1038ab4382d2SGreg Kroah-Hartman struct uart_state *state, unsigned int __user *value) 1039ab4382d2SGreg Kroah-Hartman { 10404047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 1041ab4382d2SGreg Kroah-Hartman unsigned int result; 1042ab4382d2SGreg Kroah-Hartman 1043ab4382d2SGreg Kroah-Hartman result = uport->ops->tx_empty(uport); 1044ab4382d2SGreg Kroah-Hartman 1045ab4382d2SGreg Kroah-Hartman /* 1046ab4382d2SGreg Kroah-Hartman * If we're about to load something into the transmit 1047ab4382d2SGreg Kroah-Hartman * register, we'll pretend the transmitter isn't empty to 1048ab4382d2SGreg Kroah-Hartman * avoid a race condition (depending on when the transmit 1049ab4382d2SGreg Kroah-Hartman * interrupt happens). 1050ab4382d2SGreg Kroah-Hartman */ 1051ab4382d2SGreg Kroah-Hartman if (uport->x_char || 1052ab4382d2SGreg Kroah-Hartman ((uart_circ_chars_pending(&state->xmit) > 0) && 1053d01f4d18SPeter Hurley !uart_tx_stopped(uport))) 1054ab4382d2SGreg Kroah-Hartman result &= ~TIOCSER_TEMT; 1055ab4382d2SGreg Kroah-Hartman 1056ab4382d2SGreg Kroah-Hartman return put_user(result, value); 1057ab4382d2SGreg Kroah-Hartman } 1058ab4382d2SGreg Kroah-Hartman 105960b33c13SAlan Cox static int uart_tiocmget(struct tty_struct *tty) 1060ab4382d2SGreg Kroah-Hartman { 1061ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1062ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 10634047b371SPeter Hurley struct uart_port *uport; 1064ab4382d2SGreg Kroah-Hartman int result = -EIO; 1065ab4382d2SGreg Kroah-Hartman 1066ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 10674047b371SPeter Hurley uport = uart_port_check(state); 10684047b371SPeter Hurley if (!uport) 10694047b371SPeter Hurley goto out; 10704047b371SPeter Hurley 107118900ca6SPeter Hurley if (!tty_io_error(tty)) { 1072ab4382d2SGreg Kroah-Hartman result = uport->mctrl; 1073ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 1074ab4382d2SGreg Kroah-Hartman result |= uport->ops->get_mctrl(uport); 1075ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 1076ab4382d2SGreg Kroah-Hartman } 10774047b371SPeter Hurley out: 1078ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 1079ab4382d2SGreg Kroah-Hartman return result; 1080ab4382d2SGreg Kroah-Hartman } 1081ab4382d2SGreg Kroah-Hartman 1082ab4382d2SGreg Kroah-Hartman static int 108320b9d177SAlan Cox uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear) 1084ab4382d2SGreg Kroah-Hartman { 1085ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1086ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 10874047b371SPeter Hurley struct uart_port *uport; 1088ab4382d2SGreg Kroah-Hartman int ret = -EIO; 1089ab4382d2SGreg Kroah-Hartman 1090ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 10914047b371SPeter Hurley uport = uart_port_check(state); 10924047b371SPeter Hurley if (!uport) 10934047b371SPeter Hurley goto out; 10944047b371SPeter Hurley 109518900ca6SPeter Hurley if (!tty_io_error(tty)) { 1096ab4382d2SGreg Kroah-Hartman uart_update_mctrl(uport, set, clear); 1097ab4382d2SGreg Kroah-Hartman ret = 0; 1098ab4382d2SGreg Kroah-Hartman } 10994047b371SPeter Hurley out: 1100ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 1101ab4382d2SGreg Kroah-Hartman return ret; 1102ab4382d2SGreg Kroah-Hartman } 1103ab4382d2SGreg Kroah-Hartman 1104ab4382d2SGreg Kroah-Hartman static int uart_break_ctl(struct tty_struct *tty, int break_state) 1105ab4382d2SGreg Kroah-Hartman { 1106ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1107ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 11084047b371SPeter Hurley struct uart_port *uport; 11094047b371SPeter Hurley int ret = -EIO; 1110ab4382d2SGreg Kroah-Hartman 1111ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 11124047b371SPeter Hurley uport = uart_port_check(state); 11134047b371SPeter Hurley if (!uport) 11144047b371SPeter Hurley goto out; 1115ab4382d2SGreg Kroah-Hartman 11167d73170eSJiangfeng Xiao if (uport->type != PORT_UNKNOWN && uport->ops->break_ctl) 1117ab4382d2SGreg Kroah-Hartman uport->ops->break_ctl(uport, break_state); 11184047b371SPeter Hurley ret = 0; 11194047b371SPeter Hurley out: 1120ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 11214047b371SPeter Hurley return ret; 1122ab4382d2SGreg Kroah-Hartman } 1123ab4382d2SGreg Kroah-Hartman 1124ab4382d2SGreg Kroah-Hartman static int uart_do_autoconfig(struct tty_struct *tty, struct uart_state *state) 1125ab4382d2SGreg Kroah-Hartman { 1126ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 11274047b371SPeter Hurley struct uart_port *uport; 1128ab4382d2SGreg Kroah-Hartman int flags, ret; 1129ab4382d2SGreg Kroah-Hartman 1130ab4382d2SGreg Kroah-Hartman if (!capable(CAP_SYS_ADMIN)) 1131ab4382d2SGreg Kroah-Hartman return -EPERM; 1132ab4382d2SGreg Kroah-Hartman 1133ab4382d2SGreg Kroah-Hartman /* 1134ab4382d2SGreg Kroah-Hartman * Take the per-port semaphore. This prevents count from 1135ab4382d2SGreg Kroah-Hartman * changing, and hence any extra opens of the port while 1136ab4382d2SGreg Kroah-Hartman * we're auto-configuring. 1137ab4382d2SGreg Kroah-Hartman */ 1138ab4382d2SGreg Kroah-Hartman if (mutex_lock_interruptible(&port->mutex)) 1139ab4382d2SGreg Kroah-Hartman return -ERESTARTSYS; 1140ab4382d2SGreg Kroah-Hartman 11414047b371SPeter Hurley uport = uart_port_check(state); 11424047b371SPeter Hurley if (!uport) { 11434047b371SPeter Hurley ret = -EIO; 11444047b371SPeter Hurley goto out; 11454047b371SPeter Hurley } 11464047b371SPeter Hurley 1147ab4382d2SGreg Kroah-Hartman ret = -EBUSY; 1148ab4382d2SGreg Kroah-Hartman if (tty_port_users(port) == 1) { 1149ab4382d2SGreg Kroah-Hartman uart_shutdown(tty, state); 1150ab4382d2SGreg Kroah-Hartman 1151ab4382d2SGreg Kroah-Hartman /* 1152ab4382d2SGreg Kroah-Hartman * If we already have a port type configured, 1153ab4382d2SGreg Kroah-Hartman * we must release its resources. 1154ab4382d2SGreg Kroah-Hartman */ 1155a7cfaf16SFabio Estevam if (uport->type != PORT_UNKNOWN && uport->ops->release_port) 1156ab4382d2SGreg Kroah-Hartman uport->ops->release_port(uport); 1157ab4382d2SGreg Kroah-Hartman 1158ab4382d2SGreg Kroah-Hartman flags = UART_CONFIG_TYPE; 1159ab4382d2SGreg Kroah-Hartman if (uport->flags & UPF_AUTO_IRQ) 1160ab4382d2SGreg Kroah-Hartman flags |= UART_CONFIG_IRQ; 1161ab4382d2SGreg Kroah-Hartman 1162ab4382d2SGreg Kroah-Hartman /* 1163ab4382d2SGreg Kroah-Hartman * This will claim the ports resources if 1164ab4382d2SGreg Kroah-Hartman * a port is found. 1165ab4382d2SGreg Kroah-Hartman */ 1166ab4382d2SGreg Kroah-Hartman uport->ops->config_port(uport, flags); 1167ab4382d2SGreg Kroah-Hartman 1168ab4382d2SGreg Kroah-Hartman ret = uart_startup(tty, state, 1); 116971456906SSebastian Andrzej Siewior if (ret == 0) 117071456906SSebastian Andrzej Siewior tty_port_set_initialized(port, true); 1171b3b57646SRob Herring if (ret > 0) 1172b3b57646SRob Herring ret = 0; 1173ab4382d2SGreg Kroah-Hartman } 11744047b371SPeter Hurley out: 1175ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 1176ab4382d2SGreg Kroah-Hartman return ret; 1177ab4382d2SGreg Kroah-Hartman } 1178ab4382d2SGreg Kroah-Hartman 11791fdc3106SAlexander Shiyan static void uart_enable_ms(struct uart_port *uport) 11801fdc3106SAlexander Shiyan { 11811fdc3106SAlexander Shiyan /* 11821fdc3106SAlexander Shiyan * Force modem status interrupts on 11831fdc3106SAlexander Shiyan */ 11841fdc3106SAlexander Shiyan if (uport->ops->enable_ms) 11851fdc3106SAlexander Shiyan uport->ops->enable_ms(uport); 11861fdc3106SAlexander Shiyan } 11871fdc3106SAlexander Shiyan 1188ab4382d2SGreg Kroah-Hartman /* 1189ab4382d2SGreg Kroah-Hartman * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change 1190ab4382d2SGreg Kroah-Hartman * - mask passed in arg for lines of interest 1191ab4382d2SGreg Kroah-Hartman * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking) 1192ab4382d2SGreg Kroah-Hartman * Caller should use TIOCGICOUNT to see which one it was 1193ab4382d2SGreg Kroah-Hartman * 1194ab4382d2SGreg Kroah-Hartman * FIXME: This wants extracting into a common all driver implementation 1195ab4382d2SGreg Kroah-Hartman * of TIOCMWAIT using tty_port. 1196ab4382d2SGreg Kroah-Hartman */ 11979ed19428SPeter Hurley static int uart_wait_modem_status(struct uart_state *state, unsigned long arg) 1198ab4382d2SGreg Kroah-Hartman { 11999ed19428SPeter Hurley struct uart_port *uport; 1200ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 1201ab4382d2SGreg Kroah-Hartman DECLARE_WAITQUEUE(wait, current); 1202ab4382d2SGreg Kroah-Hartman struct uart_icount cprev, cnow; 1203ab4382d2SGreg Kroah-Hartman int ret; 1204ab4382d2SGreg Kroah-Hartman 1205ab4382d2SGreg Kroah-Hartman /* 1206ab4382d2SGreg Kroah-Hartman * note the counters on entry 1207ab4382d2SGreg Kroah-Hartman */ 12089ed19428SPeter Hurley uport = uart_port_ref(state); 12099ed19428SPeter Hurley if (!uport) 12109ed19428SPeter Hurley return -EIO; 1211ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 1212ab4382d2SGreg Kroah-Hartman memcpy(&cprev, &uport->icount, sizeof(struct uart_icount)); 12131fdc3106SAlexander Shiyan uart_enable_ms(uport); 1214ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 1215ab4382d2SGreg Kroah-Hartman 1216ab4382d2SGreg Kroah-Hartman add_wait_queue(&port->delta_msr_wait, &wait); 1217ab4382d2SGreg Kroah-Hartman for (;;) { 1218ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 1219ab4382d2SGreg Kroah-Hartman memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); 1220ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 1221ab4382d2SGreg Kroah-Hartman 1222ab4382d2SGreg Kroah-Hartman set_current_state(TASK_INTERRUPTIBLE); 1223ab4382d2SGreg Kroah-Hartman 1224ab4382d2SGreg Kroah-Hartman if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 1225ab4382d2SGreg Kroah-Hartman ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 1226ab4382d2SGreg Kroah-Hartman ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || 1227ab4382d2SGreg Kroah-Hartman ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { 1228ab4382d2SGreg Kroah-Hartman ret = 0; 1229ab4382d2SGreg Kroah-Hartman break; 1230ab4382d2SGreg Kroah-Hartman } 1231ab4382d2SGreg Kroah-Hartman 1232ab4382d2SGreg Kroah-Hartman schedule(); 1233ab4382d2SGreg Kroah-Hartman 1234ab4382d2SGreg Kroah-Hartman /* see if a signal did it */ 1235ab4382d2SGreg Kroah-Hartman if (signal_pending(current)) { 1236ab4382d2SGreg Kroah-Hartman ret = -ERESTARTSYS; 1237ab4382d2SGreg Kroah-Hartman break; 1238ab4382d2SGreg Kroah-Hartman } 1239ab4382d2SGreg Kroah-Hartman 1240ab4382d2SGreg Kroah-Hartman cprev = cnow; 1241ab4382d2SGreg Kroah-Hartman } 124297f9f707SFabian Frederick __set_current_state(TASK_RUNNING); 1243ab4382d2SGreg Kroah-Hartman remove_wait_queue(&port->delta_msr_wait, &wait); 12449ed19428SPeter Hurley uart_port_deref(uport); 1245ab4382d2SGreg Kroah-Hartman 1246ab4382d2SGreg Kroah-Hartman return ret; 1247ab4382d2SGreg Kroah-Hartman } 1248ab4382d2SGreg Kroah-Hartman 1249ab4382d2SGreg Kroah-Hartman /* 1250ab4382d2SGreg Kroah-Hartman * Get counter of input serial line interrupts (DCD,RI,DSR,CTS) 1251ab4382d2SGreg Kroah-Hartman * Return: write counters to the user passed counter struct 1252ab4382d2SGreg Kroah-Hartman * NB: both 1->0 and 0->1 transitions are counted except for 1253ab4382d2SGreg Kroah-Hartman * RI where only 0->1 is counted. 1254ab4382d2SGreg Kroah-Hartman */ 1255ab4382d2SGreg Kroah-Hartman static int uart_get_icount(struct tty_struct *tty, 1256ab4382d2SGreg Kroah-Hartman struct serial_icounter_struct *icount) 1257ab4382d2SGreg Kroah-Hartman { 1258ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1259ab4382d2SGreg Kroah-Hartman struct uart_icount cnow; 12609ed19428SPeter Hurley struct uart_port *uport; 1261ab4382d2SGreg Kroah-Hartman 12629ed19428SPeter Hurley uport = uart_port_ref(state); 12639ed19428SPeter Hurley if (!uport) 12649ed19428SPeter Hurley return -EIO; 1265ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 1266ab4382d2SGreg Kroah-Hartman memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); 1267ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 12689ed19428SPeter Hurley uart_port_deref(uport); 1269ab4382d2SGreg Kroah-Hartman 1270ab4382d2SGreg Kroah-Hartman icount->cts = cnow.cts; 1271ab4382d2SGreg Kroah-Hartman icount->dsr = cnow.dsr; 1272ab4382d2SGreg Kroah-Hartman icount->rng = cnow.rng; 1273ab4382d2SGreg Kroah-Hartman icount->dcd = cnow.dcd; 1274ab4382d2SGreg Kroah-Hartman icount->rx = cnow.rx; 1275ab4382d2SGreg Kroah-Hartman icount->tx = cnow.tx; 1276ab4382d2SGreg Kroah-Hartman icount->frame = cnow.frame; 1277ab4382d2SGreg Kroah-Hartman icount->overrun = cnow.overrun; 1278ab4382d2SGreg Kroah-Hartman icount->parity = cnow.parity; 1279ab4382d2SGreg Kroah-Hartman icount->brk = cnow.brk; 1280ab4382d2SGreg Kroah-Hartman icount->buf_overrun = cnow.buf_overrun; 1281ab4382d2SGreg Kroah-Hartman 1282ab4382d2SGreg Kroah-Hartman return 0; 1283ab4382d2SGreg Kroah-Hartman } 1284ab4382d2SGreg Kroah-Hartman 1285a5f276f1SRicardo Ribalda Delgado static int uart_get_rs485_config(struct uart_port *port, 1286a5f276f1SRicardo Ribalda Delgado struct serial_rs485 __user *rs485) 1287a5f276f1SRicardo Ribalda Delgado { 1288bd737f87SRicardo Ribalda Delgado unsigned long flags; 1289bd737f87SRicardo Ribalda Delgado struct serial_rs485 aux; 1290bd737f87SRicardo Ribalda Delgado 1291bd737f87SRicardo Ribalda Delgado spin_lock_irqsave(&port->lock, flags); 1292bd737f87SRicardo Ribalda Delgado aux = port->rs485; 1293bd737f87SRicardo Ribalda Delgado spin_unlock_irqrestore(&port->lock, flags); 1294bd737f87SRicardo Ribalda Delgado 1295bd737f87SRicardo Ribalda Delgado if (copy_to_user(rs485, &aux, sizeof(aux))) 1296a5f276f1SRicardo Ribalda Delgado return -EFAULT; 1297bd737f87SRicardo Ribalda Delgado 1298a5f276f1SRicardo Ribalda Delgado return 0; 1299a5f276f1SRicardo Ribalda Delgado } 1300a5f276f1SRicardo Ribalda Delgado 1301a5f276f1SRicardo Ribalda Delgado static int uart_set_rs485_config(struct uart_port *port, 1302a5f276f1SRicardo Ribalda Delgado struct serial_rs485 __user *rs485_user) 1303a5f276f1SRicardo Ribalda Delgado { 1304a5f276f1SRicardo Ribalda Delgado struct serial_rs485 rs485; 1305a5f276f1SRicardo Ribalda Delgado int ret; 1306bd737f87SRicardo Ribalda Delgado unsigned long flags; 1307a5f276f1SRicardo Ribalda Delgado 1308a5f276f1SRicardo Ribalda Delgado if (!port->rs485_config) 1309a5f276f1SRicardo Ribalda Delgado return -ENOIOCTLCMD; 1310a5f276f1SRicardo Ribalda Delgado 1311a5f276f1SRicardo Ribalda Delgado if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user))) 1312a5f276f1SRicardo Ribalda Delgado return -EFAULT; 1313a5f276f1SRicardo Ribalda Delgado 1314bd737f87SRicardo Ribalda Delgado spin_lock_irqsave(&port->lock, flags); 1315a5f276f1SRicardo Ribalda Delgado ret = port->rs485_config(port, &rs485); 1316bd737f87SRicardo Ribalda Delgado spin_unlock_irqrestore(&port->lock, flags); 1317a5f276f1SRicardo Ribalda Delgado if (ret) 1318a5f276f1SRicardo Ribalda Delgado return ret; 1319a5f276f1SRicardo Ribalda Delgado 1320a5f276f1SRicardo Ribalda Delgado if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485))) 1321a5f276f1SRicardo Ribalda Delgado return -EFAULT; 1322a5f276f1SRicardo Ribalda Delgado 1323a5f276f1SRicardo Ribalda Delgado return 0; 1324a5f276f1SRicardo Ribalda Delgado } 1325a5f276f1SRicardo Ribalda Delgado 1326ad8c0eaaSNicolas Ferre static int uart_get_iso7816_config(struct uart_port *port, 1327ad8c0eaaSNicolas Ferre struct serial_iso7816 __user *iso7816) 1328ad8c0eaaSNicolas Ferre { 1329ad8c0eaaSNicolas Ferre unsigned long flags; 1330ad8c0eaaSNicolas Ferre struct serial_iso7816 aux; 1331ad8c0eaaSNicolas Ferre 1332ad8c0eaaSNicolas Ferre if (!port->iso7816_config) 1333ad8c0eaaSNicolas Ferre return -ENOIOCTLCMD; 1334ad8c0eaaSNicolas Ferre 1335ad8c0eaaSNicolas Ferre spin_lock_irqsave(&port->lock, flags); 1336ad8c0eaaSNicolas Ferre aux = port->iso7816; 1337ad8c0eaaSNicolas Ferre spin_unlock_irqrestore(&port->lock, flags); 1338ad8c0eaaSNicolas Ferre 1339ad8c0eaaSNicolas Ferre if (copy_to_user(iso7816, &aux, sizeof(aux))) 1340ad8c0eaaSNicolas Ferre return -EFAULT; 1341ad8c0eaaSNicolas Ferre 1342ad8c0eaaSNicolas Ferre return 0; 1343ad8c0eaaSNicolas Ferre } 1344ad8c0eaaSNicolas Ferre 1345ad8c0eaaSNicolas Ferre static int uart_set_iso7816_config(struct uart_port *port, 1346ad8c0eaaSNicolas Ferre struct serial_iso7816 __user *iso7816_user) 1347ad8c0eaaSNicolas Ferre { 1348ad8c0eaaSNicolas Ferre struct serial_iso7816 iso7816; 1349ad8c0eaaSNicolas Ferre int i, ret; 1350ad8c0eaaSNicolas Ferre unsigned long flags; 1351ad8c0eaaSNicolas Ferre 1352ad8c0eaaSNicolas Ferre if (!port->iso7816_config) 1353ad8c0eaaSNicolas Ferre return -ENOIOCTLCMD; 1354ad8c0eaaSNicolas Ferre 1355ad8c0eaaSNicolas Ferre if (copy_from_user(&iso7816, iso7816_user, sizeof(*iso7816_user))) 1356ad8c0eaaSNicolas Ferre return -EFAULT; 1357ad8c0eaaSNicolas Ferre 1358ad8c0eaaSNicolas Ferre /* 1359ad8c0eaaSNicolas Ferre * There are 5 words reserved for future use. Check that userspace 1360ad8c0eaaSNicolas Ferre * doesn't put stuff in there to prevent breakages in the future. 1361ad8c0eaaSNicolas Ferre */ 1362ad8c0eaaSNicolas Ferre for (i = 0; i < 5; i++) 1363ad8c0eaaSNicolas Ferre if (iso7816.reserved[i]) 1364ad8c0eaaSNicolas Ferre return -EINVAL; 1365ad8c0eaaSNicolas Ferre 1366ad8c0eaaSNicolas Ferre spin_lock_irqsave(&port->lock, flags); 1367ad8c0eaaSNicolas Ferre ret = port->iso7816_config(port, &iso7816); 1368ad8c0eaaSNicolas Ferre spin_unlock_irqrestore(&port->lock, flags); 1369ad8c0eaaSNicolas Ferre if (ret) 1370ad8c0eaaSNicolas Ferre return ret; 1371ad8c0eaaSNicolas Ferre 1372ad8c0eaaSNicolas Ferre if (copy_to_user(iso7816_user, &port->iso7816, sizeof(port->iso7816))) 1373ad8c0eaaSNicolas Ferre return -EFAULT; 1374ad8c0eaaSNicolas Ferre 1375ad8c0eaaSNicolas Ferre return 0; 1376ad8c0eaaSNicolas Ferre } 1377ad8c0eaaSNicolas Ferre 1378ab4382d2SGreg Kroah-Hartman /* 1379ab4382d2SGreg Kroah-Hartman * Called via sys_ioctl. We can use spin_lock_irq() here. 1380ab4382d2SGreg Kroah-Hartman */ 1381ab4382d2SGreg Kroah-Hartman static int 13824047b371SPeter Hurley uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg) 1383ab4382d2SGreg Kroah-Hartman { 1384ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1385ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 13864047b371SPeter Hurley struct uart_port *uport; 1387ab4382d2SGreg Kroah-Hartman void __user *uarg = (void __user *)arg; 1388ab4382d2SGreg Kroah-Hartman int ret = -ENOIOCTLCMD; 1389ab4382d2SGreg Kroah-Hartman 1390ab4382d2SGreg Kroah-Hartman 1391ab4382d2SGreg Kroah-Hartman /* 1392ab4382d2SGreg Kroah-Hartman * These ioctls don't rely on the hardware to be present. 1393ab4382d2SGreg Kroah-Hartman */ 1394ab4382d2SGreg Kroah-Hartman switch (cmd) { 1395ab4382d2SGreg Kroah-Hartman case TIOCSERCONFIG: 13967c8ab967SPeter Hurley down_write(&tty->termios_rwsem); 1397ab4382d2SGreg Kroah-Hartman ret = uart_do_autoconfig(tty, state); 13987c8ab967SPeter Hurley up_write(&tty->termios_rwsem); 1399ab4382d2SGreg Kroah-Hartman break; 1400ab4382d2SGreg Kroah-Hartman } 1401ab4382d2SGreg Kroah-Hartman 1402ab4382d2SGreg Kroah-Hartman if (ret != -ENOIOCTLCMD) 1403ab4382d2SGreg Kroah-Hartman goto out; 1404ab4382d2SGreg Kroah-Hartman 140518900ca6SPeter Hurley if (tty_io_error(tty)) { 1406ab4382d2SGreg Kroah-Hartman ret = -EIO; 1407ab4382d2SGreg Kroah-Hartman goto out; 1408ab4382d2SGreg Kroah-Hartman } 1409ab4382d2SGreg Kroah-Hartman 1410ab4382d2SGreg Kroah-Hartman /* 1411ab4382d2SGreg Kroah-Hartman * The following should only be used when hardware is present. 1412ab4382d2SGreg Kroah-Hartman */ 1413ab4382d2SGreg Kroah-Hartman switch (cmd) { 1414ab4382d2SGreg Kroah-Hartman case TIOCMIWAIT: 1415ab4382d2SGreg Kroah-Hartman ret = uart_wait_modem_status(state, arg); 1416ab4382d2SGreg Kroah-Hartman break; 1417ab4382d2SGreg Kroah-Hartman } 1418ab4382d2SGreg Kroah-Hartman 1419ab4382d2SGreg Kroah-Hartman if (ret != -ENOIOCTLCMD) 1420ab4382d2SGreg Kroah-Hartman goto out; 1421ab4382d2SGreg Kroah-Hartman 1422ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 14234047b371SPeter Hurley uport = uart_port_check(state); 1424ab4382d2SGreg Kroah-Hartman 14254047b371SPeter Hurley if (!uport || tty_io_error(tty)) { 1426ab4382d2SGreg Kroah-Hartman ret = -EIO; 1427ab4382d2SGreg Kroah-Hartman goto out_up; 1428ab4382d2SGreg Kroah-Hartman } 1429ab4382d2SGreg Kroah-Hartman 1430ab4382d2SGreg Kroah-Hartman /* 1431ab4382d2SGreg Kroah-Hartman * All these rely on hardware being present and need to be 1432ab4382d2SGreg Kroah-Hartman * protected against the tty being hung up. 1433ab4382d2SGreg Kroah-Hartman */ 1434a9c20a9cSRicardo Ribalda Delgado 1435ab4382d2SGreg Kroah-Hartman switch (cmd) { 1436a9c20a9cSRicardo Ribalda Delgado case TIOCSERGETLSR: /* Get line status register */ 1437a9c20a9cSRicardo Ribalda Delgado ret = uart_get_lsr_info(tty, state, uarg); 1438a9c20a9cSRicardo Ribalda Delgado break; 1439a9c20a9cSRicardo Ribalda Delgado 1440a5f276f1SRicardo Ribalda Delgado case TIOCGRS485: 14414047b371SPeter Hurley ret = uart_get_rs485_config(uport, uarg); 1442a5f276f1SRicardo Ribalda Delgado break; 1443a5f276f1SRicardo Ribalda Delgado 1444a5f276f1SRicardo Ribalda Delgado case TIOCSRS485: 14454047b371SPeter Hurley ret = uart_set_rs485_config(uport, uarg); 1446a5f276f1SRicardo Ribalda Delgado break; 1447ad8c0eaaSNicolas Ferre 1448ad8c0eaaSNicolas Ferre case TIOCSISO7816: 1449ad8c0eaaSNicolas Ferre ret = uart_set_iso7816_config(state->uart_port, uarg); 1450ad8c0eaaSNicolas Ferre break; 1451ad8c0eaaSNicolas Ferre 1452ad8c0eaaSNicolas Ferre case TIOCGISO7816: 1453ad8c0eaaSNicolas Ferre ret = uart_get_iso7816_config(state->uart_port, uarg); 1454ad8c0eaaSNicolas Ferre break; 14554047b371SPeter Hurley default: 1456ab4382d2SGreg Kroah-Hartman if (uport->ops->ioctl) 1457ab4382d2SGreg Kroah-Hartman ret = uport->ops->ioctl(uport, cmd, arg); 1458ab4382d2SGreg Kroah-Hartman break; 1459ab4382d2SGreg Kroah-Hartman } 1460ab4382d2SGreg Kroah-Hartman out_up: 1461ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 1462ab4382d2SGreg Kroah-Hartman out: 1463ab4382d2SGreg Kroah-Hartman return ret; 1464ab4382d2SGreg Kroah-Hartman } 1465ab4382d2SGreg Kroah-Hartman 1466ab4382d2SGreg Kroah-Hartman static void uart_set_ldisc(struct tty_struct *tty) 1467ab4382d2SGreg Kroah-Hartman { 1468ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 14694047b371SPeter Hurley struct uart_port *uport; 1470ab4382d2SGreg Kroah-Hartman 1471db1b9dfcSPeter Hurley mutex_lock(&state->port.mutex); 14724047b371SPeter Hurley uport = uart_port_check(state); 14734047b371SPeter Hurley if (uport && uport->ops->set_ldisc) 1474732a84a0SPeter Hurley uport->ops->set_ldisc(uport, &tty->termios); 1475db1b9dfcSPeter Hurley mutex_unlock(&state->port.mutex); 1476db1b9dfcSPeter Hurley } 1477ab4382d2SGreg Kroah-Hartman 1478ab4382d2SGreg Kroah-Hartman static void uart_set_termios(struct tty_struct *tty, 1479ab4382d2SGreg Kroah-Hartman struct ktermios *old_termios) 1480ab4382d2SGreg Kroah-Hartman { 1481ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 14824047b371SPeter Hurley struct uart_port *uport; 1483adc8d746SAlan Cox unsigned int cflag = tty->termios.c_cflag; 14842cbacafdSRussell King unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK; 14852cbacafdSRussell King bool sw_changed = false; 1486ab4382d2SGreg Kroah-Hartman 14874047b371SPeter Hurley mutex_lock(&state->port.mutex); 14884047b371SPeter Hurley uport = uart_port_check(state); 14894047b371SPeter Hurley if (!uport) 14904047b371SPeter Hurley goto out; 14914047b371SPeter Hurley 14922cbacafdSRussell King /* 14932cbacafdSRussell King * Drivers doing software flow control also need to know 14942cbacafdSRussell King * about changes to these input settings. 14952cbacafdSRussell King */ 14962cbacafdSRussell King if (uport->flags & UPF_SOFT_FLOW) { 14972cbacafdSRussell King iflag_mask |= IXANY|IXON|IXOFF; 14982cbacafdSRussell King sw_changed = 14992cbacafdSRussell King tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] || 15002cbacafdSRussell King tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP]; 15012cbacafdSRussell King } 1502ab4382d2SGreg Kroah-Hartman 1503ab4382d2SGreg Kroah-Hartman /* 1504ab4382d2SGreg Kroah-Hartman * These are the bits that are used to setup various 1505ab4382d2SGreg Kroah-Hartman * flags in the low level driver. We can ignore the Bfoo 1506ab4382d2SGreg Kroah-Hartman * bits in c_cflag; c_[io]speed will always be set 1507ab4382d2SGreg Kroah-Hartman * appropriately by set_termios() in tty_ioctl.c 1508ab4382d2SGreg Kroah-Hartman */ 1509ab4382d2SGreg Kroah-Hartman if ((cflag ^ old_termios->c_cflag) == 0 && 1510adc8d746SAlan Cox tty->termios.c_ospeed == old_termios->c_ospeed && 1511adc8d746SAlan Cox tty->termios.c_ispeed == old_termios->c_ispeed && 15122cbacafdSRussell King ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 && 15132cbacafdSRussell King !sw_changed) { 15144047b371SPeter Hurley goto out; 1515ab4382d2SGreg Kroah-Hartman } 1516ab4382d2SGreg Kroah-Hartman 1517ab4382d2SGreg Kroah-Hartman uart_change_speed(tty, state, old_termios); 1518c7a6b9e4SHariprasad Kelam /* reload cflag from termios; port driver may have overridden flags */ 1519c18b55fdSPeter Hurley cflag = tty->termios.c_cflag; 1520ab4382d2SGreg Kroah-Hartman 1521ab4382d2SGreg Kroah-Hartman /* Handle transition to B0 status */ 1522ab4382d2SGreg Kroah-Hartman if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) 1523dec94e70SRussell King uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR); 1524ab4382d2SGreg Kroah-Hartman /* Handle transition away from B0 status */ 1525ab4382d2SGreg Kroah-Hartman else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) { 1526ab4382d2SGreg Kroah-Hartman unsigned int mask = TIOCM_DTR; 15274ed71addSTamseel Shams 152897ef38b8SPeter Hurley if (!(cflag & CRTSCTS) || !tty_throttled(tty)) 1529ab4382d2SGreg Kroah-Hartman mask |= TIOCM_RTS; 1530dec94e70SRussell King uart_set_mctrl(uport, mask); 1531ab4382d2SGreg Kroah-Hartman } 15324047b371SPeter Hurley out: 15334047b371SPeter Hurley mutex_unlock(&state->port.mutex); 1534ab4382d2SGreg Kroah-Hartman } 1535ab4382d2SGreg Kroah-Hartman 1536ab4382d2SGreg Kroah-Hartman /* 1537ef4f527cSKevin Cernekee * Calls to uart_close() are serialised via the tty_lock in 1538ef4f527cSKevin Cernekee * drivers/tty/tty_io.c:tty_release() 1539ef4f527cSKevin Cernekee * drivers/tty/tty_io.c:do_tty_hangup() 1540ab4382d2SGreg Kroah-Hartman */ 1541ab4382d2SGreg Kroah-Hartman static void uart_close(struct tty_struct *tty, struct file *filp) 1542ab4382d2SGreg Kroah-Hartman { 1543ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1544ab4382d2SGreg Kroah-Hartman 154591b32f54SPeter Hurley if (!state) { 154691b32f54SPeter Hurley struct uart_driver *drv = tty->driver->driver_state; 15479356335fSColin Ian King struct tty_port *port; 154891b32f54SPeter Hurley 154991b32f54SPeter Hurley state = drv->state + tty->index; 155091b32f54SPeter Hurley port = &state->port; 155191b32f54SPeter Hurley spin_lock_irq(&port->lock); 155291b32f54SPeter Hurley --port->count; 155391b32f54SPeter Hurley spin_unlock_irq(&port->lock); 1554ab4382d2SGreg Kroah-Hartman return; 155591b32f54SPeter Hurley } 1556ab4382d2SGreg Kroah-Hartman 155739b3d892SPeter Hurley pr_debug("uart_close(%d) called\n", tty->index); 1558ab4382d2SGreg Kroah-Hartman 1559761ed4a9SRob Herring tty_port_close(tty->port, tty, filp); 1560761ed4a9SRob Herring } 1561ab4382d2SGreg Kroah-Hartman 1562761ed4a9SRob Herring static void uart_tty_port_shutdown(struct tty_port *port) 1563761ed4a9SRob Herring { 1564761ed4a9SRob Herring struct uart_state *state = container_of(port, struct uart_state, port); 1565761ed4a9SRob Herring struct uart_port *uport = uart_port_check(state); 1566af224ca2SPeter Hurley 1567ab4382d2SGreg Kroah-Hartman /* 1568ab4382d2SGreg Kroah-Hartman * At this point, we stop accepting input. To do this, we 1569ab4382d2SGreg Kroah-Hartman * disable the receive line status interrupts. 1570ab4382d2SGreg Kroah-Hartman */ 1571a5a2b130SAndy Shevchenko if (WARN(!uport, "detached port still initialized!\n")) 1572a5a2b130SAndy Shevchenko return; 1573761ed4a9SRob Herring 1574a5a2b130SAndy Shevchenko spin_lock_irq(&uport->lock); 1575ab4382d2SGreg Kroah-Hartman uport->ops->stop_rx(uport); 15766fb98fb3SPeter Hurley spin_unlock_irq(&uport->lock); 1577761ed4a9SRob Herring 1578761ed4a9SRob Herring uart_port_shutdown(port); 1579761ed4a9SRob Herring 1580ab4382d2SGreg Kroah-Hartman /* 1581761ed4a9SRob Herring * It's possible for shutdown to be called after suspend if we get 1582761ed4a9SRob Herring * a DCD drop (hangup) at just the right time. Clear suspended bit so 1583761ed4a9SRob Herring * we don't try to resume a port that has been shutdown. 1584ab4382d2SGreg Kroah-Hartman */ 1585761ed4a9SRob Herring tty_port_set_suspended(port, 0); 1586ab4382d2SGreg Kroah-Hartman 15876f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_OFF); 1588ab4382d2SGreg Kroah-Hartman 1589ab4382d2SGreg Kroah-Hartman } 1590ab4382d2SGreg Kroah-Hartman 15911f33a51dSJiri Slaby static void uart_wait_until_sent(struct tty_struct *tty, int timeout) 1592ab4382d2SGreg Kroah-Hartman { 15931f33a51dSJiri Slaby struct uart_state *state = tty->driver_data; 15949ed19428SPeter Hurley struct uart_port *port; 1595ab4382d2SGreg Kroah-Hartman unsigned long char_time, expire; 1596ab4382d2SGreg Kroah-Hartman 15979ed19428SPeter Hurley port = uart_port_ref(state); 1598ef510beaSAndy Shevchenko if (!port) 1599ef510beaSAndy Shevchenko return; 1600ef510beaSAndy Shevchenko 1601ef510beaSAndy Shevchenko if (port->type == PORT_UNKNOWN || port->fifosize == 0) { 16029ed19428SPeter Hurley uart_port_deref(port); 1603ab4382d2SGreg Kroah-Hartman return; 16049ed19428SPeter Hurley } 1605ab4382d2SGreg Kroah-Hartman 1606ab4382d2SGreg Kroah-Hartman /* 1607ab4382d2SGreg Kroah-Hartman * Set the check interval to be 1/5 of the estimated time to 1608ab4382d2SGreg Kroah-Hartman * send a single character, and make it at least 1. The check 1609ab4382d2SGreg Kroah-Hartman * interval should also be less than the timeout. 1610ab4382d2SGreg Kroah-Hartman * 1611ab4382d2SGreg Kroah-Hartman * Note: we have to use pretty tight timings here to satisfy 1612ab4382d2SGreg Kroah-Hartman * the NIST-PCTS. 1613ab4382d2SGreg Kroah-Hartman */ 1614ab4382d2SGreg Kroah-Hartman char_time = (port->timeout - HZ/50) / port->fifosize; 1615ab4382d2SGreg Kroah-Hartman char_time = char_time / 5; 1616ab4382d2SGreg Kroah-Hartman if (char_time == 0) 1617ab4382d2SGreg Kroah-Hartman char_time = 1; 1618ab4382d2SGreg Kroah-Hartman if (timeout && timeout < char_time) 1619ab4382d2SGreg Kroah-Hartman char_time = timeout; 1620ab4382d2SGreg Kroah-Hartman 1621ab4382d2SGreg Kroah-Hartman /* 1622ab4382d2SGreg Kroah-Hartman * If the transmitter hasn't cleared in twice the approximate 1623ab4382d2SGreg Kroah-Hartman * amount of time to send the entire FIFO, it probably won't 1624ab4382d2SGreg Kroah-Hartman * ever clear. This assumes the UART isn't doing flow 1625ab4382d2SGreg Kroah-Hartman * control, which is currently the case. Hence, if it ever 1626ab4382d2SGreg Kroah-Hartman * takes longer than port->timeout, this is probably due to a 1627ab4382d2SGreg Kroah-Hartman * UART bug of some kind. So, we clamp the timeout parameter at 1628ab4382d2SGreg Kroah-Hartman * 2*port->timeout. 1629ab4382d2SGreg Kroah-Hartman */ 1630ab4382d2SGreg Kroah-Hartman if (timeout == 0 || timeout > 2 * port->timeout) 1631ab4382d2SGreg Kroah-Hartman timeout = 2 * port->timeout; 1632ab4382d2SGreg Kroah-Hartman 1633ab4382d2SGreg Kroah-Hartman expire = jiffies + timeout; 1634ab4382d2SGreg Kroah-Hartman 1635ab4382d2SGreg Kroah-Hartman pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n", 1636ab4382d2SGreg Kroah-Hartman port->line, jiffies, expire); 1637ab4382d2SGreg Kroah-Hartman 1638ab4382d2SGreg Kroah-Hartman /* 1639ab4382d2SGreg Kroah-Hartman * Check whether the transmitter is empty every 'char_time'. 1640ab4382d2SGreg Kroah-Hartman * 'timeout' / 'expire' give us the maximum amount of time 1641ab4382d2SGreg Kroah-Hartman * we wait. 1642ab4382d2SGreg Kroah-Hartman */ 1643ab4382d2SGreg Kroah-Hartman while (!port->ops->tx_empty(port)) { 1644ab4382d2SGreg Kroah-Hartman msleep_interruptible(jiffies_to_msecs(char_time)); 1645ab4382d2SGreg Kroah-Hartman if (signal_pending(current)) 1646ab4382d2SGreg Kroah-Hartman break; 1647ab4382d2SGreg Kroah-Hartman if (time_after(jiffies, expire)) 1648ab4382d2SGreg Kroah-Hartman break; 1649ab4382d2SGreg Kroah-Hartman } 16509ed19428SPeter Hurley uart_port_deref(port); 1651ab4382d2SGreg Kroah-Hartman } 1652ab4382d2SGreg Kroah-Hartman 1653ab4382d2SGreg Kroah-Hartman /* 1654ef4f527cSKevin Cernekee * Calls to uart_hangup() are serialised by the tty_lock in 1655ef4f527cSKevin Cernekee * drivers/tty/tty_io.c:do_tty_hangup() 1656ef4f527cSKevin Cernekee * This runs from a workqueue and can sleep for a _short_ time only. 1657ab4382d2SGreg Kroah-Hartman */ 1658ab4382d2SGreg Kroah-Hartman static void uart_hangup(struct tty_struct *tty) 1659ab4382d2SGreg Kroah-Hartman { 1660ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1661ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 1662af224ca2SPeter Hurley struct uart_port *uport; 1663ab4382d2SGreg Kroah-Hartman unsigned long flags; 1664ab4382d2SGreg Kroah-Hartman 166539b3d892SPeter Hurley pr_debug("uart_hangup(%d)\n", tty->index); 1666ab4382d2SGreg Kroah-Hartman 1667ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 1668af224ca2SPeter Hurley uport = uart_port_check(state); 1669af224ca2SPeter Hurley WARN(!uport, "hangup of detached port!\n"); 1670af224ca2SPeter Hurley 1671807c8d81SPeter Hurley if (tty_port_active(port)) { 1672ab4382d2SGreg Kroah-Hartman uart_flush_buffer(tty); 1673ab4382d2SGreg Kroah-Hartman uart_shutdown(tty, state); 1674ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 1675ab4382d2SGreg Kroah-Hartman port->count = 0; 1676ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 1677807c8d81SPeter Hurley tty_port_set_active(port, 0); 1678ab4382d2SGreg Kroah-Hartman tty_port_tty_set(port, NULL); 1679af224ca2SPeter Hurley if (uport && !uart_console(uport)) 1680bf903c0cSGeert Uytterhoeven uart_change_pm(state, UART_PM_STATE_OFF); 1681ab4382d2SGreg Kroah-Hartman wake_up_interruptible(&port->open_wait); 1682ab4382d2SGreg Kroah-Hartman wake_up_interruptible(&port->delta_msr_wait); 1683ab4382d2SGreg Kroah-Hartman } 1684ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 1685ab4382d2SGreg Kroah-Hartman } 1686ab4382d2SGreg Kroah-Hartman 1687af224ca2SPeter Hurley /* uport == NULL if uart_port has already been removed */ 16880b1db830SJiri Slaby static void uart_port_shutdown(struct tty_port *port) 16890b1db830SJiri Slaby { 1690b922e19dSJiri Slaby struct uart_state *state = container_of(port, struct uart_state, port); 16914047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 1692b922e19dSJiri Slaby 1693b922e19dSJiri Slaby /* 1694b922e19dSJiri Slaby * clear delta_msr_wait queue to avoid mem leaks: we may free 1695b922e19dSJiri Slaby * the irq here so the queue might never be woken up. Note 1696b922e19dSJiri Slaby * that we won't end up waiting on delta_msr_wait again since 1697b922e19dSJiri Slaby * any outstanding file descriptors should be pointing at 1698b922e19dSJiri Slaby * hung_up_tty_fops now. 1699b922e19dSJiri Slaby */ 1700b922e19dSJiri Slaby wake_up_interruptible(&port->delta_msr_wait); 1701b922e19dSJiri Slaby 1702b922e19dSJiri Slaby /* 1703b922e19dSJiri Slaby * Free the IRQ and disable the port. 1704b922e19dSJiri Slaby */ 1705af224ca2SPeter Hurley if (uport) 1706b922e19dSJiri Slaby uport->ops->shutdown(uport); 1707b922e19dSJiri Slaby 1708b922e19dSJiri Slaby /* 1709b922e19dSJiri Slaby * Ensure that the IRQ handler isn't running on another CPU. 1710b922e19dSJiri Slaby */ 1711af224ca2SPeter Hurley if (uport) 1712b922e19dSJiri Slaby synchronize_irq(uport->irq); 17130b1db830SJiri Slaby } 17140b1db830SJiri Slaby 1715ab4382d2SGreg Kroah-Hartman static int uart_carrier_raised(struct tty_port *port) 1716ab4382d2SGreg Kroah-Hartman { 1717ab4382d2SGreg Kroah-Hartman struct uart_state *state = container_of(port, struct uart_state, port); 17189ed19428SPeter Hurley struct uart_port *uport; 1719ab4382d2SGreg Kroah-Hartman int mctrl; 17209ed19428SPeter Hurley 17219ed19428SPeter Hurley uport = uart_port_ref(state); 17229ed19428SPeter Hurley /* 17239ed19428SPeter Hurley * Should never observe uport == NULL since checks for hangup should 17249ed19428SPeter Hurley * abort the tty_port_block_til_ready() loop before checking for carrier 17259ed19428SPeter Hurley * raised -- but report carrier raised if it does anyway so open will 17269ed19428SPeter Hurley * continue and not sleep 17279ed19428SPeter Hurley */ 17289ed19428SPeter Hurley if (WARN_ON(!uport)) 17299ed19428SPeter Hurley return 1; 1730ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 17311fdc3106SAlexander Shiyan uart_enable_ms(uport); 1732ab4382d2SGreg Kroah-Hartman mctrl = uport->ops->get_mctrl(uport); 1733ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 17349ed19428SPeter Hurley uart_port_deref(uport); 1735ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_CAR) 1736ab4382d2SGreg Kroah-Hartman return 1; 1737ab4382d2SGreg Kroah-Hartman return 0; 1738ab4382d2SGreg Kroah-Hartman } 1739ab4382d2SGreg Kroah-Hartman 1740a6845e1eSRafael Gago static void uart_dtr_rts(struct tty_port *port, int raise) 1741ab4382d2SGreg Kroah-Hartman { 1742ab4382d2SGreg Kroah-Hartman struct uart_state *state = container_of(port, struct uart_state, port); 17439ed19428SPeter Hurley struct uart_port *uport; 17449ed19428SPeter Hurley 17459ed19428SPeter Hurley uport = uart_port_ref(state); 17469ed19428SPeter Hurley if (!uport) 17479ed19428SPeter Hurley return; 1748a6845e1eSRafael Gago uart_port_dtr_rts(uport, raise); 17499ed19428SPeter Hurley uart_port_deref(uport); 1750ab4382d2SGreg Kroah-Hartman } 1751ab4382d2SGreg Kroah-Hartman 17524cdd17baSJiri Slaby static int uart_install(struct tty_driver *driver, struct tty_struct *tty) 17534cdd17baSJiri Slaby { 17544cdd17baSJiri Slaby struct uart_driver *drv = driver->driver_state; 17554cdd17baSJiri Slaby struct uart_state *state = drv->state + tty->index; 17564cdd17baSJiri Slaby 17574cdd17baSJiri Slaby tty->driver_data = state; 17584cdd17baSJiri Slaby 17594cdd17baSJiri Slaby return tty_standard_install(driver, tty); 17604cdd17baSJiri Slaby } 17614cdd17baSJiri Slaby 1762ab4382d2SGreg Kroah-Hartman /* 1763ef4f527cSKevin Cernekee * Calls to uart_open are serialised by the tty_lock in 1764ef4f527cSKevin Cernekee * drivers/tty/tty_io.c:tty_open() 1765ab4382d2SGreg Kroah-Hartman * Note that if this fails, then uart_close() _will_ be called. 1766ab4382d2SGreg Kroah-Hartman * 1767ab4382d2SGreg Kroah-Hartman * In time, we want to scrap the "opening nonpresent ports" 1768ab4382d2SGreg Kroah-Hartman * behaviour and implement an alternative way for setserial 1769ab4382d2SGreg Kroah-Hartman * to set base addresses/ports/types. This will allow us to 1770ab4382d2SGreg Kroah-Hartman * get rid of a certain amount of extra tests. 1771ab4382d2SGreg Kroah-Hartman */ 1772ab4382d2SGreg Kroah-Hartman static int uart_open(struct tty_struct *tty, struct file *filp) 1773ab4382d2SGreg Kroah-Hartman { 17744cdd17baSJiri Slaby struct uart_state *state = tty->driver_data; 17754cdd17baSJiri Slaby int retval; 1776b3b57646SRob Herring 1777b3b57646SRob Herring retval = tty_port_open(&state->port, tty, filp); 1778b3b57646SRob Herring if (retval > 0) 1779b3b57646SRob Herring retval = 0; 1780b3b57646SRob Herring 1781b3b57646SRob Herring return retval; 1782b3b57646SRob Herring } 1783b3b57646SRob Herring 1784b3b57646SRob Herring static int uart_port_activate(struct tty_port *port, struct tty_struct *tty) 1785b3b57646SRob Herring { 1786b3b57646SRob Herring struct uart_state *state = container_of(port, struct uart_state, port); 1787b3b57646SRob Herring struct uart_port *uport; 178813b18d35SSerge Semin int ret; 1789b3b57646SRob Herring 1790b3b57646SRob Herring uport = uart_port_check(state); 1791b3b57646SRob Herring if (!uport || uport->flags & UPF_DEAD) 1792b3b57646SRob Herring return -ENXIO; 1793b3b57646SRob Herring 17944047b371SPeter Hurley port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0; 1795ab4382d2SGreg Kroah-Hartman 1796ab4382d2SGreg Kroah-Hartman /* 1797ab4382d2SGreg Kroah-Hartman * Start up the serial port. 1798ab4382d2SGreg Kroah-Hartman */ 179913b18d35SSerge Semin ret = uart_startup(tty, state, 0); 180013b18d35SSerge Semin if (ret > 0) 180113b18d35SSerge Semin tty_port_set_active(port, 1); 180213b18d35SSerge Semin 180313b18d35SSerge Semin return ret; 1804ab4382d2SGreg Kroah-Hartman } 1805ab4382d2SGreg Kroah-Hartman 1806ab4382d2SGreg Kroah-Hartman static const char *uart_type(struct uart_port *port) 1807ab4382d2SGreg Kroah-Hartman { 1808ab4382d2SGreg Kroah-Hartman const char *str = NULL; 1809ab4382d2SGreg Kroah-Hartman 1810ab4382d2SGreg Kroah-Hartman if (port->ops->type) 1811ab4382d2SGreg Kroah-Hartman str = port->ops->type(port); 1812ab4382d2SGreg Kroah-Hartman 1813ab4382d2SGreg Kroah-Hartman if (!str) 1814ab4382d2SGreg Kroah-Hartman str = "unknown"; 1815ab4382d2SGreg Kroah-Hartman 1816ab4382d2SGreg Kroah-Hartman return str; 1817ab4382d2SGreg Kroah-Hartman } 1818ab4382d2SGreg Kroah-Hartman 1819ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PROC_FS 1820ab4382d2SGreg Kroah-Hartman 1821ab4382d2SGreg Kroah-Hartman static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i) 1822ab4382d2SGreg Kroah-Hartman { 1823ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + i; 1824ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 18256f538fe3SLinus Walleij enum uart_pm_state pm_state; 18264047b371SPeter Hurley struct uart_port *uport; 1827ab4382d2SGreg Kroah-Hartman char stat_buf[32]; 1828ab4382d2SGreg Kroah-Hartman unsigned int status; 1829ab4382d2SGreg Kroah-Hartman int mmio; 1830ab4382d2SGreg Kroah-Hartman 18314047b371SPeter Hurley mutex_lock(&port->mutex); 18324047b371SPeter Hurley uport = uart_port_check(state); 1833ab4382d2SGreg Kroah-Hartman if (!uport) 18344047b371SPeter Hurley goto out; 1835ab4382d2SGreg Kroah-Hartman 1836ab4382d2SGreg Kroah-Hartman mmio = uport->iotype >= UPIO_MEM; 1837ab4382d2SGreg Kroah-Hartman seq_printf(m, "%d: uart:%s %s%08llX irq:%d", 1838ab4382d2SGreg Kroah-Hartman uport->line, uart_type(uport), 1839ab4382d2SGreg Kroah-Hartman mmio ? "mmio:0x" : "port:", 1840ab4382d2SGreg Kroah-Hartman mmio ? (unsigned long long)uport->mapbase 1841ab4382d2SGreg Kroah-Hartman : (unsigned long long)uport->iobase, 1842ab4382d2SGreg Kroah-Hartman uport->irq); 1843ab4382d2SGreg Kroah-Hartman 1844ab4382d2SGreg Kroah-Hartman if (uport->type == PORT_UNKNOWN) { 1845ab4382d2SGreg Kroah-Hartman seq_putc(m, '\n'); 18464047b371SPeter Hurley goto out; 1847ab4382d2SGreg Kroah-Hartman } 1848ab4382d2SGreg Kroah-Hartman 1849ab4382d2SGreg Kroah-Hartman if (capable(CAP_SYS_ADMIN)) { 1850ab4382d2SGreg Kroah-Hartman pm_state = state->pm_state; 18516f538fe3SLinus Walleij if (pm_state != UART_PM_STATE_ON) 18526f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_ON); 1853ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 1854ab4382d2SGreg Kroah-Hartman status = uport->ops->get_mctrl(uport); 1855ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 18566f538fe3SLinus Walleij if (pm_state != UART_PM_STATE_ON) 1857ab4382d2SGreg Kroah-Hartman uart_change_pm(state, pm_state); 1858ab4382d2SGreg Kroah-Hartman 1859ab4382d2SGreg Kroah-Hartman seq_printf(m, " tx:%d rx:%d", 1860ab4382d2SGreg Kroah-Hartman uport->icount.tx, uport->icount.rx); 1861ab4382d2SGreg Kroah-Hartman if (uport->icount.frame) 1862968af298SPeter Hurley seq_printf(m, " fe:%d", uport->icount.frame); 1863ab4382d2SGreg Kroah-Hartman if (uport->icount.parity) 1864968af298SPeter Hurley seq_printf(m, " pe:%d", uport->icount.parity); 1865ab4382d2SGreg Kroah-Hartman if (uport->icount.brk) 1866968af298SPeter Hurley seq_printf(m, " brk:%d", uport->icount.brk); 1867ab4382d2SGreg Kroah-Hartman if (uport->icount.overrun) 1868968af298SPeter Hurley seq_printf(m, " oe:%d", uport->icount.overrun); 18694f794097SJeremy Kerr if (uport->icount.buf_overrun) 18704f794097SJeremy Kerr seq_printf(m, " bo:%d", uport->icount.buf_overrun); 1871ab4382d2SGreg Kroah-Hartman 1872ab4382d2SGreg Kroah-Hartman #define INFOBIT(bit, str) \ 1873ab4382d2SGreg Kroah-Hartman if (uport->mctrl & (bit)) \ 1874ab4382d2SGreg Kroah-Hartman strncat(stat_buf, (str), sizeof(stat_buf) - \ 1875ab4382d2SGreg Kroah-Hartman strlen(stat_buf) - 2) 1876ab4382d2SGreg Kroah-Hartman #define STATBIT(bit, str) \ 1877ab4382d2SGreg Kroah-Hartman if (status & (bit)) \ 1878ab4382d2SGreg Kroah-Hartman strncat(stat_buf, (str), sizeof(stat_buf) - \ 1879ab4382d2SGreg Kroah-Hartman strlen(stat_buf) - 2) 1880ab4382d2SGreg Kroah-Hartman 1881ab4382d2SGreg Kroah-Hartman stat_buf[0] = '\0'; 1882ab4382d2SGreg Kroah-Hartman stat_buf[1] = '\0'; 1883ab4382d2SGreg Kroah-Hartman INFOBIT(TIOCM_RTS, "|RTS"); 1884ab4382d2SGreg Kroah-Hartman STATBIT(TIOCM_CTS, "|CTS"); 1885ab4382d2SGreg Kroah-Hartman INFOBIT(TIOCM_DTR, "|DTR"); 1886ab4382d2SGreg Kroah-Hartman STATBIT(TIOCM_DSR, "|DSR"); 1887ab4382d2SGreg Kroah-Hartman STATBIT(TIOCM_CAR, "|CD"); 1888ab4382d2SGreg Kroah-Hartman STATBIT(TIOCM_RNG, "|RI"); 1889ab4382d2SGreg Kroah-Hartman if (stat_buf[0]) 1890ab4382d2SGreg Kroah-Hartman stat_buf[0] = ' '; 1891ab4382d2SGreg Kroah-Hartman 1892ab4382d2SGreg Kroah-Hartman seq_puts(m, stat_buf); 1893ab4382d2SGreg Kroah-Hartman } 1894ab4382d2SGreg Kroah-Hartman seq_putc(m, '\n'); 1895ab4382d2SGreg Kroah-Hartman #undef STATBIT 1896ab4382d2SGreg Kroah-Hartman #undef INFOBIT 18974047b371SPeter Hurley out: 18984047b371SPeter Hurley mutex_unlock(&port->mutex); 1899ab4382d2SGreg Kroah-Hartman } 1900ab4382d2SGreg Kroah-Hartman 1901ab4382d2SGreg Kroah-Hartman static int uart_proc_show(struct seq_file *m, void *v) 1902ab4382d2SGreg Kroah-Hartman { 1903ab4382d2SGreg Kroah-Hartman struct tty_driver *ttydrv = m->private; 1904ab4382d2SGreg Kroah-Hartman struct uart_driver *drv = ttydrv->driver_state; 1905ab4382d2SGreg Kroah-Hartman int i; 1906ab4382d2SGreg Kroah-Hartman 1907968af298SPeter Hurley seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", ""); 1908ab4382d2SGreg Kroah-Hartman for (i = 0; i < drv->nr; i++) 1909ab4382d2SGreg Kroah-Hartman uart_line_info(m, drv, i); 1910ab4382d2SGreg Kroah-Hartman return 0; 1911ab4382d2SGreg Kroah-Hartman } 1912ab4382d2SGreg Kroah-Hartman #endif 1913ab4382d2SGreg Kroah-Hartman 19147a49955aSAndy Shevchenko static inline bool uart_console_enabled(struct uart_port *port) 19157a49955aSAndy Shevchenko { 19167a49955aSAndy Shevchenko return uart_console(port) && (port->cons->flags & CON_ENABLED); 19177a49955aSAndy Shevchenko } 19187a49955aSAndy Shevchenko 1919f743061aSAndy Shevchenko static void __uart_port_spin_lock_init(struct uart_port *port) 1920f743061aSAndy Shevchenko { 1921f743061aSAndy Shevchenko spin_lock_init(&port->lock); 1922f743061aSAndy Shevchenko lockdep_set_class(&port->lock, &port_lock_key); 1923f743061aSAndy Shevchenko } 1924f743061aSAndy Shevchenko 1925d2403cadSAndy Shevchenko /* 1926d2403cadSAndy Shevchenko * Ensure that the serial console lock is initialised early. 1927d2403cadSAndy Shevchenko * If this port is a console, then the spinlock is already initialised. 1928d2403cadSAndy Shevchenko */ 1929d2403cadSAndy Shevchenko static inline void uart_port_spin_lock_init(struct uart_port *port) 1930d2403cadSAndy Shevchenko { 1931a3cb39d2SAndy Shevchenko if (uart_console(port)) 1932d2403cadSAndy Shevchenko return; 1933d2403cadSAndy Shevchenko 1934f743061aSAndy Shevchenko __uart_port_spin_lock_init(port); 1935d2403cadSAndy Shevchenko } 1936d2403cadSAndy Shevchenko 1937ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL) 19381cfe42b7SPeter Hurley /** 1939ab4382d2SGreg Kroah-Hartman * uart_console_write - write a console message to a serial port 1940ab4382d2SGreg Kroah-Hartman * @port: the port to write the message 1941ab4382d2SGreg Kroah-Hartman * @s: array of characters 1942ab4382d2SGreg Kroah-Hartman * @count: number of characters in string to write 194310afbe34SPeter Hurley * @putchar: function to write character to port 1944ab4382d2SGreg Kroah-Hartman */ 1945ab4382d2SGreg Kroah-Hartman void uart_console_write(struct uart_port *port, const char *s, 1946ab4382d2SGreg Kroah-Hartman unsigned int count, 1947ab4382d2SGreg Kroah-Hartman void (*putchar)(struct uart_port *, int)) 1948ab4382d2SGreg Kroah-Hartman { 1949ab4382d2SGreg Kroah-Hartman unsigned int i; 1950ab4382d2SGreg Kroah-Hartman 1951ab4382d2SGreg Kroah-Hartman for (i = 0; i < count; i++, s++) { 1952ab4382d2SGreg Kroah-Hartman if (*s == '\n') 1953ab4382d2SGreg Kroah-Hartman putchar(port, '\r'); 1954ab4382d2SGreg Kroah-Hartman putchar(port, *s); 1955ab4382d2SGreg Kroah-Hartman } 1956ab4382d2SGreg Kroah-Hartman } 1957ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_console_write); 1958ab4382d2SGreg Kroah-Hartman 1959ab4382d2SGreg Kroah-Hartman /* 1960ab4382d2SGreg Kroah-Hartman * Check whether an invalid uart number has been specified, and 1961ab4382d2SGreg Kroah-Hartman * if so, search for the first available port that does have 1962ab4382d2SGreg Kroah-Hartman * console support. 1963ab4382d2SGreg Kroah-Hartman */ 1964ab4382d2SGreg Kroah-Hartman struct uart_port * __init 1965ab4382d2SGreg Kroah-Hartman uart_get_console(struct uart_port *ports, int nr, struct console *co) 1966ab4382d2SGreg Kroah-Hartman { 1967ab4382d2SGreg Kroah-Hartman int idx = co->index; 1968ab4382d2SGreg Kroah-Hartman 1969ab4382d2SGreg Kroah-Hartman if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 && 1970ab4382d2SGreg Kroah-Hartman ports[idx].membase == NULL)) 1971ab4382d2SGreg Kroah-Hartman for (idx = 0; idx < nr; idx++) 1972ab4382d2SGreg Kroah-Hartman if (ports[idx].iobase != 0 || 1973ab4382d2SGreg Kroah-Hartman ports[idx].membase != NULL) 1974ab4382d2SGreg Kroah-Hartman break; 1975ab4382d2SGreg Kroah-Hartman 1976ab4382d2SGreg Kroah-Hartman co->index = idx; 1977ab4382d2SGreg Kroah-Hartman 1978ab4382d2SGreg Kroah-Hartman return ports + idx; 1979ab4382d2SGreg Kroah-Hartman } 1980ab4382d2SGreg Kroah-Hartman 1981ab4382d2SGreg Kroah-Hartman /** 198273abaf87SPeter Hurley * uart_parse_earlycon - Parse earlycon options 198373abaf87SPeter Hurley * @p: ptr to 2nd field (ie., just beyond '<name>,') 198473abaf87SPeter Hurley * @iotype: ptr for decoded iotype (out) 198573abaf87SPeter Hurley * @addr: ptr for decoded mapbase/iobase (out) 198673abaf87SPeter Hurley * @options: ptr for <options> field; NULL if not present (out) 198773abaf87SPeter Hurley * 198873abaf87SPeter Hurley * Decodes earlycon kernel command line parameters of the form 1989bd94c407SMasahiro Yamada * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options> 1990bd94c407SMasahiro Yamada * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options> 199173abaf87SPeter Hurley * 199273abaf87SPeter Hurley * The optional form 1993ff30283aSRandy Dunlap * 199473abaf87SPeter Hurley * earlycon=<name>,0x<addr>,<options> 199573abaf87SPeter Hurley * console=<name>,0x<addr>,<options> 1996ff30283aSRandy Dunlap * 199773abaf87SPeter Hurley * is also accepted; the returned @iotype will be UPIO_MEM. 199873abaf87SPeter Hurley * 19998b2303deSAlexander Sverdlin * Returns 0 on success or -EINVAL on failure 200073abaf87SPeter Hurley */ 200146e36683SAlexander Sverdlin int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr, 200273abaf87SPeter Hurley char **options) 200373abaf87SPeter Hurley { 200473abaf87SPeter Hurley if (strncmp(p, "mmio,", 5) == 0) { 200573abaf87SPeter Hurley *iotype = UPIO_MEM; 200673abaf87SPeter Hurley p += 5; 2007bd94c407SMasahiro Yamada } else if (strncmp(p, "mmio16,", 7) == 0) { 2008bd94c407SMasahiro Yamada *iotype = UPIO_MEM16; 2009bd94c407SMasahiro Yamada p += 7; 201073abaf87SPeter Hurley } else if (strncmp(p, "mmio32,", 7) == 0) { 201173abaf87SPeter Hurley *iotype = UPIO_MEM32; 201273abaf87SPeter Hurley p += 7; 20136e63be3fSNoam Camus } else if (strncmp(p, "mmio32be,", 9) == 0) { 20146e63be3fSNoam Camus *iotype = UPIO_MEM32BE; 20156e63be3fSNoam Camus p += 9; 2016d215d809SMax Filippov } else if (strncmp(p, "mmio32native,", 13) == 0) { 2017d215d809SMax Filippov *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ? 2018d215d809SMax Filippov UPIO_MEM32BE : UPIO_MEM32; 2019d215d809SMax Filippov p += 13; 202073abaf87SPeter Hurley } else if (strncmp(p, "io,", 3) == 0) { 202173abaf87SPeter Hurley *iotype = UPIO_PORT; 202273abaf87SPeter Hurley p += 3; 202373abaf87SPeter Hurley } else if (strncmp(p, "0x", 2) == 0) { 202473abaf87SPeter Hurley *iotype = UPIO_MEM; 202573abaf87SPeter Hurley } else { 202673abaf87SPeter Hurley return -EINVAL; 202773abaf87SPeter Hurley } 202873abaf87SPeter Hurley 20298b2303deSAlexander Sverdlin /* 20308b2303deSAlexander Sverdlin * Before you replace it with kstrtoull(), think about options separator 20318b2303deSAlexander Sverdlin * (',') it will not tolerate 20328b2303deSAlexander Sverdlin */ 20338b2303deSAlexander Sverdlin *addr = simple_strtoull(p, NULL, 0); 203473abaf87SPeter Hurley p = strchr(p, ','); 203573abaf87SPeter Hurley if (p) 203673abaf87SPeter Hurley p++; 203773abaf87SPeter Hurley 203873abaf87SPeter Hurley *options = p; 203973abaf87SPeter Hurley return 0; 204073abaf87SPeter Hurley } 204173abaf87SPeter Hurley EXPORT_SYMBOL_GPL(uart_parse_earlycon); 204273abaf87SPeter Hurley 204373abaf87SPeter Hurley /** 204402088ca6SGeert Uytterhoeven * uart_parse_options - Parse serial port baud/parity/bits/flow control. 2045ab4382d2SGreg Kroah-Hartman * @options: pointer to option string 2046ab4382d2SGreg Kroah-Hartman * @baud: pointer to an 'int' variable for the baud rate. 2047ab4382d2SGreg Kroah-Hartman * @parity: pointer to an 'int' variable for the parity. 2048ab4382d2SGreg Kroah-Hartman * @bits: pointer to an 'int' variable for the number of data bits. 2049ab4382d2SGreg Kroah-Hartman * @flow: pointer to an 'int' variable for the flow control character. 2050ab4382d2SGreg Kroah-Hartman * 2051ab4382d2SGreg Kroah-Hartman * uart_parse_options decodes a string containing the serial console 2052ab4382d2SGreg Kroah-Hartman * options. The format of the string is <baud><parity><bits><flow>, 2053ab4382d2SGreg Kroah-Hartman * eg: 115200n8r 2054ab4382d2SGreg Kroah-Hartman */ 2055ab4382d2SGreg Kroah-Hartman void 20560f646b63SPaul Cercueil uart_parse_options(const char *options, int *baud, int *parity, 20570f646b63SPaul Cercueil int *bits, int *flow) 2058ab4382d2SGreg Kroah-Hartman { 20590f646b63SPaul Cercueil const char *s = options; 2060ab4382d2SGreg Kroah-Hartman 2061ab4382d2SGreg Kroah-Hartman *baud = simple_strtoul(s, NULL, 10); 2062ab4382d2SGreg Kroah-Hartman while (*s >= '0' && *s <= '9') 2063ab4382d2SGreg Kroah-Hartman s++; 2064ab4382d2SGreg Kroah-Hartman if (*s) 2065ab4382d2SGreg Kroah-Hartman *parity = *s++; 2066ab4382d2SGreg Kroah-Hartman if (*s) 2067ab4382d2SGreg Kroah-Hartman *bits = *s++ - '0'; 2068ab4382d2SGreg Kroah-Hartman if (*s) 2069ab4382d2SGreg Kroah-Hartman *flow = *s; 2070ab4382d2SGreg Kroah-Hartman } 2071ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_parse_options); 2072ab4382d2SGreg Kroah-Hartman 2073ab4382d2SGreg Kroah-Hartman /** 2074ab4382d2SGreg Kroah-Hartman * uart_set_options - setup the serial console parameters 2075ab4382d2SGreg Kroah-Hartman * @port: pointer to the serial ports uart_port structure 2076ab4382d2SGreg Kroah-Hartman * @co: console pointer 2077ab4382d2SGreg Kroah-Hartman * @baud: baud rate 2078ab4382d2SGreg Kroah-Hartman * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even) 2079ab4382d2SGreg Kroah-Hartman * @bits: number of data bits 2080ab4382d2SGreg Kroah-Hartman * @flow: flow control character - 'r' (rts) 2081ab4382d2SGreg Kroah-Hartman */ 2082ab4382d2SGreg Kroah-Hartman int 2083ab4382d2SGreg Kroah-Hartman uart_set_options(struct uart_port *port, struct console *co, 2084ab4382d2SGreg Kroah-Hartman int baud, int parity, int bits, int flow) 2085ab4382d2SGreg Kroah-Hartman { 2086ab4382d2SGreg Kroah-Hartman struct ktermios termios; 2087ab4382d2SGreg Kroah-Hartman static struct ktermios dummy; 2088ab4382d2SGreg Kroah-Hartman 2089d2403cadSAndy Shevchenko uart_port_spin_lock_init(port); 2090ab4382d2SGreg Kroah-Hartman 2091ab4382d2SGreg Kroah-Hartman memset(&termios, 0, sizeof(struct ktermios)); 2092ab4382d2SGreg Kroah-Hartman 2093ba47f97aSJeffy Chen termios.c_cflag |= CREAD | HUPCL | CLOCAL; 2094ba47f97aSJeffy Chen tty_termios_encode_baud_rate(&termios, baud, baud); 2095ab4382d2SGreg Kroah-Hartman 2096ab4382d2SGreg Kroah-Hartman if (bits == 7) 2097ab4382d2SGreg Kroah-Hartman termios.c_cflag |= CS7; 2098ab4382d2SGreg Kroah-Hartman else 2099ab4382d2SGreg Kroah-Hartman termios.c_cflag |= CS8; 2100ab4382d2SGreg Kroah-Hartman 2101ab4382d2SGreg Kroah-Hartman switch (parity) { 2102ab4382d2SGreg Kroah-Hartman case 'o': case 'O': 2103ab4382d2SGreg Kroah-Hartman termios.c_cflag |= PARODD; 2104df561f66SGustavo A. R. Silva fallthrough; 2105ab4382d2SGreg Kroah-Hartman case 'e': case 'E': 2106ab4382d2SGreg Kroah-Hartman termios.c_cflag |= PARENB; 2107ab4382d2SGreg Kroah-Hartman break; 2108ab4382d2SGreg Kroah-Hartman } 2109ab4382d2SGreg Kroah-Hartman 2110ab4382d2SGreg Kroah-Hartman if (flow == 'r') 2111ab4382d2SGreg Kroah-Hartman termios.c_cflag |= CRTSCTS; 2112ab4382d2SGreg Kroah-Hartman 2113ab4382d2SGreg Kroah-Hartman /* 2114ab4382d2SGreg Kroah-Hartman * some uarts on other side don't support no flow control. 2115ab4382d2SGreg Kroah-Hartman * So we set * DTR in host uart to make them happy 2116ab4382d2SGreg Kroah-Hartman */ 2117ab4382d2SGreg Kroah-Hartman port->mctrl |= TIOCM_DTR; 2118ab4382d2SGreg Kroah-Hartman 2119ab4382d2SGreg Kroah-Hartman port->ops->set_termios(port, &termios, &dummy); 2120ab4382d2SGreg Kroah-Hartman /* 2121ab4382d2SGreg Kroah-Hartman * Allow the setting of the UART parameters with a NULL console 2122ab4382d2SGreg Kroah-Hartman * too: 2123ab4382d2SGreg Kroah-Hartman */ 2124ab4382d2SGreg Kroah-Hartman if (co) 2125ab4382d2SGreg Kroah-Hartman co->cflag = termios.c_cflag; 2126ab4382d2SGreg Kroah-Hartman 2127ab4382d2SGreg Kroah-Hartman return 0; 2128ab4382d2SGreg Kroah-Hartman } 2129ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_set_options); 2130ab4382d2SGreg Kroah-Hartman #endif /* CONFIG_SERIAL_CORE_CONSOLE */ 2131ab4382d2SGreg Kroah-Hartman 2132cf75525fSJiri Slaby /** 2133cf75525fSJiri Slaby * uart_change_pm - set power state of the port 2134cf75525fSJiri Slaby * 2135cf75525fSJiri Slaby * @state: port descriptor 2136cf75525fSJiri Slaby * @pm_state: new state 2137cf75525fSJiri Slaby * 2138cf75525fSJiri Slaby * Locking: port->mutex has to be held 2139cf75525fSJiri Slaby */ 21406f538fe3SLinus Walleij static void uart_change_pm(struct uart_state *state, 21416f538fe3SLinus Walleij enum uart_pm_state pm_state) 2142ab4382d2SGreg Kroah-Hartman { 21434047b371SPeter Hurley struct uart_port *port = uart_port_check(state); 2144ab4382d2SGreg Kroah-Hartman 2145ab4382d2SGreg Kroah-Hartman if (state->pm_state != pm_state) { 21464047b371SPeter Hurley if (port && port->ops->pm) 2147ab4382d2SGreg Kroah-Hartman port->ops->pm(port, pm_state, state->pm_state); 2148ab4382d2SGreg Kroah-Hartman state->pm_state = pm_state; 2149ab4382d2SGreg Kroah-Hartman } 2150ab4382d2SGreg Kroah-Hartman } 2151ab4382d2SGreg Kroah-Hartman 2152ab4382d2SGreg Kroah-Hartman struct uart_match { 2153ab4382d2SGreg Kroah-Hartman struct uart_port *port; 2154ab4382d2SGreg Kroah-Hartman struct uart_driver *driver; 2155ab4382d2SGreg Kroah-Hartman }; 2156ab4382d2SGreg Kroah-Hartman 2157ab4382d2SGreg Kroah-Hartman static int serial_match_port(struct device *dev, void *data) 2158ab4382d2SGreg Kroah-Hartman { 2159ab4382d2SGreg Kroah-Hartman struct uart_match *match = data; 2160ab4382d2SGreg Kroah-Hartman struct tty_driver *tty_drv = match->driver->tty_driver; 2161ab4382d2SGreg Kroah-Hartman dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) + 2162ab4382d2SGreg Kroah-Hartman match->port->line; 2163ab4382d2SGreg Kroah-Hartman 2164ab4382d2SGreg Kroah-Hartman return dev->devt == devt; /* Actually, only one tty per port */ 2165ab4382d2SGreg Kroah-Hartman } 2166ab4382d2SGreg Kroah-Hartman 2167ab4382d2SGreg Kroah-Hartman int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport) 2168ab4382d2SGreg Kroah-Hartman { 2169ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + uport->line; 2170ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 2171ab4382d2SGreg Kroah-Hartman struct device *tty_dev; 2172ab4382d2SGreg Kroah-Hartman struct uart_match match = {uport, drv}; 2173ab4382d2SGreg Kroah-Hartman 2174ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 2175ab4382d2SGreg Kroah-Hartman 2176ab4382d2SGreg Kroah-Hartman tty_dev = device_find_child(uport->dev, &match, serial_match_port); 217788e2582eSLucas Stach if (tty_dev && device_may_wakeup(tty_dev)) { 2178aef3ad10SAndy Shevchenko enable_irq_wake(uport->irq); 2179ab4382d2SGreg Kroah-Hartman put_device(tty_dev); 2180ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2181ab4382d2SGreg Kroah-Hartman return 0; 2182ab4382d2SGreg Kroah-Hartman } 21835a65dcc0SFederico Vaga put_device(tty_dev); 21845a65dcc0SFederico Vaga 2185b164c972SPeter Hurley /* Nothing to do if the console is not suspending */ 2186b164c972SPeter Hurley if (!console_suspend_enabled && uart_console(uport)) 2187b164c972SPeter Hurley goto unlock; 2188b164c972SPeter Hurley 2189ab4382d2SGreg Kroah-Hartman uport->suspended = 1; 2190ab4382d2SGreg Kroah-Hartman 2191d41861caSPeter Hurley if (tty_port_initialized(port)) { 2192ab4382d2SGreg Kroah-Hartman const struct uart_ops *ops = uport->ops; 2193ab4382d2SGreg Kroah-Hartman int tries; 2194ab4382d2SGreg Kroah-Hartman 219580f02d54SPeter Hurley tty_port_set_suspended(port, 1); 2196d41861caSPeter Hurley tty_port_set_initialized(port, 0); 2197ab4382d2SGreg Kroah-Hartman 2198ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 2199ab4382d2SGreg Kroah-Hartman ops->stop_tx(uport); 2200ab4382d2SGreg Kroah-Hartman ops->set_mctrl(uport, 0); 2201ab4382d2SGreg Kroah-Hartman ops->stop_rx(uport); 2202ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 2203ab4382d2SGreg Kroah-Hartman 2204ab4382d2SGreg Kroah-Hartman /* 2205ab4382d2SGreg Kroah-Hartman * Wait for the transmitter to empty. 2206ab4382d2SGreg Kroah-Hartman */ 2207ab4382d2SGreg Kroah-Hartman for (tries = 3; !ops->tx_empty(uport) && tries; tries--) 2208ab4382d2SGreg Kroah-Hartman msleep(10); 2209ab4382d2SGreg Kroah-Hartman if (!tries) 2210cade3580SAndy Shevchenko dev_err(uport->dev, "%s: Unable to drain transmitter\n", 2211cade3580SAndy Shevchenko uport->name); 2212ab4382d2SGreg Kroah-Hartman 2213ab4382d2SGreg Kroah-Hartman ops->shutdown(uport); 2214ab4382d2SGreg Kroah-Hartman } 2215ab4382d2SGreg Kroah-Hartman 2216ab4382d2SGreg Kroah-Hartman /* 2217ab4382d2SGreg Kroah-Hartman * Disable the console device before suspending. 2218ab4382d2SGreg Kroah-Hartman */ 2219b164c972SPeter Hurley if (uart_console(uport)) 2220ab4382d2SGreg Kroah-Hartman console_stop(uport->cons); 2221ab4382d2SGreg Kroah-Hartman 22226f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_OFF); 2223b164c972SPeter Hurley unlock: 2224ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2225ab4382d2SGreg Kroah-Hartman 2226ab4382d2SGreg Kroah-Hartman return 0; 2227ab4382d2SGreg Kroah-Hartman } 2228ab4382d2SGreg Kroah-Hartman 2229ab4382d2SGreg Kroah-Hartman int uart_resume_port(struct uart_driver *drv, struct uart_port *uport) 2230ab4382d2SGreg Kroah-Hartman { 2231ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + uport->line; 2232ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 2233ab4382d2SGreg Kroah-Hartman struct device *tty_dev; 2234ab4382d2SGreg Kroah-Hartman struct uart_match match = {uport, drv}; 2235ab4382d2SGreg Kroah-Hartman struct ktermios termios; 2236ab4382d2SGreg Kroah-Hartman 2237ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 2238ab4382d2SGreg Kroah-Hartman 2239ab4382d2SGreg Kroah-Hartman tty_dev = device_find_child(uport->dev, &match, serial_match_port); 2240ab4382d2SGreg Kroah-Hartman if (!uport->suspended && device_may_wakeup(tty_dev)) { 2241aef3ad10SAndy Shevchenko if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq)))) 2242ab4382d2SGreg Kroah-Hartman disable_irq_wake(uport->irq); 22435a65dcc0SFederico Vaga put_device(tty_dev); 2244ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2245ab4382d2SGreg Kroah-Hartman return 0; 2246ab4382d2SGreg Kroah-Hartman } 22475a65dcc0SFederico Vaga put_device(tty_dev); 2248ab4382d2SGreg Kroah-Hartman uport->suspended = 0; 2249ab4382d2SGreg Kroah-Hartman 2250ab4382d2SGreg Kroah-Hartman /* 2251ab4382d2SGreg Kroah-Hartman * Re-enable the console device after suspending. 2252ab4382d2SGreg Kroah-Hartman */ 22535933a161SYin Kangkai if (uart_console(uport)) { 2254ab4382d2SGreg Kroah-Hartman /* 2255ab4382d2SGreg Kroah-Hartman * First try to use the console cflag setting. 2256ab4382d2SGreg Kroah-Hartman */ 2257ab4382d2SGreg Kroah-Hartman memset(&termios, 0, sizeof(struct ktermios)); 2258ab4382d2SGreg Kroah-Hartman termios.c_cflag = uport->cons->cflag; 2259ab4382d2SGreg Kroah-Hartman 2260ab4382d2SGreg Kroah-Hartman /* 2261ab4382d2SGreg Kroah-Hartman * If that's unset, use the tty termios setting. 2262ab4382d2SGreg Kroah-Hartman */ 2263adc8d746SAlan Cox if (port->tty && termios.c_cflag == 0) 2264adc8d746SAlan Cox termios = port->tty->termios; 2265ab4382d2SGreg Kroah-Hartman 226694abc56fSNing Jiang if (console_suspend_enabled) 22676f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_ON); 2268ab4382d2SGreg Kroah-Hartman uport->ops->set_termios(uport, &termios, NULL); 22695933a161SYin Kangkai if (console_suspend_enabled) 2270ab4382d2SGreg Kroah-Hartman console_start(uport->cons); 2271ab4382d2SGreg Kroah-Hartman } 2272ab4382d2SGreg Kroah-Hartman 227380f02d54SPeter Hurley if (tty_port_suspended(port)) { 2274ab4382d2SGreg Kroah-Hartman const struct uart_ops *ops = uport->ops; 2275ab4382d2SGreg Kroah-Hartman int ret; 2276ab4382d2SGreg Kroah-Hartman 22776f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_ON); 2278ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 2279ab4382d2SGreg Kroah-Hartman ops->set_mctrl(uport, 0); 2280ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 2281ab4382d2SGreg Kroah-Hartman if (console_suspend_enabled || !uart_console(uport)) { 2282ab4382d2SGreg Kroah-Hartman /* Protected by port mutex for now */ 2283ab4382d2SGreg Kroah-Hartman struct tty_struct *tty = port->tty; 22844ed71addSTamseel Shams 2285ab4382d2SGreg Kroah-Hartman ret = ops->startup(uport); 2286ab4382d2SGreg Kroah-Hartman if (ret == 0) { 2287ab4382d2SGreg Kroah-Hartman if (tty) 2288ab4382d2SGreg Kroah-Hartman uart_change_speed(tty, state, NULL); 2289ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 2290ab4382d2SGreg Kroah-Hartman ops->set_mctrl(uport, uport->mctrl); 2291ab4382d2SGreg Kroah-Hartman ops->start_tx(uport); 2292ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 2293d41861caSPeter Hurley tty_port_set_initialized(port, 1); 2294ab4382d2SGreg Kroah-Hartman } else { 2295ab4382d2SGreg Kroah-Hartman /* 2296ab4382d2SGreg Kroah-Hartman * Failed to resume - maybe hardware went away? 2297ab4382d2SGreg Kroah-Hartman * Clear the "initialized" flag so we won't try 2298ab4382d2SGreg Kroah-Hartman * to call the low level drivers shutdown method. 2299ab4382d2SGreg Kroah-Hartman */ 2300ab4382d2SGreg Kroah-Hartman uart_shutdown(tty, state); 2301ab4382d2SGreg Kroah-Hartman } 2302ab4382d2SGreg Kroah-Hartman } 2303ab4382d2SGreg Kroah-Hartman 230480f02d54SPeter Hurley tty_port_set_suspended(port, 0); 2305ab4382d2SGreg Kroah-Hartman } 2306ab4382d2SGreg Kroah-Hartman 2307ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2308ab4382d2SGreg Kroah-Hartman 2309ab4382d2SGreg Kroah-Hartman return 0; 2310ab4382d2SGreg Kroah-Hartman } 2311ab4382d2SGreg Kroah-Hartman 2312ab4382d2SGreg Kroah-Hartman static inline void 2313ab4382d2SGreg Kroah-Hartman uart_report_port(struct uart_driver *drv, struct uart_port *port) 2314ab4382d2SGreg Kroah-Hartman { 2315ab4382d2SGreg Kroah-Hartman char address[64]; 2316ab4382d2SGreg Kroah-Hartman 2317ab4382d2SGreg Kroah-Hartman switch (port->iotype) { 2318ab4382d2SGreg Kroah-Hartman case UPIO_PORT: 2319ab4382d2SGreg Kroah-Hartman snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase); 2320ab4382d2SGreg Kroah-Hartman break; 2321ab4382d2SGreg Kroah-Hartman case UPIO_HUB6: 2322ab4382d2SGreg Kroah-Hartman snprintf(address, sizeof(address), 2323ab4382d2SGreg Kroah-Hartman "I/O 0x%lx offset 0x%x", port->iobase, port->hub6); 2324ab4382d2SGreg Kroah-Hartman break; 2325ab4382d2SGreg Kroah-Hartman case UPIO_MEM: 2326bd94c407SMasahiro Yamada case UPIO_MEM16: 2327ab4382d2SGreg Kroah-Hartman case UPIO_MEM32: 23283ffb1a81SKevin Cernekee case UPIO_MEM32BE: 2329ab4382d2SGreg Kroah-Hartman case UPIO_AU: 2330ab4382d2SGreg Kroah-Hartman case UPIO_TSI: 2331ab4382d2SGreg Kroah-Hartman snprintf(address, sizeof(address), 2332ab4382d2SGreg Kroah-Hartman "MMIO 0x%llx", (unsigned long long)port->mapbase); 2333ab4382d2SGreg Kroah-Hartman break; 2334ab4382d2SGreg Kroah-Hartman default: 2335ab4382d2SGreg Kroah-Hartman strlcpy(address, "*unknown*", sizeof(address)); 2336ab4382d2SGreg Kroah-Hartman break; 2337ab4382d2SGreg Kroah-Hartman } 2338ab4382d2SGreg Kroah-Hartman 2339cade3580SAndy Shevchenko pr_info("%s%s%s at %s (irq = %d, base_baud = %d) is a %s\n", 234068ed7e1cSJames Bottomley port->dev ? dev_name(port->dev) : "", 234168ed7e1cSJames Bottomley port->dev ? ": " : "", 2342cade3580SAndy Shevchenko port->name, 23437d12b976SKees Cook address, port->irq, port->uartclk / 16, uart_type(port)); 2344ab4382d2SGreg Kroah-Hartman } 2345ab4382d2SGreg Kroah-Hartman 2346ab4382d2SGreg Kroah-Hartman static void 2347ab4382d2SGreg Kroah-Hartman uart_configure_port(struct uart_driver *drv, struct uart_state *state, 2348ab4382d2SGreg Kroah-Hartman struct uart_port *port) 2349ab4382d2SGreg Kroah-Hartman { 2350ab4382d2SGreg Kroah-Hartman unsigned int flags; 2351ab4382d2SGreg Kroah-Hartman 2352ab4382d2SGreg Kroah-Hartman /* 2353ab4382d2SGreg Kroah-Hartman * If there isn't a port here, don't do anything further. 2354ab4382d2SGreg Kroah-Hartman */ 2355ab4382d2SGreg Kroah-Hartman if (!port->iobase && !port->mapbase && !port->membase) 2356ab4382d2SGreg Kroah-Hartman return; 2357ab4382d2SGreg Kroah-Hartman 2358ab4382d2SGreg Kroah-Hartman /* 2359ab4382d2SGreg Kroah-Hartman * Now do the auto configuration stuff. Note that config_port 2360ab4382d2SGreg Kroah-Hartman * is expected to claim the resources and map the port for us. 2361ab4382d2SGreg Kroah-Hartman */ 2362ab4382d2SGreg Kroah-Hartman flags = 0; 2363ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_AUTO_IRQ) 2364ab4382d2SGreg Kroah-Hartman flags |= UART_CONFIG_IRQ; 2365ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_BOOT_AUTOCONF) { 2366ab4382d2SGreg Kroah-Hartman if (!(port->flags & UPF_FIXED_TYPE)) { 2367ab4382d2SGreg Kroah-Hartman port->type = PORT_UNKNOWN; 2368ab4382d2SGreg Kroah-Hartman flags |= UART_CONFIG_TYPE; 2369ab4382d2SGreg Kroah-Hartman } 2370ab4382d2SGreg Kroah-Hartman port->ops->config_port(port, flags); 2371ab4382d2SGreg Kroah-Hartman } 2372ab4382d2SGreg Kroah-Hartman 2373ab4382d2SGreg Kroah-Hartman if (port->type != PORT_UNKNOWN) { 2374ab4382d2SGreg Kroah-Hartman unsigned long flags; 2375ab4382d2SGreg Kroah-Hartman 2376ab4382d2SGreg Kroah-Hartman uart_report_port(drv, port); 2377ab4382d2SGreg Kroah-Hartman 2378ab4382d2SGreg Kroah-Hartman /* Power up port for set_mctrl() */ 23796f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_ON); 2380ab4382d2SGreg Kroah-Hartman 2381ab4382d2SGreg Kroah-Hartman /* 2382ab4382d2SGreg Kroah-Hartman * Ensure that the modem control lines are de-activated. 2383ab4382d2SGreg Kroah-Hartman * keep the DTR setting that is set in uart_set_options() 2384ab4382d2SGreg Kroah-Hartman * We probably don't need a spinlock around this, but 2385ab4382d2SGreg Kroah-Hartman */ 2386ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 2387ab4382d2SGreg Kroah-Hartman port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR); 2388ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 2389ab4382d2SGreg Kroah-Hartman 2390ab4382d2SGreg Kroah-Hartman /* 2391ab4382d2SGreg Kroah-Hartman * If this driver supports console, and it hasn't been 2392ab4382d2SGreg Kroah-Hartman * successfully registered yet, try to re-register it. 2393ab4382d2SGreg Kroah-Hartman * It may be that the port was not available. 2394ab4382d2SGreg Kroah-Hartman */ 2395ab4382d2SGreg Kroah-Hartman if (port->cons && !(port->cons->flags & CON_ENABLED)) 2396ab4382d2SGreg Kroah-Hartman register_console(port->cons); 2397ab4382d2SGreg Kroah-Hartman 2398ab4382d2SGreg Kroah-Hartman /* 2399ab4382d2SGreg Kroah-Hartman * Power down all ports by default, except the 2400ab4382d2SGreg Kroah-Hartman * console if we have one. 2401ab4382d2SGreg Kroah-Hartman */ 2402ab4382d2SGreg Kroah-Hartman if (!uart_console(port)) 24036f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_OFF); 2404ab4382d2SGreg Kroah-Hartman } 2405ab4382d2SGreg Kroah-Hartman } 2406ab4382d2SGreg Kroah-Hartman 2407ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2408ab4382d2SGreg Kroah-Hartman 2409ab4382d2SGreg Kroah-Hartman static int uart_poll_init(struct tty_driver *driver, int line, char *options) 2410ab4382d2SGreg Kroah-Hartman { 2411ab4382d2SGreg Kroah-Hartman struct uart_driver *drv = driver->driver_state; 2412ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + line; 241349c02304SPeter Hurley struct tty_port *tport; 2414ab4382d2SGreg Kroah-Hartman struct uart_port *port; 2415ab4382d2SGreg Kroah-Hartman int baud = 9600; 2416ab4382d2SGreg Kroah-Hartman int bits = 8; 2417ab4382d2SGreg Kroah-Hartman int parity = 'n'; 2418ab4382d2SGreg Kroah-Hartman int flow = 'n'; 241949c02304SPeter Hurley int ret = 0; 2420ab4382d2SGreg Kroah-Hartman 242149c02304SPeter Hurley tport = &state->port; 242249c02304SPeter Hurley mutex_lock(&tport->mutex); 242349c02304SPeter Hurley 24244047b371SPeter Hurley port = uart_port_check(state); 24254047b371SPeter Hurley if (!port || !(port->ops->poll_get_char && port->ops->poll_put_char)) { 242649c02304SPeter Hurley ret = -1; 242749c02304SPeter Hurley goto out; 242849c02304SPeter Hurley } 2429ab4382d2SGreg Kroah-Hartman 2430c7f3e708SAnton Vorontsov if (port->ops->poll_init) { 2431c7f3e708SAnton Vorontsov /* 2432d41861caSPeter Hurley * We don't set initialized as we only initialized the hw, 2433d41861caSPeter Hurley * e.g. state->xmit is still uninitialized. 2434c7f3e708SAnton Vorontsov */ 2435d41861caSPeter Hurley if (!tty_port_initialized(tport)) 2436c7f3e708SAnton Vorontsov ret = port->ops->poll_init(port); 2437c7f3e708SAnton Vorontsov } 2438c7f3e708SAnton Vorontsov 243949c02304SPeter Hurley if (!ret && options) { 2440ab4382d2SGreg Kroah-Hartman uart_parse_options(options, &baud, &parity, &bits, &flow); 244149c02304SPeter Hurley ret = uart_set_options(port, NULL, baud, parity, bits, flow); 2442ab4382d2SGreg Kroah-Hartman } 244349c02304SPeter Hurley out: 244449c02304SPeter Hurley mutex_unlock(&tport->mutex); 244549c02304SPeter Hurley return ret; 2446ab4382d2SGreg Kroah-Hartman } 2447ab4382d2SGreg Kroah-Hartman 2448ab4382d2SGreg Kroah-Hartman static int uart_poll_get_char(struct tty_driver *driver, int line) 2449ab4382d2SGreg Kroah-Hartman { 2450ab4382d2SGreg Kroah-Hartman struct uart_driver *drv = driver->driver_state; 2451ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + line; 2452ab4382d2SGreg Kroah-Hartman struct uart_port *port; 24539ed19428SPeter Hurley int ret = -1; 2454ab4382d2SGreg Kroah-Hartman 24559ed19428SPeter Hurley port = uart_port_ref(state); 2456ef510beaSAndy Shevchenko if (port) { 24579ed19428SPeter Hurley ret = port->ops->poll_get_char(port); 24589ed19428SPeter Hurley uart_port_deref(port); 24599ed19428SPeter Hurley } 246022077b09SJiri Slaby 24619ed19428SPeter Hurley return ret; 2462ab4382d2SGreg Kroah-Hartman } 2463ab4382d2SGreg Kroah-Hartman 2464ab4382d2SGreg Kroah-Hartman static void uart_poll_put_char(struct tty_driver *driver, int line, char ch) 2465ab4382d2SGreg Kroah-Hartman { 2466ab4382d2SGreg Kroah-Hartman struct uart_driver *drv = driver->driver_state; 2467ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + line; 2468ab4382d2SGreg Kroah-Hartman struct uart_port *port; 2469ab4382d2SGreg Kroah-Hartman 24709ed19428SPeter Hurley port = uart_port_ref(state); 24719ed19428SPeter Hurley if (!port) 24729ed19428SPeter Hurley return; 2473c7d44a02SDoug Anderson 2474c7d44a02SDoug Anderson if (ch == '\n') 2475c7d44a02SDoug Anderson port->ops->poll_put_char(port, '\r'); 2476ab4382d2SGreg Kroah-Hartman port->ops->poll_put_char(port, ch); 24779ed19428SPeter Hurley uart_port_deref(port); 2478ab4382d2SGreg Kroah-Hartman } 2479ab4382d2SGreg Kroah-Hartman #endif 2480ab4382d2SGreg Kroah-Hartman 2481ab4382d2SGreg Kroah-Hartman static const struct tty_operations uart_ops = { 24824cdd17baSJiri Slaby .install = uart_install, 2483ab4382d2SGreg Kroah-Hartman .open = uart_open, 2484ab4382d2SGreg Kroah-Hartman .close = uart_close, 2485ab4382d2SGreg Kroah-Hartman .write = uart_write, 2486ab4382d2SGreg Kroah-Hartman .put_char = uart_put_char, 2487ab4382d2SGreg Kroah-Hartman .flush_chars = uart_flush_chars, 2488ab4382d2SGreg Kroah-Hartman .write_room = uart_write_room, 2489ab4382d2SGreg Kroah-Hartman .chars_in_buffer= uart_chars_in_buffer, 2490ab4382d2SGreg Kroah-Hartman .flush_buffer = uart_flush_buffer, 2491ab4382d2SGreg Kroah-Hartman .ioctl = uart_ioctl, 2492ab4382d2SGreg Kroah-Hartman .throttle = uart_throttle, 2493ab4382d2SGreg Kroah-Hartman .unthrottle = uart_unthrottle, 2494ab4382d2SGreg Kroah-Hartman .send_xchar = uart_send_xchar, 2495ab4382d2SGreg Kroah-Hartman .set_termios = uart_set_termios, 2496ab4382d2SGreg Kroah-Hartman .set_ldisc = uart_set_ldisc, 2497ab4382d2SGreg Kroah-Hartman .stop = uart_stop, 2498ab4382d2SGreg Kroah-Hartman .start = uart_start, 2499ab4382d2SGreg Kroah-Hartman .hangup = uart_hangup, 2500ab4382d2SGreg Kroah-Hartman .break_ctl = uart_break_ctl, 2501ab4382d2SGreg Kroah-Hartman .wait_until_sent= uart_wait_until_sent, 2502ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PROC_FS 25038a8dcabfSChristoph Hellwig .proc_show = uart_proc_show, 2504ab4382d2SGreg Kroah-Hartman #endif 2505ab4382d2SGreg Kroah-Hartman .tiocmget = uart_tiocmget, 2506ab4382d2SGreg Kroah-Hartman .tiocmset = uart_tiocmset, 25075099d234SAl Viro .set_serial = uart_set_info_user, 25085099d234SAl Viro .get_serial = uart_get_info_user, 2509ab4382d2SGreg Kroah-Hartman .get_icount = uart_get_icount, 2510ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2511ab4382d2SGreg Kroah-Hartman .poll_init = uart_poll_init, 2512ab4382d2SGreg Kroah-Hartman .poll_get_char = uart_poll_get_char, 2513ab4382d2SGreg Kroah-Hartman .poll_put_char = uart_poll_put_char, 2514ab4382d2SGreg Kroah-Hartman #endif 2515ab4382d2SGreg Kroah-Hartman }; 2516ab4382d2SGreg Kroah-Hartman 2517ab4382d2SGreg Kroah-Hartman static const struct tty_port_operations uart_port_ops = { 2518ab4382d2SGreg Kroah-Hartman .carrier_raised = uart_carrier_raised, 2519ab4382d2SGreg Kroah-Hartman .dtr_rts = uart_dtr_rts, 2520b3b57646SRob Herring .activate = uart_port_activate, 2521761ed4a9SRob Herring .shutdown = uart_tty_port_shutdown, 2522ab4382d2SGreg Kroah-Hartman }; 2523ab4382d2SGreg Kroah-Hartman 2524ab4382d2SGreg Kroah-Hartman /** 2525ab4382d2SGreg Kroah-Hartman * uart_register_driver - register a driver with the uart core layer 2526ab4382d2SGreg Kroah-Hartman * @drv: low level driver structure 2527ab4382d2SGreg Kroah-Hartman * 2528ab4382d2SGreg Kroah-Hartman * Register a uart driver with the core driver. We in turn register 2529ab4382d2SGreg Kroah-Hartman * with the tty layer, and initialise the core driver per-port state. 2530ab4382d2SGreg Kroah-Hartman * 2531ab4382d2SGreg Kroah-Hartman * We have a proc file in /proc/tty/driver which is named after the 2532ab4382d2SGreg Kroah-Hartman * normal driver. 2533ab4382d2SGreg Kroah-Hartman * 2534ab4382d2SGreg Kroah-Hartman * drv->port should be NULL, and the per-port structures should be 2535ab4382d2SGreg Kroah-Hartman * registered using uart_add_one_port after this call has succeeded. 2536ab4382d2SGreg Kroah-Hartman */ 2537ab4382d2SGreg Kroah-Hartman int uart_register_driver(struct uart_driver *drv) 2538ab4382d2SGreg Kroah-Hartman { 2539ab4382d2SGreg Kroah-Hartman struct tty_driver *normal; 2540050dfc09SSergey Organov int i, retval = -ENOMEM; 2541ab4382d2SGreg Kroah-Hartman 2542ab4382d2SGreg Kroah-Hartman BUG_ON(drv->state); 2543ab4382d2SGreg Kroah-Hartman 2544ab4382d2SGreg Kroah-Hartman /* 2545ab4382d2SGreg Kroah-Hartman * Maybe we should be using a slab cache for this, especially if 2546ab4382d2SGreg Kroah-Hartman * we have a large number of ports to handle. 2547ab4382d2SGreg Kroah-Hartman */ 25486396bb22SKees Cook drv->state = kcalloc(drv->nr, sizeof(struct uart_state), GFP_KERNEL); 2549ab4382d2SGreg Kroah-Hartman if (!drv->state) 2550ab4382d2SGreg Kroah-Hartman goto out; 2551ab4382d2SGreg Kroah-Hartman 2552ab4382d2SGreg Kroah-Hartman normal = alloc_tty_driver(drv->nr); 2553ab4382d2SGreg Kroah-Hartman if (!normal) 2554ab4382d2SGreg Kroah-Hartman goto out_kfree; 2555ab4382d2SGreg Kroah-Hartman 2556ab4382d2SGreg Kroah-Hartman drv->tty_driver = normal; 2557ab4382d2SGreg Kroah-Hartman 2558ab4382d2SGreg Kroah-Hartman normal->driver_name = drv->driver_name; 2559ab4382d2SGreg Kroah-Hartman normal->name = drv->dev_name; 2560ab4382d2SGreg Kroah-Hartman normal->major = drv->major; 2561ab4382d2SGreg Kroah-Hartman normal->minor_start = drv->minor; 2562ab4382d2SGreg Kroah-Hartman normal->type = TTY_DRIVER_TYPE_SERIAL; 2563ab4382d2SGreg Kroah-Hartman normal->subtype = SERIAL_TYPE_NORMAL; 2564ab4382d2SGreg Kroah-Hartman normal->init_termios = tty_std_termios; 2565ab4382d2SGreg Kroah-Hartman normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; 2566ab4382d2SGreg Kroah-Hartman normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600; 2567ab4382d2SGreg Kroah-Hartman normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 2568ab4382d2SGreg Kroah-Hartman normal->driver_state = drv; 2569ab4382d2SGreg Kroah-Hartman tty_set_operations(normal, &uart_ops); 2570ab4382d2SGreg Kroah-Hartman 2571ab4382d2SGreg Kroah-Hartman /* 2572ab4382d2SGreg Kroah-Hartman * Initialise the UART state(s). 2573ab4382d2SGreg Kroah-Hartman */ 2574ab4382d2SGreg Kroah-Hartman for (i = 0; i < drv->nr; i++) { 2575ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + i; 2576ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 2577ab4382d2SGreg Kroah-Hartman 2578ab4382d2SGreg Kroah-Hartman tty_port_init(port); 2579ab4382d2SGreg Kroah-Hartman port->ops = &uart_port_ops; 2580ab4382d2SGreg Kroah-Hartman } 2581ab4382d2SGreg Kroah-Hartman 2582ab4382d2SGreg Kroah-Hartman retval = tty_register_driver(normal); 2583ab4382d2SGreg Kroah-Hartman if (retval >= 0) 2584ab4382d2SGreg Kroah-Hartman return retval; 2585ab4382d2SGreg Kroah-Hartman 2586191c5f10SJiri Slaby for (i = 0; i < drv->nr; i++) 2587191c5f10SJiri Slaby tty_port_destroy(&drv->state[i].port); 2588ab4382d2SGreg Kroah-Hartman put_tty_driver(normal); 2589ab4382d2SGreg Kroah-Hartman out_kfree: 2590ab4382d2SGreg Kroah-Hartman kfree(drv->state); 2591ab4382d2SGreg Kroah-Hartman out: 2592050dfc09SSergey Organov return retval; 2593ab4382d2SGreg Kroah-Hartman } 2594ab4382d2SGreg Kroah-Hartman 2595ab4382d2SGreg Kroah-Hartman /** 2596ab4382d2SGreg Kroah-Hartman * uart_unregister_driver - remove a driver from the uart core layer 2597ab4382d2SGreg Kroah-Hartman * @drv: low level driver structure 2598ab4382d2SGreg Kroah-Hartman * 2599ab4382d2SGreg Kroah-Hartman * Remove all references to a driver from the core driver. The low 2600ab4382d2SGreg Kroah-Hartman * level driver must have removed all its ports via the 2601ab4382d2SGreg Kroah-Hartman * uart_remove_one_port() if it registered them with uart_add_one_port(). 2602ab4382d2SGreg Kroah-Hartman * (ie, drv->port == NULL) 2603ab4382d2SGreg Kroah-Hartman */ 2604ab4382d2SGreg Kroah-Hartman void uart_unregister_driver(struct uart_driver *drv) 2605ab4382d2SGreg Kroah-Hartman { 2606ab4382d2SGreg Kroah-Hartman struct tty_driver *p = drv->tty_driver; 2607191c5f10SJiri Slaby unsigned int i; 2608191c5f10SJiri Slaby 2609ab4382d2SGreg Kroah-Hartman tty_unregister_driver(p); 2610ab4382d2SGreg Kroah-Hartman put_tty_driver(p); 2611191c5f10SJiri Slaby for (i = 0; i < drv->nr; i++) 2612191c5f10SJiri Slaby tty_port_destroy(&drv->state[i].port); 2613ab4382d2SGreg Kroah-Hartman kfree(drv->state); 26141e66cdedSAlan Cox drv->state = NULL; 2615ab4382d2SGreg Kroah-Hartman drv->tty_driver = NULL; 2616ab4382d2SGreg Kroah-Hartman } 2617ab4382d2SGreg Kroah-Hartman 2618ab4382d2SGreg Kroah-Hartman struct tty_driver *uart_console_device(struct console *co, int *index) 2619ab4382d2SGreg Kroah-Hartman { 2620ab4382d2SGreg Kroah-Hartman struct uart_driver *p = co->data; 2621ab4382d2SGreg Kroah-Hartman *index = co->index; 2622ab4382d2SGreg Kroah-Hartman return p->tty_driver; 2623ab4382d2SGreg Kroah-Hartman } 2624488f49acSJohn Stultz EXPORT_SYMBOL_GPL(uart_console_device); 2625ab4382d2SGreg Kroah-Hartman 2626143c02c8SAndy Shevchenko static ssize_t uartclk_show(struct device *dev, 26276915c0e4STomas Hlavacek struct device_attribute *attr, char *buf) 26286915c0e4STomas Hlavacek { 2629bebe73e3SAlan Cox struct serial_struct tmp; 26306915c0e4STomas Hlavacek struct tty_port *port = dev_get_drvdata(dev); 2631b1b79916STomas Hlavacek 26329f109694SAlan Cox uart_get_info(port, &tmp); 2633bebe73e3SAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16); 26346915c0e4STomas Hlavacek } 26356915c0e4STomas Hlavacek 2636143c02c8SAndy Shevchenko static ssize_t type_show(struct device *dev, 2637373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2638373bac4cSAlan Cox { 2639373bac4cSAlan Cox struct serial_struct tmp; 2640373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2641373bac4cSAlan Cox 2642373bac4cSAlan Cox uart_get_info(port, &tmp); 2643373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type); 2644373bac4cSAlan Cox } 2645143c02c8SAndy Shevchenko 2646143c02c8SAndy Shevchenko static ssize_t line_show(struct device *dev, 2647373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2648373bac4cSAlan Cox { 2649373bac4cSAlan Cox struct serial_struct tmp; 2650373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2651373bac4cSAlan Cox 2652373bac4cSAlan Cox uart_get_info(port, &tmp); 2653373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line); 2654373bac4cSAlan Cox } 2655373bac4cSAlan Cox 2656143c02c8SAndy Shevchenko static ssize_t port_show(struct device *dev, 2657373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2658373bac4cSAlan Cox { 2659373bac4cSAlan Cox struct serial_struct tmp; 2660373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2661fd985e1dSAndrew Morton unsigned long ioaddr; 2662373bac4cSAlan Cox 2663373bac4cSAlan Cox uart_get_info(port, &tmp); 2664fd985e1dSAndrew Morton ioaddr = tmp.port; 2665fd985e1dSAndrew Morton if (HIGH_BITS_OFFSET) 2666fd985e1dSAndrew Morton ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET; 2667fd985e1dSAndrew Morton return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr); 2668373bac4cSAlan Cox } 2669373bac4cSAlan Cox 2670143c02c8SAndy Shevchenko static ssize_t irq_show(struct device *dev, 2671373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2672373bac4cSAlan Cox { 2673373bac4cSAlan Cox struct serial_struct tmp; 2674373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2675373bac4cSAlan Cox 2676373bac4cSAlan Cox uart_get_info(port, &tmp); 2677373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq); 2678373bac4cSAlan Cox } 2679373bac4cSAlan Cox 2680143c02c8SAndy Shevchenko static ssize_t flags_show(struct device *dev, 2681373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2682373bac4cSAlan Cox { 2683373bac4cSAlan Cox struct serial_struct tmp; 2684373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2685373bac4cSAlan Cox 2686373bac4cSAlan Cox uart_get_info(port, &tmp); 2687373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags); 2688373bac4cSAlan Cox } 2689373bac4cSAlan Cox 2690143c02c8SAndy Shevchenko static ssize_t xmit_fifo_size_show(struct device *dev, 2691373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2692373bac4cSAlan Cox { 2693373bac4cSAlan Cox struct serial_struct tmp; 2694373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2695373bac4cSAlan Cox 2696373bac4cSAlan Cox uart_get_info(port, &tmp); 2697373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size); 2698373bac4cSAlan Cox } 2699373bac4cSAlan Cox 2700143c02c8SAndy Shevchenko static ssize_t close_delay_show(struct device *dev, 2701373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2702373bac4cSAlan Cox { 2703373bac4cSAlan Cox struct serial_struct tmp; 2704373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2705373bac4cSAlan Cox 2706373bac4cSAlan Cox uart_get_info(port, &tmp); 2707373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay); 2708373bac4cSAlan Cox } 2709373bac4cSAlan Cox 2710143c02c8SAndy Shevchenko static ssize_t closing_wait_show(struct device *dev, 2711373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2712373bac4cSAlan Cox { 2713373bac4cSAlan Cox struct serial_struct tmp; 2714373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2715373bac4cSAlan Cox 2716373bac4cSAlan Cox uart_get_info(port, &tmp); 2717373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait); 2718373bac4cSAlan Cox } 2719373bac4cSAlan Cox 2720143c02c8SAndy Shevchenko static ssize_t custom_divisor_show(struct device *dev, 2721373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2722373bac4cSAlan Cox { 2723373bac4cSAlan Cox struct serial_struct tmp; 2724373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2725373bac4cSAlan Cox 2726373bac4cSAlan Cox uart_get_info(port, &tmp); 2727373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor); 2728373bac4cSAlan Cox } 2729373bac4cSAlan Cox 2730143c02c8SAndy Shevchenko static ssize_t io_type_show(struct device *dev, 2731373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2732373bac4cSAlan Cox { 2733373bac4cSAlan Cox struct serial_struct tmp; 2734373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2735373bac4cSAlan Cox 2736373bac4cSAlan Cox uart_get_info(port, &tmp); 2737373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type); 2738373bac4cSAlan Cox } 2739373bac4cSAlan Cox 2740143c02c8SAndy Shevchenko static ssize_t iomem_base_show(struct device *dev, 2741373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2742373bac4cSAlan Cox { 2743373bac4cSAlan Cox struct serial_struct tmp; 2744373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2745373bac4cSAlan Cox 2746373bac4cSAlan Cox uart_get_info(port, &tmp); 2747373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base); 2748373bac4cSAlan Cox } 2749373bac4cSAlan Cox 2750143c02c8SAndy Shevchenko static ssize_t iomem_reg_shift_show(struct device *dev, 2751373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2752373bac4cSAlan Cox { 2753373bac4cSAlan Cox struct serial_struct tmp; 2754373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2755373bac4cSAlan Cox 2756373bac4cSAlan Cox uart_get_info(port, &tmp); 2757373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift); 2758373bac4cSAlan Cox } 2759373bac4cSAlan Cox 2760a3cb39d2SAndy Shevchenko static ssize_t console_show(struct device *dev, 2761a3cb39d2SAndy Shevchenko struct device_attribute *attr, char *buf) 2762a3cb39d2SAndy Shevchenko { 2763a3cb39d2SAndy Shevchenko struct tty_port *port = dev_get_drvdata(dev); 2764a3cb39d2SAndy Shevchenko struct uart_state *state = container_of(port, struct uart_state, port); 2765a3cb39d2SAndy Shevchenko struct uart_port *uport; 2766a3cb39d2SAndy Shevchenko bool console = false; 2767a3cb39d2SAndy Shevchenko 2768a3cb39d2SAndy Shevchenko mutex_lock(&port->mutex); 2769a3cb39d2SAndy Shevchenko uport = uart_port_check(state); 2770a3cb39d2SAndy Shevchenko if (uport) 2771a3cb39d2SAndy Shevchenko console = uart_console_enabled(uport); 2772a3cb39d2SAndy Shevchenko mutex_unlock(&port->mutex); 2773a3cb39d2SAndy Shevchenko 2774a3cb39d2SAndy Shevchenko return sprintf(buf, "%c\n", console ? 'Y' : 'N'); 2775a3cb39d2SAndy Shevchenko } 2776a3cb39d2SAndy Shevchenko 2777a3cb39d2SAndy Shevchenko static ssize_t console_store(struct device *dev, 2778a3cb39d2SAndy Shevchenko struct device_attribute *attr, const char *buf, size_t count) 2779a3cb39d2SAndy Shevchenko { 2780a3cb39d2SAndy Shevchenko struct tty_port *port = dev_get_drvdata(dev); 2781a3cb39d2SAndy Shevchenko struct uart_state *state = container_of(port, struct uart_state, port); 2782a3cb39d2SAndy Shevchenko struct uart_port *uport; 2783a3cb39d2SAndy Shevchenko bool oldconsole, newconsole; 2784a3cb39d2SAndy Shevchenko int ret; 2785a3cb39d2SAndy Shevchenko 2786a3cb39d2SAndy Shevchenko ret = kstrtobool(buf, &newconsole); 2787a3cb39d2SAndy Shevchenko if (ret) 2788a3cb39d2SAndy Shevchenko return ret; 2789a3cb39d2SAndy Shevchenko 2790a3cb39d2SAndy Shevchenko mutex_lock(&port->mutex); 2791a3cb39d2SAndy Shevchenko uport = uart_port_check(state); 2792a3cb39d2SAndy Shevchenko if (uport) { 2793a3cb39d2SAndy Shevchenko oldconsole = uart_console_enabled(uport); 2794a3cb39d2SAndy Shevchenko if (oldconsole && !newconsole) { 2795a3cb39d2SAndy Shevchenko ret = unregister_console(uport->cons); 2796a3cb39d2SAndy Shevchenko } else if (!oldconsole && newconsole) { 2797a3cb39d2SAndy Shevchenko if (uart_console(uport)) 2798a3cb39d2SAndy Shevchenko register_console(uport->cons); 2799a3cb39d2SAndy Shevchenko else 2800a3cb39d2SAndy Shevchenko ret = -ENOENT; 2801a3cb39d2SAndy Shevchenko } 2802a3cb39d2SAndy Shevchenko } else { 2803a3cb39d2SAndy Shevchenko ret = -ENXIO; 2804a3cb39d2SAndy Shevchenko } 2805a3cb39d2SAndy Shevchenko mutex_unlock(&port->mutex); 2806a3cb39d2SAndy Shevchenko 2807a3cb39d2SAndy Shevchenko return ret < 0 ? ret : count; 2808a3cb39d2SAndy Shevchenko } 2809a3cb39d2SAndy Shevchenko 2810143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(uartclk); 2811143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(type); 2812143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(line); 2813143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(port); 2814143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(irq); 2815143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(flags); 2816143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(xmit_fifo_size); 2817143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(close_delay); 2818143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(closing_wait); 2819143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(custom_divisor); 2820143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(io_type); 2821143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(iomem_base); 2822143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(iomem_reg_shift); 2823a3cb39d2SAndy Shevchenko static DEVICE_ATTR_RW(console); 28246915c0e4STomas Hlavacek 28256915c0e4STomas Hlavacek static struct attribute *tty_dev_attrs[] = { 2826143c02c8SAndy Shevchenko &dev_attr_uartclk.attr, 2827373bac4cSAlan Cox &dev_attr_type.attr, 2828373bac4cSAlan Cox &dev_attr_line.attr, 2829373bac4cSAlan Cox &dev_attr_port.attr, 2830373bac4cSAlan Cox &dev_attr_irq.attr, 2831373bac4cSAlan Cox &dev_attr_flags.attr, 2832373bac4cSAlan Cox &dev_attr_xmit_fifo_size.attr, 2833373bac4cSAlan Cox &dev_attr_close_delay.attr, 2834373bac4cSAlan Cox &dev_attr_closing_wait.attr, 2835373bac4cSAlan Cox &dev_attr_custom_divisor.attr, 2836373bac4cSAlan Cox &dev_attr_io_type.attr, 2837373bac4cSAlan Cox &dev_attr_iomem_base.attr, 2838373bac4cSAlan Cox &dev_attr_iomem_reg_shift.attr, 2839a3cb39d2SAndy Shevchenko &dev_attr_console.attr, 2840a3cb39d2SAndy Shevchenko NULL 28416915c0e4STomas Hlavacek }; 28426915c0e4STomas Hlavacek 2843b1b79916STomas Hlavacek static const struct attribute_group tty_dev_attr_group = { 28446915c0e4STomas Hlavacek .attrs = tty_dev_attrs, 28456915c0e4STomas Hlavacek }; 28466915c0e4STomas Hlavacek 2847ab4382d2SGreg Kroah-Hartman /** 2848ab4382d2SGreg Kroah-Hartman * uart_add_one_port - attach a driver-defined port structure 2849ab4382d2SGreg Kroah-Hartman * @drv: pointer to the uart low level driver structure for this port 2850ab4382d2SGreg Kroah-Hartman * @uport: uart port structure to use for this port. 2851ab4382d2SGreg Kroah-Hartman * 2852ab4382d2SGreg Kroah-Hartman * This allows the driver to register its own uart_port structure 2853ab4382d2SGreg Kroah-Hartman * with the core driver. The main purpose is to allow the low 2854ab4382d2SGreg Kroah-Hartman * level uart drivers to expand uart_port, rather than having yet 2855ab4382d2SGreg Kroah-Hartman * more levels of structures. 2856ab4382d2SGreg Kroah-Hartman */ 2857ab4382d2SGreg Kroah-Hartman int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport) 2858ab4382d2SGreg Kroah-Hartman { 2859ab4382d2SGreg Kroah-Hartman struct uart_state *state; 2860ab4382d2SGreg Kroah-Hartman struct tty_port *port; 2861ab4382d2SGreg Kroah-Hartman int ret = 0; 2862ab4382d2SGreg Kroah-Hartman struct device *tty_dev; 2863266dcff0SGreg Kroah-Hartman int num_groups; 2864ab4382d2SGreg Kroah-Hartman 2865ab4382d2SGreg Kroah-Hartman BUG_ON(in_interrupt()); 2866ab4382d2SGreg Kroah-Hartman 2867ab4382d2SGreg Kroah-Hartman if (uport->line >= drv->nr) 2868ab4382d2SGreg Kroah-Hartman return -EINVAL; 2869ab4382d2SGreg Kroah-Hartman 2870ab4382d2SGreg Kroah-Hartman state = drv->state + uport->line; 2871ab4382d2SGreg Kroah-Hartman port = &state->port; 2872ab4382d2SGreg Kroah-Hartman 2873ab4382d2SGreg Kroah-Hartman mutex_lock(&port_mutex); 2874ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 2875ab4382d2SGreg Kroah-Hartman if (state->uart_port) { 2876ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2877ab4382d2SGreg Kroah-Hartman goto out; 2878ab4382d2SGreg Kroah-Hartman } 2879ab4382d2SGreg Kroah-Hartman 28802b702b9bSPeter Hurley /* Link the port to the driver state table and vice versa */ 28819ed19428SPeter Hurley atomic_set(&state->refcount, 1); 28829ed19428SPeter Hurley init_waitqueue_head(&state->remove_wait); 2883ab4382d2SGreg Kroah-Hartman state->uart_port = uport; 2884ab4382d2SGreg Kroah-Hartman uport->state = state; 2885ab4382d2SGreg Kroah-Hartman 28862b702b9bSPeter Hurley state->pm_state = UART_PM_STATE_UNDEFINED; 28872b702b9bSPeter Hurley uport->cons = drv->cons; 2888959801feSPeter Hurley uport->minor = drv->tty_driver->minor_start + uport->line; 2889f7048b15SVignesh R uport->name = kasprintf(GFP_KERNEL, "%s%d", drv->dev_name, 2890f7048b15SVignesh R drv->tty_driver->name_base + uport->line); 2891f7048b15SVignesh R if (!uport->name) { 2892f7048b15SVignesh R ret = -ENOMEM; 2893f7048b15SVignesh R goto out; 2894f7048b15SVignesh R } 28952b702b9bSPeter Hurley 2896fe88c648SJohan Hovold /* 2897fe88c648SJohan Hovold * If this port is in use as a console then the spinlock is already 2898fe88c648SJohan Hovold * initialised. 2899fe88c648SJohan Hovold */ 2900fe88c648SJohan Hovold if (!uart_console_enabled(uport)) 2901fe88c648SJohan Hovold __uart_port_spin_lock_init(uport); 2902d2403cadSAndy Shevchenko 2903a208ffd2SGrant Likely if (uport->cons && uport->dev) 2904dd8b7a1dSMichal Simek of_console_check(uport->dev->of_node, uport->cons->name, uport->line); 2905ab4382d2SGreg Kroah-Hartman 2906fb2b9001SSudip Mukherjee tty_port_link_device(port, drv->tty_driver, uport->line); 2907ab4382d2SGreg Kroah-Hartman uart_configure_port(drv, state, uport); 2908ab4382d2SGreg Kroah-Hartman 29094dda864dSGeert Uytterhoeven port->console = uart_console(uport); 29104dda864dSGeert Uytterhoeven 2911266dcff0SGreg Kroah-Hartman num_groups = 2; 2912266dcff0SGreg Kroah-Hartman if (uport->attr_group) 2913266dcff0SGreg Kroah-Hartman num_groups++; 2914266dcff0SGreg Kroah-Hartman 2915c2b703b8SYoshihiro YUNOMAE uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups), 2916266dcff0SGreg Kroah-Hartman GFP_KERNEL); 2917266dcff0SGreg Kroah-Hartman if (!uport->tty_groups) { 2918266dcff0SGreg Kroah-Hartman ret = -ENOMEM; 2919266dcff0SGreg Kroah-Hartman goto out; 2920266dcff0SGreg Kroah-Hartman } 2921266dcff0SGreg Kroah-Hartman uport->tty_groups[0] = &tty_dev_attr_group; 2922266dcff0SGreg Kroah-Hartman if (uport->attr_group) 2923266dcff0SGreg Kroah-Hartman uport->tty_groups[1] = uport->attr_group; 2924266dcff0SGreg Kroah-Hartman 2925ab4382d2SGreg Kroah-Hartman /* 2926ab4382d2SGreg Kroah-Hartman * Register the port whether it's detected or not. This allows 2927015355b7SGeert Uytterhoeven * setserial to be used to alter this port's parameters. 2928ab4382d2SGreg Kroah-Hartman */ 2929da4c2799SJohan Hovold tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver, 2930266dcff0SGreg Kroah-Hartman uport->line, uport->dev, port, uport->tty_groups); 2931b289c496SChengguang Xu if (!IS_ERR(tty_dev)) { 293277359835SSimon Glass device_set_wakeup_capable(tty_dev, 1); 293377359835SSimon Glass } else { 29342f2dafe7SSudip Mukherjee dev_err(uport->dev, "Cannot register tty device on line %d\n", 2935ab4382d2SGreg Kroah-Hartman uport->line); 293677359835SSimon Glass } 2937ab4382d2SGreg Kroah-Hartman 2938ab4382d2SGreg Kroah-Hartman /* 2939ab4382d2SGreg Kroah-Hartman * Ensure UPF_DEAD is not set. 2940ab4382d2SGreg Kroah-Hartman */ 2941ab4382d2SGreg Kroah-Hartman uport->flags &= ~UPF_DEAD; 2942ab4382d2SGreg Kroah-Hartman 2943ab4382d2SGreg Kroah-Hartman out: 2944ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2945ab4382d2SGreg Kroah-Hartman mutex_unlock(&port_mutex); 2946ab4382d2SGreg Kroah-Hartman 2947ab4382d2SGreg Kroah-Hartman return ret; 2948ab4382d2SGreg Kroah-Hartman } 2949ab4382d2SGreg Kroah-Hartman 2950ab4382d2SGreg Kroah-Hartman /** 2951ab4382d2SGreg Kroah-Hartman * uart_remove_one_port - detach a driver defined port structure 2952ab4382d2SGreg Kroah-Hartman * @drv: pointer to the uart low level driver structure for this port 2953ab4382d2SGreg Kroah-Hartman * @uport: uart port structure for this port 2954ab4382d2SGreg Kroah-Hartman * 2955ab4382d2SGreg Kroah-Hartman * This unhooks (and hangs up) the specified port structure from the 2956ab4382d2SGreg Kroah-Hartman * core driver. No further calls will be made to the low-level code 2957ab4382d2SGreg Kroah-Hartman * for this port. 2958ab4382d2SGreg Kroah-Hartman */ 2959ab4382d2SGreg Kroah-Hartman int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport) 2960ab4382d2SGreg Kroah-Hartman { 2961ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + uport->line; 2962ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 29634047b371SPeter Hurley struct uart_port *uart_port; 29644c6d5b4dSGeert Uytterhoeven struct tty_struct *tty; 2965b342dd51SChen Gang int ret = 0; 2966ab4382d2SGreg Kroah-Hartman 2967ab4382d2SGreg Kroah-Hartman BUG_ON(in_interrupt()); 2968ab4382d2SGreg Kroah-Hartman 2969ab4382d2SGreg Kroah-Hartman mutex_lock(&port_mutex); 2970ab4382d2SGreg Kroah-Hartman 2971ab4382d2SGreg Kroah-Hartman /* 2972ab4382d2SGreg Kroah-Hartman * Mark the port "dead" - this prevents any opens from 2973ab4382d2SGreg Kroah-Hartman * succeeding while we shut down the port. 2974ab4382d2SGreg Kroah-Hartman */ 2975ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 29764047b371SPeter Hurley uart_port = uart_port_check(state); 29774047b371SPeter Hurley if (uart_port != uport) 29784047b371SPeter Hurley dev_alert(uport->dev, "Removing wrong port: %p != %p\n", 29794047b371SPeter Hurley uart_port, uport); 29804047b371SPeter Hurley 29814047b371SPeter Hurley if (!uart_port) { 2982b342dd51SChen Gang mutex_unlock(&port->mutex); 2983b342dd51SChen Gang ret = -EINVAL; 2984b342dd51SChen Gang goto out; 2985b342dd51SChen Gang } 2986ab4382d2SGreg Kroah-Hartman uport->flags |= UPF_DEAD; 2987ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2988ab4382d2SGreg Kroah-Hartman 2989ab4382d2SGreg Kroah-Hartman /* 2990ab4382d2SGreg Kroah-Hartman * Remove the devices from the tty layer 2991ab4382d2SGreg Kroah-Hartman */ 2992da4c2799SJohan Hovold tty_port_unregister_device(port, drv->tty_driver, uport->line); 2993ab4382d2SGreg Kroah-Hartman 29944c6d5b4dSGeert Uytterhoeven tty = tty_port_tty_get(port); 29954c6d5b4dSGeert Uytterhoeven if (tty) { 2996ab4382d2SGreg Kroah-Hartman tty_vhangup(port->tty); 29974c6d5b4dSGeert Uytterhoeven tty_kref_put(tty); 29984c6d5b4dSGeert Uytterhoeven } 2999ab4382d2SGreg Kroah-Hartman 3000ab4382d2SGreg Kroah-Hartman /* 30015f5c9ae5SGeert Uytterhoeven * If the port is used as a console, unregister it 30025f5c9ae5SGeert Uytterhoeven */ 30035f5c9ae5SGeert Uytterhoeven if (uart_console(uport)) 30045f5c9ae5SGeert Uytterhoeven unregister_console(uport->cons); 30055f5c9ae5SGeert Uytterhoeven 30065f5c9ae5SGeert Uytterhoeven /* 3007ab4382d2SGreg Kroah-Hartman * Free the port IO and memory resources, if any. 3008ab4382d2SGreg Kroah-Hartman */ 3009a7cfaf16SFabio Estevam if (uport->type != PORT_UNKNOWN && uport->ops->release_port) 3010ab4382d2SGreg Kroah-Hartman uport->ops->release_port(uport); 3011266dcff0SGreg Kroah-Hartman kfree(uport->tty_groups); 3012f7048b15SVignesh R kfree(uport->name); 3013ab4382d2SGreg Kroah-Hartman 3014ab4382d2SGreg Kroah-Hartman /* 3015ab4382d2SGreg Kroah-Hartman * Indicate that there isn't a port here anymore. 3016ab4382d2SGreg Kroah-Hartman */ 3017ab4382d2SGreg Kroah-Hartman uport->type = PORT_UNKNOWN; 3018ab4382d2SGreg Kroah-Hartman 30194047b371SPeter Hurley mutex_lock(&port->mutex); 30209ed19428SPeter Hurley WARN_ON(atomic_dec_return(&state->refcount) < 0); 30219ed19428SPeter Hurley wait_event(state->remove_wait, !atomic_read(&state->refcount)); 3022ab4382d2SGreg Kroah-Hartman state->uart_port = NULL; 30234047b371SPeter Hurley mutex_unlock(&port->mutex); 3024b342dd51SChen Gang out: 3025ab4382d2SGreg Kroah-Hartman mutex_unlock(&port_mutex); 3026ab4382d2SGreg Kroah-Hartman 3027b342dd51SChen Gang return ret; 3028ab4382d2SGreg Kroah-Hartman } 3029ab4382d2SGreg Kroah-Hartman 3030ab4382d2SGreg Kroah-Hartman /* 3031ab4382d2SGreg Kroah-Hartman * Are the two ports equivalent? 3032ab4382d2SGreg Kroah-Hartman */ 3033ab4382d2SGreg Kroah-Hartman int uart_match_port(struct uart_port *port1, struct uart_port *port2) 3034ab4382d2SGreg Kroah-Hartman { 3035ab4382d2SGreg Kroah-Hartman if (port1->iotype != port2->iotype) 3036ab4382d2SGreg Kroah-Hartman return 0; 3037ab4382d2SGreg Kroah-Hartman 3038ab4382d2SGreg Kroah-Hartman switch (port1->iotype) { 3039ab4382d2SGreg Kroah-Hartman case UPIO_PORT: 3040ab4382d2SGreg Kroah-Hartman return (port1->iobase == port2->iobase); 3041ab4382d2SGreg Kroah-Hartman case UPIO_HUB6: 3042ab4382d2SGreg Kroah-Hartman return (port1->iobase == port2->iobase) && 3043ab4382d2SGreg Kroah-Hartman (port1->hub6 == port2->hub6); 3044ab4382d2SGreg Kroah-Hartman case UPIO_MEM: 3045bd94c407SMasahiro Yamada case UPIO_MEM16: 3046ab4382d2SGreg Kroah-Hartman case UPIO_MEM32: 30473ffb1a81SKevin Cernekee case UPIO_MEM32BE: 3048ab4382d2SGreg Kroah-Hartman case UPIO_AU: 3049ab4382d2SGreg Kroah-Hartman case UPIO_TSI: 3050ab4382d2SGreg Kroah-Hartman return (port1->mapbase == port2->mapbase); 3051ab4382d2SGreg Kroah-Hartman } 3052ab4382d2SGreg Kroah-Hartman return 0; 3053ab4382d2SGreg Kroah-Hartman } 3054ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_match_port); 3055ab4382d2SGreg Kroah-Hartman 3056027d7dacSJiri Slaby /** 3057027d7dacSJiri Slaby * uart_handle_dcd_change - handle a change of carrier detect state 3058027d7dacSJiri Slaby * @uport: uart_port structure for the open port 3059027d7dacSJiri Slaby * @status: new carrier detect status, nonzero if active 30604d90bb14SPeter Hurley * 30614d90bb14SPeter Hurley * Caller must hold uport->lock 3062027d7dacSJiri Slaby */ 3063027d7dacSJiri Slaby void uart_handle_dcd_change(struct uart_port *uport, unsigned int status) 3064027d7dacSJiri Slaby { 306542381572SGeorge Spelvin struct tty_port *port = &uport->state->port; 306643eca0aeSAlan Cox struct tty_struct *tty = port->tty; 3067c993257bSPeter Hurley struct tty_ldisc *ld; 3068027d7dacSJiri Slaby 30694d90bb14SPeter Hurley lockdep_assert_held_once(&uport->lock); 30704d90bb14SPeter Hurley 3071c993257bSPeter Hurley if (tty) { 3072c993257bSPeter Hurley ld = tty_ldisc_ref(tty); 307342381572SGeorge Spelvin if (ld) { 307442381572SGeorge Spelvin if (ld->ops->dcd_change) 3075593fb1aeSGeorge Spelvin ld->ops->dcd_change(tty, status); 307642381572SGeorge Spelvin tty_ldisc_deref(ld); 307742381572SGeorge Spelvin } 3078c993257bSPeter Hurley } 3079027d7dacSJiri Slaby 3080027d7dacSJiri Slaby uport->icount.dcd++; 3081027d7dacSJiri Slaby 3082299245a1SPeter Hurley if (uart_dcd_enabled(uport)) { 3083027d7dacSJiri Slaby if (status) 3084027d7dacSJiri Slaby wake_up_interruptible(&port->open_wait); 308543eca0aeSAlan Cox else if (tty) 308643eca0aeSAlan Cox tty_hangup(tty); 3087027d7dacSJiri Slaby } 3088027d7dacSJiri Slaby } 3089027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_handle_dcd_change); 3090027d7dacSJiri Slaby 3091027d7dacSJiri Slaby /** 3092027d7dacSJiri Slaby * uart_handle_cts_change - handle a change of clear-to-send state 3093027d7dacSJiri Slaby * @uport: uart_port structure for the open port 3094027d7dacSJiri Slaby * @status: new clear to send status, nonzero if active 30954d90bb14SPeter Hurley * 30964d90bb14SPeter Hurley * Caller must hold uport->lock 3097027d7dacSJiri Slaby */ 3098027d7dacSJiri Slaby void uart_handle_cts_change(struct uart_port *uport, unsigned int status) 3099027d7dacSJiri Slaby { 31004d90bb14SPeter Hurley lockdep_assert_held_once(&uport->lock); 31014d90bb14SPeter Hurley 3102027d7dacSJiri Slaby uport->icount.cts++; 3103027d7dacSJiri Slaby 3104391f93f2SPeter Hurley if (uart_softcts_mode(uport)) { 3105d01f4d18SPeter Hurley if (uport->hw_stopped) { 3106027d7dacSJiri Slaby if (status) { 3107d01f4d18SPeter Hurley uport->hw_stopped = 0; 3108027d7dacSJiri Slaby uport->ops->start_tx(uport); 3109027d7dacSJiri Slaby uart_write_wakeup(uport); 3110027d7dacSJiri Slaby } 3111027d7dacSJiri Slaby } else { 3112027d7dacSJiri Slaby if (!status) { 3113d01f4d18SPeter Hurley uport->hw_stopped = 1; 3114027d7dacSJiri Slaby uport->ops->stop_tx(uport); 3115027d7dacSJiri Slaby } 3116027d7dacSJiri Slaby } 3117391f93f2SPeter Hurley 3118027d7dacSJiri Slaby } 3119027d7dacSJiri Slaby } 3120027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_handle_cts_change); 3121027d7dacSJiri Slaby 3122cf75525fSJiri Slaby /** 3123cf75525fSJiri Slaby * uart_insert_char - push a char to the uart layer 3124cf75525fSJiri Slaby * 3125cf75525fSJiri Slaby * User is responsible to call tty_flip_buffer_push when they are done with 3126cf75525fSJiri Slaby * insertion. 3127cf75525fSJiri Slaby * 3128cf75525fSJiri Slaby * @port: corresponding port 3129cf75525fSJiri Slaby * @status: state of the serial port RX buffer (LSR for 8250) 3130cf75525fSJiri Slaby * @overrun: mask of overrun bits in @status 3131cf75525fSJiri Slaby * @ch: character to push 3132cf75525fSJiri Slaby * @flag: flag for the character (see TTY_NORMAL and friends) 3133cf75525fSJiri Slaby */ 3134027d7dacSJiri Slaby void uart_insert_char(struct uart_port *port, unsigned int status, 3135027d7dacSJiri Slaby unsigned int overrun, unsigned int ch, unsigned int flag) 3136027d7dacSJiri Slaby { 313792a19f9cSJiri Slaby struct tty_port *tport = &port->state->port; 3138027d7dacSJiri Slaby 3139027d7dacSJiri Slaby if ((status & port->ignore_status_mask & ~overrun) == 0) 314092a19f9cSJiri Slaby if (tty_insert_flip_char(tport, ch, flag) == 0) 3141dabfb351SCorbin ++port->icount.buf_overrun; 3142027d7dacSJiri Slaby 3143027d7dacSJiri Slaby /* 3144027d7dacSJiri Slaby * Overrun is special. Since it's reported immediately, 3145027d7dacSJiri Slaby * it doesn't affect the current character. 3146027d7dacSJiri Slaby */ 3147027d7dacSJiri Slaby if (status & ~port->ignore_status_mask & overrun) 314892a19f9cSJiri Slaby if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0) 3149dabfb351SCorbin ++port->icount.buf_overrun; 3150027d7dacSJiri Slaby } 3151027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_insert_char); 3152027d7dacSJiri Slaby 315368af4317SDmitry Safonov #ifdef CONFIG_MAGIC_SYSRQ_SERIAL 315468af4317SDmitry Safonov static const char sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE; 315568af4317SDmitry Safonov 315668af4317SDmitry Safonov static void uart_sysrq_on(struct work_struct *w) 315768af4317SDmitry Safonov { 3158b18896ffSAndy Shevchenko int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq); 3159b18896ffSAndy Shevchenko 316068af4317SDmitry Safonov sysrq_toggle_support(1); 3161b18896ffSAndy Shevchenko pr_info("SysRq is enabled by magic sequence '%*pE' on serial\n", 3162b18896ffSAndy Shevchenko sysrq_toggle_seq_len, sysrq_toggle_seq); 316368af4317SDmitry Safonov } 316468af4317SDmitry Safonov static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on); 316568af4317SDmitry Safonov 316668af4317SDmitry Safonov /** 316768af4317SDmitry Safonov * uart_try_toggle_sysrq - Enables SysRq from serial line 316868af4317SDmitry Safonov * @port: uart_port structure where char(s) after BREAK met 316968af4317SDmitry Safonov * @ch: new character in the sequence after received BREAK 317068af4317SDmitry Safonov * 317168af4317SDmitry Safonov * Enables magic SysRq when the required sequence is met on port 317268af4317SDmitry Safonov * (see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE). 317368af4317SDmitry Safonov * 317468af4317SDmitry Safonov * Returns false if @ch is out of enabling sequence and should be 317568af4317SDmitry Safonov * handled some other way, true if @ch was consumed. 317668af4317SDmitry Safonov */ 317708d54703SJohan Hovold bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch) 317868af4317SDmitry Safonov { 31792ce5eaceSAndy Shevchenko int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq); 31802ce5eaceSAndy Shevchenko 31812ce5eaceSAndy Shevchenko if (!sysrq_toggle_seq_len) 318268af4317SDmitry Safonov return false; 318368af4317SDmitry Safonov 318468af4317SDmitry Safonov BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq) >= U8_MAX); 318568af4317SDmitry Safonov if (sysrq_toggle_seq[port->sysrq_seq] != ch) { 318668af4317SDmitry Safonov port->sysrq_seq = 0; 318768af4317SDmitry Safonov return false; 318868af4317SDmitry Safonov } 318968af4317SDmitry Safonov 31902ce5eaceSAndy Shevchenko if (++port->sysrq_seq < sysrq_toggle_seq_len) { 319168af4317SDmitry Safonov port->sysrq = jiffies + SYSRQ_TIMEOUT; 319268af4317SDmitry Safonov return true; 319368af4317SDmitry Safonov } 319468af4317SDmitry Safonov 319568af4317SDmitry Safonov schedule_work(&sysrq_enable_work); 319668af4317SDmitry Safonov 319768af4317SDmitry Safonov port->sysrq = 0; 319868af4317SDmitry Safonov return true; 319968af4317SDmitry Safonov } 320008d54703SJohan Hovold EXPORT_SYMBOL_GPL(uart_try_toggle_sysrq); 320168af4317SDmitry Safonov #endif 320268af4317SDmitry Safonov 3203ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_write_wakeup); 3204ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_register_driver); 3205ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_unregister_driver); 3206ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_suspend_port); 3207ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_resume_port); 3208ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_add_one_port); 3209ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_remove_one_port); 3210ab4382d2SGreg Kroah-Hartman 3211ef838a81SUwe Kleine-König /** 3212743f93f8SLukas Wunner * uart_get_rs485_mode() - retrieve rs485 properties for given uart 3213a7172561SRandy Dunlap * @port: uart device's target port 3214ef838a81SUwe Kleine-König * 3215ef838a81SUwe Kleine-König * This function implements the device tree binding described in 3216ef838a81SUwe Kleine-König * Documentation/devicetree/bindings/serial/rs485.txt. 3217ef838a81SUwe Kleine-König */ 3218c150c0f3SLukas Wunner int uart_get_rs485_mode(struct uart_port *port) 3219ef838a81SUwe Kleine-König { 3220c150c0f3SLukas Wunner struct serial_rs485 *rs485conf = &port->rs485; 3221c150c0f3SLukas Wunner struct device *dev = port->dev; 3222ef838a81SUwe Kleine-König u32 rs485_delay[2]; 3223ef838a81SUwe Kleine-König int ret; 3224ef838a81SUwe Kleine-König 3225743f93f8SLukas Wunner ret = device_property_read_u32_array(dev, "rs485-rts-delay", 3226743f93f8SLukas Wunner rs485_delay, 2); 3227ef838a81SUwe Kleine-König if (!ret) { 3228ef838a81SUwe Kleine-König rs485conf->delay_rts_before_send = rs485_delay[0]; 3229ef838a81SUwe Kleine-König rs485conf->delay_rts_after_send = rs485_delay[1]; 3230ef838a81SUwe Kleine-König } else { 3231ef838a81SUwe Kleine-König rs485conf->delay_rts_before_send = 0; 3232ef838a81SUwe Kleine-König rs485conf->delay_rts_after_send = 0; 3233ef838a81SUwe Kleine-König } 3234ef838a81SUwe Kleine-König 3235ef838a81SUwe Kleine-König /* 3236f1e5b618SLukas Wunner * Clear full-duplex and enabled flags, set RTS polarity to active high 3237f1e5b618SLukas Wunner * to get to a defined state with the following properties: 3238ef838a81SUwe Kleine-König */ 3239f1e5b618SLukas Wunner rs485conf->flags &= ~(SER_RS485_RX_DURING_TX | SER_RS485_ENABLED | 3240d58a2df3SLukas Wunner SER_RS485_TERMINATE_BUS | 3241f1e5b618SLukas Wunner SER_RS485_RTS_AFTER_SEND); 3242f1e5b618SLukas Wunner rs485conf->flags |= SER_RS485_RTS_ON_SEND; 3243ef838a81SUwe Kleine-König 3244743f93f8SLukas Wunner if (device_property_read_bool(dev, "rs485-rx-during-tx")) 3245ef838a81SUwe Kleine-König rs485conf->flags |= SER_RS485_RX_DURING_TX; 3246ef838a81SUwe Kleine-König 3247743f93f8SLukas Wunner if (device_property_read_bool(dev, "linux,rs485-enabled-at-boot-time")) 3248ef838a81SUwe Kleine-König rs485conf->flags |= SER_RS485_ENABLED; 3249f1e5b618SLukas Wunner 3250f1e5b618SLukas Wunner if (device_property_read_bool(dev, "rs485-rts-active-low")) { 3251f1e5b618SLukas Wunner rs485conf->flags &= ~SER_RS485_RTS_ON_SEND; 3252f1e5b618SLukas Wunner rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 3253f1e5b618SLukas Wunner } 3254c150c0f3SLukas Wunner 3255d58a2df3SLukas Wunner /* 3256d58a2df3SLukas Wunner * Disabling termination by default is the safe choice: Else if many 3257d58a2df3SLukas Wunner * bus participants enable it, no communication is possible at all. 3258d58a2df3SLukas Wunner * Works fine for short cables and users may enable for longer cables. 3259d58a2df3SLukas Wunner */ 3260d58a2df3SLukas Wunner port->rs485_term_gpio = devm_gpiod_get_optional(dev, "rs485-term", 3261d58a2df3SLukas Wunner GPIOD_OUT_LOW); 3262d58a2df3SLukas Wunner if (IS_ERR(port->rs485_term_gpio)) { 3263d58a2df3SLukas Wunner ret = PTR_ERR(port->rs485_term_gpio); 3264d58a2df3SLukas Wunner port->rs485_term_gpio = NULL; 3265d58a2df3SLukas Wunner if (ret != -EPROBE_DEFER) 3266d58a2df3SLukas Wunner dev_err(dev, "Cannot get rs485-term-gpios\n"); 3267d58a2df3SLukas Wunner return ret; 3268d58a2df3SLukas Wunner } 3269d58a2df3SLukas Wunner 3270c150c0f3SLukas Wunner return 0; 3271ef838a81SUwe Kleine-König } 3272743f93f8SLukas Wunner EXPORT_SYMBOL_GPL(uart_get_rs485_mode); 3273ef838a81SUwe Kleine-König 3274ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Serial driver core"); 3275ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL"); 3276