blob: b0bb29d804ae43620c7d0ce1ba5c94cc9f11a28a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/core.c
3 *
4 * Driver core for serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * Copyright 1999 ARM Limited
9 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/tty.h>
27#include <linux/slab.h>
28#include <linux/init.h>
29#include <linux/console.h>
Alexey Dobriyand196a942009-03-31 15:19:21 -070030#include <linux/proc_fs.h>
31#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/serial_core.h>
33#include <linux/smp_lock.h>
34#include <linux/device.h>
35#include <linux/serial.h> /* for serial_state and serial_icounter_struct */
36#include <linux/delay.h>
Arjan van de Venf392ecf2006-01-12 18:44:32 +000037#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/irq.h>
40#include <asm/uaccess.h>
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
43 * This is used to lock changes in serial line configuration.
44 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +000045static DEFINE_MUTEX(port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Ingo Molnar13e83592006-07-03 00:25:03 -070047/*
48 * lockdep: port->lock is initialized in two places, but we
49 * want only one lock-class:
50 */
51static struct lock_class_key port_lock_key;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
54
Alan Coxf7519282009-01-02 13:49:21 +000055#define uart_users(state) ((state)->count + (state)->info.port.blocked_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#ifdef CONFIG_SERIAL_CORE_CONSOLE
58#define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
59#else
60#define uart_console(port) (0)
61#endif
62
Alan Coxa46c9992008-02-08 04:18:53 -080063static void uart_change_speed(struct uart_state *state,
64 struct ktermios *old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
66static void uart_change_pm(struct uart_state *state, int pm_state);
67
68/*
69 * This routine is used by the interrupt handler to schedule processing in
70 * the software interrupt portion of the driver.
71 */
72void uart_write_wakeup(struct uart_port *port)
73{
74 struct uart_info *info = port->info;
Pavel Machekd5f735e2006-03-07 21:55:20 -080075 /*
76 * This means you called this function _after_ the port was
77 * closed. No cookie for you.
78 */
79 BUG_ON(!info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 tasklet_schedule(&info->tlet);
81}
82
83static void uart_stop(struct tty_struct *tty)
84{
85 struct uart_state *state = tty->driver_data;
86 struct uart_port *port = state->port;
87 unsigned long flags;
88
89 spin_lock_irqsave(&port->lock, flags);
Russell Kingb129a8c2005-08-31 10:12:14 +010090 port->ops->stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 spin_unlock_irqrestore(&port->lock, flags);
92}
93
94static void __uart_start(struct tty_struct *tty)
95{
96 struct uart_state *state = tty->driver_data;
97 struct uart_port *port = state->port;
98
Alan Coxf7519282009-01-02 13:49:21 +000099 if (!uart_circ_empty(&state->info.xmit) && state->info.xmit.buf &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 !tty->stopped && !tty->hw_stopped)
Russell Kingb129a8c2005-08-31 10:12:14 +0100101 port->ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104static void uart_start(struct tty_struct *tty)
105{
106 struct uart_state *state = tty->driver_data;
107 struct uart_port *port = state->port;
108 unsigned long flags;
109
110 spin_lock_irqsave(&port->lock, flags);
111 __uart_start(tty);
112 spin_unlock_irqrestore(&port->lock, flags);
113}
114
115static void uart_tasklet_action(unsigned long data)
116{
117 struct uart_state *state = (struct uart_state *)data;
Alan Coxf7519282009-01-02 13:49:21 +0000118 tty_wakeup(state->info.port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121static inline void
122uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
123{
124 unsigned long flags;
125 unsigned int old;
126
127 spin_lock_irqsave(&port->lock, flags);
128 old = port->mctrl;
129 port->mctrl = (old & ~clear) | set;
130 if (old != port->mctrl)
131 port->ops->set_mctrl(port, port->mctrl);
132 spin_unlock_irqrestore(&port->lock, flags);
133}
134
Alan Coxa46c9992008-02-08 04:18:53 -0800135#define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
136#define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/*
139 * Startup the port. This will be called once per open. All calls
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100140 * will be serialised by the per-port mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 */
142static int uart_startup(struct uart_state *state, int init_hw)
143{
Alan Coxf7519282009-01-02 13:49:21 +0000144 struct uart_info *info = &state->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct uart_port *port = state->port;
146 unsigned long page;
147 int retval = 0;
148
149 if (info->flags & UIF_INITIALIZED)
150 return 0;
151
152 /*
153 * Set the TTY IO error marker - we will only clear this
154 * once we have successfully opened the port. Also set
155 * up the tty->alt_speed kludge
156 */
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100157 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 if (port->type == PORT_UNKNOWN)
160 return 0;
161
162 /*
163 * Initialise and allocate the transmit and temporary
164 * buffer.
165 */
166 if (!info->xmit.buf) {
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100167 /* This is protected by the per port mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 page = get_zeroed_page(GFP_KERNEL);
169 if (!page)
170 return -ENOMEM;
171
172 info->xmit.buf = (unsigned char *) page;
173 uart_circ_clear(&info->xmit);
174 }
175
176 retval = port->ops->startup(port);
177 if (retval == 0) {
178 if (init_hw) {
179 /*
180 * Initialise the hardware port settings.
181 */
182 uart_change_speed(state, NULL);
183
184 /*
185 * Setup the RTS and DTR signals once the
186 * port is open and ready to respond.
187 */
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100188 if (info->port.tty->termios->c_cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
190 }
191
Russell King0dd7a1a2005-06-29 18:40:53 +0100192 if (info->flags & UIF_CTS_FLOW) {
193 spin_lock_irq(&port->lock);
194 if (!(port->ops->get_mctrl(port) & TIOCM_CTS))
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100195 info->port.tty->hw_stopped = 1;
Russell King0dd7a1a2005-06-29 18:40:53 +0100196 spin_unlock_irq(&port->lock);
197 }
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 info->flags |= UIF_INITIALIZED;
200
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100201 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203
204 if (retval && capable(CAP_SYS_ADMIN))
205 retval = 0;
206
207 return retval;
208}
209
210/*
211 * This routine will shutdown a serial port; interrupts are disabled, and
212 * DTR is dropped if the hangup on close termio flag is on. Calls to
213 * uart_shutdown are serialised by the per-port semaphore.
214 */
215static void uart_shutdown(struct uart_state *state)
216{
Alan Coxf7519282009-01-02 13:49:21 +0000217 struct uart_info *info = &state->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 struct uart_port *port = state->port;
Alan Coxf7519282009-01-02 13:49:21 +0000219 struct tty_struct *tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Russell Kingee31b332005-11-13 15:28:51 +0000221 /*
222 * Set the TTY IO error marker
223 */
Alan Coxf7519282009-01-02 13:49:21 +0000224 if (tty)
225 set_bit(TTY_IO_ERROR, &tty->flags);
Russell Kingee31b332005-11-13 15:28:51 +0000226
227 if (info->flags & UIF_INITIALIZED) {
228 info->flags &= ~UIF_INITIALIZED;
229
230 /*
231 * Turn off DTR and RTS early.
232 */
Alan Coxf7519282009-01-02 13:49:21 +0000233 if (!tty || (tty->termios->c_cflag & HUPCL))
Russell Kingee31b332005-11-13 15:28:51 +0000234 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
235
236 /*
237 * clear delta_msr_wait queue to avoid mem leaks: we may free
238 * the irq here so the queue might never be woken up. Note
239 * that we won't end up waiting on delta_msr_wait again since
240 * any outstanding file descriptors should be pointing at
241 * hung_up_tty_fops now.
242 */
243 wake_up_interruptible(&info->delta_msr_wait);
244
245 /*
246 * Free the IRQ and disable the port.
247 */
248 port->ops->shutdown(port);
249
250 /*
251 * Ensure that the IRQ handler isn't running on another CPU.
252 */
253 synchronize_irq(port->irq);
254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 /*
Russell Kingee31b332005-11-13 15:28:51 +0000257 * kill off our tasklet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 */
Russell Kingee31b332005-11-13 15:28:51 +0000259 tasklet_kill(&info->tlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /*
262 * Free the transmit buffer page.
263 */
264 if (info->xmit.buf) {
265 free_page((unsigned long)info->xmit.buf);
266 info->xmit.buf = NULL;
267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270/**
271 * uart_update_timeout - update per-port FIFO timeout.
272 * @port: uart_port structure describing the port
273 * @cflag: termios cflag value
274 * @baud: speed of the port
275 *
276 * Set the port FIFO timeout value. The @cflag value should
277 * reflect the actual hardware settings.
278 */
279void
280uart_update_timeout(struct uart_port *port, unsigned int cflag,
281 unsigned int baud)
282{
283 unsigned int bits;
284
285 /* byte size and parity */
286 switch (cflag & CSIZE) {
287 case CS5:
288 bits = 7;
289 break;
290 case CS6:
291 bits = 8;
292 break;
293 case CS7:
294 bits = 9;
295 break;
296 default:
297 bits = 10;
Alan Coxa46c9992008-02-08 04:18:53 -0800298 break; /* CS8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 if (cflag & CSTOPB)
302 bits++;
303 if (cflag & PARENB)
304 bits++;
305
306 /*
307 * The total number of bits to be transmitted in the fifo.
308 */
309 bits = bits * port->fifosize;
310
311 /*
312 * Figure the timeout to send the above number of bits.
313 * Add .02 seconds of slop
314 */
315 port->timeout = (HZ * bits) / baud + HZ/50;
316}
317
318EXPORT_SYMBOL(uart_update_timeout);
319
320/**
321 * uart_get_baud_rate - return baud rate for a particular port
322 * @port: uart_port structure describing the port in question.
323 * @termios: desired termios settings.
324 * @old: old termios (or NULL)
325 * @min: minimum acceptable baud rate
326 * @max: maximum acceptable baud rate
327 *
328 * Decode the termios structure into a numeric baud rate,
329 * taking account of the magic 38400 baud rate (with spd_*
330 * flags), and mapping the %B0 rate to 9600 baud.
331 *
332 * If the new baud rate is invalid, try the old termios setting.
333 * If it's still invalid, we try 9600 baud.
334 *
335 * Update the @termios structure to reflect the baud rate
Alan Coxeb424fd2008-04-28 02:14:07 -0700336 * we're actually going to be using. Don't do this for the case
337 * where B0 is requested ("hang up").
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
339unsigned int
Alan Cox606d0992006-12-08 02:38:45 -0800340uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
341 struct ktermios *old, unsigned int min, unsigned int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 unsigned int try, baud, altbaud = 38400;
Alan Coxeb424fd2008-04-28 02:14:07 -0700344 int hung_up = 0;
Russell King0077d452006-01-21 23:03:28 +0000345 upf_t flags = port->flags & UPF_SPD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if (flags == UPF_SPD_HI)
348 altbaud = 57600;
349 if (flags == UPF_SPD_VHI)
350 altbaud = 115200;
351 if (flags == UPF_SPD_SHI)
352 altbaud = 230400;
353 if (flags == UPF_SPD_WARP)
354 altbaud = 460800;
355
356 for (try = 0; try < 2; try++) {
357 baud = tty_termios_baud_rate(termios);
358
359 /*
360 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
361 * Die! Die! Die!
362 */
363 if (baud == 38400)
364 baud = altbaud;
365
366 /*
367 * Special case: B0 rate.
368 */
Alan Coxeb424fd2008-04-28 02:14:07 -0700369 if (baud == 0) {
370 hung_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 baud = 9600;
Alan Coxeb424fd2008-04-28 02:14:07 -0700372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 if (baud >= min && baud <= max)
375 return baud;
376
377 /*
378 * Oops, the quotient was zero. Try again with
379 * the old baud rate if possible.
380 */
381 termios->c_cflag &= ~CBAUD;
382 if (old) {
Alan Cox6d4d67b2008-02-04 22:27:53 -0800383 baud = tty_termios_baud_rate(old);
Alan Coxeb424fd2008-04-28 02:14:07 -0700384 if (!hung_up)
385 tty_termios_encode_baud_rate(termios,
386 baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 old = NULL;
388 continue;
389 }
390
391 /*
392 * As a last resort, if the quotient is zero,
393 * default to 9600 bps
394 */
Alan Coxeb424fd2008-04-28 02:14:07 -0700395 if (!hung_up)
396 tty_termios_encode_baud_rate(termios, 9600, 9600);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
399 return 0;
400}
401
402EXPORT_SYMBOL(uart_get_baud_rate);
403
404/**
405 * uart_get_divisor - return uart clock divisor
406 * @port: uart_port structure describing the port.
407 * @baud: desired baud rate
408 *
409 * Calculate the uart clock divisor for the port.
410 */
411unsigned int
412uart_get_divisor(struct uart_port *port, unsigned int baud)
413{
414 unsigned int quot;
415
416 /*
417 * Old custom speed handling.
418 */
419 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
420 quot = port->custom_divisor;
421 else
422 quot = (port->uartclk + (8 * baud)) / (16 * baud);
423
424 return quot;
425}
426
427EXPORT_SYMBOL(uart_get_divisor);
428
Alan Cox23d22ce2008-04-30 00:54:11 -0700429/* FIXME: Consistent locking policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430static void
Alan Cox606d0992006-12-08 02:38:45 -0800431uart_change_speed(struct uart_state *state, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Alan Coxf7519282009-01-02 13:49:21 +0000433 struct tty_struct *tty = state->info.port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct uart_port *port = state->port;
Alan Cox606d0992006-12-08 02:38:45 -0800435 struct ktermios *termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 /*
438 * If we have no tty, termios, or the port does not exist,
439 * then we can't set the parameters for this port.
440 */
441 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
442 return;
443
444 termios = tty->termios;
445
446 /*
447 * Set flags based on termios cflag
448 */
449 if (termios->c_cflag & CRTSCTS)
Alan Coxf7519282009-01-02 13:49:21 +0000450 state->info.flags |= UIF_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 else
Alan Coxf7519282009-01-02 13:49:21 +0000452 state->info.flags &= ~UIF_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 if (termios->c_cflag & CLOCAL)
Alan Coxf7519282009-01-02 13:49:21 +0000455 state->info.flags &= ~UIF_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 else
Alan Coxf7519282009-01-02 13:49:21 +0000457 state->info.flags |= UIF_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 port->ops->set_termios(port, termios, old_termios);
460}
461
Alan Cox23d22ce2008-04-30 00:54:11 -0700462static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463__uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
464{
465 unsigned long flags;
Alan Cox23d22ce2008-04-30 00:54:11 -0700466 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 if (!circ->buf)
Alan Cox23d22ce2008-04-30 00:54:11 -0700469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 spin_lock_irqsave(&port->lock, flags);
472 if (uart_circ_chars_free(circ) != 0) {
473 circ->buf[circ->head] = c;
474 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
Alan Cox23d22ce2008-04-30 00:54:11 -0700475 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 spin_unlock_irqrestore(&port->lock, flags);
Alan Cox23d22ce2008-04-30 00:54:11 -0700478 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Alan Cox23d22ce2008-04-30 00:54:11 -0700481static int uart_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 struct uart_state *state = tty->driver_data;
484
Alan Coxf7519282009-01-02 13:49:21 +0000485 return __uart_put_char(state->port, &state->info.xmit, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488static void uart_flush_chars(struct tty_struct *tty)
489{
490 uart_start(tty);
491}
492
493static int
Pavel Machekd5f735e2006-03-07 21:55:20 -0800494uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 struct uart_state *state = tty->driver_data;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800497 struct uart_port *port;
498 struct circ_buf *circ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 unsigned long flags;
500 int c, ret = 0;
501
Pavel Machekd5f735e2006-03-07 21:55:20 -0800502 /*
503 * This means you called this function _after_ the port was
504 * closed. No cookie for you.
505 */
Alan Coxf7519282009-01-02 13:49:21 +0000506 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800507 WARN_ON(1);
508 return -EL3HLT;
509 }
510
511 port = state->port;
Alan Coxf7519282009-01-02 13:49:21 +0000512 circ = &state->info.xmit;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (!circ->buf)
515 return 0;
516
517 spin_lock_irqsave(&port->lock, flags);
518 while (1) {
519 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
520 if (count < c)
521 c = count;
522 if (c <= 0)
523 break;
524 memcpy(circ->buf + circ->head, buf, c);
525 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
526 buf += c;
527 count -= c;
528 ret += c;
529 }
530 spin_unlock_irqrestore(&port->lock, flags);
531
532 uart_start(tty);
533 return ret;
534}
535
536static int uart_write_room(struct tty_struct *tty)
537{
538 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700539 unsigned long flags;
540 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Alan Coxf34d7a52008-04-30 00:54:13 -0700542 spin_lock_irqsave(&state->port->lock, flags);
Alan Coxf7519282009-01-02 13:49:21 +0000543 ret = uart_circ_chars_free(&state->info.xmit);
Alan Coxf34d7a52008-04-30 00:54:13 -0700544 spin_unlock_irqrestore(&state->port->lock, flags);
545 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
548static int uart_chars_in_buffer(struct tty_struct *tty)
549{
550 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700551 unsigned long flags;
552 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Alan Coxf34d7a52008-04-30 00:54:13 -0700554 spin_lock_irqsave(&state->port->lock, flags);
Alan Coxf7519282009-01-02 13:49:21 +0000555 ret = uart_circ_chars_pending(&state->info.xmit);
Alan Coxf34d7a52008-04-30 00:54:13 -0700556 spin_unlock_irqrestore(&state->port->lock, flags);
557 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560static void uart_flush_buffer(struct tty_struct *tty)
561{
562 struct uart_state *state = tty->driver_data;
Tetsuo Handa55d7b682008-05-06 20:42:27 -0700563 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 unsigned long flags;
565
Pavel Machekd5f735e2006-03-07 21:55:20 -0800566 /*
567 * This means you called this function _after_ the port was
568 * closed. No cookie for you.
569 */
Alan Coxf7519282009-01-02 13:49:21 +0000570 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800571 WARN_ON(1);
572 return;
573 }
574
Tetsuo Handa55d7b682008-05-06 20:42:27 -0700575 port = state->port;
Jiri Slabyeb3a1e12007-05-06 14:48:52 -0700576 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 spin_lock_irqsave(&port->lock, flags);
Alan Coxf7519282009-01-02 13:49:21 +0000579 uart_circ_clear(&state->info.xmit);
Haavard Skinnemoen6bb0e3a2008-07-16 21:52:36 +0100580 if (port->ops->flush_buffer)
581 port->ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 spin_unlock_irqrestore(&port->lock, flags);
583 tty_wakeup(tty);
584}
585
586/*
587 * This function is used to send a high-priority XON/XOFF character to
588 * the device
589 */
590static void uart_send_xchar(struct tty_struct *tty, char ch)
591{
592 struct uart_state *state = tty->driver_data;
593 struct uart_port *port = state->port;
594 unsigned long flags;
595
596 if (port->ops->send_xchar)
597 port->ops->send_xchar(port, ch);
598 else {
599 port->x_char = ch;
600 if (ch) {
601 spin_lock_irqsave(&port->lock, flags);
Russell Kingb129a8c2005-08-31 10:12:14 +0100602 port->ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 spin_unlock_irqrestore(&port->lock, flags);
604 }
605 }
606}
607
608static void uart_throttle(struct tty_struct *tty)
609{
610 struct uart_state *state = tty->driver_data;
611
612 if (I_IXOFF(tty))
613 uart_send_xchar(tty, STOP_CHAR(tty));
614
615 if (tty->termios->c_cflag & CRTSCTS)
616 uart_clear_mctrl(state->port, TIOCM_RTS);
617}
618
619static void uart_unthrottle(struct tty_struct *tty)
620{
621 struct uart_state *state = tty->driver_data;
622 struct uart_port *port = state->port;
623
624 if (I_IXOFF(tty)) {
625 if (port->x_char)
626 port->x_char = 0;
627 else
628 uart_send_xchar(tty, START_CHAR(tty));
629 }
630
631 if (tty->termios->c_cflag & CRTSCTS)
632 uart_set_mctrl(port, TIOCM_RTS);
633}
634
635static int uart_get_info(struct uart_state *state,
636 struct serial_struct __user *retinfo)
637{
638 struct uart_port *port = state->port;
639 struct serial_struct tmp;
640
641 memset(&tmp, 0, sizeof(tmp));
Alan Coxf34d7a52008-04-30 00:54:13 -0700642
643 /* Ensure the state we copy is consistent and no hardware changes
644 occur as we go */
645 mutex_lock(&state->mutex);
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 tmp.type = port->type;
648 tmp.line = port->line;
649 tmp.port = port->iobase;
650 if (HIGH_BITS_OFFSET)
651 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
652 tmp.irq = port->irq;
653 tmp.flags = port->flags;
654 tmp.xmit_fifo_size = port->fifosize;
655 tmp.baud_base = port->uartclk / 16;
656 tmp.close_delay = state->close_delay / 10;
657 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
658 ASYNC_CLOSING_WAIT_NONE :
Alan Coxa46c9992008-02-08 04:18:53 -0800659 state->closing_wait / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 tmp.custom_divisor = port->custom_divisor;
661 tmp.hub6 = port->hub6;
662 tmp.io_type = port->iotype;
663 tmp.iomem_reg_shift = port->regshift;
Josh Boyer4f640ef2007-07-23 18:43:44 -0700664 tmp.iomem_base = (void *)(unsigned long)port->mapbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Alan Coxf34d7a52008-04-30 00:54:13 -0700666 mutex_unlock(&state->mutex);
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
669 return -EFAULT;
670 return 0;
671}
672
673static int uart_set_info(struct uart_state *state,
674 struct serial_struct __user *newinfo)
675{
676 struct serial_struct new_serial;
677 struct uart_port *port = state->port;
678 unsigned long new_port;
Russell King0077d452006-01-21 23:03:28 +0000679 unsigned int change_irq, change_port, closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 unsigned int old_custom_divisor, close_delay;
Russell King0077d452006-01-21 23:03:28 +0000681 upf_t old_flags, new_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 int retval = 0;
683
684 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
685 return -EFAULT;
686
687 new_port = new_serial.port;
688 if (HIGH_BITS_OFFSET)
689 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
690
691 new_serial.irq = irq_canonicalize(new_serial.irq);
692 close_delay = new_serial.close_delay * 10;
693 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
694 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
695
696 /*
697 * This semaphore protects state->count. It is also
698 * very useful to prevent opens. Also, take the
699 * port configuration semaphore to make sure that a
700 * module insertion/removal doesn't change anything
701 * under us.
702 */
Ingo Molnare2862f62006-01-13 21:37:07 +0000703 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
David Gibsonabb4a232007-05-06 14:48:49 -0700705 change_irq = !(port->flags & UPF_FIXED_PORT)
706 && new_serial.irq != port->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 /*
709 * Since changing the 'type' of the port changes its resource
710 * allocations, we should treat type changes the same as
711 * IO port changes.
712 */
David Gibsonabb4a232007-05-06 14:48:49 -0700713 change_port = !(port->flags & UPF_FIXED_PORT)
714 && (new_port != port->iobase ||
715 (unsigned long)new_serial.iomem_base != port->mapbase ||
716 new_serial.hub6 != port->hub6 ||
717 new_serial.io_type != port->iotype ||
718 new_serial.iomem_reg_shift != port->regshift ||
719 new_serial.type != port->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 old_flags = port->flags;
Russell King0077d452006-01-21 23:03:28 +0000722 new_flags = new_serial.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 old_custom_divisor = port->custom_divisor;
724
725 if (!capable(CAP_SYS_ADMIN)) {
726 retval = -EPERM;
727 if (change_irq || change_port ||
728 (new_serial.baud_base != port->uartclk / 16) ||
729 (close_delay != state->close_delay) ||
730 (closing_wait != state->closing_wait) ||
Russell King947deee2006-07-02 20:45:51 +0100731 (new_serial.xmit_fifo_size &&
732 new_serial.xmit_fifo_size != port->fifosize) ||
Russell King0077d452006-01-21 23:03:28 +0000733 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 goto exit;
735 port->flags = ((port->flags & ~UPF_USR_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000736 (new_flags & UPF_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 port->custom_divisor = new_serial.custom_divisor;
738 goto check_and_exit;
739 }
740
741 /*
742 * Ask the low level driver to verify the settings.
743 */
744 if (port->ops->verify_port)
745 retval = port->ops->verify_port(port, &new_serial);
746
Yinghai Lua62c4132008-08-19 20:49:55 -0700747 if ((new_serial.irq >= nr_irqs) || (new_serial.irq < 0) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 (new_serial.baud_base < 9600))
749 retval = -EINVAL;
750
751 if (retval)
752 goto exit;
753
754 if (change_port || change_irq) {
755 retval = -EBUSY;
756
757 /*
758 * Make sure that we are the sole user of this port.
759 */
760 if (uart_users(state) > 1)
761 goto exit;
762
763 /*
764 * We need to shutdown the serial port at the old
765 * port/type/irq combination.
766 */
767 uart_shutdown(state);
768 }
769
770 if (change_port) {
771 unsigned long old_iobase, old_mapbase;
772 unsigned int old_type, old_iotype, old_hub6, old_shift;
773
774 old_iobase = port->iobase;
775 old_mapbase = port->mapbase;
776 old_type = port->type;
777 old_hub6 = port->hub6;
778 old_iotype = port->iotype;
779 old_shift = port->regshift;
780
781 /*
782 * Free and release old regions
783 */
784 if (old_type != PORT_UNKNOWN)
785 port->ops->release_port(port);
786
787 port->iobase = new_port;
788 port->type = new_serial.type;
789 port->hub6 = new_serial.hub6;
790 port->iotype = new_serial.io_type;
791 port->regshift = new_serial.iomem_reg_shift;
792 port->mapbase = (unsigned long)new_serial.iomem_base;
793
794 /*
795 * Claim and map the new regions
796 */
797 if (port->type != PORT_UNKNOWN) {
798 retval = port->ops->request_port(port);
799 } else {
800 /* Always success - Jean II */
801 retval = 0;
802 }
803
804 /*
805 * If we fail to request resources for the
806 * new port, try to restore the old settings.
807 */
808 if (retval && old_type != PORT_UNKNOWN) {
809 port->iobase = old_iobase;
810 port->type = old_type;
811 port->hub6 = old_hub6;
812 port->iotype = old_iotype;
813 port->regshift = old_shift;
814 port->mapbase = old_mapbase;
815 retval = port->ops->request_port(port);
816 /*
817 * If we failed to restore the old settings,
818 * we fail like this.
819 */
820 if (retval)
821 port->type = PORT_UNKNOWN;
822
823 /*
824 * We failed anyway.
825 */
826 retval = -EBUSY;
Alan Coxa46c9992008-02-08 04:18:53 -0800827 /* Added to return the correct error -Ram Gupta */
828 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830 }
831
David Gibsonabb4a232007-05-06 14:48:49 -0700832 if (change_irq)
833 port->irq = new_serial.irq;
834 if (!(port->flags & UPF_FIXED_PORT))
835 port->uartclk = new_serial.baud_base * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000837 (new_flags & UPF_CHANGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 port->custom_divisor = new_serial.custom_divisor;
839 state->close_delay = close_delay;
840 state->closing_wait = closing_wait;
Russell King947deee2006-07-02 20:45:51 +0100841 if (new_serial.xmit_fifo_size)
842 port->fifosize = new_serial.xmit_fifo_size;
Alan Coxf7519282009-01-02 13:49:21 +0000843 if (state->info.port.tty)
844 state->info.port.tty->low_latency =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
846
847 check_and_exit:
848 retval = 0;
849 if (port->type == PORT_UNKNOWN)
850 goto exit;
Alan Coxf7519282009-01-02 13:49:21 +0000851 if (state->info.flags & UIF_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
853 old_custom_divisor != port->custom_divisor) {
854 /*
855 * If they're setting up a custom divisor or speed,
856 * instead of clearing it, then bitch about it. No
857 * need to rate-limit; it's CAP_SYS_ADMIN only.
858 */
859 if (port->flags & UPF_SPD_MASK) {
860 char buf[64];
861 printk(KERN_NOTICE
862 "%s sets custom speed on %s. This "
863 "is deprecated.\n", current->comm,
Alan Coxf7519282009-01-02 13:49:21 +0000864 tty_name(state->info.port.tty, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866 uart_change_speed(state, NULL);
867 }
868 } else
869 retval = uart_startup(state, 1);
870 exit:
Ingo Molnare2862f62006-01-13 21:37:07 +0000871 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return retval;
873}
874
875
876/*
877 * uart_get_lsr_info - get line status register info.
878 * Note: uart_ioctl protects us against hangups.
879 */
880static int uart_get_lsr_info(struct uart_state *state,
881 unsigned int __user *value)
882{
883 struct uart_port *port = state->port;
884 unsigned int result;
885
886 result = port->ops->tx_empty(port);
887
888 /*
889 * If we're about to load something into the transmit
890 * register, we'll pretend the transmitter isn't empty to
891 * avoid a race condition (depending on when the transmit
892 * interrupt happens).
893 */
894 if (port->x_char ||
Alan Coxf7519282009-01-02 13:49:21 +0000895 ((uart_circ_chars_pending(&state->info.xmit) > 0) &&
896 !state->info.port.tty->stopped && !state->info.port.tty->hw_stopped))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 result &= ~TIOCSER_TEMT;
Alan Coxa46c9992008-02-08 04:18:53 -0800898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return put_user(result, value);
900}
901
902static int uart_tiocmget(struct tty_struct *tty, struct file *file)
903{
904 struct uart_state *state = tty->driver_data;
905 struct uart_port *port = state->port;
906 int result = -EIO;
907
Ingo Molnare2862f62006-01-13 21:37:07 +0000908 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if ((!file || !tty_hung_up_p(file)) &&
910 !(tty->flags & (1 << TTY_IO_ERROR))) {
911 result = port->mctrl;
Russell Kingc5f46442005-06-29 09:42:38 +0100912
913 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 result |= port->ops->get_mctrl(port);
Russell Kingc5f46442005-06-29 09:42:38 +0100915 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000917 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 return result;
920}
921
922static int
923uart_tiocmset(struct tty_struct *tty, struct file *file,
924 unsigned int set, unsigned int clear)
925{
926 struct uart_state *state = tty->driver_data;
927 struct uart_port *port = state->port;
928 int ret = -EIO;
929
Ingo Molnare2862f62006-01-13 21:37:07 +0000930 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if ((!file || !tty_hung_up_p(file)) &&
932 !(tty->flags & (1 << TTY_IO_ERROR))) {
933 uart_update_mctrl(port, set, clear);
934 ret = 0;
935 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000936 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return ret;
938}
939
Alan Cox9e989662008-07-22 11:18:03 +0100940static int uart_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 struct uart_state *state = tty->driver_data;
943 struct uart_port *port = state->port;
944
Ingo Molnare2862f62006-01-13 21:37:07 +0000945 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 if (port->type != PORT_UNKNOWN)
948 port->ops->break_ctl(port, break_state);
949
Ingo Molnare2862f62006-01-13 21:37:07 +0000950 mutex_unlock(&state->mutex);
Alan Cox9e989662008-07-22 11:18:03 +0100951 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
954static int uart_do_autoconfig(struct uart_state *state)
955{
956 struct uart_port *port = state->port;
957 int flags, ret;
958
959 if (!capable(CAP_SYS_ADMIN))
960 return -EPERM;
961
962 /*
963 * Take the per-port semaphore. This prevents count from
964 * changing, and hence any extra opens of the port while
965 * we're auto-configuring.
966 */
Ingo Molnare2862f62006-01-13 21:37:07 +0000967 if (mutex_lock_interruptible(&state->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return -ERESTARTSYS;
969
970 ret = -EBUSY;
971 if (uart_users(state) == 1) {
972 uart_shutdown(state);
973
974 /*
975 * If we already have a port type configured,
976 * we must release its resources.
977 */
978 if (port->type != PORT_UNKNOWN)
979 port->ops->release_port(port);
980
981 flags = UART_CONFIG_TYPE;
982 if (port->flags & UPF_AUTO_IRQ)
983 flags |= UART_CONFIG_IRQ;
984
985 /*
986 * This will claim the ports resources if
987 * a port is found.
988 */
989 port->ops->config_port(port, flags);
990
991 ret = uart_startup(state, 1);
992 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000993 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return ret;
995}
996
997/*
998 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
999 * - mask passed in arg for lines of interest
1000 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1001 * Caller should use TIOCGICOUNT to see which one it was
1002 */
1003static int
1004uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1005{
1006 struct uart_port *port = state->port;
1007 DECLARE_WAITQUEUE(wait, current);
1008 struct uart_icount cprev, cnow;
1009 int ret;
1010
1011 /*
1012 * note the counters on entry
1013 */
1014 spin_lock_irq(&port->lock);
1015 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
1016
1017 /*
1018 * Force modem status interrupts on
1019 */
1020 port->ops->enable_ms(port);
1021 spin_unlock_irq(&port->lock);
1022
Alan Coxf7519282009-01-02 13:49:21 +00001023 add_wait_queue(&state->info.delta_msr_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 for (;;) {
1025 spin_lock_irq(&port->lock);
1026 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1027 spin_unlock_irq(&port->lock);
1028
1029 set_current_state(TASK_INTERRUPTIBLE);
1030
1031 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1032 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1033 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1034 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Alan Coxa46c9992008-02-08 04:18:53 -08001035 ret = 0;
1036 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
1039 schedule();
1040
1041 /* see if a signal did it */
1042 if (signal_pending(current)) {
1043 ret = -ERESTARTSYS;
1044 break;
1045 }
1046
1047 cprev = cnow;
1048 }
1049
1050 current->state = TASK_RUNNING;
Alan Coxf7519282009-01-02 13:49:21 +00001051 remove_wait_queue(&state->info.delta_msr_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 return ret;
1054}
1055
1056/*
1057 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1058 * Return: write counters to the user passed counter struct
1059 * NB: both 1->0 and 0->1 transitions are counted except for
1060 * RI where only 0->1 is counted.
1061 */
1062static int uart_get_count(struct uart_state *state,
1063 struct serial_icounter_struct __user *icnt)
1064{
1065 struct serial_icounter_struct icount;
1066 struct uart_icount cnow;
1067 struct uart_port *port = state->port;
1068
1069 spin_lock_irq(&port->lock);
1070 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1071 spin_unlock_irq(&port->lock);
1072
1073 icount.cts = cnow.cts;
1074 icount.dsr = cnow.dsr;
1075 icount.rng = cnow.rng;
1076 icount.dcd = cnow.dcd;
1077 icount.rx = cnow.rx;
1078 icount.tx = cnow.tx;
1079 icount.frame = cnow.frame;
1080 icount.overrun = cnow.overrun;
1081 icount.parity = cnow.parity;
1082 icount.brk = cnow.brk;
1083 icount.buf_overrun = cnow.buf_overrun;
1084
1085 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1086}
1087
1088/*
Alan Coxe5238442008-04-30 00:53:28 -07001089 * Called via sys_ioctl. We can use spin_lock_irq() here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 */
1091static int
1092uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1093 unsigned long arg)
1094{
1095 struct uart_state *state = tty->driver_data;
1096 void __user *uarg = (void __user *)arg;
1097 int ret = -ENOIOCTLCMD;
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 /*
1101 * These ioctls don't rely on the hardware to be present.
1102 */
1103 switch (cmd) {
1104 case TIOCGSERIAL:
1105 ret = uart_get_info(state, uarg);
1106 break;
1107
1108 case TIOCSSERIAL:
1109 ret = uart_set_info(state, uarg);
1110 break;
1111
1112 case TIOCSERCONFIG:
1113 ret = uart_do_autoconfig(state);
1114 break;
1115
1116 case TIOCSERGWILD: /* obsolete */
1117 case TIOCSERSWILD: /* obsolete */
1118 ret = 0;
1119 break;
1120 }
1121
1122 if (ret != -ENOIOCTLCMD)
1123 goto out;
1124
1125 if (tty->flags & (1 << TTY_IO_ERROR)) {
1126 ret = -EIO;
1127 goto out;
1128 }
1129
1130 /*
1131 * The following should only be used when hardware is present.
1132 */
1133 switch (cmd) {
1134 case TIOCMIWAIT:
1135 ret = uart_wait_modem_status(state, arg);
1136 break;
1137
1138 case TIOCGICOUNT:
1139 ret = uart_get_count(state, uarg);
1140 break;
1141 }
1142
1143 if (ret != -ENOIOCTLCMD)
1144 goto out;
1145
Ingo Molnare2862f62006-01-13 21:37:07 +00001146 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 if (tty_hung_up_p(filp)) {
1149 ret = -EIO;
1150 goto out_up;
1151 }
1152
1153 /*
1154 * All these rely on hardware being present and need to be
1155 * protected against the tty being hung up.
1156 */
1157 switch (cmd) {
1158 case TIOCSERGETLSR: /* Get line status register */
1159 ret = uart_get_lsr_info(state, uarg);
1160 break;
1161
1162 default: {
1163 struct uart_port *port = state->port;
1164 if (port->ops->ioctl)
1165 ret = port->ops->ioctl(port, cmd, arg);
1166 break;
1167 }
1168 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001169out_up:
Ingo Molnare2862f62006-01-13 21:37:07 +00001170 mutex_unlock(&state->mutex);
Alan Coxf34d7a52008-04-30 00:54:13 -07001171out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return ret;
1173}
1174
Linus Torvaldsedeb2802008-06-04 10:35:03 -07001175static void uart_set_ldisc(struct tty_struct *tty)
Alan Cox64e91592008-06-03 15:18:54 +01001176{
1177 struct uart_state *state = tty->driver_data;
1178 struct uart_port *port = state->port;
1179
1180 if (port->ops->set_ldisc)
1181 port->ops->set_ldisc(port);
1182}
1183
Alan Coxa46c9992008-02-08 04:18:53 -08001184static void uart_set_termios(struct tty_struct *tty,
1185 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
1187 struct uart_state *state = tty->driver_data;
1188 unsigned long flags;
1189 unsigned int cflag = tty->termios->c_cflag;
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 /*
1193 * These are the bits that are used to setup various
David Woodhouse20620d62007-08-22 14:01:11 -07001194 * flags in the low level driver. We can ignore the Bfoo
1195 * bits in c_cflag; c_[io]speed will always be set
1196 * appropriately by set_termios() in tty_ioctl.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 */
1198#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if ((cflag ^ old_termios->c_cflag) == 0 &&
David Woodhouse20620d62007-08-22 14:01:11 -07001200 tty->termios->c_ospeed == old_termios->c_ospeed &&
1201 tty->termios->c_ispeed == old_termios->c_ispeed &&
Alan Coxe5238442008-04-30 00:53:28 -07001202 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return;
Alan Coxe5238442008-04-30 00:53:28 -07001204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 uart_change_speed(state, old_termios);
1207
1208 /* Handle transition to B0 status */
1209 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1210 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1211
1212 /* Handle transition away from B0 status */
1213 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1214 unsigned int mask = TIOCM_DTR;
1215 if (!(cflag & CRTSCTS) ||
1216 !test_bit(TTY_THROTTLED, &tty->flags))
1217 mask |= TIOCM_RTS;
1218 uart_set_mctrl(state->port, mask);
1219 }
1220
1221 /* Handle turning off CRTSCTS */
1222 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1223 spin_lock_irqsave(&state->port->lock, flags);
1224 tty->hw_stopped = 0;
1225 __uart_start(tty);
1226 spin_unlock_irqrestore(&state->port->lock, flags);
1227 }
1228
Russell King0dd7a1a2005-06-29 18:40:53 +01001229 /* Handle turning on CRTSCTS */
1230 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1231 spin_lock_irqsave(&state->port->lock, flags);
1232 if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
1233 tty->hw_stopped = 1;
Russell Kingb129a8c2005-08-31 10:12:14 +01001234 state->port->ops->stop_tx(state->port);
Russell King0dd7a1a2005-06-29 18:40:53 +01001235 }
1236 spin_unlock_irqrestore(&state->port->lock, flags);
1237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238#if 0
1239 /*
1240 * No need to wake up processes in open wait, since they
1241 * sample the CLOCAL flag once, and don't recheck it.
1242 * XXX It's not clear whether the current behavior is correct
1243 * or not. Hence, this may change.....
1244 */
1245 if (!(old_termios->c_cflag & CLOCAL) &&
1246 (tty->termios->c_cflag & CLOCAL))
Alan Coxf7519282009-01-02 13:49:21 +00001247 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248#endif
1249}
1250
1251/*
1252 * In 2.4.5, calls to this will be serialized via the BKL in
1253 * linux/drivers/char/tty_io.c:tty_release()
1254 * linux/drivers/char/tty_io.c:do_tty_handup()
1255 */
1256static void uart_close(struct tty_struct *tty, struct file *filp)
1257{
1258 struct uart_state *state = tty->driver_data;
1259 struct uart_port *port;
Alan Coxa46c9992008-02-08 04:18:53 -08001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 BUG_ON(!kernel_locked());
1262
1263 if (!state || !state->port)
1264 return;
1265
1266 port = state->port;
1267
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001268 pr_debug("uart_close(%d) called\n", port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Ingo Molnare2862f62006-01-13 21:37:07 +00001270 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 if (tty_hung_up_p(filp))
1273 goto done;
1274
1275 if ((tty->count == 1) && (state->count != 1)) {
1276 /*
1277 * Uh, oh. tty->count is 1, which means that the tty
1278 * structure will be freed. state->count should always
1279 * be one in these conditions. If it's greater than
1280 * one, we've got real problems, since it means the
1281 * serial port won't be shutdown.
1282 */
1283 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1284 "state->count is %d\n", state->count);
1285 state->count = 1;
1286 }
1287 if (--state->count < 0) {
1288 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1289 tty->name, state->count);
1290 state->count = 0;
1291 }
1292 if (state->count)
1293 goto done;
1294
1295 /*
1296 * Now we wait for the transmit buffer to clear; and we notify
1297 * the line discipline to only process XON/XOFF characters by
1298 * setting tty->closing.
1299 */
1300 tty->closing = 1;
1301
1302 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1303 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1304
1305 /*
1306 * At this point, we stop accepting input. To do this, we
1307 * disable the receive line status interrupts.
1308 */
Alan Coxf7519282009-01-02 13:49:21 +00001309 if (state->info.flags & UIF_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 unsigned long flags;
1311 spin_lock_irqsave(&port->lock, flags);
1312 port->ops->stop_rx(port);
1313 spin_unlock_irqrestore(&port->lock, flags);
1314 /*
1315 * Before we drop DTR, make sure the UART transmitter
1316 * has completely drained; this is especially
1317 * important if there is a transmit FIFO!
1318 */
1319 uart_wait_until_sent(tty, port->timeout);
1320 }
1321
1322 uart_shutdown(state);
1323 uart_flush_buffer(tty);
1324
Alan Coxa46c9992008-02-08 04:18:53 -08001325 tty_ldisc_flush(tty);
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 tty->closing = 0;
Alan Coxf7519282009-01-02 13:49:21 +00001328 state->info.port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Alan Coxf7519282009-01-02 13:49:21 +00001330 if (state->info.port.blocked_open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (state->close_delay)
1332 msleep_interruptible(state->close_delay);
1333 } else if (!uart_console(port)) {
1334 uart_change_pm(state, 3);
1335 }
1336
1337 /*
1338 * Wake up anyone trying to open this port.
1339 */
Alan Coxf7519282009-01-02 13:49:21 +00001340 state->info.flags &= ~UIF_NORMAL_ACTIVE;
1341 wake_up_interruptible(&state->info.port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 done:
Ingo Molnare2862f62006-01-13 21:37:07 +00001344 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
1347static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1348{
1349 struct uart_state *state = tty->driver_data;
1350 struct uart_port *port = state->port;
1351 unsigned long char_time, expire;
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1354 return;
1355
Alan Coxf34d7a52008-04-30 00:54:13 -07001356 lock_kernel();
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 /*
1359 * Set the check interval to be 1/5 of the estimated time to
1360 * send a single character, and make it at least 1. The check
1361 * interval should also be less than the timeout.
1362 *
1363 * Note: we have to use pretty tight timings here to satisfy
1364 * the NIST-PCTS.
1365 */
1366 char_time = (port->timeout - HZ/50) / port->fifosize;
1367 char_time = char_time / 5;
1368 if (char_time == 0)
1369 char_time = 1;
1370 if (timeout && timeout < char_time)
1371 char_time = timeout;
1372
1373 /*
1374 * If the transmitter hasn't cleared in twice the approximate
1375 * amount of time to send the entire FIFO, it probably won't
1376 * ever clear. This assumes the UART isn't doing flow
1377 * control, which is currently the case. Hence, if it ever
1378 * takes longer than port->timeout, this is probably due to a
1379 * UART bug of some kind. So, we clamp the timeout parameter at
1380 * 2*port->timeout.
1381 */
1382 if (timeout == 0 || timeout > 2 * port->timeout)
1383 timeout = 2 * port->timeout;
1384
1385 expire = jiffies + timeout;
1386
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001387 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
Alan Coxa46c9992008-02-08 04:18:53 -08001388 port->line, jiffies, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 /*
1391 * Check whether the transmitter is empty every 'char_time'.
1392 * 'timeout' / 'expire' give us the maximum amount of time
1393 * we wait.
1394 */
1395 while (!port->ops->tx_empty(port)) {
1396 msleep_interruptible(jiffies_to_msecs(char_time));
1397 if (signal_pending(current))
1398 break;
1399 if (time_after(jiffies, expire))
1400 break;
1401 }
1402 set_current_state(TASK_RUNNING); /* might not be needed */
Alan Coxf34d7a52008-04-30 00:54:13 -07001403 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
1406/*
1407 * This is called with the BKL held in
1408 * linux/drivers/char/tty_io.c:do_tty_hangup()
1409 * We're called from the eventd thread, so we can sleep for
1410 * a _short_ time only.
1411 */
1412static void uart_hangup(struct tty_struct *tty)
1413{
1414 struct uart_state *state = tty->driver_data;
Alan Coxf7519282009-01-02 13:49:21 +00001415 struct uart_info *info = &state->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 BUG_ON(!kernel_locked());
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001418 pr_debug("uart_hangup(%d)\n", state->port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Ingo Molnare2862f62006-01-13 21:37:07 +00001420 mutex_lock(&state->mutex);
Alan Coxf7519282009-01-02 13:49:21 +00001421 if (info->flags & UIF_NORMAL_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 uart_flush_buffer(tty);
1423 uart_shutdown(state);
1424 state->count = 0;
Alan Coxf7519282009-01-02 13:49:21 +00001425 info->flags &= ~UIF_NORMAL_ACTIVE;
1426 info->port.tty = NULL;
1427 wake_up_interruptible(&info->port.open_wait);
1428 wake_up_interruptible(&info->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
Ingo Molnare2862f62006-01-13 21:37:07 +00001430 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
1433/*
1434 * Copy across the serial console cflag setting into the termios settings
1435 * for the initial open of the port. This allows continuity between the
1436 * kernel settings, and the settings init adopts when it opens the port
1437 * for the first time.
1438 */
1439static void uart_update_termios(struct uart_state *state)
1440{
Alan Coxf7519282009-01-02 13:49:21 +00001441 struct tty_struct *tty = state->info.port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 struct uart_port *port = state->port;
1443
1444 if (uart_console(port) && port->cons->cflag) {
1445 tty->termios->c_cflag = port->cons->cflag;
1446 port->cons->cflag = 0;
1447 }
1448
1449 /*
1450 * If the device failed to grab its irq resources,
1451 * or some other error occurred, don't try to talk
1452 * to the port hardware.
1453 */
1454 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1455 /*
1456 * Make termios settings take effect.
1457 */
1458 uart_change_speed(state, NULL);
1459
1460 /*
1461 * And finally enable the RTS and DTR signals.
1462 */
1463 if (tty->termios->c_cflag & CBAUD)
1464 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1465 }
1466}
1467
1468/*
1469 * Block the open until the port is ready. We must be called with
1470 * the per-port semaphore held.
1471 */
1472static int
1473uart_block_til_ready(struct file *filp, struct uart_state *state)
1474{
1475 DECLARE_WAITQUEUE(wait, current);
Alan Coxf7519282009-01-02 13:49:21 +00001476 struct uart_info *info = &state->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 struct uart_port *port = state->port;
Russell Kingc5f46442005-06-29 09:42:38 +01001478 unsigned int mctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001480 info->port.blocked_open++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 state->count--;
1482
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001483 add_wait_queue(&info->port.open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 while (1) {
1485 set_current_state(TASK_INTERRUPTIBLE);
1486
1487 /*
1488 * If we have been hung up, tell userspace/restart open.
1489 */
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001490 if (tty_hung_up_p(filp) || info->port.tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 break;
1492
1493 /*
1494 * If the port has been closed, tell userspace/restart open.
1495 */
1496 if (!(info->flags & UIF_INITIALIZED))
1497 break;
1498
1499 /*
1500 * If non-blocking mode is set, or CLOCAL mode is set,
1501 * we don't want to wait for the modem status lines to
1502 * indicate that the port is ready.
1503 *
1504 * Also, if the port is not enabled/configured, we want
1505 * to allow the open to succeed here. Note that we will
1506 * have set TTY_IO_ERROR for a non-existant port.
1507 */
1508 if ((filp->f_flags & O_NONBLOCK) ||
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001509 (info->port.tty->termios->c_cflag & CLOCAL) ||
1510 (info->port.tty->flags & (1 << TTY_IO_ERROR)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 /*
1514 * Set DTR to allow modem to know we're waiting. Do
1515 * not set RTS here - we want to make sure we catch
1516 * the data from the modem.
1517 */
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001518 if (info->port.tty->termios->c_cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 uart_set_mctrl(port, TIOCM_DTR);
1520
1521 /*
1522 * and wait for the carrier to indicate that the
1523 * modem is ready for us.
1524 */
Russell Kingc5f46442005-06-29 09:42:38 +01001525 spin_lock_irq(&port->lock);
Russell Kingf61051c2006-01-07 23:11:23 +00001526 port->ops->enable_ms(port);
Russell Kingc5f46442005-06-29 09:42:38 +01001527 mctrl = port->ops->get_mctrl(port);
1528 spin_unlock_irq(&port->lock);
1529 if (mctrl & TIOCM_CAR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 break;
1531
Ingo Molnare2862f62006-01-13 21:37:07 +00001532 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 schedule();
Ingo Molnare2862f62006-01-13 21:37:07 +00001534 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 if (signal_pending(current))
1537 break;
1538 }
1539 set_current_state(TASK_RUNNING);
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001540 remove_wait_queue(&info->port.open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 state->count++;
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001543 info->port.blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 if (signal_pending(current))
1546 return -ERESTARTSYS;
1547
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001548 if (!info->port.tty || tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return -EAGAIN;
1550
1551 return 0;
1552}
1553
1554static struct uart_state *uart_get(struct uart_driver *drv, int line)
1555{
1556 struct uart_state *state;
Russell King68ac64c2006-04-30 11:13:50 +01001557 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 state = drv->state + line;
Ingo Molnare2862f62006-01-13 21:37:07 +00001560 if (mutex_lock_interruptible(&state->mutex)) {
Russell King68ac64c2006-04-30 11:13:50 +01001561 ret = -ERESTARTSYS;
1562 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564
1565 state->count++;
Russell King68ac64c2006-04-30 11:13:50 +01001566 if (!state->port || state->port->flags & UPF_DEAD) {
1567 ret = -ENXIO;
1568 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return state;
Russell King68ac64c2006-04-30 11:13:50 +01001571
1572 err_unlock:
1573 state->count--;
1574 mutex_unlock(&state->mutex);
1575 err:
1576 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
1579/*
Denis Cheng922f9cf2008-02-08 04:22:00 -08001580 * calls to uart_open are serialised by the BKL in
1581 * fs/char_dev.c:chrdev_open()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 * Note that if this fails, then uart_close() _will_ be called.
1583 *
1584 * In time, we want to scrap the "opening nonpresent ports"
1585 * behaviour and implement an alternative way for setserial
1586 * to set base addresses/ports/types. This will allow us to
1587 * get rid of a certain amount of extra tests.
1588 */
1589static int uart_open(struct tty_struct *tty, struct file *filp)
1590{
1591 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1592 struct uart_state *state;
1593 int retval, line = tty->index;
1594
1595 BUG_ON(!kernel_locked());
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001596 pr_debug("uart_open(%d) called\n", line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 /*
1599 * tty->driver->num won't change, so we won't fail here with
1600 * tty->driver_data set to something non-NULL (and therefore
1601 * we won't get caught by uart_close()).
1602 */
1603 retval = -ENODEV;
1604 if (line >= tty->driver->num)
1605 goto fail;
1606
1607 /*
1608 * We take the semaphore inside uart_get to guarantee that we won't
1609 * be re-entered while allocating the info structure, or while we
1610 * request any IRQs that the driver may need. This also has the nice
1611 * side-effect that it delays the action of uart_hangup, so we can
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001612 * guarantee that info->port.tty will always contain something reasonable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 */
1614 state = uart_get(drv, line);
1615 if (IS_ERR(state)) {
1616 retval = PTR_ERR(state);
1617 goto fail;
1618 }
1619
1620 /*
1621 * Once we set tty->driver_data here, we are guaranteed that
1622 * uart_close() will decrement the driver module use count.
1623 * Any failures from here onwards should not touch the count.
1624 */
1625 tty->driver_data = state;
Alan Coxf7519282009-01-02 13:49:21 +00001626 state->port->info = &state->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1628 tty->alt_speed = 0;
Alan Coxf7519282009-01-02 13:49:21 +00001629 state->info.port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 /*
1632 * If the port is in the middle of closing, bail out now.
1633 */
1634 if (tty_hung_up_p(filp)) {
1635 retval = -EAGAIN;
1636 state->count--;
Ingo Molnare2862f62006-01-13 21:37:07 +00001637 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 goto fail;
1639 }
1640
1641 /*
1642 * Make sure the device is in D0 state.
1643 */
1644 if (state->count == 1)
1645 uart_change_pm(state, 0);
1646
1647 /*
1648 * Start up the serial port.
1649 */
1650 retval = uart_startup(state, 0);
1651
1652 /*
1653 * If we succeeded, wait until the port is ready.
1654 */
1655 if (retval == 0)
1656 retval = uart_block_til_ready(filp, state);
Ingo Molnare2862f62006-01-13 21:37:07 +00001657 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 /*
1660 * If this is the first open to succeed, adjust things to suit.
1661 */
Alan Coxf7519282009-01-02 13:49:21 +00001662 if (retval == 0 && !(state->info.flags & UIF_NORMAL_ACTIVE)) {
1663 state->info.flags |= UIF_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 uart_update_termios(state);
1666 }
1667
1668 fail:
1669 return retval;
1670}
1671
1672static const char *uart_type(struct uart_port *port)
1673{
1674 const char *str = NULL;
1675
1676 if (port->ops->type)
1677 str = port->ops->type(port);
1678
1679 if (!str)
1680 str = "unknown";
1681
1682 return str;
1683}
1684
1685#ifdef CONFIG_PROC_FS
1686
Alexey Dobriyand196a942009-03-31 15:19:21 -07001687static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 struct uart_state *state = drv->state + i;
George G. Davis3689a0e2007-02-14 00:33:06 -08001690 int pm_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 struct uart_port *port = state->port;
1692 char stat_buf[32];
1693 unsigned int status;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001694 int mmio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 if (!port)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001697 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001699 mmio = port->iotype >= UPIO_MEM;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001700 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 port->line, uart_type(port),
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001702 mmio ? "mmio:0x" : "port:",
Josh Boyer4f640ef2007-07-23 18:43:44 -07001703 mmio ? (unsigned long long)port->mapbase
Alan Coxa46c9992008-02-08 04:18:53 -08001704 : (unsigned long long) port->iobase,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 port->irq);
1706
1707 if (port->type == PORT_UNKNOWN) {
Alexey Dobriyand196a942009-03-31 15:19:21 -07001708 seq_putc(m, '\n');
1709 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711
Alan Coxa46c9992008-02-08 04:18:53 -08001712 if (capable(CAP_SYS_ADMIN)) {
George G. Davis3689a0e2007-02-14 00:33:06 -08001713 mutex_lock(&state->mutex);
1714 pm_state = state->pm_state;
1715 if (pm_state)
1716 uart_change_pm(state, 0);
Russell Kingc5f46442005-06-29 09:42:38 +01001717 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 status = port->ops->get_mctrl(port);
Russell Kingc5f46442005-06-29 09:42:38 +01001719 spin_unlock_irq(&port->lock);
George G. Davis3689a0e2007-02-14 00:33:06 -08001720 if (pm_state)
1721 uart_change_pm(state, pm_state);
1722 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Alexey Dobriyand196a942009-03-31 15:19:21 -07001724 seq_printf(m, " tx:%d rx:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 port->icount.tx, port->icount.rx);
1726 if (port->icount.frame)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001727 seq_printf(m, " fe:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 port->icount.frame);
1729 if (port->icount.parity)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001730 seq_printf(m, " pe:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 port->icount.parity);
1732 if (port->icount.brk)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001733 seq_printf(m, " brk:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 port->icount.brk);
1735 if (port->icount.overrun)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001736 seq_printf(m, " oe:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 port->icount.overrun);
Alan Coxa46c9992008-02-08 04:18:53 -08001738
1739#define INFOBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (port->mctrl & (bit)) \
1741 strncat(stat_buf, (str), sizeof(stat_buf) - \
1742 strlen(stat_buf) - 2)
Alan Coxa46c9992008-02-08 04:18:53 -08001743#define STATBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (status & (bit)) \
1745 strncat(stat_buf, (str), sizeof(stat_buf) - \
1746 strlen(stat_buf) - 2)
1747
1748 stat_buf[0] = '\0';
1749 stat_buf[1] = '\0';
1750 INFOBIT(TIOCM_RTS, "|RTS");
1751 STATBIT(TIOCM_CTS, "|CTS");
1752 INFOBIT(TIOCM_DTR, "|DTR");
1753 STATBIT(TIOCM_DSR, "|DSR");
1754 STATBIT(TIOCM_CAR, "|CD");
1755 STATBIT(TIOCM_RNG, "|RI");
1756 if (stat_buf[0])
1757 stat_buf[0] = ' ';
Alan Coxa46c9992008-02-08 04:18:53 -08001758
Alexey Dobriyand196a942009-03-31 15:19:21 -07001759 seq_puts(m, stat_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
Alexey Dobriyand196a942009-03-31 15:19:21 -07001761 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762#undef STATBIT
1763#undef INFOBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
Alexey Dobriyand196a942009-03-31 15:19:21 -07001766static int uart_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Alexey Dobriyan833bb302009-04-02 01:30:04 +04001768 struct tty_driver *ttydrv = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 struct uart_driver *drv = ttydrv->driver_state;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001770 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Alexey Dobriyand196a942009-03-31 15:19:21 -07001772 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 "", "", "");
Alexey Dobriyand196a942009-03-31 15:19:21 -07001774 for (i = 0; i < drv->nr; i++)
1775 uart_line_info(m, drv, i);
1776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
Alexey Dobriyand196a942009-03-31 15:19:21 -07001778
1779static int uart_proc_open(struct inode *inode, struct file *file)
1780{
1781 return single_open(file, uart_proc_show, PDE(inode)->data);
1782}
1783
1784static const struct file_operations uart_proc_fops = {
1785 .owner = THIS_MODULE,
1786 .open = uart_proc_open,
1787 .read = seq_read,
1788 .llseek = seq_lseek,
1789 .release = single_release,
1790};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791#endif
1792
Andrew Morton4a1b5502008-03-07 15:51:16 -08001793#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794/*
Russell Kingd3587882006-03-20 20:00:09 +00001795 * uart_console_write - write a console message to a serial port
1796 * @port: the port to write the message
1797 * @s: array of characters
1798 * @count: number of characters in string to write
1799 * @write: function to write character to port
1800 */
1801void uart_console_write(struct uart_port *port, const char *s,
1802 unsigned int count,
1803 void (*putchar)(struct uart_port *, int))
1804{
1805 unsigned int i;
1806
1807 for (i = 0; i < count; i++, s++) {
1808 if (*s == '\n')
1809 putchar(port, '\r');
1810 putchar(port, *s);
1811 }
1812}
1813EXPORT_SYMBOL_GPL(uart_console_write);
1814
1815/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 * Check whether an invalid uart number has been specified, and
1817 * if so, search for the first available port that does have
1818 * console support.
1819 */
1820struct uart_port * __init
1821uart_get_console(struct uart_port *ports, int nr, struct console *co)
1822{
1823 int idx = co->index;
1824
1825 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1826 ports[idx].membase == NULL))
1827 for (idx = 0; idx < nr; idx++)
1828 if (ports[idx].iobase != 0 ||
1829 ports[idx].membase != NULL)
1830 break;
1831
1832 co->index = idx;
1833
1834 return ports + idx;
1835}
1836
1837/**
1838 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1839 * @options: pointer to option string
1840 * @baud: pointer to an 'int' variable for the baud rate.
1841 * @parity: pointer to an 'int' variable for the parity.
1842 * @bits: pointer to an 'int' variable for the number of data bits.
1843 * @flow: pointer to an 'int' variable for the flow control character.
1844 *
1845 * uart_parse_options decodes a string containing the serial console
1846 * options. The format of the string is <baud><parity><bits><flow>,
1847 * eg: 115200n8r
1848 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001849void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1851{
1852 char *s = options;
1853
1854 *baud = simple_strtoul(s, NULL, 10);
1855 while (*s >= '0' && *s <= '9')
1856 s++;
1857 if (*s)
1858 *parity = *s++;
1859 if (*s)
1860 *bits = *s++ - '0';
1861 if (*s)
1862 *flow = *s;
1863}
Jason Wesself2d937f2008-04-17 20:05:37 +02001864EXPORT_SYMBOL_GPL(uart_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866struct baud_rates {
1867 unsigned int rate;
1868 unsigned int cflag;
1869};
1870
Arjan van de Vencb3592b2005-11-28 21:04:11 +00001871static const struct baud_rates baud_rates[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 { 921600, B921600 },
1873 { 460800, B460800 },
1874 { 230400, B230400 },
1875 { 115200, B115200 },
1876 { 57600, B57600 },
1877 { 38400, B38400 },
1878 { 19200, B19200 },
1879 { 9600, B9600 },
1880 { 4800, B4800 },
1881 { 2400, B2400 },
1882 { 1200, B1200 },
1883 { 0, B38400 }
1884};
1885
1886/**
1887 * uart_set_options - setup the serial console parameters
1888 * @port: pointer to the serial ports uart_port structure
1889 * @co: console pointer
1890 * @baud: baud rate
1891 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1892 * @bits: number of data bits
1893 * @flow: flow control character - 'r' (rts)
1894 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001895int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896uart_set_options(struct uart_port *port, struct console *co,
1897 int baud, int parity, int bits, int flow)
1898{
Alan Cox606d0992006-12-08 02:38:45 -08001899 struct ktermios termios;
Alan Cox149b36e2007-10-18 01:24:16 -07001900 static struct ktermios dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 int i;
1902
Russell King976ecd12005-07-03 21:05:45 +01001903 /*
1904 * Ensure that the serial console lock is initialised
1905 * early.
1906 */
1907 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07001908 lockdep_set_class(&port->lock, &port_lock_key);
Russell King976ecd12005-07-03 21:05:45 +01001909
Alan Cox606d0992006-12-08 02:38:45 -08001910 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1913
1914 /*
1915 * Construct a cflag setting.
1916 */
1917 for (i = 0; baud_rates[i].rate; i++)
1918 if (baud_rates[i].rate <= baud)
1919 break;
1920
1921 termios.c_cflag |= baud_rates[i].cflag;
1922
1923 if (bits == 7)
1924 termios.c_cflag |= CS7;
1925 else
1926 termios.c_cflag |= CS8;
1927
1928 switch (parity) {
1929 case 'o': case 'O':
1930 termios.c_cflag |= PARODD;
1931 /*fall through*/
1932 case 'e': case 'E':
1933 termios.c_cflag |= PARENB;
1934 break;
1935 }
1936
1937 if (flow == 'r')
1938 termios.c_cflag |= CRTSCTS;
1939
Yinghai Lu79492682007-07-15 23:37:25 -07001940 /*
1941 * some uarts on other side don't support no flow control.
1942 * So we set * DTR in host uart to make them happy
1943 */
1944 port->mctrl |= TIOCM_DTR;
1945
Alan Cox149b36e2007-10-18 01:24:16 -07001946 port->ops->set_termios(port, &termios, &dummy);
Jason Wesself2d937f2008-04-17 20:05:37 +02001947 /*
1948 * Allow the setting of the UART parameters with a NULL console
1949 * too:
1950 */
1951 if (co)
1952 co->cflag = termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 return 0;
1955}
Jason Wesself2d937f2008-04-17 20:05:37 +02001956EXPORT_SYMBOL_GPL(uart_set_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957#endif /* CONFIG_SERIAL_CORE_CONSOLE */
1958
1959static void uart_change_pm(struct uart_state *state, int pm_state)
1960{
1961 struct uart_port *port = state->port;
Andrew Victor1281e362006-05-16 11:28:49 +01001962
1963 if (state->pm_state != pm_state) {
1964 if (port->ops->pm)
1965 port->ops->pm(port, pm_state, state->pm_state);
1966 state->pm_state = pm_state;
1967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001970struct uart_match {
1971 struct uart_port *port;
1972 struct uart_driver *driver;
1973};
1974
1975static int serial_match_port(struct device *dev, void *data)
1976{
1977 struct uart_match *match = data;
Guennadi Liakhovetski7ca796f2008-07-04 09:59:28 -07001978 struct tty_driver *tty_drv = match->driver->tty_driver;
1979 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1980 match->port->line;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001981
1982 return dev->devt == devt; /* Actually, only one tty per port */
1983}
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1986{
1987 struct uart_state *state = drv->state + port->line;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001988 struct device *tty_dev;
1989 struct uart_match match = {port, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Ingo Molnare2862f62006-01-13 21:37:07 +00001991 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001993 if (!console_suspend_enabled && uart_console(port)) {
1994 /* we're going to avoid suspending serial console */
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07001995 mutex_unlock(&state->mutex);
1996 return 0;
1997 }
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07001998
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001999 tty_dev = device_find_child(port->dev, &match, serial_match_port);
2000 if (device_may_wakeup(tty_dev)) {
2001 enable_irq_wake(port->irq);
2002 put_device(tty_dev);
2003 mutex_unlock(&state->mutex);
2004 return 0;
2005 }
2006 port->suspended = 1;
2007
Alan Coxf7519282009-01-02 13:49:21 +00002008 if (state->info.flags & UIF_INITIALIZED) {
Russell Kingba899db2006-01-21 22:45:50 +00002009 const struct uart_ops *ops = port->ops;
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002010 int tries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Alan Coxf7519282009-01-02 13:49:21 +00002012 state->info.flags = (state->info.flags & ~UIF_INITIALIZED)
Russell Kinga6b93a92006-10-01 17:17:40 +01002013 | UIF_SUSPENDED;
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 spin_lock_irq(&port->lock);
Russell Kingb129a8c2005-08-31 10:12:14 +01002016 ops->stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 ops->set_mctrl(port, 0);
2018 ops->stop_rx(port);
2019 spin_unlock_irq(&port->lock);
2020
2021 /*
2022 * Wait for the transmitter to empty.
2023 */
Alan Coxa46c9992008-02-08 04:18:53 -08002024 for (tries = 3; !ops->tx_empty(port) && tries; tries--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 msleep(10);
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002026 if (!tries)
Alan Coxa46c9992008-02-08 04:18:53 -08002027 printk(KERN_ERR "%s%s%s%d: Unable to drain "
2028 "transmitter\n",
Kay Sievers4bfe0902009-01-06 10:44:37 -08002029 port->dev ? dev_name(port->dev) : "",
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002030 port->dev ? ": " : "",
David S. Miller84408382008-10-13 10:45:26 +01002031 drv->dev_name,
2032 drv->tty_driver->name_base + port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 ops->shutdown(port);
2035 }
2036
2037 /*
2038 * Disable the console device before suspending.
2039 */
2040 if (uart_console(port))
2041 console_stop(port->cons);
2042
2043 uart_change_pm(state, 3);
2044
Ingo Molnare2862f62006-01-13 21:37:07 +00002045 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 return 0;
2048}
2049
2050int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
2051{
2052 struct uart_state *state = drv->state + port->line;
Arjan van de Ven03a74dc2008-05-23 13:04:49 -07002053 struct device *tty_dev;
2054 struct uart_match match = {port, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Ingo Molnare2862f62006-01-13 21:37:07 +00002056 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002058 if (!console_suspend_enabled && uart_console(port)) {
2059 /* no need to resume serial console, it wasn't suspended */
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002060 mutex_unlock(&state->mutex);
2061 return 0;
2062 }
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002063
Arjan van de Ven03a74dc2008-05-23 13:04:49 -07002064 tty_dev = device_find_child(port->dev, &match, serial_match_port);
2065 if (!port->suspended && device_may_wakeup(tty_dev)) {
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002066 disable_irq_wake(port->irq);
2067 mutex_unlock(&state->mutex);
2068 return 0;
2069 }
2070 port->suspended = 0;
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 /*
2073 * Re-enable the console device after suspending.
2074 */
2075 if (uart_console(port)) {
Alan Cox606d0992006-12-08 02:38:45 -08002076 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 /*
2079 * First try to use the console cflag setting.
2080 */
Alan Cox606d0992006-12-08 02:38:45 -08002081 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 termios.c_cflag = port->cons->cflag;
2083
2084 /*
2085 * If that's unset, use the tty termios setting.
2086 */
Alan Coxf7519282009-01-02 13:49:21 +00002087 if (state->info.port.tty && termios.c_cflag == 0)
2088 termios = *state->info.port.tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Russell King9d778a62008-02-04 22:27:51 -08002090 uart_change_pm(state, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 port->ops->set_termios(port, &termios, NULL);
2092 console_start(port->cons);
2093 }
2094
Alan Coxf7519282009-01-02 13:49:21 +00002095 if (state->info.flags & UIF_SUSPENDED) {
Russell Kingba899db2006-01-21 22:45:50 +00002096 const struct uart_ops *ops = port->ops;
Russell Kingee31b332005-11-13 15:28:51 +00002097 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Russell King9d778a62008-02-04 22:27:51 -08002099 uart_change_pm(state, 0);
Alan Coxf34d7a52008-04-30 00:54:13 -07002100 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 ops->set_mctrl(port, 0);
Alan Coxf34d7a52008-04-30 00:54:13 -07002102 spin_unlock_irq(&port->lock);
Russell Kingee31b332005-11-13 15:28:51 +00002103 ret = ops->startup(port);
2104 if (ret == 0) {
2105 uart_change_speed(state, NULL);
2106 spin_lock_irq(&port->lock);
2107 ops->set_mctrl(port, port->mctrl);
2108 ops->start_tx(port);
2109 spin_unlock_irq(&port->lock);
Alan Coxf7519282009-01-02 13:49:21 +00002110 state->info.flags |= UIF_INITIALIZED;
Russell Kingee31b332005-11-13 15:28:51 +00002111 } else {
2112 /*
2113 * Failed to resume - maybe hardware went away?
2114 * Clear the "initialized" flag so we won't try
2115 * to call the low level drivers shutdown method.
2116 */
Russell Kingee31b332005-11-13 15:28:51 +00002117 uart_shutdown(state);
2118 }
Russell Kinga6b93a92006-10-01 17:17:40 +01002119
Alan Coxf7519282009-01-02 13:49:21 +00002120 state->info.flags &= ~UIF_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
2122
Ingo Molnare2862f62006-01-13 21:37:07 +00002123 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 return 0;
2126}
2127
2128static inline void
2129uart_report_port(struct uart_driver *drv, struct uart_port *port)
2130{
Russell King30b7a3b2005-09-03 15:30:21 +01002131 char address[64];
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 switch (port->iotype) {
2134 case UPIO_PORT:
Andrew Morton9bde10a2008-10-13 10:35:42 +01002135 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 break;
2137 case UPIO_HUB6:
Russell King30b7a3b2005-09-03 15:30:21 +01002138 snprintf(address, sizeof(address),
Andrew Morton9bde10a2008-10-13 10:35:42 +01002139 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 break;
2141 case UPIO_MEM:
2142 case UPIO_MEM32:
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002143 case UPIO_AU:
Zang Roy-r619113be91ec2006-06-30 02:29:58 -07002144 case UPIO_TSI:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002145 case UPIO_DWAPB:
Russell King30b7a3b2005-09-03 15:30:21 +01002146 snprintf(address, sizeof(address),
Josh Boyer4f640ef2007-07-23 18:43:44 -07002147 "MMIO 0x%llx", (unsigned long long)port->mapbase);
Russell King30b7a3b2005-09-03 15:30:21 +01002148 break;
2149 default:
2150 strlcpy(address, "*unknown*", sizeof(address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 break;
2152 }
Russell King30b7a3b2005-09-03 15:30:21 +01002153
Russell King0cf669d2005-10-31 11:42:22 +00002154 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
Kay Sievers4bfe0902009-01-06 10:44:37 -08002155 port->dev ? dev_name(port->dev) : "",
Russell King0cf669d2005-10-31 11:42:22 +00002156 port->dev ? ": " : "",
David S. Miller84408382008-10-13 10:45:26 +01002157 drv->dev_name,
2158 drv->tty_driver->name_base + port->line,
2159 address, port->irq, uart_type(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
2162static void
2163uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2164 struct uart_port *port)
2165{
2166 unsigned int flags;
2167
2168 /*
2169 * If there isn't a port here, don't do anything further.
2170 */
2171 if (!port->iobase && !port->mapbase && !port->membase)
2172 return;
2173
2174 /*
2175 * Now do the auto configuration stuff. Note that config_port
2176 * is expected to claim the resources and map the port for us.
2177 */
David Daney8e23fcc2009-01-02 13:49:54 +00002178 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 if (port->flags & UPF_AUTO_IRQ)
2180 flags |= UART_CONFIG_IRQ;
2181 if (port->flags & UPF_BOOT_AUTOCONF) {
David Daney8e23fcc2009-01-02 13:49:54 +00002182 if (!(port->flags & UPF_FIXED_TYPE)) {
2183 port->type = PORT_UNKNOWN;
2184 flags |= UART_CONFIG_TYPE;
2185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 port->ops->config_port(port, flags);
2187 }
2188
2189 if (port->type != PORT_UNKNOWN) {
2190 unsigned long flags;
2191
2192 uart_report_port(drv, port);
2193
George G. Davis3689a0e2007-02-14 00:33:06 -08002194 /* Power up port for set_mctrl() */
2195 uart_change_pm(state, 0);
2196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 /*
2198 * Ensure that the modem control lines are de-activated.
Yinghai Luc3e46422008-02-04 22:27:46 -08002199 * keep the DTR setting that is set in uart_set_options()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 * We probably don't need a spinlock around this, but
2201 */
2202 spin_lock_irqsave(&port->lock, flags);
Yinghai Luc3e46422008-02-04 22:27:46 -08002203 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 spin_unlock_irqrestore(&port->lock, flags);
2205
2206 /*
Russell King97d97222007-09-01 21:25:09 +01002207 * If this driver supports console, and it hasn't been
2208 * successfully registered yet, try to re-register it.
2209 * It may be that the port was not available.
2210 */
2211 if (port->cons && !(port->cons->flags & CON_ENABLED))
2212 register_console(port->cons);
2213
2214 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 * Power down all ports by default, except the
2216 * console if we have one.
2217 */
2218 if (!uart_console(port))
2219 uart_change_pm(state, 3);
2220 }
2221}
2222
Jason Wesself2d937f2008-04-17 20:05:37 +02002223#ifdef CONFIG_CONSOLE_POLL
2224
2225static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2226{
2227 struct uart_driver *drv = driver->driver_state;
2228 struct uart_state *state = drv->state + line;
2229 struct uart_port *port;
2230 int baud = 9600;
2231 int bits = 8;
2232 int parity = 'n';
2233 int flow = 'n';
2234
2235 if (!state || !state->port)
2236 return -1;
2237
2238 port = state->port;
2239 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2240 return -1;
2241
2242 if (options) {
2243 uart_parse_options(options, &baud, &parity, &bits, &flow);
2244 return uart_set_options(port, NULL, baud, parity, bits, flow);
2245 }
2246
2247 return 0;
2248}
2249
2250static int uart_poll_get_char(struct tty_driver *driver, int line)
2251{
2252 struct uart_driver *drv = driver->driver_state;
2253 struct uart_state *state = drv->state + line;
2254 struct uart_port *port;
2255
2256 if (!state || !state->port)
2257 return -1;
2258
2259 port = state->port;
2260 return port->ops->poll_get_char(port);
2261}
2262
2263static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2264{
2265 struct uart_driver *drv = driver->driver_state;
2266 struct uart_state *state = drv->state + line;
2267 struct uart_port *port;
2268
2269 if (!state || !state->port)
2270 return;
2271
2272 port = state->port;
2273 port->ops->poll_put_char(port, ch);
2274}
2275#endif
2276
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002277static const struct tty_operations uart_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 .open = uart_open,
2279 .close = uart_close,
2280 .write = uart_write,
2281 .put_char = uart_put_char,
2282 .flush_chars = uart_flush_chars,
2283 .write_room = uart_write_room,
2284 .chars_in_buffer= uart_chars_in_buffer,
2285 .flush_buffer = uart_flush_buffer,
2286 .ioctl = uart_ioctl,
2287 .throttle = uart_throttle,
2288 .unthrottle = uart_unthrottle,
2289 .send_xchar = uart_send_xchar,
2290 .set_termios = uart_set_termios,
Alan Cox64e91592008-06-03 15:18:54 +01002291 .set_ldisc = uart_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 .stop = uart_stop,
2293 .start = uart_start,
2294 .hangup = uart_hangup,
2295 .break_ctl = uart_break_ctl,
2296 .wait_until_sent= uart_wait_until_sent,
2297#ifdef CONFIG_PROC_FS
Alexey Dobriyand196a942009-03-31 15:19:21 -07002298 .proc_fops = &uart_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299#endif
2300 .tiocmget = uart_tiocmget,
2301 .tiocmset = uart_tiocmset,
Jason Wesself2d937f2008-04-17 20:05:37 +02002302#ifdef CONFIG_CONSOLE_POLL
2303 .poll_init = uart_poll_init,
2304 .poll_get_char = uart_poll_get_char,
2305 .poll_put_char = uart_poll_put_char,
2306#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307};
2308
2309/**
2310 * uart_register_driver - register a driver with the uart core layer
2311 * @drv: low level driver structure
2312 *
2313 * Register a uart driver with the core driver. We in turn register
2314 * with the tty layer, and initialise the core driver per-port state.
2315 *
2316 * We have a proc file in /proc/tty/driver which is named after the
2317 * normal driver.
2318 *
2319 * drv->port should be NULL, and the per-port structures should be
2320 * registered using uart_add_one_port after this call has succeeded.
2321 */
2322int uart_register_driver(struct uart_driver *drv)
2323{
2324 struct tty_driver *normal = NULL;
2325 int i, retval;
2326
2327 BUG_ON(drv->state);
2328
2329 /*
2330 * Maybe we should be using a slab cache for this, especially if
2331 * we have a large number of ports to handle.
2332 */
Burman Yan8f31bb32007-02-14 00:33:07 -08002333 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 retval = -ENOMEM;
2335 if (!drv->state)
2336 goto out;
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 normal = alloc_tty_driver(drv->nr);
2339 if (!normal)
2340 goto out;
2341
2342 drv->tty_driver = normal;
2343
2344 normal->owner = drv->owner;
2345 normal->driver_name = drv->driver_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 normal->name = drv->dev_name;
2347 normal->major = drv->major;
2348 normal->minor_start = drv->minor;
2349 normal->type = TTY_DRIVER_TYPE_SERIAL;
2350 normal->subtype = SERIAL_TYPE_NORMAL;
2351 normal->init_termios = tty_std_termios;
2352 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -08002353 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07002354 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 normal->driver_state = drv;
2356 tty_set_operations(normal, &uart_ops);
2357
2358 /*
2359 * Initialise the UART state(s).
2360 */
2361 for (i = 0; i < drv->nr; i++) {
2362 struct uart_state *state = drv->state + i;
2363
2364 state->close_delay = 500; /* .5 seconds */
2365 state->closing_wait = 30000; /* 30 seconds */
Ingo Molnare2862f62006-01-13 21:37:07 +00002366 mutex_init(&state->mutex);
Alan Coxf7519282009-01-02 13:49:21 +00002367
2368 tty_port_init(&state->info.port);
2369 init_waitqueue_head(&state->info.delta_msr_wait);
2370 tasklet_init(&state->info.tlet, uart_tasklet_action,
2371 (unsigned long)state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
2373
2374 retval = tty_register_driver(normal);
2375 out:
2376 if (retval < 0) {
2377 put_tty_driver(normal);
2378 kfree(drv->state);
2379 }
2380 return retval;
2381}
2382
2383/**
2384 * uart_unregister_driver - remove a driver from the uart core layer
2385 * @drv: low level driver structure
2386 *
2387 * Remove all references to a driver from the core driver. The low
2388 * level driver must have removed all its ports via the
2389 * uart_remove_one_port() if it registered them with uart_add_one_port().
2390 * (ie, drv->port == NULL)
2391 */
2392void uart_unregister_driver(struct uart_driver *drv)
2393{
2394 struct tty_driver *p = drv->tty_driver;
2395 tty_unregister_driver(p);
2396 put_tty_driver(p);
2397 kfree(drv->state);
2398 drv->tty_driver = NULL;
2399}
2400
2401struct tty_driver *uart_console_device(struct console *co, int *index)
2402{
2403 struct uart_driver *p = co->data;
2404 *index = co->index;
2405 return p->tty_driver;
2406}
2407
2408/**
2409 * uart_add_one_port - attach a driver-defined port structure
2410 * @drv: pointer to the uart low level driver structure for this port
2411 * @port: uart port structure to use for this port.
2412 *
2413 * This allows the driver to register its own uart_port structure
2414 * with the core driver. The main purpose is to allow the low
2415 * level uart drivers to expand uart_port, rather than having yet
2416 * more levels of structures.
2417 */
2418int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2419{
2420 struct uart_state *state;
2421 int ret = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002422 struct device *tty_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
2424 BUG_ON(in_interrupt());
2425
2426 if (port->line >= drv->nr)
2427 return -EINVAL;
2428
2429 state = drv->state + port->line;
2430
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002431 mutex_lock(&port_mutex);
Russell King68ac64c2006-04-30 11:13:50 +01002432 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if (state->port) {
2434 ret = -EINVAL;
2435 goto out;
2436 }
2437
2438 state->port = port;
Russell King97d97222007-09-01 21:25:09 +01002439 state->pm_state = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 port->cons = drv->cons;
Alan Coxf7519282009-01-02 13:49:21 +00002442 port->info = &state->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Russell King976ecd12005-07-03 21:05:45 +01002444 /*
2445 * If this port is a console, then the spinlock is already
2446 * initialised.
2447 */
Ingo Molnar13e83592006-07-03 00:25:03 -07002448 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
Russell King976ecd12005-07-03 21:05:45 +01002449 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07002450 lockdep_set_class(&port->lock, &port_lock_key);
2451 }
Russell King976ecd12005-07-03 21:05:45 +01002452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 uart_configure_port(drv, state, port);
2454
2455 /*
2456 * Register the port whether it's detected or not. This allows
2457 * setserial to be used to alter this ports parameters.
2458 */
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002459 tty_dev = tty_register_device(drv->tty_driver, port->line, port->dev);
2460 if (likely(!IS_ERR(tty_dev))) {
Alan Stern74081f82008-03-19 22:35:13 +01002461 device_init_wakeup(tty_dev, 1);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002462 device_set_wakeup_enable(tty_dev, 0);
2463 } else
2464 printk(KERN_ERR "Cannot register tty device on line %d\n",
2465 port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467 /*
Russell King68ac64c2006-04-30 11:13:50 +01002468 * Ensure UPF_DEAD is not set.
2469 */
2470 port->flags &= ~UPF_DEAD;
2471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 out:
Russell King68ac64c2006-04-30 11:13:50 +01002473 mutex_unlock(&state->mutex);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002474 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476 return ret;
2477}
2478
2479/**
2480 * uart_remove_one_port - detach a driver defined port structure
2481 * @drv: pointer to the uart low level driver structure for this port
2482 * @port: uart port structure for this port
2483 *
2484 * This unhooks (and hangs up) the specified port structure from the
2485 * core driver. No further calls will be made to the low-level code
2486 * for this port.
2487 */
2488int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2489{
2490 struct uart_state *state = drv->state + port->line;
Russell King68ac64c2006-04-30 11:13:50 +01002491 struct uart_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 BUG_ON(in_interrupt());
2494
2495 if (state->port != port)
2496 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2497 state->port, port);
2498
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002499 mutex_lock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501 /*
Russell King68ac64c2006-04-30 11:13:50 +01002502 * Mark the port "dead" - this prevents any opens from
2503 * succeeding while we shut down the port.
2504 */
2505 mutex_lock(&state->mutex);
2506 port->flags |= UPF_DEAD;
2507 mutex_unlock(&state->mutex);
2508
2509 /*
Greg Kroah-Hartmanaa4148c2005-06-20 21:15:16 -07002510 * Remove the devices from the tty layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 */
2512 tty_unregister_device(drv->tty_driver, port->line);
2513
Alan Coxf7519282009-01-02 13:49:21 +00002514 info = &state->info;
Alan Coxdf4f4dd2008-07-16 21:53:50 +01002515 if (info && info->port.tty)
2516 tty_vhangup(info->port.tty);
Russell King68ac64c2006-04-30 11:13:50 +01002517
2518 /*
Russell King68ac64c2006-04-30 11:13:50 +01002519 * Free the port IO and memory resources, if any.
2520 */
2521 if (port->type != PORT_UNKNOWN)
2522 port->ops->release_port(port);
2523
2524 /*
2525 * Indicate that there isn't a port here anymore.
2526 */
2527 port->type = PORT_UNKNOWN;
2528
2529 /*
2530 * Kill the tasklet, and free resources.
2531 */
Alexander Beregalovbc325622009-01-02 13:49:32 +00002532 if (info)
Russell King68ac64c2006-04-30 11:13:50 +01002533 tasklet_kill(&info->tlet);
Russell King68ac64c2006-04-30 11:13:50 +01002534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 state->port = NULL;
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002536 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538 return 0;
2539}
2540
2541/*
2542 * Are the two ports equivalent?
2543 */
2544int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2545{
2546 if (port1->iotype != port2->iotype)
2547 return 0;
2548
2549 switch (port1->iotype) {
2550 case UPIO_PORT:
2551 return (port1->iobase == port2->iobase);
2552 case UPIO_HUB6:
2553 return (port1->iobase == port2->iobase) &&
2554 (port1->hub6 == port2->hub6);
2555 case UPIO_MEM:
Sergei Shtylyovd21b55d2006-08-28 19:49:03 +04002556 case UPIO_MEM32:
2557 case UPIO_AU:
2558 case UPIO_TSI:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002559 case UPIO_DWAPB:
Benjamin Herrenschmidt1624f002006-01-04 18:09:44 +00002560 return (port1->mapbase == port2->mapbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 }
2562 return 0;
2563}
2564EXPORT_SYMBOL(uart_match_port);
2565
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566EXPORT_SYMBOL(uart_write_wakeup);
2567EXPORT_SYMBOL(uart_register_driver);
2568EXPORT_SYMBOL(uart_unregister_driver);
2569EXPORT_SYMBOL(uart_suspend_port);
2570EXPORT_SYMBOL(uart_resume_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571EXPORT_SYMBOL(uart_add_one_port);
2572EXPORT_SYMBOL(uart_remove_one_port);
2573
2574MODULE_DESCRIPTION("Serial driver core");
2575MODULE_LICENSE("GPL");