blob: 0ffefb3f1d8fe951a65dec5e37afa2600ec91db9 [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 Coxebd2c8f2009-09-19 13:13:28 -070055#define uart_users(state) ((state)->count + (state)->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{
Alan Coxebd2c8f2009-09-19 13:13:28 -070074 struct uart_state *state = port->state;
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 */
Alan Coxebd2c8f2009-09-19 13:13:28 -070079 BUG_ON(!state);
80 tasklet_schedule(&state->tlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
83static void uart_stop(struct tty_struct *tty)
84{
85 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -070086 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 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;
Alan Coxebd2c8f2009-09-19 13:13:28 -070097 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Alan Coxebd2c8f2009-09-19 13:13:28 -070099 if (!uart_circ_empty(&state->xmit) && state->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;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700107 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 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 Coxebd2c8f2009-09-19 13:13:28 -0700118 tty_wakeup(state->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 Cox46d57a42009-09-19 13:13:29 -0700144 struct uart_port *uport = state->uart_port;
145 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 unsigned long page;
147 int retval = 0;
148
Alan Coxebd2c8f2009-09-19 13:13:28 -0700149 if (state->flags & UIF_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 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 Cox46d57a42009-09-19 13:13:29 -0700157 set_bit(TTY_IO_ERROR, &port->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Alan Cox46d57a42009-09-19 13:13:29 -0700159 if (uport->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return 0;
161
162 /*
163 * Initialise and allocate the transmit and temporary
164 * buffer.
165 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700166 if (!state->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
Alan Coxebd2c8f2009-09-19 13:13:28 -0700172 state->xmit.buf = (unsigned char *) page;
173 uart_circ_clear(&state->xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175
Alan Cox46d57a42009-09-19 13:13:29 -0700176 retval = uport->ops->startup(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 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 Cox46d57a42009-09-19 13:13:29 -0700188 if (port->tty->termios->c_cflag & CBAUD)
189 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191
Alan Coxebd2c8f2009-09-19 13:13:28 -0700192 if (state->flags & UIF_CTS_FLOW) {
Alan Cox46d57a42009-09-19 13:13:29 -0700193 spin_lock_irq(&uport->lock);
194 if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS))
195 port->tty->hw_stopped = 1;
196 spin_unlock_irq(&uport->lock);
Russell King0dd7a1a2005-06-29 18:40:53 +0100197 }
198
Alan Coxebd2c8f2009-09-19 13:13:28 -0700199 state->flags |= UIF_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Alan Cox46d57a42009-09-19 13:13:29 -0700201 clear_bit(TTY_IO_ERROR, &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 Coxebd2c8f2009-09-19 13:13:28 -0700217 struct uart_port *port = state->uart_port;
218 struct tty_struct *tty = state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Russell Kingee31b332005-11-13 15:28:51 +0000220 /*
221 * Set the TTY IO error marker
222 */
Alan Coxf7519282009-01-02 13:49:21 +0000223 if (tty)
224 set_bit(TTY_IO_ERROR, &tty->flags);
Russell Kingee31b332005-11-13 15:28:51 +0000225
Alan Coxebd2c8f2009-09-19 13:13:28 -0700226 if (state->flags & UIF_INITIALIZED) {
227 state->flags &= ~UIF_INITIALIZED;
Russell Kingee31b332005-11-13 15:28:51 +0000228
229 /*
230 * Turn off DTR and RTS early.
231 */
Alan Coxf7519282009-01-02 13:49:21 +0000232 if (!tty || (tty->termios->c_cflag & HUPCL))
Russell Kingee31b332005-11-13 15:28:51 +0000233 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
234
235 /*
236 * clear delta_msr_wait queue to avoid mem leaks: we may free
237 * the irq here so the queue might never be woken up. Note
238 * that we won't end up waiting on delta_msr_wait again since
239 * any outstanding file descriptors should be pointing at
240 * hung_up_tty_fops now.
241 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700242 wake_up_interruptible(&state->delta_msr_wait);
Russell Kingee31b332005-11-13 15:28:51 +0000243
244 /*
245 * Free the IRQ and disable the port.
246 */
247 port->ops->shutdown(port);
248
249 /*
250 * Ensure that the IRQ handler isn't running on another CPU.
251 */
252 synchronize_irq(port->irq);
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 /*
Russell Kingee31b332005-11-13 15:28:51 +0000256 * kill off our tasklet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700258 tasklet_kill(&state->tlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /*
261 * Free the transmit buffer page.
262 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700263 if (state->xmit.buf) {
264 free_page((unsigned long)state->xmit.buf);
265 state->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269/**
270 * uart_update_timeout - update per-port FIFO timeout.
271 * @port: uart_port structure describing the port
272 * @cflag: termios cflag value
273 * @baud: speed of the port
274 *
275 * Set the port FIFO timeout value. The @cflag value should
276 * reflect the actual hardware settings.
277 */
278void
279uart_update_timeout(struct uart_port *port, unsigned int cflag,
280 unsigned int baud)
281{
282 unsigned int bits;
283
284 /* byte size and parity */
285 switch (cflag & CSIZE) {
286 case CS5:
287 bits = 7;
288 break;
289 case CS6:
290 bits = 8;
291 break;
292 case CS7:
293 bits = 9;
294 break;
295 default:
296 bits = 10;
Alan Coxa46c9992008-02-08 04:18:53 -0800297 break; /* CS8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
300 if (cflag & CSTOPB)
301 bits++;
302 if (cflag & PARENB)
303 bits++;
304
305 /*
306 * The total number of bits to be transmitted in the fifo.
307 */
308 bits = bits * port->fifosize;
309
310 /*
311 * Figure the timeout to send the above number of bits.
312 * Add .02 seconds of slop
313 */
314 port->timeout = (HZ * bits) / baud + HZ/50;
315}
316
317EXPORT_SYMBOL(uart_update_timeout);
318
319/**
320 * uart_get_baud_rate - return baud rate for a particular port
321 * @port: uart_port structure describing the port in question.
322 * @termios: desired termios settings.
323 * @old: old termios (or NULL)
324 * @min: minimum acceptable baud rate
325 * @max: maximum acceptable baud rate
326 *
327 * Decode the termios structure into a numeric baud rate,
328 * taking account of the magic 38400 baud rate (with spd_*
329 * flags), and mapping the %B0 rate to 9600 baud.
330 *
331 * If the new baud rate is invalid, try the old termios setting.
332 * If it's still invalid, we try 9600 baud.
333 *
334 * Update the @termios structure to reflect the baud rate
Alan Coxeb424fd2008-04-28 02:14:07 -0700335 * we're actually going to be using. Don't do this for the case
336 * where B0 is requested ("hang up").
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
338unsigned int
Alan Cox606d0992006-12-08 02:38:45 -0800339uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
340 struct ktermios *old, unsigned int min, unsigned int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 unsigned int try, baud, altbaud = 38400;
Alan Coxeb424fd2008-04-28 02:14:07 -0700343 int hung_up = 0;
Russell King0077d452006-01-21 23:03:28 +0000344 upf_t flags = port->flags & UPF_SPD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 if (flags == UPF_SPD_HI)
347 altbaud = 57600;
348 if (flags == UPF_SPD_VHI)
349 altbaud = 115200;
350 if (flags == UPF_SPD_SHI)
351 altbaud = 230400;
352 if (flags == UPF_SPD_WARP)
353 altbaud = 460800;
354
355 for (try = 0; try < 2; try++) {
356 baud = tty_termios_baud_rate(termios);
357
358 /*
359 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
360 * Die! Die! Die!
361 */
362 if (baud == 38400)
363 baud = altbaud;
364
365 /*
366 * Special case: B0 rate.
367 */
Alan Coxeb424fd2008-04-28 02:14:07 -0700368 if (baud == 0) {
369 hung_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 baud = 9600;
Alan Coxeb424fd2008-04-28 02:14:07 -0700371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 if (baud >= min && baud <= max)
374 return baud;
375
376 /*
377 * Oops, the quotient was zero. Try again with
378 * the old baud rate if possible.
379 */
380 termios->c_cflag &= ~CBAUD;
381 if (old) {
Alan Cox6d4d67b2008-02-04 22:27:53 -0800382 baud = tty_termios_baud_rate(old);
Alan Coxeb424fd2008-04-28 02:14:07 -0700383 if (!hung_up)
384 tty_termios_encode_baud_rate(termios,
385 baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 old = NULL;
387 continue;
388 }
389
390 /*
391 * As a last resort, if the quotient is zero,
392 * default to 9600 bps
393 */
Alan Coxeb424fd2008-04-28 02:14:07 -0700394 if (!hung_up)
395 tty_termios_encode_baud_rate(termios, 9600, 9600);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397
398 return 0;
399}
400
401EXPORT_SYMBOL(uart_get_baud_rate);
402
403/**
404 * uart_get_divisor - return uart clock divisor
405 * @port: uart_port structure describing the port.
406 * @baud: desired baud rate
407 *
408 * Calculate the uart clock divisor for the port.
409 */
410unsigned int
411uart_get_divisor(struct uart_port *port, unsigned int baud)
412{
413 unsigned int quot;
414
415 /*
416 * Old custom speed handling.
417 */
418 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
419 quot = port->custom_divisor;
420 else
421 quot = (port->uartclk + (8 * baud)) / (16 * baud);
422
423 return quot;
424}
425
426EXPORT_SYMBOL(uart_get_divisor);
427
Alan Cox23d22ce2008-04-30 00:54:11 -0700428/* FIXME: Consistent locking policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static void
Alan Cox606d0992006-12-08 02:38:45 -0800430uart_change_speed(struct uart_state *state, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700432 struct tty_struct *tty = state->port.tty;
433 struct uart_port *port = state->uart_port;
Alan Cox606d0992006-12-08 02:38:45 -0800434 struct ktermios *termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /*
437 * If we have no tty, termios, or the port does not exist,
438 * then we can't set the parameters for this port.
439 */
440 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
441 return;
442
443 termios = tty->termios;
444
445 /*
446 * Set flags based on termios cflag
447 */
448 if (termios->c_cflag & CRTSCTS)
Alan Coxebd2c8f2009-09-19 13:13:28 -0700449 state->flags |= UIF_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 else
Alan Coxebd2c8f2009-09-19 13:13:28 -0700451 state->flags &= ~UIF_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 if (termios->c_cflag & CLOCAL)
Alan Coxebd2c8f2009-09-19 13:13:28 -0700454 state->flags &= ~UIF_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 else
Alan Coxebd2c8f2009-09-19 13:13:28 -0700456 state->flags |= UIF_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 port->ops->set_termios(port, termios, old_termios);
459}
460
Alan Cox23d22ce2008-04-30 00:54:11 -0700461static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462__uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
463{
464 unsigned long flags;
Alan Cox23d22ce2008-04-30 00:54:11 -0700465 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 if (!circ->buf)
Alan Cox23d22ce2008-04-30 00:54:11 -0700468 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 spin_lock_irqsave(&port->lock, flags);
471 if (uart_circ_chars_free(circ) != 0) {
472 circ->buf[circ->head] = c;
473 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
Alan Cox23d22ce2008-04-30 00:54:11 -0700474 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 spin_unlock_irqrestore(&port->lock, flags);
Alan Cox23d22ce2008-04-30 00:54:11 -0700477 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Alan Cox23d22ce2008-04-30 00:54:11 -0700480static int uart_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct uart_state *state = tty->driver_data;
483
Alan Coxebd2c8f2009-09-19 13:13:28 -0700484 return __uart_put_char(state->uart_port, &state->xmit, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
487static void uart_flush_chars(struct tty_struct *tty)
488{
489 uart_start(tty);
490}
491
492static int
Pavel Machekd5f735e2006-03-07 21:55:20 -0800493uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct uart_state *state = tty->driver_data;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800496 struct uart_port *port;
497 struct circ_buf *circ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 unsigned long flags;
499 int c, ret = 0;
500
Pavel Machekd5f735e2006-03-07 21:55:20 -0800501 /*
502 * This means you called this function _after_ the port was
503 * closed. No cookie for you.
504 */
Alan Coxf7519282009-01-02 13:49:21 +0000505 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800506 WARN_ON(1);
507 return -EL3HLT;
508 }
509
Alan Coxebd2c8f2009-09-19 13:13:28 -0700510 port = state->uart_port;
511 circ = &state->xmit;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (!circ->buf)
514 return 0;
515
516 spin_lock_irqsave(&port->lock, flags);
517 while (1) {
518 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
519 if (count < c)
520 c = count;
521 if (c <= 0)
522 break;
523 memcpy(circ->buf + circ->head, buf, c);
524 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
525 buf += c;
526 count -= c;
527 ret += c;
528 }
529 spin_unlock_irqrestore(&port->lock, flags);
530
531 uart_start(tty);
532 return ret;
533}
534
535static int uart_write_room(struct tty_struct *tty)
536{
537 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700538 unsigned long flags;
539 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Alan Coxebd2c8f2009-09-19 13:13:28 -0700541 spin_lock_irqsave(&state->uart_port->lock, flags);
542 ret = uart_circ_chars_free(&state->xmit);
543 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700544 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
547static int uart_chars_in_buffer(struct tty_struct *tty)
548{
549 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700550 unsigned long flags;
551 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Alan Coxebd2c8f2009-09-19 13:13:28 -0700553 spin_lock_irqsave(&state->uart_port->lock, flags);
554 ret = uart_circ_chars_pending(&state->xmit);
555 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700556 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
559static void uart_flush_buffer(struct tty_struct *tty)
560{
561 struct uart_state *state = tty->driver_data;
Tetsuo Handa55d7b682008-05-06 20:42:27 -0700562 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 unsigned long flags;
564
Pavel Machekd5f735e2006-03-07 21:55:20 -0800565 /*
566 * This means you called this function _after_ the port was
567 * closed. No cookie for you.
568 */
Alan Coxf7519282009-01-02 13:49:21 +0000569 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800570 WARN_ON(1);
571 return;
572 }
573
Alan Coxebd2c8f2009-09-19 13:13:28 -0700574 port = state->uart_port;
Jiri Slabyeb3a1e12007-05-06 14:48:52 -0700575 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 spin_lock_irqsave(&port->lock, flags);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700578 uart_circ_clear(&state->xmit);
Haavard Skinnemoen6bb0e3a2008-07-16 21:52:36 +0100579 if (port->ops->flush_buffer)
580 port->ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 spin_unlock_irqrestore(&port->lock, flags);
582 tty_wakeup(tty);
583}
584
585/*
586 * This function is used to send a high-priority XON/XOFF character to
587 * the device
588 */
589static void uart_send_xchar(struct tty_struct *tty, char ch)
590{
591 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700592 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 unsigned long flags;
594
595 if (port->ops->send_xchar)
596 port->ops->send_xchar(port, ch);
597 else {
598 port->x_char = ch;
599 if (ch) {
600 spin_lock_irqsave(&port->lock, flags);
Russell Kingb129a8c2005-08-31 10:12:14 +0100601 port->ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 spin_unlock_irqrestore(&port->lock, flags);
603 }
604 }
605}
606
607static void uart_throttle(struct tty_struct *tty)
608{
609 struct uart_state *state = tty->driver_data;
610
611 if (I_IXOFF(tty))
612 uart_send_xchar(tty, STOP_CHAR(tty));
613
614 if (tty->termios->c_cflag & CRTSCTS)
Alan Coxebd2c8f2009-09-19 13:13:28 -0700615 uart_clear_mctrl(state->uart_port, TIOCM_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
618static void uart_unthrottle(struct tty_struct *tty)
619{
620 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700621 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 if (I_IXOFF(tty)) {
624 if (port->x_char)
625 port->x_char = 0;
626 else
627 uart_send_xchar(tty, START_CHAR(tty));
628 }
629
630 if (tty->termios->c_cflag & CRTSCTS)
631 uart_set_mctrl(port, TIOCM_RTS);
632}
633
634static int uart_get_info(struct uart_state *state,
635 struct serial_struct __user *retinfo)
636{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700637 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct serial_struct tmp;
639
640 memset(&tmp, 0, sizeof(tmp));
Alan Coxf34d7a52008-04-30 00:54:13 -0700641
642 /* Ensure the state we copy is consistent and no hardware changes
643 occur as we go */
644 mutex_lock(&state->mutex);
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 tmp.type = port->type;
647 tmp.line = port->line;
648 tmp.port = port->iobase;
649 if (HIGH_BITS_OFFSET)
650 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
651 tmp.irq = port->irq;
652 tmp.flags = port->flags;
653 tmp.xmit_fifo_size = port->fifosize;
654 tmp.baud_base = port->uartclk / 16;
Alan Cox5e99df52009-09-19 13:13:28 -0700655 tmp.close_delay = state->port.close_delay / 10;
656 tmp.closing_wait = state->port.closing_wait == USF_CLOSING_WAIT_NONE ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ASYNC_CLOSING_WAIT_NONE :
Alan Cox5e99df52009-09-19 13:13:28 -0700658 state->port.closing_wait / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 tmp.custom_divisor = port->custom_divisor;
660 tmp.hub6 = port->hub6;
661 tmp.io_type = port->iotype;
662 tmp.iomem_reg_shift = port->regshift;
Josh Boyer4f640ef2007-07-23 18:43:44 -0700663 tmp.iomem_base = (void *)(unsigned long)port->mapbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Alan Coxf34d7a52008-04-30 00:54:13 -0700665 mutex_unlock(&state->mutex);
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
668 return -EFAULT;
669 return 0;
670}
671
672static int uart_set_info(struct uart_state *state,
673 struct serial_struct __user *newinfo)
674{
675 struct serial_struct new_serial;
Alan Cox46d57a42009-09-19 13:13:29 -0700676 struct uart_port *uport = state->uart_port;
677 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 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
Alan Cox46d57a42009-09-19 13:13:29 -0700705 change_irq = !(uport->flags & UPF_FIXED_PORT)
706 && new_serial.irq != uport->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 */
Alan Cox46d57a42009-09-19 13:13:29 -0700713 change_port = !(uport->flags & UPF_FIXED_PORT)
714 && (new_port != uport->iobase ||
715 (unsigned long)new_serial.iomem_base != uport->mapbase ||
716 new_serial.hub6 != uport->hub6 ||
717 new_serial.io_type != uport->iotype ||
718 new_serial.iomem_reg_shift != uport->regshift ||
719 new_serial.type != uport->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Alan Cox46d57a42009-09-19 13:13:29 -0700721 old_flags = uport->flags;
Russell King0077d452006-01-21 23:03:28 +0000722 new_flags = new_serial.flags;
Alan Cox46d57a42009-09-19 13:13:29 -0700723 old_custom_divisor = uport->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 if (!capable(CAP_SYS_ADMIN)) {
726 retval = -EPERM;
727 if (change_irq || change_port ||
Alan Cox46d57a42009-09-19 13:13:29 -0700728 (new_serial.baud_base != uport->uartclk / 16) ||
729 (close_delay != port->close_delay) ||
730 (closing_wait != port->closing_wait) ||
Russell King947deee2006-07-02 20:45:51 +0100731 (new_serial.xmit_fifo_size &&
Alan Cox46d57a42009-09-19 13:13:29 -0700732 new_serial.xmit_fifo_size != uport->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;
Alan Cox46d57a42009-09-19 13:13:29 -0700735 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000736 (new_flags & UPF_USR_MASK));
Alan Cox46d57a42009-09-19 13:13:29 -0700737 uport->custom_divisor = new_serial.custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 goto check_and_exit;
739 }
740
741 /*
742 * Ask the low level driver to verify the settings.
743 */
Alan Cox46d57a42009-09-19 13:13:29 -0700744 if (uport->ops->verify_port)
745 retval = uport->ops->verify_port(uport, &new_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
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
Alan Cox46d57a42009-09-19 13:13:29 -0700774 old_iobase = uport->iobase;
775 old_mapbase = uport->mapbase;
776 old_type = uport->type;
777 old_hub6 = uport->hub6;
778 old_iotype = uport->iotype;
779 old_shift = uport->regshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 /*
782 * Free and release old regions
783 */
784 if (old_type != PORT_UNKNOWN)
Alan Cox46d57a42009-09-19 13:13:29 -0700785 uport->ops->release_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Alan Cox46d57a42009-09-19 13:13:29 -0700787 uport->iobase = new_port;
788 uport->type = new_serial.type;
789 uport->hub6 = new_serial.hub6;
790 uport->iotype = new_serial.io_type;
791 uport->regshift = new_serial.iomem_reg_shift;
792 uport->mapbase = (unsigned long)new_serial.iomem_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 /*
795 * Claim and map the new regions
796 */
Alan Cox46d57a42009-09-19 13:13:29 -0700797 if (uport->type != PORT_UNKNOWN) {
798 retval = uport->ops->request_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 } 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) {
Alan Cox46d57a42009-09-19 13:13:29 -0700809 uport->iobase = old_iobase;
810 uport->type = old_type;
811 uport->hub6 = old_hub6;
812 uport->iotype = old_iotype;
813 uport->regshift = old_shift;
814 uport->mapbase = old_mapbase;
815 retval = uport->ops->request_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /*
817 * If we failed to restore the old settings,
818 * we fail like this.
819 */
820 if (retval)
Alan Cox46d57a42009-09-19 13:13:29 -0700821 uport->type = PORT_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
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)
Alan Cox46d57a42009-09-19 13:13:29 -0700833 uport->irq = new_serial.irq;
834 if (!(uport->flags & UPF_FIXED_PORT))
835 uport->uartclk = new_serial.baud_base * 16;
836 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000837 (new_flags & UPF_CHANGE_MASK);
Alan Cox46d57a42009-09-19 13:13:29 -0700838 uport->custom_divisor = new_serial.custom_divisor;
839 port->close_delay = close_delay;
840 port->closing_wait = closing_wait;
Russell King947deee2006-07-02 20:45:51 +0100841 if (new_serial.xmit_fifo_size)
Alan Cox46d57a42009-09-19 13:13:29 -0700842 uport->fifosize = new_serial.xmit_fifo_size;
843 if (port->tty)
844 port->tty->low_latency =
845 (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 check_and_exit:
848 retval = 0;
Alan Cox46d57a42009-09-19 13:13:29 -0700849 if (uport->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 goto exit;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700851 if (state->flags & UIF_INITIALIZED) {
Alan Cox46d57a42009-09-19 13:13:29 -0700852 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
853 old_custom_divisor != uport->custom_divisor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /*
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 */
Alan Cox46d57a42009-09-19 13:13:29 -0700859 if (uport->flags & UPF_SPD_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 char buf[64];
861 printk(KERN_NOTICE
862 "%s sets custom speed on %s. This "
863 "is deprecated.\n", current->comm,
Alan Cox46d57a42009-09-19 13:13:29 -0700864 tty_name(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{
Alan Cox46d57a42009-09-19 13:13:29 -0700883 struct uart_port *uport = state->uart_port;
884 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 unsigned int result;
886
Alan Cox46d57a42009-09-19 13:13:29 -0700887 result = uport->ops->tx_empty(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 /*
890 * If we're about to load something into the transmit
891 * register, we'll pretend the transmitter isn't empty to
892 * avoid a race condition (depending on when the transmit
893 * interrupt happens).
894 */
Alan Cox46d57a42009-09-19 13:13:29 -0700895 if (uport->x_char ||
Alan Coxebd2c8f2009-09-19 13:13:28 -0700896 ((uart_circ_chars_pending(&state->xmit) > 0) &&
Alan Cox46d57a42009-09-19 13:13:29 -0700897 !port->tty->stopped && !port->tty->hw_stopped))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 result &= ~TIOCSER_TEMT;
Alan Coxa46c9992008-02-08 04:18:53 -0800899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return put_user(result, value);
901}
902
903static int uart_tiocmget(struct tty_struct *tty, struct file *file)
904{
905 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -0700906 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 int result = -EIO;
908
Ingo Molnare2862f62006-01-13 21:37:07 +0000909 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if ((!file || !tty_hung_up_p(file)) &&
911 !(tty->flags & (1 << TTY_IO_ERROR))) {
Alan Cox46d57a42009-09-19 13:13:29 -0700912 result = uport->mctrl;
Russell Kingc5f46442005-06-29 09:42:38 +0100913
Alan Cox46d57a42009-09-19 13:13:29 -0700914 spin_lock_irq(&uport->lock);
915 result |= uport->ops->get_mctrl(uport);
916 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000918 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 return result;
921}
922
923static int
924uart_tiocmset(struct tty_struct *tty, struct file *file,
925 unsigned int set, unsigned int clear)
926{
927 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -0700928 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 int ret = -EIO;
930
Ingo Molnare2862f62006-01-13 21:37:07 +0000931 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if ((!file || !tty_hung_up_p(file)) &&
933 !(tty->flags & (1 << TTY_IO_ERROR))) {
Alan Cox46d57a42009-09-19 13:13:29 -0700934 uart_update_mctrl(uport, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 ret = 0;
936 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000937 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return ret;
939}
940
Alan Cox9e989662008-07-22 11:18:03 +0100941static int uart_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -0700944 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Ingo Molnare2862f62006-01-13 21:37:07 +0000946 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Alan Cox46d57a42009-09-19 13:13:29 -0700948 if (uport->type != PORT_UNKNOWN)
949 uport->ops->break_ctl(uport, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Ingo Molnare2862f62006-01-13 21:37:07 +0000951 mutex_unlock(&state->mutex);
Alan Cox9e989662008-07-22 11:18:03 +0100952 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
955static int uart_do_autoconfig(struct uart_state *state)
956{
Alan Cox46d57a42009-09-19 13:13:29 -0700957 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 int flags, ret;
959
960 if (!capable(CAP_SYS_ADMIN))
961 return -EPERM;
962
963 /*
964 * Take the per-port semaphore. This prevents count from
965 * changing, and hence any extra opens of the port while
966 * we're auto-configuring.
967 */
Ingo Molnare2862f62006-01-13 21:37:07 +0000968 if (mutex_lock_interruptible(&state->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return -ERESTARTSYS;
970
971 ret = -EBUSY;
972 if (uart_users(state) == 1) {
973 uart_shutdown(state);
974
975 /*
976 * If we already have a port type configured,
977 * we must release its resources.
978 */
Alan Cox46d57a42009-09-19 13:13:29 -0700979 if (uport->type != PORT_UNKNOWN)
980 uport->ops->release_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 flags = UART_CONFIG_TYPE;
Alan Cox46d57a42009-09-19 13:13:29 -0700983 if (uport->flags & UPF_AUTO_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 flags |= UART_CONFIG_IRQ;
985
986 /*
987 * This will claim the ports resources if
988 * a port is found.
989 */
Alan Cox46d57a42009-09-19 13:13:29 -0700990 uport->ops->config_port(uport, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 ret = uart_startup(state, 1);
993 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000994 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return ret;
996}
997
998/*
999 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1000 * - mask passed in arg for lines of interest
1001 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1002 * Caller should use TIOCGICOUNT to see which one it was
1003 */
1004static int
1005uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1006{
Alan Cox46d57a42009-09-19 13:13:29 -07001007 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 DECLARE_WAITQUEUE(wait, current);
1009 struct uart_icount cprev, cnow;
1010 int ret;
1011
1012 /*
1013 * note the counters on entry
1014 */
Alan Cox46d57a42009-09-19 13:13:29 -07001015 spin_lock_irq(&uport->lock);
1016 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 /*
1019 * Force modem status interrupts on
1020 */
Alan Cox46d57a42009-09-19 13:13:29 -07001021 uport->ops->enable_ms(uport);
1022 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Alan Coxebd2c8f2009-09-19 13:13:28 -07001024 add_wait_queue(&state->delta_msr_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 for (;;) {
Alan Cox46d57a42009-09-19 13:13:29 -07001026 spin_lock_irq(&uport->lock);
1027 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1028 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 set_current_state(TASK_INTERRUPTIBLE);
1031
1032 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1033 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1034 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1035 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Alan Coxa46c9992008-02-08 04:18:53 -08001036 ret = 0;
1037 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039
1040 schedule();
1041
1042 /* see if a signal did it */
1043 if (signal_pending(current)) {
1044 ret = -ERESTARTSYS;
1045 break;
1046 }
1047
1048 cprev = cnow;
1049 }
1050
1051 current->state = TASK_RUNNING;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001052 remove_wait_queue(&state->delta_msr_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 return ret;
1055}
1056
1057/*
1058 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1059 * Return: write counters to the user passed counter struct
1060 * NB: both 1->0 and 0->1 transitions are counted except for
1061 * RI where only 0->1 is counted.
1062 */
1063static int uart_get_count(struct uart_state *state,
1064 struct serial_icounter_struct __user *icnt)
1065{
1066 struct serial_icounter_struct icount;
1067 struct uart_icount cnow;
Alan Cox46d57a42009-09-19 13:13:29 -07001068 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Alan Cox46d57a42009-09-19 13:13:29 -07001070 spin_lock_irq(&uport->lock);
1071 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1072 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 icount.cts = cnow.cts;
1075 icount.dsr = cnow.dsr;
1076 icount.rng = cnow.rng;
1077 icount.dcd = cnow.dcd;
1078 icount.rx = cnow.rx;
1079 icount.tx = cnow.tx;
1080 icount.frame = cnow.frame;
1081 icount.overrun = cnow.overrun;
1082 icount.parity = cnow.parity;
1083 icount.brk = cnow.brk;
1084 icount.buf_overrun = cnow.buf_overrun;
1085
1086 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1087}
1088
1089/*
Alan Coxe5238442008-04-30 00:53:28 -07001090 * Called via sys_ioctl. We can use spin_lock_irq() here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 */
1092static int
1093uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1094 unsigned long arg)
1095{
1096 struct uart_state *state = tty->driver_data;
1097 void __user *uarg = (void __user *)arg;
1098 int ret = -ENOIOCTLCMD;
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 /*
1102 * These ioctls don't rely on the hardware to be present.
1103 */
1104 switch (cmd) {
1105 case TIOCGSERIAL:
1106 ret = uart_get_info(state, uarg);
1107 break;
1108
1109 case TIOCSSERIAL:
1110 ret = uart_set_info(state, uarg);
1111 break;
1112
1113 case TIOCSERCONFIG:
1114 ret = uart_do_autoconfig(state);
1115 break;
1116
1117 case TIOCSERGWILD: /* obsolete */
1118 case TIOCSERSWILD: /* obsolete */
1119 ret = 0;
1120 break;
1121 }
1122
1123 if (ret != -ENOIOCTLCMD)
1124 goto out;
1125
1126 if (tty->flags & (1 << TTY_IO_ERROR)) {
1127 ret = -EIO;
1128 goto out;
1129 }
1130
1131 /*
1132 * The following should only be used when hardware is present.
1133 */
1134 switch (cmd) {
1135 case TIOCMIWAIT:
1136 ret = uart_wait_modem_status(state, arg);
1137 break;
1138
1139 case TIOCGICOUNT:
1140 ret = uart_get_count(state, uarg);
1141 break;
1142 }
1143
1144 if (ret != -ENOIOCTLCMD)
1145 goto out;
1146
Ingo Molnare2862f62006-01-13 21:37:07 +00001147 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 if (tty_hung_up_p(filp)) {
1150 ret = -EIO;
1151 goto out_up;
1152 }
1153
1154 /*
1155 * All these rely on hardware being present and need to be
1156 * protected against the tty being hung up.
1157 */
1158 switch (cmd) {
1159 case TIOCSERGETLSR: /* Get line status register */
1160 ret = uart_get_lsr_info(state, uarg);
1161 break;
1162
1163 default: {
Alan Cox46d57a42009-09-19 13:13:29 -07001164 struct uart_port *uport = state->uart_port;
1165 if (uport->ops->ioctl)
1166 ret = uport->ops->ioctl(uport, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 break;
1168 }
1169 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001170out_up:
Ingo Molnare2862f62006-01-13 21:37:07 +00001171 mutex_unlock(&state->mutex);
Alan Coxf34d7a52008-04-30 00:54:13 -07001172out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return ret;
1174}
1175
Linus Torvaldsedeb2802008-06-04 10:35:03 -07001176static void uart_set_ldisc(struct tty_struct *tty)
Alan Cox64e91592008-06-03 15:18:54 +01001177{
1178 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -07001179 struct uart_port *uport = state->uart_port;
Alan Cox64e91592008-06-03 15:18:54 +01001180
Alan Cox46d57a42009-09-19 13:13:29 -07001181 if (uport->ops->set_ldisc)
1182 uport->ops->set_ldisc(uport);
Alan Cox64e91592008-06-03 15:18:54 +01001183}
1184
Alan Coxa46c9992008-02-08 04:18:53 -08001185static void uart_set_termios(struct tty_struct *tty,
1186 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 struct uart_state *state = tty->driver_data;
1189 unsigned long flags;
1190 unsigned int cflag = tty->termios->c_cflag;
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 /*
1194 * These are the bits that are used to setup various
David Woodhouse20620d62007-08-22 14:01:11 -07001195 * flags in the low level driver. We can ignore the Bfoo
1196 * bits in c_cflag; c_[io]speed will always be set
1197 * appropriately by set_termios() in tty_ioctl.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 */
1199#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if ((cflag ^ old_termios->c_cflag) == 0 &&
David Woodhouse20620d62007-08-22 14:01:11 -07001201 tty->termios->c_ospeed == old_termios->c_ospeed &&
1202 tty->termios->c_ispeed == old_termios->c_ispeed &&
Alan Coxe5238442008-04-30 00:53:28 -07001203 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return;
Alan Coxe5238442008-04-30 00:53:28 -07001205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 uart_change_speed(state, old_termios);
1208
1209 /* Handle transition to B0 status */
1210 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
Alan Coxebd2c8f2009-09-19 13:13:28 -07001211 uart_clear_mctrl(state->uart_port, TIOCM_RTS | TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 /* Handle transition away from B0 status */
1214 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1215 unsigned int mask = TIOCM_DTR;
1216 if (!(cflag & CRTSCTS) ||
1217 !test_bit(TTY_THROTTLED, &tty->flags))
1218 mask |= TIOCM_RTS;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001219 uart_set_mctrl(state->uart_port, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
1221
1222 /* Handle turning off CRTSCTS */
1223 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
Alan Coxebd2c8f2009-09-19 13:13:28 -07001224 spin_lock_irqsave(&state->uart_port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 tty->hw_stopped = 0;
1226 __uart_start(tty);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001227 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229
Russell King0dd7a1a2005-06-29 18:40:53 +01001230 /* Handle turning on CRTSCTS */
1231 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
Alan Coxebd2c8f2009-09-19 13:13:28 -07001232 spin_lock_irqsave(&state->uart_port->lock, flags);
1233 if (!(state->uart_port->ops->get_mctrl(state->uart_port) & TIOCM_CTS)) {
Russell King0dd7a1a2005-06-29 18:40:53 +01001234 tty->hw_stopped = 1;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001235 state->uart_port->ops->stop_tx(state->uart_port);
Russell King0dd7a1a2005-06-29 18:40:53 +01001236 }
Alan Coxebd2c8f2009-09-19 13:13:28 -07001237 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Russell King0dd7a1a2005-06-29 18:40:53 +01001238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239#if 0
1240 /*
1241 * No need to wake up processes in open wait, since they
1242 * sample the CLOCAL flag once, and don't recheck it.
1243 * XXX It's not clear whether the current behavior is correct
1244 * or not. Hence, this may change.....
1245 */
1246 if (!(old_termios->c_cflag & CLOCAL) &&
1247 (tty->termios->c_cflag & CLOCAL))
Alan Coxebd2c8f2009-09-19 13:13:28 -07001248 wake_up_interruptible(&state->uart_port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249#endif
1250}
1251
1252/*
1253 * In 2.4.5, calls to this will be serialized via the BKL in
1254 * linux/drivers/char/tty_io.c:tty_release()
1255 * linux/drivers/char/tty_io.c:do_tty_handup()
1256 */
1257static void uart_close(struct tty_struct *tty, struct file *filp)
1258{
1259 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -07001260 struct tty_port *port;
1261 struct uart_port *uport;
Alan Coxa46c9992008-02-08 04:18:53 -08001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 BUG_ON(!kernel_locked());
1264
Alan Cox46d57a42009-09-19 13:13:29 -07001265 uport = state->uart_port;
1266 port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Alan Cox46d57a42009-09-19 13:13:29 -07001268 pr_debug("uart_close(%d) called\n", uport->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
Alan Cox46d57a42009-09-19 13:13:29 -07001302 if (port->closing_wait != USF_CLOSING_WAIT_NONE)
1303 tty_wait_until_sent(tty, msecs_to_jiffies(port->closing_wait));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 /*
1306 * At this point, we stop accepting input. To do this, we
1307 * disable the receive line status interrupts.
1308 */
Alan Coxebd2c8f2009-09-19 13:13:28 -07001309 if (state->flags & UIF_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 unsigned long flags;
1311 spin_lock_irqsave(&port->lock, flags);
Alan Cox46d57a42009-09-19 13:13:29 -07001312 uport->ops->stop_rx(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 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 */
Alan Cox46d57a42009-09-19 13:13:29 -07001319 uart_wait_until_sent(tty, uport->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
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 Cox46d57a42009-09-19 13:13:29 -07001328 port->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Alan Cox46d57a42009-09-19 13:13:29 -07001330 if (port->blocked_open) {
1331 if (port->close_delay)
1332 msleep_interruptible(port->close_delay);
1333 } else if (!uart_console(uport)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 uart_change_pm(state, 3);
1335 }
1336
1337 /*
1338 * Wake up anyone trying to open this port.
1339 */
Alan Coxebd2c8f2009-09-19 13:13:28 -07001340 state->flags &= ~UIF_NORMAL_ACTIVE;
Alan Cox46d57a42009-09-19 13:13:29 -07001341 wake_up_interruptible(&port->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Alan Cox46d57a42009-09-19 13:13:29 -07001343done:
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;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001350 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 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 Cox46d57a42009-09-19 13:13:29 -07001415 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 BUG_ON(!kernel_locked());
Alan Coxebd2c8f2009-09-19 13:13:28 -07001418 pr_debug("uart_hangup(%d)\n", state->uart_port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Ingo Molnare2862f62006-01-13 21:37:07 +00001420 mutex_lock(&state->mutex);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001421 if (state->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 Coxebd2c8f2009-09-19 13:13:28 -07001425 state->flags &= ~UIF_NORMAL_ACTIVE;
Alan Cox46d57a42009-09-19 13:13:29 -07001426 port->tty = NULL;
1427 wake_up_interruptible(&port->open_wait);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001428 wake_up_interruptible(&state->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 Coxebd2c8f2009-09-19 13:13:28 -07001441 struct tty_struct *tty = state->port.tty;
1442 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
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 Cox46d57a42009-09-19 13:13:29 -07001476 struct uart_port *uport = state->uart_port;
1477 struct tty_port *port = &state->port;
Russell Kingc5f46442005-06-29 09:42:38 +01001478 unsigned int mctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Alan Cox46d57a42009-09-19 13:13:29 -07001480 port->blocked_open++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 state->count--;
1482
Alan Cox46d57a42009-09-19 13:13:29 -07001483 add_wait_queue(&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 Cox46d57a42009-09-19 13:13:29 -07001490 if (tty_hung_up_p(filp) || 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 */
Alan Coxebd2c8f2009-09-19 13:13:28 -07001496 if (!(state->flags & UIF_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 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 Cox46d57a42009-09-19 13:13:29 -07001509 (port->tty->termios->c_cflag & CLOCAL) ||
1510 (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 Cox46d57a42009-09-19 13:13:29 -07001518 if (port->tty->termios->c_cflag & CBAUD)
1519 uart_set_mctrl(uport, TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 /*
1522 * and wait for the carrier to indicate that the
1523 * modem is ready for us.
1524 */
Alan Cox46d57a42009-09-19 13:13:29 -07001525 spin_lock_irq(&uport->lock);
1526 uport->ops->enable_ms(uport);
1527 mctrl = uport->ops->get_mctrl(uport);
1528 spin_unlock_irq(&uport->lock);
Russell Kingc5f46442005-06-29 09:42:38 +01001529 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 Cox46d57a42009-09-19 13:13:29 -07001540 remove_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 state->count++;
Alan Cox46d57a42009-09-19 13:13:29 -07001543 port->blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 if (signal_pending(current))
1546 return -ERESTARTSYS;
1547
Alan Cox46d57a42009-09-19 13:13:29 -07001548 if (!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++;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001566 if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
Russell King68ac64c2006-04-30 11:13:50 +01001567 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
Alan Coxebd2c8f2009-09-19 13:13:28 -07001609 * be re-entered while allocating the state structure, or while we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 * 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 Coxebd2c8f2009-09-19 13:13:28 -07001612 * guarantee that state->port.tty will always contain something
1613 * reasonable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 */
1615 state = uart_get(drv, line);
1616 if (IS_ERR(state)) {
1617 retval = PTR_ERR(state);
1618 goto fail;
1619 }
1620
1621 /*
1622 * Once we set tty->driver_data here, we are guaranteed that
1623 * uart_close() will decrement the driver module use count.
1624 * Any failures from here onwards should not touch the count.
1625 */
1626 tty->driver_data = state;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001627 state->uart_port->state = state;
1628 tty->low_latency = (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 tty->alt_speed = 0;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001630 state->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 /*
1633 * If the port is in the middle of closing, bail out now.
1634 */
1635 if (tty_hung_up_p(filp)) {
1636 retval = -EAGAIN;
1637 state->count--;
Ingo Molnare2862f62006-01-13 21:37:07 +00001638 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 goto fail;
1640 }
1641
1642 /*
1643 * Make sure the device is in D0 state.
1644 */
1645 if (state->count == 1)
1646 uart_change_pm(state, 0);
1647
1648 /*
1649 * Start up the serial port.
1650 */
1651 retval = uart_startup(state, 0);
1652
1653 /*
1654 * If we succeeded, wait until the port is ready.
1655 */
1656 if (retval == 0)
1657 retval = uart_block_til_ready(filp, state);
Ingo Molnare2862f62006-01-13 21:37:07 +00001658 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 /*
1661 * If this is the first open to succeed, adjust things to suit.
1662 */
Alan Coxebd2c8f2009-09-19 13:13:28 -07001663 if (retval == 0 && !(state->flags & UIF_NORMAL_ACTIVE)) {
1664 state->flags |= UIF_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 uart_update_termios(state);
1667 }
1668
1669 fail:
1670 return retval;
1671}
1672
1673static const char *uart_type(struct uart_port *port)
1674{
1675 const char *str = NULL;
1676
1677 if (port->ops->type)
1678 str = port->ops->type(port);
1679
1680 if (!str)
1681 str = "unknown";
1682
1683 return str;
1684}
1685
1686#ifdef CONFIG_PROC_FS
1687
Alexey Dobriyand196a942009-03-31 15:19:21 -07001688static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
1690 struct uart_state *state = drv->state + i;
George G. Davis3689a0e2007-02-14 00:33:06 -08001691 int pm_state;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001692 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 char stat_buf[32];
1694 unsigned int status;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001695 int mmio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 if (!port)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001698 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001700 mmio = port->iotype >= UPIO_MEM;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001701 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 port->line, uart_type(port),
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001703 mmio ? "mmio:0x" : "port:",
Josh Boyer4f640ef2007-07-23 18:43:44 -07001704 mmio ? (unsigned long long)port->mapbase
Alan Coxa46c9992008-02-08 04:18:53 -08001705 : (unsigned long long) port->iobase,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 port->irq);
1707
1708 if (port->type == PORT_UNKNOWN) {
Alexey Dobriyand196a942009-03-31 15:19:21 -07001709 seq_putc(m, '\n');
1710 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
1712
Alan Coxa46c9992008-02-08 04:18:53 -08001713 if (capable(CAP_SYS_ADMIN)) {
George G. Davis3689a0e2007-02-14 00:33:06 -08001714 mutex_lock(&state->mutex);
1715 pm_state = state->pm_state;
1716 if (pm_state)
1717 uart_change_pm(state, 0);
Russell Kingc5f46442005-06-29 09:42:38 +01001718 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 status = port->ops->get_mctrl(port);
Russell Kingc5f46442005-06-29 09:42:38 +01001720 spin_unlock_irq(&port->lock);
George G. Davis3689a0e2007-02-14 00:33:06 -08001721 if (pm_state)
1722 uart_change_pm(state, pm_state);
1723 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Alexey Dobriyand196a942009-03-31 15:19:21 -07001725 seq_printf(m, " tx:%d rx:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 port->icount.tx, port->icount.rx);
1727 if (port->icount.frame)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001728 seq_printf(m, " fe:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 port->icount.frame);
1730 if (port->icount.parity)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001731 seq_printf(m, " pe:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 port->icount.parity);
1733 if (port->icount.brk)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001734 seq_printf(m, " brk:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 port->icount.brk);
1736 if (port->icount.overrun)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001737 seq_printf(m, " oe:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 port->icount.overrun);
Alan Coxa46c9992008-02-08 04:18:53 -08001739
1740#define INFOBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if (port->mctrl & (bit)) \
1742 strncat(stat_buf, (str), sizeof(stat_buf) - \
1743 strlen(stat_buf) - 2)
Alan Coxa46c9992008-02-08 04:18:53 -08001744#define STATBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (status & (bit)) \
1746 strncat(stat_buf, (str), sizeof(stat_buf) - \
1747 strlen(stat_buf) - 2)
1748
1749 stat_buf[0] = '\0';
1750 stat_buf[1] = '\0';
1751 INFOBIT(TIOCM_RTS, "|RTS");
1752 STATBIT(TIOCM_CTS, "|CTS");
1753 INFOBIT(TIOCM_DTR, "|DTR");
1754 STATBIT(TIOCM_DSR, "|DSR");
1755 STATBIT(TIOCM_CAR, "|CD");
1756 STATBIT(TIOCM_RNG, "|RI");
1757 if (stat_buf[0])
1758 stat_buf[0] = ' ';
Alan Coxa46c9992008-02-08 04:18:53 -08001759
Alexey Dobriyand196a942009-03-31 15:19:21 -07001760 seq_puts(m, stat_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
Alexey Dobriyand196a942009-03-31 15:19:21 -07001762 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763#undef STATBIT
1764#undef INFOBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
Alexey Dobriyand196a942009-03-31 15:19:21 -07001767static int uart_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
Alexey Dobriyan833bb302009-04-02 01:30:04 +04001769 struct tty_driver *ttydrv = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 struct uart_driver *drv = ttydrv->driver_state;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001771 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Alexey Dobriyand196a942009-03-31 15:19:21 -07001773 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 "", "", "");
Alexey Dobriyand196a942009-03-31 15:19:21 -07001775 for (i = 0; i < drv->nr; i++)
1776 uart_line_info(m, drv, i);
1777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
Alexey Dobriyand196a942009-03-31 15:19:21 -07001779
1780static int uart_proc_open(struct inode *inode, struct file *file)
1781{
1782 return single_open(file, uart_proc_show, PDE(inode)->data);
1783}
1784
1785static const struct file_operations uart_proc_fops = {
1786 .owner = THIS_MODULE,
1787 .open = uart_proc_open,
1788 .read = seq_read,
1789 .llseek = seq_lseek,
1790 .release = single_release,
1791};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792#endif
1793
Andrew Morton4a1b5502008-03-07 15:51:16 -08001794#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795/*
Russell Kingd3587882006-03-20 20:00:09 +00001796 * uart_console_write - write a console message to a serial port
1797 * @port: the port to write the message
1798 * @s: array of characters
1799 * @count: number of characters in string to write
1800 * @write: function to write character to port
1801 */
1802void uart_console_write(struct uart_port *port, const char *s,
1803 unsigned int count,
1804 void (*putchar)(struct uart_port *, int))
1805{
1806 unsigned int i;
1807
1808 for (i = 0; i < count; i++, s++) {
1809 if (*s == '\n')
1810 putchar(port, '\r');
1811 putchar(port, *s);
1812 }
1813}
1814EXPORT_SYMBOL_GPL(uart_console_write);
1815
1816/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 * Check whether an invalid uart number has been specified, and
1818 * if so, search for the first available port that does have
1819 * console support.
1820 */
1821struct uart_port * __init
1822uart_get_console(struct uart_port *ports, int nr, struct console *co)
1823{
1824 int idx = co->index;
1825
1826 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1827 ports[idx].membase == NULL))
1828 for (idx = 0; idx < nr; idx++)
1829 if (ports[idx].iobase != 0 ||
1830 ports[idx].membase != NULL)
1831 break;
1832
1833 co->index = idx;
1834
1835 return ports + idx;
1836}
1837
1838/**
1839 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1840 * @options: pointer to option string
1841 * @baud: pointer to an 'int' variable for the baud rate.
1842 * @parity: pointer to an 'int' variable for the parity.
1843 * @bits: pointer to an 'int' variable for the number of data bits.
1844 * @flow: pointer to an 'int' variable for the flow control character.
1845 *
1846 * uart_parse_options decodes a string containing the serial console
1847 * options. The format of the string is <baud><parity><bits><flow>,
1848 * eg: 115200n8r
1849 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001850void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1852{
1853 char *s = options;
1854
1855 *baud = simple_strtoul(s, NULL, 10);
1856 while (*s >= '0' && *s <= '9')
1857 s++;
1858 if (*s)
1859 *parity = *s++;
1860 if (*s)
1861 *bits = *s++ - '0';
1862 if (*s)
1863 *flow = *s;
1864}
Jason Wesself2d937f2008-04-17 20:05:37 +02001865EXPORT_SYMBOL_GPL(uart_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867struct baud_rates {
1868 unsigned int rate;
1869 unsigned int cflag;
1870};
1871
Arjan van de Vencb3592b2005-11-28 21:04:11 +00001872static const struct baud_rates baud_rates[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 { 921600, B921600 },
1874 { 460800, B460800 },
1875 { 230400, B230400 },
1876 { 115200, B115200 },
1877 { 57600, B57600 },
1878 { 38400, B38400 },
1879 { 19200, B19200 },
1880 { 9600, B9600 },
1881 { 4800, B4800 },
1882 { 2400, B2400 },
1883 { 1200, B1200 },
1884 { 0, B38400 }
1885};
1886
1887/**
1888 * uart_set_options - setup the serial console parameters
1889 * @port: pointer to the serial ports uart_port structure
1890 * @co: console pointer
1891 * @baud: baud rate
1892 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1893 * @bits: number of data bits
1894 * @flow: flow control character - 'r' (rts)
1895 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001896int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897uart_set_options(struct uart_port *port, struct console *co,
1898 int baud, int parity, int bits, int flow)
1899{
Alan Cox606d0992006-12-08 02:38:45 -08001900 struct ktermios termios;
Alan Cox149b36e2007-10-18 01:24:16 -07001901 static struct ktermios dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 int i;
1903
Russell King976ecd12005-07-03 21:05:45 +01001904 /*
1905 * Ensure that the serial console lock is initialised
1906 * early.
1907 */
1908 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07001909 lockdep_set_class(&port->lock, &port_lock_key);
Russell King976ecd12005-07-03 21:05:45 +01001910
Alan Cox606d0992006-12-08 02:38:45 -08001911 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1914
1915 /*
1916 * Construct a cflag setting.
1917 */
1918 for (i = 0; baud_rates[i].rate; i++)
1919 if (baud_rates[i].rate <= baud)
1920 break;
1921
1922 termios.c_cflag |= baud_rates[i].cflag;
1923
1924 if (bits == 7)
1925 termios.c_cflag |= CS7;
1926 else
1927 termios.c_cflag |= CS8;
1928
1929 switch (parity) {
1930 case 'o': case 'O':
1931 termios.c_cflag |= PARODD;
1932 /*fall through*/
1933 case 'e': case 'E':
1934 termios.c_cflag |= PARENB;
1935 break;
1936 }
1937
1938 if (flow == 'r')
1939 termios.c_cflag |= CRTSCTS;
1940
Yinghai Lu79492682007-07-15 23:37:25 -07001941 /*
1942 * some uarts on other side don't support no flow control.
1943 * So we set * DTR in host uart to make them happy
1944 */
1945 port->mctrl |= TIOCM_DTR;
1946
Alan Cox149b36e2007-10-18 01:24:16 -07001947 port->ops->set_termios(port, &termios, &dummy);
Jason Wesself2d937f2008-04-17 20:05:37 +02001948 /*
1949 * Allow the setting of the UART parameters with a NULL console
1950 * too:
1951 */
1952 if (co)
1953 co->cflag = termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 return 0;
1956}
Jason Wesself2d937f2008-04-17 20:05:37 +02001957EXPORT_SYMBOL_GPL(uart_set_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958#endif /* CONFIG_SERIAL_CORE_CONSOLE */
1959
1960static void uart_change_pm(struct uart_state *state, int pm_state)
1961{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001962 struct uart_port *port = state->uart_port;
Andrew Victor1281e362006-05-16 11:28:49 +01001963
1964 if (state->pm_state != pm_state) {
1965 if (port->ops->pm)
1966 port->ops->pm(port, pm_state, state->pm_state);
1967 state->pm_state = pm_state;
1968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001971struct uart_match {
1972 struct uart_port *port;
1973 struct uart_driver *driver;
1974};
1975
1976static int serial_match_port(struct device *dev, void *data)
1977{
1978 struct uart_match *match = data;
Guennadi Liakhovetski7ca796f2008-07-04 09:59:28 -07001979 struct tty_driver *tty_drv = match->driver->tty_driver;
1980 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1981 match->port->line;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001982
1983 return dev->devt == devt; /* Actually, only one tty per port */
1984}
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1987{
1988 struct uart_state *state = drv->state + port->line;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001989 struct device *tty_dev;
1990 struct uart_match match = {port, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Ingo Molnare2862f62006-01-13 21:37:07 +00001992 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001994 if (!console_suspend_enabled && uart_console(port)) {
1995 /* we're going to avoid suspending serial console */
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07001996 mutex_unlock(&state->mutex);
1997 return 0;
1998 }
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07001999
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002000 tty_dev = device_find_child(port->dev, &match, serial_match_port);
2001 if (device_may_wakeup(tty_dev)) {
2002 enable_irq_wake(port->irq);
2003 put_device(tty_dev);
2004 mutex_unlock(&state->mutex);
2005 return 0;
2006 }
2007 port->suspended = 1;
2008
Alan Coxebd2c8f2009-09-19 13:13:28 -07002009 if (state->flags & UIF_INITIALIZED) {
Russell Kingba899db2006-01-21 22:45:50 +00002010 const struct uart_ops *ops = port->ops;
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002011 int tries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Alan Coxebd2c8f2009-09-19 13:13:28 -07002013 state->flags = (state->flags & ~UIF_INITIALIZED)
Russell Kinga6b93a92006-10-01 17:17:40 +01002014 | UIF_SUSPENDED;
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 spin_lock_irq(&port->lock);
Russell Kingb129a8c2005-08-31 10:12:14 +01002017 ops->stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 ops->set_mctrl(port, 0);
2019 ops->stop_rx(port);
2020 spin_unlock_irq(&port->lock);
2021
2022 /*
2023 * Wait for the transmitter to empty.
2024 */
Alan Coxa46c9992008-02-08 04:18:53 -08002025 for (tries = 3; !ops->tx_empty(port) && tries; tries--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 msleep(10);
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002027 if (!tries)
Alan Coxa46c9992008-02-08 04:18:53 -08002028 printk(KERN_ERR "%s%s%s%d: Unable to drain "
2029 "transmitter\n",
Kay Sievers4bfe0902009-01-06 10:44:37 -08002030 port->dev ? dev_name(port->dev) : "",
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002031 port->dev ? ": " : "",
David S. Miller84408382008-10-13 10:45:26 +01002032 drv->dev_name,
2033 drv->tty_driver->name_base + port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 ops->shutdown(port);
2036 }
2037
2038 /*
2039 * Disable the console device before suspending.
2040 */
2041 if (uart_console(port))
2042 console_stop(port->cons);
2043
2044 uart_change_pm(state, 3);
2045
Ingo Molnare2862f62006-01-13 21:37:07 +00002046 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 return 0;
2049}
2050
2051int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
2052{
2053 struct uart_state *state = drv->state + port->line;
Arjan van de Ven03a74dc2008-05-23 13:04:49 -07002054 struct device *tty_dev;
2055 struct uart_match match = {port, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Ingo Molnare2862f62006-01-13 21:37:07 +00002057 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002059 if (!console_suspend_enabled && uart_console(port)) {
2060 /* no need to resume serial console, it wasn't suspended */
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002061 mutex_unlock(&state->mutex);
2062 return 0;
2063 }
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002064
Arjan van de Ven03a74dc2008-05-23 13:04:49 -07002065 tty_dev = device_find_child(port->dev, &match, serial_match_port);
2066 if (!port->suspended && device_may_wakeup(tty_dev)) {
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002067 disable_irq_wake(port->irq);
2068 mutex_unlock(&state->mutex);
2069 return 0;
2070 }
2071 port->suspended = 0;
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 /*
2074 * Re-enable the console device after suspending.
2075 */
2076 if (uart_console(port)) {
Alan Cox606d0992006-12-08 02:38:45 -08002077 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079 /*
2080 * First try to use the console cflag setting.
2081 */
Alan Cox606d0992006-12-08 02:38:45 -08002082 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 termios.c_cflag = port->cons->cflag;
2084
2085 /*
2086 * If that's unset, use the tty termios setting.
2087 */
Alan Coxebd2c8f2009-09-19 13:13:28 -07002088 if (state->port.tty && termios.c_cflag == 0)
2089 termios = *state->port.tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Russell King9d778a62008-02-04 22:27:51 -08002091 uart_change_pm(state, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 port->ops->set_termios(port, &termios, NULL);
2093 console_start(port->cons);
2094 }
2095
Alan Coxebd2c8f2009-09-19 13:13:28 -07002096 if (state->flags & UIF_SUSPENDED) {
Russell Kingba899db2006-01-21 22:45:50 +00002097 const struct uart_ops *ops = port->ops;
Russell Kingee31b332005-11-13 15:28:51 +00002098 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Russell King9d778a62008-02-04 22:27:51 -08002100 uart_change_pm(state, 0);
Alan Coxf34d7a52008-04-30 00:54:13 -07002101 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 ops->set_mctrl(port, 0);
Alan Coxf34d7a52008-04-30 00:54:13 -07002103 spin_unlock_irq(&port->lock);
Russell Kingee31b332005-11-13 15:28:51 +00002104 ret = ops->startup(port);
2105 if (ret == 0) {
2106 uart_change_speed(state, NULL);
2107 spin_lock_irq(&port->lock);
2108 ops->set_mctrl(port, port->mctrl);
2109 ops->start_tx(port);
2110 spin_unlock_irq(&port->lock);
Alan Coxebd2c8f2009-09-19 13:13:28 -07002111 state->flags |= UIF_INITIALIZED;
Russell Kingee31b332005-11-13 15:28:51 +00002112 } else {
2113 /*
2114 * Failed to resume - maybe hardware went away?
2115 * Clear the "initialized" flag so we won't try
2116 * to call the low level drivers shutdown method.
2117 */
Russell Kingee31b332005-11-13 15:28:51 +00002118 uart_shutdown(state);
2119 }
Russell Kinga6b93a92006-10-01 17:17:40 +01002120
Alan Coxebd2c8f2009-09-19 13:13:28 -07002121 state->flags &= ~UIF_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 }
2123
Ingo Molnare2862f62006-01-13 21:37:07 +00002124 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 return 0;
2127}
2128
2129static inline void
2130uart_report_port(struct uart_driver *drv, struct uart_port *port)
2131{
Russell King30b7a3b2005-09-03 15:30:21 +01002132 char address[64];
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 switch (port->iotype) {
2135 case UPIO_PORT:
Andrew Morton9bde10a2008-10-13 10:35:42 +01002136 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 break;
2138 case UPIO_HUB6:
Russell King30b7a3b2005-09-03 15:30:21 +01002139 snprintf(address, sizeof(address),
Andrew Morton9bde10a2008-10-13 10:35:42 +01002140 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 break;
2142 case UPIO_MEM:
2143 case UPIO_MEM32:
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002144 case UPIO_AU:
Zang Roy-r619113be91ec2006-06-30 02:29:58 -07002145 case UPIO_TSI:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002146 case UPIO_DWAPB:
Russell King30b7a3b2005-09-03 15:30:21 +01002147 snprintf(address, sizeof(address),
Josh Boyer4f640ef2007-07-23 18:43:44 -07002148 "MMIO 0x%llx", (unsigned long long)port->mapbase);
Russell King30b7a3b2005-09-03 15:30:21 +01002149 break;
2150 default:
2151 strlcpy(address, "*unknown*", sizeof(address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 break;
2153 }
Russell King30b7a3b2005-09-03 15:30:21 +01002154
Russell King0cf669d2005-10-31 11:42:22 +00002155 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
Kay Sievers4bfe0902009-01-06 10:44:37 -08002156 port->dev ? dev_name(port->dev) : "",
Russell King0cf669d2005-10-31 11:42:22 +00002157 port->dev ? ": " : "",
David S. Miller84408382008-10-13 10:45:26 +01002158 drv->dev_name,
2159 drv->tty_driver->name_base + port->line,
2160 address, port->irq, uart_type(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
2163static void
2164uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2165 struct uart_port *port)
2166{
2167 unsigned int flags;
2168
2169 /*
2170 * If there isn't a port here, don't do anything further.
2171 */
2172 if (!port->iobase && !port->mapbase && !port->membase)
2173 return;
2174
2175 /*
2176 * Now do the auto configuration stuff. Note that config_port
2177 * is expected to claim the resources and map the port for us.
2178 */
David Daney8e23fcc2009-01-02 13:49:54 +00002179 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (port->flags & UPF_AUTO_IRQ)
2181 flags |= UART_CONFIG_IRQ;
2182 if (port->flags & UPF_BOOT_AUTOCONF) {
David Daney8e23fcc2009-01-02 13:49:54 +00002183 if (!(port->flags & UPF_FIXED_TYPE)) {
2184 port->type = PORT_UNKNOWN;
2185 flags |= UART_CONFIG_TYPE;
2186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 port->ops->config_port(port, flags);
2188 }
2189
2190 if (port->type != PORT_UNKNOWN) {
2191 unsigned long flags;
2192
2193 uart_report_port(drv, port);
2194
George G. Davis3689a0e2007-02-14 00:33:06 -08002195 /* Power up port for set_mctrl() */
2196 uart_change_pm(state, 0);
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 /*
2199 * Ensure that the modem control lines are de-activated.
Yinghai Luc3e46422008-02-04 22:27:46 -08002200 * keep the DTR setting that is set in uart_set_options()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 * We probably don't need a spinlock around this, but
2202 */
2203 spin_lock_irqsave(&port->lock, flags);
Yinghai Luc3e46422008-02-04 22:27:46 -08002204 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 spin_unlock_irqrestore(&port->lock, flags);
2206
2207 /*
Russell King97d97222007-09-01 21:25:09 +01002208 * If this driver supports console, and it hasn't been
2209 * successfully registered yet, try to re-register it.
2210 * It may be that the port was not available.
2211 */
2212 if (port->cons && !(port->cons->flags & CON_ENABLED))
2213 register_console(port->cons);
2214
2215 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 * Power down all ports by default, except the
2217 * console if we have one.
2218 */
2219 if (!uart_console(port))
2220 uart_change_pm(state, 3);
2221 }
2222}
2223
Jason Wesself2d937f2008-04-17 20:05:37 +02002224#ifdef CONFIG_CONSOLE_POLL
2225
2226static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2227{
2228 struct uart_driver *drv = driver->driver_state;
2229 struct uart_state *state = drv->state + line;
2230 struct uart_port *port;
2231 int baud = 9600;
2232 int bits = 8;
2233 int parity = 'n';
2234 int flow = 'n';
2235
Alan Coxebd2c8f2009-09-19 13:13:28 -07002236 if (!state || !state->uart_port)
Jason Wesself2d937f2008-04-17 20:05:37 +02002237 return -1;
2238
Alan Coxebd2c8f2009-09-19 13:13:28 -07002239 port = state->uart_port;
Jason Wesself2d937f2008-04-17 20:05:37 +02002240 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2241 return -1;
2242
2243 if (options) {
2244 uart_parse_options(options, &baud, &parity, &bits, &flow);
2245 return uart_set_options(port, NULL, baud, parity, bits, flow);
2246 }
2247
2248 return 0;
2249}
2250
2251static int uart_poll_get_char(struct tty_driver *driver, int line)
2252{
2253 struct uart_driver *drv = driver->driver_state;
2254 struct uart_state *state = drv->state + line;
2255 struct uart_port *port;
2256
Alan Coxebd2c8f2009-09-19 13:13:28 -07002257 if (!state || !state->uart_port)
Jason Wesself2d937f2008-04-17 20:05:37 +02002258 return -1;
2259
Alan Coxebd2c8f2009-09-19 13:13:28 -07002260 port = state->uart_port;
Jason Wesself2d937f2008-04-17 20:05:37 +02002261 return port->ops->poll_get_char(port);
2262}
2263
2264static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2265{
2266 struct uart_driver *drv = driver->driver_state;
2267 struct uart_state *state = drv->state + line;
2268 struct uart_port *port;
2269
Alan Coxebd2c8f2009-09-19 13:13:28 -07002270 if (!state || !state->uart_port)
Jason Wesself2d937f2008-04-17 20:05:37 +02002271 return;
2272
Alan Coxebd2c8f2009-09-19 13:13:28 -07002273 port = state->uart_port;
Jason Wesself2d937f2008-04-17 20:05:37 +02002274 port->ops->poll_put_char(port, ch);
2275}
2276#endif
2277
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002278static const struct tty_operations uart_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 .open = uart_open,
2280 .close = uart_close,
2281 .write = uart_write,
2282 .put_char = uart_put_char,
2283 .flush_chars = uart_flush_chars,
2284 .write_room = uart_write_room,
2285 .chars_in_buffer= uart_chars_in_buffer,
2286 .flush_buffer = uart_flush_buffer,
2287 .ioctl = uart_ioctl,
2288 .throttle = uart_throttle,
2289 .unthrottle = uart_unthrottle,
2290 .send_xchar = uart_send_xchar,
2291 .set_termios = uart_set_termios,
Alan Cox64e91592008-06-03 15:18:54 +01002292 .set_ldisc = uart_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 .stop = uart_stop,
2294 .start = uart_start,
2295 .hangup = uart_hangup,
2296 .break_ctl = uart_break_ctl,
2297 .wait_until_sent= uart_wait_until_sent,
2298#ifdef CONFIG_PROC_FS
Alexey Dobriyand196a942009-03-31 15:19:21 -07002299 .proc_fops = &uart_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300#endif
2301 .tiocmget = uart_tiocmget,
2302 .tiocmset = uart_tiocmset,
Jason Wesself2d937f2008-04-17 20:05:37 +02002303#ifdef CONFIG_CONSOLE_POLL
2304 .poll_init = uart_poll_init,
2305 .poll_get_char = uart_poll_get_char,
2306 .poll_put_char = uart_poll_put_char,
2307#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308};
2309
2310/**
2311 * uart_register_driver - register a driver with the uart core layer
2312 * @drv: low level driver structure
2313 *
2314 * Register a uart driver with the core driver. We in turn register
2315 * with the tty layer, and initialise the core driver per-port state.
2316 *
2317 * We have a proc file in /proc/tty/driver which is named after the
2318 * normal driver.
2319 *
2320 * drv->port should be NULL, and the per-port structures should be
2321 * registered using uart_add_one_port after this call has succeeded.
2322 */
2323int uart_register_driver(struct uart_driver *drv)
2324{
2325 struct tty_driver *normal = NULL;
2326 int i, retval;
2327
2328 BUG_ON(drv->state);
2329
2330 /*
2331 * Maybe we should be using a slab cache for this, especially if
2332 * we have a large number of ports to handle.
2333 */
Burman Yan8f31bb32007-02-14 00:33:07 -08002334 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 retval = -ENOMEM;
2336 if (!drv->state)
2337 goto out;
2338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 normal = alloc_tty_driver(drv->nr);
2340 if (!normal)
2341 goto out;
2342
2343 drv->tty_driver = normal;
2344
2345 normal->owner = drv->owner;
2346 normal->driver_name = drv->driver_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 normal->name = drv->dev_name;
2348 normal->major = drv->major;
2349 normal->minor_start = drv->minor;
2350 normal->type = TTY_DRIVER_TYPE_SERIAL;
2351 normal->subtype = SERIAL_TYPE_NORMAL;
2352 normal->init_termios = tty_std_termios;
2353 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -08002354 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07002355 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 normal->driver_state = drv;
2357 tty_set_operations(normal, &uart_ops);
2358
2359 /*
2360 * Initialise the UART state(s).
2361 */
2362 for (i = 0; i < drv->nr; i++) {
2363 struct uart_state *state = drv->state + i;
2364
Ingo Molnare2862f62006-01-13 21:37:07 +00002365 mutex_init(&state->mutex);
Alan Coxf7519282009-01-02 13:49:21 +00002366
Alan Coxebd2c8f2009-09-19 13:13:28 -07002367 tty_port_init(&state->port);
Alan Cox5e99df52009-09-19 13:13:28 -07002368 state->port.close_delay = 500; /* .5 seconds */
2369 state->port.closing_wait = 30000; /* 30 seconds */
Alan Coxebd2c8f2009-09-19 13:13:28 -07002370 init_waitqueue_head(&state->delta_msr_wait);
2371 tasklet_init(&state->tlet, uart_tasklet_action,
Alan Coxf7519282009-01-02 13:49:21 +00002372 (unsigned long)state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
2374
2375 retval = tty_register_driver(normal);
2376 out:
2377 if (retval < 0) {
2378 put_tty_driver(normal);
2379 kfree(drv->state);
2380 }
2381 return retval;
2382}
2383
2384/**
2385 * uart_unregister_driver - remove a driver from the uart core layer
2386 * @drv: low level driver structure
2387 *
2388 * Remove all references to a driver from the core driver. The low
2389 * level driver must have removed all its ports via the
2390 * uart_remove_one_port() if it registered them with uart_add_one_port().
2391 * (ie, drv->port == NULL)
2392 */
2393void uart_unregister_driver(struct uart_driver *drv)
2394{
2395 struct tty_driver *p = drv->tty_driver;
2396 tty_unregister_driver(p);
2397 put_tty_driver(p);
2398 kfree(drv->state);
2399 drv->tty_driver = NULL;
2400}
2401
2402struct tty_driver *uart_console_device(struct console *co, int *index)
2403{
2404 struct uart_driver *p = co->data;
2405 *index = co->index;
2406 return p->tty_driver;
2407}
2408
2409/**
2410 * uart_add_one_port - attach a driver-defined port structure
2411 * @drv: pointer to the uart low level driver structure for this port
2412 * @port: uart port structure to use for this port.
2413 *
2414 * This allows the driver to register its own uart_port structure
2415 * with the core driver. The main purpose is to allow the low
2416 * level uart drivers to expand uart_port, rather than having yet
2417 * more levels of structures.
2418 */
2419int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2420{
2421 struct uart_state *state;
2422 int ret = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002423 struct device *tty_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 BUG_ON(in_interrupt());
2426
2427 if (port->line >= drv->nr)
2428 return -EINVAL;
2429
2430 state = drv->state + port->line;
2431
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002432 mutex_lock(&port_mutex);
Russell King68ac64c2006-04-30 11:13:50 +01002433 mutex_lock(&state->mutex);
Alan Coxebd2c8f2009-09-19 13:13:28 -07002434 if (state->uart_port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 ret = -EINVAL;
2436 goto out;
2437 }
2438
Alan Coxebd2c8f2009-09-19 13:13:28 -07002439 state->uart_port = port;
Russell King97d97222007-09-01 21:25:09 +01002440 state->pm_state = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 port->cons = drv->cons;
Alan Coxebd2c8f2009-09-19 13:13:28 -07002443 port->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Russell King976ecd12005-07-03 21:05:45 +01002445 /*
2446 * If this port is a console, then the spinlock is already
2447 * initialised.
2448 */
Ingo Molnar13e83592006-07-03 00:25:03 -07002449 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
Russell King976ecd12005-07-03 21:05:45 +01002450 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07002451 lockdep_set_class(&port->lock, &port_lock_key);
2452 }
Russell King976ecd12005-07-03 21:05:45 +01002453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 uart_configure_port(drv, state, port);
2455
2456 /*
2457 * Register the port whether it's detected or not. This allows
2458 * setserial to be used to alter this ports parameters.
2459 */
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002460 tty_dev = tty_register_device(drv->tty_driver, port->line, port->dev);
2461 if (likely(!IS_ERR(tty_dev))) {
Alan Stern74081f82008-03-19 22:35:13 +01002462 device_init_wakeup(tty_dev, 1);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002463 device_set_wakeup_enable(tty_dev, 0);
2464 } else
2465 printk(KERN_ERR "Cannot register tty device on line %d\n",
2466 port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 /*
Russell King68ac64c2006-04-30 11:13:50 +01002469 * Ensure UPF_DEAD is not set.
2470 */
2471 port->flags &= ~UPF_DEAD;
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 out:
Russell King68ac64c2006-04-30 11:13:50 +01002474 mutex_unlock(&state->mutex);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002475 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 return ret;
2478}
2479
2480/**
2481 * uart_remove_one_port - detach a driver defined port structure
2482 * @drv: pointer to the uart low level driver structure for this port
2483 * @port: uart port structure for this port
2484 *
2485 * This unhooks (and hangs up) the specified port structure from the
2486 * core driver. No further calls will be made to the low-level code
2487 * for this port.
2488 */
2489int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2490{
2491 struct uart_state *state = drv->state + port->line;
2492
2493 BUG_ON(in_interrupt());
2494
Alan Coxebd2c8f2009-09-19 13:13:28 -07002495 if (state->uart_port != port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
Alan Coxebd2c8f2009-09-19 13:13:28 -07002497 state->uart_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
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 Coxebd2c8f2009-09-19 13:13:28 -07002514 if (state->port.tty)
2515 tty_vhangup(state->port.tty);
Russell King68ac64c2006-04-30 11:13:50 +01002516
2517 /*
Russell King68ac64c2006-04-30 11:13:50 +01002518 * Free the port IO and memory resources, if any.
2519 */
2520 if (port->type != PORT_UNKNOWN)
2521 port->ops->release_port(port);
2522
2523 /*
2524 * Indicate that there isn't a port here anymore.
2525 */
2526 port->type = PORT_UNKNOWN;
2527
2528 /*
2529 * Kill the tasklet, and free resources.
2530 */
Alan Coxebd2c8f2009-09-19 13:13:28 -07002531 tasklet_kill(&state->tlet);
Russell King68ac64c2006-04-30 11:13:50 +01002532
Alan Coxebd2c8f2009-09-19 13:13:28 -07002533 state->uart_port = NULL;
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002534 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 return 0;
2537}
2538
2539/*
2540 * Are the two ports equivalent?
2541 */
2542int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2543{
2544 if (port1->iotype != port2->iotype)
2545 return 0;
2546
2547 switch (port1->iotype) {
2548 case UPIO_PORT:
2549 return (port1->iobase == port2->iobase);
2550 case UPIO_HUB6:
2551 return (port1->iobase == port2->iobase) &&
2552 (port1->hub6 == port2->hub6);
2553 case UPIO_MEM:
Sergei Shtylyovd21b55d2006-08-28 19:49:03 +04002554 case UPIO_MEM32:
2555 case UPIO_AU:
2556 case UPIO_TSI:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002557 case UPIO_DWAPB:
Benjamin Herrenschmidt1624f002006-01-04 18:09:44 +00002558 return (port1->mapbase == port2->mapbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 }
2560 return 0;
2561}
2562EXPORT_SYMBOL(uart_match_port);
2563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564EXPORT_SYMBOL(uart_write_wakeup);
2565EXPORT_SYMBOL(uart_register_driver);
2566EXPORT_SYMBOL(uart_unregister_driver);
2567EXPORT_SYMBOL(uart_suspend_port);
2568EXPORT_SYMBOL(uart_resume_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569EXPORT_SYMBOL(uart_add_one_port);
2570EXPORT_SYMBOL(uart_remove_one_port);
2571
2572MODULE_DESCRIPTION("Serial driver core");
2573MODULE_LICENSE("GPL");