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> 17a208ffd2SGrant Likely #include <linux/of.h> 18ab4382d2SGreg Kroah-Hartman #include <linux/proc_fs.h> 19ab4382d2SGreg Kroah-Hartman #include <linux/seq_file.h> 20ab4382d2SGreg Kroah-Hartman #include <linux/device.h> 21ab4382d2SGreg Kroah-Hartman #include <linux/serial.h> /* for serial_state and serial_icounter_struct */ 22ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h> 23ab4382d2SGreg Kroah-Hartman #include <linux/delay.h> 24ab4382d2SGreg Kroah-Hartman #include <linux/mutex.h> 25794edf30SDavid Howells #include <linux/security.h> 26ab4382d2SGreg Kroah-Hartman 27aef3ad10SAndy Shevchenko #include <linux/irq.h> 287c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 29ab4382d2SGreg Kroah-Hartman 30ab4382d2SGreg Kroah-Hartman /* 31ab4382d2SGreg Kroah-Hartman * This is used to lock changes in serial line configuration. 32ab4382d2SGreg Kroah-Hartman */ 33ab4382d2SGreg Kroah-Hartman static DEFINE_MUTEX(port_mutex); 34ab4382d2SGreg Kroah-Hartman 35ab4382d2SGreg Kroah-Hartman /* 36ab4382d2SGreg Kroah-Hartman * lockdep: port->lock is initialized in two places, but we 37ab4382d2SGreg Kroah-Hartman * want only one lock-class: 38ab4382d2SGreg Kroah-Hartman */ 39ab4382d2SGreg Kroah-Hartman static struct lock_class_key port_lock_key; 40ab4382d2SGreg Kroah-Hartman 41ab4382d2SGreg Kroah-Hartman #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8) 42ab4382d2SGreg Kroah-Hartman 43ab4382d2SGreg Kroah-Hartman static void uart_change_speed(struct tty_struct *tty, struct uart_state *state, 44ab4382d2SGreg Kroah-Hartman struct ktermios *old_termios); 451f33a51dSJiri Slaby static void uart_wait_until_sent(struct tty_struct *tty, int timeout); 466f538fe3SLinus Walleij static void uart_change_pm(struct uart_state *state, 476f538fe3SLinus Walleij enum uart_pm_state pm_state); 48ab4382d2SGreg Kroah-Hartman 49b922e19dSJiri Slaby static void uart_port_shutdown(struct tty_port *port); 50b922e19dSJiri Slaby 51299245a1SPeter Hurley static int uart_dcd_enabled(struct uart_port *uport) 52299245a1SPeter Hurley { 53d4260b51SPeter Hurley return !!(uport->status & UPSTAT_DCD_ENABLE); 54299245a1SPeter Hurley } 55299245a1SPeter Hurley 569ed19428SPeter Hurley static inline struct uart_port *uart_port_ref(struct uart_state *state) 579ed19428SPeter Hurley { 589ed19428SPeter Hurley if (atomic_add_unless(&state->refcount, 1, 0)) 599ed19428SPeter Hurley return state->uart_port; 609ed19428SPeter Hurley return NULL; 619ed19428SPeter Hurley } 629ed19428SPeter Hurley 639ed19428SPeter Hurley static inline void uart_port_deref(struct uart_port *uport) 649ed19428SPeter Hurley { 65ef510beaSAndy Shevchenko if (atomic_dec_and_test(&uport->state->refcount)) 669ed19428SPeter Hurley wake_up(&uport->state->remove_wait); 679ed19428SPeter Hurley } 689ed19428SPeter Hurley 699ed19428SPeter Hurley #define uart_port_lock(state, flags) \ 709ed19428SPeter Hurley ({ \ 719ed19428SPeter Hurley struct uart_port *__uport = uart_port_ref(state); \ 729ed19428SPeter Hurley if (__uport) \ 739ed19428SPeter Hurley spin_lock_irqsave(&__uport->lock, flags); \ 749ed19428SPeter Hurley __uport; \ 759ed19428SPeter Hurley }) 769ed19428SPeter Hurley 779ed19428SPeter Hurley #define uart_port_unlock(uport, flags) \ 789ed19428SPeter Hurley ({ \ 799ed19428SPeter Hurley struct uart_port *__uport = uport; \ 80ef510beaSAndy Shevchenko if (__uport) { \ 819ed19428SPeter Hurley spin_unlock_irqrestore(&__uport->lock, flags); \ 829ed19428SPeter Hurley uart_port_deref(__uport); \ 83ef510beaSAndy Shevchenko } \ 849ed19428SPeter Hurley }) 859ed19428SPeter Hurley 864047b371SPeter Hurley static inline struct uart_port *uart_port_check(struct uart_state *state) 874047b371SPeter Hurley { 887da4b8b7SPeter Hurley lockdep_assert_held(&state->port.mutex); 894047b371SPeter Hurley return state->uart_port; 904047b371SPeter Hurley } 914047b371SPeter Hurley 92ab4382d2SGreg Kroah-Hartman /* 93ab4382d2SGreg Kroah-Hartman * This routine is used by the interrupt handler to schedule processing in 94ab4382d2SGreg Kroah-Hartman * the software interrupt portion of the driver. 95ab4382d2SGreg Kroah-Hartman */ 96ab4382d2SGreg Kroah-Hartman void uart_write_wakeup(struct uart_port *port) 97ab4382d2SGreg Kroah-Hartman { 98ab4382d2SGreg Kroah-Hartman struct uart_state *state = port->state; 99ab4382d2SGreg Kroah-Hartman /* 100ab4382d2SGreg Kroah-Hartman * This means you called this function _after_ the port was 101ab4382d2SGreg Kroah-Hartman * closed. No cookie for you. 102ab4382d2SGreg Kroah-Hartman */ 103ab4382d2SGreg Kroah-Hartman BUG_ON(!state); 104d0f4bce2SRob Herring tty_port_tty_wakeup(&state->port); 105ab4382d2SGreg Kroah-Hartman } 106ab4382d2SGreg Kroah-Hartman 107ab4382d2SGreg Kroah-Hartman static void uart_stop(struct tty_struct *tty) 108ab4382d2SGreg Kroah-Hartman { 109ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1109ed19428SPeter Hurley struct uart_port *port; 111ab4382d2SGreg Kroah-Hartman unsigned long flags; 112ab4382d2SGreg Kroah-Hartman 1139ed19428SPeter Hurley port = uart_port_lock(state, flags); 1149ed19428SPeter Hurley if (port) 115ab4382d2SGreg Kroah-Hartman port->ops->stop_tx(port); 1169ed19428SPeter Hurley uart_port_unlock(port, flags); 117ab4382d2SGreg Kroah-Hartman } 118ab4382d2SGreg Kroah-Hartman 119ab4382d2SGreg Kroah-Hartman static void __uart_start(struct tty_struct *tty) 120ab4382d2SGreg Kroah-Hartman { 121ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 122ab4382d2SGreg Kroah-Hartman struct uart_port *port = state->uart_port; 123ab4382d2SGreg Kroah-Hartman 1249ed19428SPeter Hurley if (port && !uart_tx_stopped(port)) 125ab4382d2SGreg Kroah-Hartman port->ops->start_tx(port); 126ab4382d2SGreg Kroah-Hartman } 127ab4382d2SGreg Kroah-Hartman 128ab4382d2SGreg Kroah-Hartman static void uart_start(struct tty_struct *tty) 129ab4382d2SGreg Kroah-Hartman { 130ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1319ed19428SPeter Hurley struct uart_port *port; 132ab4382d2SGreg Kroah-Hartman unsigned long flags; 133ab4382d2SGreg Kroah-Hartman 1349ed19428SPeter Hurley port = uart_port_lock(state, flags); 135ab4382d2SGreg Kroah-Hartman __uart_start(tty); 1369ed19428SPeter Hurley uart_port_unlock(port, flags); 137ab4382d2SGreg Kroah-Hartman } 138ab4382d2SGreg Kroah-Hartman 139f4581cabSDenys Vlasenko static void 140ab4382d2SGreg Kroah-Hartman uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear) 141ab4382d2SGreg Kroah-Hartman { 142ab4382d2SGreg Kroah-Hartman unsigned long flags; 143ab4382d2SGreg Kroah-Hartman unsigned int old; 144ab4382d2SGreg Kroah-Hartman 145ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 146ab4382d2SGreg Kroah-Hartman old = port->mctrl; 147ab4382d2SGreg Kroah-Hartman port->mctrl = (old & ~clear) | set; 148ab4382d2SGreg Kroah-Hartman if (old != port->mctrl) 149ab4382d2SGreg Kroah-Hartman port->ops->set_mctrl(port, port->mctrl); 150ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 151ab4382d2SGreg Kroah-Hartman } 152ab4382d2SGreg Kroah-Hartman 153ab4382d2SGreg Kroah-Hartman #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0) 154ab4382d2SGreg Kroah-Hartman #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear) 155ab4382d2SGreg Kroah-Hartman 156a6845e1eSRafael Gago static void uart_port_dtr_rts(struct uart_port *uport, int raise) 157a6845e1eSRafael Gago { 158a6845e1eSRafael Gago int rs485_on = uport->rs485_config && 159a6845e1eSRafael Gago (uport->rs485.flags & SER_RS485_ENABLED); 160a6845e1eSRafael Gago int RTS_after_send = !!(uport->rs485.flags & SER_RS485_RTS_AFTER_SEND); 161a6845e1eSRafael Gago 162a6845e1eSRafael Gago if (raise) { 163a6845e1eSRafael Gago if (rs485_on && !RTS_after_send) { 164a6845e1eSRafael Gago uart_set_mctrl(uport, TIOCM_DTR); 165a6845e1eSRafael Gago uart_clear_mctrl(uport, TIOCM_RTS); 166a6845e1eSRafael Gago } else { 167a6845e1eSRafael Gago uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS); 168a6845e1eSRafael Gago } 169a6845e1eSRafael Gago } else { 170a6845e1eSRafael Gago unsigned int clear = TIOCM_DTR; 171a6845e1eSRafael Gago 172a6845e1eSRafael Gago clear |= (!rs485_on || !RTS_after_send) ? TIOCM_RTS : 0; 173a6845e1eSRafael Gago uart_clear_mctrl(uport, clear); 174a6845e1eSRafael Gago } 175a6845e1eSRafael Gago } 176a6845e1eSRafael Gago 177ab4382d2SGreg Kroah-Hartman /* 178ab4382d2SGreg Kroah-Hartman * Startup the port. This will be called once per open. All calls 179ab4382d2SGreg Kroah-Hartman * will be serialised by the per-port mutex. 180ab4382d2SGreg Kroah-Hartman */ 181c0d92be6SJiri Slaby static int uart_port_startup(struct tty_struct *tty, struct uart_state *state, 182c0d92be6SJiri Slaby int init_hw) 183ab4382d2SGreg Kroah-Hartman { 1844047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 185ab4382d2SGreg Kroah-Hartman unsigned long page; 186a5ba1d95STycho Andersen unsigned long flags = 0; 187ab4382d2SGreg Kroah-Hartman int retval = 0; 188ab4382d2SGreg Kroah-Hartman 189ab4382d2SGreg Kroah-Hartman if (uport->type == PORT_UNKNOWN) 190c0d92be6SJiri Slaby return 1; 191ab4382d2SGreg Kroah-Hartman 192ab4382d2SGreg Kroah-Hartman /* 1937deb39edSThomas Pfaff * Make sure the device is in D0 state. 1947deb39edSThomas Pfaff */ 1957deb39edSThomas Pfaff uart_change_pm(state, UART_PM_STATE_ON); 1967deb39edSThomas Pfaff 1977deb39edSThomas Pfaff /* 198ab4382d2SGreg Kroah-Hartman * Initialise and allocate the transmit and temporary 199ab4382d2SGreg Kroah-Hartman * buffer. 200ab4382d2SGreg Kroah-Hartman */ 201ab4382d2SGreg Kroah-Hartman page = get_zeroed_page(GFP_KERNEL); 202ab4382d2SGreg Kroah-Hartman if (!page) 203ab4382d2SGreg Kroah-Hartman return -ENOMEM; 204ab4382d2SGreg Kroah-Hartman 205a5ba1d95STycho Andersen uart_port_lock(state, flags); 206a5ba1d95STycho Andersen if (!state->xmit.buf) { 207ab4382d2SGreg Kroah-Hartman state->xmit.buf = (unsigned char *) page; 208ab4382d2SGreg Kroah-Hartman uart_circ_clear(&state->xmit); 209d7240214SSergey Senozhatsky uart_port_unlock(uport, flags); 210a5ba1d95STycho Andersen } else { 211d7240214SSergey Senozhatsky uart_port_unlock(uport, flags); 212d7240214SSergey Senozhatsky /* 213d7240214SSergey Senozhatsky * Do not free() the page under the port lock, see 214d7240214SSergey Senozhatsky * uart_shutdown(). 215d7240214SSergey Senozhatsky */ 216a5ba1d95STycho Andersen free_page(page); 217ab4382d2SGreg Kroah-Hartman } 218ab4382d2SGreg Kroah-Hartman 219ab4382d2SGreg Kroah-Hartman retval = uport->ops->startup(uport); 220ab4382d2SGreg Kroah-Hartman if (retval == 0) { 221c7d7abffSJiri Slaby if (uart_console(uport) && uport->cons->cflag) { 222adc8d746SAlan Cox tty->termios.c_cflag = uport->cons->cflag; 223c7d7abffSJiri Slaby uport->cons->cflag = 0; 224c7d7abffSJiri Slaby } 225ab4382d2SGreg Kroah-Hartman /* 226ab4382d2SGreg Kroah-Hartman * Initialise the hardware port settings. 227ab4382d2SGreg Kroah-Hartman */ 228ab4382d2SGreg Kroah-Hartman uart_change_speed(tty, state, NULL); 229ab4382d2SGreg Kroah-Hartman 230ab4382d2SGreg Kroah-Hartman /* 231ab4382d2SGreg Kroah-Hartman * Setup the RTS and DTR signals once the 232ab4382d2SGreg Kroah-Hartman * port is open and ready to respond. 233ab4382d2SGreg Kroah-Hartman */ 2349db276f8SPeter Hurley if (init_hw && C_BAUD(tty)) 235a6845e1eSRafael Gago uart_port_dtr_rts(uport, 1); 236ab4382d2SGreg Kroah-Hartman } 237ab4382d2SGreg Kroah-Hartman 2380055197eSJiri Slaby /* 2390055197eSJiri Slaby * This is to allow setserial on this port. People may want to set 2400055197eSJiri Slaby * port/irq/type and then reconfigure the port properly if it failed 2410055197eSJiri Slaby * now. 2420055197eSJiri Slaby */ 243ab4382d2SGreg Kroah-Hartman if (retval && capable(CAP_SYS_ADMIN)) 244c0d92be6SJiri Slaby return 1; 245c0d92be6SJiri Slaby 246c0d92be6SJiri Slaby return retval; 247c0d92be6SJiri Slaby } 248c0d92be6SJiri Slaby 249c0d92be6SJiri Slaby static int uart_startup(struct tty_struct *tty, struct uart_state *state, 250c0d92be6SJiri Slaby int init_hw) 251c0d92be6SJiri Slaby { 252c0d92be6SJiri Slaby struct tty_port *port = &state->port; 253c0d92be6SJiri Slaby int retval; 254c0d92be6SJiri Slaby 255d41861caSPeter Hurley if (tty_port_initialized(port)) 256c0d92be6SJiri Slaby return 0; 257c0d92be6SJiri Slaby 258c0d92be6SJiri Slaby retval = uart_port_startup(tty, state, init_hw); 259b3b57646SRob Herring if (retval) 260b3b57646SRob Herring set_bit(TTY_IO_ERROR, &tty->flags); 261ab4382d2SGreg Kroah-Hartman 262ab4382d2SGreg Kroah-Hartman return retval; 263ab4382d2SGreg Kroah-Hartman } 264ab4382d2SGreg Kroah-Hartman 265ab4382d2SGreg Kroah-Hartman /* 266ab4382d2SGreg Kroah-Hartman * This routine will shutdown a serial port; interrupts are disabled, and 267ab4382d2SGreg Kroah-Hartman * DTR is dropped if the hangup on close termio flag is on. Calls to 268ab4382d2SGreg Kroah-Hartman * uart_shutdown are serialised by the per-port semaphore. 269af224ca2SPeter Hurley * 270af224ca2SPeter Hurley * uport == NULL if uart_port has already been removed 271ab4382d2SGreg Kroah-Hartman */ 272ab4382d2SGreg Kroah-Hartman static void uart_shutdown(struct tty_struct *tty, struct uart_state *state) 273ab4382d2SGreg Kroah-Hartman { 2744047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 275ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 276a5ba1d95STycho Andersen unsigned long flags = 0; 277d7240214SSergey Senozhatsky char *xmit_buf = NULL; 278ab4382d2SGreg Kroah-Hartman 279ab4382d2SGreg Kroah-Hartman /* 280ab4382d2SGreg Kroah-Hartman * Set the TTY IO error marker 281ab4382d2SGreg Kroah-Hartman */ 282ab4382d2SGreg Kroah-Hartman if (tty) 283ab4382d2SGreg Kroah-Hartman set_bit(TTY_IO_ERROR, &tty->flags); 284ab4382d2SGreg Kroah-Hartman 285d41861caSPeter Hurley if (tty_port_initialized(port)) { 286d41861caSPeter Hurley tty_port_set_initialized(port, 0); 287d41861caSPeter Hurley 288ab4382d2SGreg Kroah-Hartman /* 289ab4382d2SGreg Kroah-Hartman * Turn off DTR and RTS early. 290ab4382d2SGreg Kroah-Hartman */ 291af224ca2SPeter Hurley if (uport && uart_console(uport) && tty) 292ae84db96SPeter Hurley uport->cons->cflag = tty->termios.c_cflag; 293ae84db96SPeter Hurley 2949db276f8SPeter Hurley if (!tty || C_HUPCL(tty)) 295a6845e1eSRafael Gago uart_port_dtr_rts(uport, 0); 296ab4382d2SGreg Kroah-Hartman 297b922e19dSJiri Slaby uart_port_shutdown(port); 298ab4382d2SGreg Kroah-Hartman } 299ab4382d2SGreg Kroah-Hartman 300ab4382d2SGreg Kroah-Hartman /* 301d208a3bfSDoug Anderson * It's possible for shutdown to be called after suspend if we get 302d208a3bfSDoug Anderson * a DCD drop (hangup) at just the right time. Clear suspended bit so 303d208a3bfSDoug Anderson * we don't try to resume a port that has been shutdown. 304ab4382d2SGreg Kroah-Hartman */ 30580f02d54SPeter Hurley tty_port_set_suspended(port, 0); 306ab4382d2SGreg Kroah-Hartman 307ab4382d2SGreg Kroah-Hartman /* 308d7240214SSergey Senozhatsky * Do not free() the transmit buffer page under the port lock since 309d7240214SSergey Senozhatsky * this can create various circular locking scenarios. For instance, 310d7240214SSergey Senozhatsky * console driver may need to allocate/free a debug object, which 311d7240214SSergey Senozhatsky * can endup in printk() recursion. 312ab4382d2SGreg Kroah-Hartman */ 313a5ba1d95STycho Andersen uart_port_lock(state, flags); 314d7240214SSergey Senozhatsky xmit_buf = state->xmit.buf; 315ab4382d2SGreg Kroah-Hartman state->xmit.buf = NULL; 316a5ba1d95STycho Andersen uart_port_unlock(uport, flags); 317d7240214SSergey Senozhatsky 318d7240214SSergey Senozhatsky if (xmit_buf) 319d7240214SSergey Senozhatsky free_page((unsigned long)xmit_buf); 320ab4382d2SGreg Kroah-Hartman } 321ab4382d2SGreg Kroah-Hartman 322ab4382d2SGreg Kroah-Hartman /** 323ab4382d2SGreg Kroah-Hartman * uart_update_timeout - update per-port FIFO timeout. 324ab4382d2SGreg Kroah-Hartman * @port: uart_port structure describing the port 325ab4382d2SGreg Kroah-Hartman * @cflag: termios cflag value 326ab4382d2SGreg Kroah-Hartman * @baud: speed of the port 327ab4382d2SGreg Kroah-Hartman * 328ab4382d2SGreg Kroah-Hartman * Set the port FIFO timeout value. The @cflag value should 329ab4382d2SGreg Kroah-Hartman * reflect the actual hardware settings. 330ab4382d2SGreg Kroah-Hartman */ 331ab4382d2SGreg Kroah-Hartman void 332ab4382d2SGreg Kroah-Hartman uart_update_timeout(struct uart_port *port, unsigned int cflag, 333ab4382d2SGreg Kroah-Hartman unsigned int baud) 334ab4382d2SGreg Kroah-Hartman { 335ab4382d2SGreg Kroah-Hartman unsigned int bits; 336ab4382d2SGreg Kroah-Hartman 337ab4382d2SGreg Kroah-Hartman /* byte size and parity */ 338ab4382d2SGreg Kroah-Hartman switch (cflag & CSIZE) { 339ab4382d2SGreg Kroah-Hartman case CS5: 340ab4382d2SGreg Kroah-Hartman bits = 7; 341ab4382d2SGreg Kroah-Hartman break; 342ab4382d2SGreg Kroah-Hartman case CS6: 343ab4382d2SGreg Kroah-Hartman bits = 8; 344ab4382d2SGreg Kroah-Hartman break; 345ab4382d2SGreg Kroah-Hartman case CS7: 346ab4382d2SGreg Kroah-Hartman bits = 9; 347ab4382d2SGreg Kroah-Hartman break; 348ab4382d2SGreg Kroah-Hartman default: 349ab4382d2SGreg Kroah-Hartman bits = 10; 350ab4382d2SGreg Kroah-Hartman break; /* CS8 */ 351ab4382d2SGreg Kroah-Hartman } 352ab4382d2SGreg Kroah-Hartman 353ab4382d2SGreg Kroah-Hartman if (cflag & CSTOPB) 354ab4382d2SGreg Kroah-Hartman bits++; 355ab4382d2SGreg Kroah-Hartman if (cflag & PARENB) 356ab4382d2SGreg Kroah-Hartman bits++; 357ab4382d2SGreg Kroah-Hartman 358ab4382d2SGreg Kroah-Hartman /* 359ab4382d2SGreg Kroah-Hartman * The total number of bits to be transmitted in the fifo. 360ab4382d2SGreg Kroah-Hartman */ 361ab4382d2SGreg Kroah-Hartman bits = bits * port->fifosize; 362ab4382d2SGreg Kroah-Hartman 363ab4382d2SGreg Kroah-Hartman /* 364ab4382d2SGreg Kroah-Hartman * Figure the timeout to send the above number of bits. 365ab4382d2SGreg Kroah-Hartman * Add .02 seconds of slop 366ab4382d2SGreg Kroah-Hartman */ 367ab4382d2SGreg Kroah-Hartman port->timeout = (HZ * bits) / baud + HZ/50; 368ab4382d2SGreg Kroah-Hartman } 369ab4382d2SGreg Kroah-Hartman 370ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_update_timeout); 371ab4382d2SGreg Kroah-Hartman 372ab4382d2SGreg Kroah-Hartman /** 373ab4382d2SGreg Kroah-Hartman * uart_get_baud_rate - return baud rate for a particular port 374ab4382d2SGreg Kroah-Hartman * @port: uart_port structure describing the port in question. 375ab4382d2SGreg Kroah-Hartman * @termios: desired termios settings. 376ab4382d2SGreg Kroah-Hartman * @old: old termios (or NULL) 377ab4382d2SGreg Kroah-Hartman * @min: minimum acceptable baud rate 378ab4382d2SGreg Kroah-Hartman * @max: maximum acceptable baud rate 379ab4382d2SGreg Kroah-Hartman * 380ab4382d2SGreg Kroah-Hartman * Decode the termios structure into a numeric baud rate, 381ab4382d2SGreg Kroah-Hartman * taking account of the magic 38400 baud rate (with spd_* 382ab4382d2SGreg Kroah-Hartman * flags), and mapping the %B0 rate to 9600 baud. 383ab4382d2SGreg Kroah-Hartman * 384ab4382d2SGreg Kroah-Hartman * If the new baud rate is invalid, try the old termios setting. 385ab4382d2SGreg Kroah-Hartman * If it's still invalid, we try 9600 baud. 386ab4382d2SGreg Kroah-Hartman * 387ab4382d2SGreg Kroah-Hartman * Update the @termios structure to reflect the baud rate 388ab4382d2SGreg Kroah-Hartman * we're actually going to be using. Don't do this for the case 389ab4382d2SGreg Kroah-Hartman * where B0 is requested ("hang up"). 390ab4382d2SGreg Kroah-Hartman */ 391ab4382d2SGreg Kroah-Hartman unsigned int 392ab4382d2SGreg Kroah-Hartman uart_get_baud_rate(struct uart_port *port, struct ktermios *termios, 393ab4382d2SGreg Kroah-Hartman struct ktermios *old, unsigned int min, unsigned int max) 394ab4382d2SGreg Kroah-Hartman { 395f10a2233SJoakim Nordell unsigned int try; 396f10a2233SJoakim Nordell unsigned int baud; 397f10a2233SJoakim Nordell unsigned int altbaud; 398ab4382d2SGreg Kroah-Hartman int hung_up = 0; 399ab4382d2SGreg Kroah-Hartman upf_t flags = port->flags & UPF_SPD_MASK; 400ab4382d2SGreg Kroah-Hartman 401f10a2233SJoakim Nordell switch (flags) { 402f10a2233SJoakim Nordell case UPF_SPD_HI: 403ab4382d2SGreg Kroah-Hartman altbaud = 57600; 404f10a2233SJoakim Nordell break; 405f10a2233SJoakim Nordell case UPF_SPD_VHI: 406ab4382d2SGreg Kroah-Hartman altbaud = 115200; 407f10a2233SJoakim Nordell break; 408f10a2233SJoakim Nordell case UPF_SPD_SHI: 409ab4382d2SGreg Kroah-Hartman altbaud = 230400; 410f10a2233SJoakim Nordell break; 411f10a2233SJoakim Nordell case UPF_SPD_WARP: 412ab4382d2SGreg Kroah-Hartman altbaud = 460800; 413f10a2233SJoakim Nordell break; 414f10a2233SJoakim Nordell default: 415f10a2233SJoakim Nordell altbaud = 38400; 416f10a2233SJoakim Nordell break; 417f10a2233SJoakim Nordell } 418ab4382d2SGreg Kroah-Hartman 419ab4382d2SGreg Kroah-Hartman for (try = 0; try < 2; try++) { 420ab4382d2SGreg Kroah-Hartman baud = tty_termios_baud_rate(termios); 421ab4382d2SGreg Kroah-Hartman 422ab4382d2SGreg Kroah-Hartman /* 423ab4382d2SGreg Kroah-Hartman * The spd_hi, spd_vhi, spd_shi, spd_warp kludge... 424ab4382d2SGreg Kroah-Hartman * Die! Die! Die! 425ab4382d2SGreg Kroah-Hartman */ 426547039ecSPeter Hurley if (try == 0 && baud == 38400) 427ab4382d2SGreg Kroah-Hartman baud = altbaud; 428ab4382d2SGreg Kroah-Hartman 429ab4382d2SGreg Kroah-Hartman /* 430ab4382d2SGreg Kroah-Hartman * Special case: B0 rate. 431ab4382d2SGreg Kroah-Hartman */ 432ab4382d2SGreg Kroah-Hartman if (baud == 0) { 433ab4382d2SGreg Kroah-Hartman hung_up = 1; 434ab4382d2SGreg Kroah-Hartman baud = 9600; 435ab4382d2SGreg Kroah-Hartman } 436ab4382d2SGreg Kroah-Hartman 437ab4382d2SGreg Kroah-Hartman if (baud >= min && baud <= max) 438ab4382d2SGreg Kroah-Hartman return baud; 439ab4382d2SGreg Kroah-Hartman 440ab4382d2SGreg Kroah-Hartman /* 441ab4382d2SGreg Kroah-Hartman * Oops, the quotient was zero. Try again with 442ab4382d2SGreg Kroah-Hartman * the old baud rate if possible. 443ab4382d2SGreg Kroah-Hartman */ 444ab4382d2SGreg Kroah-Hartman termios->c_cflag &= ~CBAUD; 445ab4382d2SGreg Kroah-Hartman if (old) { 446ab4382d2SGreg Kroah-Hartman baud = tty_termios_baud_rate(old); 447ab4382d2SGreg Kroah-Hartman if (!hung_up) 448ab4382d2SGreg Kroah-Hartman tty_termios_encode_baud_rate(termios, 449ab4382d2SGreg Kroah-Hartman baud, baud); 450ab4382d2SGreg Kroah-Hartman old = NULL; 451ab4382d2SGreg Kroah-Hartman continue; 452ab4382d2SGreg Kroah-Hartman } 453ab4382d2SGreg Kroah-Hartman 454ab4382d2SGreg Kroah-Hartman /* 455ab4382d2SGreg Kroah-Hartman * As a last resort, if the range cannot be met then clip to 456ab4382d2SGreg Kroah-Hartman * the nearest chip supported rate. 457ab4382d2SGreg Kroah-Hartman */ 458ab4382d2SGreg Kroah-Hartman if (!hung_up) { 459ab4382d2SGreg Kroah-Hartman if (baud <= min) 460ab4382d2SGreg Kroah-Hartman tty_termios_encode_baud_rate(termios, 461ab4382d2SGreg Kroah-Hartman min + 1, min + 1); 462ab4382d2SGreg Kroah-Hartman else 463ab4382d2SGreg Kroah-Hartman tty_termios_encode_baud_rate(termios, 464ab4382d2SGreg Kroah-Hartman max - 1, max - 1); 465ab4382d2SGreg Kroah-Hartman } 466ab4382d2SGreg Kroah-Hartman } 467ab4382d2SGreg Kroah-Hartman /* Should never happen */ 468ab4382d2SGreg Kroah-Hartman WARN_ON(1); 469ab4382d2SGreg Kroah-Hartman return 0; 470ab4382d2SGreg Kroah-Hartman } 471ab4382d2SGreg Kroah-Hartman 472ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_get_baud_rate); 473ab4382d2SGreg Kroah-Hartman 474ab4382d2SGreg Kroah-Hartman /** 475ab4382d2SGreg Kroah-Hartman * uart_get_divisor - return uart clock divisor 476ab4382d2SGreg Kroah-Hartman * @port: uart_port structure describing the port. 477ab4382d2SGreg Kroah-Hartman * @baud: desired baud rate 478ab4382d2SGreg Kroah-Hartman * 479ab4382d2SGreg Kroah-Hartman * Calculate the uart clock divisor for the port. 480ab4382d2SGreg Kroah-Hartman */ 481ab4382d2SGreg Kroah-Hartman unsigned int 482ab4382d2SGreg Kroah-Hartman uart_get_divisor(struct uart_port *port, unsigned int baud) 483ab4382d2SGreg Kroah-Hartman { 484ab4382d2SGreg Kroah-Hartman unsigned int quot; 485ab4382d2SGreg Kroah-Hartman 486ab4382d2SGreg Kroah-Hartman /* 487ab4382d2SGreg Kroah-Hartman * Old custom speed handling. 488ab4382d2SGreg Kroah-Hartman */ 489ab4382d2SGreg Kroah-Hartman if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST) 490ab4382d2SGreg Kroah-Hartman quot = port->custom_divisor; 491ab4382d2SGreg Kroah-Hartman else 49297d24634SUwe Kleine-König quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud); 493ab4382d2SGreg Kroah-Hartman 494ab4382d2SGreg Kroah-Hartman return quot; 495ab4382d2SGreg Kroah-Hartman } 496ab4382d2SGreg Kroah-Hartman 497ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_get_divisor); 498ab4382d2SGreg Kroah-Hartman 4997c8ab967SPeter Hurley /* Caller holds port mutex */ 500ab4382d2SGreg Kroah-Hartman static void uart_change_speed(struct tty_struct *tty, struct uart_state *state, 501ab4382d2SGreg Kroah-Hartman struct ktermios *old_termios) 502ab4382d2SGreg Kroah-Hartman { 5034047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 504ab4382d2SGreg Kroah-Hartman struct ktermios *termios; 505391f93f2SPeter Hurley int hw_stopped; 506ab4382d2SGreg Kroah-Hartman 507ab4382d2SGreg Kroah-Hartman /* 508ab4382d2SGreg Kroah-Hartman * If we have no tty, termios, or the port does not exist, 509ab4382d2SGreg Kroah-Hartman * then we can't set the parameters for this port. 510ab4382d2SGreg Kroah-Hartman */ 511adc8d746SAlan Cox if (!tty || uport->type == PORT_UNKNOWN) 512ab4382d2SGreg Kroah-Hartman return; 513ab4382d2SGreg Kroah-Hartman 514adc8d746SAlan Cox termios = &tty->termios; 515c18b55fdSPeter Hurley uport->ops->set_termios(uport, termios, old_termios); 516ab4382d2SGreg Kroah-Hartman 517ab4382d2SGreg Kroah-Hartman /* 518299245a1SPeter Hurley * Set modem status enables based on termios cflag 519ab4382d2SGreg Kroah-Hartman */ 520299245a1SPeter Hurley spin_lock_irq(&uport->lock); 521ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CRTSCTS) 522299245a1SPeter Hurley uport->status |= UPSTAT_CTS_ENABLE; 523ab4382d2SGreg Kroah-Hartman else 524299245a1SPeter Hurley uport->status &= ~UPSTAT_CTS_ENABLE; 525ab4382d2SGreg Kroah-Hartman 526ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CLOCAL) 527299245a1SPeter Hurley uport->status &= ~UPSTAT_DCD_ENABLE; 528ab4382d2SGreg Kroah-Hartman else 529299245a1SPeter Hurley uport->status |= UPSTAT_DCD_ENABLE; 530391f93f2SPeter Hurley 531391f93f2SPeter Hurley /* reset sw-assisted CTS flow control based on (possibly) new mode */ 532391f93f2SPeter Hurley hw_stopped = uport->hw_stopped; 533391f93f2SPeter Hurley uport->hw_stopped = uart_softcts_mode(uport) && 534391f93f2SPeter Hurley !(uport->ops->get_mctrl(uport) & TIOCM_CTS); 535391f93f2SPeter Hurley if (uport->hw_stopped) { 536391f93f2SPeter Hurley if (!hw_stopped) 537391f93f2SPeter Hurley uport->ops->stop_tx(uport); 538391f93f2SPeter Hurley } else { 539391f93f2SPeter Hurley if (hw_stopped) 540391f93f2SPeter Hurley __uart_start(tty); 541391f93f2SPeter Hurley } 542299245a1SPeter Hurley spin_unlock_irq(&uport->lock); 543ab4382d2SGreg Kroah-Hartman } 544ab4382d2SGreg Kroah-Hartman 545f5291eccSPeter Hurley static int uart_put_char(struct tty_struct *tty, unsigned char c) 546ab4382d2SGreg Kroah-Hartman { 547f5291eccSPeter Hurley struct uart_state *state = tty->driver_data; 5489ed19428SPeter Hurley struct uart_port *port; 549f5291eccSPeter Hurley struct circ_buf *circ; 550ab4382d2SGreg Kroah-Hartman unsigned long flags; 551ab4382d2SGreg Kroah-Hartman int ret = 0; 552ab4382d2SGreg Kroah-Hartman 553f5291eccSPeter Hurley circ = &state->xmit; 5549ed19428SPeter Hurley port = uart_port_lock(state, flags); 555aff9cf59SSamir Virmani if (!circ->buf) { 556aff9cf59SSamir Virmani uart_port_unlock(port, flags); 557aff9cf59SSamir Virmani return 0; 558aff9cf59SSamir Virmani } 559aff9cf59SSamir Virmani 5609ed19428SPeter Hurley if (port && uart_circ_chars_free(circ) != 0) { 561ab4382d2SGreg Kroah-Hartman circ->buf[circ->head] = c; 562ab4382d2SGreg Kroah-Hartman circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1); 563ab4382d2SGreg Kroah-Hartman ret = 1; 564ab4382d2SGreg Kroah-Hartman } 5659ed19428SPeter Hurley uart_port_unlock(port, flags); 566ab4382d2SGreg Kroah-Hartman return ret; 567ab4382d2SGreg Kroah-Hartman } 568ab4382d2SGreg Kroah-Hartman 569ab4382d2SGreg Kroah-Hartman static void uart_flush_chars(struct tty_struct *tty) 570ab4382d2SGreg Kroah-Hartman { 571ab4382d2SGreg Kroah-Hartman uart_start(tty); 572ab4382d2SGreg Kroah-Hartman } 573ab4382d2SGreg Kroah-Hartman 574ab4382d2SGreg Kroah-Hartman static int uart_write(struct tty_struct *tty, 575ab4382d2SGreg Kroah-Hartman const unsigned char *buf, int count) 576ab4382d2SGreg Kroah-Hartman { 577ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 578ab4382d2SGreg Kroah-Hartman struct uart_port *port; 579ab4382d2SGreg Kroah-Hartman struct circ_buf *circ; 580ab4382d2SGreg Kroah-Hartman unsigned long flags; 581ab4382d2SGreg Kroah-Hartman int c, ret = 0; 582ab4382d2SGreg Kroah-Hartman 583ab4382d2SGreg Kroah-Hartman /* 584ab4382d2SGreg Kroah-Hartman * This means you called this function _after_ the port was 585ab4382d2SGreg Kroah-Hartman * closed. No cookie for you. 586ab4382d2SGreg Kroah-Hartman */ 587ab4382d2SGreg Kroah-Hartman if (!state) { 588ab4382d2SGreg Kroah-Hartman WARN_ON(1); 589ab4382d2SGreg Kroah-Hartman return -EL3HLT; 590ab4382d2SGreg Kroah-Hartman } 591ab4382d2SGreg Kroah-Hartman 5929ed19428SPeter Hurley port = uart_port_lock(state, flags); 593aff9cf59SSamir Virmani circ = &state->xmit; 594aff9cf59SSamir Virmani if (!circ->buf) { 595aff9cf59SSamir Virmani uart_port_unlock(port, flags); 596aff9cf59SSamir Virmani return 0; 597aff9cf59SSamir Virmani } 598aff9cf59SSamir Virmani 5999ed19428SPeter Hurley while (port) { 600ab4382d2SGreg Kroah-Hartman c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE); 601ab4382d2SGreg Kroah-Hartman if (count < c) 602ab4382d2SGreg Kroah-Hartman c = count; 603ab4382d2SGreg Kroah-Hartman if (c <= 0) 604ab4382d2SGreg Kroah-Hartman break; 605ab4382d2SGreg Kroah-Hartman memcpy(circ->buf + circ->head, buf, c); 606ab4382d2SGreg Kroah-Hartman circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1); 607ab4382d2SGreg Kroah-Hartman buf += c; 608ab4382d2SGreg Kroah-Hartman count -= c; 609ab4382d2SGreg Kroah-Hartman ret += c; 610ab4382d2SGreg Kroah-Hartman } 61164dbee31SPeter Hurley 61264dbee31SPeter Hurley __uart_start(tty); 6139ed19428SPeter Hurley uart_port_unlock(port, flags); 614ab4382d2SGreg Kroah-Hartman return ret; 615ab4382d2SGreg Kroah-Hartman } 616ab4382d2SGreg Kroah-Hartman 617ab4382d2SGreg Kroah-Hartman static int uart_write_room(struct tty_struct *tty) 618ab4382d2SGreg Kroah-Hartman { 619ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 6209ed19428SPeter Hurley struct uart_port *port; 621ab4382d2SGreg Kroah-Hartman unsigned long flags; 622ab4382d2SGreg Kroah-Hartman int ret; 623ab4382d2SGreg Kroah-Hartman 6249ed19428SPeter Hurley port = uart_port_lock(state, flags); 625ab4382d2SGreg Kroah-Hartman ret = uart_circ_chars_free(&state->xmit); 6269ed19428SPeter Hurley uart_port_unlock(port, flags); 627ab4382d2SGreg Kroah-Hartman return ret; 628ab4382d2SGreg Kroah-Hartman } 629ab4382d2SGreg Kroah-Hartman 630ab4382d2SGreg Kroah-Hartman static int uart_chars_in_buffer(struct tty_struct *tty) 631ab4382d2SGreg Kroah-Hartman { 632ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 6339ed19428SPeter Hurley struct uart_port *port; 634ab4382d2SGreg Kroah-Hartman unsigned long flags; 635ab4382d2SGreg Kroah-Hartman int ret; 636ab4382d2SGreg Kroah-Hartman 6379ed19428SPeter Hurley port = uart_port_lock(state, flags); 638ab4382d2SGreg Kroah-Hartman ret = uart_circ_chars_pending(&state->xmit); 6399ed19428SPeter Hurley uart_port_unlock(port, flags); 640ab4382d2SGreg Kroah-Hartman return ret; 641ab4382d2SGreg Kroah-Hartman } 642ab4382d2SGreg Kroah-Hartman 643ab4382d2SGreg Kroah-Hartman static void uart_flush_buffer(struct tty_struct *tty) 644ab4382d2SGreg Kroah-Hartman { 645ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 646ab4382d2SGreg Kroah-Hartman struct uart_port *port; 647ab4382d2SGreg Kroah-Hartman unsigned long flags; 648ab4382d2SGreg Kroah-Hartman 649ab4382d2SGreg Kroah-Hartman /* 650ab4382d2SGreg Kroah-Hartman * This means you called this function _after_ the port was 651ab4382d2SGreg Kroah-Hartman * closed. No cookie for you. 652ab4382d2SGreg Kroah-Hartman */ 653ab4382d2SGreg Kroah-Hartman if (!state) { 654ab4382d2SGreg Kroah-Hartman WARN_ON(1); 655ab4382d2SGreg Kroah-Hartman return; 656ab4382d2SGreg Kroah-Hartman } 657ab4382d2SGreg Kroah-Hartman 658ab4382d2SGreg Kroah-Hartman pr_debug("uart_flush_buffer(%d) called\n", tty->index); 659ab4382d2SGreg Kroah-Hartman 6609ed19428SPeter Hurley port = uart_port_lock(state, flags); 6619ed19428SPeter Hurley if (!port) 6629ed19428SPeter Hurley return; 663ab4382d2SGreg Kroah-Hartman uart_circ_clear(&state->xmit); 664ab4382d2SGreg Kroah-Hartman if (port->ops->flush_buffer) 665ab4382d2SGreg Kroah-Hartman port->ops->flush_buffer(port); 6669ed19428SPeter Hurley uart_port_unlock(port, flags); 667d0f4bce2SRob Herring tty_port_tty_wakeup(&state->port); 668ab4382d2SGreg Kroah-Hartman } 669ab4382d2SGreg Kroah-Hartman 670ab4382d2SGreg Kroah-Hartman /* 671ab4382d2SGreg Kroah-Hartman * This function is used to send a high-priority XON/XOFF character to 672ab4382d2SGreg Kroah-Hartman * the device 673ab4382d2SGreg Kroah-Hartman */ 674ab4382d2SGreg Kroah-Hartman static void uart_send_xchar(struct tty_struct *tty, char ch) 675ab4382d2SGreg Kroah-Hartman { 676ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 6779ed19428SPeter Hurley struct uart_port *port; 678ab4382d2SGreg Kroah-Hartman unsigned long flags; 679ab4382d2SGreg Kroah-Hartman 6809ed19428SPeter Hurley port = uart_port_ref(state); 6819ed19428SPeter Hurley if (!port) 6829ed19428SPeter Hurley return; 6839ed19428SPeter Hurley 684ab4382d2SGreg Kroah-Hartman if (port->ops->send_xchar) 685ab4382d2SGreg Kroah-Hartman port->ops->send_xchar(port, ch); 686ab4382d2SGreg Kroah-Hartman else { 687ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 688c235ccc1SPeter Hurley port->x_char = ch; 689c235ccc1SPeter Hurley if (ch) 690ab4382d2SGreg Kroah-Hartman port->ops->start_tx(port); 691ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 692ab4382d2SGreg Kroah-Hartman } 6939ed19428SPeter Hurley uart_port_deref(port); 694ab4382d2SGreg Kroah-Hartman } 695ab4382d2SGreg Kroah-Hartman 696ab4382d2SGreg Kroah-Hartman static void uart_throttle(struct tty_struct *tty) 697ab4382d2SGreg Kroah-Hartman { 698ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 699c5f78b1fSJeremy Kerr upstat_t mask = UPSTAT_SYNC_FIFO; 7009ed19428SPeter Hurley struct uart_port *port; 701ab4382d2SGreg Kroah-Hartman 7029ed19428SPeter Hurley port = uart_port_ref(state); 7039ed19428SPeter Hurley if (!port) 7049ed19428SPeter Hurley return; 7059ed19428SPeter Hurley 706ab4382d2SGreg Kroah-Hartman if (I_IXOFF(tty)) 707391f93f2SPeter Hurley mask |= UPSTAT_AUTOXOFF; 7089db276f8SPeter Hurley if (C_CRTSCTS(tty)) 709391f93f2SPeter Hurley mask |= UPSTAT_AUTORTS; 7109aba8d5bSRussell King 711391f93f2SPeter Hurley if (port->status & mask) { 7129aba8d5bSRussell King port->ops->throttle(port); 713391f93f2SPeter Hurley mask &= ~port->status; 7149aba8d5bSRussell King } 7159aba8d5bSRussell King 716391f93f2SPeter Hurley if (mask & UPSTAT_AUTORTS) 7179aba8d5bSRussell King uart_clear_mctrl(port, TIOCM_RTS); 718b4749b97SPeter Hurley 719b4749b97SPeter Hurley if (mask & UPSTAT_AUTOXOFF) 720b4749b97SPeter Hurley uart_send_xchar(tty, STOP_CHAR(tty)); 7219ed19428SPeter Hurley 7229ed19428SPeter Hurley uart_port_deref(port); 723ab4382d2SGreg Kroah-Hartman } 724ab4382d2SGreg Kroah-Hartman 725ab4382d2SGreg Kroah-Hartman static void uart_unthrottle(struct tty_struct *tty) 726ab4382d2SGreg Kroah-Hartman { 727ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 728c5f78b1fSJeremy Kerr upstat_t mask = UPSTAT_SYNC_FIFO; 7299ed19428SPeter Hurley struct uart_port *port; 730ab4382d2SGreg Kroah-Hartman 7319ed19428SPeter Hurley port = uart_port_ref(state); 7329ed19428SPeter Hurley if (!port) 7339ed19428SPeter Hurley return; 7349ed19428SPeter Hurley 7359aba8d5bSRussell King if (I_IXOFF(tty)) 736391f93f2SPeter Hurley mask |= UPSTAT_AUTOXOFF; 7379db276f8SPeter Hurley if (C_CRTSCTS(tty)) 738391f93f2SPeter Hurley mask |= UPSTAT_AUTORTS; 7399aba8d5bSRussell King 740391f93f2SPeter Hurley if (port->status & mask) { 7419aba8d5bSRussell King port->ops->unthrottle(port); 742391f93f2SPeter Hurley mask &= ~port->status; 7439aba8d5bSRussell King } 7449aba8d5bSRussell King 745391f93f2SPeter Hurley if (mask & UPSTAT_AUTORTS) 746ab4382d2SGreg Kroah-Hartman uart_set_mctrl(port, TIOCM_RTS); 747b4749b97SPeter Hurley 748b4749b97SPeter Hurley if (mask & UPSTAT_AUTOXOFF) 749b4749b97SPeter Hurley uart_send_xchar(tty, START_CHAR(tty)); 7509ed19428SPeter Hurley 7519ed19428SPeter Hurley uart_port_deref(port); 752ab4382d2SGreg Kroah-Hartman } 753ab4382d2SGreg Kroah-Hartman 7544047b371SPeter Hurley static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo) 755ab4382d2SGreg Kroah-Hartman { 7569f109694SAlan Cox struct uart_state *state = container_of(port, struct uart_state, port); 7574047b371SPeter Hurley struct uart_port *uport; 7584047b371SPeter Hurley int ret = -ENODEV; 7597ba2e769SAlan Cox 76037cd0c99SFengguang Wu memset(retinfo, 0, sizeof(*retinfo)); 7617ba2e769SAlan Cox 7623abe8c76SPeter Hurley /* 7633abe8c76SPeter Hurley * Ensure the state we copy is consistent and no hardware changes 7643abe8c76SPeter Hurley * occur as we go 7653abe8c76SPeter Hurley */ 7663abe8c76SPeter Hurley mutex_lock(&port->mutex); 7674047b371SPeter Hurley uport = uart_port_check(state); 7684047b371SPeter Hurley if (!uport) 7694047b371SPeter Hurley goto out; 7704047b371SPeter Hurley 7717ba2e769SAlan Cox retinfo->type = uport->type; 7727ba2e769SAlan Cox retinfo->line = uport->line; 7737ba2e769SAlan Cox retinfo->port = uport->iobase; 7747ba2e769SAlan Cox if (HIGH_BITS_OFFSET) 7757ba2e769SAlan Cox retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET; 7767ba2e769SAlan Cox retinfo->irq = uport->irq; 777a17e74c5SAndy Shevchenko retinfo->flags = (__force int)uport->flags; 7787ba2e769SAlan Cox retinfo->xmit_fifo_size = uport->fifosize; 7797ba2e769SAlan Cox retinfo->baud_base = uport->uartclk / 16; 7807ba2e769SAlan Cox retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10; 7817ba2e769SAlan Cox retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ? 7827ba2e769SAlan Cox ASYNC_CLOSING_WAIT_NONE : 7837ba2e769SAlan Cox jiffies_to_msecs(port->closing_wait) / 10; 7847ba2e769SAlan Cox retinfo->custom_divisor = uport->custom_divisor; 7857ba2e769SAlan Cox retinfo->hub6 = uport->hub6; 7867ba2e769SAlan Cox retinfo->io_type = uport->iotype; 7877ba2e769SAlan Cox retinfo->iomem_reg_shift = uport->regshift; 7887ba2e769SAlan Cox retinfo->iomem_base = (void *)(unsigned long)uport->mapbase; 7894047b371SPeter Hurley 7904047b371SPeter Hurley ret = 0; 7914047b371SPeter Hurley out: 792ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 7934047b371SPeter Hurley return ret; 7949f109694SAlan Cox } 7959f109694SAlan Cox 7965099d234SAl Viro static int uart_get_info_user(struct tty_struct *tty, 7975099d234SAl Viro struct serial_struct *ss) 7989f109694SAlan Cox { 7995099d234SAl Viro struct uart_state *state = tty->driver_data; 8005099d234SAl Viro struct tty_port *port = &state->port; 8013abe8c76SPeter Hurley 8025099d234SAl Viro return uart_get_info(port, ss) < 0 ? -EIO : 0; 803ab4382d2SGreg Kroah-Hartman } 804ab4382d2SGreg Kroah-Hartman 8057ba2e769SAlan Cox static int uart_set_info(struct tty_struct *tty, struct tty_port *port, 8067ba2e769SAlan Cox struct uart_state *state, 8077ba2e769SAlan Cox struct serial_struct *new_info) 808ab4382d2SGreg Kroah-Hartman { 8094047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 810ab4382d2SGreg Kroah-Hartman unsigned long new_port; 811ab4382d2SGreg Kroah-Hartman unsigned int change_irq, change_port, closing_wait; 812ab4382d2SGreg Kroah-Hartman unsigned int old_custom_divisor, close_delay; 813ab4382d2SGreg Kroah-Hartman upf_t old_flags, new_flags; 814ab4382d2SGreg Kroah-Hartman int retval = 0; 815ab4382d2SGreg Kroah-Hartman 8164047b371SPeter Hurley if (!uport) 8174047b371SPeter Hurley return -EIO; 8184047b371SPeter Hurley 8197ba2e769SAlan Cox new_port = new_info->port; 820ab4382d2SGreg Kroah-Hartman if (HIGH_BITS_OFFSET) 8217ba2e769SAlan Cox new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET; 822ab4382d2SGreg Kroah-Hartman 8237ba2e769SAlan Cox new_info->irq = irq_canonicalize(new_info->irq); 8247ba2e769SAlan Cox close_delay = msecs_to_jiffies(new_info->close_delay * 10); 8257ba2e769SAlan Cox closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ? 8264cb0fbfdSJiri Slaby ASYNC_CLOSING_WAIT_NONE : 8277ba2e769SAlan Cox msecs_to_jiffies(new_info->closing_wait * 10); 828ab4382d2SGreg Kroah-Hartman 829ab4382d2SGreg Kroah-Hartman 830ab4382d2SGreg Kroah-Hartman change_irq = !(uport->flags & UPF_FIXED_PORT) 8317ba2e769SAlan Cox && new_info->irq != uport->irq; 832ab4382d2SGreg Kroah-Hartman 833ab4382d2SGreg Kroah-Hartman /* 834ab4382d2SGreg Kroah-Hartman * Since changing the 'type' of the port changes its resource 835ab4382d2SGreg Kroah-Hartman * allocations, we should treat type changes the same as 836ab4382d2SGreg Kroah-Hartman * IO port changes. 837ab4382d2SGreg Kroah-Hartman */ 838ab4382d2SGreg Kroah-Hartman change_port = !(uport->flags & UPF_FIXED_PORT) 839ab4382d2SGreg Kroah-Hartman && (new_port != uport->iobase || 8407ba2e769SAlan Cox (unsigned long)new_info->iomem_base != uport->mapbase || 8417ba2e769SAlan Cox new_info->hub6 != uport->hub6 || 8427ba2e769SAlan Cox new_info->io_type != uport->iotype || 8437ba2e769SAlan Cox new_info->iomem_reg_shift != uport->regshift || 8447ba2e769SAlan Cox new_info->type != uport->type); 845ab4382d2SGreg Kroah-Hartman 846ab4382d2SGreg Kroah-Hartman old_flags = uport->flags; 847a17e74c5SAndy Shevchenko new_flags = (__force upf_t)new_info->flags; 848ab4382d2SGreg Kroah-Hartman old_custom_divisor = uport->custom_divisor; 849ab4382d2SGreg Kroah-Hartman 850ab4382d2SGreg Kroah-Hartman if (!capable(CAP_SYS_ADMIN)) { 851ab4382d2SGreg Kroah-Hartman retval = -EPERM; 852ab4382d2SGreg Kroah-Hartman if (change_irq || change_port || 8537ba2e769SAlan Cox (new_info->baud_base != uport->uartclk / 16) || 854ab4382d2SGreg Kroah-Hartman (close_delay != port->close_delay) || 855ab4382d2SGreg Kroah-Hartman (closing_wait != port->closing_wait) || 8567ba2e769SAlan Cox (new_info->xmit_fifo_size && 8577ba2e769SAlan Cox new_info->xmit_fifo_size != uport->fifosize) || 858ab4382d2SGreg Kroah-Hartman (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0)) 859ab4382d2SGreg Kroah-Hartman goto exit; 860ab4382d2SGreg Kroah-Hartman uport->flags = ((uport->flags & ~UPF_USR_MASK) | 861ab4382d2SGreg Kroah-Hartman (new_flags & UPF_USR_MASK)); 8627ba2e769SAlan Cox uport->custom_divisor = new_info->custom_divisor; 863ab4382d2SGreg Kroah-Hartman goto check_and_exit; 864ab4382d2SGreg Kroah-Hartman } 865ab4382d2SGreg Kroah-Hartman 866794edf30SDavid Howells retval = security_locked_down(LOCKDOWN_TIOCSSERIAL); 867794edf30SDavid Howells if (retval && (change_irq || change_port)) 868794edf30SDavid Howells goto exit; 869794edf30SDavid Howells 870ab4382d2SGreg Kroah-Hartman /* 871ab4382d2SGreg Kroah-Hartman * Ask the low level driver to verify the settings. 872ab4382d2SGreg Kroah-Hartman */ 873ab4382d2SGreg Kroah-Hartman if (uport->ops->verify_port) 8747ba2e769SAlan Cox retval = uport->ops->verify_port(uport, new_info); 875ab4382d2SGreg Kroah-Hartman 8767ba2e769SAlan Cox if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) || 8777ba2e769SAlan Cox (new_info->baud_base < 9600)) 878ab4382d2SGreg Kroah-Hartman retval = -EINVAL; 879ab4382d2SGreg Kroah-Hartman 880ab4382d2SGreg Kroah-Hartman if (retval) 881ab4382d2SGreg Kroah-Hartman goto exit; 882ab4382d2SGreg Kroah-Hartman 883ab4382d2SGreg Kroah-Hartman if (change_port || change_irq) { 884ab4382d2SGreg Kroah-Hartman retval = -EBUSY; 885ab4382d2SGreg Kroah-Hartman 886ab4382d2SGreg Kroah-Hartman /* 887ab4382d2SGreg Kroah-Hartman * Make sure that we are the sole user of this port. 888ab4382d2SGreg Kroah-Hartman */ 889ab4382d2SGreg Kroah-Hartman if (tty_port_users(port) > 1) 890ab4382d2SGreg Kroah-Hartman goto exit; 891ab4382d2SGreg Kroah-Hartman 892ab4382d2SGreg Kroah-Hartman /* 893ab4382d2SGreg Kroah-Hartman * We need to shutdown the serial port at the old 894ab4382d2SGreg Kroah-Hartman * port/type/irq combination. 895ab4382d2SGreg Kroah-Hartman */ 896ab4382d2SGreg Kroah-Hartman uart_shutdown(tty, state); 897ab4382d2SGreg Kroah-Hartman } 898ab4382d2SGreg Kroah-Hartman 899ab4382d2SGreg Kroah-Hartman if (change_port) { 900ab4382d2SGreg Kroah-Hartman unsigned long old_iobase, old_mapbase; 901ab4382d2SGreg Kroah-Hartman unsigned int old_type, old_iotype, old_hub6, old_shift; 902ab4382d2SGreg Kroah-Hartman 903ab4382d2SGreg Kroah-Hartman old_iobase = uport->iobase; 904ab4382d2SGreg Kroah-Hartman old_mapbase = uport->mapbase; 905ab4382d2SGreg Kroah-Hartman old_type = uport->type; 906ab4382d2SGreg Kroah-Hartman old_hub6 = uport->hub6; 907ab4382d2SGreg Kroah-Hartman old_iotype = uport->iotype; 908ab4382d2SGreg Kroah-Hartman old_shift = uport->regshift; 909ab4382d2SGreg Kroah-Hartman 910ab4382d2SGreg Kroah-Hartman /* 911ab4382d2SGreg Kroah-Hartman * Free and release old regions 912ab4382d2SGreg Kroah-Hartman */ 913a7cfaf16SFabio Estevam if (old_type != PORT_UNKNOWN && uport->ops->release_port) 914ab4382d2SGreg Kroah-Hartman uport->ops->release_port(uport); 915ab4382d2SGreg Kroah-Hartman 916ab4382d2SGreg Kroah-Hartman uport->iobase = new_port; 9177ba2e769SAlan Cox uport->type = new_info->type; 9187ba2e769SAlan Cox uport->hub6 = new_info->hub6; 9197ba2e769SAlan Cox uport->iotype = new_info->io_type; 9207ba2e769SAlan Cox uport->regshift = new_info->iomem_reg_shift; 9217ba2e769SAlan Cox uport->mapbase = (unsigned long)new_info->iomem_base; 922ab4382d2SGreg Kroah-Hartman 923ab4382d2SGreg Kroah-Hartman /* 924ab4382d2SGreg Kroah-Hartman * Claim and map the new regions 925ab4382d2SGreg Kroah-Hartman */ 926a7cfaf16SFabio Estevam if (uport->type != PORT_UNKNOWN && uport->ops->request_port) { 927ab4382d2SGreg Kroah-Hartman retval = uport->ops->request_port(uport); 928ab4382d2SGreg Kroah-Hartman } else { 929ab4382d2SGreg Kroah-Hartman /* Always success - Jean II */ 930ab4382d2SGreg Kroah-Hartman retval = 0; 931ab4382d2SGreg Kroah-Hartman } 932ab4382d2SGreg Kroah-Hartman 933ab4382d2SGreg Kroah-Hartman /* 934ab4382d2SGreg Kroah-Hartman * If we fail to request resources for the 935ab4382d2SGreg Kroah-Hartman * new port, try to restore the old settings. 936ab4382d2SGreg Kroah-Hartman */ 9377deb39edSThomas Pfaff if (retval) { 938ab4382d2SGreg Kroah-Hartman uport->iobase = old_iobase; 939ab4382d2SGreg Kroah-Hartman uport->type = old_type; 940ab4382d2SGreg Kroah-Hartman uport->hub6 = old_hub6; 941ab4382d2SGreg Kroah-Hartman uport->iotype = old_iotype; 942ab4382d2SGreg Kroah-Hartman uport->regshift = old_shift; 943ab4382d2SGreg Kroah-Hartman uport->mapbase = old_mapbase; 9447deb39edSThomas Pfaff 9457deb39edSThomas Pfaff if (old_type != PORT_UNKNOWN) { 946ab4382d2SGreg Kroah-Hartman retval = uport->ops->request_port(uport); 947ab4382d2SGreg Kroah-Hartman /* 948ab4382d2SGreg Kroah-Hartman * If we failed to restore the old settings, 949ab4382d2SGreg Kroah-Hartman * we fail like this. 950ab4382d2SGreg Kroah-Hartman */ 951ab4382d2SGreg Kroah-Hartman if (retval) 952ab4382d2SGreg Kroah-Hartman uport->type = PORT_UNKNOWN; 953ab4382d2SGreg Kroah-Hartman 954ab4382d2SGreg Kroah-Hartman /* 955ab4382d2SGreg Kroah-Hartman * We failed anyway. 956ab4382d2SGreg Kroah-Hartman */ 957ab4382d2SGreg Kroah-Hartman retval = -EBUSY; 9587deb39edSThomas Pfaff } 9597deb39edSThomas Pfaff 960ab4382d2SGreg Kroah-Hartman /* Added to return the correct error -Ram Gupta */ 961ab4382d2SGreg Kroah-Hartman goto exit; 962ab4382d2SGreg Kroah-Hartman } 963ab4382d2SGreg Kroah-Hartman } 964ab4382d2SGreg Kroah-Hartman 965ab4382d2SGreg Kroah-Hartman if (change_irq) 9667ba2e769SAlan Cox uport->irq = new_info->irq; 967ab4382d2SGreg Kroah-Hartman if (!(uport->flags & UPF_FIXED_PORT)) 9687ba2e769SAlan Cox uport->uartclk = new_info->baud_base * 16; 969ab4382d2SGreg Kroah-Hartman uport->flags = (uport->flags & ~UPF_CHANGE_MASK) | 970ab4382d2SGreg Kroah-Hartman (new_flags & UPF_CHANGE_MASK); 9717ba2e769SAlan Cox uport->custom_divisor = new_info->custom_divisor; 972ab4382d2SGreg Kroah-Hartman port->close_delay = close_delay; 973ab4382d2SGreg Kroah-Hartman port->closing_wait = closing_wait; 9747ba2e769SAlan Cox if (new_info->xmit_fifo_size) 9757ba2e769SAlan Cox uport->fifosize = new_info->xmit_fifo_size; 976d6c53c0eSJiri Slaby port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0; 977ab4382d2SGreg Kroah-Hartman 978ab4382d2SGreg Kroah-Hartman check_and_exit: 979ab4382d2SGreg Kroah-Hartman retval = 0; 980ab4382d2SGreg Kroah-Hartman if (uport->type == PORT_UNKNOWN) 981ab4382d2SGreg Kroah-Hartman goto exit; 982d41861caSPeter Hurley if (tty_port_initialized(port)) { 983ab4382d2SGreg Kroah-Hartman if (((old_flags ^ uport->flags) & UPF_SPD_MASK) || 984ab4382d2SGreg Kroah-Hartman old_custom_divisor != uport->custom_divisor) { 985ab4382d2SGreg Kroah-Hartman /* 986ab4382d2SGreg Kroah-Hartman * If they're setting up a custom divisor or speed, 987dc2f7271SJohan Hovold * instead of clearing it, then bitch about it. 988ab4382d2SGreg Kroah-Hartman */ 989ab4382d2SGreg Kroah-Hartman if (uport->flags & UPF_SPD_MASK) { 990dc2f7271SJohan Hovold dev_notice_ratelimited(uport->dev, 9912f2dafe7SSudip Mukherjee "%s sets custom speed on %s. This is deprecated.\n", 9922f2dafe7SSudip Mukherjee current->comm, 993429b4749SRasmus Villemoes tty_name(port->tty)); 994ab4382d2SGreg Kroah-Hartman } 995ab4382d2SGreg Kroah-Hartman uart_change_speed(tty, state, NULL); 996ab4382d2SGreg Kroah-Hartman } 997b3b57646SRob Herring } else { 998ab4382d2SGreg Kroah-Hartman retval = uart_startup(tty, state, 1); 99944117a1dSSebastian Andrzej Siewior if (retval == 0) 100044117a1dSSebastian Andrzej Siewior tty_port_set_initialized(port, true); 1001b3b57646SRob Herring if (retval > 0) 1002b3b57646SRob Herring retval = 0; 1003b3b57646SRob Herring } 1004ab4382d2SGreg Kroah-Hartman exit: 10057ba2e769SAlan Cox return retval; 10067ba2e769SAlan Cox } 10077ba2e769SAlan Cox 10085099d234SAl Viro static int uart_set_info_user(struct tty_struct *tty, struct serial_struct *ss) 10097ba2e769SAlan Cox { 10105099d234SAl Viro struct uart_state *state = tty->driver_data; 10117ba2e769SAlan Cox struct tty_port *port = &state->port; 10127ba2e769SAlan Cox int retval; 10137ba2e769SAlan Cox 10145099d234SAl Viro down_write(&tty->termios_rwsem); 10157ba2e769SAlan Cox /* 10167ba2e769SAlan Cox * This semaphore protects port->count. It is also 10177ba2e769SAlan Cox * very useful to prevent opens. Also, take the 10187ba2e769SAlan Cox * port configuration semaphore to make sure that a 10197ba2e769SAlan Cox * module insertion/removal doesn't change anything 10207ba2e769SAlan Cox * under us. 10217ba2e769SAlan Cox */ 10227ba2e769SAlan Cox mutex_lock(&port->mutex); 10235099d234SAl Viro retval = uart_set_info(tty, port, state, ss); 1024ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 10255099d234SAl Viro up_write(&tty->termios_rwsem); 1026ab4382d2SGreg Kroah-Hartman return retval; 1027ab4382d2SGreg Kroah-Hartman } 1028ab4382d2SGreg Kroah-Hartman 1029ab4382d2SGreg Kroah-Hartman /** 1030ab4382d2SGreg Kroah-Hartman * uart_get_lsr_info - get line status register info 1031ab4382d2SGreg Kroah-Hartman * @tty: tty associated with the UART 1032ab4382d2SGreg Kroah-Hartman * @state: UART being queried 1033ab4382d2SGreg Kroah-Hartman * @value: returned modem value 1034ab4382d2SGreg Kroah-Hartman */ 1035ab4382d2SGreg Kroah-Hartman static int uart_get_lsr_info(struct tty_struct *tty, 1036ab4382d2SGreg Kroah-Hartman struct uart_state *state, unsigned int __user *value) 1037ab4382d2SGreg Kroah-Hartman { 10384047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 1039ab4382d2SGreg Kroah-Hartman unsigned int result; 1040ab4382d2SGreg Kroah-Hartman 1041ab4382d2SGreg Kroah-Hartman result = uport->ops->tx_empty(uport); 1042ab4382d2SGreg Kroah-Hartman 1043ab4382d2SGreg Kroah-Hartman /* 1044ab4382d2SGreg Kroah-Hartman * If we're about to load something into the transmit 1045ab4382d2SGreg Kroah-Hartman * register, we'll pretend the transmitter isn't empty to 1046ab4382d2SGreg Kroah-Hartman * avoid a race condition (depending on when the transmit 1047ab4382d2SGreg Kroah-Hartman * interrupt happens). 1048ab4382d2SGreg Kroah-Hartman */ 1049ab4382d2SGreg Kroah-Hartman if (uport->x_char || 1050ab4382d2SGreg Kroah-Hartman ((uart_circ_chars_pending(&state->xmit) > 0) && 1051d01f4d18SPeter Hurley !uart_tx_stopped(uport))) 1052ab4382d2SGreg Kroah-Hartman result &= ~TIOCSER_TEMT; 1053ab4382d2SGreg Kroah-Hartman 1054ab4382d2SGreg Kroah-Hartman return put_user(result, value); 1055ab4382d2SGreg Kroah-Hartman } 1056ab4382d2SGreg Kroah-Hartman 105760b33c13SAlan Cox static int uart_tiocmget(struct tty_struct *tty) 1058ab4382d2SGreg Kroah-Hartman { 1059ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1060ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 10614047b371SPeter Hurley struct uart_port *uport; 1062ab4382d2SGreg Kroah-Hartman int result = -EIO; 1063ab4382d2SGreg Kroah-Hartman 1064ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 10654047b371SPeter Hurley uport = uart_port_check(state); 10664047b371SPeter Hurley if (!uport) 10674047b371SPeter Hurley goto out; 10684047b371SPeter Hurley 106918900ca6SPeter Hurley if (!tty_io_error(tty)) { 1070ab4382d2SGreg Kroah-Hartman result = uport->mctrl; 1071ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 1072ab4382d2SGreg Kroah-Hartman result |= uport->ops->get_mctrl(uport); 1073ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 1074ab4382d2SGreg Kroah-Hartman } 10754047b371SPeter Hurley out: 1076ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 1077ab4382d2SGreg Kroah-Hartman return result; 1078ab4382d2SGreg Kroah-Hartman } 1079ab4382d2SGreg Kroah-Hartman 1080ab4382d2SGreg Kroah-Hartman static int 108120b9d177SAlan Cox uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear) 1082ab4382d2SGreg Kroah-Hartman { 1083ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1084ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 10854047b371SPeter Hurley struct uart_port *uport; 1086ab4382d2SGreg Kroah-Hartman int ret = -EIO; 1087ab4382d2SGreg Kroah-Hartman 1088ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 10894047b371SPeter Hurley uport = uart_port_check(state); 10904047b371SPeter Hurley if (!uport) 10914047b371SPeter Hurley goto out; 10924047b371SPeter Hurley 109318900ca6SPeter Hurley if (!tty_io_error(tty)) { 1094ab4382d2SGreg Kroah-Hartman uart_update_mctrl(uport, set, clear); 1095ab4382d2SGreg Kroah-Hartman ret = 0; 1096ab4382d2SGreg Kroah-Hartman } 10974047b371SPeter Hurley out: 1098ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 1099ab4382d2SGreg Kroah-Hartman return ret; 1100ab4382d2SGreg Kroah-Hartman } 1101ab4382d2SGreg Kroah-Hartman 1102ab4382d2SGreg Kroah-Hartman static int uart_break_ctl(struct tty_struct *tty, int break_state) 1103ab4382d2SGreg Kroah-Hartman { 1104ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1105ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 11064047b371SPeter Hurley struct uart_port *uport; 11074047b371SPeter Hurley int ret = -EIO; 1108ab4382d2SGreg Kroah-Hartman 1109ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 11104047b371SPeter Hurley uport = uart_port_check(state); 11114047b371SPeter Hurley if (!uport) 11124047b371SPeter Hurley goto out; 1113ab4382d2SGreg Kroah-Hartman 11147d73170eSJiangfeng Xiao if (uport->type != PORT_UNKNOWN && uport->ops->break_ctl) 1115ab4382d2SGreg Kroah-Hartman uport->ops->break_ctl(uport, break_state); 11164047b371SPeter Hurley ret = 0; 11174047b371SPeter Hurley out: 1118ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 11194047b371SPeter Hurley return ret; 1120ab4382d2SGreg Kroah-Hartman } 1121ab4382d2SGreg Kroah-Hartman 1122ab4382d2SGreg Kroah-Hartman static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state) 1123ab4382d2SGreg Kroah-Hartman { 1124ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 11254047b371SPeter Hurley struct uart_port *uport; 1126ab4382d2SGreg Kroah-Hartman int flags, ret; 1127ab4382d2SGreg Kroah-Hartman 1128ab4382d2SGreg Kroah-Hartman if (!capable(CAP_SYS_ADMIN)) 1129ab4382d2SGreg Kroah-Hartman return -EPERM; 1130ab4382d2SGreg Kroah-Hartman 1131ab4382d2SGreg Kroah-Hartman /* 1132ab4382d2SGreg Kroah-Hartman * Take the per-port semaphore. This prevents count from 1133ab4382d2SGreg Kroah-Hartman * changing, and hence any extra opens of the port while 1134ab4382d2SGreg Kroah-Hartman * we're auto-configuring. 1135ab4382d2SGreg Kroah-Hartman */ 1136ab4382d2SGreg Kroah-Hartman if (mutex_lock_interruptible(&port->mutex)) 1137ab4382d2SGreg Kroah-Hartman return -ERESTARTSYS; 1138ab4382d2SGreg Kroah-Hartman 11394047b371SPeter Hurley uport = uart_port_check(state); 11404047b371SPeter Hurley if (!uport) { 11414047b371SPeter Hurley ret = -EIO; 11424047b371SPeter Hurley goto out; 11434047b371SPeter Hurley } 11444047b371SPeter Hurley 1145ab4382d2SGreg Kroah-Hartman ret = -EBUSY; 1146ab4382d2SGreg Kroah-Hartman if (tty_port_users(port) == 1) { 1147ab4382d2SGreg Kroah-Hartman uart_shutdown(tty, state); 1148ab4382d2SGreg Kroah-Hartman 1149ab4382d2SGreg Kroah-Hartman /* 1150ab4382d2SGreg Kroah-Hartman * If we already have a port type configured, 1151ab4382d2SGreg Kroah-Hartman * we must release its resources. 1152ab4382d2SGreg Kroah-Hartman */ 1153a7cfaf16SFabio Estevam if (uport->type != PORT_UNKNOWN && uport->ops->release_port) 1154ab4382d2SGreg Kroah-Hartman uport->ops->release_port(uport); 1155ab4382d2SGreg Kroah-Hartman 1156ab4382d2SGreg Kroah-Hartman flags = UART_CONFIG_TYPE; 1157ab4382d2SGreg Kroah-Hartman if (uport->flags & UPF_AUTO_IRQ) 1158ab4382d2SGreg Kroah-Hartman flags |= UART_CONFIG_IRQ; 1159ab4382d2SGreg Kroah-Hartman 1160ab4382d2SGreg Kroah-Hartman /* 1161ab4382d2SGreg Kroah-Hartman * This will claim the ports resources if 1162ab4382d2SGreg Kroah-Hartman * a port is found. 1163ab4382d2SGreg Kroah-Hartman */ 1164ab4382d2SGreg Kroah-Hartman uport->ops->config_port(uport, flags); 1165ab4382d2SGreg Kroah-Hartman 1166ab4382d2SGreg Kroah-Hartman ret = uart_startup(tty, state, 1); 116771456906SSebastian Andrzej Siewior if (ret == 0) 116871456906SSebastian Andrzej Siewior tty_port_set_initialized(port, true); 1169b3b57646SRob Herring if (ret > 0) 1170b3b57646SRob Herring ret = 0; 1171ab4382d2SGreg Kroah-Hartman } 11724047b371SPeter Hurley out: 1173ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 1174ab4382d2SGreg Kroah-Hartman return ret; 1175ab4382d2SGreg Kroah-Hartman } 1176ab4382d2SGreg Kroah-Hartman 11771fdc3106SAlexander Shiyan static void uart_enable_ms(struct uart_port *uport) 11781fdc3106SAlexander Shiyan { 11791fdc3106SAlexander Shiyan /* 11801fdc3106SAlexander Shiyan * Force modem status interrupts on 11811fdc3106SAlexander Shiyan */ 11821fdc3106SAlexander Shiyan if (uport->ops->enable_ms) 11831fdc3106SAlexander Shiyan uport->ops->enable_ms(uport); 11841fdc3106SAlexander Shiyan } 11851fdc3106SAlexander Shiyan 1186ab4382d2SGreg Kroah-Hartman /* 1187ab4382d2SGreg Kroah-Hartman * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change 1188ab4382d2SGreg Kroah-Hartman * - mask passed in arg for lines of interest 1189ab4382d2SGreg Kroah-Hartman * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking) 1190ab4382d2SGreg Kroah-Hartman * Caller should use TIOCGICOUNT to see which one it was 1191ab4382d2SGreg Kroah-Hartman * 1192ab4382d2SGreg Kroah-Hartman * FIXME: This wants extracting into a common all driver implementation 1193ab4382d2SGreg Kroah-Hartman * of TIOCMWAIT using tty_port. 1194ab4382d2SGreg Kroah-Hartman */ 11959ed19428SPeter Hurley static int uart_wait_modem_status(struct uart_state *state, unsigned long arg) 1196ab4382d2SGreg Kroah-Hartman { 11979ed19428SPeter Hurley struct uart_port *uport; 1198ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 1199ab4382d2SGreg Kroah-Hartman DECLARE_WAITQUEUE(wait, current); 1200ab4382d2SGreg Kroah-Hartman struct uart_icount cprev, cnow; 1201ab4382d2SGreg Kroah-Hartman int ret; 1202ab4382d2SGreg Kroah-Hartman 1203ab4382d2SGreg Kroah-Hartman /* 1204ab4382d2SGreg Kroah-Hartman * note the counters on entry 1205ab4382d2SGreg Kroah-Hartman */ 12069ed19428SPeter Hurley uport = uart_port_ref(state); 12079ed19428SPeter Hurley if (!uport) 12089ed19428SPeter Hurley return -EIO; 1209ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 1210ab4382d2SGreg Kroah-Hartman memcpy(&cprev, &uport->icount, sizeof(struct uart_icount)); 12111fdc3106SAlexander Shiyan uart_enable_ms(uport); 1212ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 1213ab4382d2SGreg Kroah-Hartman 1214ab4382d2SGreg Kroah-Hartman add_wait_queue(&port->delta_msr_wait, &wait); 1215ab4382d2SGreg Kroah-Hartman for (;;) { 1216ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 1217ab4382d2SGreg Kroah-Hartman memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); 1218ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 1219ab4382d2SGreg Kroah-Hartman 1220ab4382d2SGreg Kroah-Hartman set_current_state(TASK_INTERRUPTIBLE); 1221ab4382d2SGreg Kroah-Hartman 1222ab4382d2SGreg Kroah-Hartman if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 1223ab4382d2SGreg Kroah-Hartman ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 1224ab4382d2SGreg Kroah-Hartman ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || 1225ab4382d2SGreg Kroah-Hartman ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { 1226ab4382d2SGreg Kroah-Hartman ret = 0; 1227ab4382d2SGreg Kroah-Hartman break; 1228ab4382d2SGreg Kroah-Hartman } 1229ab4382d2SGreg Kroah-Hartman 1230ab4382d2SGreg Kroah-Hartman schedule(); 1231ab4382d2SGreg Kroah-Hartman 1232ab4382d2SGreg Kroah-Hartman /* see if a signal did it */ 1233ab4382d2SGreg Kroah-Hartman if (signal_pending(current)) { 1234ab4382d2SGreg Kroah-Hartman ret = -ERESTARTSYS; 1235ab4382d2SGreg Kroah-Hartman break; 1236ab4382d2SGreg Kroah-Hartman } 1237ab4382d2SGreg Kroah-Hartman 1238ab4382d2SGreg Kroah-Hartman cprev = cnow; 1239ab4382d2SGreg Kroah-Hartman } 124097f9f707SFabian Frederick __set_current_state(TASK_RUNNING); 1241ab4382d2SGreg Kroah-Hartman remove_wait_queue(&port->delta_msr_wait, &wait); 12429ed19428SPeter Hurley uart_port_deref(uport); 1243ab4382d2SGreg Kroah-Hartman 1244ab4382d2SGreg Kroah-Hartman return ret; 1245ab4382d2SGreg Kroah-Hartman } 1246ab4382d2SGreg Kroah-Hartman 1247ab4382d2SGreg Kroah-Hartman /* 1248ab4382d2SGreg Kroah-Hartman * Get counter of input serial line interrupts (DCD,RI,DSR,CTS) 1249ab4382d2SGreg Kroah-Hartman * Return: write counters to the user passed counter struct 1250ab4382d2SGreg Kroah-Hartman * NB: both 1->0 and 0->1 transitions are counted except for 1251ab4382d2SGreg Kroah-Hartman * RI where only 0->1 is counted. 1252ab4382d2SGreg Kroah-Hartman */ 1253ab4382d2SGreg Kroah-Hartman static int uart_get_icount(struct tty_struct *tty, 1254ab4382d2SGreg Kroah-Hartman struct serial_icounter_struct *icount) 1255ab4382d2SGreg Kroah-Hartman { 1256ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1257ab4382d2SGreg Kroah-Hartman struct uart_icount cnow; 12589ed19428SPeter Hurley struct uart_port *uport; 1259ab4382d2SGreg Kroah-Hartman 12609ed19428SPeter Hurley uport = uart_port_ref(state); 12619ed19428SPeter Hurley if (!uport) 12629ed19428SPeter Hurley return -EIO; 1263ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 1264ab4382d2SGreg Kroah-Hartman memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); 1265ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 12669ed19428SPeter Hurley uart_port_deref(uport); 1267ab4382d2SGreg Kroah-Hartman 1268ab4382d2SGreg Kroah-Hartman icount->cts = cnow.cts; 1269ab4382d2SGreg Kroah-Hartman icount->dsr = cnow.dsr; 1270ab4382d2SGreg Kroah-Hartman icount->rng = cnow.rng; 1271ab4382d2SGreg Kroah-Hartman icount->dcd = cnow.dcd; 1272ab4382d2SGreg Kroah-Hartman icount->rx = cnow.rx; 1273ab4382d2SGreg Kroah-Hartman icount->tx = cnow.tx; 1274ab4382d2SGreg Kroah-Hartman icount->frame = cnow.frame; 1275ab4382d2SGreg Kroah-Hartman icount->overrun = cnow.overrun; 1276ab4382d2SGreg Kroah-Hartman icount->parity = cnow.parity; 1277ab4382d2SGreg Kroah-Hartman icount->brk = cnow.brk; 1278ab4382d2SGreg Kroah-Hartman icount->buf_overrun = cnow.buf_overrun; 1279ab4382d2SGreg Kroah-Hartman 1280ab4382d2SGreg Kroah-Hartman return 0; 1281ab4382d2SGreg Kroah-Hartman } 1282ab4382d2SGreg Kroah-Hartman 1283a5f276f1SRicardo Ribalda Delgado static int uart_get_rs485_config(struct uart_port *port, 1284a5f276f1SRicardo Ribalda Delgado struct serial_rs485 __user *rs485) 1285a5f276f1SRicardo Ribalda Delgado { 1286bd737f87SRicardo Ribalda Delgado unsigned long flags; 1287bd737f87SRicardo Ribalda Delgado struct serial_rs485 aux; 1288bd737f87SRicardo Ribalda Delgado 1289bd737f87SRicardo Ribalda Delgado spin_lock_irqsave(&port->lock, flags); 1290bd737f87SRicardo Ribalda Delgado aux = port->rs485; 1291bd737f87SRicardo Ribalda Delgado spin_unlock_irqrestore(&port->lock, flags); 1292bd737f87SRicardo Ribalda Delgado 1293bd737f87SRicardo Ribalda Delgado if (copy_to_user(rs485, &aux, sizeof(aux))) 1294a5f276f1SRicardo Ribalda Delgado return -EFAULT; 1295bd737f87SRicardo Ribalda Delgado 1296a5f276f1SRicardo Ribalda Delgado return 0; 1297a5f276f1SRicardo Ribalda Delgado } 1298a5f276f1SRicardo Ribalda Delgado 1299a5f276f1SRicardo Ribalda Delgado static int uart_set_rs485_config(struct uart_port *port, 1300a5f276f1SRicardo Ribalda Delgado struct serial_rs485 __user *rs485_user) 1301a5f276f1SRicardo Ribalda Delgado { 1302a5f276f1SRicardo Ribalda Delgado struct serial_rs485 rs485; 1303a5f276f1SRicardo Ribalda Delgado int ret; 1304bd737f87SRicardo Ribalda Delgado unsigned long flags; 1305a5f276f1SRicardo Ribalda Delgado 1306a5f276f1SRicardo Ribalda Delgado if (!port->rs485_config) 1307a5f276f1SRicardo Ribalda Delgado return -ENOIOCTLCMD; 1308a5f276f1SRicardo Ribalda Delgado 1309a5f276f1SRicardo Ribalda Delgado if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user))) 1310a5f276f1SRicardo Ribalda Delgado return -EFAULT; 1311a5f276f1SRicardo Ribalda Delgado 1312bd737f87SRicardo Ribalda Delgado spin_lock_irqsave(&port->lock, flags); 1313a5f276f1SRicardo Ribalda Delgado ret = port->rs485_config(port, &rs485); 1314bd737f87SRicardo Ribalda Delgado spin_unlock_irqrestore(&port->lock, flags); 1315a5f276f1SRicardo Ribalda Delgado if (ret) 1316a5f276f1SRicardo Ribalda Delgado return ret; 1317a5f276f1SRicardo Ribalda Delgado 1318a5f276f1SRicardo Ribalda Delgado if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485))) 1319a5f276f1SRicardo Ribalda Delgado return -EFAULT; 1320a5f276f1SRicardo Ribalda Delgado 1321a5f276f1SRicardo Ribalda Delgado return 0; 1322a5f276f1SRicardo Ribalda Delgado } 1323a5f276f1SRicardo Ribalda Delgado 1324ad8c0eaaSNicolas Ferre static int uart_get_iso7816_config(struct uart_port *port, 1325ad8c0eaaSNicolas Ferre struct serial_iso7816 __user *iso7816) 1326ad8c0eaaSNicolas Ferre { 1327ad8c0eaaSNicolas Ferre unsigned long flags; 1328ad8c0eaaSNicolas Ferre struct serial_iso7816 aux; 1329ad8c0eaaSNicolas Ferre 1330ad8c0eaaSNicolas Ferre if (!port->iso7816_config) 1331ad8c0eaaSNicolas Ferre return -ENOIOCTLCMD; 1332ad8c0eaaSNicolas Ferre 1333ad8c0eaaSNicolas Ferre spin_lock_irqsave(&port->lock, flags); 1334ad8c0eaaSNicolas Ferre aux = port->iso7816; 1335ad8c0eaaSNicolas Ferre spin_unlock_irqrestore(&port->lock, flags); 1336ad8c0eaaSNicolas Ferre 1337ad8c0eaaSNicolas Ferre if (copy_to_user(iso7816, &aux, sizeof(aux))) 1338ad8c0eaaSNicolas Ferre return -EFAULT; 1339ad8c0eaaSNicolas Ferre 1340ad8c0eaaSNicolas Ferre return 0; 1341ad8c0eaaSNicolas Ferre } 1342ad8c0eaaSNicolas Ferre 1343ad8c0eaaSNicolas Ferre static int uart_set_iso7816_config(struct uart_port *port, 1344ad8c0eaaSNicolas Ferre struct serial_iso7816 __user *iso7816_user) 1345ad8c0eaaSNicolas Ferre { 1346ad8c0eaaSNicolas Ferre struct serial_iso7816 iso7816; 1347ad8c0eaaSNicolas Ferre int i, ret; 1348ad8c0eaaSNicolas Ferre unsigned long flags; 1349ad8c0eaaSNicolas Ferre 1350ad8c0eaaSNicolas Ferre if (!port->iso7816_config) 1351ad8c0eaaSNicolas Ferre return -ENOIOCTLCMD; 1352ad8c0eaaSNicolas Ferre 1353ad8c0eaaSNicolas Ferre if (copy_from_user(&iso7816, iso7816_user, sizeof(*iso7816_user))) 1354ad8c0eaaSNicolas Ferre return -EFAULT; 1355ad8c0eaaSNicolas Ferre 1356ad8c0eaaSNicolas Ferre /* 1357ad8c0eaaSNicolas Ferre * There are 5 words reserved for future use. Check that userspace 1358ad8c0eaaSNicolas Ferre * doesn't put stuff in there to prevent breakages in the future. 1359ad8c0eaaSNicolas Ferre */ 1360ad8c0eaaSNicolas Ferre for (i = 0; i < 5; i++) 1361ad8c0eaaSNicolas Ferre if (iso7816.reserved[i]) 1362ad8c0eaaSNicolas Ferre return -EINVAL; 1363ad8c0eaaSNicolas Ferre 1364ad8c0eaaSNicolas Ferre spin_lock_irqsave(&port->lock, flags); 1365ad8c0eaaSNicolas Ferre ret = port->iso7816_config(port, &iso7816); 1366ad8c0eaaSNicolas Ferre spin_unlock_irqrestore(&port->lock, flags); 1367ad8c0eaaSNicolas Ferre if (ret) 1368ad8c0eaaSNicolas Ferre return ret; 1369ad8c0eaaSNicolas Ferre 1370ad8c0eaaSNicolas Ferre if (copy_to_user(iso7816_user, &port->iso7816, sizeof(port->iso7816))) 1371ad8c0eaaSNicolas Ferre return -EFAULT; 1372ad8c0eaaSNicolas Ferre 1373ad8c0eaaSNicolas Ferre return 0; 1374ad8c0eaaSNicolas Ferre } 1375ad8c0eaaSNicolas Ferre 1376ab4382d2SGreg Kroah-Hartman /* 1377ab4382d2SGreg Kroah-Hartman * Called via sys_ioctl. We can use spin_lock_irq() here. 1378ab4382d2SGreg Kroah-Hartman */ 1379ab4382d2SGreg Kroah-Hartman static int 13804047b371SPeter Hurley uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg) 1381ab4382d2SGreg Kroah-Hartman { 1382ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1383ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 13844047b371SPeter Hurley struct uart_port *uport; 1385ab4382d2SGreg Kroah-Hartman void __user *uarg = (void __user *)arg; 1386ab4382d2SGreg Kroah-Hartman int ret = -ENOIOCTLCMD; 1387ab4382d2SGreg Kroah-Hartman 1388ab4382d2SGreg Kroah-Hartman 1389ab4382d2SGreg Kroah-Hartman /* 1390ab4382d2SGreg Kroah-Hartman * These ioctls don't rely on the hardware to be present. 1391ab4382d2SGreg Kroah-Hartman */ 1392ab4382d2SGreg Kroah-Hartman switch (cmd) { 1393ab4382d2SGreg Kroah-Hartman case TIOCSERCONFIG: 13947c8ab967SPeter Hurley down_write(&tty->termios_rwsem); 1395ab4382d2SGreg Kroah-Hartman ret = uart_do_autoconfig(tty, state); 13967c8ab967SPeter Hurley up_write(&tty->termios_rwsem); 1397ab4382d2SGreg Kroah-Hartman break; 1398ab4382d2SGreg Kroah-Hartman } 1399ab4382d2SGreg Kroah-Hartman 1400ab4382d2SGreg Kroah-Hartman if (ret != -ENOIOCTLCMD) 1401ab4382d2SGreg Kroah-Hartman goto out; 1402ab4382d2SGreg Kroah-Hartman 140318900ca6SPeter Hurley if (tty_io_error(tty)) { 1404ab4382d2SGreg Kroah-Hartman ret = -EIO; 1405ab4382d2SGreg Kroah-Hartman goto out; 1406ab4382d2SGreg Kroah-Hartman } 1407ab4382d2SGreg Kroah-Hartman 1408ab4382d2SGreg Kroah-Hartman /* 1409ab4382d2SGreg Kroah-Hartman * The following should only be used when hardware is present. 1410ab4382d2SGreg Kroah-Hartman */ 1411ab4382d2SGreg Kroah-Hartman switch (cmd) { 1412ab4382d2SGreg Kroah-Hartman case TIOCMIWAIT: 1413ab4382d2SGreg Kroah-Hartman ret = uart_wait_modem_status(state, arg); 1414ab4382d2SGreg Kroah-Hartman break; 1415ab4382d2SGreg Kroah-Hartman } 1416ab4382d2SGreg Kroah-Hartman 1417ab4382d2SGreg Kroah-Hartman if (ret != -ENOIOCTLCMD) 1418ab4382d2SGreg Kroah-Hartman goto out; 1419ab4382d2SGreg Kroah-Hartman 1420ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 14214047b371SPeter Hurley uport = uart_port_check(state); 1422ab4382d2SGreg Kroah-Hartman 14234047b371SPeter Hurley if (!uport || tty_io_error(tty)) { 1424ab4382d2SGreg Kroah-Hartman ret = -EIO; 1425ab4382d2SGreg Kroah-Hartman goto out_up; 1426ab4382d2SGreg Kroah-Hartman } 1427ab4382d2SGreg Kroah-Hartman 1428ab4382d2SGreg Kroah-Hartman /* 1429ab4382d2SGreg Kroah-Hartman * All these rely on hardware being present and need to be 1430ab4382d2SGreg Kroah-Hartman * protected against the tty being hung up. 1431ab4382d2SGreg Kroah-Hartman */ 1432a9c20a9cSRicardo Ribalda Delgado 1433ab4382d2SGreg Kroah-Hartman switch (cmd) { 1434a9c20a9cSRicardo Ribalda Delgado case TIOCSERGETLSR: /* Get line status register */ 1435a9c20a9cSRicardo Ribalda Delgado ret = uart_get_lsr_info(tty, state, uarg); 1436a9c20a9cSRicardo Ribalda Delgado break; 1437a9c20a9cSRicardo Ribalda Delgado 1438a5f276f1SRicardo Ribalda Delgado case TIOCGRS485: 14394047b371SPeter Hurley ret = uart_get_rs485_config(uport, uarg); 1440a5f276f1SRicardo Ribalda Delgado break; 1441a5f276f1SRicardo Ribalda Delgado 1442a5f276f1SRicardo Ribalda Delgado case TIOCSRS485: 14434047b371SPeter Hurley ret = uart_set_rs485_config(uport, uarg); 1444a5f276f1SRicardo Ribalda Delgado break; 1445ad8c0eaaSNicolas Ferre 1446ad8c0eaaSNicolas Ferre case TIOCSISO7816: 1447ad8c0eaaSNicolas Ferre ret = uart_set_iso7816_config(state->uart_port, uarg); 1448ad8c0eaaSNicolas Ferre break; 1449ad8c0eaaSNicolas Ferre 1450ad8c0eaaSNicolas Ferre case TIOCGISO7816: 1451ad8c0eaaSNicolas Ferre ret = uart_get_iso7816_config(state->uart_port, uarg); 1452ad8c0eaaSNicolas Ferre break; 14534047b371SPeter Hurley default: 1454ab4382d2SGreg Kroah-Hartman if (uport->ops->ioctl) 1455ab4382d2SGreg Kroah-Hartman ret = uport->ops->ioctl(uport, cmd, arg); 1456ab4382d2SGreg Kroah-Hartman break; 1457ab4382d2SGreg Kroah-Hartman } 1458ab4382d2SGreg Kroah-Hartman out_up: 1459ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 1460ab4382d2SGreg Kroah-Hartman out: 1461ab4382d2SGreg Kroah-Hartman return ret; 1462ab4382d2SGreg Kroah-Hartman } 1463ab4382d2SGreg Kroah-Hartman 1464ab4382d2SGreg Kroah-Hartman static void uart_set_ldisc(struct tty_struct *tty) 1465ab4382d2SGreg Kroah-Hartman { 1466ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 14674047b371SPeter Hurley struct uart_port *uport; 1468ab4382d2SGreg Kroah-Hartman 1469db1b9dfcSPeter Hurley mutex_lock(&state->port.mutex); 14704047b371SPeter Hurley uport = uart_port_check(state); 14714047b371SPeter Hurley if (uport && uport->ops->set_ldisc) 1472732a84a0SPeter Hurley uport->ops->set_ldisc(uport, &tty->termios); 1473db1b9dfcSPeter Hurley mutex_unlock(&state->port.mutex); 1474db1b9dfcSPeter Hurley } 1475ab4382d2SGreg Kroah-Hartman 1476ab4382d2SGreg Kroah-Hartman static void uart_set_termios(struct tty_struct *tty, 1477ab4382d2SGreg Kroah-Hartman struct ktermios *old_termios) 1478ab4382d2SGreg Kroah-Hartman { 1479ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 14804047b371SPeter Hurley struct uart_port *uport; 1481adc8d746SAlan Cox unsigned int cflag = tty->termios.c_cflag; 14822cbacafdSRussell King unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK; 14832cbacafdSRussell King bool sw_changed = false; 1484ab4382d2SGreg Kroah-Hartman 14854047b371SPeter Hurley mutex_lock(&state->port.mutex); 14864047b371SPeter Hurley uport = uart_port_check(state); 14874047b371SPeter Hurley if (!uport) 14884047b371SPeter Hurley goto out; 14894047b371SPeter Hurley 14902cbacafdSRussell King /* 14912cbacafdSRussell King * Drivers doing software flow control also need to know 14922cbacafdSRussell King * about changes to these input settings. 14932cbacafdSRussell King */ 14942cbacafdSRussell King if (uport->flags & UPF_SOFT_FLOW) { 14952cbacafdSRussell King iflag_mask |= IXANY|IXON|IXOFF; 14962cbacafdSRussell King sw_changed = 14972cbacafdSRussell King tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] || 14982cbacafdSRussell King tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP]; 14992cbacafdSRussell King } 1500ab4382d2SGreg Kroah-Hartman 1501ab4382d2SGreg Kroah-Hartman /* 1502ab4382d2SGreg Kroah-Hartman * These are the bits that are used to setup various 1503ab4382d2SGreg Kroah-Hartman * flags in the low level driver. We can ignore the Bfoo 1504ab4382d2SGreg Kroah-Hartman * bits in c_cflag; c_[io]speed will always be set 1505ab4382d2SGreg Kroah-Hartman * appropriately by set_termios() in tty_ioctl.c 1506ab4382d2SGreg Kroah-Hartman */ 1507ab4382d2SGreg Kroah-Hartman if ((cflag ^ old_termios->c_cflag) == 0 && 1508adc8d746SAlan Cox tty->termios.c_ospeed == old_termios->c_ospeed && 1509adc8d746SAlan Cox tty->termios.c_ispeed == old_termios->c_ispeed && 15102cbacafdSRussell King ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 && 15112cbacafdSRussell King !sw_changed) { 15124047b371SPeter Hurley goto out; 1513ab4382d2SGreg Kroah-Hartman } 1514ab4382d2SGreg Kroah-Hartman 1515ab4382d2SGreg Kroah-Hartman uart_change_speed(tty, state, old_termios); 1516c7a6b9e4SHariprasad Kelam /* reload cflag from termios; port driver may have overridden flags */ 1517c18b55fdSPeter Hurley cflag = tty->termios.c_cflag; 1518ab4382d2SGreg Kroah-Hartman 1519ab4382d2SGreg Kroah-Hartman /* Handle transition to B0 status */ 1520ab4382d2SGreg Kroah-Hartman if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) 1521dec94e70SRussell King uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR); 1522ab4382d2SGreg Kroah-Hartman /* Handle transition away from B0 status */ 1523ab4382d2SGreg Kroah-Hartman else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) { 1524ab4382d2SGreg Kroah-Hartman unsigned int mask = TIOCM_DTR; 152597ef38b8SPeter Hurley if (!(cflag & CRTSCTS) || !tty_throttled(tty)) 1526ab4382d2SGreg Kroah-Hartman mask |= TIOCM_RTS; 1527dec94e70SRussell King uart_set_mctrl(uport, mask); 1528ab4382d2SGreg Kroah-Hartman } 15294047b371SPeter Hurley out: 15304047b371SPeter Hurley mutex_unlock(&state->port.mutex); 1531ab4382d2SGreg Kroah-Hartman } 1532ab4382d2SGreg Kroah-Hartman 1533ab4382d2SGreg Kroah-Hartman /* 1534ef4f527cSKevin Cernekee * Calls to uart_close() are serialised via the tty_lock in 1535ef4f527cSKevin Cernekee * drivers/tty/tty_io.c:tty_release() 1536ef4f527cSKevin Cernekee * drivers/tty/tty_io.c:do_tty_hangup() 1537ab4382d2SGreg Kroah-Hartman */ 1538ab4382d2SGreg Kroah-Hartman static void uart_close(struct tty_struct *tty, struct file *filp) 1539ab4382d2SGreg Kroah-Hartman { 1540ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1541ab4382d2SGreg Kroah-Hartman 154291b32f54SPeter Hurley if (!state) { 154391b32f54SPeter Hurley struct uart_driver *drv = tty->driver->driver_state; 15449356335fSColin Ian King struct tty_port *port; 154591b32f54SPeter Hurley 154691b32f54SPeter Hurley state = drv->state + tty->index; 154791b32f54SPeter Hurley port = &state->port; 154891b32f54SPeter Hurley spin_lock_irq(&port->lock); 154991b32f54SPeter Hurley --port->count; 155091b32f54SPeter Hurley spin_unlock_irq(&port->lock); 1551ab4382d2SGreg Kroah-Hartman return; 155291b32f54SPeter Hurley } 1553ab4382d2SGreg Kroah-Hartman 155439b3d892SPeter Hurley pr_debug("uart_close(%d) called\n", tty->index); 1555ab4382d2SGreg Kroah-Hartman 1556761ed4a9SRob Herring tty_port_close(tty->port, tty, filp); 1557761ed4a9SRob Herring } 1558ab4382d2SGreg Kroah-Hartman 1559761ed4a9SRob Herring static void uart_tty_port_shutdown(struct tty_port *port) 1560761ed4a9SRob Herring { 1561761ed4a9SRob Herring struct uart_state *state = container_of(port, struct uart_state, port); 1562761ed4a9SRob Herring struct uart_port *uport = uart_port_check(state); 1563af224ca2SPeter Hurley 1564ab4382d2SGreg Kroah-Hartman /* 1565ab4382d2SGreg Kroah-Hartman * At this point, we stop accepting input. To do this, we 1566ab4382d2SGreg Kroah-Hartman * disable the receive line status interrupts. 1567ab4382d2SGreg Kroah-Hartman */ 1568a5a2b130SAndy Shevchenko if (WARN(!uport, "detached port still initialized!\n")) 1569a5a2b130SAndy Shevchenko return; 1570761ed4a9SRob Herring 1571a5a2b130SAndy Shevchenko spin_lock_irq(&uport->lock); 1572ab4382d2SGreg Kroah-Hartman uport->ops->stop_rx(uport); 15736fb98fb3SPeter Hurley spin_unlock_irq(&uport->lock); 1574761ed4a9SRob Herring 1575761ed4a9SRob Herring uart_port_shutdown(port); 1576761ed4a9SRob Herring 1577ab4382d2SGreg Kroah-Hartman /* 1578761ed4a9SRob Herring * It's possible for shutdown to be called after suspend if we get 1579761ed4a9SRob Herring * a DCD drop (hangup) at just the right time. Clear suspended bit so 1580761ed4a9SRob Herring * we don't try to resume a port that has been shutdown. 1581ab4382d2SGreg Kroah-Hartman */ 1582761ed4a9SRob Herring tty_port_set_suspended(port, 0); 1583ab4382d2SGreg Kroah-Hartman 15846f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_OFF); 1585ab4382d2SGreg Kroah-Hartman 1586ab4382d2SGreg Kroah-Hartman } 1587ab4382d2SGreg Kroah-Hartman 15881f33a51dSJiri Slaby static void uart_wait_until_sent(struct tty_struct *tty, int timeout) 1589ab4382d2SGreg Kroah-Hartman { 15901f33a51dSJiri Slaby struct uart_state *state = tty->driver_data; 15919ed19428SPeter Hurley struct uart_port *port; 1592ab4382d2SGreg Kroah-Hartman unsigned long char_time, expire; 1593ab4382d2SGreg Kroah-Hartman 15949ed19428SPeter Hurley port = uart_port_ref(state); 1595ef510beaSAndy Shevchenko if (!port) 1596ef510beaSAndy Shevchenko return; 1597ef510beaSAndy Shevchenko 1598ef510beaSAndy Shevchenko if (port->type == PORT_UNKNOWN || port->fifosize == 0) { 15999ed19428SPeter Hurley uart_port_deref(port); 1600ab4382d2SGreg Kroah-Hartman return; 16019ed19428SPeter Hurley } 1602ab4382d2SGreg Kroah-Hartman 1603ab4382d2SGreg Kroah-Hartman /* 1604ab4382d2SGreg Kroah-Hartman * Set the check interval to be 1/5 of the estimated time to 1605ab4382d2SGreg Kroah-Hartman * send a single character, and make it at least 1. The check 1606ab4382d2SGreg Kroah-Hartman * interval should also be less than the timeout. 1607ab4382d2SGreg Kroah-Hartman * 1608ab4382d2SGreg Kroah-Hartman * Note: we have to use pretty tight timings here to satisfy 1609ab4382d2SGreg Kroah-Hartman * the NIST-PCTS. 1610ab4382d2SGreg Kroah-Hartman */ 1611ab4382d2SGreg Kroah-Hartman char_time = (port->timeout - HZ/50) / port->fifosize; 1612ab4382d2SGreg Kroah-Hartman char_time = char_time / 5; 1613ab4382d2SGreg Kroah-Hartman if (char_time == 0) 1614ab4382d2SGreg Kroah-Hartman char_time = 1; 1615ab4382d2SGreg Kroah-Hartman if (timeout && timeout < char_time) 1616ab4382d2SGreg Kroah-Hartman char_time = timeout; 1617ab4382d2SGreg Kroah-Hartman 1618ab4382d2SGreg Kroah-Hartman /* 1619ab4382d2SGreg Kroah-Hartman * If the transmitter hasn't cleared in twice the approximate 1620ab4382d2SGreg Kroah-Hartman * amount of time to send the entire FIFO, it probably won't 1621ab4382d2SGreg Kroah-Hartman * ever clear. This assumes the UART isn't doing flow 1622ab4382d2SGreg Kroah-Hartman * control, which is currently the case. Hence, if it ever 1623ab4382d2SGreg Kroah-Hartman * takes longer than port->timeout, this is probably due to a 1624ab4382d2SGreg Kroah-Hartman * UART bug of some kind. So, we clamp the timeout parameter at 1625ab4382d2SGreg Kroah-Hartman * 2*port->timeout. 1626ab4382d2SGreg Kroah-Hartman */ 1627ab4382d2SGreg Kroah-Hartman if (timeout == 0 || timeout > 2 * port->timeout) 1628ab4382d2SGreg Kroah-Hartman timeout = 2 * port->timeout; 1629ab4382d2SGreg Kroah-Hartman 1630ab4382d2SGreg Kroah-Hartman expire = jiffies + timeout; 1631ab4382d2SGreg Kroah-Hartman 1632ab4382d2SGreg Kroah-Hartman pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n", 1633ab4382d2SGreg Kroah-Hartman port->line, jiffies, expire); 1634ab4382d2SGreg Kroah-Hartman 1635ab4382d2SGreg Kroah-Hartman /* 1636ab4382d2SGreg Kroah-Hartman * Check whether the transmitter is empty every 'char_time'. 1637ab4382d2SGreg Kroah-Hartman * 'timeout' / 'expire' give us the maximum amount of time 1638ab4382d2SGreg Kroah-Hartman * we wait. 1639ab4382d2SGreg Kroah-Hartman */ 1640ab4382d2SGreg Kroah-Hartman while (!port->ops->tx_empty(port)) { 1641ab4382d2SGreg Kroah-Hartman msleep_interruptible(jiffies_to_msecs(char_time)); 1642ab4382d2SGreg Kroah-Hartman if (signal_pending(current)) 1643ab4382d2SGreg Kroah-Hartman break; 1644ab4382d2SGreg Kroah-Hartman if (time_after(jiffies, expire)) 1645ab4382d2SGreg Kroah-Hartman break; 1646ab4382d2SGreg Kroah-Hartman } 16479ed19428SPeter Hurley uart_port_deref(port); 1648ab4382d2SGreg Kroah-Hartman } 1649ab4382d2SGreg Kroah-Hartman 1650ab4382d2SGreg Kroah-Hartman /* 1651ef4f527cSKevin Cernekee * Calls to uart_hangup() are serialised by the tty_lock in 1652ef4f527cSKevin Cernekee * drivers/tty/tty_io.c:do_tty_hangup() 1653ef4f527cSKevin Cernekee * This runs from a workqueue and can sleep for a _short_ time only. 1654ab4382d2SGreg Kroah-Hartman */ 1655ab4382d2SGreg Kroah-Hartman static void uart_hangup(struct tty_struct *tty) 1656ab4382d2SGreg Kroah-Hartman { 1657ab4382d2SGreg Kroah-Hartman struct uart_state *state = tty->driver_data; 1658ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 1659af224ca2SPeter Hurley struct uart_port *uport; 1660ab4382d2SGreg Kroah-Hartman unsigned long flags; 1661ab4382d2SGreg Kroah-Hartman 166239b3d892SPeter Hurley pr_debug("uart_hangup(%d)\n", tty->index); 1663ab4382d2SGreg Kroah-Hartman 1664ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 1665af224ca2SPeter Hurley uport = uart_port_check(state); 1666af224ca2SPeter Hurley WARN(!uport, "hangup of detached port!\n"); 1667af224ca2SPeter Hurley 1668807c8d81SPeter Hurley if (tty_port_active(port)) { 1669ab4382d2SGreg Kroah-Hartman uart_flush_buffer(tty); 1670ab4382d2SGreg Kroah-Hartman uart_shutdown(tty, state); 1671ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 1672ab4382d2SGreg Kroah-Hartman port->count = 0; 1673ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 1674807c8d81SPeter Hurley tty_port_set_active(port, 0); 1675ab4382d2SGreg Kroah-Hartman tty_port_tty_set(port, NULL); 1676af224ca2SPeter Hurley if (uport && !uart_console(uport)) 1677bf903c0cSGeert Uytterhoeven uart_change_pm(state, UART_PM_STATE_OFF); 1678ab4382d2SGreg Kroah-Hartman wake_up_interruptible(&port->open_wait); 1679ab4382d2SGreg Kroah-Hartman wake_up_interruptible(&port->delta_msr_wait); 1680ab4382d2SGreg Kroah-Hartman } 1681ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 1682ab4382d2SGreg Kroah-Hartman } 1683ab4382d2SGreg Kroah-Hartman 1684af224ca2SPeter Hurley /* uport == NULL if uart_port has already been removed */ 16850b1db830SJiri Slaby static void uart_port_shutdown(struct tty_port *port) 16860b1db830SJiri Slaby { 1687b922e19dSJiri Slaby struct uart_state *state = container_of(port, struct uart_state, port); 16884047b371SPeter Hurley struct uart_port *uport = uart_port_check(state); 1689b922e19dSJiri Slaby 1690b922e19dSJiri Slaby /* 1691b922e19dSJiri Slaby * clear delta_msr_wait queue to avoid mem leaks: we may free 1692b922e19dSJiri Slaby * the irq here so the queue might never be woken up. Note 1693b922e19dSJiri Slaby * that we won't end up waiting on delta_msr_wait again since 1694b922e19dSJiri Slaby * any outstanding file descriptors should be pointing at 1695b922e19dSJiri Slaby * hung_up_tty_fops now. 1696b922e19dSJiri Slaby */ 1697b922e19dSJiri Slaby wake_up_interruptible(&port->delta_msr_wait); 1698b922e19dSJiri Slaby 1699b922e19dSJiri Slaby /* 1700b922e19dSJiri Slaby * Free the IRQ and disable the port. 1701b922e19dSJiri Slaby */ 1702af224ca2SPeter Hurley if (uport) 1703b922e19dSJiri Slaby uport->ops->shutdown(uport); 1704b922e19dSJiri Slaby 1705b922e19dSJiri Slaby /* 1706b922e19dSJiri Slaby * Ensure that the IRQ handler isn't running on another CPU. 1707b922e19dSJiri Slaby */ 1708af224ca2SPeter Hurley if (uport) 1709b922e19dSJiri Slaby synchronize_irq(uport->irq); 17100b1db830SJiri Slaby } 17110b1db830SJiri Slaby 1712ab4382d2SGreg Kroah-Hartman static int uart_carrier_raised(struct tty_port *port) 1713ab4382d2SGreg Kroah-Hartman { 1714ab4382d2SGreg Kroah-Hartman struct uart_state *state = container_of(port, struct uart_state, port); 17159ed19428SPeter Hurley struct uart_port *uport; 1716ab4382d2SGreg Kroah-Hartman int mctrl; 17179ed19428SPeter Hurley 17189ed19428SPeter Hurley uport = uart_port_ref(state); 17199ed19428SPeter Hurley /* 17209ed19428SPeter Hurley * Should never observe uport == NULL since checks for hangup should 17219ed19428SPeter Hurley * abort the tty_port_block_til_ready() loop before checking for carrier 17229ed19428SPeter Hurley * raised -- but report carrier raised if it does anyway so open will 17239ed19428SPeter Hurley * continue and not sleep 17249ed19428SPeter Hurley */ 17259ed19428SPeter Hurley if (WARN_ON(!uport)) 17269ed19428SPeter Hurley return 1; 1727ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 17281fdc3106SAlexander Shiyan uart_enable_ms(uport); 1729ab4382d2SGreg Kroah-Hartman mctrl = uport->ops->get_mctrl(uport); 1730ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 17319ed19428SPeter Hurley uart_port_deref(uport); 1732ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_CAR) 1733ab4382d2SGreg Kroah-Hartman return 1; 1734ab4382d2SGreg Kroah-Hartman return 0; 1735ab4382d2SGreg Kroah-Hartman } 1736ab4382d2SGreg Kroah-Hartman 1737a6845e1eSRafael Gago static void uart_dtr_rts(struct tty_port *port, int raise) 1738ab4382d2SGreg Kroah-Hartman { 1739ab4382d2SGreg Kroah-Hartman struct uart_state *state = container_of(port, struct uart_state, port); 17409ed19428SPeter Hurley struct uart_port *uport; 17419ed19428SPeter Hurley 17429ed19428SPeter Hurley uport = uart_port_ref(state); 17439ed19428SPeter Hurley if (!uport) 17449ed19428SPeter Hurley return; 1745a6845e1eSRafael Gago uart_port_dtr_rts(uport, raise); 17469ed19428SPeter Hurley uart_port_deref(uport); 1747ab4382d2SGreg Kroah-Hartman } 1748ab4382d2SGreg Kroah-Hartman 17494cdd17baSJiri Slaby static int uart_install(struct tty_driver *driver, struct tty_struct *tty) 17504cdd17baSJiri Slaby { 17514cdd17baSJiri Slaby struct uart_driver *drv = driver->driver_state; 17524cdd17baSJiri Slaby struct uart_state *state = drv->state + tty->index; 17534cdd17baSJiri Slaby 17544cdd17baSJiri Slaby tty->driver_data = state; 17554cdd17baSJiri Slaby 17564cdd17baSJiri Slaby return tty_standard_install(driver, tty); 17574cdd17baSJiri Slaby } 17584cdd17baSJiri Slaby 1759ab4382d2SGreg Kroah-Hartman /* 1760ef4f527cSKevin Cernekee * Calls to uart_open are serialised by the tty_lock in 1761ef4f527cSKevin Cernekee * drivers/tty/tty_io.c:tty_open() 1762ab4382d2SGreg Kroah-Hartman * Note that if this fails, then uart_close() _will_ be called. 1763ab4382d2SGreg Kroah-Hartman * 1764ab4382d2SGreg Kroah-Hartman * In time, we want to scrap the "opening nonpresent ports" 1765ab4382d2SGreg Kroah-Hartman * behaviour and implement an alternative way for setserial 1766ab4382d2SGreg Kroah-Hartman * to set base addresses/ports/types. This will allow us to 1767ab4382d2SGreg Kroah-Hartman * get rid of a certain amount of extra tests. 1768ab4382d2SGreg Kroah-Hartman */ 1769ab4382d2SGreg Kroah-Hartman static int uart_open(struct tty_struct *tty, struct file *filp) 1770ab4382d2SGreg Kroah-Hartman { 17714cdd17baSJiri Slaby struct uart_state *state = tty->driver_data; 17724cdd17baSJiri Slaby int retval; 1773b3b57646SRob Herring 1774b3b57646SRob Herring retval = tty_port_open(&state->port, tty, filp); 1775b3b57646SRob Herring if (retval > 0) 1776b3b57646SRob Herring retval = 0; 1777b3b57646SRob Herring 1778b3b57646SRob Herring return retval; 1779b3b57646SRob Herring } 1780b3b57646SRob Herring 1781b3b57646SRob Herring static int uart_port_activate(struct tty_port *port, struct tty_struct *tty) 1782b3b57646SRob Herring { 1783b3b57646SRob Herring struct uart_state *state = container_of(port, struct uart_state, port); 1784b3b57646SRob Herring struct uart_port *uport; 178513b18d35SSerge Semin int ret; 1786b3b57646SRob Herring 1787b3b57646SRob Herring uport = uart_port_check(state); 1788b3b57646SRob Herring if (!uport || uport->flags & UPF_DEAD) 1789b3b57646SRob Herring return -ENXIO; 1790b3b57646SRob Herring 17914047b371SPeter Hurley port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0; 1792ab4382d2SGreg Kroah-Hartman 1793ab4382d2SGreg Kroah-Hartman /* 1794ab4382d2SGreg Kroah-Hartman * Start up the serial port. 1795ab4382d2SGreg Kroah-Hartman */ 179613b18d35SSerge Semin ret = uart_startup(tty, state, 0); 179713b18d35SSerge Semin if (ret > 0) 179813b18d35SSerge Semin tty_port_set_active(port, 1); 179913b18d35SSerge Semin 180013b18d35SSerge Semin return ret; 1801ab4382d2SGreg Kroah-Hartman } 1802ab4382d2SGreg Kroah-Hartman 1803ab4382d2SGreg Kroah-Hartman static const char *uart_type(struct uart_port *port) 1804ab4382d2SGreg Kroah-Hartman { 1805ab4382d2SGreg Kroah-Hartman const char *str = NULL; 1806ab4382d2SGreg Kroah-Hartman 1807ab4382d2SGreg Kroah-Hartman if (port->ops->type) 1808ab4382d2SGreg Kroah-Hartman str = port->ops->type(port); 1809ab4382d2SGreg Kroah-Hartman 1810ab4382d2SGreg Kroah-Hartman if (!str) 1811ab4382d2SGreg Kroah-Hartman str = "unknown"; 1812ab4382d2SGreg Kroah-Hartman 1813ab4382d2SGreg Kroah-Hartman return str; 1814ab4382d2SGreg Kroah-Hartman } 1815ab4382d2SGreg Kroah-Hartman 1816ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PROC_FS 1817ab4382d2SGreg Kroah-Hartman 1818ab4382d2SGreg Kroah-Hartman static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i) 1819ab4382d2SGreg Kroah-Hartman { 1820ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + i; 1821ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 18226f538fe3SLinus Walleij enum uart_pm_state pm_state; 18234047b371SPeter Hurley struct uart_port *uport; 1824ab4382d2SGreg Kroah-Hartman char stat_buf[32]; 1825ab4382d2SGreg Kroah-Hartman unsigned int status; 1826ab4382d2SGreg Kroah-Hartman int mmio; 1827ab4382d2SGreg Kroah-Hartman 18284047b371SPeter Hurley mutex_lock(&port->mutex); 18294047b371SPeter Hurley uport = uart_port_check(state); 1830ab4382d2SGreg Kroah-Hartman if (!uport) 18314047b371SPeter Hurley goto out; 1832ab4382d2SGreg Kroah-Hartman 1833ab4382d2SGreg Kroah-Hartman mmio = uport->iotype >= UPIO_MEM; 1834ab4382d2SGreg Kroah-Hartman seq_printf(m, "%d: uart:%s %s%08llX irq:%d", 1835ab4382d2SGreg Kroah-Hartman uport->line, uart_type(uport), 1836ab4382d2SGreg Kroah-Hartman mmio ? "mmio:0x" : "port:", 1837ab4382d2SGreg Kroah-Hartman mmio ? (unsigned long long)uport->mapbase 1838ab4382d2SGreg Kroah-Hartman : (unsigned long long)uport->iobase, 1839ab4382d2SGreg Kroah-Hartman uport->irq); 1840ab4382d2SGreg Kroah-Hartman 1841ab4382d2SGreg Kroah-Hartman if (uport->type == PORT_UNKNOWN) { 1842ab4382d2SGreg Kroah-Hartman seq_putc(m, '\n'); 18434047b371SPeter Hurley goto out; 1844ab4382d2SGreg Kroah-Hartman } 1845ab4382d2SGreg Kroah-Hartman 1846ab4382d2SGreg Kroah-Hartman if (capable(CAP_SYS_ADMIN)) { 1847ab4382d2SGreg Kroah-Hartman pm_state = state->pm_state; 18486f538fe3SLinus Walleij if (pm_state != UART_PM_STATE_ON) 18496f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_ON); 1850ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 1851ab4382d2SGreg Kroah-Hartman status = uport->ops->get_mctrl(uport); 1852ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 18536f538fe3SLinus Walleij if (pm_state != UART_PM_STATE_ON) 1854ab4382d2SGreg Kroah-Hartman uart_change_pm(state, pm_state); 1855ab4382d2SGreg Kroah-Hartman 1856ab4382d2SGreg Kroah-Hartman seq_printf(m, " tx:%d rx:%d", 1857ab4382d2SGreg Kroah-Hartman uport->icount.tx, uport->icount.rx); 1858ab4382d2SGreg Kroah-Hartman if (uport->icount.frame) 1859968af298SPeter Hurley seq_printf(m, " fe:%d", uport->icount.frame); 1860ab4382d2SGreg Kroah-Hartman if (uport->icount.parity) 1861968af298SPeter Hurley seq_printf(m, " pe:%d", uport->icount.parity); 1862ab4382d2SGreg Kroah-Hartman if (uport->icount.brk) 1863968af298SPeter Hurley seq_printf(m, " brk:%d", uport->icount.brk); 1864ab4382d2SGreg Kroah-Hartman if (uport->icount.overrun) 1865968af298SPeter Hurley seq_printf(m, " oe:%d", uport->icount.overrun); 18664f794097SJeremy Kerr if (uport->icount.buf_overrun) 18674f794097SJeremy Kerr seq_printf(m, " bo:%d", uport->icount.buf_overrun); 1868ab4382d2SGreg Kroah-Hartman 1869ab4382d2SGreg Kroah-Hartman #define INFOBIT(bit, str) \ 1870ab4382d2SGreg Kroah-Hartman if (uport->mctrl & (bit)) \ 1871ab4382d2SGreg Kroah-Hartman strncat(stat_buf, (str), sizeof(stat_buf) - \ 1872ab4382d2SGreg Kroah-Hartman strlen(stat_buf) - 2) 1873ab4382d2SGreg Kroah-Hartman #define STATBIT(bit, str) \ 1874ab4382d2SGreg Kroah-Hartman if (status & (bit)) \ 1875ab4382d2SGreg Kroah-Hartman strncat(stat_buf, (str), sizeof(stat_buf) - \ 1876ab4382d2SGreg Kroah-Hartman strlen(stat_buf) - 2) 1877ab4382d2SGreg Kroah-Hartman 1878ab4382d2SGreg Kroah-Hartman stat_buf[0] = '\0'; 1879ab4382d2SGreg Kroah-Hartman stat_buf[1] = '\0'; 1880ab4382d2SGreg Kroah-Hartman INFOBIT(TIOCM_RTS, "|RTS"); 1881ab4382d2SGreg Kroah-Hartman STATBIT(TIOCM_CTS, "|CTS"); 1882ab4382d2SGreg Kroah-Hartman INFOBIT(TIOCM_DTR, "|DTR"); 1883ab4382d2SGreg Kroah-Hartman STATBIT(TIOCM_DSR, "|DSR"); 1884ab4382d2SGreg Kroah-Hartman STATBIT(TIOCM_CAR, "|CD"); 1885ab4382d2SGreg Kroah-Hartman STATBIT(TIOCM_RNG, "|RI"); 1886ab4382d2SGreg Kroah-Hartman if (stat_buf[0]) 1887ab4382d2SGreg Kroah-Hartman stat_buf[0] = ' '; 1888ab4382d2SGreg Kroah-Hartman 1889ab4382d2SGreg Kroah-Hartman seq_puts(m, stat_buf); 1890ab4382d2SGreg Kroah-Hartman } 1891ab4382d2SGreg Kroah-Hartman seq_putc(m, '\n'); 1892ab4382d2SGreg Kroah-Hartman #undef STATBIT 1893ab4382d2SGreg Kroah-Hartman #undef INFOBIT 18944047b371SPeter Hurley out: 18954047b371SPeter Hurley mutex_unlock(&port->mutex); 1896ab4382d2SGreg Kroah-Hartman } 1897ab4382d2SGreg Kroah-Hartman 1898ab4382d2SGreg Kroah-Hartman static int uart_proc_show(struct seq_file *m, void *v) 1899ab4382d2SGreg Kroah-Hartman { 1900ab4382d2SGreg Kroah-Hartman struct tty_driver *ttydrv = m->private; 1901ab4382d2SGreg Kroah-Hartman struct uart_driver *drv = ttydrv->driver_state; 1902ab4382d2SGreg Kroah-Hartman int i; 1903ab4382d2SGreg Kroah-Hartman 1904968af298SPeter Hurley seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", ""); 1905ab4382d2SGreg Kroah-Hartman for (i = 0; i < drv->nr; i++) 1906ab4382d2SGreg Kroah-Hartman uart_line_info(m, drv, i); 1907ab4382d2SGreg Kroah-Hartman return 0; 1908ab4382d2SGreg Kroah-Hartman } 1909ab4382d2SGreg Kroah-Hartman #endif 1910ab4382d2SGreg Kroah-Hartman 1911ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL) 19121cfe42b7SPeter Hurley /** 1913ab4382d2SGreg Kroah-Hartman * uart_console_write - write a console message to a serial port 1914ab4382d2SGreg Kroah-Hartman * @port: the port to write the message 1915ab4382d2SGreg Kroah-Hartman * @s: array of characters 1916ab4382d2SGreg Kroah-Hartman * @count: number of characters in string to write 191710afbe34SPeter Hurley * @putchar: function to write character to port 1918ab4382d2SGreg Kroah-Hartman */ 1919ab4382d2SGreg Kroah-Hartman void uart_console_write(struct uart_port *port, const char *s, 1920ab4382d2SGreg Kroah-Hartman unsigned int count, 1921ab4382d2SGreg Kroah-Hartman void (*putchar)(struct uart_port *, int)) 1922ab4382d2SGreg Kroah-Hartman { 1923ab4382d2SGreg Kroah-Hartman unsigned int i; 1924ab4382d2SGreg Kroah-Hartman 1925ab4382d2SGreg Kroah-Hartman for (i = 0; i < count; i++, s++) { 1926ab4382d2SGreg Kroah-Hartman if (*s == '\n') 1927ab4382d2SGreg Kroah-Hartman putchar(port, '\r'); 1928ab4382d2SGreg Kroah-Hartman putchar(port, *s); 1929ab4382d2SGreg Kroah-Hartman } 1930ab4382d2SGreg Kroah-Hartman } 1931ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_console_write); 1932ab4382d2SGreg Kroah-Hartman 1933ab4382d2SGreg Kroah-Hartman /* 1934ab4382d2SGreg Kroah-Hartman * Check whether an invalid uart number has been specified, and 1935ab4382d2SGreg Kroah-Hartman * if so, search for the first available port that does have 1936ab4382d2SGreg Kroah-Hartman * console support. 1937ab4382d2SGreg Kroah-Hartman */ 1938ab4382d2SGreg Kroah-Hartman struct uart_port * __init 1939ab4382d2SGreg Kroah-Hartman uart_get_console(struct uart_port *ports, int nr, struct console *co) 1940ab4382d2SGreg Kroah-Hartman { 1941ab4382d2SGreg Kroah-Hartman int idx = co->index; 1942ab4382d2SGreg Kroah-Hartman 1943ab4382d2SGreg Kroah-Hartman if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 && 1944ab4382d2SGreg Kroah-Hartman ports[idx].membase == NULL)) 1945ab4382d2SGreg Kroah-Hartman for (idx = 0; idx < nr; idx++) 1946ab4382d2SGreg Kroah-Hartman if (ports[idx].iobase != 0 || 1947ab4382d2SGreg Kroah-Hartman ports[idx].membase != NULL) 1948ab4382d2SGreg Kroah-Hartman break; 1949ab4382d2SGreg Kroah-Hartman 1950ab4382d2SGreg Kroah-Hartman co->index = idx; 1951ab4382d2SGreg Kroah-Hartman 1952ab4382d2SGreg Kroah-Hartman return ports + idx; 1953ab4382d2SGreg Kroah-Hartman } 1954ab4382d2SGreg Kroah-Hartman 1955ab4382d2SGreg Kroah-Hartman /** 195673abaf87SPeter Hurley * uart_parse_earlycon - Parse earlycon options 195773abaf87SPeter Hurley * @p: ptr to 2nd field (ie., just beyond '<name>,') 195873abaf87SPeter Hurley * @iotype: ptr for decoded iotype (out) 195973abaf87SPeter Hurley * @addr: ptr for decoded mapbase/iobase (out) 196073abaf87SPeter Hurley * @options: ptr for <options> field; NULL if not present (out) 196173abaf87SPeter Hurley * 196273abaf87SPeter Hurley * Decodes earlycon kernel command line parameters of the form 1963bd94c407SMasahiro Yamada * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options> 1964bd94c407SMasahiro Yamada * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options> 196573abaf87SPeter Hurley * 196673abaf87SPeter Hurley * The optional form 1967ff30283aSRandy Dunlap * 196873abaf87SPeter Hurley * earlycon=<name>,0x<addr>,<options> 196973abaf87SPeter Hurley * console=<name>,0x<addr>,<options> 1970ff30283aSRandy Dunlap * 197173abaf87SPeter Hurley * is also accepted; the returned @iotype will be UPIO_MEM. 197273abaf87SPeter Hurley * 19738b2303deSAlexander Sverdlin * Returns 0 on success or -EINVAL on failure 197473abaf87SPeter Hurley */ 197546e36683SAlexander Sverdlin int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr, 197673abaf87SPeter Hurley char **options) 197773abaf87SPeter Hurley { 197873abaf87SPeter Hurley if (strncmp(p, "mmio,", 5) == 0) { 197973abaf87SPeter Hurley *iotype = UPIO_MEM; 198073abaf87SPeter Hurley p += 5; 1981bd94c407SMasahiro Yamada } else if (strncmp(p, "mmio16,", 7) == 0) { 1982bd94c407SMasahiro Yamada *iotype = UPIO_MEM16; 1983bd94c407SMasahiro Yamada p += 7; 198473abaf87SPeter Hurley } else if (strncmp(p, "mmio32,", 7) == 0) { 198573abaf87SPeter Hurley *iotype = UPIO_MEM32; 198673abaf87SPeter Hurley p += 7; 19876e63be3fSNoam Camus } else if (strncmp(p, "mmio32be,", 9) == 0) { 19886e63be3fSNoam Camus *iotype = UPIO_MEM32BE; 19896e63be3fSNoam Camus p += 9; 1990d215d809SMax Filippov } else if (strncmp(p, "mmio32native,", 13) == 0) { 1991d215d809SMax Filippov *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ? 1992d215d809SMax Filippov UPIO_MEM32BE : UPIO_MEM32; 1993d215d809SMax Filippov p += 13; 199473abaf87SPeter Hurley } else if (strncmp(p, "io,", 3) == 0) { 199573abaf87SPeter Hurley *iotype = UPIO_PORT; 199673abaf87SPeter Hurley p += 3; 199773abaf87SPeter Hurley } else if (strncmp(p, "0x", 2) == 0) { 199873abaf87SPeter Hurley *iotype = UPIO_MEM; 199973abaf87SPeter Hurley } else { 200073abaf87SPeter Hurley return -EINVAL; 200173abaf87SPeter Hurley } 200273abaf87SPeter Hurley 20038b2303deSAlexander Sverdlin /* 20048b2303deSAlexander Sverdlin * Before you replace it with kstrtoull(), think about options separator 20058b2303deSAlexander Sverdlin * (',') it will not tolerate 20068b2303deSAlexander Sverdlin */ 20078b2303deSAlexander Sverdlin *addr = simple_strtoull(p, NULL, 0); 200873abaf87SPeter Hurley p = strchr(p, ','); 200973abaf87SPeter Hurley if (p) 201073abaf87SPeter Hurley p++; 201173abaf87SPeter Hurley 201273abaf87SPeter Hurley *options = p; 201373abaf87SPeter Hurley return 0; 201473abaf87SPeter Hurley } 201573abaf87SPeter Hurley EXPORT_SYMBOL_GPL(uart_parse_earlycon); 201673abaf87SPeter Hurley 201773abaf87SPeter Hurley /** 201802088ca6SGeert Uytterhoeven * uart_parse_options - Parse serial port baud/parity/bits/flow control. 2019ab4382d2SGreg Kroah-Hartman * @options: pointer to option string 2020ab4382d2SGreg Kroah-Hartman * @baud: pointer to an 'int' variable for the baud rate. 2021ab4382d2SGreg Kroah-Hartman * @parity: pointer to an 'int' variable for the parity. 2022ab4382d2SGreg Kroah-Hartman * @bits: pointer to an 'int' variable for the number of data bits. 2023ab4382d2SGreg Kroah-Hartman * @flow: pointer to an 'int' variable for the flow control character. 2024ab4382d2SGreg Kroah-Hartman * 2025ab4382d2SGreg Kroah-Hartman * uart_parse_options decodes a string containing the serial console 2026ab4382d2SGreg Kroah-Hartman * options. The format of the string is <baud><parity><bits><flow>, 2027ab4382d2SGreg Kroah-Hartman * eg: 115200n8r 2028ab4382d2SGreg Kroah-Hartman */ 2029ab4382d2SGreg Kroah-Hartman void 20300f646b63SPaul Cercueil uart_parse_options(const char *options, int *baud, int *parity, 20310f646b63SPaul Cercueil int *bits, int *flow) 2032ab4382d2SGreg Kroah-Hartman { 20330f646b63SPaul Cercueil const char *s = options; 2034ab4382d2SGreg Kroah-Hartman 2035ab4382d2SGreg Kroah-Hartman *baud = simple_strtoul(s, NULL, 10); 2036ab4382d2SGreg Kroah-Hartman while (*s >= '0' && *s <= '9') 2037ab4382d2SGreg Kroah-Hartman s++; 2038ab4382d2SGreg Kroah-Hartman if (*s) 2039ab4382d2SGreg Kroah-Hartman *parity = *s++; 2040ab4382d2SGreg Kroah-Hartman if (*s) 2041ab4382d2SGreg Kroah-Hartman *bits = *s++ - '0'; 2042ab4382d2SGreg Kroah-Hartman if (*s) 2043ab4382d2SGreg Kroah-Hartman *flow = *s; 2044ab4382d2SGreg Kroah-Hartman } 2045ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_parse_options); 2046ab4382d2SGreg Kroah-Hartman 2047ab4382d2SGreg Kroah-Hartman /** 2048ab4382d2SGreg Kroah-Hartman * uart_set_options - setup the serial console parameters 2049ab4382d2SGreg Kroah-Hartman * @port: pointer to the serial ports uart_port structure 2050ab4382d2SGreg Kroah-Hartman * @co: console pointer 2051ab4382d2SGreg Kroah-Hartman * @baud: baud rate 2052ab4382d2SGreg Kroah-Hartman * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even) 2053ab4382d2SGreg Kroah-Hartman * @bits: number of data bits 2054ab4382d2SGreg Kroah-Hartman * @flow: flow control character - 'r' (rts) 2055ab4382d2SGreg Kroah-Hartman */ 2056ab4382d2SGreg Kroah-Hartman int 2057ab4382d2SGreg Kroah-Hartman uart_set_options(struct uart_port *port, struct console *co, 2058ab4382d2SGreg Kroah-Hartman int baud, int parity, int bits, int flow) 2059ab4382d2SGreg Kroah-Hartman { 2060ab4382d2SGreg Kroah-Hartman struct ktermios termios; 2061ab4382d2SGreg Kroah-Hartman static struct ktermios dummy; 2062ab4382d2SGreg Kroah-Hartman 2063ab4382d2SGreg Kroah-Hartman /* 2064ab4382d2SGreg Kroah-Hartman * Ensure that the serial console lock is initialised 2065ab4382d2SGreg Kroah-Hartman * early. 206642b6a1baSRandy Witt * If this port is a console, then the spinlock is already 206742b6a1baSRandy Witt * initialised. 2068ab4382d2SGreg Kroah-Hartman */ 206942b6a1baSRandy Witt if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) { 2070ab4382d2SGreg Kroah-Hartman spin_lock_init(&port->lock); 2071ab4382d2SGreg Kroah-Hartman lockdep_set_class(&port->lock, &port_lock_key); 207242b6a1baSRandy Witt } 2073ab4382d2SGreg Kroah-Hartman 2074ab4382d2SGreg Kroah-Hartman memset(&termios, 0, sizeof(struct ktermios)); 2075ab4382d2SGreg Kroah-Hartman 2076ba47f97aSJeffy Chen termios.c_cflag |= CREAD | HUPCL | CLOCAL; 2077ba47f97aSJeffy Chen tty_termios_encode_baud_rate(&termios, baud, baud); 2078ab4382d2SGreg Kroah-Hartman 2079ab4382d2SGreg Kroah-Hartman if (bits == 7) 2080ab4382d2SGreg Kroah-Hartman termios.c_cflag |= CS7; 2081ab4382d2SGreg Kroah-Hartman else 2082ab4382d2SGreg Kroah-Hartman termios.c_cflag |= CS8; 2083ab4382d2SGreg Kroah-Hartman 2084ab4382d2SGreg Kroah-Hartman switch (parity) { 2085ab4382d2SGreg Kroah-Hartman case 'o': case 'O': 2086ab4382d2SGreg Kroah-Hartman termios.c_cflag |= PARODD; 2087ab4382d2SGreg Kroah-Hartman /*fall through*/ 2088ab4382d2SGreg Kroah-Hartman case 'e': case 'E': 2089ab4382d2SGreg Kroah-Hartman termios.c_cflag |= PARENB; 2090ab4382d2SGreg Kroah-Hartman break; 2091ab4382d2SGreg Kroah-Hartman } 2092ab4382d2SGreg Kroah-Hartman 2093ab4382d2SGreg Kroah-Hartman if (flow == 'r') 2094ab4382d2SGreg Kroah-Hartman termios.c_cflag |= CRTSCTS; 2095ab4382d2SGreg Kroah-Hartman 2096ab4382d2SGreg Kroah-Hartman /* 2097ab4382d2SGreg Kroah-Hartman * some uarts on other side don't support no flow control. 2098ab4382d2SGreg Kroah-Hartman * So we set * DTR in host uart to make them happy 2099ab4382d2SGreg Kroah-Hartman */ 2100ab4382d2SGreg Kroah-Hartman port->mctrl |= TIOCM_DTR; 2101ab4382d2SGreg Kroah-Hartman 2102ab4382d2SGreg Kroah-Hartman port->ops->set_termios(port, &termios, &dummy); 2103ab4382d2SGreg Kroah-Hartman /* 2104ab4382d2SGreg Kroah-Hartman * Allow the setting of the UART parameters with a NULL console 2105ab4382d2SGreg Kroah-Hartman * too: 2106ab4382d2SGreg Kroah-Hartman */ 2107ab4382d2SGreg Kroah-Hartman if (co) 2108ab4382d2SGreg Kroah-Hartman co->cflag = termios.c_cflag; 2109ab4382d2SGreg Kroah-Hartman 2110ab4382d2SGreg Kroah-Hartman return 0; 2111ab4382d2SGreg Kroah-Hartman } 2112ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_set_options); 2113ab4382d2SGreg Kroah-Hartman #endif /* CONFIG_SERIAL_CORE_CONSOLE */ 2114ab4382d2SGreg Kroah-Hartman 2115cf75525fSJiri Slaby /** 2116cf75525fSJiri Slaby * uart_change_pm - set power state of the port 2117cf75525fSJiri Slaby * 2118cf75525fSJiri Slaby * @state: port descriptor 2119cf75525fSJiri Slaby * @pm_state: new state 2120cf75525fSJiri Slaby * 2121cf75525fSJiri Slaby * Locking: port->mutex has to be held 2122cf75525fSJiri Slaby */ 21236f538fe3SLinus Walleij static void uart_change_pm(struct uart_state *state, 21246f538fe3SLinus Walleij enum uart_pm_state pm_state) 2125ab4382d2SGreg Kroah-Hartman { 21264047b371SPeter Hurley struct uart_port *port = uart_port_check(state); 2127ab4382d2SGreg Kroah-Hartman 2128ab4382d2SGreg Kroah-Hartman if (state->pm_state != pm_state) { 21294047b371SPeter Hurley if (port && port->ops->pm) 2130ab4382d2SGreg Kroah-Hartman port->ops->pm(port, pm_state, state->pm_state); 2131ab4382d2SGreg Kroah-Hartman state->pm_state = pm_state; 2132ab4382d2SGreg Kroah-Hartman } 2133ab4382d2SGreg Kroah-Hartman } 2134ab4382d2SGreg Kroah-Hartman 2135ab4382d2SGreg Kroah-Hartman struct uart_match { 2136ab4382d2SGreg Kroah-Hartman struct uart_port *port; 2137ab4382d2SGreg Kroah-Hartman struct uart_driver *driver; 2138ab4382d2SGreg Kroah-Hartman }; 2139ab4382d2SGreg Kroah-Hartman 2140ab4382d2SGreg Kroah-Hartman static int serial_match_port(struct device *dev, void *data) 2141ab4382d2SGreg Kroah-Hartman { 2142ab4382d2SGreg Kroah-Hartman struct uart_match *match = data; 2143ab4382d2SGreg Kroah-Hartman struct tty_driver *tty_drv = match->driver->tty_driver; 2144ab4382d2SGreg Kroah-Hartman dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) + 2145ab4382d2SGreg Kroah-Hartman match->port->line; 2146ab4382d2SGreg Kroah-Hartman 2147ab4382d2SGreg Kroah-Hartman return dev->devt == devt; /* Actually, only one tty per port */ 2148ab4382d2SGreg Kroah-Hartman } 2149ab4382d2SGreg Kroah-Hartman 2150ab4382d2SGreg Kroah-Hartman int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport) 2151ab4382d2SGreg Kroah-Hartman { 2152ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + uport->line; 2153ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 2154ab4382d2SGreg Kroah-Hartman struct device *tty_dev; 2155ab4382d2SGreg Kroah-Hartman struct uart_match match = {uport, drv}; 2156ab4382d2SGreg Kroah-Hartman 2157ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 2158ab4382d2SGreg Kroah-Hartman 2159ab4382d2SGreg Kroah-Hartman tty_dev = device_find_child(uport->dev, &match, serial_match_port); 216088e2582eSLucas Stach if (tty_dev && device_may_wakeup(tty_dev)) { 2161aef3ad10SAndy Shevchenko enable_irq_wake(uport->irq); 2162ab4382d2SGreg Kroah-Hartman put_device(tty_dev); 2163ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2164ab4382d2SGreg Kroah-Hartman return 0; 2165ab4382d2SGreg Kroah-Hartman } 21665a65dcc0SFederico Vaga put_device(tty_dev); 21675a65dcc0SFederico Vaga 2168b164c972SPeter Hurley /* Nothing to do if the console is not suspending */ 2169b164c972SPeter Hurley if (!console_suspend_enabled && uart_console(uport)) 2170b164c972SPeter Hurley goto unlock; 2171b164c972SPeter Hurley 2172ab4382d2SGreg Kroah-Hartman uport->suspended = 1; 2173ab4382d2SGreg Kroah-Hartman 2174d41861caSPeter Hurley if (tty_port_initialized(port)) { 2175ab4382d2SGreg Kroah-Hartman const struct uart_ops *ops = uport->ops; 2176ab4382d2SGreg Kroah-Hartman int tries; 2177ab4382d2SGreg Kroah-Hartman 217880f02d54SPeter Hurley tty_port_set_suspended(port, 1); 2179d41861caSPeter Hurley tty_port_set_initialized(port, 0); 2180ab4382d2SGreg Kroah-Hartman 2181ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 2182ab4382d2SGreg Kroah-Hartman ops->stop_tx(uport); 2183ab4382d2SGreg Kroah-Hartman ops->set_mctrl(uport, 0); 2184ab4382d2SGreg Kroah-Hartman ops->stop_rx(uport); 2185ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 2186ab4382d2SGreg Kroah-Hartman 2187ab4382d2SGreg Kroah-Hartman /* 2188ab4382d2SGreg Kroah-Hartman * Wait for the transmitter to empty. 2189ab4382d2SGreg Kroah-Hartman */ 2190ab4382d2SGreg Kroah-Hartman for (tries = 3; !ops->tx_empty(uport) && tries; tries--) 2191ab4382d2SGreg Kroah-Hartman msleep(10); 2192ab4382d2SGreg Kroah-Hartman if (!tries) 2193cade3580SAndy Shevchenko dev_err(uport->dev, "%s: Unable to drain transmitter\n", 2194cade3580SAndy Shevchenko uport->name); 2195ab4382d2SGreg Kroah-Hartman 2196ab4382d2SGreg Kroah-Hartman ops->shutdown(uport); 2197ab4382d2SGreg Kroah-Hartman } 2198ab4382d2SGreg Kroah-Hartman 2199ab4382d2SGreg Kroah-Hartman /* 2200ab4382d2SGreg Kroah-Hartman * Disable the console device before suspending. 2201ab4382d2SGreg Kroah-Hartman */ 2202b164c972SPeter Hurley if (uart_console(uport)) 2203ab4382d2SGreg Kroah-Hartman console_stop(uport->cons); 2204ab4382d2SGreg Kroah-Hartman 22056f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_OFF); 2206b164c972SPeter Hurley unlock: 2207ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2208ab4382d2SGreg Kroah-Hartman 2209ab4382d2SGreg Kroah-Hartman return 0; 2210ab4382d2SGreg Kroah-Hartman } 2211ab4382d2SGreg Kroah-Hartman 2212ab4382d2SGreg Kroah-Hartman int uart_resume_port(struct uart_driver *drv, struct uart_port *uport) 2213ab4382d2SGreg Kroah-Hartman { 2214ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + uport->line; 2215ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 2216ab4382d2SGreg Kroah-Hartman struct device *tty_dev; 2217ab4382d2SGreg Kroah-Hartman struct uart_match match = {uport, drv}; 2218ab4382d2SGreg Kroah-Hartman struct ktermios termios; 2219ab4382d2SGreg Kroah-Hartman 2220ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 2221ab4382d2SGreg Kroah-Hartman 2222ab4382d2SGreg Kroah-Hartman tty_dev = device_find_child(uport->dev, &match, serial_match_port); 2223ab4382d2SGreg Kroah-Hartman if (!uport->suspended && device_may_wakeup(tty_dev)) { 2224aef3ad10SAndy Shevchenko if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq)))) 2225ab4382d2SGreg Kroah-Hartman disable_irq_wake(uport->irq); 22265a65dcc0SFederico Vaga put_device(tty_dev); 2227ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2228ab4382d2SGreg Kroah-Hartman return 0; 2229ab4382d2SGreg Kroah-Hartman } 22305a65dcc0SFederico Vaga put_device(tty_dev); 2231ab4382d2SGreg Kroah-Hartman uport->suspended = 0; 2232ab4382d2SGreg Kroah-Hartman 2233ab4382d2SGreg Kroah-Hartman /* 2234ab4382d2SGreg Kroah-Hartman * Re-enable the console device after suspending. 2235ab4382d2SGreg Kroah-Hartman */ 22365933a161SYin Kangkai if (uart_console(uport)) { 2237ab4382d2SGreg Kroah-Hartman /* 2238ab4382d2SGreg Kroah-Hartman * First try to use the console cflag setting. 2239ab4382d2SGreg Kroah-Hartman */ 2240ab4382d2SGreg Kroah-Hartman memset(&termios, 0, sizeof(struct ktermios)); 2241ab4382d2SGreg Kroah-Hartman termios.c_cflag = uport->cons->cflag; 2242ab4382d2SGreg Kroah-Hartman 2243ab4382d2SGreg Kroah-Hartman /* 2244ab4382d2SGreg Kroah-Hartman * If that's unset, use the tty termios setting. 2245ab4382d2SGreg Kroah-Hartman */ 2246adc8d746SAlan Cox if (port->tty && termios.c_cflag == 0) 2247adc8d746SAlan Cox termios = port->tty->termios; 2248ab4382d2SGreg Kroah-Hartman 224994abc56fSNing Jiang if (console_suspend_enabled) 22506f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_ON); 2251ab4382d2SGreg Kroah-Hartman uport->ops->set_termios(uport, &termios, NULL); 22525933a161SYin Kangkai if (console_suspend_enabled) 2253ab4382d2SGreg Kroah-Hartman console_start(uport->cons); 2254ab4382d2SGreg Kroah-Hartman } 2255ab4382d2SGreg Kroah-Hartman 225680f02d54SPeter Hurley if (tty_port_suspended(port)) { 2257ab4382d2SGreg Kroah-Hartman const struct uart_ops *ops = uport->ops; 2258ab4382d2SGreg Kroah-Hartman int ret; 2259ab4382d2SGreg Kroah-Hartman 22606f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_ON); 2261ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 2262ab4382d2SGreg Kroah-Hartman ops->set_mctrl(uport, 0); 2263ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 2264ab4382d2SGreg Kroah-Hartman if (console_suspend_enabled || !uart_console(uport)) { 2265ab4382d2SGreg Kroah-Hartman /* Protected by port mutex for now */ 2266ab4382d2SGreg Kroah-Hartman struct tty_struct *tty = port->tty; 2267ab4382d2SGreg Kroah-Hartman ret = ops->startup(uport); 2268ab4382d2SGreg Kroah-Hartman if (ret == 0) { 2269ab4382d2SGreg Kroah-Hartman if (tty) 2270ab4382d2SGreg Kroah-Hartman uart_change_speed(tty, state, NULL); 2271ab4382d2SGreg Kroah-Hartman spin_lock_irq(&uport->lock); 2272ab4382d2SGreg Kroah-Hartman ops->set_mctrl(uport, uport->mctrl); 2273ab4382d2SGreg Kroah-Hartman ops->start_tx(uport); 2274ab4382d2SGreg Kroah-Hartman spin_unlock_irq(&uport->lock); 2275d41861caSPeter Hurley tty_port_set_initialized(port, 1); 2276ab4382d2SGreg Kroah-Hartman } else { 2277ab4382d2SGreg Kroah-Hartman /* 2278ab4382d2SGreg Kroah-Hartman * Failed to resume - maybe hardware went away? 2279ab4382d2SGreg Kroah-Hartman * Clear the "initialized" flag so we won't try 2280ab4382d2SGreg Kroah-Hartman * to call the low level drivers shutdown method. 2281ab4382d2SGreg Kroah-Hartman */ 2282ab4382d2SGreg Kroah-Hartman uart_shutdown(tty, state); 2283ab4382d2SGreg Kroah-Hartman } 2284ab4382d2SGreg Kroah-Hartman } 2285ab4382d2SGreg Kroah-Hartman 228680f02d54SPeter Hurley tty_port_set_suspended(port, 0); 2287ab4382d2SGreg Kroah-Hartman } 2288ab4382d2SGreg Kroah-Hartman 2289ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2290ab4382d2SGreg Kroah-Hartman 2291ab4382d2SGreg Kroah-Hartman return 0; 2292ab4382d2SGreg Kroah-Hartman } 2293ab4382d2SGreg Kroah-Hartman 2294ab4382d2SGreg Kroah-Hartman static inline void 2295ab4382d2SGreg Kroah-Hartman uart_report_port(struct uart_driver *drv, struct uart_port *port) 2296ab4382d2SGreg Kroah-Hartman { 2297ab4382d2SGreg Kroah-Hartman char address[64]; 2298ab4382d2SGreg Kroah-Hartman 2299ab4382d2SGreg Kroah-Hartman switch (port->iotype) { 2300ab4382d2SGreg Kroah-Hartman case UPIO_PORT: 2301ab4382d2SGreg Kroah-Hartman snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase); 2302ab4382d2SGreg Kroah-Hartman break; 2303ab4382d2SGreg Kroah-Hartman case UPIO_HUB6: 2304ab4382d2SGreg Kroah-Hartman snprintf(address, sizeof(address), 2305ab4382d2SGreg Kroah-Hartman "I/O 0x%lx offset 0x%x", port->iobase, port->hub6); 2306ab4382d2SGreg Kroah-Hartman break; 2307ab4382d2SGreg Kroah-Hartman case UPIO_MEM: 2308bd94c407SMasahiro Yamada case UPIO_MEM16: 2309ab4382d2SGreg Kroah-Hartman case UPIO_MEM32: 23103ffb1a81SKevin Cernekee case UPIO_MEM32BE: 2311ab4382d2SGreg Kroah-Hartman case UPIO_AU: 2312ab4382d2SGreg Kroah-Hartman case UPIO_TSI: 2313ab4382d2SGreg Kroah-Hartman snprintf(address, sizeof(address), 2314ab4382d2SGreg Kroah-Hartman "MMIO 0x%llx", (unsigned long long)port->mapbase); 2315ab4382d2SGreg Kroah-Hartman break; 2316ab4382d2SGreg Kroah-Hartman default: 2317ab4382d2SGreg Kroah-Hartman strlcpy(address, "*unknown*", sizeof(address)); 2318ab4382d2SGreg Kroah-Hartman break; 2319ab4382d2SGreg Kroah-Hartman } 2320ab4382d2SGreg Kroah-Hartman 2321cade3580SAndy Shevchenko pr_info("%s%s%s at %s (irq = %d, base_baud = %d) is a %s\n", 232268ed7e1cSJames Bottomley port->dev ? dev_name(port->dev) : "", 232368ed7e1cSJames Bottomley port->dev ? ": " : "", 2324cade3580SAndy Shevchenko port->name, 23257d12b976SKees Cook address, port->irq, port->uartclk / 16, uart_type(port)); 2326ab4382d2SGreg Kroah-Hartman } 2327ab4382d2SGreg Kroah-Hartman 2328ab4382d2SGreg Kroah-Hartman static void 2329ab4382d2SGreg Kroah-Hartman uart_configure_port(struct uart_driver *drv, struct uart_state *state, 2330ab4382d2SGreg Kroah-Hartman struct uart_port *port) 2331ab4382d2SGreg Kroah-Hartman { 2332ab4382d2SGreg Kroah-Hartman unsigned int flags; 2333ab4382d2SGreg Kroah-Hartman 2334ab4382d2SGreg Kroah-Hartman /* 2335ab4382d2SGreg Kroah-Hartman * If there isn't a port here, don't do anything further. 2336ab4382d2SGreg Kroah-Hartman */ 2337ab4382d2SGreg Kroah-Hartman if (!port->iobase && !port->mapbase && !port->membase) 2338ab4382d2SGreg Kroah-Hartman return; 2339ab4382d2SGreg Kroah-Hartman 2340ab4382d2SGreg Kroah-Hartman /* 2341ab4382d2SGreg Kroah-Hartman * Now do the auto configuration stuff. Note that config_port 2342ab4382d2SGreg Kroah-Hartman * is expected to claim the resources and map the port for us. 2343ab4382d2SGreg Kroah-Hartman */ 2344ab4382d2SGreg Kroah-Hartman flags = 0; 2345ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_AUTO_IRQ) 2346ab4382d2SGreg Kroah-Hartman flags |= UART_CONFIG_IRQ; 2347ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_BOOT_AUTOCONF) { 2348ab4382d2SGreg Kroah-Hartman if (!(port->flags & UPF_FIXED_TYPE)) { 2349ab4382d2SGreg Kroah-Hartman port->type = PORT_UNKNOWN; 2350ab4382d2SGreg Kroah-Hartman flags |= UART_CONFIG_TYPE; 2351ab4382d2SGreg Kroah-Hartman } 2352ab4382d2SGreg Kroah-Hartman port->ops->config_port(port, flags); 2353ab4382d2SGreg Kroah-Hartman } 2354ab4382d2SGreg Kroah-Hartman 2355ab4382d2SGreg Kroah-Hartman if (port->type != PORT_UNKNOWN) { 2356ab4382d2SGreg Kroah-Hartman unsigned long flags; 2357ab4382d2SGreg Kroah-Hartman 2358ab4382d2SGreg Kroah-Hartman uart_report_port(drv, port); 2359ab4382d2SGreg Kroah-Hartman 2360ab4382d2SGreg Kroah-Hartman /* Power up port for set_mctrl() */ 23616f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_ON); 2362ab4382d2SGreg Kroah-Hartman 2363ab4382d2SGreg Kroah-Hartman /* 2364ab4382d2SGreg Kroah-Hartman * Ensure that the modem control lines are de-activated. 2365ab4382d2SGreg Kroah-Hartman * keep the DTR setting that is set in uart_set_options() 2366ab4382d2SGreg Kroah-Hartman * We probably don't need a spinlock around this, but 2367ab4382d2SGreg Kroah-Hartman */ 2368ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 2369ab4382d2SGreg Kroah-Hartman port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR); 2370ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 2371ab4382d2SGreg Kroah-Hartman 2372ab4382d2SGreg Kroah-Hartman /* 2373ab4382d2SGreg Kroah-Hartman * If this driver supports console, and it hasn't been 2374ab4382d2SGreg Kroah-Hartman * successfully registered yet, try to re-register it. 2375ab4382d2SGreg Kroah-Hartman * It may be that the port was not available. 2376ab4382d2SGreg Kroah-Hartman */ 2377ab4382d2SGreg Kroah-Hartman if (port->cons && !(port->cons->flags & CON_ENABLED)) 2378ab4382d2SGreg Kroah-Hartman register_console(port->cons); 2379ab4382d2SGreg Kroah-Hartman 2380ab4382d2SGreg Kroah-Hartman /* 2381ab4382d2SGreg Kroah-Hartman * Power down all ports by default, except the 2382ab4382d2SGreg Kroah-Hartman * console if we have one. 2383ab4382d2SGreg Kroah-Hartman */ 2384ab4382d2SGreg Kroah-Hartman if (!uart_console(port)) 23856f538fe3SLinus Walleij uart_change_pm(state, UART_PM_STATE_OFF); 2386ab4382d2SGreg Kroah-Hartman } 2387ab4382d2SGreg Kroah-Hartman } 2388ab4382d2SGreg Kroah-Hartman 2389ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2390ab4382d2SGreg Kroah-Hartman 2391ab4382d2SGreg Kroah-Hartman static int uart_poll_init(struct tty_driver *driver, int line, char *options) 2392ab4382d2SGreg Kroah-Hartman { 2393ab4382d2SGreg Kroah-Hartman struct uart_driver *drv = driver->driver_state; 2394ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + line; 239549c02304SPeter Hurley struct tty_port *tport; 2396ab4382d2SGreg Kroah-Hartman struct uart_port *port; 2397ab4382d2SGreg Kroah-Hartman int baud = 9600; 2398ab4382d2SGreg Kroah-Hartman int bits = 8; 2399ab4382d2SGreg Kroah-Hartman int parity = 'n'; 2400ab4382d2SGreg Kroah-Hartman int flow = 'n'; 240149c02304SPeter Hurley int ret = 0; 2402ab4382d2SGreg Kroah-Hartman 240349c02304SPeter Hurley tport = &state->port; 240449c02304SPeter Hurley mutex_lock(&tport->mutex); 240549c02304SPeter Hurley 24064047b371SPeter Hurley port = uart_port_check(state); 24074047b371SPeter Hurley if (!port || !(port->ops->poll_get_char && port->ops->poll_put_char)) { 240849c02304SPeter Hurley ret = -1; 240949c02304SPeter Hurley goto out; 241049c02304SPeter Hurley } 2411ab4382d2SGreg Kroah-Hartman 2412c7f3e708SAnton Vorontsov if (port->ops->poll_init) { 2413c7f3e708SAnton Vorontsov /* 2414d41861caSPeter Hurley * We don't set initialized as we only initialized the hw, 2415d41861caSPeter Hurley * e.g. state->xmit is still uninitialized. 2416c7f3e708SAnton Vorontsov */ 2417d41861caSPeter Hurley if (!tty_port_initialized(tport)) 2418c7f3e708SAnton Vorontsov ret = port->ops->poll_init(port); 2419c7f3e708SAnton Vorontsov } 2420c7f3e708SAnton Vorontsov 242149c02304SPeter Hurley if (!ret && options) { 2422ab4382d2SGreg Kroah-Hartman uart_parse_options(options, &baud, &parity, &bits, &flow); 242349c02304SPeter Hurley ret = uart_set_options(port, NULL, baud, parity, bits, flow); 2424ab4382d2SGreg Kroah-Hartman } 242549c02304SPeter Hurley out: 242649c02304SPeter Hurley mutex_unlock(&tport->mutex); 242749c02304SPeter Hurley return ret; 2428ab4382d2SGreg Kroah-Hartman } 2429ab4382d2SGreg Kroah-Hartman 2430ab4382d2SGreg Kroah-Hartman static int uart_poll_get_char(struct tty_driver *driver, int line) 2431ab4382d2SGreg Kroah-Hartman { 2432ab4382d2SGreg Kroah-Hartman struct uart_driver *drv = driver->driver_state; 2433ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + line; 2434ab4382d2SGreg Kroah-Hartman struct uart_port *port; 24359ed19428SPeter Hurley int ret = -1; 2436ab4382d2SGreg Kroah-Hartman 24379ed19428SPeter Hurley port = uart_port_ref(state); 2438ef510beaSAndy Shevchenko if (port) { 24399ed19428SPeter Hurley ret = port->ops->poll_get_char(port); 24409ed19428SPeter Hurley uart_port_deref(port); 24419ed19428SPeter Hurley } 244222077b09SJiri Slaby 24439ed19428SPeter Hurley return ret; 2444ab4382d2SGreg Kroah-Hartman } 2445ab4382d2SGreg Kroah-Hartman 2446ab4382d2SGreg Kroah-Hartman static void uart_poll_put_char(struct tty_driver *driver, int line, char ch) 2447ab4382d2SGreg Kroah-Hartman { 2448ab4382d2SGreg Kroah-Hartman struct uart_driver *drv = driver->driver_state; 2449ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + line; 2450ab4382d2SGreg Kroah-Hartman struct uart_port *port; 2451ab4382d2SGreg Kroah-Hartman 24529ed19428SPeter Hurley port = uart_port_ref(state); 24539ed19428SPeter Hurley if (!port) 24549ed19428SPeter Hurley return; 2455c7d44a02SDoug Anderson 2456c7d44a02SDoug Anderson if (ch == '\n') 2457c7d44a02SDoug Anderson port->ops->poll_put_char(port, '\r'); 2458ab4382d2SGreg Kroah-Hartman port->ops->poll_put_char(port, ch); 24599ed19428SPeter Hurley uart_port_deref(port); 2460ab4382d2SGreg Kroah-Hartman } 2461ab4382d2SGreg Kroah-Hartman #endif 2462ab4382d2SGreg Kroah-Hartman 2463ab4382d2SGreg Kroah-Hartman static const struct tty_operations uart_ops = { 24644cdd17baSJiri Slaby .install = uart_install, 2465ab4382d2SGreg Kroah-Hartman .open = uart_open, 2466ab4382d2SGreg Kroah-Hartman .close = uart_close, 2467ab4382d2SGreg Kroah-Hartman .write = uart_write, 2468ab4382d2SGreg Kroah-Hartman .put_char = uart_put_char, 2469ab4382d2SGreg Kroah-Hartman .flush_chars = uart_flush_chars, 2470ab4382d2SGreg Kroah-Hartman .write_room = uart_write_room, 2471ab4382d2SGreg Kroah-Hartman .chars_in_buffer= uart_chars_in_buffer, 2472ab4382d2SGreg Kroah-Hartman .flush_buffer = uart_flush_buffer, 2473ab4382d2SGreg Kroah-Hartman .ioctl = uart_ioctl, 2474ab4382d2SGreg Kroah-Hartman .throttle = uart_throttle, 2475ab4382d2SGreg Kroah-Hartman .unthrottle = uart_unthrottle, 2476ab4382d2SGreg Kroah-Hartman .send_xchar = uart_send_xchar, 2477ab4382d2SGreg Kroah-Hartman .set_termios = uart_set_termios, 2478ab4382d2SGreg Kroah-Hartman .set_ldisc = uart_set_ldisc, 2479ab4382d2SGreg Kroah-Hartman .stop = uart_stop, 2480ab4382d2SGreg Kroah-Hartman .start = uart_start, 2481ab4382d2SGreg Kroah-Hartman .hangup = uart_hangup, 2482ab4382d2SGreg Kroah-Hartman .break_ctl = uart_break_ctl, 2483ab4382d2SGreg Kroah-Hartman .wait_until_sent= uart_wait_until_sent, 2484ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PROC_FS 24858a8dcabfSChristoph Hellwig .proc_show = uart_proc_show, 2486ab4382d2SGreg Kroah-Hartman #endif 2487ab4382d2SGreg Kroah-Hartman .tiocmget = uart_tiocmget, 2488ab4382d2SGreg Kroah-Hartman .tiocmset = uart_tiocmset, 24895099d234SAl Viro .set_serial = uart_set_info_user, 24905099d234SAl Viro .get_serial = uart_get_info_user, 2491ab4382d2SGreg Kroah-Hartman .get_icount = uart_get_icount, 2492ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2493ab4382d2SGreg Kroah-Hartman .poll_init = uart_poll_init, 2494ab4382d2SGreg Kroah-Hartman .poll_get_char = uart_poll_get_char, 2495ab4382d2SGreg Kroah-Hartman .poll_put_char = uart_poll_put_char, 2496ab4382d2SGreg Kroah-Hartman #endif 2497ab4382d2SGreg Kroah-Hartman }; 2498ab4382d2SGreg Kroah-Hartman 2499ab4382d2SGreg Kroah-Hartman static const struct tty_port_operations uart_port_ops = { 2500ab4382d2SGreg Kroah-Hartman .carrier_raised = uart_carrier_raised, 2501ab4382d2SGreg Kroah-Hartman .dtr_rts = uart_dtr_rts, 2502b3b57646SRob Herring .activate = uart_port_activate, 2503761ed4a9SRob Herring .shutdown = uart_tty_port_shutdown, 2504ab4382d2SGreg Kroah-Hartman }; 2505ab4382d2SGreg Kroah-Hartman 2506ab4382d2SGreg Kroah-Hartman /** 2507ab4382d2SGreg Kroah-Hartman * uart_register_driver - register a driver with the uart core layer 2508ab4382d2SGreg Kroah-Hartman * @drv: low level driver structure 2509ab4382d2SGreg Kroah-Hartman * 2510ab4382d2SGreg Kroah-Hartman * Register a uart driver with the core driver. We in turn register 2511ab4382d2SGreg Kroah-Hartman * with the tty layer, and initialise the core driver per-port state. 2512ab4382d2SGreg Kroah-Hartman * 2513ab4382d2SGreg Kroah-Hartman * We have a proc file in /proc/tty/driver which is named after the 2514ab4382d2SGreg Kroah-Hartman * normal driver. 2515ab4382d2SGreg Kroah-Hartman * 2516ab4382d2SGreg Kroah-Hartman * drv->port should be NULL, and the per-port structures should be 2517ab4382d2SGreg Kroah-Hartman * registered using uart_add_one_port after this call has succeeded. 2518ab4382d2SGreg Kroah-Hartman */ 2519ab4382d2SGreg Kroah-Hartman int uart_register_driver(struct uart_driver *drv) 2520ab4382d2SGreg Kroah-Hartman { 2521ab4382d2SGreg Kroah-Hartman struct tty_driver *normal; 2522050dfc09SSergey Organov int i, retval = -ENOMEM; 2523ab4382d2SGreg Kroah-Hartman 2524ab4382d2SGreg Kroah-Hartman BUG_ON(drv->state); 2525ab4382d2SGreg Kroah-Hartman 2526ab4382d2SGreg Kroah-Hartman /* 2527ab4382d2SGreg Kroah-Hartman * Maybe we should be using a slab cache for this, especially if 2528ab4382d2SGreg Kroah-Hartman * we have a large number of ports to handle. 2529ab4382d2SGreg Kroah-Hartman */ 25306396bb22SKees Cook drv->state = kcalloc(drv->nr, sizeof(struct uart_state), GFP_KERNEL); 2531ab4382d2SGreg Kroah-Hartman if (!drv->state) 2532ab4382d2SGreg Kroah-Hartman goto out; 2533ab4382d2SGreg Kroah-Hartman 2534ab4382d2SGreg Kroah-Hartman normal = alloc_tty_driver(drv->nr); 2535ab4382d2SGreg Kroah-Hartman if (!normal) 2536ab4382d2SGreg Kroah-Hartman goto out_kfree; 2537ab4382d2SGreg Kroah-Hartman 2538ab4382d2SGreg Kroah-Hartman drv->tty_driver = normal; 2539ab4382d2SGreg Kroah-Hartman 2540ab4382d2SGreg Kroah-Hartman normal->driver_name = drv->driver_name; 2541ab4382d2SGreg Kroah-Hartman normal->name = drv->dev_name; 2542ab4382d2SGreg Kroah-Hartman normal->major = drv->major; 2543ab4382d2SGreg Kroah-Hartman normal->minor_start = drv->minor; 2544ab4382d2SGreg Kroah-Hartman normal->type = TTY_DRIVER_TYPE_SERIAL; 2545ab4382d2SGreg Kroah-Hartman normal->subtype = SERIAL_TYPE_NORMAL; 2546ab4382d2SGreg Kroah-Hartman normal->init_termios = tty_std_termios; 2547ab4382d2SGreg Kroah-Hartman normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; 2548ab4382d2SGreg Kroah-Hartman normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600; 2549ab4382d2SGreg Kroah-Hartman normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 2550ab4382d2SGreg Kroah-Hartman normal->driver_state = drv; 2551ab4382d2SGreg Kroah-Hartman tty_set_operations(normal, &uart_ops); 2552ab4382d2SGreg Kroah-Hartman 2553ab4382d2SGreg Kroah-Hartman /* 2554ab4382d2SGreg Kroah-Hartman * Initialise the UART state(s). 2555ab4382d2SGreg Kroah-Hartman */ 2556ab4382d2SGreg Kroah-Hartman for (i = 0; i < drv->nr; i++) { 2557ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + i; 2558ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 2559ab4382d2SGreg Kroah-Hartman 2560ab4382d2SGreg Kroah-Hartman tty_port_init(port); 2561ab4382d2SGreg Kroah-Hartman port->ops = &uart_port_ops; 2562ab4382d2SGreg Kroah-Hartman } 2563ab4382d2SGreg Kroah-Hartman 2564ab4382d2SGreg Kroah-Hartman retval = tty_register_driver(normal); 2565ab4382d2SGreg Kroah-Hartman if (retval >= 0) 2566ab4382d2SGreg Kroah-Hartman return retval; 2567ab4382d2SGreg Kroah-Hartman 2568191c5f10SJiri Slaby for (i = 0; i < drv->nr; i++) 2569191c5f10SJiri Slaby tty_port_destroy(&drv->state[i].port); 2570ab4382d2SGreg Kroah-Hartman put_tty_driver(normal); 2571ab4382d2SGreg Kroah-Hartman out_kfree: 2572ab4382d2SGreg Kroah-Hartman kfree(drv->state); 2573ab4382d2SGreg Kroah-Hartman out: 2574050dfc09SSergey Organov return retval; 2575ab4382d2SGreg Kroah-Hartman } 2576ab4382d2SGreg Kroah-Hartman 2577ab4382d2SGreg Kroah-Hartman /** 2578ab4382d2SGreg Kroah-Hartman * uart_unregister_driver - remove a driver from the uart core layer 2579ab4382d2SGreg Kroah-Hartman * @drv: low level driver structure 2580ab4382d2SGreg Kroah-Hartman * 2581ab4382d2SGreg Kroah-Hartman * Remove all references to a driver from the core driver. The low 2582ab4382d2SGreg Kroah-Hartman * level driver must have removed all its ports via the 2583ab4382d2SGreg Kroah-Hartman * uart_remove_one_port() if it registered them with uart_add_one_port(). 2584ab4382d2SGreg Kroah-Hartman * (ie, drv->port == NULL) 2585ab4382d2SGreg Kroah-Hartman */ 2586ab4382d2SGreg Kroah-Hartman void uart_unregister_driver(struct uart_driver *drv) 2587ab4382d2SGreg Kroah-Hartman { 2588ab4382d2SGreg Kroah-Hartman struct tty_driver *p = drv->tty_driver; 2589191c5f10SJiri Slaby unsigned int i; 2590191c5f10SJiri Slaby 2591ab4382d2SGreg Kroah-Hartman tty_unregister_driver(p); 2592ab4382d2SGreg Kroah-Hartman put_tty_driver(p); 2593191c5f10SJiri Slaby for (i = 0; i < drv->nr; i++) 2594191c5f10SJiri Slaby tty_port_destroy(&drv->state[i].port); 2595ab4382d2SGreg Kroah-Hartman kfree(drv->state); 25961e66cdedSAlan Cox drv->state = NULL; 2597ab4382d2SGreg Kroah-Hartman drv->tty_driver = NULL; 2598ab4382d2SGreg Kroah-Hartman } 2599ab4382d2SGreg Kroah-Hartman 2600ab4382d2SGreg Kroah-Hartman struct tty_driver *uart_console_device(struct console *co, int *index) 2601ab4382d2SGreg Kroah-Hartman { 2602ab4382d2SGreg Kroah-Hartman struct uart_driver *p = co->data; 2603ab4382d2SGreg Kroah-Hartman *index = co->index; 2604ab4382d2SGreg Kroah-Hartman return p->tty_driver; 2605ab4382d2SGreg Kroah-Hartman } 2606488f49acSJohn Stultz EXPORT_SYMBOL_GPL(uart_console_device); 2607ab4382d2SGreg Kroah-Hartman 26086915c0e4STomas Hlavacek static ssize_t uart_get_attr_uartclk(struct device *dev, 26096915c0e4STomas Hlavacek struct device_attribute *attr, char *buf) 26106915c0e4STomas Hlavacek { 2611bebe73e3SAlan Cox struct serial_struct tmp; 26126915c0e4STomas Hlavacek struct tty_port *port = dev_get_drvdata(dev); 2613b1b79916STomas Hlavacek 26149f109694SAlan Cox uart_get_info(port, &tmp); 2615bebe73e3SAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16); 26166915c0e4STomas Hlavacek } 26176915c0e4STomas Hlavacek 2618373bac4cSAlan Cox static ssize_t uart_get_attr_type(struct device *dev, 2619373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2620373bac4cSAlan Cox { 2621373bac4cSAlan Cox struct serial_struct tmp; 2622373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2623373bac4cSAlan Cox 2624373bac4cSAlan Cox uart_get_info(port, &tmp); 2625373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type); 2626373bac4cSAlan Cox } 2627373bac4cSAlan Cox static ssize_t uart_get_attr_line(struct device *dev, 2628373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2629373bac4cSAlan Cox { 2630373bac4cSAlan Cox struct serial_struct tmp; 2631373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2632373bac4cSAlan Cox 2633373bac4cSAlan Cox uart_get_info(port, &tmp); 2634373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line); 2635373bac4cSAlan Cox } 2636373bac4cSAlan Cox 2637373bac4cSAlan Cox static ssize_t uart_get_attr_port(struct device *dev, 2638373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2639373bac4cSAlan Cox { 2640373bac4cSAlan Cox struct serial_struct tmp; 2641373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2642fd985e1dSAndrew Morton unsigned long ioaddr; 2643373bac4cSAlan Cox 2644373bac4cSAlan Cox uart_get_info(port, &tmp); 2645fd985e1dSAndrew Morton ioaddr = tmp.port; 2646fd985e1dSAndrew Morton if (HIGH_BITS_OFFSET) 2647fd985e1dSAndrew Morton ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET; 2648fd985e1dSAndrew Morton return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr); 2649373bac4cSAlan Cox } 2650373bac4cSAlan Cox 2651373bac4cSAlan Cox static ssize_t uart_get_attr_irq(struct device *dev, 2652373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2653373bac4cSAlan Cox { 2654373bac4cSAlan Cox struct serial_struct tmp; 2655373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2656373bac4cSAlan Cox 2657373bac4cSAlan Cox uart_get_info(port, &tmp); 2658373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq); 2659373bac4cSAlan Cox } 2660373bac4cSAlan Cox 2661373bac4cSAlan Cox static ssize_t uart_get_attr_flags(struct device *dev, 2662373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2663373bac4cSAlan Cox { 2664373bac4cSAlan Cox struct serial_struct tmp; 2665373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2666373bac4cSAlan Cox 2667373bac4cSAlan Cox uart_get_info(port, &tmp); 2668373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags); 2669373bac4cSAlan Cox } 2670373bac4cSAlan Cox 2671373bac4cSAlan Cox static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev, 2672373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2673373bac4cSAlan Cox { 2674373bac4cSAlan Cox struct serial_struct tmp; 2675373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2676373bac4cSAlan Cox 2677373bac4cSAlan Cox uart_get_info(port, &tmp); 2678373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size); 2679373bac4cSAlan Cox } 2680373bac4cSAlan Cox 2681373bac4cSAlan Cox 2682373bac4cSAlan Cox static ssize_t uart_get_attr_close_delay(struct device *dev, 2683373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2684373bac4cSAlan Cox { 2685373bac4cSAlan Cox struct serial_struct tmp; 2686373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2687373bac4cSAlan Cox 2688373bac4cSAlan Cox uart_get_info(port, &tmp); 2689373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay); 2690373bac4cSAlan Cox } 2691373bac4cSAlan Cox 2692373bac4cSAlan Cox 2693373bac4cSAlan Cox static ssize_t uart_get_attr_closing_wait(struct device *dev, 2694373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2695373bac4cSAlan Cox { 2696373bac4cSAlan Cox struct serial_struct tmp; 2697373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2698373bac4cSAlan Cox 2699373bac4cSAlan Cox uart_get_info(port, &tmp); 2700373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait); 2701373bac4cSAlan Cox } 2702373bac4cSAlan Cox 2703373bac4cSAlan Cox static ssize_t uart_get_attr_custom_divisor(struct device *dev, 2704373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2705373bac4cSAlan Cox { 2706373bac4cSAlan Cox struct serial_struct tmp; 2707373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2708373bac4cSAlan Cox 2709373bac4cSAlan Cox uart_get_info(port, &tmp); 2710373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor); 2711373bac4cSAlan Cox } 2712373bac4cSAlan Cox 2713373bac4cSAlan Cox static ssize_t uart_get_attr_io_type(struct device *dev, 2714373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2715373bac4cSAlan Cox { 2716373bac4cSAlan Cox struct serial_struct tmp; 2717373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2718373bac4cSAlan Cox 2719373bac4cSAlan Cox uart_get_info(port, &tmp); 2720373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type); 2721373bac4cSAlan Cox } 2722373bac4cSAlan Cox 2723373bac4cSAlan Cox static ssize_t uart_get_attr_iomem_base(struct device *dev, 2724373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2725373bac4cSAlan Cox { 2726373bac4cSAlan Cox struct serial_struct tmp; 2727373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2728373bac4cSAlan Cox 2729373bac4cSAlan Cox uart_get_info(port, &tmp); 2730373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base); 2731373bac4cSAlan Cox } 2732373bac4cSAlan Cox 2733373bac4cSAlan Cox static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev, 2734373bac4cSAlan Cox struct device_attribute *attr, char *buf) 2735373bac4cSAlan Cox { 2736373bac4cSAlan Cox struct serial_struct tmp; 2737373bac4cSAlan Cox struct tty_port *port = dev_get_drvdata(dev); 2738373bac4cSAlan Cox 2739373bac4cSAlan Cox uart_get_info(port, &tmp); 2740373bac4cSAlan Cox return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift); 2741373bac4cSAlan Cox } 2742373bac4cSAlan Cox 2743373bac4cSAlan Cox static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL); 2744373bac4cSAlan Cox static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL); 2745373bac4cSAlan Cox static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL); 2746373bac4cSAlan Cox static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL); 2747373bac4cSAlan Cox static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL); 2748373bac4cSAlan Cox static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL); 27496915c0e4STomas Hlavacek static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL); 2750373bac4cSAlan Cox static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL); 2751373bac4cSAlan Cox static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL); 2752373bac4cSAlan Cox static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL); 2753373bac4cSAlan Cox static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL); 2754373bac4cSAlan Cox static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL); 2755373bac4cSAlan Cox static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL); 27566915c0e4STomas Hlavacek 27576915c0e4STomas Hlavacek static struct attribute *tty_dev_attrs[] = { 2758373bac4cSAlan Cox &dev_attr_type.attr, 2759373bac4cSAlan Cox &dev_attr_line.attr, 2760373bac4cSAlan Cox &dev_attr_port.attr, 2761373bac4cSAlan Cox &dev_attr_irq.attr, 2762373bac4cSAlan Cox &dev_attr_flags.attr, 2763373bac4cSAlan Cox &dev_attr_xmit_fifo_size.attr, 27646915c0e4STomas Hlavacek &dev_attr_uartclk.attr, 2765373bac4cSAlan Cox &dev_attr_close_delay.attr, 2766373bac4cSAlan Cox &dev_attr_closing_wait.attr, 2767373bac4cSAlan Cox &dev_attr_custom_divisor.attr, 2768373bac4cSAlan Cox &dev_attr_io_type.attr, 2769373bac4cSAlan Cox &dev_attr_iomem_base.attr, 2770373bac4cSAlan Cox &dev_attr_iomem_reg_shift.attr, 27716915c0e4STomas Hlavacek NULL, 27726915c0e4STomas Hlavacek }; 27736915c0e4STomas Hlavacek 2774b1b79916STomas Hlavacek static const struct attribute_group tty_dev_attr_group = { 27756915c0e4STomas Hlavacek .attrs = tty_dev_attrs, 27766915c0e4STomas Hlavacek }; 27776915c0e4STomas Hlavacek 2778ab4382d2SGreg Kroah-Hartman /** 2779ab4382d2SGreg Kroah-Hartman * uart_add_one_port - attach a driver-defined port structure 2780ab4382d2SGreg Kroah-Hartman * @drv: pointer to the uart low level driver structure for this port 2781ab4382d2SGreg Kroah-Hartman * @uport: uart port structure to use for this port. 2782ab4382d2SGreg Kroah-Hartman * 2783ab4382d2SGreg Kroah-Hartman * This allows the driver to register its own uart_port structure 2784ab4382d2SGreg Kroah-Hartman * with the core driver. The main purpose is to allow the low 2785ab4382d2SGreg Kroah-Hartman * level uart drivers to expand uart_port, rather than having yet 2786ab4382d2SGreg Kroah-Hartman * more levels of structures. 2787ab4382d2SGreg Kroah-Hartman */ 2788ab4382d2SGreg Kroah-Hartman int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport) 2789ab4382d2SGreg Kroah-Hartman { 2790ab4382d2SGreg Kroah-Hartman struct uart_state *state; 2791ab4382d2SGreg Kroah-Hartman struct tty_port *port; 2792ab4382d2SGreg Kroah-Hartman int ret = 0; 2793ab4382d2SGreg Kroah-Hartman struct device *tty_dev; 2794266dcff0SGreg Kroah-Hartman int num_groups; 2795ab4382d2SGreg Kroah-Hartman 2796ab4382d2SGreg Kroah-Hartman BUG_ON(in_interrupt()); 2797ab4382d2SGreg Kroah-Hartman 2798ab4382d2SGreg Kroah-Hartman if (uport->line >= drv->nr) 2799ab4382d2SGreg Kroah-Hartman return -EINVAL; 2800ab4382d2SGreg Kroah-Hartman 2801ab4382d2SGreg Kroah-Hartman state = drv->state + uport->line; 2802ab4382d2SGreg Kroah-Hartman port = &state->port; 2803ab4382d2SGreg Kroah-Hartman 2804ab4382d2SGreg Kroah-Hartman mutex_lock(&port_mutex); 2805ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 2806ab4382d2SGreg Kroah-Hartman if (state->uart_port) { 2807ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2808ab4382d2SGreg Kroah-Hartman goto out; 2809ab4382d2SGreg Kroah-Hartman } 2810ab4382d2SGreg Kroah-Hartman 28112b702b9bSPeter Hurley /* Link the port to the driver state table and vice versa */ 28129ed19428SPeter Hurley atomic_set(&state->refcount, 1); 28139ed19428SPeter Hurley init_waitqueue_head(&state->remove_wait); 2814ab4382d2SGreg Kroah-Hartman state->uart_port = uport; 2815ab4382d2SGreg Kroah-Hartman uport->state = state; 2816ab4382d2SGreg Kroah-Hartman 28172b702b9bSPeter Hurley state->pm_state = UART_PM_STATE_UNDEFINED; 28182b702b9bSPeter Hurley uport->cons = drv->cons; 2819959801feSPeter Hurley uport->minor = drv->tty_driver->minor_start + uport->line; 2820f7048b15SVignesh R uport->name = kasprintf(GFP_KERNEL, "%s%d", drv->dev_name, 2821f7048b15SVignesh R drv->tty_driver->name_base + uport->line); 2822f7048b15SVignesh R if (!uport->name) { 2823f7048b15SVignesh R ret = -ENOMEM; 2824f7048b15SVignesh R goto out; 2825f7048b15SVignesh R } 28262b702b9bSPeter Hurley 2827ab4382d2SGreg Kroah-Hartman /* 2828ab4382d2SGreg Kroah-Hartman * If this port is a console, then the spinlock is already 2829ab4382d2SGreg Kroah-Hartman * initialised. 2830ab4382d2SGreg Kroah-Hartman */ 2831ab4382d2SGreg Kroah-Hartman if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) { 2832ab4382d2SGreg Kroah-Hartman spin_lock_init(&uport->lock); 2833ab4382d2SGreg Kroah-Hartman lockdep_set_class(&uport->lock, &port_lock_key); 2834ab4382d2SGreg Kroah-Hartman } 2835a208ffd2SGrant Likely if (uport->cons && uport->dev) 2836dd8b7a1dSMichal Simek of_console_check(uport->dev->of_node, uport->cons->name, uport->line); 2837ab4382d2SGreg Kroah-Hartman 2838fb2b9001SSudip Mukherjee tty_port_link_device(port, drv->tty_driver, uport->line); 2839ab4382d2SGreg Kroah-Hartman uart_configure_port(drv, state, uport); 2840ab4382d2SGreg Kroah-Hartman 28414dda864dSGeert Uytterhoeven port->console = uart_console(uport); 28424dda864dSGeert Uytterhoeven 2843266dcff0SGreg Kroah-Hartman num_groups = 2; 2844266dcff0SGreg Kroah-Hartman if (uport->attr_group) 2845266dcff0SGreg Kroah-Hartman num_groups++; 2846266dcff0SGreg Kroah-Hartman 2847c2b703b8SYoshihiro YUNOMAE uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups), 2848266dcff0SGreg Kroah-Hartman GFP_KERNEL); 2849266dcff0SGreg Kroah-Hartman if (!uport->tty_groups) { 2850266dcff0SGreg Kroah-Hartman ret = -ENOMEM; 2851266dcff0SGreg Kroah-Hartman goto out; 2852266dcff0SGreg Kroah-Hartman } 2853266dcff0SGreg Kroah-Hartman uport->tty_groups[0] = &tty_dev_attr_group; 2854266dcff0SGreg Kroah-Hartman if (uport->attr_group) 2855266dcff0SGreg Kroah-Hartman uport->tty_groups[1] = uport->attr_group; 2856266dcff0SGreg Kroah-Hartman 2857ab4382d2SGreg Kroah-Hartman /* 2858ab4382d2SGreg Kroah-Hartman * Register the port whether it's detected or not. This allows 2859015355b7SGeert Uytterhoeven * setserial to be used to alter this port's parameters. 2860ab4382d2SGreg Kroah-Hartman */ 2861da4c2799SJohan Hovold tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver, 2862266dcff0SGreg Kroah-Hartman uport->line, uport->dev, port, uport->tty_groups); 2863b289c496SChengguang Xu if (!IS_ERR(tty_dev)) { 286477359835SSimon Glass device_set_wakeup_capable(tty_dev, 1); 286577359835SSimon Glass } else { 28662f2dafe7SSudip Mukherjee dev_err(uport->dev, "Cannot register tty device on line %d\n", 2867ab4382d2SGreg Kroah-Hartman uport->line); 286877359835SSimon Glass } 2869ab4382d2SGreg Kroah-Hartman 2870ab4382d2SGreg Kroah-Hartman /* 2871ab4382d2SGreg Kroah-Hartman * Ensure UPF_DEAD is not set. 2872ab4382d2SGreg Kroah-Hartman */ 2873ab4382d2SGreg Kroah-Hartman uport->flags &= ~UPF_DEAD; 2874ab4382d2SGreg Kroah-Hartman 2875ab4382d2SGreg Kroah-Hartman out: 2876ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2877ab4382d2SGreg Kroah-Hartman mutex_unlock(&port_mutex); 2878ab4382d2SGreg Kroah-Hartman 2879ab4382d2SGreg Kroah-Hartman return ret; 2880ab4382d2SGreg Kroah-Hartman } 2881ab4382d2SGreg Kroah-Hartman 2882ab4382d2SGreg Kroah-Hartman /** 2883ab4382d2SGreg Kroah-Hartman * uart_remove_one_port - detach a driver defined port structure 2884ab4382d2SGreg Kroah-Hartman * @drv: pointer to the uart low level driver structure for this port 2885ab4382d2SGreg Kroah-Hartman * @uport: uart port structure for this port 2886ab4382d2SGreg Kroah-Hartman * 2887ab4382d2SGreg Kroah-Hartman * This unhooks (and hangs up) the specified port structure from the 2888ab4382d2SGreg Kroah-Hartman * core driver. No further calls will be made to the low-level code 2889ab4382d2SGreg Kroah-Hartman * for this port. 2890ab4382d2SGreg Kroah-Hartman */ 2891ab4382d2SGreg Kroah-Hartman int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport) 2892ab4382d2SGreg Kroah-Hartman { 2893ab4382d2SGreg Kroah-Hartman struct uart_state *state = drv->state + uport->line; 2894ab4382d2SGreg Kroah-Hartman struct tty_port *port = &state->port; 28954047b371SPeter Hurley struct uart_port *uart_port; 28964c6d5b4dSGeert Uytterhoeven struct tty_struct *tty; 2897b342dd51SChen Gang int ret = 0; 2898ab4382d2SGreg Kroah-Hartman 2899ab4382d2SGreg Kroah-Hartman BUG_ON(in_interrupt()); 2900ab4382d2SGreg Kroah-Hartman 2901ab4382d2SGreg Kroah-Hartman mutex_lock(&port_mutex); 2902ab4382d2SGreg Kroah-Hartman 2903ab4382d2SGreg Kroah-Hartman /* 2904ab4382d2SGreg Kroah-Hartman * Mark the port "dead" - this prevents any opens from 2905ab4382d2SGreg Kroah-Hartman * succeeding while we shut down the port. 2906ab4382d2SGreg Kroah-Hartman */ 2907ab4382d2SGreg Kroah-Hartman mutex_lock(&port->mutex); 29084047b371SPeter Hurley uart_port = uart_port_check(state); 29094047b371SPeter Hurley if (uart_port != uport) 29104047b371SPeter Hurley dev_alert(uport->dev, "Removing wrong port: %p != %p\n", 29114047b371SPeter Hurley uart_port, uport); 29124047b371SPeter Hurley 29134047b371SPeter Hurley if (!uart_port) { 2914b342dd51SChen Gang mutex_unlock(&port->mutex); 2915b342dd51SChen Gang ret = -EINVAL; 2916b342dd51SChen Gang goto out; 2917b342dd51SChen Gang } 2918ab4382d2SGreg Kroah-Hartman uport->flags |= UPF_DEAD; 2919ab4382d2SGreg Kroah-Hartman mutex_unlock(&port->mutex); 2920ab4382d2SGreg Kroah-Hartman 2921ab4382d2SGreg Kroah-Hartman /* 2922ab4382d2SGreg Kroah-Hartman * Remove the devices from the tty layer 2923ab4382d2SGreg Kroah-Hartman */ 2924da4c2799SJohan Hovold tty_port_unregister_device(port, drv->tty_driver, uport->line); 2925ab4382d2SGreg Kroah-Hartman 29264c6d5b4dSGeert Uytterhoeven tty = tty_port_tty_get(port); 29274c6d5b4dSGeert Uytterhoeven if (tty) { 2928ab4382d2SGreg Kroah-Hartman tty_vhangup(port->tty); 29294c6d5b4dSGeert Uytterhoeven tty_kref_put(tty); 29304c6d5b4dSGeert Uytterhoeven } 2931ab4382d2SGreg Kroah-Hartman 2932ab4382d2SGreg Kroah-Hartman /* 29335f5c9ae5SGeert Uytterhoeven * If the port is used as a console, unregister it 29345f5c9ae5SGeert Uytterhoeven */ 29355f5c9ae5SGeert Uytterhoeven if (uart_console(uport)) 29365f5c9ae5SGeert Uytterhoeven unregister_console(uport->cons); 29375f5c9ae5SGeert Uytterhoeven 29385f5c9ae5SGeert Uytterhoeven /* 2939ab4382d2SGreg Kroah-Hartman * Free the port IO and memory resources, if any. 2940ab4382d2SGreg Kroah-Hartman */ 2941a7cfaf16SFabio Estevam if (uport->type != PORT_UNKNOWN && uport->ops->release_port) 2942ab4382d2SGreg Kroah-Hartman uport->ops->release_port(uport); 2943266dcff0SGreg Kroah-Hartman kfree(uport->tty_groups); 2944f7048b15SVignesh R kfree(uport->name); 2945ab4382d2SGreg Kroah-Hartman 2946ab4382d2SGreg Kroah-Hartman /* 2947ab4382d2SGreg Kroah-Hartman * Indicate that there isn't a port here anymore. 2948ab4382d2SGreg Kroah-Hartman */ 2949ab4382d2SGreg Kroah-Hartman uport->type = PORT_UNKNOWN; 2950ab4382d2SGreg Kroah-Hartman 29514047b371SPeter Hurley mutex_lock(&port->mutex); 29529ed19428SPeter Hurley WARN_ON(atomic_dec_return(&state->refcount) < 0); 29539ed19428SPeter Hurley wait_event(state->remove_wait, !atomic_read(&state->refcount)); 2954ab4382d2SGreg Kroah-Hartman state->uart_port = NULL; 29554047b371SPeter Hurley mutex_unlock(&port->mutex); 2956b342dd51SChen Gang out: 2957ab4382d2SGreg Kroah-Hartman mutex_unlock(&port_mutex); 2958ab4382d2SGreg Kroah-Hartman 2959b342dd51SChen Gang return ret; 2960ab4382d2SGreg Kroah-Hartman } 2961ab4382d2SGreg Kroah-Hartman 2962ab4382d2SGreg Kroah-Hartman /* 2963ab4382d2SGreg Kroah-Hartman * Are the two ports equivalent? 2964ab4382d2SGreg Kroah-Hartman */ 2965ab4382d2SGreg Kroah-Hartman int uart_match_port(struct uart_port *port1, struct uart_port *port2) 2966ab4382d2SGreg Kroah-Hartman { 2967ab4382d2SGreg Kroah-Hartman if (port1->iotype != port2->iotype) 2968ab4382d2SGreg Kroah-Hartman return 0; 2969ab4382d2SGreg Kroah-Hartman 2970ab4382d2SGreg Kroah-Hartman switch (port1->iotype) { 2971ab4382d2SGreg Kroah-Hartman case UPIO_PORT: 2972ab4382d2SGreg Kroah-Hartman return (port1->iobase == port2->iobase); 2973ab4382d2SGreg Kroah-Hartman case UPIO_HUB6: 2974ab4382d2SGreg Kroah-Hartman return (port1->iobase == port2->iobase) && 2975ab4382d2SGreg Kroah-Hartman (port1->hub6 == port2->hub6); 2976ab4382d2SGreg Kroah-Hartman case UPIO_MEM: 2977bd94c407SMasahiro Yamada case UPIO_MEM16: 2978ab4382d2SGreg Kroah-Hartman case UPIO_MEM32: 29793ffb1a81SKevin Cernekee case UPIO_MEM32BE: 2980ab4382d2SGreg Kroah-Hartman case UPIO_AU: 2981ab4382d2SGreg Kroah-Hartman case UPIO_TSI: 2982ab4382d2SGreg Kroah-Hartman return (port1->mapbase == port2->mapbase); 2983ab4382d2SGreg Kroah-Hartman } 2984ab4382d2SGreg Kroah-Hartman return 0; 2985ab4382d2SGreg Kroah-Hartman } 2986ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_match_port); 2987ab4382d2SGreg Kroah-Hartman 2988027d7dacSJiri Slaby /** 2989027d7dacSJiri Slaby * uart_handle_dcd_change - handle a change of carrier detect state 2990027d7dacSJiri Slaby * @uport: uart_port structure for the open port 2991027d7dacSJiri Slaby * @status: new carrier detect status, nonzero if active 29924d90bb14SPeter Hurley * 29934d90bb14SPeter Hurley * Caller must hold uport->lock 2994027d7dacSJiri Slaby */ 2995027d7dacSJiri Slaby void uart_handle_dcd_change(struct uart_port *uport, unsigned int status) 2996027d7dacSJiri Slaby { 299742381572SGeorge Spelvin struct tty_port *port = &uport->state->port; 299843eca0aeSAlan Cox struct tty_struct *tty = port->tty; 2999c993257bSPeter Hurley struct tty_ldisc *ld; 3000027d7dacSJiri Slaby 30014d90bb14SPeter Hurley lockdep_assert_held_once(&uport->lock); 30024d90bb14SPeter Hurley 3003c993257bSPeter Hurley if (tty) { 3004c993257bSPeter Hurley ld = tty_ldisc_ref(tty); 300542381572SGeorge Spelvin if (ld) { 300642381572SGeorge Spelvin if (ld->ops->dcd_change) 3007593fb1aeSGeorge Spelvin ld->ops->dcd_change(tty, status); 300842381572SGeorge Spelvin tty_ldisc_deref(ld); 300942381572SGeorge Spelvin } 3010c993257bSPeter Hurley } 3011027d7dacSJiri Slaby 3012027d7dacSJiri Slaby uport->icount.dcd++; 3013027d7dacSJiri Slaby 3014299245a1SPeter Hurley if (uart_dcd_enabled(uport)) { 3015027d7dacSJiri Slaby if (status) 3016027d7dacSJiri Slaby wake_up_interruptible(&port->open_wait); 301743eca0aeSAlan Cox else if (tty) 301843eca0aeSAlan Cox tty_hangup(tty); 3019027d7dacSJiri Slaby } 3020027d7dacSJiri Slaby } 3021027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_handle_dcd_change); 3022027d7dacSJiri Slaby 3023027d7dacSJiri Slaby /** 3024027d7dacSJiri Slaby * uart_handle_cts_change - handle a change of clear-to-send state 3025027d7dacSJiri Slaby * @uport: uart_port structure for the open port 3026027d7dacSJiri Slaby * @status: new clear to send status, nonzero if active 30274d90bb14SPeter Hurley * 30284d90bb14SPeter Hurley * Caller must hold uport->lock 3029027d7dacSJiri Slaby */ 3030027d7dacSJiri Slaby void uart_handle_cts_change(struct uart_port *uport, unsigned int status) 3031027d7dacSJiri Slaby { 30324d90bb14SPeter Hurley lockdep_assert_held_once(&uport->lock); 30334d90bb14SPeter Hurley 3034027d7dacSJiri Slaby uport->icount.cts++; 3035027d7dacSJiri Slaby 3036391f93f2SPeter Hurley if (uart_softcts_mode(uport)) { 3037d01f4d18SPeter Hurley if (uport->hw_stopped) { 3038027d7dacSJiri Slaby if (status) { 3039d01f4d18SPeter Hurley uport->hw_stopped = 0; 3040027d7dacSJiri Slaby uport->ops->start_tx(uport); 3041027d7dacSJiri Slaby uart_write_wakeup(uport); 3042027d7dacSJiri Slaby } 3043027d7dacSJiri Slaby } else { 3044027d7dacSJiri Slaby if (!status) { 3045d01f4d18SPeter Hurley uport->hw_stopped = 1; 3046027d7dacSJiri Slaby uport->ops->stop_tx(uport); 3047027d7dacSJiri Slaby } 3048027d7dacSJiri Slaby } 3049391f93f2SPeter Hurley 3050027d7dacSJiri Slaby } 3051027d7dacSJiri Slaby } 3052027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_handle_cts_change); 3053027d7dacSJiri Slaby 3054cf75525fSJiri Slaby /** 3055cf75525fSJiri Slaby * uart_insert_char - push a char to the uart layer 3056cf75525fSJiri Slaby * 3057cf75525fSJiri Slaby * User is responsible to call tty_flip_buffer_push when they are done with 3058cf75525fSJiri Slaby * insertion. 3059cf75525fSJiri Slaby * 3060cf75525fSJiri Slaby * @port: corresponding port 3061cf75525fSJiri Slaby * @status: state of the serial port RX buffer (LSR for 8250) 3062cf75525fSJiri Slaby * @overrun: mask of overrun bits in @status 3063cf75525fSJiri Slaby * @ch: character to push 3064cf75525fSJiri Slaby * @flag: flag for the character (see TTY_NORMAL and friends) 3065cf75525fSJiri Slaby */ 3066027d7dacSJiri Slaby void uart_insert_char(struct uart_port *port, unsigned int status, 3067027d7dacSJiri Slaby unsigned int overrun, unsigned int ch, unsigned int flag) 3068027d7dacSJiri Slaby { 306992a19f9cSJiri Slaby struct tty_port *tport = &port->state->port; 3070027d7dacSJiri Slaby 3071027d7dacSJiri Slaby if ((status & port->ignore_status_mask & ~overrun) == 0) 307292a19f9cSJiri Slaby if (tty_insert_flip_char(tport, ch, flag) == 0) 3073dabfb351SCorbin ++port->icount.buf_overrun; 3074027d7dacSJiri Slaby 3075027d7dacSJiri Slaby /* 3076027d7dacSJiri Slaby * Overrun is special. Since it's reported immediately, 3077027d7dacSJiri Slaby * it doesn't affect the current character. 3078027d7dacSJiri Slaby */ 3079027d7dacSJiri Slaby if (status & ~port->ignore_status_mask & overrun) 308092a19f9cSJiri Slaby if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0) 3081dabfb351SCorbin ++port->icount.buf_overrun; 3082027d7dacSJiri Slaby } 3083027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_insert_char); 3084027d7dacSJiri Slaby 30858e20fc39SDmitry Safonov int uart_handle_sysrq_char(struct uart_port *port, unsigned int ch) 30868e20fc39SDmitry Safonov { 30878e20fc39SDmitry Safonov if (!IS_ENABLED(CONFIG_MAGIC_SYSRQ_SERIAL)) 30888e20fc39SDmitry Safonov return 0; 30898e20fc39SDmitry Safonov 30908e20fc39SDmitry Safonov if (!port->has_sysrq || !port->sysrq) 30918e20fc39SDmitry Safonov return 0; 30928e20fc39SDmitry Safonov 30938e20fc39SDmitry Safonov if (ch && time_before(jiffies, port->sysrq)) { 30948e20fc39SDmitry Safonov handle_sysrq(ch); 30958e20fc39SDmitry Safonov port->sysrq = 0; 30968e20fc39SDmitry Safonov return 1; 30978e20fc39SDmitry Safonov } 30988e20fc39SDmitry Safonov port->sysrq = 0; 30998e20fc39SDmitry Safonov 31008e20fc39SDmitry Safonov return 0; 31018e20fc39SDmitry Safonov } 31028e20fc39SDmitry Safonov EXPORT_SYMBOL_GPL(uart_handle_sysrq_char); 31038e20fc39SDmitry Safonov 31048e20fc39SDmitry Safonov int uart_prepare_sysrq_char(struct uart_port *port, unsigned int ch) 31058e20fc39SDmitry Safonov { 31068e20fc39SDmitry Safonov if (!IS_ENABLED(CONFIG_MAGIC_SYSRQ_SERIAL)) 31078e20fc39SDmitry Safonov return 0; 31088e20fc39SDmitry Safonov 31098e20fc39SDmitry Safonov if (!port->has_sysrq || !port->sysrq) 31108e20fc39SDmitry Safonov return 0; 31118e20fc39SDmitry Safonov 31128e20fc39SDmitry Safonov if (ch && time_before(jiffies, port->sysrq)) { 31138e20fc39SDmitry Safonov port->sysrq_ch = ch; 31148e20fc39SDmitry Safonov port->sysrq = 0; 31158e20fc39SDmitry Safonov return 1; 31168e20fc39SDmitry Safonov } 31178e20fc39SDmitry Safonov port->sysrq = 0; 31188e20fc39SDmitry Safonov 31198e20fc39SDmitry Safonov return 0; 31208e20fc39SDmitry Safonov } 31218e20fc39SDmitry Safonov EXPORT_SYMBOL_GPL(uart_prepare_sysrq_char); 31228e20fc39SDmitry Safonov 31238e20fc39SDmitry Safonov void uart_unlock_and_check_sysrq(struct uart_port *port, unsigned long irqflags) 31248e20fc39SDmitry Safonov { 31258e20fc39SDmitry Safonov int sysrq_ch; 31268e20fc39SDmitry Safonov 31278e20fc39SDmitry Safonov if (!port->has_sysrq) { 31288e20fc39SDmitry Safonov spin_unlock_irqrestore(&port->lock, irqflags); 31298e20fc39SDmitry Safonov return; 31308e20fc39SDmitry Safonov } 31318e20fc39SDmitry Safonov 31328e20fc39SDmitry Safonov sysrq_ch = port->sysrq_ch; 31338e20fc39SDmitry Safonov port->sysrq_ch = 0; 31348e20fc39SDmitry Safonov 31358e20fc39SDmitry Safonov spin_unlock_irqrestore(&port->lock, irqflags); 31368e20fc39SDmitry Safonov 31378e20fc39SDmitry Safonov if (sysrq_ch) 31388e20fc39SDmitry Safonov handle_sysrq(sysrq_ch); 31398e20fc39SDmitry Safonov } 31408e20fc39SDmitry Safonov EXPORT_SYMBOL_GPL(uart_unlock_and_check_sysrq); 31418e20fc39SDmitry Safonov 31428e20fc39SDmitry Safonov /* 31438e20fc39SDmitry Safonov * We do the SysRQ and SAK checking like this... 31448e20fc39SDmitry Safonov */ 31458e20fc39SDmitry Safonov int uart_handle_break(struct uart_port *port) 31468e20fc39SDmitry Safonov { 31478e20fc39SDmitry Safonov struct uart_state *state = port->state; 31488e20fc39SDmitry Safonov 31498e20fc39SDmitry Safonov if (port->handle_break) 31508e20fc39SDmitry Safonov port->handle_break(port); 31518e20fc39SDmitry Safonov 31528e20fc39SDmitry Safonov if (port->has_sysrq) { 31538e20fc39SDmitry Safonov if (port->cons && port->cons->index == port->line) { 31548e20fc39SDmitry Safonov if (!port->sysrq) { 31558e20fc39SDmitry Safonov port->sysrq = jiffies + HZ*5; 31568e20fc39SDmitry Safonov return 1; 31578e20fc39SDmitry Safonov } 31588e20fc39SDmitry Safonov port->sysrq = 0; 31598e20fc39SDmitry Safonov } 31608e20fc39SDmitry Safonov } 31618e20fc39SDmitry Safonov 31628e20fc39SDmitry Safonov if (port->flags & UPF_SAK) 31638e20fc39SDmitry Safonov do_SAK(state->port.tty); 31648e20fc39SDmitry Safonov return 0; 31658e20fc39SDmitry Safonov } 31668e20fc39SDmitry Safonov EXPORT_SYMBOL_GPL(uart_handle_break); 31678e20fc39SDmitry Safonov 3168ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_write_wakeup); 3169ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_register_driver); 3170ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_unregister_driver); 3171ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_suspend_port); 3172ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_resume_port); 3173ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_add_one_port); 3174ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_remove_one_port); 3175ab4382d2SGreg Kroah-Hartman 3176ef838a81SUwe Kleine-König /** 3177743f93f8SLukas Wunner * uart_get_rs485_mode() - retrieve rs485 properties for given uart 3178743f93f8SLukas Wunner * @dev: uart device 3179ef838a81SUwe Kleine-König * @rs485conf: output parameter 3180ef838a81SUwe Kleine-König * 3181ef838a81SUwe Kleine-König * This function implements the device tree binding described in 3182ef838a81SUwe Kleine-König * Documentation/devicetree/bindings/serial/rs485.txt. 3183ef838a81SUwe Kleine-König */ 3184743f93f8SLukas Wunner void uart_get_rs485_mode(struct device *dev, struct serial_rs485 *rs485conf) 3185ef838a81SUwe Kleine-König { 3186ef838a81SUwe Kleine-König u32 rs485_delay[2]; 3187ef838a81SUwe Kleine-König int ret; 3188ef838a81SUwe Kleine-König 3189743f93f8SLukas Wunner ret = device_property_read_u32_array(dev, "rs485-rts-delay", 3190743f93f8SLukas Wunner rs485_delay, 2); 3191ef838a81SUwe Kleine-König if (!ret) { 3192ef838a81SUwe Kleine-König rs485conf->delay_rts_before_send = rs485_delay[0]; 3193ef838a81SUwe Kleine-König rs485conf->delay_rts_after_send = rs485_delay[1]; 3194ef838a81SUwe Kleine-König } else { 3195ef838a81SUwe Kleine-König rs485conf->delay_rts_before_send = 0; 3196ef838a81SUwe Kleine-König rs485conf->delay_rts_after_send = 0; 3197ef838a81SUwe Kleine-König } 3198ef838a81SUwe Kleine-König 3199ef838a81SUwe Kleine-König /* 3200f1e5b618SLukas Wunner * Clear full-duplex and enabled flags, set RTS polarity to active high 3201f1e5b618SLukas Wunner * to get to a defined state with the following properties: 3202ef838a81SUwe Kleine-König */ 3203f1e5b618SLukas Wunner rs485conf->flags &= ~(SER_RS485_RX_DURING_TX | SER_RS485_ENABLED | 3204f1e5b618SLukas Wunner SER_RS485_RTS_AFTER_SEND); 3205f1e5b618SLukas Wunner rs485conf->flags |= SER_RS485_RTS_ON_SEND; 3206ef838a81SUwe Kleine-König 3207743f93f8SLukas Wunner if (device_property_read_bool(dev, "rs485-rx-during-tx")) 3208ef838a81SUwe Kleine-König rs485conf->flags |= SER_RS485_RX_DURING_TX; 3209ef838a81SUwe Kleine-König 3210743f93f8SLukas Wunner if (device_property_read_bool(dev, "linux,rs485-enabled-at-boot-time")) 3211ef838a81SUwe Kleine-König rs485conf->flags |= SER_RS485_ENABLED; 3212f1e5b618SLukas Wunner 3213f1e5b618SLukas Wunner if (device_property_read_bool(dev, "rs485-rts-active-low")) { 3214f1e5b618SLukas Wunner rs485conf->flags &= ~SER_RS485_RTS_ON_SEND; 3215f1e5b618SLukas Wunner rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 3216f1e5b618SLukas Wunner } 3217ef838a81SUwe Kleine-König } 3218743f93f8SLukas Wunner EXPORT_SYMBOL_GPL(uart_get_rs485_mode); 3219ef838a81SUwe Kleine-König 3220ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Serial driver core"); 3221ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL"); 3222