blob: 0464360781fe556b7e2b213912bf3530f2bcef3f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Driver core for serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/tty.h>
25#include <linux/slab.h>
26#include <linux/init.h>
27#include <linux/console.h>
Alexey Dobriyand196a942009-03-31 15:19:21 -070028#include <linux/proc_fs.h>
29#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/device.h>
31#include <linux/serial.h> /* for serial_state and serial_icounter_struct */
Alan Coxccce6de2009-09-19 13:13:30 -070032#include <linux/serial_core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
Arjan van de Venf392ecf2006-01-12 18:44:32 +000034#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <asm/irq.h>
37#include <asm/uaccess.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
40 * This is used to lock changes in serial line configuration.
41 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +000042static DEFINE_MUTEX(port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Ingo Molnar13e83592006-07-03 00:25:03 -070044/*
45 * lockdep: port->lock is initialized in two places, but we
46 * want only one lock-class:
47 */
48static struct lock_class_key port_lock_key;
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#ifdef CONFIG_SERIAL_CORE_CONSOLE
53#define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
54#else
55#define uart_console(port) (0)
56#endif
57
Alan Cox19225132010-06-01 22:52:51 +020058static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
Alan Coxa46c9992008-02-08 04:18:53 -080059 struct ktermios *old_termios);
Jiri Slaby1f33a512011-07-14 14:35:10 +020060static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static void uart_change_pm(struct uart_state *state, int pm_state);
62
63/*
64 * This routine is used by the interrupt handler to schedule processing in
65 * the software interrupt portion of the driver.
66 */
67void uart_write_wakeup(struct uart_port *port)
68{
Alan Coxebd2c8f2009-09-19 13:13:28 -070069 struct uart_state *state = port->state;
Pavel Machekd5f735e2006-03-07 21:55:20 -080070 /*
71 * This means you called this function _after_ the port was
72 * closed. No cookie for you.
73 */
Alan Coxebd2c8f2009-09-19 13:13:28 -070074 BUG_ON(!state);
Jiri Slaby6a3e492b2011-07-14 14:35:12 +020075 tty_wakeup(state->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
78static void uart_stop(struct tty_struct *tty)
79{
80 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -070081 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 unsigned long flags;
83
84 spin_lock_irqsave(&port->lock, flags);
Russell Kingb129a8c2005-08-31 10:12:14 +010085 port->ops->stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 spin_unlock_irqrestore(&port->lock, flags);
87}
88
89static void __uart_start(struct tty_struct *tty)
90{
91 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -070092 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Alan Coxebd2c8f2009-09-19 13:13:28 -070094 if (!uart_circ_empty(&state->xmit) && state->xmit.buf &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 !tty->stopped && !tty->hw_stopped)
Russell Kingb129a8c2005-08-31 10:12:14 +010096 port->ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99static void uart_start(struct tty_struct *tty)
100{
101 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700102 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned long flags;
104
105 spin_lock_irqsave(&port->lock, flags);
106 __uart_start(tty);
107 spin_unlock_irqrestore(&port->lock, flags);
108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static inline void
111uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
112{
113 unsigned long flags;
114 unsigned int old;
115
116 spin_lock_irqsave(&port->lock, flags);
117 old = port->mctrl;
118 port->mctrl = (old & ~clear) | set;
119 if (old != port->mctrl)
120 port->ops->set_mctrl(port, port->mctrl);
121 spin_unlock_irqrestore(&port->lock, flags);
122}
123
Alan Coxa46c9992008-02-08 04:18:53 -0800124#define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
125#define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/*
128 * Startup the port. This will be called once per open. All calls
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100129 * will be serialised by the per-port mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
Alan Cox19225132010-06-01 22:52:51 +0200131static int uart_startup(struct tty_struct *tty, struct uart_state *state, int init_hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Alan Cox46d57a42009-09-19 13:13:29 -0700133 struct uart_port *uport = state->uart_port;
134 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 unsigned long page;
136 int retval = 0;
137
Alan Coxccce6de2009-09-19 13:13:30 -0700138 if (port->flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return 0;
140
141 /*
142 * Set the TTY IO error marker - we will only clear this
143 * once we have successfully opened the port. Also set
144 * up the tty->alt_speed kludge
145 */
Alan Cox19225132010-06-01 22:52:51 +0200146 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Alan Cox46d57a42009-09-19 13:13:29 -0700148 if (uport->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return 0;
150
151 /*
152 * Initialise and allocate the transmit and temporary
153 * buffer.
154 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700155 if (!state->xmit.buf) {
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100156 /* This is protected by the per port mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 page = get_zeroed_page(GFP_KERNEL);
158 if (!page)
159 return -ENOMEM;
160
Alan Coxebd2c8f2009-09-19 13:13:28 -0700161 state->xmit.buf = (unsigned char *) page;
162 uart_circ_clear(&state->xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164
Alan Cox46d57a42009-09-19 13:13:29 -0700165 retval = uport->ops->startup(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (retval == 0) {
Jiri Slabyc7d7abf2011-03-30 00:10:55 +0200167 if (uart_console(uport) && uport->cons->cflag) {
168 tty->termios->c_cflag = uport->cons->cflag;
169 uport->cons->cflag = 0;
170 }
171 /*
172 * Initialise the hardware port settings.
173 */
174 uart_change_speed(tty, state, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Jiri Slabyc7d7abf2011-03-30 00:10:55 +0200176 if (init_hw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 /*
178 * Setup the RTS and DTR signals once the
179 * port is open and ready to respond.
180 */
Alan Cox19225132010-06-01 22:52:51 +0200181 if (tty->termios->c_cflag & CBAUD)
Alan Cox46d57a42009-09-19 13:13:29 -0700182 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184
Alan Coxccce6de2009-09-19 13:13:30 -0700185 if (port->flags & ASYNC_CTS_FLOW) {
Alan Cox46d57a42009-09-19 13:13:29 -0700186 spin_lock_irq(&uport->lock);
187 if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS))
Alan Cox19225132010-06-01 22:52:51 +0200188 tty->hw_stopped = 1;
Alan Cox46d57a42009-09-19 13:13:29 -0700189 spin_unlock_irq(&uport->lock);
Russell King0dd7a1a2005-06-29 18:40:53 +0100190 }
191
Alan Coxccce6de2009-09-19 13:13:30 -0700192 set_bit(ASYNCB_INITIALIZED, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Alan Cox19225132010-06-01 22:52:51 +0200194 clear_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196
197 if (retval && capable(CAP_SYS_ADMIN))
198 retval = 0;
199
200 return retval;
201}
202
203/*
204 * This routine will shutdown a serial port; interrupts are disabled, and
205 * DTR is dropped if the hangup on close termio flag is on. Calls to
206 * uart_shutdown are serialised by the per-port semaphore.
207 */
Alan Cox19225132010-06-01 22:52:51 +0200208static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Alan Coxccce6de2009-09-19 13:13:30 -0700210 struct uart_port *uport = state->uart_port;
Alan Coxbdc04e32009-09-19 13:13:31 -0700211 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Russell Kingee31b332005-11-13 15:28:51 +0000213 /*
214 * Set the TTY IO error marker
215 */
Alan Coxf7519282009-01-02 13:49:21 +0000216 if (tty)
217 set_bit(TTY_IO_ERROR, &tty->flags);
Russell Kingee31b332005-11-13 15:28:51 +0000218
Alan Coxbdc04e32009-09-19 13:13:31 -0700219 if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
Russell Kingee31b332005-11-13 15:28:51 +0000220 /*
221 * Turn off DTR and RTS early.
222 */
Alan Coxf7519282009-01-02 13:49:21 +0000223 if (!tty || (tty->termios->c_cflag & HUPCL))
Alan Coxccce6de2009-09-19 13:13:30 -0700224 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
Russell Kingee31b332005-11-13 15:28:51 +0000225
226 /*
227 * clear delta_msr_wait queue to avoid mem leaks: we may free
228 * the irq here so the queue might never be woken up. Note
229 * that we won't end up waiting on delta_msr_wait again since
230 * any outstanding file descriptors should be pointing at
231 * hung_up_tty_fops now.
232 */
Alan Coxbdc04e32009-09-19 13:13:31 -0700233 wake_up_interruptible(&port->delta_msr_wait);
Russell Kingee31b332005-11-13 15:28:51 +0000234
235 /*
236 * Free the IRQ and disable the port.
237 */
Alan Coxccce6de2009-09-19 13:13:30 -0700238 uport->ops->shutdown(uport);
Russell Kingee31b332005-11-13 15:28:51 +0000239
240 /*
241 * Ensure that the IRQ handler isn't running on another CPU.
242 */
Alan Coxccce6de2009-09-19 13:13:30 -0700243 synchronize_irq(uport->irq);
Russell Kingee31b332005-11-13 15:28:51 +0000244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * Free the transmit buffer page.
248 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700249 if (state->xmit.buf) {
250 free_page((unsigned long)state->xmit.buf);
251 state->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/**
256 * uart_update_timeout - update per-port FIFO timeout.
257 * @port: uart_port structure describing the port
258 * @cflag: termios cflag value
259 * @baud: speed of the port
260 *
261 * Set the port FIFO timeout value. The @cflag value should
262 * reflect the actual hardware settings.
263 */
264void
265uart_update_timeout(struct uart_port *port, unsigned int cflag,
266 unsigned int baud)
267{
268 unsigned int bits;
269
270 /* byte size and parity */
271 switch (cflag & CSIZE) {
272 case CS5:
273 bits = 7;
274 break;
275 case CS6:
276 bits = 8;
277 break;
278 case CS7:
279 bits = 9;
280 break;
281 default:
282 bits = 10;
Alan Coxa46c9992008-02-08 04:18:53 -0800283 break; /* CS8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
286 if (cflag & CSTOPB)
287 bits++;
288 if (cflag & PARENB)
289 bits++;
290
291 /*
292 * The total number of bits to be transmitted in the fifo.
293 */
294 bits = bits * port->fifosize;
295
296 /*
297 * Figure the timeout to send the above number of bits.
298 * Add .02 seconds of slop
299 */
300 port->timeout = (HZ * bits) / baud + HZ/50;
301}
302
303EXPORT_SYMBOL(uart_update_timeout);
304
305/**
306 * uart_get_baud_rate - return baud rate for a particular port
307 * @port: uart_port structure describing the port in question.
308 * @termios: desired termios settings.
309 * @old: old termios (or NULL)
310 * @min: minimum acceptable baud rate
311 * @max: maximum acceptable baud rate
312 *
313 * Decode the termios structure into a numeric baud rate,
314 * taking account of the magic 38400 baud rate (with spd_*
315 * flags), and mapping the %B0 rate to 9600 baud.
316 *
317 * If the new baud rate is invalid, try the old termios setting.
318 * If it's still invalid, we try 9600 baud.
319 *
320 * Update the @termios structure to reflect the baud rate
Alan Coxeb424fd2008-04-28 02:14:07 -0700321 * we're actually going to be using. Don't do this for the case
322 * where B0 is requested ("hang up").
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
324unsigned int
Alan Cox606d0992006-12-08 02:38:45 -0800325uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
326 struct ktermios *old, unsigned int min, unsigned int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 unsigned int try, baud, altbaud = 38400;
Alan Coxeb424fd2008-04-28 02:14:07 -0700329 int hung_up = 0;
Russell King0077d452006-01-21 23:03:28 +0000330 upf_t flags = port->flags & UPF_SPD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if (flags == UPF_SPD_HI)
333 altbaud = 57600;
André Goddard Rosa82cb7ba2009-10-25 11:18:26 -0200334 else if (flags == UPF_SPD_VHI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 altbaud = 115200;
André Goddard Rosa82cb7ba2009-10-25 11:18:26 -0200336 else if (flags == UPF_SPD_SHI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 altbaud = 230400;
André Goddard Rosa82cb7ba2009-10-25 11:18:26 -0200338 else if (flags == UPF_SPD_WARP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 altbaud = 460800;
340
341 for (try = 0; try < 2; try++) {
342 baud = tty_termios_baud_rate(termios);
343
344 /*
345 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
346 * Die! Die! Die!
347 */
348 if (baud == 38400)
349 baud = altbaud;
350
351 /*
352 * Special case: B0 rate.
353 */
Alan Coxeb424fd2008-04-28 02:14:07 -0700354 if (baud == 0) {
355 hung_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 baud = 9600;
Alan Coxeb424fd2008-04-28 02:14:07 -0700357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 if (baud >= min && baud <= max)
360 return baud;
361
362 /*
363 * Oops, the quotient was zero. Try again with
364 * the old baud rate if possible.
365 */
366 termios->c_cflag &= ~CBAUD;
367 if (old) {
Alan Cox6d4d67b2008-02-04 22:27:53 -0800368 baud = tty_termios_baud_rate(old);
Alan Coxeb424fd2008-04-28 02:14:07 -0700369 if (!hung_up)
370 tty_termios_encode_baud_rate(termios,
371 baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 old = NULL;
373 continue;
374 }
375
376 /*
Alan Cox16ae2a82010-01-04 16:26:21 +0000377 * As a last resort, if the range cannot be met then clip to
378 * the nearest chip supported rate.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 */
Alan Cox16ae2a82010-01-04 16:26:21 +0000380 if (!hung_up) {
381 if (baud <= min)
382 tty_termios_encode_baud_rate(termios,
383 min + 1, min + 1);
384 else
385 tty_termios_encode_baud_rate(termios,
386 max - 1, max - 1);
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Alan Cox16ae2a82010-01-04 16:26:21 +0000389 /* Should never happen */
390 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return 0;
392}
393
394EXPORT_SYMBOL(uart_get_baud_rate);
395
396/**
397 * uart_get_divisor - return uart clock divisor
398 * @port: uart_port structure describing the port.
399 * @baud: desired baud rate
400 *
401 * Calculate the uart clock divisor for the port.
402 */
403unsigned int
404uart_get_divisor(struct uart_port *port, unsigned int baud)
405{
406 unsigned int quot;
407
408 /*
409 * Old custom speed handling.
410 */
411 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
412 quot = port->custom_divisor;
413 else
414 quot = (port->uartclk + (8 * baud)) / (16 * baud);
415
416 return quot;
417}
418
419EXPORT_SYMBOL(uart_get_divisor);
420
Alan Cox23d22ce2008-04-30 00:54:11 -0700421/* FIXME: Consistent locking policy */
Alan Cox19225132010-06-01 22:52:51 +0200422static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
423 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Alan Coxccce6de2009-09-19 13:13:30 -0700425 struct tty_port *port = &state->port;
Alan Coxccce6de2009-09-19 13:13:30 -0700426 struct uart_port *uport = state->uart_port;
Alan Cox606d0992006-12-08 02:38:45 -0800427 struct ktermios *termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /*
430 * If we have no tty, termios, or the port does not exist,
431 * then we can't set the parameters for this port.
432 */
Alan Coxccce6de2009-09-19 13:13:30 -0700433 if (!tty || !tty->termios || uport->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return;
435
436 termios = tty->termios;
437
438 /*
439 * Set flags based on termios cflag
440 */
441 if (termios->c_cflag & CRTSCTS)
Alan Coxccce6de2009-09-19 13:13:30 -0700442 set_bit(ASYNCB_CTS_FLOW, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 else
Alan Coxccce6de2009-09-19 13:13:30 -0700444 clear_bit(ASYNCB_CTS_FLOW, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 if (termios->c_cflag & CLOCAL)
Alan Coxccce6de2009-09-19 13:13:30 -0700447 clear_bit(ASYNCB_CHECK_CD, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 else
Alan Coxccce6de2009-09-19 13:13:30 -0700449 set_bit(ASYNCB_CHECK_CD, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Alan Coxccce6de2009-09-19 13:13:30 -0700451 uport->ops->set_termios(uport, termios, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Alan Cox19225132010-06-01 22:52:51 +0200454static inline int __uart_put_char(struct uart_port *port,
455 struct circ_buf *circ, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 unsigned long flags;
Alan Cox23d22ce2008-04-30 00:54:11 -0700458 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 if (!circ->buf)
Alan Cox23d22ce2008-04-30 00:54:11 -0700461 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 spin_lock_irqsave(&port->lock, flags);
464 if (uart_circ_chars_free(circ) != 0) {
465 circ->buf[circ->head] = c;
466 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
Alan Cox23d22ce2008-04-30 00:54:11 -0700467 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 spin_unlock_irqrestore(&port->lock, flags);
Alan Cox23d22ce2008-04-30 00:54:11 -0700470 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Alan Cox23d22ce2008-04-30 00:54:11 -0700473static int uart_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 struct uart_state *state = tty->driver_data;
476
Alan Coxebd2c8f2009-09-19 13:13:28 -0700477 return __uart_put_char(state->uart_port, &state->xmit, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480static void uart_flush_chars(struct tty_struct *tty)
481{
482 uart_start(tty);
483}
484
Alan Cox19225132010-06-01 22:52:51 +0200485static int uart_write(struct tty_struct *tty,
486 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 struct uart_state *state = tty->driver_data;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800489 struct uart_port *port;
490 struct circ_buf *circ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 unsigned long flags;
492 int c, ret = 0;
493
Pavel Machekd5f735e2006-03-07 21:55:20 -0800494 /*
495 * This means you called this function _after_ the port was
496 * closed. No cookie for you.
497 */
Alan Coxf7519282009-01-02 13:49:21 +0000498 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800499 WARN_ON(1);
500 return -EL3HLT;
501 }
502
Alan Coxebd2c8f2009-09-19 13:13:28 -0700503 port = state->uart_port;
504 circ = &state->xmit;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (!circ->buf)
507 return 0;
508
509 spin_lock_irqsave(&port->lock, flags);
510 while (1) {
511 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
512 if (count < c)
513 c = count;
514 if (c <= 0)
515 break;
516 memcpy(circ->buf + circ->head, buf, c);
517 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
518 buf += c;
519 count -= c;
520 ret += c;
521 }
522 spin_unlock_irqrestore(&port->lock, flags);
523
524 uart_start(tty);
525 return ret;
526}
527
528static int uart_write_room(struct tty_struct *tty)
529{
530 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700531 unsigned long flags;
532 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Alan Coxebd2c8f2009-09-19 13:13:28 -0700534 spin_lock_irqsave(&state->uart_port->lock, flags);
535 ret = uart_circ_chars_free(&state->xmit);
536 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700537 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
540static int uart_chars_in_buffer(struct tty_struct *tty)
541{
542 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700543 unsigned long flags;
544 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Alan Coxebd2c8f2009-09-19 13:13:28 -0700546 spin_lock_irqsave(&state->uart_port->lock, flags);
547 ret = uart_circ_chars_pending(&state->xmit);
548 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700549 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552static void uart_flush_buffer(struct tty_struct *tty)
553{
554 struct uart_state *state = tty->driver_data;
Tetsuo Handa55d7b682008-05-06 20:42:27 -0700555 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 unsigned long flags;
557
Pavel Machekd5f735e2006-03-07 21:55:20 -0800558 /*
559 * This means you called this function _after_ the port was
560 * closed. No cookie for you.
561 */
Alan Coxf7519282009-01-02 13:49:21 +0000562 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800563 WARN_ON(1);
564 return;
565 }
566
Alan Coxebd2c8f2009-09-19 13:13:28 -0700567 port = state->uart_port;
Jiri Slabyeb3a1e12007-05-06 14:48:52 -0700568 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 spin_lock_irqsave(&port->lock, flags);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700571 uart_circ_clear(&state->xmit);
Haavard Skinnemoen6bb0e3a2008-07-16 21:52:36 +0100572 if (port->ops->flush_buffer)
573 port->ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 spin_unlock_irqrestore(&port->lock, flags);
575 tty_wakeup(tty);
576}
577
578/*
579 * This function is used to send a high-priority XON/XOFF character to
580 * the device
581 */
582static void uart_send_xchar(struct tty_struct *tty, char ch)
583{
584 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700585 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 unsigned long flags;
587
588 if (port->ops->send_xchar)
589 port->ops->send_xchar(port, ch);
590 else {
591 port->x_char = ch;
592 if (ch) {
593 spin_lock_irqsave(&port->lock, flags);
Russell Kingb129a8c2005-08-31 10:12:14 +0100594 port->ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 spin_unlock_irqrestore(&port->lock, flags);
596 }
597 }
598}
599
600static void uart_throttle(struct tty_struct *tty)
601{
602 struct uart_state *state = tty->driver_data;
603
604 if (I_IXOFF(tty))
605 uart_send_xchar(tty, STOP_CHAR(tty));
606
607 if (tty->termios->c_cflag & CRTSCTS)
Alan Coxebd2c8f2009-09-19 13:13:28 -0700608 uart_clear_mctrl(state->uart_port, TIOCM_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611static void uart_unthrottle(struct tty_struct *tty)
612{
613 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700614 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 if (I_IXOFF(tty)) {
617 if (port->x_char)
618 port->x_char = 0;
619 else
620 uart_send_xchar(tty, START_CHAR(tty));
621 }
622
623 if (tty->termios->c_cflag & CRTSCTS)
624 uart_set_mctrl(port, TIOCM_RTS);
625}
626
627static int uart_get_info(struct uart_state *state,
628 struct serial_struct __user *retinfo)
629{
Alan Coxa2bceae2009-09-19 13:13:31 -0700630 struct uart_port *uport = state->uart_port;
631 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct serial_struct tmp;
633
634 memset(&tmp, 0, sizeof(tmp));
Alan Coxf34d7a52008-04-30 00:54:13 -0700635
636 /* Ensure the state we copy is consistent and no hardware changes
637 occur as we go */
Alan Coxa2bceae2009-09-19 13:13:31 -0700638 mutex_lock(&port->mutex);
Alan Coxf34d7a52008-04-30 00:54:13 -0700639
Alan Coxa2bceae2009-09-19 13:13:31 -0700640 tmp.type = uport->type;
641 tmp.line = uport->line;
642 tmp.port = uport->iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (HIGH_BITS_OFFSET)
Alan Coxa2bceae2009-09-19 13:13:31 -0700644 tmp.port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
645 tmp.irq = uport->irq;
646 tmp.flags = uport->flags;
647 tmp.xmit_fifo_size = uport->fifosize;
648 tmp.baud_base = uport->uartclk / 16;
649 tmp.close_delay = port->close_delay / 10;
Alan Cox016af532009-09-19 13:13:32 -0700650 tmp.closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 ASYNC_CLOSING_WAIT_NONE :
Alan Coxa2bceae2009-09-19 13:13:31 -0700652 port->closing_wait / 10;
653 tmp.custom_divisor = uport->custom_divisor;
654 tmp.hub6 = uport->hub6;
655 tmp.io_type = uport->iotype;
656 tmp.iomem_reg_shift = uport->regshift;
657 tmp.iomem_base = (void *)(unsigned long)uport->mapbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Alan Coxa2bceae2009-09-19 13:13:31 -0700659 mutex_unlock(&port->mutex);
Alan Coxf34d7a52008-04-30 00:54:13 -0700660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
662 return -EFAULT;
663 return 0;
664}
665
Alan Cox19225132010-06-01 22:52:51 +0200666static int uart_set_info(struct tty_struct *tty, struct uart_state *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 struct serial_struct __user *newinfo)
668{
669 struct serial_struct new_serial;
Alan Cox46d57a42009-09-19 13:13:29 -0700670 struct uart_port *uport = state->uart_port;
671 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 unsigned long new_port;
Russell King0077d452006-01-21 23:03:28 +0000673 unsigned int change_irq, change_port, closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 unsigned int old_custom_divisor, close_delay;
Russell King0077d452006-01-21 23:03:28 +0000675 upf_t old_flags, new_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int retval = 0;
677
678 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
679 return -EFAULT;
680
681 new_port = new_serial.port;
682 if (HIGH_BITS_OFFSET)
683 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
684
685 new_serial.irq = irq_canonicalize(new_serial.irq);
686 close_delay = new_serial.close_delay * 10;
687 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
Alan Cox016af532009-09-19 13:13:32 -0700688 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 /*
Alan Cox91312cd2009-09-19 13:13:29 -0700691 * This semaphore protects port->count. It is also
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 * very useful to prevent opens. Also, take the
693 * port configuration semaphore to make sure that a
694 * module insertion/removal doesn't change anything
695 * under us.
696 */
Alan Coxa2bceae2009-09-19 13:13:31 -0700697 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Alan Cox46d57a42009-09-19 13:13:29 -0700699 change_irq = !(uport->flags & UPF_FIXED_PORT)
700 && new_serial.irq != uport->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 /*
703 * Since changing the 'type' of the port changes its resource
704 * allocations, we should treat type changes the same as
705 * IO port changes.
706 */
Alan Cox46d57a42009-09-19 13:13:29 -0700707 change_port = !(uport->flags & UPF_FIXED_PORT)
708 && (new_port != uport->iobase ||
709 (unsigned long)new_serial.iomem_base != uport->mapbase ||
710 new_serial.hub6 != uport->hub6 ||
711 new_serial.io_type != uport->iotype ||
712 new_serial.iomem_reg_shift != uport->regshift ||
713 new_serial.type != uport->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Alan Cox46d57a42009-09-19 13:13:29 -0700715 old_flags = uport->flags;
Russell King0077d452006-01-21 23:03:28 +0000716 new_flags = new_serial.flags;
Alan Cox46d57a42009-09-19 13:13:29 -0700717 old_custom_divisor = uport->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 if (!capable(CAP_SYS_ADMIN)) {
720 retval = -EPERM;
721 if (change_irq || change_port ||
Alan Cox46d57a42009-09-19 13:13:29 -0700722 (new_serial.baud_base != uport->uartclk / 16) ||
723 (close_delay != port->close_delay) ||
724 (closing_wait != port->closing_wait) ||
Russell King947deee2006-07-02 20:45:51 +0100725 (new_serial.xmit_fifo_size &&
Alan Cox46d57a42009-09-19 13:13:29 -0700726 new_serial.xmit_fifo_size != uport->fifosize) ||
Russell King0077d452006-01-21 23:03:28 +0000727 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 goto exit;
Alan Cox46d57a42009-09-19 13:13:29 -0700729 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000730 (new_flags & UPF_USR_MASK));
Alan Cox46d57a42009-09-19 13:13:29 -0700731 uport->custom_divisor = new_serial.custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 goto check_and_exit;
733 }
734
735 /*
736 * Ask the low level driver to verify the settings.
737 */
Alan Cox46d57a42009-09-19 13:13:29 -0700738 if (uport->ops->verify_port)
739 retval = uport->ops->verify_port(uport, &new_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Yinghai Lua62c4132008-08-19 20:49:55 -0700741 if ((new_serial.irq >= nr_irqs) || (new_serial.irq < 0) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 (new_serial.baud_base < 9600))
743 retval = -EINVAL;
744
745 if (retval)
746 goto exit;
747
748 if (change_port || change_irq) {
749 retval = -EBUSY;
750
751 /*
752 * Make sure that we are the sole user of this port.
753 */
Alan Coxb58d13a2009-09-19 13:13:32 -0700754 if (tty_port_users(port) > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 goto exit;
756
757 /*
758 * We need to shutdown the serial port at the old
759 * port/type/irq combination.
760 */
Alan Cox19225132010-06-01 22:52:51 +0200761 uart_shutdown(tty, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763
764 if (change_port) {
765 unsigned long old_iobase, old_mapbase;
766 unsigned int old_type, old_iotype, old_hub6, old_shift;
767
Alan Cox46d57a42009-09-19 13:13:29 -0700768 old_iobase = uport->iobase;
769 old_mapbase = uport->mapbase;
770 old_type = uport->type;
771 old_hub6 = uport->hub6;
772 old_iotype = uport->iotype;
773 old_shift = uport->regshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 /*
776 * Free and release old regions
777 */
778 if (old_type != PORT_UNKNOWN)
Alan Cox46d57a42009-09-19 13:13:29 -0700779 uport->ops->release_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Alan Cox46d57a42009-09-19 13:13:29 -0700781 uport->iobase = new_port;
782 uport->type = new_serial.type;
783 uport->hub6 = new_serial.hub6;
784 uport->iotype = new_serial.io_type;
785 uport->regshift = new_serial.iomem_reg_shift;
786 uport->mapbase = (unsigned long)new_serial.iomem_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 /*
789 * Claim and map the new regions
790 */
Alan Cox46d57a42009-09-19 13:13:29 -0700791 if (uport->type != PORT_UNKNOWN) {
792 retval = uport->ops->request_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 } else {
794 /* Always success - Jean II */
795 retval = 0;
796 }
797
798 /*
799 * If we fail to request resources for the
800 * new port, try to restore the old settings.
801 */
802 if (retval && old_type != PORT_UNKNOWN) {
Alan Cox46d57a42009-09-19 13:13:29 -0700803 uport->iobase = old_iobase;
804 uport->type = old_type;
805 uport->hub6 = old_hub6;
806 uport->iotype = old_iotype;
807 uport->regshift = old_shift;
808 uport->mapbase = old_mapbase;
809 retval = uport->ops->request_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 /*
811 * If we failed to restore the old settings,
812 * we fail like this.
813 */
814 if (retval)
Alan Cox46d57a42009-09-19 13:13:29 -0700815 uport->type = PORT_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 /*
818 * We failed anyway.
819 */
820 retval = -EBUSY;
Alan Coxa46c9992008-02-08 04:18:53 -0800821 /* Added to return the correct error -Ram Gupta */
822 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824 }
825
David Gibsonabb4a232007-05-06 14:48:49 -0700826 if (change_irq)
Alan Cox46d57a42009-09-19 13:13:29 -0700827 uport->irq = new_serial.irq;
828 if (!(uport->flags & UPF_FIXED_PORT))
829 uport->uartclk = new_serial.baud_base * 16;
830 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000831 (new_flags & UPF_CHANGE_MASK);
Alan Cox46d57a42009-09-19 13:13:29 -0700832 uport->custom_divisor = new_serial.custom_divisor;
833 port->close_delay = close_delay;
834 port->closing_wait = closing_wait;
Russell King947deee2006-07-02 20:45:51 +0100835 if (new_serial.xmit_fifo_size)
Alan Cox46d57a42009-09-19 13:13:29 -0700836 uport->fifosize = new_serial.xmit_fifo_size;
837 if (port->tty)
838 port->tty->low_latency =
839 (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 check_and_exit:
842 retval = 0;
Alan Cox46d57a42009-09-19 13:13:29 -0700843 if (uport->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 goto exit;
Alan Coxccce6de2009-09-19 13:13:30 -0700845 if (port->flags & ASYNC_INITIALIZED) {
Alan Cox46d57a42009-09-19 13:13:29 -0700846 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
847 old_custom_divisor != uport->custom_divisor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /*
849 * If they're setting up a custom divisor or speed,
850 * instead of clearing it, then bitch about it. No
851 * need to rate-limit; it's CAP_SYS_ADMIN only.
852 */
Alan Cox46d57a42009-09-19 13:13:29 -0700853 if (uport->flags & UPF_SPD_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 char buf[64];
855 printk(KERN_NOTICE
856 "%s sets custom speed on %s. This "
857 "is deprecated.\n", current->comm,
Alan Cox46d57a42009-09-19 13:13:29 -0700858 tty_name(port->tty, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
Alan Cox19225132010-06-01 22:52:51 +0200860 uart_change_speed(tty, state, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862 } else
Alan Cox19225132010-06-01 22:52:51 +0200863 retval = uart_startup(tty, state, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 exit:
Alan Coxa2bceae2009-09-19 13:13:31 -0700865 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return retval;
867}
868
Alan Cox19225132010-06-01 22:52:51 +0200869/**
870 * uart_get_lsr_info - get line status register info
871 * @tty: tty associated with the UART
872 * @state: UART being queried
873 * @value: returned modem value
874 *
875 * Note: uart_ioctl protects us against hangups.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 */
Alan Cox19225132010-06-01 22:52:51 +0200877static int uart_get_lsr_info(struct tty_struct *tty,
878 struct uart_state *state, unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Alan Cox46d57a42009-09-19 13:13:29 -0700880 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 unsigned int result;
882
Alan Cox46d57a42009-09-19 13:13:29 -0700883 result = uport->ops->tx_empty(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 /*
886 * If we're about to load something into the transmit
887 * register, we'll pretend the transmitter isn't empty to
888 * avoid a race condition (depending on when the transmit
889 * interrupt happens).
890 */
Alan Cox46d57a42009-09-19 13:13:29 -0700891 if (uport->x_char ||
Alan Coxebd2c8f2009-09-19 13:13:28 -0700892 ((uart_circ_chars_pending(&state->xmit) > 0) &&
Alan Cox19225132010-06-01 22:52:51 +0200893 !tty->stopped && !tty->hw_stopped))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 result &= ~TIOCSER_TEMT;
Alan Coxa46c9992008-02-08 04:18:53 -0800895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return put_user(result, value);
897}
898
Alan Cox60b33c12011-02-14 16:26:14 +0000899static int uart_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 struct uart_state *state = tty->driver_data;
Alan Coxa2bceae2009-09-19 13:13:31 -0700902 struct tty_port *port = &state->port;
Alan Cox46d57a42009-09-19 13:13:29 -0700903 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 int result = -EIO;
905
Alan Coxa2bceae2009-09-19 13:13:31 -0700906 mutex_lock(&port->mutex);
Alan Cox60b33c12011-02-14 16:26:14 +0000907 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
Alan Cox46d57a42009-09-19 13:13:29 -0700908 result = uport->mctrl;
Alan Cox46d57a42009-09-19 13:13:29 -0700909 spin_lock_irq(&uport->lock);
910 result |= uport->ops->get_mctrl(uport);
911 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
Alan Coxa2bceae2009-09-19 13:13:31 -0700913 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 return result;
916}
917
918static int
Alan Cox20b9d172011-02-14 16:26:50 +0000919uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -0700922 struct uart_port *uport = state->uart_port;
Alan Coxa2bceae2009-09-19 13:13:31 -0700923 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 int ret = -EIO;
925
Alan Coxa2bceae2009-09-19 13:13:31 -0700926 mutex_lock(&port->mutex);
Alan Cox20b9d172011-02-14 16:26:50 +0000927 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
Alan Cox46d57a42009-09-19 13:13:29 -0700928 uart_update_mctrl(uport, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 ret = 0;
930 }
Alan Coxa2bceae2009-09-19 13:13:31 -0700931 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return ret;
933}
934
Alan Cox9e989662008-07-22 11:18:03 +0100935static int uart_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 struct uart_state *state = tty->driver_data;
Alan Coxa2bceae2009-09-19 13:13:31 -0700938 struct tty_port *port = &state->port;
Alan Cox46d57a42009-09-19 13:13:29 -0700939 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Alan Coxa2bceae2009-09-19 13:13:31 -0700941 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Alan Cox46d57a42009-09-19 13:13:29 -0700943 if (uport->type != PORT_UNKNOWN)
944 uport->ops->break_ctl(uport, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Alan Coxa2bceae2009-09-19 13:13:31 -0700946 mutex_unlock(&port->mutex);
Alan Cox9e989662008-07-22 11:18:03 +0100947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
Alan Cox19225132010-06-01 22:52:51 +0200950static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Alan Cox46d57a42009-09-19 13:13:29 -0700952 struct uart_port *uport = state->uart_port;
Alan Coxa2bceae2009-09-19 13:13:31 -0700953 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 int flags, ret;
955
956 if (!capable(CAP_SYS_ADMIN))
957 return -EPERM;
958
959 /*
960 * Take the per-port semaphore. This prevents count from
961 * changing, and hence any extra opens of the port while
962 * we're auto-configuring.
963 */
Alan Coxa2bceae2009-09-19 13:13:31 -0700964 if (mutex_lock_interruptible(&port->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return -ERESTARTSYS;
966
967 ret = -EBUSY;
Alan Coxb58d13a2009-09-19 13:13:32 -0700968 if (tty_port_users(port) == 1) {
Alan Cox19225132010-06-01 22:52:51 +0200969 uart_shutdown(tty, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 /*
972 * If we already have a port type configured,
973 * we must release its resources.
974 */
Alan Cox46d57a42009-09-19 13:13:29 -0700975 if (uport->type != PORT_UNKNOWN)
976 uport->ops->release_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 flags = UART_CONFIG_TYPE;
Alan Cox46d57a42009-09-19 13:13:29 -0700979 if (uport->flags & UPF_AUTO_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 flags |= UART_CONFIG_IRQ;
981
982 /*
983 * This will claim the ports resources if
984 * a port is found.
985 */
Alan Cox46d57a42009-09-19 13:13:29 -0700986 uport->ops->config_port(uport, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Alan Cox19225132010-06-01 22:52:51 +0200988 ret = uart_startup(tty, state, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
Alan Coxa2bceae2009-09-19 13:13:31 -0700990 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return ret;
992}
993
994/*
995 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
996 * - mask passed in arg for lines of interest
997 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
998 * Caller should use TIOCGICOUNT to see which one it was
Alan Coxbdc04e32009-09-19 13:13:31 -0700999 *
1000 * FIXME: This wants extracting into a common all driver implementation
1001 * of TIOCMWAIT using tty_port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
1003static int
1004uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1005{
Alan Cox46d57a42009-09-19 13:13:29 -07001006 struct uart_port *uport = state->uart_port;
Alan Coxbdc04e32009-09-19 13:13:31 -07001007 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 DECLARE_WAITQUEUE(wait, current);
1009 struct uart_icount cprev, cnow;
1010 int ret;
1011
1012 /*
1013 * note the counters on entry
1014 */
Alan Cox46d57a42009-09-19 13:13:29 -07001015 spin_lock_irq(&uport->lock);
1016 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 /*
1019 * Force modem status interrupts on
1020 */
Alan Cox46d57a42009-09-19 13:13:29 -07001021 uport->ops->enable_ms(uport);
1022 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Alan Coxbdc04e32009-09-19 13:13:31 -07001024 add_wait_queue(&port->delta_msr_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 for (;;) {
Alan Cox46d57a42009-09-19 13:13:29 -07001026 spin_lock_irq(&uport->lock);
1027 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1028 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 set_current_state(TASK_INTERRUPTIBLE);
1031
1032 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1033 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1034 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1035 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Alan Coxa46c9992008-02-08 04:18:53 -08001036 ret = 0;
1037 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039
1040 schedule();
1041
1042 /* see if a signal did it */
1043 if (signal_pending(current)) {
1044 ret = -ERESTARTSYS;
1045 break;
1046 }
1047
1048 cprev = cnow;
1049 }
1050
1051 current->state = TASK_RUNNING;
Alan Coxbdc04e32009-09-19 13:13:31 -07001052 remove_wait_queue(&port->delta_msr_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 return ret;
1055}
1056
1057/*
1058 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1059 * Return: write counters to the user passed counter struct
1060 * NB: both 1->0 and 0->1 transitions are counted except for
1061 * RI where only 0->1 is counted.
1062 */
Alan Coxd281da72010-09-16 18:21:24 +01001063static int uart_get_icount(struct tty_struct *tty,
1064 struct serial_icounter_struct *icount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Alan Coxd281da72010-09-16 18:21:24 +01001066 struct uart_state *state = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 struct uart_icount cnow;
Alan Cox46d57a42009-09-19 13:13:29 -07001068 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Alan Cox46d57a42009-09-19 13:13:29 -07001070 spin_lock_irq(&uport->lock);
1071 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1072 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Alan Coxd281da72010-09-16 18:21:24 +01001074 icount->cts = cnow.cts;
1075 icount->dsr = cnow.dsr;
1076 icount->rng = cnow.rng;
1077 icount->dcd = cnow.dcd;
1078 icount->rx = cnow.rx;
1079 icount->tx = cnow.tx;
1080 icount->frame = cnow.frame;
1081 icount->overrun = cnow.overrun;
1082 icount->parity = cnow.parity;
1083 icount->brk = cnow.brk;
1084 icount->buf_overrun = cnow.buf_overrun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Alan Coxd281da72010-09-16 18:21:24 +01001086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089/*
Alan Coxe5238442008-04-30 00:53:28 -07001090 * Called via sys_ioctl. We can use spin_lock_irq() here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 */
1092static int
Alan Cox6caa76b2011-02-14 16:27:22 +00001093uart_ioctl(struct tty_struct *tty, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 unsigned long arg)
1095{
1096 struct uart_state *state = tty->driver_data;
Alan Coxa2bceae2009-09-19 13:13:31 -07001097 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 void __user *uarg = (void __user *)arg;
1099 int ret = -ENOIOCTLCMD;
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 /*
1103 * These ioctls don't rely on the hardware to be present.
1104 */
1105 switch (cmd) {
1106 case TIOCGSERIAL:
1107 ret = uart_get_info(state, uarg);
1108 break;
1109
1110 case TIOCSSERIAL:
Alan Cox19225132010-06-01 22:52:51 +02001111 ret = uart_set_info(tty, state, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 break;
1113
1114 case TIOCSERCONFIG:
Alan Cox19225132010-06-01 22:52:51 +02001115 ret = uart_do_autoconfig(tty, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 break;
1117
1118 case TIOCSERGWILD: /* obsolete */
1119 case TIOCSERSWILD: /* obsolete */
1120 ret = 0;
1121 break;
1122 }
1123
1124 if (ret != -ENOIOCTLCMD)
1125 goto out;
1126
1127 if (tty->flags & (1 << TTY_IO_ERROR)) {
1128 ret = -EIO;
1129 goto out;
1130 }
1131
1132 /*
1133 * The following should only be used when hardware is present.
1134 */
1135 switch (cmd) {
1136 case TIOCMIWAIT:
1137 ret = uart_wait_modem_status(state, arg);
1138 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140
1141 if (ret != -ENOIOCTLCMD)
1142 goto out;
1143
Alan Coxa2bceae2009-09-19 13:13:31 -07001144 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Alan Cox6caa76b2011-02-14 16:27:22 +00001146 if (tty->flags & (1 << TTY_IO_ERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 ret = -EIO;
1148 goto out_up;
1149 }
1150
1151 /*
1152 * All these rely on hardware being present and need to be
1153 * protected against the tty being hung up.
1154 */
1155 switch (cmd) {
1156 case TIOCSERGETLSR: /* Get line status register */
Alan Cox19225132010-06-01 22:52:51 +02001157 ret = uart_get_lsr_info(tty, state, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 break;
1159
1160 default: {
Alan Cox46d57a42009-09-19 13:13:29 -07001161 struct uart_port *uport = state->uart_port;
1162 if (uport->ops->ioctl)
1163 ret = uport->ops->ioctl(uport, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 break;
1165 }
1166 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001167out_up:
Alan Coxa2bceae2009-09-19 13:13:31 -07001168 mutex_unlock(&port->mutex);
Alan Coxf34d7a52008-04-30 00:54:13 -07001169out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return ret;
1171}
1172
Linus Torvaldsedeb2802008-06-04 10:35:03 -07001173static void uart_set_ldisc(struct tty_struct *tty)
Alan Cox64e91592008-06-03 15:18:54 +01001174{
1175 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -07001176 struct uart_port *uport = state->uart_port;
Alan Cox64e91592008-06-03 15:18:54 +01001177
Alan Cox46d57a42009-09-19 13:13:29 -07001178 if (uport->ops->set_ldisc)
Alan Coxd87d9b72010-06-01 22:52:53 +02001179 uport->ops->set_ldisc(uport, tty->termios->c_line);
Alan Cox64e91592008-06-03 15:18:54 +01001180}
1181
Alan Coxa46c9992008-02-08 04:18:53 -08001182static void uart_set_termios(struct tty_struct *tty,
1183 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 struct uart_state *state = tty->driver_data;
1186 unsigned long flags;
1187 unsigned int cflag = tty->termios->c_cflag;
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /*
1191 * These are the bits that are used to setup various
David Woodhouse20620d62007-08-22 14:01:11 -07001192 * flags in the low level driver. We can ignore the Bfoo
1193 * bits in c_cflag; c_[io]speed will always be set
1194 * appropriately by set_termios() in tty_ioctl.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 */
1196#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if ((cflag ^ old_termios->c_cflag) == 0 &&
David Woodhouse20620d62007-08-22 14:01:11 -07001198 tty->termios->c_ospeed == old_termios->c_ospeed &&
1199 tty->termios->c_ispeed == old_termios->c_ispeed &&
Alan Coxe5238442008-04-30 00:53:28 -07001200 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return;
Alan Coxe5238442008-04-30 00:53:28 -07001202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Alan Cox19225132010-06-01 22:52:51 +02001204 uart_change_speed(tty, state, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 /* Handle transition to B0 status */
1207 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
Alan Coxebd2c8f2009-09-19 13:13:28 -07001208 uart_clear_mctrl(state->uart_port, TIOCM_RTS | TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /* Handle transition away from B0 status */
André Goddard Rosa82cb7ba2009-10-25 11:18:26 -02001210 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 unsigned int mask = TIOCM_DTR;
1212 if (!(cflag & CRTSCTS) ||
1213 !test_bit(TTY_THROTTLED, &tty->flags))
1214 mask |= TIOCM_RTS;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001215 uart_set_mctrl(state->uart_port, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
1218 /* Handle turning off CRTSCTS */
1219 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
Alan Coxebd2c8f2009-09-19 13:13:28 -07001220 spin_lock_irqsave(&state->uart_port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 tty->hw_stopped = 0;
1222 __uart_start(tty);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001223 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
Russell King0dd7a1a2005-06-29 18:40:53 +01001225 /* Handle turning on CRTSCTS */
André Goddard Rosa82cb7ba2009-10-25 11:18:26 -02001226 else if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
Alan Coxebd2c8f2009-09-19 13:13:28 -07001227 spin_lock_irqsave(&state->uart_port->lock, flags);
1228 if (!(state->uart_port->ops->get_mctrl(state->uart_port) & TIOCM_CTS)) {
Russell King0dd7a1a2005-06-29 18:40:53 +01001229 tty->hw_stopped = 1;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001230 state->uart_port->ops->stop_tx(state->uart_port);
Russell King0dd7a1a2005-06-29 18:40:53 +01001231 }
Alan Coxebd2c8f2009-09-19 13:13:28 -07001232 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Russell King0dd7a1a2005-06-29 18:40:53 +01001233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
1236/*
1237 * In 2.4.5, calls to this will be serialized via the BKL in
1238 * linux/drivers/char/tty_io.c:tty_release()
1239 * linux/drivers/char/tty_io.c:do_tty_handup()
1240 */
1241static void uart_close(struct tty_struct *tty, struct file *filp)
1242{
1243 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -07001244 struct tty_port *port;
1245 struct uart_port *uport;
Alan Cox61cd8a22010-06-01 22:52:57 +02001246 unsigned long flags;
Alan Coxa46c9992008-02-08 04:18:53 -08001247
Linus Torvaldseea7e172009-10-12 19:13:54 +02001248 if (!state)
1249 return;
1250
Alan Cox46d57a42009-09-19 13:13:29 -07001251 uport = state->uart_port;
1252 port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Alan Cox46d57a42009-09-19 13:13:29 -07001254 pr_debug("uart_close(%d) called\n", uport->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Alan Cox61cd8a22010-06-01 22:52:57 +02001256 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Alan Cox61cd8a22010-06-01 22:52:57 +02001258 if (tty_hung_up_p(filp)) {
1259 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 goto done;
Alan Cox61cd8a22010-06-01 22:52:57 +02001261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Alan Cox91312cd2009-09-19 13:13:29 -07001263 if ((tty->count == 1) && (port->count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /*
1265 * Uh, oh. tty->count is 1, which means that the tty
Alan Cox91312cd2009-09-19 13:13:29 -07001266 * structure will be freed. port->count should always
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 * be one in these conditions. If it's greater than
1268 * one, we've got real problems, since it means the
1269 * serial port won't be shutdown.
1270 */
1271 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
Alan Cox91312cd2009-09-19 13:13:29 -07001272 "port->count is %d\n", port->count);
1273 port->count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
Alan Cox91312cd2009-09-19 13:13:29 -07001275 if (--port->count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
Alan Cox91312cd2009-09-19 13:13:29 -07001277 tty->name, port->count);
1278 port->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
Alan Cox61cd8a22010-06-01 22:52:57 +02001280 if (port->count) {
1281 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 goto done;
Alan Cox61cd8a22010-06-01 22:52:57 +02001283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 /*
1286 * Now we wait for the transmit buffer to clear; and we notify
1287 * the line discipline to only process XON/XOFF characters by
1288 * setting tty->closing.
1289 */
Jiri Slaby426929f2011-08-25 15:12:04 +02001290 set_bit(ASYNCB_CLOSING, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 tty->closing = 1;
Alan Cox61cd8a22010-06-01 22:52:57 +02001292 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Jiri Slaby1f33a512011-07-14 14:35:10 +02001294 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1295 tty_wait_until_sent(tty, msecs_to_jiffies(port->closing_wait));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /*
1298 * At this point, we stop accepting input. To do this, we
1299 * disable the receive line status interrupts.
1300 */
Alan Coxccce6de2009-09-19 13:13:30 -07001301 if (port->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 unsigned long flags;
Linus Torvaldseea7e172009-10-12 19:13:54 +02001303 spin_lock_irqsave(&uport->lock, flags);
Alan Cox46d57a42009-09-19 13:13:29 -07001304 uport->ops->stop_rx(uport);
Linus Torvaldseea7e172009-10-12 19:13:54 +02001305 spin_unlock_irqrestore(&uport->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /*
1307 * Before we drop DTR, make sure the UART transmitter
1308 * has completely drained; this is especially
1309 * important if there is a transmit FIFO!
1310 */
Jiri Slaby1f33a512011-07-14 14:35:10 +02001311 uart_wait_until_sent(tty, uport->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313
Jiri Slabybafb0bd2011-08-25 15:12:05 +02001314 mutex_lock(&port->mutex);
Alan Cox19225132010-06-01 22:52:51 +02001315 uart_shutdown(tty, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 uart_flush_buffer(tty);
1317
Alan Coxa46c9992008-02-08 04:18:53 -08001318 tty_ldisc_flush(tty);
1319
Alan Cox7b014782009-09-19 13:13:33 -07001320 tty_port_tty_set(port, NULL);
Alan Cox61cd8a22010-06-01 22:52:57 +02001321 spin_lock_irqsave(&port->lock, flags);
1322 tty->closing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Alan Cox46d57a42009-09-19 13:13:29 -07001324 if (port->blocked_open) {
Alan Cox61cd8a22010-06-01 22:52:57 +02001325 spin_unlock_irqrestore(&port->lock, flags);
Alan Cox46d57a42009-09-19 13:13:29 -07001326 if (port->close_delay)
1327 msleep_interruptible(port->close_delay);
Alan Cox61cd8a22010-06-01 22:52:57 +02001328 spin_lock_irqsave(&port->lock, flags);
Alan Cox46d57a42009-09-19 13:13:29 -07001329 } else if (!uart_console(uport)) {
Alan Cox61cd8a22010-06-01 22:52:57 +02001330 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 uart_change_pm(state, 3);
Alan Cox61cd8a22010-06-01 22:52:57 +02001332 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
1334
1335 /*
1336 * Wake up anyone trying to open this port.
1337 */
Alan Coxccce6de2009-09-19 13:13:30 -07001338 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
Jiri Slaby426929f2011-08-25 15:12:04 +02001339 clear_bit(ASYNCB_CLOSING, &port->flags);
Alan Cox61cd8a22010-06-01 22:52:57 +02001340 spin_unlock_irqrestore(&port->lock, flags);
Alan Cox46d57a42009-09-19 13:13:29 -07001341 wake_up_interruptible(&port->open_wait);
Jiri Slaby426929f2011-08-25 15:12:04 +02001342 wake_up_interruptible(&port->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Alan Cox46d57a42009-09-19 13:13:29 -07001344done:
Alan Coxa2bceae2009-09-19 13:13:31 -07001345 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
Jiri Slaby1f33a512011-07-14 14:35:10 +02001348static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Jiri Slaby1f33a512011-07-14 14:35:10 +02001350 struct uart_state *state = tty->driver_data;
1351 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 unsigned long char_time, expire;
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1355 return;
1356
1357 /*
1358 * Set the check interval to be 1/5 of the estimated time to
1359 * send a single character, and make it at least 1. The check
1360 * interval should also be less than the timeout.
1361 *
1362 * Note: we have to use pretty tight timings here to satisfy
1363 * the NIST-PCTS.
1364 */
1365 char_time = (port->timeout - HZ/50) / port->fifosize;
1366 char_time = char_time / 5;
1367 if (char_time == 0)
1368 char_time = 1;
1369 if (timeout && timeout < char_time)
1370 char_time = timeout;
1371
1372 /*
1373 * If the transmitter hasn't cleared in twice the approximate
1374 * amount of time to send the entire FIFO, it probably won't
1375 * ever clear. This assumes the UART isn't doing flow
1376 * control, which is currently the case. Hence, if it ever
1377 * takes longer than port->timeout, this is probably due to a
1378 * UART bug of some kind. So, we clamp the timeout parameter at
1379 * 2*port->timeout.
1380 */
1381 if (timeout == 0 || timeout > 2 * port->timeout)
1382 timeout = 2 * port->timeout;
1383
1384 expire = jiffies + timeout;
1385
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001386 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
Alan Coxa46c9992008-02-08 04:18:53 -08001387 port->line, jiffies, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 /*
1390 * Check whether the transmitter is empty every 'char_time'.
1391 * 'timeout' / 'expire' give us the maximum amount of time
1392 * we wait.
1393 */
1394 while (!port->ops->tx_empty(port)) {
1395 msleep_interruptible(jiffies_to_msecs(char_time));
1396 if (signal_pending(current))
1397 break;
1398 if (time_after(jiffies, expire))
1399 break;
1400 }
Arnd Bergmann20365212010-06-01 22:53:07 +02001401}
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403/*
1404 * This is called with the BKL held in
1405 * linux/drivers/char/tty_io.c:do_tty_hangup()
1406 * We're called from the eventd thread, so we can sleep for
1407 * a _short_ time only.
1408 */
1409static void uart_hangup(struct tty_struct *tty)
1410{
1411 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -07001412 struct tty_port *port = &state->port;
Alan Cox61cd8a22010-06-01 22:52:57 +02001413 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Alan Coxebd2c8f2009-09-19 13:13:28 -07001415 pr_debug("uart_hangup(%d)\n", state->uart_port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Alan Coxa2bceae2009-09-19 13:13:31 -07001417 mutex_lock(&port->mutex);
Alan Coxccce6de2009-09-19 13:13:30 -07001418 if (port->flags & ASYNC_NORMAL_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 uart_flush_buffer(tty);
Alan Cox19225132010-06-01 22:52:51 +02001420 uart_shutdown(tty, state);
Alan Cox61cd8a22010-06-01 22:52:57 +02001421 spin_lock_irqsave(&port->lock, flags);
Alan Cox91312cd2009-09-19 13:13:29 -07001422 port->count = 0;
Alan Coxccce6de2009-09-19 13:13:30 -07001423 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
Alan Cox61cd8a22010-06-01 22:52:57 +02001424 spin_unlock_irqrestore(&port->lock, flags);
Alan Cox7b014782009-09-19 13:13:33 -07001425 tty_port_tty_set(port, NULL);
Alan Cox46d57a42009-09-19 13:13:29 -07001426 wake_up_interruptible(&port->open_wait);
Alan Coxbdc04e32009-09-19 13:13:31 -07001427 wake_up_interruptible(&port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
Alan Coxa2bceae2009-09-19 13:13:31 -07001429 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Alan Coxde0c8cb2010-06-01 22:52:58 +02001432static int uart_carrier_raised(struct tty_port *port)
1433{
1434 struct uart_state *state = container_of(port, struct uart_state, port);
1435 struct uart_port *uport = state->uart_port;
1436 int mctrl;
Alan Coxde0c8cb2010-06-01 22:52:58 +02001437 spin_lock_irq(&uport->lock);
1438 uport->ops->enable_ms(uport);
1439 mctrl = uport->ops->get_mctrl(uport);
1440 spin_unlock_irq(&uport->lock);
Alan Coxde0c8cb2010-06-01 22:52:58 +02001441 if (mctrl & TIOCM_CAR)
1442 return 1;
1443 return 0;
1444}
1445
1446static void uart_dtr_rts(struct tty_port *port, int onoff)
1447{
1448 struct uart_state *state = container_of(port, struct uart_state, port);
1449 struct uart_port *uport = state->uart_port;
Alan Cox24fcc7c2010-06-01 22:52:59 +02001450
Jiri Slaby6f5c24a2011-03-30 00:10:57 +02001451 if (onoff)
Alan Coxde0c8cb2010-06-01 22:52:58 +02001452 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1453 else
1454 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
Alan Coxde0c8cb2010-06-01 22:52:58 +02001455}
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457static struct uart_state *uart_get(struct uart_driver *drv, int line)
1458{
1459 struct uart_state *state;
Alan Coxa2bceae2009-09-19 13:13:31 -07001460 struct tty_port *port;
Russell King68ac64c2006-04-30 11:13:50 +01001461 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 state = drv->state + line;
Alan Coxa2bceae2009-09-19 13:13:31 -07001464 port = &state->port;
1465 if (mutex_lock_interruptible(&port->mutex)) {
Russell King68ac64c2006-04-30 11:13:50 +01001466 ret = -ERESTARTSYS;
1467 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
1469
Alan Coxa2bceae2009-09-19 13:13:31 -07001470 port->count++;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001471 if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
Russell King68ac64c2006-04-30 11:13:50 +01001472 ret = -ENXIO;
1473 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return state;
Russell King68ac64c2006-04-30 11:13:50 +01001476
1477 err_unlock:
Alan Coxa2bceae2009-09-19 13:13:31 -07001478 port->count--;
1479 mutex_unlock(&port->mutex);
Russell King68ac64c2006-04-30 11:13:50 +01001480 err:
1481 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
1484/*
Denis Cheng922f9cf2008-02-08 04:22:00 -08001485 * calls to uart_open are serialised by the BKL in
1486 * fs/char_dev.c:chrdev_open()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 * Note that if this fails, then uart_close() _will_ be called.
1488 *
1489 * In time, we want to scrap the "opening nonpresent ports"
1490 * behaviour and implement an alternative way for setserial
1491 * to set base addresses/ports/types. This will allow us to
1492 * get rid of a certain amount of extra tests.
1493 */
1494static int uart_open(struct tty_struct *tty, struct file *filp)
1495{
1496 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1497 struct uart_state *state;
Alan Cox91312cd2009-09-19 13:13:29 -07001498 struct tty_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 int retval, line = tty->index;
1500
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001501 pr_debug("uart_open(%d) called\n", line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 * We take the semaphore inside uart_get to guarantee that we won't
Alan Coxebd2c8f2009-09-19 13:13:28 -07001505 * be re-entered while allocating the state structure, or while we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 * request any IRQs that the driver may need. This also has the nice
1507 * side-effect that it delays the action of uart_hangup, so we can
Alan Coxebd2c8f2009-09-19 13:13:28 -07001508 * guarantee that state->port.tty will always contain something
1509 * reasonable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 */
1511 state = uart_get(drv, line);
1512 if (IS_ERR(state)) {
1513 retval = PTR_ERR(state);
1514 goto fail;
1515 }
Alan Cox91312cd2009-09-19 13:13:29 -07001516 port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 /*
1519 * Once we set tty->driver_data here, we are guaranteed that
1520 * uart_close() will decrement the driver module use count.
1521 * Any failures from here onwards should not touch the count.
1522 */
1523 tty->driver_data = state;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001524 state->uart_port->state = state;
1525 tty->low_latency = (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 tty->alt_speed = 0;
Alan Cox7b014782009-09-19 13:13:33 -07001527 tty_port_tty_set(port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 /*
1530 * If the port is in the middle of closing, bail out now.
1531 */
1532 if (tty_hung_up_p(filp)) {
1533 retval = -EAGAIN;
Alan Cox91312cd2009-09-19 13:13:29 -07001534 port->count--;
Alan Coxa2bceae2009-09-19 13:13:31 -07001535 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 goto fail;
1537 }
1538
1539 /*
1540 * Make sure the device is in D0 state.
1541 */
Alan Cox91312cd2009-09-19 13:13:29 -07001542 if (port->count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 uart_change_pm(state, 0);
1544
1545 /*
1546 * Start up the serial port.
1547 */
Alan Cox19225132010-06-01 22:52:51 +02001548 retval = uart_startup(tty, state, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 /*
1551 * If we succeeded, wait until the port is ready.
1552 */
Alan Cox61cd8a22010-06-01 22:52:57 +02001553 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (retval == 0)
Alan Cox74c21072010-06-01 22:53:00 +02001555 retval = tty_port_block_til_ready(port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Alan Coxa2bceae2009-09-19 13:13:31 -07001557fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return retval;
1559}
1560
1561static const char *uart_type(struct uart_port *port)
1562{
1563 const char *str = NULL;
1564
1565 if (port->ops->type)
1566 str = port->ops->type(port);
1567
1568 if (!str)
1569 str = "unknown";
1570
1571 return str;
1572}
1573
1574#ifdef CONFIG_PROC_FS
1575
Alexey Dobriyand196a942009-03-31 15:19:21 -07001576static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
1578 struct uart_state *state = drv->state + i;
Alan Coxa2bceae2009-09-19 13:13:31 -07001579 struct tty_port *port = &state->port;
George G. Davis3689a0e2007-02-14 00:33:06 -08001580 int pm_state;
Alan Coxa2bceae2009-09-19 13:13:31 -07001581 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 char stat_buf[32];
1583 unsigned int status;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001584 int mmio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Alan Coxa2bceae2009-09-19 13:13:31 -07001586 if (!uport)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001587 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Alan Coxa2bceae2009-09-19 13:13:31 -07001589 mmio = uport->iotype >= UPIO_MEM;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001590 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001591 uport->line, uart_type(uport),
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001592 mmio ? "mmio:0x" : "port:",
Alan Coxa2bceae2009-09-19 13:13:31 -07001593 mmio ? (unsigned long long)uport->mapbase
1594 : (unsigned long long)uport->iobase,
1595 uport->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Alan Coxa2bceae2009-09-19 13:13:31 -07001597 if (uport->type == PORT_UNKNOWN) {
Alexey Dobriyand196a942009-03-31 15:19:21 -07001598 seq_putc(m, '\n');
1599 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
1601
Alan Coxa46c9992008-02-08 04:18:53 -08001602 if (capable(CAP_SYS_ADMIN)) {
Alan Coxa2bceae2009-09-19 13:13:31 -07001603 mutex_lock(&port->mutex);
George G. Davis3689a0e2007-02-14 00:33:06 -08001604 pm_state = state->pm_state;
1605 if (pm_state)
1606 uart_change_pm(state, 0);
Alan Coxa2bceae2009-09-19 13:13:31 -07001607 spin_lock_irq(&uport->lock);
1608 status = uport->ops->get_mctrl(uport);
1609 spin_unlock_irq(&uport->lock);
George G. Davis3689a0e2007-02-14 00:33:06 -08001610 if (pm_state)
1611 uart_change_pm(state, pm_state);
Alan Coxa2bceae2009-09-19 13:13:31 -07001612 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Alexey Dobriyand196a942009-03-31 15:19:21 -07001614 seq_printf(m, " tx:%d rx:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001615 uport->icount.tx, uport->icount.rx);
1616 if (uport->icount.frame)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001617 seq_printf(m, " fe:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001618 uport->icount.frame);
1619 if (uport->icount.parity)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001620 seq_printf(m, " pe:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001621 uport->icount.parity);
1622 if (uport->icount.brk)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001623 seq_printf(m, " brk:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001624 uport->icount.brk);
1625 if (uport->icount.overrun)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001626 seq_printf(m, " oe:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001627 uport->icount.overrun);
Alan Coxa46c9992008-02-08 04:18:53 -08001628
1629#define INFOBIT(bit, str) \
Alan Coxa2bceae2009-09-19 13:13:31 -07001630 if (uport->mctrl & (bit)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 strncat(stat_buf, (str), sizeof(stat_buf) - \
1632 strlen(stat_buf) - 2)
Alan Coxa46c9992008-02-08 04:18:53 -08001633#define STATBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (status & (bit)) \
1635 strncat(stat_buf, (str), sizeof(stat_buf) - \
1636 strlen(stat_buf) - 2)
1637
1638 stat_buf[0] = '\0';
1639 stat_buf[1] = '\0';
1640 INFOBIT(TIOCM_RTS, "|RTS");
1641 STATBIT(TIOCM_CTS, "|CTS");
1642 INFOBIT(TIOCM_DTR, "|DTR");
1643 STATBIT(TIOCM_DSR, "|DSR");
1644 STATBIT(TIOCM_CAR, "|CD");
1645 STATBIT(TIOCM_RNG, "|RI");
1646 if (stat_buf[0])
1647 stat_buf[0] = ' ';
Alan Coxa46c9992008-02-08 04:18:53 -08001648
Alexey Dobriyand196a942009-03-31 15:19:21 -07001649 seq_puts(m, stat_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
Alexey Dobriyand196a942009-03-31 15:19:21 -07001651 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652#undef STATBIT
1653#undef INFOBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
Alexey Dobriyand196a942009-03-31 15:19:21 -07001656static int uart_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Alexey Dobriyan833bb302009-04-02 01:30:04 +04001658 struct tty_driver *ttydrv = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 struct uart_driver *drv = ttydrv->driver_state;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001660 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Alexey Dobriyand196a942009-03-31 15:19:21 -07001662 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 "", "", "");
Alexey Dobriyand196a942009-03-31 15:19:21 -07001664 for (i = 0; i < drv->nr; i++)
1665 uart_line_info(m, drv, i);
1666 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
Alexey Dobriyand196a942009-03-31 15:19:21 -07001668
1669static int uart_proc_open(struct inode *inode, struct file *file)
1670{
1671 return single_open(file, uart_proc_show, PDE(inode)->data);
1672}
1673
1674static const struct file_operations uart_proc_fops = {
1675 .owner = THIS_MODULE,
1676 .open = uart_proc_open,
1677 .read = seq_read,
1678 .llseek = seq_lseek,
1679 .release = single_release,
1680};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681#endif
1682
Andrew Morton4a1b5502008-03-07 15:51:16 -08001683#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684/*
Russell Kingd3587882006-03-20 20:00:09 +00001685 * uart_console_write - write a console message to a serial port
1686 * @port: the port to write the message
1687 * @s: array of characters
1688 * @count: number of characters in string to write
1689 * @write: function to write character to port
1690 */
1691void uart_console_write(struct uart_port *port, const char *s,
1692 unsigned int count,
1693 void (*putchar)(struct uart_port *, int))
1694{
1695 unsigned int i;
1696
1697 for (i = 0; i < count; i++, s++) {
1698 if (*s == '\n')
1699 putchar(port, '\r');
1700 putchar(port, *s);
1701 }
1702}
1703EXPORT_SYMBOL_GPL(uart_console_write);
1704
1705/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 * Check whether an invalid uart number has been specified, and
1707 * if so, search for the first available port that does have
1708 * console support.
1709 */
1710struct uart_port * __init
1711uart_get_console(struct uart_port *ports, int nr, struct console *co)
1712{
1713 int idx = co->index;
1714
1715 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1716 ports[idx].membase == NULL))
1717 for (idx = 0; idx < nr; idx++)
1718 if (ports[idx].iobase != 0 ||
1719 ports[idx].membase != NULL)
1720 break;
1721
1722 co->index = idx;
1723
1724 return ports + idx;
1725}
1726
1727/**
1728 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1729 * @options: pointer to option string
1730 * @baud: pointer to an 'int' variable for the baud rate.
1731 * @parity: pointer to an 'int' variable for the parity.
1732 * @bits: pointer to an 'int' variable for the number of data bits.
1733 * @flow: pointer to an 'int' variable for the flow control character.
1734 *
1735 * uart_parse_options decodes a string containing the serial console
1736 * options. The format of the string is <baud><parity><bits><flow>,
1737 * eg: 115200n8r
1738 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001739void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1741{
1742 char *s = options;
1743
1744 *baud = simple_strtoul(s, NULL, 10);
1745 while (*s >= '0' && *s <= '9')
1746 s++;
1747 if (*s)
1748 *parity = *s++;
1749 if (*s)
1750 *bits = *s++ - '0';
1751 if (*s)
1752 *flow = *s;
1753}
Jason Wesself2d937f2008-04-17 20:05:37 +02001754EXPORT_SYMBOL_GPL(uart_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756struct baud_rates {
1757 unsigned int rate;
1758 unsigned int cflag;
1759};
1760
Arjan van de Vencb3592b2005-11-28 21:04:11 +00001761static const struct baud_rates baud_rates[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 { 921600, B921600 },
1763 { 460800, B460800 },
1764 { 230400, B230400 },
1765 { 115200, B115200 },
1766 { 57600, B57600 },
1767 { 38400, B38400 },
1768 { 19200, B19200 },
1769 { 9600, B9600 },
1770 { 4800, B4800 },
1771 { 2400, B2400 },
1772 { 1200, B1200 },
1773 { 0, B38400 }
1774};
1775
1776/**
1777 * uart_set_options - setup the serial console parameters
1778 * @port: pointer to the serial ports uart_port structure
1779 * @co: console pointer
1780 * @baud: baud rate
1781 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1782 * @bits: number of data bits
1783 * @flow: flow control character - 'r' (rts)
1784 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001785int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786uart_set_options(struct uart_port *port, struct console *co,
1787 int baud, int parity, int bits, int flow)
1788{
Alan Cox606d0992006-12-08 02:38:45 -08001789 struct ktermios termios;
Alan Cox149b36e2007-10-18 01:24:16 -07001790 static struct ktermios dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 int i;
1792
Russell King976ecd12005-07-03 21:05:45 +01001793 /*
1794 * Ensure that the serial console lock is initialised
1795 * early.
1796 */
1797 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07001798 lockdep_set_class(&port->lock, &port_lock_key);
Russell King976ecd12005-07-03 21:05:45 +01001799
Alan Cox606d0992006-12-08 02:38:45 -08001800 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1803
1804 /*
1805 * Construct a cflag setting.
1806 */
1807 for (i = 0; baud_rates[i].rate; i++)
1808 if (baud_rates[i].rate <= baud)
1809 break;
1810
1811 termios.c_cflag |= baud_rates[i].cflag;
1812
1813 if (bits == 7)
1814 termios.c_cflag |= CS7;
1815 else
1816 termios.c_cflag |= CS8;
1817
1818 switch (parity) {
1819 case 'o': case 'O':
1820 termios.c_cflag |= PARODD;
1821 /*fall through*/
1822 case 'e': case 'E':
1823 termios.c_cflag |= PARENB;
1824 break;
1825 }
1826
1827 if (flow == 'r')
1828 termios.c_cflag |= CRTSCTS;
1829
Yinghai Lu79492682007-07-15 23:37:25 -07001830 /*
1831 * some uarts on other side don't support no flow control.
1832 * So we set * DTR in host uart to make them happy
1833 */
1834 port->mctrl |= TIOCM_DTR;
1835
Alan Cox149b36e2007-10-18 01:24:16 -07001836 port->ops->set_termios(port, &termios, &dummy);
Jason Wesself2d937f2008-04-17 20:05:37 +02001837 /*
1838 * Allow the setting of the UART parameters with a NULL console
1839 * too:
1840 */
1841 if (co)
1842 co->cflag = termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 return 0;
1845}
Jason Wesself2d937f2008-04-17 20:05:37 +02001846EXPORT_SYMBOL_GPL(uart_set_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847#endif /* CONFIG_SERIAL_CORE_CONSOLE */
1848
1849static void uart_change_pm(struct uart_state *state, int pm_state)
1850{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001851 struct uart_port *port = state->uart_port;
Andrew Victor1281e362006-05-16 11:28:49 +01001852
1853 if (state->pm_state != pm_state) {
1854 if (port->ops->pm)
1855 port->ops->pm(port, pm_state, state->pm_state);
1856 state->pm_state = pm_state;
1857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001860struct uart_match {
1861 struct uart_port *port;
1862 struct uart_driver *driver;
1863};
1864
1865static int serial_match_port(struct device *dev, void *data)
1866{
1867 struct uart_match *match = data;
Guennadi Liakhovetski7ca796f2008-07-04 09:59:28 -07001868 struct tty_driver *tty_drv = match->driver->tty_driver;
1869 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1870 match->port->line;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001871
1872 return dev->devt == devt; /* Actually, only one tty per port */
1873}
1874
Alan Coxccce6de2009-09-19 13:13:30 -07001875int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Alan Coxccce6de2009-09-19 13:13:30 -07001877 struct uart_state *state = drv->state + uport->line;
1878 struct tty_port *port = &state->port;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001879 struct device *tty_dev;
Alan Coxccce6de2009-09-19 13:13:30 -07001880 struct uart_match match = {uport, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Alan Coxa2bceae2009-09-19 13:13:31 -07001882 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Alan Coxccce6de2009-09-19 13:13:30 -07001884 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001885 if (device_may_wakeup(tty_dev)) {
Govindraj.R3f960db2010-12-16 18:12:47 +05301886 if (!enable_irq_wake(uport->irq))
1887 uport->irq_wake = 1;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001888 put_device(tty_dev);
Alan Coxa2bceae2009-09-19 13:13:31 -07001889 mutex_unlock(&port->mutex);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001890 return 0;
1891 }
Stanislav Brabec4547be72009-12-02 16:20:56 +01001892 if (console_suspend_enabled || !uart_console(uport))
1893 uport->suspended = 1;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001894
Alan Coxccce6de2009-09-19 13:13:30 -07001895 if (port->flags & ASYNC_INITIALIZED) {
1896 const struct uart_ops *ops = uport->ops;
Russell Kingc8c6bfa2008-02-04 22:27:52 -08001897 int tries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Stanislav Brabec4547be72009-12-02 16:20:56 +01001899 if (console_suspend_enabled || !uart_console(uport)) {
1900 set_bit(ASYNCB_SUSPENDED, &port->flags);
1901 clear_bit(ASYNCB_INITIALIZED, &port->flags);
Russell Kinga6b93a92006-10-01 17:17:40 +01001902
Stanislav Brabec4547be72009-12-02 16:20:56 +01001903 spin_lock_irq(&uport->lock);
1904 ops->stop_tx(uport);
1905 ops->set_mctrl(uport, 0);
1906 ops->stop_rx(uport);
1907 spin_unlock_irq(&uport->lock);
1908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 /*
1911 * Wait for the transmitter to empty.
1912 */
Alan Coxccce6de2009-09-19 13:13:30 -07001913 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 msleep(10);
Russell Kingc8c6bfa2008-02-04 22:27:52 -08001915 if (!tries)
Alan Coxa46c9992008-02-08 04:18:53 -08001916 printk(KERN_ERR "%s%s%s%d: Unable to drain "
1917 "transmitter\n",
Alan Coxccce6de2009-09-19 13:13:30 -07001918 uport->dev ? dev_name(uport->dev) : "",
1919 uport->dev ? ": " : "",
David S. Miller84408382008-10-13 10:45:26 +01001920 drv->dev_name,
Alan Coxccce6de2009-09-19 13:13:30 -07001921 drv->tty_driver->name_base + uport->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Stanislav Brabec4547be72009-12-02 16:20:56 +01001923 if (console_suspend_enabled || !uart_console(uport))
1924 ops->shutdown(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
1926
1927 /*
1928 * Disable the console device before suspending.
1929 */
Stanislav Brabec4547be72009-12-02 16:20:56 +01001930 if (console_suspend_enabled && uart_console(uport))
Alan Coxccce6de2009-09-19 13:13:30 -07001931 console_stop(uport->cons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Stanislav Brabec4547be72009-12-02 16:20:56 +01001933 if (console_suspend_enabled || !uart_console(uport))
1934 uart_change_pm(state, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Alan Coxa2bceae2009-09-19 13:13:31 -07001936 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 return 0;
1939}
1940
Alan Coxccce6de2009-09-19 13:13:30 -07001941int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Alan Coxccce6de2009-09-19 13:13:30 -07001943 struct uart_state *state = drv->state + uport->line;
1944 struct tty_port *port = &state->port;
Arjan van de Ven03a74dc2008-05-23 13:04:49 -07001945 struct device *tty_dev;
Alan Coxccce6de2009-09-19 13:13:30 -07001946 struct uart_match match = {uport, drv};
Deepak Saxenaba15ab02009-09-19 13:13:33 -07001947 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Alan Coxa2bceae2009-09-19 13:13:31 -07001949 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Alan Coxccce6de2009-09-19 13:13:30 -07001951 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1952 if (!uport->suspended && device_may_wakeup(tty_dev)) {
Govindraj.R3f960db2010-12-16 18:12:47 +05301953 if (uport->irq_wake) {
1954 disable_irq_wake(uport->irq);
1955 uport->irq_wake = 0;
1956 }
Alan Coxa2bceae2009-09-19 13:13:31 -07001957 mutex_unlock(&port->mutex);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001958 return 0;
1959 }
Alan Coxccce6de2009-09-19 13:13:30 -07001960 uport->suspended = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 /*
1963 * Re-enable the console device after suspending.
1964 */
Yin Kangkai5933a162011-01-30 11:15:30 +08001965 if (uart_console(uport)) {
Jason Wang891b9dd2010-08-21 15:14:42 +08001966 /*
1967 * First try to use the console cflag setting.
1968 */
1969 memset(&termios, 0, sizeof(struct ktermios));
1970 termios.c_cflag = uport->cons->cflag;
1971
1972 /*
1973 * If that's unset, use the tty termios setting.
1974 */
1975 if (port->tty && port->tty->termios && termios.c_cflag == 0)
1976 termios = *(port->tty->termios);
1977
Alan Coxccce6de2009-09-19 13:13:30 -07001978 uport->ops->set_termios(uport, &termios, NULL);
Yin Kangkai5933a162011-01-30 11:15:30 +08001979 if (console_suspend_enabled)
1980 console_start(uport->cons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
1982
Alan Coxccce6de2009-09-19 13:13:30 -07001983 if (port->flags & ASYNC_SUSPENDED) {
1984 const struct uart_ops *ops = uport->ops;
Russell Kingee31b332005-11-13 15:28:51 +00001985 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Russell King9d778a62008-02-04 22:27:51 -08001987 uart_change_pm(state, 0);
Alan Coxccce6de2009-09-19 13:13:30 -07001988 spin_lock_irq(&uport->lock);
1989 ops->set_mctrl(uport, 0);
1990 spin_unlock_irq(&uport->lock);
Stanislav Brabec4547be72009-12-02 16:20:56 +01001991 if (console_suspend_enabled || !uart_console(uport)) {
Alan Cox19225132010-06-01 22:52:51 +02001992 /* Protected by port mutex for now */
1993 struct tty_struct *tty = port->tty;
Stanislav Brabec4547be72009-12-02 16:20:56 +01001994 ret = ops->startup(uport);
1995 if (ret == 0) {
Alan Cox19225132010-06-01 22:52:51 +02001996 if (tty)
1997 uart_change_speed(tty, state, NULL);
Stanislav Brabec4547be72009-12-02 16:20:56 +01001998 spin_lock_irq(&uport->lock);
1999 ops->set_mctrl(uport, uport->mctrl);
2000 ops->start_tx(uport);
2001 spin_unlock_irq(&uport->lock);
2002 set_bit(ASYNCB_INITIALIZED, &port->flags);
2003 } else {
2004 /*
2005 * Failed to resume - maybe hardware went away?
2006 * Clear the "initialized" flag so we won't try
2007 * to call the low level drivers shutdown method.
2008 */
Alan Cox19225132010-06-01 22:52:51 +02002009 uart_shutdown(tty, state);
Stanislav Brabec4547be72009-12-02 16:20:56 +01002010 }
Russell Kingee31b332005-11-13 15:28:51 +00002011 }
Russell Kinga6b93a92006-10-01 17:17:40 +01002012
Alan Coxccce6de2009-09-19 13:13:30 -07002013 clear_bit(ASYNCB_SUSPENDED, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015
Alan Coxa2bceae2009-09-19 13:13:31 -07002016 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 return 0;
2019}
2020
2021static inline void
2022uart_report_port(struct uart_driver *drv, struct uart_port *port)
2023{
Russell King30b7a3b2005-09-03 15:30:21 +01002024 char address[64];
2025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 switch (port->iotype) {
2027 case UPIO_PORT:
Andrew Morton9bde10a2008-10-13 10:35:42 +01002028 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 break;
2030 case UPIO_HUB6:
Russell King30b7a3b2005-09-03 15:30:21 +01002031 snprintf(address, sizeof(address),
Andrew Morton9bde10a2008-10-13 10:35:42 +01002032 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 break;
2034 case UPIO_MEM:
2035 case UPIO_MEM32:
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002036 case UPIO_AU:
Zang Roy-r619113be91ec2006-06-30 02:29:58 -07002037 case UPIO_TSI:
Russell King30b7a3b2005-09-03 15:30:21 +01002038 snprintf(address, sizeof(address),
Josh Boyer4f640ef2007-07-23 18:43:44 -07002039 "MMIO 0x%llx", (unsigned long long)port->mapbase);
Russell King30b7a3b2005-09-03 15:30:21 +01002040 break;
2041 default:
2042 strlcpy(address, "*unknown*", sizeof(address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 break;
2044 }
Russell King30b7a3b2005-09-03 15:30:21 +01002045
Russell King0cf669d2005-10-31 11:42:22 +00002046 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
Kay Sievers4bfe0902009-01-06 10:44:37 -08002047 port->dev ? dev_name(port->dev) : "",
Russell King0cf669d2005-10-31 11:42:22 +00002048 port->dev ? ": " : "",
David S. Miller84408382008-10-13 10:45:26 +01002049 drv->dev_name,
2050 drv->tty_driver->name_base + port->line,
2051 address, port->irq, uart_type(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
2054static void
2055uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2056 struct uart_port *port)
2057{
2058 unsigned int flags;
2059
2060 /*
2061 * If there isn't a port here, don't do anything further.
2062 */
2063 if (!port->iobase && !port->mapbase && !port->membase)
2064 return;
2065
2066 /*
2067 * Now do the auto configuration stuff. Note that config_port
2068 * is expected to claim the resources and map the port for us.
2069 */
David Daney8e23fcc2009-01-02 13:49:54 +00002070 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 if (port->flags & UPF_AUTO_IRQ)
2072 flags |= UART_CONFIG_IRQ;
2073 if (port->flags & UPF_BOOT_AUTOCONF) {
David Daney8e23fcc2009-01-02 13:49:54 +00002074 if (!(port->flags & UPF_FIXED_TYPE)) {
2075 port->type = PORT_UNKNOWN;
2076 flags |= UART_CONFIG_TYPE;
2077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 port->ops->config_port(port, flags);
2079 }
2080
2081 if (port->type != PORT_UNKNOWN) {
2082 unsigned long flags;
2083
2084 uart_report_port(drv, port);
2085
George G. Davis3689a0e2007-02-14 00:33:06 -08002086 /* Power up port for set_mctrl() */
2087 uart_change_pm(state, 0);
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 /*
2090 * Ensure that the modem control lines are de-activated.
Yinghai Luc3e46422008-02-04 22:27:46 -08002091 * keep the DTR setting that is set in uart_set_options()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 * We probably don't need a spinlock around this, but
2093 */
2094 spin_lock_irqsave(&port->lock, flags);
Yinghai Luc3e46422008-02-04 22:27:46 -08002095 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 spin_unlock_irqrestore(&port->lock, flags);
2097
2098 /*
Russell King97d97222007-09-01 21:25:09 +01002099 * If this driver supports console, and it hasn't been
2100 * successfully registered yet, try to re-register it.
2101 * It may be that the port was not available.
2102 */
2103 if (port->cons && !(port->cons->flags & CON_ENABLED))
2104 register_console(port->cons);
2105
2106 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 * Power down all ports by default, except the
2108 * console if we have one.
2109 */
2110 if (!uart_console(port))
2111 uart_change_pm(state, 3);
2112 }
2113}
2114
Jason Wesself2d937f2008-04-17 20:05:37 +02002115#ifdef CONFIG_CONSOLE_POLL
2116
2117static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2118{
2119 struct uart_driver *drv = driver->driver_state;
2120 struct uart_state *state = drv->state + line;
2121 struct uart_port *port;
2122 int baud = 9600;
2123 int bits = 8;
2124 int parity = 'n';
2125 int flow = 'n';
2126
Alan Coxebd2c8f2009-09-19 13:13:28 -07002127 if (!state || !state->uart_port)
Jason Wesself2d937f2008-04-17 20:05:37 +02002128 return -1;
2129
Alan Coxebd2c8f2009-09-19 13:13:28 -07002130 port = state->uart_port;
Jason Wesself2d937f2008-04-17 20:05:37 +02002131 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2132 return -1;
2133
2134 if (options) {
2135 uart_parse_options(options, &baud, &parity, &bits, &flow);
2136 return uart_set_options(port, NULL, baud, parity, bits, flow);
2137 }
2138
2139 return 0;
2140}
2141
2142static int uart_poll_get_char(struct tty_driver *driver, int line)
2143{
2144 struct uart_driver *drv = driver->driver_state;
2145 struct uart_state *state = drv->state + line;
2146 struct uart_port *port;
2147
Alan Coxebd2c8f2009-09-19 13:13:28 -07002148 if (!state || !state->uart_port)
Jason Wesself2d937f2008-04-17 20:05:37 +02002149 return -1;
2150
Alan Coxebd2c8f2009-09-19 13:13:28 -07002151 port = state->uart_port;
Jason Wesself2d937f2008-04-17 20:05:37 +02002152 return port->ops->poll_get_char(port);
2153}
2154
2155static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2156{
2157 struct uart_driver *drv = driver->driver_state;
2158 struct uart_state *state = drv->state + line;
2159 struct uart_port *port;
2160
Alan Coxebd2c8f2009-09-19 13:13:28 -07002161 if (!state || !state->uart_port)
Jason Wesself2d937f2008-04-17 20:05:37 +02002162 return;
2163
Alan Coxebd2c8f2009-09-19 13:13:28 -07002164 port = state->uart_port;
Jason Wesself2d937f2008-04-17 20:05:37 +02002165 port->ops->poll_put_char(port, ch);
2166}
2167#endif
2168
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002169static const struct tty_operations uart_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 .open = uart_open,
2171 .close = uart_close,
2172 .write = uart_write,
2173 .put_char = uart_put_char,
2174 .flush_chars = uart_flush_chars,
2175 .write_room = uart_write_room,
2176 .chars_in_buffer= uart_chars_in_buffer,
2177 .flush_buffer = uart_flush_buffer,
2178 .ioctl = uart_ioctl,
2179 .throttle = uart_throttle,
2180 .unthrottle = uart_unthrottle,
2181 .send_xchar = uart_send_xchar,
2182 .set_termios = uart_set_termios,
Alan Cox64e91592008-06-03 15:18:54 +01002183 .set_ldisc = uart_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 .stop = uart_stop,
2185 .start = uart_start,
2186 .hangup = uart_hangup,
2187 .break_ctl = uart_break_ctl,
2188 .wait_until_sent= uart_wait_until_sent,
2189#ifdef CONFIG_PROC_FS
Alexey Dobriyand196a942009-03-31 15:19:21 -07002190 .proc_fops = &uart_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191#endif
2192 .tiocmget = uart_tiocmget,
2193 .tiocmset = uart_tiocmset,
Alan Coxd281da72010-09-16 18:21:24 +01002194 .get_icount = uart_get_icount,
Jason Wesself2d937f2008-04-17 20:05:37 +02002195#ifdef CONFIG_CONSOLE_POLL
2196 .poll_init = uart_poll_init,
2197 .poll_get_char = uart_poll_get_char,
2198 .poll_put_char = uart_poll_put_char,
2199#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200};
2201
Alan Coxde0c8cb2010-06-01 22:52:58 +02002202static const struct tty_port_operations uart_port_ops = {
2203 .carrier_raised = uart_carrier_raised,
2204 .dtr_rts = uart_dtr_rts,
2205};
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207/**
2208 * uart_register_driver - register a driver with the uart core layer
2209 * @drv: low level driver structure
2210 *
2211 * Register a uart driver with the core driver. We in turn register
2212 * with the tty layer, and initialise the core driver per-port state.
2213 *
2214 * We have a proc file in /proc/tty/driver which is named after the
2215 * normal driver.
2216 *
2217 * drv->port should be NULL, and the per-port structures should be
2218 * registered using uart_add_one_port after this call has succeeded.
2219 */
2220int uart_register_driver(struct uart_driver *drv)
2221{
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002222 struct tty_driver *normal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 int i, retval;
2224
2225 BUG_ON(drv->state);
2226
2227 /*
2228 * Maybe we should be using a slab cache for this, especially if
2229 * we have a large number of ports to handle.
2230 */
Burman Yan8f31bb32007-02-14 00:33:07 -08002231 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (!drv->state)
2233 goto out;
2234
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002235 normal = alloc_tty_driver(drv->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 if (!normal)
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002237 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 drv->tty_driver = normal;
2240
2241 normal->owner = drv->owner;
2242 normal->driver_name = drv->driver_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 normal->name = drv->dev_name;
2244 normal->major = drv->major;
2245 normal->minor_start = drv->minor;
2246 normal->type = TTY_DRIVER_TYPE_SERIAL;
2247 normal->subtype = SERIAL_TYPE_NORMAL;
2248 normal->init_termios = tty_std_termios;
2249 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -08002250 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07002251 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 normal->driver_state = drv;
2253 tty_set_operations(normal, &uart_ops);
2254
2255 /*
2256 * Initialise the UART state(s).
2257 */
2258 for (i = 0; i < drv->nr; i++) {
2259 struct uart_state *state = drv->state + i;
Alan Coxa2bceae2009-09-19 13:13:31 -07002260 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Alan Coxa2bceae2009-09-19 13:13:31 -07002262 tty_port_init(port);
Alan Coxde0c8cb2010-06-01 22:52:58 +02002263 port->ops = &uart_port_ops;
Alan Coxa2bceae2009-09-19 13:13:31 -07002264 port->close_delay = 500; /* .5 seconds */
2265 port->closing_wait = 30000; /* 30 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267
2268 retval = tty_register_driver(normal);
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002269 if (retval >= 0)
2270 return retval;
2271
2272 put_tty_driver(normal);
2273out_kfree:
2274 kfree(drv->state);
2275out:
2276 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
2278
2279/**
2280 * uart_unregister_driver - remove a driver from the uart core layer
2281 * @drv: low level driver structure
2282 *
2283 * Remove all references to a driver from the core driver. The low
2284 * level driver must have removed all its ports via the
2285 * uart_remove_one_port() if it registered them with uart_add_one_port().
2286 * (ie, drv->port == NULL)
2287 */
2288void uart_unregister_driver(struct uart_driver *drv)
2289{
2290 struct tty_driver *p = drv->tty_driver;
2291 tty_unregister_driver(p);
2292 put_tty_driver(p);
2293 kfree(drv->state);
2294 drv->tty_driver = NULL;
2295}
2296
2297struct tty_driver *uart_console_device(struct console *co, int *index)
2298{
2299 struct uart_driver *p = co->data;
2300 *index = co->index;
2301 return p->tty_driver;
2302}
2303
2304/**
2305 * uart_add_one_port - attach a driver-defined port structure
2306 * @drv: pointer to the uart low level driver structure for this port
Randy Dunlap1b9894f2009-09-21 11:12:03 -07002307 * @uport: uart port structure to use for this port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 *
2309 * This allows the driver to register its own uart_port structure
2310 * with the core driver. The main purpose is to allow the low
2311 * level uart drivers to expand uart_port, rather than having yet
2312 * more levels of structures.
2313 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002314int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
2316 struct uart_state *state;
Alan Coxa2bceae2009-09-19 13:13:31 -07002317 struct tty_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 int ret = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002319 struct device *tty_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 BUG_ON(in_interrupt());
2322
Alan Coxa2bceae2009-09-19 13:13:31 -07002323 if (uport->line >= drv->nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return -EINVAL;
2325
Alan Coxa2bceae2009-09-19 13:13:31 -07002326 state = drv->state + uport->line;
2327 port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002329 mutex_lock(&port_mutex);
Alan Coxa2bceae2009-09-19 13:13:31 -07002330 mutex_lock(&port->mutex);
Alan Coxebd2c8f2009-09-19 13:13:28 -07002331 if (state->uart_port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 ret = -EINVAL;
2333 goto out;
2334 }
2335
Alan Coxa2bceae2009-09-19 13:13:31 -07002336 state->uart_port = uport;
Russell King97d97222007-09-01 21:25:09 +01002337 state->pm_state = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Alan Coxa2bceae2009-09-19 13:13:31 -07002339 uport->cons = drv->cons;
2340 uport->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Russell King976ecd12005-07-03 21:05:45 +01002342 /*
2343 * If this port is a console, then the spinlock is already
2344 * initialised.
2345 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002346 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2347 spin_lock_init(&uport->lock);
2348 lockdep_set_class(&uport->lock, &port_lock_key);
Ingo Molnar13e83592006-07-03 00:25:03 -07002349 }
Russell King976ecd12005-07-03 21:05:45 +01002350
Alan Coxa2bceae2009-09-19 13:13:31 -07002351 uart_configure_port(drv, state, uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 /*
2354 * Register the port whether it's detected or not. This allows
2355 * setserial to be used to alter this ports parameters.
2356 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002357 tty_dev = tty_register_device(drv->tty_driver, uport->line, uport->dev);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002358 if (likely(!IS_ERR(tty_dev))) {
Alan Stern74081f82008-03-19 22:35:13 +01002359 device_init_wakeup(tty_dev, 1);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002360 device_set_wakeup_enable(tty_dev, 0);
2361 } else
2362 printk(KERN_ERR "Cannot register tty device on line %d\n",
Alan Coxa2bceae2009-09-19 13:13:31 -07002363 uport->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 /*
Russell King68ac64c2006-04-30 11:13:50 +01002366 * Ensure UPF_DEAD is not set.
2367 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002368 uport->flags &= ~UPF_DEAD;
Russell King68ac64c2006-04-30 11:13:50 +01002369
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 out:
Alan Coxa2bceae2009-09-19 13:13:31 -07002371 mutex_unlock(&port->mutex);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002372 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
2374 return ret;
2375}
2376
2377/**
2378 * uart_remove_one_port - detach a driver defined port structure
2379 * @drv: pointer to the uart low level driver structure for this port
Randy Dunlap1b9894f2009-09-21 11:12:03 -07002380 * @uport: uart port structure for this port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 *
2382 * This unhooks (and hangs up) the specified port structure from the
2383 * core driver. No further calls will be made to the low-level code
2384 * for this port.
2385 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002386int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
Alan Coxa2bceae2009-09-19 13:13:31 -07002388 struct uart_state *state = drv->state + uport->line;
2389 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 BUG_ON(in_interrupt());
2392
Alan Coxa2bceae2009-09-19 13:13:31 -07002393 if (state->uart_port != uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
Alan Coxa2bceae2009-09-19 13:13:31 -07002395 state->uart_port, uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002397 mutex_lock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
2399 /*
Russell King68ac64c2006-04-30 11:13:50 +01002400 * Mark the port "dead" - this prevents any opens from
2401 * succeeding while we shut down the port.
2402 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002403 mutex_lock(&port->mutex);
2404 uport->flags |= UPF_DEAD;
2405 mutex_unlock(&port->mutex);
Russell King68ac64c2006-04-30 11:13:50 +01002406
2407 /*
Greg Kroah-Hartmanaa4148c2005-06-20 21:15:16 -07002408 * Remove the devices from the tty layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002410 tty_unregister_device(drv->tty_driver, uport->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Alan Coxa2bceae2009-09-19 13:13:31 -07002412 if (port->tty)
2413 tty_vhangup(port->tty);
Russell King68ac64c2006-04-30 11:13:50 +01002414
2415 /*
Russell King68ac64c2006-04-30 11:13:50 +01002416 * Free the port IO and memory resources, if any.
2417 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002418 if (uport->type != PORT_UNKNOWN)
2419 uport->ops->release_port(uport);
Russell King68ac64c2006-04-30 11:13:50 +01002420
2421 /*
2422 * Indicate that there isn't a port here anymore.
2423 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002424 uport->type = PORT_UNKNOWN;
Russell King68ac64c2006-04-30 11:13:50 +01002425
Alan Coxebd2c8f2009-09-19 13:13:28 -07002426 state->uart_port = NULL;
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002427 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
2429 return 0;
2430}
2431
2432/*
2433 * Are the two ports equivalent?
2434 */
2435int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2436{
2437 if (port1->iotype != port2->iotype)
2438 return 0;
2439
2440 switch (port1->iotype) {
2441 case UPIO_PORT:
2442 return (port1->iobase == port2->iobase);
2443 case UPIO_HUB6:
2444 return (port1->iobase == port2->iobase) &&
2445 (port1->hub6 == port2->hub6);
2446 case UPIO_MEM:
Sergei Shtylyovd21b55d2006-08-28 19:49:03 +04002447 case UPIO_MEM32:
2448 case UPIO_AU:
2449 case UPIO_TSI:
Benjamin Herrenschmidt1624f002006-01-04 18:09:44 +00002450 return (port1->mapbase == port2->mapbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
2452 return 0;
2453}
2454EXPORT_SYMBOL(uart_match_port);
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456EXPORT_SYMBOL(uart_write_wakeup);
2457EXPORT_SYMBOL(uart_register_driver);
2458EXPORT_SYMBOL(uart_unregister_driver);
2459EXPORT_SYMBOL(uart_suspend_port);
2460EXPORT_SYMBOL(uart_resume_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461EXPORT_SYMBOL(uart_add_one_port);
2462EXPORT_SYMBOL(uart_remove_one_port);
2463
2464MODULE_DESCRIPTION("Serial driver core");
2465MODULE_LICENSE("GPL");