blob: 744dec9301fbe1a8c76c0991d2de88b2f8534b27 [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/smp_lock.h>
33#include <linux/device.h>
34#include <linux/serial.h> /* for serial_state and serial_icounter_struct */
Alan Coxccce6de2009-09-19 13:13:30 -070035#include <linux/serial_core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#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 Cox91312cd2009-09-19 13:13:29 -070055#define uart_users(state) ((state)->port.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 Coxccce6de2009-09-19 13:13:30 -0700149 if (port->flags & ASYNC_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 Coxccce6de2009-09-19 13:13:30 -0700192 if (port->flags & ASYNC_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 Coxccce6de2009-09-19 13:13:30 -0700199 set_bit(ASYNCB_INITIALIZED, &port->flags);
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 Coxccce6de2009-09-19 13:13:30 -0700217 struct uart_port *uport = state->uart_port;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700218 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 Coxccce6de2009-09-19 13:13:30 -0700226 if (test_and_clear_bit(ASYNCB_INITIALIZED, &state->port.flags)) {
Russell Kingee31b332005-11-13 15:28:51 +0000227 /*
228 * Turn off DTR and RTS early.
229 */
Alan Coxf7519282009-01-02 13:49:21 +0000230 if (!tty || (tty->termios->c_cflag & HUPCL))
Alan Coxccce6de2009-09-19 13:13:30 -0700231 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
Russell Kingee31b332005-11-13 15:28:51 +0000232
233 /*
234 * clear delta_msr_wait queue to avoid mem leaks: we may free
235 * the irq here so the queue might never be woken up. Note
236 * that we won't end up waiting on delta_msr_wait again since
237 * any outstanding file descriptors should be pointing at
238 * hung_up_tty_fops now.
239 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700240 wake_up_interruptible(&state->delta_msr_wait);
Russell Kingee31b332005-11-13 15:28:51 +0000241
242 /*
243 * Free the IRQ and disable the port.
244 */
Alan Coxccce6de2009-09-19 13:13:30 -0700245 uport->ops->shutdown(uport);
Russell Kingee31b332005-11-13 15:28:51 +0000246
247 /*
248 * Ensure that the IRQ handler isn't running on another CPU.
249 */
Alan Coxccce6de2009-09-19 13:13:30 -0700250 synchronize_irq(uport->irq);
Russell Kingee31b332005-11-13 15:28:51 +0000251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /*
Russell Kingee31b332005-11-13 15:28:51 +0000254 * kill off our tasklet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700256 tasklet_kill(&state->tlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /*
259 * Free the transmit buffer page.
260 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700261 if (state->xmit.buf) {
262 free_page((unsigned long)state->xmit.buf);
263 state->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/**
268 * uart_update_timeout - update per-port FIFO timeout.
269 * @port: uart_port structure describing the port
270 * @cflag: termios cflag value
271 * @baud: speed of the port
272 *
273 * Set the port FIFO timeout value. The @cflag value should
274 * reflect the actual hardware settings.
275 */
276void
277uart_update_timeout(struct uart_port *port, unsigned int cflag,
278 unsigned int baud)
279{
280 unsigned int bits;
281
282 /* byte size and parity */
283 switch (cflag & CSIZE) {
284 case CS5:
285 bits = 7;
286 break;
287 case CS6:
288 bits = 8;
289 break;
290 case CS7:
291 bits = 9;
292 break;
293 default:
294 bits = 10;
Alan Coxa46c9992008-02-08 04:18:53 -0800295 break; /* CS8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297
298 if (cflag & CSTOPB)
299 bits++;
300 if (cflag & PARENB)
301 bits++;
302
303 /*
304 * The total number of bits to be transmitted in the fifo.
305 */
306 bits = bits * port->fifosize;
307
308 /*
309 * Figure the timeout to send the above number of bits.
310 * Add .02 seconds of slop
311 */
312 port->timeout = (HZ * bits) / baud + HZ/50;
313}
314
315EXPORT_SYMBOL(uart_update_timeout);
316
317/**
318 * uart_get_baud_rate - return baud rate for a particular port
319 * @port: uart_port structure describing the port in question.
320 * @termios: desired termios settings.
321 * @old: old termios (or NULL)
322 * @min: minimum acceptable baud rate
323 * @max: maximum acceptable baud rate
324 *
325 * Decode the termios structure into a numeric baud rate,
326 * taking account of the magic 38400 baud rate (with spd_*
327 * flags), and mapping the %B0 rate to 9600 baud.
328 *
329 * If the new baud rate is invalid, try the old termios setting.
330 * If it's still invalid, we try 9600 baud.
331 *
332 * Update the @termios structure to reflect the baud rate
Alan Coxeb424fd2008-04-28 02:14:07 -0700333 * we're actually going to be using. Don't do this for the case
334 * where B0 is requested ("hang up").
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
336unsigned int
Alan Cox606d0992006-12-08 02:38:45 -0800337uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
338 struct ktermios *old, unsigned int min, unsigned int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 unsigned int try, baud, altbaud = 38400;
Alan Coxeb424fd2008-04-28 02:14:07 -0700341 int hung_up = 0;
Russell King0077d452006-01-21 23:03:28 +0000342 upf_t flags = port->flags & UPF_SPD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (flags == UPF_SPD_HI)
345 altbaud = 57600;
346 if (flags == UPF_SPD_VHI)
347 altbaud = 115200;
348 if (flags == UPF_SPD_SHI)
349 altbaud = 230400;
350 if (flags == UPF_SPD_WARP)
351 altbaud = 460800;
352
353 for (try = 0; try < 2; try++) {
354 baud = tty_termios_baud_rate(termios);
355
356 /*
357 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
358 * Die! Die! Die!
359 */
360 if (baud == 38400)
361 baud = altbaud;
362
363 /*
364 * Special case: B0 rate.
365 */
Alan Coxeb424fd2008-04-28 02:14:07 -0700366 if (baud == 0) {
367 hung_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 baud = 9600;
Alan Coxeb424fd2008-04-28 02:14:07 -0700369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 if (baud >= min && baud <= max)
372 return baud;
373
374 /*
375 * Oops, the quotient was zero. Try again with
376 * the old baud rate if possible.
377 */
378 termios->c_cflag &= ~CBAUD;
379 if (old) {
Alan Cox6d4d67b2008-02-04 22:27:53 -0800380 baud = tty_termios_baud_rate(old);
Alan Coxeb424fd2008-04-28 02:14:07 -0700381 if (!hung_up)
382 tty_termios_encode_baud_rate(termios,
383 baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 old = NULL;
385 continue;
386 }
387
388 /*
389 * As a last resort, if the quotient is zero,
390 * default to 9600 bps
391 */
Alan Coxeb424fd2008-04-28 02:14:07 -0700392 if (!hung_up)
393 tty_termios_encode_baud_rate(termios, 9600, 9600);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
396 return 0;
397}
398
399EXPORT_SYMBOL(uart_get_baud_rate);
400
401/**
402 * uart_get_divisor - return uart clock divisor
403 * @port: uart_port structure describing the port.
404 * @baud: desired baud rate
405 *
406 * Calculate the uart clock divisor for the port.
407 */
408unsigned int
409uart_get_divisor(struct uart_port *port, unsigned int baud)
410{
411 unsigned int quot;
412
413 /*
414 * Old custom speed handling.
415 */
416 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
417 quot = port->custom_divisor;
418 else
419 quot = (port->uartclk + (8 * baud)) / (16 * baud);
420
421 return quot;
422}
423
424EXPORT_SYMBOL(uart_get_divisor);
425
Alan Cox23d22ce2008-04-30 00:54:11 -0700426/* FIXME: Consistent locking policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427static void
Alan Cox606d0992006-12-08 02:38:45 -0800428uart_change_speed(struct uart_state *state, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Alan Coxccce6de2009-09-19 13:13:30 -0700430 struct tty_port *port = &state->port;
431 struct tty_struct *tty = port->tty;
432 struct uart_port *uport = state->uart_port;
Alan Cox606d0992006-12-08 02:38:45 -0800433 struct ktermios *termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /*
436 * If we have no tty, termios, or the port does not exist,
437 * then we can't set the parameters for this port.
438 */
Alan Coxccce6de2009-09-19 13:13:30 -0700439 if (!tty || !tty->termios || uport->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return;
441
442 termios = tty->termios;
443
444 /*
445 * Set flags based on termios cflag
446 */
447 if (termios->c_cflag & CRTSCTS)
Alan Coxccce6de2009-09-19 13:13:30 -0700448 set_bit(ASYNCB_CTS_FLOW, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 else
Alan Coxccce6de2009-09-19 13:13:30 -0700450 clear_bit(ASYNCB_CTS_FLOW, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 if (termios->c_cflag & CLOCAL)
Alan Coxccce6de2009-09-19 13:13:30 -0700453 clear_bit(ASYNCB_CHECK_CD, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 else
Alan Coxccce6de2009-09-19 13:13:30 -0700455 set_bit(ASYNCB_CHECK_CD, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Alan Coxccce6de2009-09-19 13:13:30 -0700457 uport->ops->set_termios(uport, termios, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Alan Cox23d22ce2008-04-30 00:54:11 -0700460static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461__uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
462{
463 unsigned long flags;
Alan Cox23d22ce2008-04-30 00:54:11 -0700464 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 if (!circ->buf)
Alan Cox23d22ce2008-04-30 00:54:11 -0700467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 spin_lock_irqsave(&port->lock, flags);
470 if (uart_circ_chars_free(circ) != 0) {
471 circ->buf[circ->head] = c;
472 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
Alan Cox23d22ce2008-04-30 00:54:11 -0700473 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475 spin_unlock_irqrestore(&port->lock, flags);
Alan Cox23d22ce2008-04-30 00:54:11 -0700476 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Alan Cox23d22ce2008-04-30 00:54:11 -0700479static int uart_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 struct uart_state *state = tty->driver_data;
482
Alan Coxebd2c8f2009-09-19 13:13:28 -0700483 return __uart_put_char(state->uart_port, &state->xmit, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486static void uart_flush_chars(struct tty_struct *tty)
487{
488 uart_start(tty);
489}
490
491static int
Pavel Machekd5f735e2006-03-07 21:55:20 -0800492uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 struct uart_state *state = tty->driver_data;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800495 struct uart_port *port;
496 struct circ_buf *circ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 unsigned long flags;
498 int c, ret = 0;
499
Pavel Machekd5f735e2006-03-07 21:55:20 -0800500 /*
501 * This means you called this function _after_ the port was
502 * closed. No cookie for you.
503 */
Alan Coxf7519282009-01-02 13:49:21 +0000504 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800505 WARN_ON(1);
506 return -EL3HLT;
507 }
508
Alan Coxebd2c8f2009-09-19 13:13:28 -0700509 port = state->uart_port;
510 circ = &state->xmit;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (!circ->buf)
513 return 0;
514
515 spin_lock_irqsave(&port->lock, flags);
516 while (1) {
517 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
518 if (count < c)
519 c = count;
520 if (c <= 0)
521 break;
522 memcpy(circ->buf + circ->head, buf, c);
523 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
524 buf += c;
525 count -= c;
526 ret += c;
527 }
528 spin_unlock_irqrestore(&port->lock, flags);
529
530 uart_start(tty);
531 return ret;
532}
533
534static int uart_write_room(struct tty_struct *tty)
535{
536 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700537 unsigned long flags;
538 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Alan Coxebd2c8f2009-09-19 13:13:28 -0700540 spin_lock_irqsave(&state->uart_port->lock, flags);
541 ret = uart_circ_chars_free(&state->xmit);
542 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700543 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
546static int uart_chars_in_buffer(struct tty_struct *tty)
547{
548 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700549 unsigned long flags;
550 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Alan Coxebd2c8f2009-09-19 13:13:28 -0700552 spin_lock_irqsave(&state->uart_port->lock, flags);
553 ret = uart_circ_chars_pending(&state->xmit);
554 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700555 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
558static void uart_flush_buffer(struct tty_struct *tty)
559{
560 struct uart_state *state = tty->driver_data;
Tetsuo Handa55d7b682008-05-06 20:42:27 -0700561 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 unsigned long flags;
563
Pavel Machekd5f735e2006-03-07 21:55:20 -0800564 /*
565 * This means you called this function _after_ the port was
566 * closed. No cookie for you.
567 */
Alan Coxf7519282009-01-02 13:49:21 +0000568 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800569 WARN_ON(1);
570 return;
571 }
572
Alan Coxebd2c8f2009-09-19 13:13:28 -0700573 port = state->uart_port;
Jiri Slabyeb3a1e12007-05-06 14:48:52 -0700574 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 spin_lock_irqsave(&port->lock, flags);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700577 uart_circ_clear(&state->xmit);
Haavard Skinnemoen6bb0e3a2008-07-16 21:52:36 +0100578 if (port->ops->flush_buffer)
579 port->ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 spin_unlock_irqrestore(&port->lock, flags);
581 tty_wakeup(tty);
582}
583
584/*
585 * This function is used to send a high-priority XON/XOFF character to
586 * the device
587 */
588static void uart_send_xchar(struct tty_struct *tty, char ch)
589{
590 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700591 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 unsigned long flags;
593
594 if (port->ops->send_xchar)
595 port->ops->send_xchar(port, ch);
596 else {
597 port->x_char = ch;
598 if (ch) {
599 spin_lock_irqsave(&port->lock, flags);
Russell Kingb129a8c2005-08-31 10:12:14 +0100600 port->ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 spin_unlock_irqrestore(&port->lock, flags);
602 }
603 }
604}
605
606static void uart_throttle(struct tty_struct *tty)
607{
608 struct uart_state *state = tty->driver_data;
609
610 if (I_IXOFF(tty))
611 uart_send_xchar(tty, STOP_CHAR(tty));
612
613 if (tty->termios->c_cflag & CRTSCTS)
Alan Coxebd2c8f2009-09-19 13:13:28 -0700614 uart_clear_mctrl(state->uart_port, TIOCM_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617static void uart_unthrottle(struct tty_struct *tty)
618{
619 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700620 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 if (I_IXOFF(tty)) {
623 if (port->x_char)
624 port->x_char = 0;
625 else
626 uart_send_xchar(tty, START_CHAR(tty));
627 }
628
629 if (tty->termios->c_cflag & CRTSCTS)
630 uart_set_mctrl(port, TIOCM_RTS);
631}
632
633static int uart_get_info(struct uart_state *state,
634 struct serial_struct __user *retinfo)
635{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700636 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 struct serial_struct tmp;
638
639 memset(&tmp, 0, sizeof(tmp));
Alan Coxf34d7a52008-04-30 00:54:13 -0700640
641 /* Ensure the state we copy is consistent and no hardware changes
642 occur as we go */
643 mutex_lock(&state->mutex);
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 tmp.type = port->type;
646 tmp.line = port->line;
647 tmp.port = port->iobase;
648 if (HIGH_BITS_OFFSET)
649 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
650 tmp.irq = port->irq;
651 tmp.flags = port->flags;
652 tmp.xmit_fifo_size = port->fifosize;
653 tmp.baud_base = port->uartclk / 16;
Alan Cox5e99df52009-09-19 13:13:28 -0700654 tmp.close_delay = state->port.close_delay / 10;
655 tmp.closing_wait = state->port.closing_wait == USF_CLOSING_WAIT_NONE ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 ASYNC_CLOSING_WAIT_NONE :
Alan Cox5e99df52009-09-19 13:13:28 -0700657 state->port.closing_wait / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 tmp.custom_divisor = port->custom_divisor;
659 tmp.hub6 = port->hub6;
660 tmp.io_type = port->iotype;
661 tmp.iomem_reg_shift = port->regshift;
Josh Boyer4f640ef2007-07-23 18:43:44 -0700662 tmp.iomem_base = (void *)(unsigned long)port->mapbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Alan Coxf34d7a52008-04-30 00:54:13 -0700664 mutex_unlock(&state->mutex);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
667 return -EFAULT;
668 return 0;
669}
670
671static int uart_set_info(struct uart_state *state,
672 struct serial_struct __user *newinfo)
673{
674 struct serial_struct new_serial;
Alan Cox46d57a42009-09-19 13:13:29 -0700675 struct uart_port *uport = state->uart_port;
676 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 unsigned long new_port;
Russell King0077d452006-01-21 23:03:28 +0000678 unsigned int change_irq, change_port, closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 unsigned int old_custom_divisor, close_delay;
Russell King0077d452006-01-21 23:03:28 +0000680 upf_t old_flags, new_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int retval = 0;
682
683 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
684 return -EFAULT;
685
686 new_port = new_serial.port;
687 if (HIGH_BITS_OFFSET)
688 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
689
690 new_serial.irq = irq_canonicalize(new_serial.irq);
691 close_delay = new_serial.close_delay * 10;
692 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
693 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
694
695 /*
Alan Cox91312cd2009-09-19 13:13:29 -0700696 * This semaphore protects port->count. It is also
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 * very useful to prevent opens. Also, take the
698 * port configuration semaphore to make sure that a
699 * module insertion/removal doesn't change anything
700 * under us.
701 */
Ingo Molnare2862f62006-01-13 21:37:07 +0000702 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Alan Cox46d57a42009-09-19 13:13:29 -0700704 change_irq = !(uport->flags & UPF_FIXED_PORT)
705 && new_serial.irq != uport->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 /*
708 * Since changing the 'type' of the port changes its resource
709 * allocations, we should treat type changes the same as
710 * IO port changes.
711 */
Alan Cox46d57a42009-09-19 13:13:29 -0700712 change_port = !(uport->flags & UPF_FIXED_PORT)
713 && (new_port != uport->iobase ||
714 (unsigned long)new_serial.iomem_base != uport->mapbase ||
715 new_serial.hub6 != uport->hub6 ||
716 new_serial.io_type != uport->iotype ||
717 new_serial.iomem_reg_shift != uport->regshift ||
718 new_serial.type != uport->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Alan Cox46d57a42009-09-19 13:13:29 -0700720 old_flags = uport->flags;
Russell King0077d452006-01-21 23:03:28 +0000721 new_flags = new_serial.flags;
Alan Cox46d57a42009-09-19 13:13:29 -0700722 old_custom_divisor = uport->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 if (!capable(CAP_SYS_ADMIN)) {
725 retval = -EPERM;
726 if (change_irq || change_port ||
Alan Cox46d57a42009-09-19 13:13:29 -0700727 (new_serial.baud_base != uport->uartclk / 16) ||
728 (close_delay != port->close_delay) ||
729 (closing_wait != port->closing_wait) ||
Russell King947deee2006-07-02 20:45:51 +0100730 (new_serial.xmit_fifo_size &&
Alan Cox46d57a42009-09-19 13:13:29 -0700731 new_serial.xmit_fifo_size != uport->fifosize) ||
Russell King0077d452006-01-21 23:03:28 +0000732 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 goto exit;
Alan Cox46d57a42009-09-19 13:13:29 -0700734 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000735 (new_flags & UPF_USR_MASK));
Alan Cox46d57a42009-09-19 13:13:29 -0700736 uport->custom_divisor = new_serial.custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 goto check_and_exit;
738 }
739
740 /*
741 * Ask the low level driver to verify the settings.
742 */
Alan Cox46d57a42009-09-19 13:13:29 -0700743 if (uport->ops->verify_port)
744 retval = uport->ops->verify_port(uport, &new_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Yinghai Lua62c4132008-08-19 20:49:55 -0700746 if ((new_serial.irq >= nr_irqs) || (new_serial.irq < 0) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 (new_serial.baud_base < 9600))
748 retval = -EINVAL;
749
750 if (retval)
751 goto exit;
752
753 if (change_port || change_irq) {
754 retval = -EBUSY;
755
756 /*
757 * Make sure that we are the sole user of this port.
758 */
759 if (uart_users(state) > 1)
760 goto exit;
761
762 /*
763 * We need to shutdown the serial port at the old
764 * port/type/irq combination.
765 */
766 uart_shutdown(state);
767 }
768
769 if (change_port) {
770 unsigned long old_iobase, old_mapbase;
771 unsigned int old_type, old_iotype, old_hub6, old_shift;
772
Alan Cox46d57a42009-09-19 13:13:29 -0700773 old_iobase = uport->iobase;
774 old_mapbase = uport->mapbase;
775 old_type = uport->type;
776 old_hub6 = uport->hub6;
777 old_iotype = uport->iotype;
778 old_shift = uport->regshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 /*
781 * Free and release old regions
782 */
783 if (old_type != PORT_UNKNOWN)
Alan Cox46d57a42009-09-19 13:13:29 -0700784 uport->ops->release_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Alan Cox46d57a42009-09-19 13:13:29 -0700786 uport->iobase = new_port;
787 uport->type = new_serial.type;
788 uport->hub6 = new_serial.hub6;
789 uport->iotype = new_serial.io_type;
790 uport->regshift = new_serial.iomem_reg_shift;
791 uport->mapbase = (unsigned long)new_serial.iomem_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /*
794 * Claim and map the new regions
795 */
Alan Cox46d57a42009-09-19 13:13:29 -0700796 if (uport->type != PORT_UNKNOWN) {
797 retval = uport->ops->request_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 } else {
799 /* Always success - Jean II */
800 retval = 0;
801 }
802
803 /*
804 * If we fail to request resources for the
805 * new port, try to restore the old settings.
806 */
807 if (retval && old_type != PORT_UNKNOWN) {
Alan Cox46d57a42009-09-19 13:13:29 -0700808 uport->iobase = old_iobase;
809 uport->type = old_type;
810 uport->hub6 = old_hub6;
811 uport->iotype = old_iotype;
812 uport->regshift = old_shift;
813 uport->mapbase = old_mapbase;
814 retval = uport->ops->request_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /*
816 * If we failed to restore the old settings,
817 * we fail like this.
818 */
819 if (retval)
Alan Cox46d57a42009-09-19 13:13:29 -0700820 uport->type = PORT_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 /*
823 * We failed anyway.
824 */
825 retval = -EBUSY;
Alan Coxa46c9992008-02-08 04:18:53 -0800826 /* Added to return the correct error -Ram Gupta */
827 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829 }
830
David Gibsonabb4a232007-05-06 14:48:49 -0700831 if (change_irq)
Alan Cox46d57a42009-09-19 13:13:29 -0700832 uport->irq = new_serial.irq;
833 if (!(uport->flags & UPF_FIXED_PORT))
834 uport->uartclk = new_serial.baud_base * 16;
835 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000836 (new_flags & UPF_CHANGE_MASK);
Alan Cox46d57a42009-09-19 13:13:29 -0700837 uport->custom_divisor = new_serial.custom_divisor;
838 port->close_delay = close_delay;
839 port->closing_wait = closing_wait;
Russell King947deee2006-07-02 20:45:51 +0100840 if (new_serial.xmit_fifo_size)
Alan Cox46d57a42009-09-19 13:13:29 -0700841 uport->fifosize = new_serial.xmit_fifo_size;
842 if (port->tty)
843 port->tty->low_latency =
844 (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 check_and_exit:
847 retval = 0;
Alan Cox46d57a42009-09-19 13:13:29 -0700848 if (uport->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 goto exit;
Alan Coxccce6de2009-09-19 13:13:30 -0700850 if (port->flags & ASYNC_INITIALIZED) {
Alan Cox46d57a42009-09-19 13:13:29 -0700851 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
852 old_custom_divisor != uport->custom_divisor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /*
854 * If they're setting up a custom divisor or speed,
855 * instead of clearing it, then bitch about it. No
856 * need to rate-limit; it's CAP_SYS_ADMIN only.
857 */
Alan Cox46d57a42009-09-19 13:13:29 -0700858 if (uport->flags & UPF_SPD_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 char buf[64];
860 printk(KERN_NOTICE
861 "%s sets custom speed on %s. This "
862 "is deprecated.\n", current->comm,
Alan Cox46d57a42009-09-19 13:13:29 -0700863 tty_name(port->tty, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865 uart_change_speed(state, NULL);
866 }
867 } else
868 retval = uart_startup(state, 1);
869 exit:
Ingo Molnare2862f62006-01-13 21:37:07 +0000870 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return retval;
872}
873
874
875/*
876 * uart_get_lsr_info - get line status register info.
877 * Note: uart_ioctl protects us against hangups.
878 */
879static int uart_get_lsr_info(struct uart_state *state,
880 unsigned int __user *value)
881{
Alan Cox46d57a42009-09-19 13:13:29 -0700882 struct uart_port *uport = state->uart_port;
883 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 unsigned int result;
885
Alan Cox46d57a42009-09-19 13:13:29 -0700886 result = uport->ops->tx_empty(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 /*
889 * If we're about to load something into the transmit
890 * register, we'll pretend the transmitter isn't empty to
891 * avoid a race condition (depending on when the transmit
892 * interrupt happens).
893 */
Alan Cox46d57a42009-09-19 13:13:29 -0700894 if (uport->x_char ||
Alan Coxebd2c8f2009-09-19 13:13:28 -0700895 ((uart_circ_chars_pending(&state->xmit) > 0) &&
Alan Cox46d57a42009-09-19 13:13:29 -0700896 !port->tty->stopped && !port->tty->hw_stopped))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 result &= ~TIOCSER_TEMT;
Alan Coxa46c9992008-02-08 04:18:53 -0800898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return put_user(result, value);
900}
901
902static int uart_tiocmget(struct tty_struct *tty, struct file *file)
903{
904 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -0700905 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 int result = -EIO;
907
Ingo Molnare2862f62006-01-13 21:37:07 +0000908 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if ((!file || !tty_hung_up_p(file)) &&
910 !(tty->flags & (1 << TTY_IO_ERROR))) {
Alan Cox46d57a42009-09-19 13:13:29 -0700911 result = uport->mctrl;
Russell Kingc5f46442005-06-29 09:42:38 +0100912
Alan Cox46d57a42009-09-19 13:13:29 -0700913 spin_lock_irq(&uport->lock);
914 result |= uport->ops->get_mctrl(uport);
915 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000917 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 return result;
920}
921
922static int
923uart_tiocmset(struct tty_struct *tty, struct file *file,
924 unsigned int set, unsigned int clear)
925{
926 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -0700927 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 int ret = -EIO;
929
Ingo Molnare2862f62006-01-13 21:37:07 +0000930 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if ((!file || !tty_hung_up_p(file)) &&
932 !(tty->flags & (1 << TTY_IO_ERROR))) {
Alan Cox46d57a42009-09-19 13:13:29 -0700933 uart_update_mctrl(uport, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 ret = 0;
935 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000936 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return ret;
938}
939
Alan Cox9e989662008-07-22 11:18:03 +0100940static int uart_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -0700943 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Ingo Molnare2862f62006-01-13 21:37:07 +0000945 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Alan Cox46d57a42009-09-19 13:13:29 -0700947 if (uport->type != PORT_UNKNOWN)
948 uport->ops->break_ctl(uport, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Ingo Molnare2862f62006-01-13 21:37:07 +0000950 mutex_unlock(&state->mutex);
Alan Cox9e989662008-07-22 11:18:03 +0100951 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
954static int uart_do_autoconfig(struct uart_state *state)
955{
Alan Cox46d57a42009-09-19 13:13:29 -0700956 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 int flags, ret;
958
959 if (!capable(CAP_SYS_ADMIN))
960 return -EPERM;
961
962 /*
963 * Take the per-port semaphore. This prevents count from
964 * changing, and hence any extra opens of the port while
965 * we're auto-configuring.
966 */
Ingo Molnare2862f62006-01-13 21:37:07 +0000967 if (mutex_lock_interruptible(&state->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return -ERESTARTSYS;
969
970 ret = -EBUSY;
971 if (uart_users(state) == 1) {
972 uart_shutdown(state);
973
974 /*
975 * If we already have a port type configured,
976 * we must release its resources.
977 */
Alan Cox46d57a42009-09-19 13:13:29 -0700978 if (uport->type != PORT_UNKNOWN)
979 uport->ops->release_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 flags = UART_CONFIG_TYPE;
Alan Cox46d57a42009-09-19 13:13:29 -0700982 if (uport->flags & UPF_AUTO_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 flags |= UART_CONFIG_IRQ;
984
985 /*
986 * This will claim the ports resources if
987 * a port is found.
988 */
Alan Cox46d57a42009-09-19 13:13:29 -0700989 uport->ops->config_port(uport, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 ret = uart_startup(state, 1);
992 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000993 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return ret;
995}
996
997/*
998 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
999 * - mask passed in arg for lines of interest
1000 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1001 * Caller should use TIOCGICOUNT to see which one it was
1002 */
1003static int
1004uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1005{
Alan Cox46d57a42009-09-19 13:13:29 -07001006 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 DECLARE_WAITQUEUE(wait, current);
1008 struct uart_icount cprev, cnow;
1009 int ret;
1010
1011 /*
1012 * note the counters on entry
1013 */
Alan Cox46d57a42009-09-19 13:13:29 -07001014 spin_lock_irq(&uport->lock);
1015 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 /*
1018 * Force modem status interrupts on
1019 */
Alan Cox46d57a42009-09-19 13:13:29 -07001020 uport->ops->enable_ms(uport);
1021 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Alan Coxebd2c8f2009-09-19 13:13:28 -07001023 add_wait_queue(&state->delta_msr_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 for (;;) {
Alan Cox46d57a42009-09-19 13:13:29 -07001025 spin_lock_irq(&uport->lock);
1026 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1027 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 set_current_state(TASK_INTERRUPTIBLE);
1030
1031 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1032 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1033 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1034 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Alan Coxa46c9992008-02-08 04:18:53 -08001035 ret = 0;
1036 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
1039 schedule();
1040
1041 /* see if a signal did it */
1042 if (signal_pending(current)) {
1043 ret = -ERESTARTSYS;
1044 break;
1045 }
1046
1047 cprev = cnow;
1048 }
1049
1050 current->state = TASK_RUNNING;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001051 remove_wait_queue(&state->delta_msr_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 return ret;
1054}
1055
1056/*
1057 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1058 * Return: write counters to the user passed counter struct
1059 * NB: both 1->0 and 0->1 transitions are counted except for
1060 * RI where only 0->1 is counted.
1061 */
1062static int uart_get_count(struct uart_state *state,
1063 struct serial_icounter_struct __user *icnt)
1064{
1065 struct serial_icounter_struct icount;
1066 struct uart_icount cnow;
Alan Cox46d57a42009-09-19 13:13:29 -07001067 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Alan Cox46d57a42009-09-19 13:13:29 -07001069 spin_lock_irq(&uport->lock);
1070 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1071 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 icount.cts = cnow.cts;
1074 icount.dsr = cnow.dsr;
1075 icount.rng = cnow.rng;
1076 icount.dcd = cnow.dcd;
1077 icount.rx = cnow.rx;
1078 icount.tx = cnow.tx;
1079 icount.frame = cnow.frame;
1080 icount.overrun = cnow.overrun;
1081 icount.parity = cnow.parity;
1082 icount.brk = cnow.brk;
1083 icount.buf_overrun = cnow.buf_overrun;
1084
1085 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1086}
1087
1088/*
Alan Coxe5238442008-04-30 00:53:28 -07001089 * Called via sys_ioctl. We can use spin_lock_irq() here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 */
1091static int
1092uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1093 unsigned long arg)
1094{
1095 struct uart_state *state = tty->driver_data;
1096 void __user *uarg = (void __user *)arg;
1097 int ret = -ENOIOCTLCMD;
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 /*
1101 * These ioctls don't rely on the hardware to be present.
1102 */
1103 switch (cmd) {
1104 case TIOCGSERIAL:
1105 ret = uart_get_info(state, uarg);
1106 break;
1107
1108 case TIOCSSERIAL:
1109 ret = uart_set_info(state, uarg);
1110 break;
1111
1112 case TIOCSERCONFIG:
1113 ret = uart_do_autoconfig(state);
1114 break;
1115
1116 case TIOCSERGWILD: /* obsolete */
1117 case TIOCSERSWILD: /* obsolete */
1118 ret = 0;
1119 break;
1120 }
1121
1122 if (ret != -ENOIOCTLCMD)
1123 goto out;
1124
1125 if (tty->flags & (1 << TTY_IO_ERROR)) {
1126 ret = -EIO;
1127 goto out;
1128 }
1129
1130 /*
1131 * The following should only be used when hardware is present.
1132 */
1133 switch (cmd) {
1134 case TIOCMIWAIT:
1135 ret = uart_wait_modem_status(state, arg);
1136 break;
1137
1138 case TIOCGICOUNT:
1139 ret = uart_get_count(state, uarg);
1140 break;
1141 }
1142
1143 if (ret != -ENOIOCTLCMD)
1144 goto out;
1145
Ingo Molnare2862f62006-01-13 21:37:07 +00001146 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 if (tty_hung_up_p(filp)) {
1149 ret = -EIO;
1150 goto out_up;
1151 }
1152
1153 /*
1154 * All these rely on hardware being present and need to be
1155 * protected against the tty being hung up.
1156 */
1157 switch (cmd) {
1158 case TIOCSERGETLSR: /* Get line status register */
1159 ret = uart_get_lsr_info(state, uarg);
1160 break;
1161
1162 default: {
Alan Cox46d57a42009-09-19 13:13:29 -07001163 struct uart_port *uport = state->uart_port;
1164 if (uport->ops->ioctl)
1165 ret = uport->ops->ioctl(uport, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 break;
1167 }
1168 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001169out_up:
Ingo Molnare2862f62006-01-13 21:37:07 +00001170 mutex_unlock(&state->mutex);
Alan Coxf34d7a52008-04-30 00:54:13 -07001171out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return ret;
1173}
1174
Linus Torvaldsedeb2802008-06-04 10:35:03 -07001175static void uart_set_ldisc(struct tty_struct *tty)
Alan Cox64e91592008-06-03 15:18:54 +01001176{
1177 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -07001178 struct uart_port *uport = state->uart_port;
Alan Cox64e91592008-06-03 15:18:54 +01001179
Alan Cox46d57a42009-09-19 13:13:29 -07001180 if (uport->ops->set_ldisc)
1181 uport->ops->set_ldisc(uport);
Alan Cox64e91592008-06-03 15:18:54 +01001182}
1183
Alan Coxa46c9992008-02-08 04:18:53 -08001184static void uart_set_termios(struct tty_struct *tty,
1185 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
1187 struct uart_state *state = tty->driver_data;
1188 unsigned long flags;
1189 unsigned int cflag = tty->termios->c_cflag;
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 /*
1193 * These are the bits that are used to setup various
David Woodhouse20620d62007-08-22 14:01:11 -07001194 * flags in the low level driver. We can ignore the Bfoo
1195 * bits in c_cflag; c_[io]speed will always be set
1196 * appropriately by set_termios() in tty_ioctl.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 */
1198#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if ((cflag ^ old_termios->c_cflag) == 0 &&
David Woodhouse20620d62007-08-22 14:01:11 -07001200 tty->termios->c_ospeed == old_termios->c_ospeed &&
1201 tty->termios->c_ispeed == old_termios->c_ispeed &&
Alan Coxe5238442008-04-30 00:53:28 -07001202 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return;
Alan Coxe5238442008-04-30 00:53:28 -07001204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 uart_change_speed(state, old_termios);
1207
1208 /* Handle transition to B0 status */
1209 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
Alan Coxebd2c8f2009-09-19 13:13:28 -07001210 uart_clear_mctrl(state->uart_port, TIOCM_RTS | TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 /* Handle transition away from B0 status */
1213 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1214 unsigned int mask = TIOCM_DTR;
1215 if (!(cflag & CRTSCTS) ||
1216 !test_bit(TTY_THROTTLED, &tty->flags))
1217 mask |= TIOCM_RTS;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001218 uart_set_mctrl(state->uart_port, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220
1221 /* Handle turning off CRTSCTS */
1222 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
Alan Coxebd2c8f2009-09-19 13:13:28 -07001223 spin_lock_irqsave(&state->uart_port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 tty->hw_stopped = 0;
1225 __uart_start(tty);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001226 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228
Russell King0dd7a1a2005-06-29 18:40:53 +01001229 /* Handle turning on CRTSCTS */
1230 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
Alan Coxebd2c8f2009-09-19 13:13:28 -07001231 spin_lock_irqsave(&state->uart_port->lock, flags);
1232 if (!(state->uart_port->ops->get_mctrl(state->uart_port) & TIOCM_CTS)) {
Russell King0dd7a1a2005-06-29 18:40:53 +01001233 tty->hw_stopped = 1;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001234 state->uart_port->ops->stop_tx(state->uart_port);
Russell King0dd7a1a2005-06-29 18:40:53 +01001235 }
Alan Coxebd2c8f2009-09-19 13:13:28 -07001236 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Russell King0dd7a1a2005-06-29 18:40:53 +01001237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238#if 0
1239 /*
1240 * No need to wake up processes in open wait, since they
1241 * sample the CLOCAL flag once, and don't recheck it.
1242 * XXX It's not clear whether the current behavior is correct
1243 * or not. Hence, this may change.....
1244 */
1245 if (!(old_termios->c_cflag & CLOCAL) &&
1246 (tty->termios->c_cflag & CLOCAL))
Alan Coxebd2c8f2009-09-19 13:13:28 -07001247 wake_up_interruptible(&state->uart_port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248#endif
1249}
1250
1251/*
1252 * In 2.4.5, calls to this will be serialized via the BKL in
1253 * linux/drivers/char/tty_io.c:tty_release()
1254 * linux/drivers/char/tty_io.c:do_tty_handup()
1255 */
1256static void uart_close(struct tty_struct *tty, struct file *filp)
1257{
1258 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -07001259 struct tty_port *port;
1260 struct uart_port *uport;
Alan Coxa46c9992008-02-08 04:18:53 -08001261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 BUG_ON(!kernel_locked());
1263
Alan Cox46d57a42009-09-19 13:13:29 -07001264 uport = state->uart_port;
1265 port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Alan Cox46d57a42009-09-19 13:13:29 -07001267 pr_debug("uart_close(%d) called\n", uport->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Ingo Molnare2862f62006-01-13 21:37:07 +00001269 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 if (tty_hung_up_p(filp))
1272 goto done;
1273
Alan Cox91312cd2009-09-19 13:13:29 -07001274 if ((tty->count == 1) && (port->count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 /*
1276 * Uh, oh. tty->count is 1, which means that the tty
Alan Cox91312cd2009-09-19 13:13:29 -07001277 * structure will be freed. port->count should always
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 * be one in these conditions. If it's greater than
1279 * one, we've got real problems, since it means the
1280 * serial port won't be shutdown.
1281 */
1282 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
Alan Cox91312cd2009-09-19 13:13:29 -07001283 "port->count is %d\n", port->count);
1284 port->count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
Alan Cox91312cd2009-09-19 13:13:29 -07001286 if (--port->count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
Alan Cox91312cd2009-09-19 13:13:29 -07001288 tty->name, port->count);
1289 port->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
Alan Cox91312cd2009-09-19 13:13:29 -07001291 if (port->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 goto done;
1293
1294 /*
1295 * Now we wait for the transmit buffer to clear; and we notify
1296 * the line discipline to only process XON/XOFF characters by
1297 * setting tty->closing.
1298 */
1299 tty->closing = 1;
1300
Alan Cox46d57a42009-09-19 13:13:29 -07001301 if (port->closing_wait != USF_CLOSING_WAIT_NONE)
1302 tty_wait_until_sent(tty, msecs_to_jiffies(port->closing_wait));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 /*
1305 * At this point, we stop accepting input. To do this, we
1306 * disable the receive line status interrupts.
1307 */
Alan Coxccce6de2009-09-19 13:13:30 -07001308 if (port->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 unsigned long flags;
1310 spin_lock_irqsave(&port->lock, flags);
Alan Cox46d57a42009-09-19 13:13:29 -07001311 uport->ops->stop_rx(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 spin_unlock_irqrestore(&port->lock, flags);
1313 /*
1314 * Before we drop DTR, make sure the UART transmitter
1315 * has completely drained; this is especially
1316 * important if there is a transmit FIFO!
1317 */
Alan Cox46d57a42009-09-19 13:13:29 -07001318 uart_wait_until_sent(tty, uport->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320
1321 uart_shutdown(state);
1322 uart_flush_buffer(tty);
1323
Alan Coxa46c9992008-02-08 04:18:53 -08001324 tty_ldisc_flush(tty);
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 tty->closing = 0;
Alan Cox46d57a42009-09-19 13:13:29 -07001327 port->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Alan Cox46d57a42009-09-19 13:13:29 -07001329 if (port->blocked_open) {
1330 if (port->close_delay)
1331 msleep_interruptible(port->close_delay);
1332 } else if (!uart_console(uport)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 uart_change_pm(state, 3);
1334 }
1335
1336 /*
1337 * Wake up anyone trying to open this port.
1338 */
Alan Coxccce6de2009-09-19 13:13:30 -07001339 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
Alan Cox46d57a42009-09-19 13:13:29 -07001340 wake_up_interruptible(&port->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Alan Cox46d57a42009-09-19 13:13:29 -07001342done:
Ingo Molnare2862f62006-01-13 21:37:07 +00001343 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
1346static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1347{
1348 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001349 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 unsigned long char_time, expire;
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1353 return;
1354
Alan Coxf34d7a52008-04-30 00:54:13 -07001355 lock_kernel();
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /*
1358 * Set the check interval to be 1/5 of the estimated time to
1359 * send a single character, and make it at least 1. The check
1360 * interval should also be less than the timeout.
1361 *
1362 * Note: we have to use pretty tight timings here to satisfy
1363 * the NIST-PCTS.
1364 */
1365 char_time = (port->timeout - HZ/50) / port->fifosize;
1366 char_time = char_time / 5;
1367 if (char_time == 0)
1368 char_time = 1;
1369 if (timeout && timeout < char_time)
1370 char_time = timeout;
1371
1372 /*
1373 * If the transmitter hasn't cleared in twice the approximate
1374 * amount of time to send the entire FIFO, it probably won't
1375 * ever clear. This assumes the UART isn't doing flow
1376 * control, which is currently the case. Hence, if it ever
1377 * takes longer than port->timeout, this is probably due to a
1378 * UART bug of some kind. So, we clamp the timeout parameter at
1379 * 2*port->timeout.
1380 */
1381 if (timeout == 0 || timeout > 2 * port->timeout)
1382 timeout = 2 * port->timeout;
1383
1384 expire = jiffies + timeout;
1385
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001386 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
Alan Coxa46c9992008-02-08 04:18:53 -08001387 port->line, jiffies, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 /*
1390 * Check whether the transmitter is empty every 'char_time'.
1391 * 'timeout' / 'expire' give us the maximum amount of time
1392 * we wait.
1393 */
1394 while (!port->ops->tx_empty(port)) {
1395 msleep_interruptible(jiffies_to_msecs(char_time));
1396 if (signal_pending(current))
1397 break;
1398 if (time_after(jiffies, expire))
1399 break;
1400 }
1401 set_current_state(TASK_RUNNING); /* might not be needed */
Alan Coxf34d7a52008-04-30 00:54:13 -07001402 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
1405/*
1406 * This is called with the BKL held in
1407 * linux/drivers/char/tty_io.c:do_tty_hangup()
1408 * We're called from the eventd thread, so we can sleep for
1409 * a _short_ time only.
1410 */
1411static void uart_hangup(struct tty_struct *tty)
1412{
1413 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -07001414 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 BUG_ON(!kernel_locked());
Alan Coxebd2c8f2009-09-19 13:13:28 -07001417 pr_debug("uart_hangup(%d)\n", state->uart_port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Ingo Molnare2862f62006-01-13 21:37:07 +00001419 mutex_lock(&state->mutex);
Alan Coxccce6de2009-09-19 13:13:30 -07001420 if (port->flags & ASYNC_NORMAL_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 uart_flush_buffer(tty);
1422 uart_shutdown(state);
Alan Cox91312cd2009-09-19 13:13:29 -07001423 port->count = 0;
Alan Coxccce6de2009-09-19 13:13:30 -07001424 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
Alan Cox46d57a42009-09-19 13:13:29 -07001425 port->tty = NULL;
1426 wake_up_interruptible(&port->open_wait);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001427 wake_up_interruptible(&state->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
Ingo Molnare2862f62006-01-13 21:37:07 +00001429 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
1432/*
1433 * Copy across the serial console cflag setting into the termios settings
1434 * for the initial open of the port. This allows continuity between the
1435 * kernel settings, and the settings init adopts when it opens the port
1436 * for the first time.
1437 */
1438static void uart_update_termios(struct uart_state *state)
1439{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001440 struct tty_struct *tty = state->port.tty;
1441 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 if (uart_console(port) && port->cons->cflag) {
1444 tty->termios->c_cflag = port->cons->cflag;
1445 port->cons->cflag = 0;
1446 }
1447
1448 /*
1449 * If the device failed to grab its irq resources,
1450 * or some other error occurred, don't try to talk
1451 * to the port hardware.
1452 */
1453 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1454 /*
1455 * Make termios settings take effect.
1456 */
1457 uart_change_speed(state, NULL);
1458
1459 /*
1460 * And finally enable the RTS and DTR signals.
1461 */
1462 if (tty->termios->c_cflag & CBAUD)
1463 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1464 }
1465}
1466
1467/*
1468 * Block the open until the port is ready. We must be called with
1469 * the per-port semaphore held.
1470 */
1471static int
1472uart_block_til_ready(struct file *filp, struct uart_state *state)
1473{
1474 DECLARE_WAITQUEUE(wait, current);
Alan Cox46d57a42009-09-19 13:13:29 -07001475 struct uart_port *uport = state->uart_port;
1476 struct tty_port *port = &state->port;
Russell Kingc5f46442005-06-29 09:42:38 +01001477 unsigned int mctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Alan Cox46d57a42009-09-19 13:13:29 -07001479 port->blocked_open++;
Alan Cox91312cd2009-09-19 13:13:29 -07001480 port->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Alan Cox46d57a42009-09-19 13:13:29 -07001482 add_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 while (1) {
1484 set_current_state(TASK_INTERRUPTIBLE);
1485
1486 /*
1487 * If we have been hung up, tell userspace/restart open.
1488 */
Alan Cox46d57a42009-09-19 13:13:29 -07001489 if (tty_hung_up_p(filp) || port->tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 break;
1491
1492 /*
1493 * If the port has been closed, tell userspace/restart open.
1494 */
Alan Coxccce6de2009-09-19 13:13:30 -07001495 if (!(port->flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 break;
1497
1498 /*
1499 * If non-blocking mode is set, or CLOCAL mode is set,
1500 * we don't want to wait for the modem status lines to
1501 * indicate that the port is ready.
1502 *
1503 * Also, if the port is not enabled/configured, we want
1504 * to allow the open to succeed here. Note that we will
1505 * have set TTY_IO_ERROR for a non-existant port.
1506 */
1507 if ((filp->f_flags & O_NONBLOCK) ||
Alan Cox46d57a42009-09-19 13:13:29 -07001508 (port->tty->termios->c_cflag & CLOCAL) ||
1509 (port->tty->flags & (1 << TTY_IO_ERROR)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 /*
1513 * Set DTR to allow modem to know we're waiting. Do
1514 * not set RTS here - we want to make sure we catch
1515 * the data from the modem.
1516 */
Alan Cox46d57a42009-09-19 13:13:29 -07001517 if (port->tty->termios->c_cflag & CBAUD)
1518 uart_set_mctrl(uport, TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 /*
1521 * and wait for the carrier to indicate that the
1522 * modem is ready for us.
1523 */
Alan Cox46d57a42009-09-19 13:13:29 -07001524 spin_lock_irq(&uport->lock);
1525 uport->ops->enable_ms(uport);
1526 mctrl = uport->ops->get_mctrl(uport);
1527 spin_unlock_irq(&uport->lock);
Russell Kingc5f46442005-06-29 09:42:38 +01001528 if (mctrl & TIOCM_CAR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 break;
1530
Ingo Molnare2862f62006-01-13 21:37:07 +00001531 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 schedule();
Ingo Molnare2862f62006-01-13 21:37:07 +00001533 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 if (signal_pending(current))
1536 break;
1537 }
1538 set_current_state(TASK_RUNNING);
Alan Cox46d57a42009-09-19 13:13:29 -07001539 remove_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Alan Cox91312cd2009-09-19 13:13:29 -07001541 port->count++;
Alan Cox46d57a42009-09-19 13:13:29 -07001542 port->blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 if (signal_pending(current))
1545 return -ERESTARTSYS;
1546
Alan Cox46d57a42009-09-19 13:13:29 -07001547 if (!port->tty || tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return -EAGAIN;
1549
1550 return 0;
1551}
1552
1553static struct uart_state *uart_get(struct uart_driver *drv, int line)
1554{
1555 struct uart_state *state;
Russell King68ac64c2006-04-30 11:13:50 +01001556 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 state = drv->state + line;
Ingo Molnare2862f62006-01-13 21:37:07 +00001559 if (mutex_lock_interruptible(&state->mutex)) {
Russell King68ac64c2006-04-30 11:13:50 +01001560 ret = -ERESTARTSYS;
1561 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
1563
Alan Cox91312cd2009-09-19 13:13:29 -07001564 state->port.count++;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001565 if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
Russell King68ac64c2006-04-30 11:13:50 +01001566 ret = -ENXIO;
1567 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return state;
Russell King68ac64c2006-04-30 11:13:50 +01001570
1571 err_unlock:
Alan Cox91312cd2009-09-19 13:13:29 -07001572 state->port.count--;
Russell King68ac64c2006-04-30 11:13:50 +01001573 mutex_unlock(&state->mutex);
1574 err:
1575 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
1578/*
Denis Cheng922f9cf2008-02-08 04:22:00 -08001579 * calls to uart_open are serialised by the BKL in
1580 * fs/char_dev.c:chrdev_open()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 * Note that if this fails, then uart_close() _will_ be called.
1582 *
1583 * In time, we want to scrap the "opening nonpresent ports"
1584 * behaviour and implement an alternative way for setserial
1585 * to set base addresses/ports/types. This will allow us to
1586 * get rid of a certain amount of extra tests.
1587 */
1588static int uart_open(struct tty_struct *tty, struct file *filp)
1589{
1590 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1591 struct uart_state *state;
Alan Cox91312cd2009-09-19 13:13:29 -07001592 struct tty_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 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 }
Alan Cox91312cd2009-09-19 13:13:29 -07001620 port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 /*
1623 * Once we set tty->driver_data here, we are guaranteed that
1624 * uart_close() will decrement the driver module use count.
1625 * Any failures from here onwards should not touch the count.
1626 */
1627 tty->driver_data = state;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001628 state->uart_port->state = state;
1629 tty->low_latency = (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 tty->alt_speed = 0;
Alan Cox91312cd2009-09-19 13:13:29 -07001631 port->tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 /*
1634 * If the port is in the middle of closing, bail out now.
1635 */
1636 if (tty_hung_up_p(filp)) {
1637 retval = -EAGAIN;
Alan Cox91312cd2009-09-19 13:13:29 -07001638 port->count--;
Ingo Molnare2862f62006-01-13 21:37:07 +00001639 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 goto fail;
1641 }
1642
1643 /*
1644 * Make sure the device is in D0 state.
1645 */
Alan Cox91312cd2009-09-19 13:13:29 -07001646 if (port->count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 uart_change_pm(state, 0);
1648
1649 /*
1650 * Start up the serial port.
1651 */
1652 retval = uart_startup(state, 0);
1653
1654 /*
1655 * If we succeeded, wait until the port is ready.
1656 */
1657 if (retval == 0)
1658 retval = uart_block_til_ready(filp, state);
Ingo Molnare2862f62006-01-13 21:37:07 +00001659 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 /*
1662 * If this is the first open to succeed, adjust things to suit.
1663 */
Alan Coxccce6de2009-09-19 13:13:30 -07001664 if (retval == 0 && !(port->flags & ASYNC_NORMAL_ACTIVE)) {
1665 set_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 uart_update_termios(state);
1668 }
1669
1670 fail:
1671 return retval;
1672}
1673
1674static const char *uart_type(struct uart_port *port)
1675{
1676 const char *str = NULL;
1677
1678 if (port->ops->type)
1679 str = port->ops->type(port);
1680
1681 if (!str)
1682 str = "unknown";
1683
1684 return str;
1685}
1686
1687#ifdef CONFIG_PROC_FS
1688
Alexey Dobriyand196a942009-03-31 15:19:21 -07001689static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 struct uart_state *state = drv->state + i;
George G. Davis3689a0e2007-02-14 00:33:06 -08001692 int pm_state;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001693 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 char stat_buf[32];
1695 unsigned int status;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001696 int mmio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 if (!port)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001699 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001701 mmio = port->iotype >= UPIO_MEM;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001702 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 port->line, uart_type(port),
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001704 mmio ? "mmio:0x" : "port:",
Josh Boyer4f640ef2007-07-23 18:43:44 -07001705 mmio ? (unsigned long long)port->mapbase
Alan Coxa46c9992008-02-08 04:18:53 -08001706 : (unsigned long long) port->iobase,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 port->irq);
1708
1709 if (port->type == PORT_UNKNOWN) {
Alexey Dobriyand196a942009-03-31 15:19:21 -07001710 seq_putc(m, '\n');
1711 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713
Alan Coxa46c9992008-02-08 04:18:53 -08001714 if (capable(CAP_SYS_ADMIN)) {
George G. Davis3689a0e2007-02-14 00:33:06 -08001715 mutex_lock(&state->mutex);
1716 pm_state = state->pm_state;
1717 if (pm_state)
1718 uart_change_pm(state, 0);
Russell Kingc5f46442005-06-29 09:42:38 +01001719 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 status = port->ops->get_mctrl(port);
Russell Kingc5f46442005-06-29 09:42:38 +01001721 spin_unlock_irq(&port->lock);
George G. Davis3689a0e2007-02-14 00:33:06 -08001722 if (pm_state)
1723 uart_change_pm(state, pm_state);
1724 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Alexey Dobriyand196a942009-03-31 15:19:21 -07001726 seq_printf(m, " tx:%d rx:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 port->icount.tx, port->icount.rx);
1728 if (port->icount.frame)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001729 seq_printf(m, " fe:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 port->icount.frame);
1731 if (port->icount.parity)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001732 seq_printf(m, " pe:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 port->icount.parity);
1734 if (port->icount.brk)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001735 seq_printf(m, " brk:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 port->icount.brk);
1737 if (port->icount.overrun)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001738 seq_printf(m, " oe:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 port->icount.overrun);
Alan Coxa46c9992008-02-08 04:18:53 -08001740
1741#define INFOBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (port->mctrl & (bit)) \
1743 strncat(stat_buf, (str), sizeof(stat_buf) - \
1744 strlen(stat_buf) - 2)
Alan Coxa46c9992008-02-08 04:18:53 -08001745#define STATBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (status & (bit)) \
1747 strncat(stat_buf, (str), sizeof(stat_buf) - \
1748 strlen(stat_buf) - 2)
1749
1750 stat_buf[0] = '\0';
1751 stat_buf[1] = '\0';
1752 INFOBIT(TIOCM_RTS, "|RTS");
1753 STATBIT(TIOCM_CTS, "|CTS");
1754 INFOBIT(TIOCM_DTR, "|DTR");
1755 STATBIT(TIOCM_DSR, "|DSR");
1756 STATBIT(TIOCM_CAR, "|CD");
1757 STATBIT(TIOCM_RNG, "|RI");
1758 if (stat_buf[0])
1759 stat_buf[0] = ' ';
Alan Coxa46c9992008-02-08 04:18:53 -08001760
Alexey Dobriyand196a942009-03-31 15:19:21 -07001761 seq_puts(m, stat_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
Alexey Dobriyand196a942009-03-31 15:19:21 -07001763 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764#undef STATBIT
1765#undef INFOBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
Alexey Dobriyand196a942009-03-31 15:19:21 -07001768static int uart_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
Alexey Dobriyan833bb302009-04-02 01:30:04 +04001770 struct tty_driver *ttydrv = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 struct uart_driver *drv = ttydrv->driver_state;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001772 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Alexey Dobriyand196a942009-03-31 15:19:21 -07001774 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 "", "", "");
Alexey Dobriyand196a942009-03-31 15:19:21 -07001776 for (i = 0; i < drv->nr; i++)
1777 uart_line_info(m, drv, i);
1778 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
Alexey Dobriyand196a942009-03-31 15:19:21 -07001780
1781static int uart_proc_open(struct inode *inode, struct file *file)
1782{
1783 return single_open(file, uart_proc_show, PDE(inode)->data);
1784}
1785
1786static const struct file_operations uart_proc_fops = {
1787 .owner = THIS_MODULE,
1788 .open = uart_proc_open,
1789 .read = seq_read,
1790 .llseek = seq_lseek,
1791 .release = single_release,
1792};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793#endif
1794
Andrew Morton4a1b5502008-03-07 15:51:16 -08001795#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796/*
Russell Kingd3587882006-03-20 20:00:09 +00001797 * uart_console_write - write a console message to a serial port
1798 * @port: the port to write the message
1799 * @s: array of characters
1800 * @count: number of characters in string to write
1801 * @write: function to write character to port
1802 */
1803void uart_console_write(struct uart_port *port, const char *s,
1804 unsigned int count,
1805 void (*putchar)(struct uart_port *, int))
1806{
1807 unsigned int i;
1808
1809 for (i = 0; i < count; i++, s++) {
1810 if (*s == '\n')
1811 putchar(port, '\r');
1812 putchar(port, *s);
1813 }
1814}
1815EXPORT_SYMBOL_GPL(uart_console_write);
1816
1817/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 * Check whether an invalid uart number has been specified, and
1819 * if so, search for the first available port that does have
1820 * console support.
1821 */
1822struct uart_port * __init
1823uart_get_console(struct uart_port *ports, int nr, struct console *co)
1824{
1825 int idx = co->index;
1826
1827 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1828 ports[idx].membase == NULL))
1829 for (idx = 0; idx < nr; idx++)
1830 if (ports[idx].iobase != 0 ||
1831 ports[idx].membase != NULL)
1832 break;
1833
1834 co->index = idx;
1835
1836 return ports + idx;
1837}
1838
1839/**
1840 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1841 * @options: pointer to option string
1842 * @baud: pointer to an 'int' variable for the baud rate.
1843 * @parity: pointer to an 'int' variable for the parity.
1844 * @bits: pointer to an 'int' variable for the number of data bits.
1845 * @flow: pointer to an 'int' variable for the flow control character.
1846 *
1847 * uart_parse_options decodes a string containing the serial console
1848 * options. The format of the string is <baud><parity><bits><flow>,
1849 * eg: 115200n8r
1850 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001851void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1853{
1854 char *s = options;
1855
1856 *baud = simple_strtoul(s, NULL, 10);
1857 while (*s >= '0' && *s <= '9')
1858 s++;
1859 if (*s)
1860 *parity = *s++;
1861 if (*s)
1862 *bits = *s++ - '0';
1863 if (*s)
1864 *flow = *s;
1865}
Jason Wesself2d937f2008-04-17 20:05:37 +02001866EXPORT_SYMBOL_GPL(uart_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868struct baud_rates {
1869 unsigned int rate;
1870 unsigned int cflag;
1871};
1872
Arjan van de Vencb3592b2005-11-28 21:04:11 +00001873static const struct baud_rates baud_rates[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 { 921600, B921600 },
1875 { 460800, B460800 },
1876 { 230400, B230400 },
1877 { 115200, B115200 },
1878 { 57600, B57600 },
1879 { 38400, B38400 },
1880 { 19200, B19200 },
1881 { 9600, B9600 },
1882 { 4800, B4800 },
1883 { 2400, B2400 },
1884 { 1200, B1200 },
1885 { 0, B38400 }
1886};
1887
1888/**
1889 * uart_set_options - setup the serial console parameters
1890 * @port: pointer to the serial ports uart_port structure
1891 * @co: console pointer
1892 * @baud: baud rate
1893 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1894 * @bits: number of data bits
1895 * @flow: flow control character - 'r' (rts)
1896 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001897int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898uart_set_options(struct uart_port *port, struct console *co,
1899 int baud, int parity, int bits, int flow)
1900{
Alan Cox606d0992006-12-08 02:38:45 -08001901 struct ktermios termios;
Alan Cox149b36e2007-10-18 01:24:16 -07001902 static struct ktermios dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 int i;
1904
Russell King976ecd12005-07-03 21:05:45 +01001905 /*
1906 * Ensure that the serial console lock is initialised
1907 * early.
1908 */
1909 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07001910 lockdep_set_class(&port->lock, &port_lock_key);
Russell King976ecd12005-07-03 21:05:45 +01001911
Alan Cox606d0992006-12-08 02:38:45 -08001912 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1915
1916 /*
1917 * Construct a cflag setting.
1918 */
1919 for (i = 0; baud_rates[i].rate; i++)
1920 if (baud_rates[i].rate <= baud)
1921 break;
1922
1923 termios.c_cflag |= baud_rates[i].cflag;
1924
1925 if (bits == 7)
1926 termios.c_cflag |= CS7;
1927 else
1928 termios.c_cflag |= CS8;
1929
1930 switch (parity) {
1931 case 'o': case 'O':
1932 termios.c_cflag |= PARODD;
1933 /*fall through*/
1934 case 'e': case 'E':
1935 termios.c_cflag |= PARENB;
1936 break;
1937 }
1938
1939 if (flow == 'r')
1940 termios.c_cflag |= CRTSCTS;
1941
Yinghai Lu79492682007-07-15 23:37:25 -07001942 /*
1943 * some uarts on other side don't support no flow control.
1944 * So we set * DTR in host uart to make them happy
1945 */
1946 port->mctrl |= TIOCM_DTR;
1947
Alan Cox149b36e2007-10-18 01:24:16 -07001948 port->ops->set_termios(port, &termios, &dummy);
Jason Wesself2d937f2008-04-17 20:05:37 +02001949 /*
1950 * Allow the setting of the UART parameters with a NULL console
1951 * too:
1952 */
1953 if (co)
1954 co->cflag = termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 return 0;
1957}
Jason Wesself2d937f2008-04-17 20:05:37 +02001958EXPORT_SYMBOL_GPL(uart_set_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959#endif /* CONFIG_SERIAL_CORE_CONSOLE */
1960
1961static void uart_change_pm(struct uart_state *state, int pm_state)
1962{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001963 struct uart_port *port = state->uart_port;
Andrew Victor1281e362006-05-16 11:28:49 +01001964
1965 if (state->pm_state != pm_state) {
1966 if (port->ops->pm)
1967 port->ops->pm(port, pm_state, state->pm_state);
1968 state->pm_state = pm_state;
1969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001972struct uart_match {
1973 struct uart_port *port;
1974 struct uart_driver *driver;
1975};
1976
1977static int serial_match_port(struct device *dev, void *data)
1978{
1979 struct uart_match *match = data;
Guennadi Liakhovetski7ca796f2008-07-04 09:59:28 -07001980 struct tty_driver *tty_drv = match->driver->tty_driver;
1981 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1982 match->port->line;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001983
1984 return dev->devt == devt; /* Actually, only one tty per port */
1985}
1986
Alan Coxccce6de2009-09-19 13:13:30 -07001987int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Alan Coxccce6de2009-09-19 13:13:30 -07001989 struct uart_state *state = drv->state + uport->line;
1990 struct tty_port *port = &state->port;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001991 struct device *tty_dev;
Alan Coxccce6de2009-09-19 13:13:30 -07001992 struct uart_match match = {uport, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Ingo Molnare2862f62006-01-13 21:37:07 +00001994 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Alan Coxccce6de2009-09-19 13:13:30 -07001996 if (!console_suspend_enabled && uart_console(uport)) {
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001997 /* we're going to avoid suspending serial console */
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07001998 mutex_unlock(&state->mutex);
1999 return 0;
2000 }
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002001
Alan Coxccce6de2009-09-19 13:13:30 -07002002 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002003 if (device_may_wakeup(tty_dev)) {
Alan Coxccce6de2009-09-19 13:13:30 -07002004 enable_irq_wake(uport->irq);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002005 put_device(tty_dev);
2006 mutex_unlock(&state->mutex);
2007 return 0;
2008 }
Alan Coxccce6de2009-09-19 13:13:30 -07002009 uport->suspended = 1;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002010
Alan Coxccce6de2009-09-19 13:13:30 -07002011 if (port->flags & ASYNC_INITIALIZED) {
2012 const struct uart_ops *ops = uport->ops;
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002013 int tries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Alan Coxccce6de2009-09-19 13:13:30 -07002015 set_bit(ASYNCB_SUSPENDED, &port->flags);
2016 clear_bit(ASYNCB_INITIALIZED, &port->flags);
Russell Kinga6b93a92006-10-01 17:17:40 +01002017
Alan Coxccce6de2009-09-19 13:13:30 -07002018 spin_lock_irq(&uport->lock);
2019 ops->stop_tx(uport);
2020 ops->set_mctrl(uport, 0);
2021 ops->stop_rx(uport);
2022 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 /*
2025 * Wait for the transmitter to empty.
2026 */
Alan Coxccce6de2009-09-19 13:13:30 -07002027 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 msleep(10);
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002029 if (!tries)
Alan Coxa46c9992008-02-08 04:18:53 -08002030 printk(KERN_ERR "%s%s%s%d: Unable to drain "
2031 "transmitter\n",
Alan Coxccce6de2009-09-19 13:13:30 -07002032 uport->dev ? dev_name(uport->dev) : "",
2033 uport->dev ? ": " : "",
David S. Miller84408382008-10-13 10:45:26 +01002034 drv->dev_name,
Alan Coxccce6de2009-09-19 13:13:30 -07002035 drv->tty_driver->name_base + uport->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Alan Coxccce6de2009-09-19 13:13:30 -07002037 ops->shutdown(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
2039
2040 /*
2041 * Disable the console device before suspending.
2042 */
Alan Coxccce6de2009-09-19 13:13:30 -07002043 if (uart_console(uport))
2044 console_stop(uport->cons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 uart_change_pm(state, 3);
2047
Ingo Molnare2862f62006-01-13 21:37:07 +00002048 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
2050 return 0;
2051}
2052
Alan Coxccce6de2009-09-19 13:13:30 -07002053int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Alan Coxccce6de2009-09-19 13:13:30 -07002055 struct uart_state *state = drv->state + uport->line;
2056 struct tty_port *port = &state->port;
Arjan van de Ven03a74dc2008-05-23 13:04:49 -07002057 struct device *tty_dev;
Alan Coxccce6de2009-09-19 13:13:30 -07002058 struct uart_match match = {uport, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Ingo Molnare2862f62006-01-13 21:37:07 +00002060 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Alan Coxccce6de2009-09-19 13:13:30 -07002062 if (!console_suspend_enabled && uart_console(uport)) {
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002063 /* no need to resume serial console, it wasn't suspended */
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002064 mutex_unlock(&state->mutex);
2065 return 0;
2066 }
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002067
Alan Coxccce6de2009-09-19 13:13:30 -07002068 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2069 if (!uport->suspended && device_may_wakeup(tty_dev)) {
2070 disable_irq_wake(uport->irq);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002071 mutex_unlock(&state->mutex);
2072 return 0;
2073 }
Alan Coxccce6de2009-09-19 13:13:30 -07002074 uport->suspended = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 /*
2077 * Re-enable the console device after suspending.
2078 */
Alan Coxccce6de2009-09-19 13:13:30 -07002079 if (uart_console(uport)) {
Alan Cox606d0992006-12-08 02:38:45 -08002080 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 /*
2083 * First try to use the console cflag setting.
2084 */
Alan Cox606d0992006-12-08 02:38:45 -08002085 memset(&termios, 0, sizeof(struct ktermios));
Alan Coxccce6de2009-09-19 13:13:30 -07002086 termios.c_cflag = uport->cons->cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 /*
2089 * If that's unset, use the tty termios setting.
2090 */
Alan Coxccce6de2009-09-19 13:13:30 -07002091 if (port->tty && termios.c_cflag == 0)
2092 termios = *port->tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Russell King9d778a62008-02-04 22:27:51 -08002094 uart_change_pm(state, 0);
Alan Coxccce6de2009-09-19 13:13:30 -07002095 uport->ops->set_termios(uport, &termios, NULL);
2096 console_start(uport->cons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
2098
Alan Coxccce6de2009-09-19 13:13:30 -07002099 if (port->flags & ASYNC_SUSPENDED) {
2100 const struct uart_ops *ops = uport->ops;
Russell Kingee31b332005-11-13 15:28:51 +00002101 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Russell King9d778a62008-02-04 22:27:51 -08002103 uart_change_pm(state, 0);
Alan Coxccce6de2009-09-19 13:13:30 -07002104 spin_lock_irq(&uport->lock);
2105 ops->set_mctrl(uport, 0);
2106 spin_unlock_irq(&uport->lock);
2107 ret = ops->startup(uport);
Russell Kingee31b332005-11-13 15:28:51 +00002108 if (ret == 0) {
2109 uart_change_speed(state, NULL);
Alan Coxccce6de2009-09-19 13:13:30 -07002110 spin_lock_irq(&uport->lock);
2111 ops->set_mctrl(uport, uport->mctrl);
2112 ops->start_tx(uport);
2113 spin_unlock_irq(&uport->lock);
2114 set_bit(ASYNCB_INITIALIZED, &port->flags);
Russell Kingee31b332005-11-13 15:28:51 +00002115 } else {
2116 /*
2117 * Failed to resume - maybe hardware went away?
2118 * Clear the "initialized" flag so we won't try
2119 * to call the low level drivers shutdown method.
2120 */
Russell Kingee31b332005-11-13 15:28:51 +00002121 uart_shutdown(state);
2122 }
Russell Kinga6b93a92006-10-01 17:17:40 +01002123
Alan Coxccce6de2009-09-19 13:13:30 -07002124 clear_bit(ASYNCB_SUSPENDED, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
2126
Ingo Molnare2862f62006-01-13 21:37:07 +00002127 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 return 0;
2130}
2131
2132static inline void
2133uart_report_port(struct uart_driver *drv, struct uart_port *port)
2134{
Russell King30b7a3b2005-09-03 15:30:21 +01002135 char address[64];
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 switch (port->iotype) {
2138 case UPIO_PORT:
Andrew Morton9bde10a2008-10-13 10:35:42 +01002139 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 break;
2141 case UPIO_HUB6:
Russell King30b7a3b2005-09-03 15:30:21 +01002142 snprintf(address, sizeof(address),
Andrew Morton9bde10a2008-10-13 10:35:42 +01002143 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 break;
2145 case UPIO_MEM:
2146 case UPIO_MEM32:
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002147 case UPIO_AU:
Zang Roy-r619113be91ec2006-06-30 02:29:58 -07002148 case UPIO_TSI:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002149 case UPIO_DWAPB:
Russell King30b7a3b2005-09-03 15:30:21 +01002150 snprintf(address, sizeof(address),
Josh Boyer4f640ef2007-07-23 18:43:44 -07002151 "MMIO 0x%llx", (unsigned long long)port->mapbase);
Russell King30b7a3b2005-09-03 15:30:21 +01002152 break;
2153 default:
2154 strlcpy(address, "*unknown*", sizeof(address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 break;
2156 }
Russell King30b7a3b2005-09-03 15:30:21 +01002157
Russell King0cf669d2005-10-31 11:42:22 +00002158 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
Kay Sievers4bfe0902009-01-06 10:44:37 -08002159 port->dev ? dev_name(port->dev) : "",
Russell King0cf669d2005-10-31 11:42:22 +00002160 port->dev ? ": " : "",
David S. Miller84408382008-10-13 10:45:26 +01002161 drv->dev_name,
2162 drv->tty_driver->name_base + port->line,
2163 address, port->irq, uart_type(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
2166static void
2167uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2168 struct uart_port *port)
2169{
2170 unsigned int flags;
2171
2172 /*
2173 * If there isn't a port here, don't do anything further.
2174 */
2175 if (!port->iobase && !port->mapbase && !port->membase)
2176 return;
2177
2178 /*
2179 * Now do the auto configuration stuff. Note that config_port
2180 * is expected to claim the resources and map the port for us.
2181 */
David Daney8e23fcc2009-01-02 13:49:54 +00002182 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (port->flags & UPF_AUTO_IRQ)
2184 flags |= UART_CONFIG_IRQ;
2185 if (port->flags & UPF_BOOT_AUTOCONF) {
David Daney8e23fcc2009-01-02 13:49:54 +00002186 if (!(port->flags & UPF_FIXED_TYPE)) {
2187 port->type = PORT_UNKNOWN;
2188 flags |= UART_CONFIG_TYPE;
2189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 port->ops->config_port(port, flags);
2191 }
2192
2193 if (port->type != PORT_UNKNOWN) {
2194 unsigned long flags;
2195
2196 uart_report_port(drv, port);
2197
George G. Davis3689a0e2007-02-14 00:33:06 -08002198 /* Power up port for set_mctrl() */
2199 uart_change_pm(state, 0);
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 /*
2202 * Ensure that the modem control lines are de-activated.
Yinghai Luc3e46422008-02-04 22:27:46 -08002203 * keep the DTR setting that is set in uart_set_options()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 * We probably don't need a spinlock around this, but
2205 */
2206 spin_lock_irqsave(&port->lock, flags);
Yinghai Luc3e46422008-02-04 22:27:46 -08002207 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 spin_unlock_irqrestore(&port->lock, flags);
2209
2210 /*
Russell King97d97222007-09-01 21:25:09 +01002211 * If this driver supports console, and it hasn't been
2212 * successfully registered yet, try to re-register it.
2213 * It may be that the port was not available.
2214 */
2215 if (port->cons && !(port->cons->flags & CON_ENABLED))
2216 register_console(port->cons);
2217
2218 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 * Power down all ports by default, except the
2220 * console if we have one.
2221 */
2222 if (!uart_console(port))
2223 uart_change_pm(state, 3);
2224 }
2225}
2226
Jason Wesself2d937f2008-04-17 20:05:37 +02002227#ifdef CONFIG_CONSOLE_POLL
2228
2229static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2230{
2231 struct uart_driver *drv = driver->driver_state;
2232 struct uart_state *state = drv->state + line;
2233 struct uart_port *port;
2234 int baud = 9600;
2235 int bits = 8;
2236 int parity = 'n';
2237 int flow = 'n';
2238
Alan Coxebd2c8f2009-09-19 13:13:28 -07002239 if (!state || !state->uart_port)
Jason Wesself2d937f2008-04-17 20:05:37 +02002240 return -1;
2241
Alan Coxebd2c8f2009-09-19 13:13:28 -07002242 port = state->uart_port;
Jason Wesself2d937f2008-04-17 20:05:37 +02002243 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2244 return -1;
2245
2246 if (options) {
2247 uart_parse_options(options, &baud, &parity, &bits, &flow);
2248 return uart_set_options(port, NULL, baud, parity, bits, flow);
2249 }
2250
2251 return 0;
2252}
2253
2254static int uart_poll_get_char(struct tty_driver *driver, int line)
2255{
2256 struct uart_driver *drv = driver->driver_state;
2257 struct uart_state *state = drv->state + line;
2258 struct uart_port *port;
2259
Alan Coxebd2c8f2009-09-19 13:13:28 -07002260 if (!state || !state->uart_port)
Jason Wesself2d937f2008-04-17 20:05:37 +02002261 return -1;
2262
Alan Coxebd2c8f2009-09-19 13:13:28 -07002263 port = state->uart_port;
Jason Wesself2d937f2008-04-17 20:05:37 +02002264 return port->ops->poll_get_char(port);
2265}
2266
2267static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2268{
2269 struct uart_driver *drv = driver->driver_state;
2270 struct uart_state *state = drv->state + line;
2271 struct uart_port *port;
2272
Alan Coxebd2c8f2009-09-19 13:13:28 -07002273 if (!state || !state->uart_port)
Jason Wesself2d937f2008-04-17 20:05:37 +02002274 return;
2275
Alan Coxebd2c8f2009-09-19 13:13:28 -07002276 port = state->uart_port;
Jason Wesself2d937f2008-04-17 20:05:37 +02002277 port->ops->poll_put_char(port, ch);
2278}
2279#endif
2280
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002281static const struct tty_operations uart_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 .open = uart_open,
2283 .close = uart_close,
2284 .write = uart_write,
2285 .put_char = uart_put_char,
2286 .flush_chars = uart_flush_chars,
2287 .write_room = uart_write_room,
2288 .chars_in_buffer= uart_chars_in_buffer,
2289 .flush_buffer = uart_flush_buffer,
2290 .ioctl = uart_ioctl,
2291 .throttle = uart_throttle,
2292 .unthrottle = uart_unthrottle,
2293 .send_xchar = uart_send_xchar,
2294 .set_termios = uart_set_termios,
Alan Cox64e91592008-06-03 15:18:54 +01002295 .set_ldisc = uart_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 .stop = uart_stop,
2297 .start = uart_start,
2298 .hangup = uart_hangup,
2299 .break_ctl = uart_break_ctl,
2300 .wait_until_sent= uart_wait_until_sent,
2301#ifdef CONFIG_PROC_FS
Alexey Dobriyand196a942009-03-31 15:19:21 -07002302 .proc_fops = &uart_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303#endif
2304 .tiocmget = uart_tiocmget,
2305 .tiocmset = uart_tiocmset,
Jason Wesself2d937f2008-04-17 20:05:37 +02002306#ifdef CONFIG_CONSOLE_POLL
2307 .poll_init = uart_poll_init,
2308 .poll_get_char = uart_poll_get_char,
2309 .poll_put_char = uart_poll_put_char,
2310#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311};
2312
2313/**
2314 * uart_register_driver - register a driver with the uart core layer
2315 * @drv: low level driver structure
2316 *
2317 * Register a uart driver with the core driver. We in turn register
2318 * with the tty layer, and initialise the core driver per-port state.
2319 *
2320 * We have a proc file in /proc/tty/driver which is named after the
2321 * normal driver.
2322 *
2323 * drv->port should be NULL, and the per-port structures should be
2324 * registered using uart_add_one_port after this call has succeeded.
2325 */
2326int uart_register_driver(struct uart_driver *drv)
2327{
2328 struct tty_driver *normal = NULL;
2329 int i, retval;
2330
2331 BUG_ON(drv->state);
2332
2333 /*
2334 * Maybe we should be using a slab cache for this, especially if
2335 * we have a large number of ports to handle.
2336 */
Burman Yan8f31bb32007-02-14 00:33:07 -08002337 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 retval = -ENOMEM;
2339 if (!drv->state)
2340 goto out;
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 normal = alloc_tty_driver(drv->nr);
2343 if (!normal)
2344 goto out;
2345
2346 drv->tty_driver = normal;
2347
2348 normal->owner = drv->owner;
2349 normal->driver_name = drv->driver_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 normal->name = drv->dev_name;
2351 normal->major = drv->major;
2352 normal->minor_start = drv->minor;
2353 normal->type = TTY_DRIVER_TYPE_SERIAL;
2354 normal->subtype = SERIAL_TYPE_NORMAL;
2355 normal->init_termios = tty_std_termios;
2356 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -08002357 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07002358 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 normal->driver_state = drv;
2360 tty_set_operations(normal, &uart_ops);
2361
2362 /*
2363 * Initialise the UART state(s).
2364 */
2365 for (i = 0; i < drv->nr; i++) {
2366 struct uart_state *state = drv->state + i;
2367
Ingo Molnare2862f62006-01-13 21:37:07 +00002368 mutex_init(&state->mutex);
Alan Coxf7519282009-01-02 13:49:21 +00002369
Alan Coxebd2c8f2009-09-19 13:13:28 -07002370 tty_port_init(&state->port);
Alan Cox5e99df52009-09-19 13:13:28 -07002371 state->port.close_delay = 500; /* .5 seconds */
2372 state->port.closing_wait = 30000; /* 30 seconds */
Alan Coxebd2c8f2009-09-19 13:13:28 -07002373 init_waitqueue_head(&state->delta_msr_wait);
2374 tasklet_init(&state->tlet, uart_tasklet_action,
Alan Coxf7519282009-01-02 13:49:21 +00002375 (unsigned long)state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
2377
2378 retval = tty_register_driver(normal);
2379 out:
2380 if (retval < 0) {
2381 put_tty_driver(normal);
2382 kfree(drv->state);
2383 }
2384 return retval;
2385}
2386
2387/**
2388 * uart_unregister_driver - remove a driver from the uart core layer
2389 * @drv: low level driver structure
2390 *
2391 * Remove all references to a driver from the core driver. The low
2392 * level driver must have removed all its ports via the
2393 * uart_remove_one_port() if it registered them with uart_add_one_port().
2394 * (ie, drv->port == NULL)
2395 */
2396void uart_unregister_driver(struct uart_driver *drv)
2397{
2398 struct tty_driver *p = drv->tty_driver;
2399 tty_unregister_driver(p);
2400 put_tty_driver(p);
2401 kfree(drv->state);
2402 drv->tty_driver = NULL;
2403}
2404
2405struct tty_driver *uart_console_device(struct console *co, int *index)
2406{
2407 struct uart_driver *p = co->data;
2408 *index = co->index;
2409 return p->tty_driver;
2410}
2411
2412/**
2413 * uart_add_one_port - attach a driver-defined port structure
2414 * @drv: pointer to the uart low level driver structure for this port
2415 * @port: uart port structure to use for this port.
2416 *
2417 * This allows the driver to register its own uart_port structure
2418 * with the core driver. The main purpose is to allow the low
2419 * level uart drivers to expand uart_port, rather than having yet
2420 * more levels of structures.
2421 */
2422int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2423{
2424 struct uart_state *state;
2425 int ret = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002426 struct device *tty_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 BUG_ON(in_interrupt());
2429
2430 if (port->line >= drv->nr)
2431 return -EINVAL;
2432
2433 state = drv->state + port->line;
2434
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002435 mutex_lock(&port_mutex);
Russell King68ac64c2006-04-30 11:13:50 +01002436 mutex_lock(&state->mutex);
Alan Coxebd2c8f2009-09-19 13:13:28 -07002437 if (state->uart_port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 ret = -EINVAL;
2439 goto out;
2440 }
2441
Alan Coxebd2c8f2009-09-19 13:13:28 -07002442 state->uart_port = port;
Russell King97d97222007-09-01 21:25:09 +01002443 state->pm_state = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 port->cons = drv->cons;
Alan Coxebd2c8f2009-09-19 13:13:28 -07002446 port->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Russell King976ecd12005-07-03 21:05:45 +01002448 /*
2449 * If this port is a console, then the spinlock is already
2450 * initialised.
2451 */
Ingo Molnar13e83592006-07-03 00:25:03 -07002452 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
Russell King976ecd12005-07-03 21:05:45 +01002453 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07002454 lockdep_set_class(&port->lock, &port_lock_key);
2455 }
Russell King976ecd12005-07-03 21:05:45 +01002456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 uart_configure_port(drv, state, port);
2458
2459 /*
2460 * Register the port whether it's detected or not. This allows
2461 * setserial to be used to alter this ports parameters.
2462 */
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002463 tty_dev = tty_register_device(drv->tty_driver, port->line, port->dev);
2464 if (likely(!IS_ERR(tty_dev))) {
Alan Stern74081f82008-03-19 22:35:13 +01002465 device_init_wakeup(tty_dev, 1);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002466 device_set_wakeup_enable(tty_dev, 0);
2467 } else
2468 printk(KERN_ERR "Cannot register tty device on line %d\n",
2469 port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 /*
Russell King68ac64c2006-04-30 11:13:50 +01002472 * Ensure UPF_DEAD is not set.
2473 */
2474 port->flags &= ~UPF_DEAD;
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 out:
Russell King68ac64c2006-04-30 11:13:50 +01002477 mutex_unlock(&state->mutex);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002478 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
2480 return ret;
2481}
2482
2483/**
2484 * uart_remove_one_port - detach a driver defined port structure
2485 * @drv: pointer to the uart low level driver structure for this port
2486 * @port: uart port structure for this port
2487 *
2488 * This unhooks (and hangs up) the specified port structure from the
2489 * core driver. No further calls will be made to the low-level code
2490 * for this port.
2491 */
2492int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2493{
2494 struct uart_state *state = drv->state + port->line;
2495
2496 BUG_ON(in_interrupt());
2497
Alan Coxebd2c8f2009-09-19 13:13:28 -07002498 if (state->uart_port != port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
Alan Coxebd2c8f2009-09-19 13:13:28 -07002500 state->uart_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002502 mutex_lock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
2504 /*
Russell King68ac64c2006-04-30 11:13:50 +01002505 * Mark the port "dead" - this prevents any opens from
2506 * succeeding while we shut down the port.
2507 */
2508 mutex_lock(&state->mutex);
2509 port->flags |= UPF_DEAD;
2510 mutex_unlock(&state->mutex);
2511
2512 /*
Greg Kroah-Hartmanaa4148c2005-06-20 21:15:16 -07002513 * Remove the devices from the tty layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 */
2515 tty_unregister_device(drv->tty_driver, port->line);
2516
Alan Coxebd2c8f2009-09-19 13:13:28 -07002517 if (state->port.tty)
2518 tty_vhangup(state->port.tty);
Russell King68ac64c2006-04-30 11:13:50 +01002519
2520 /*
Russell King68ac64c2006-04-30 11:13:50 +01002521 * Free the port IO and memory resources, if any.
2522 */
2523 if (port->type != PORT_UNKNOWN)
2524 port->ops->release_port(port);
2525
2526 /*
2527 * Indicate that there isn't a port here anymore.
2528 */
2529 port->type = PORT_UNKNOWN;
2530
2531 /*
2532 * Kill the tasklet, and free resources.
2533 */
Alan Coxebd2c8f2009-09-19 13:13:28 -07002534 tasklet_kill(&state->tlet);
Russell King68ac64c2006-04-30 11:13:50 +01002535
Alan Coxebd2c8f2009-09-19 13:13:28 -07002536 state->uart_port = NULL;
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002537 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 return 0;
2540}
2541
2542/*
2543 * Are the two ports equivalent?
2544 */
2545int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2546{
2547 if (port1->iotype != port2->iotype)
2548 return 0;
2549
2550 switch (port1->iotype) {
2551 case UPIO_PORT:
2552 return (port1->iobase == port2->iobase);
2553 case UPIO_HUB6:
2554 return (port1->iobase == port2->iobase) &&
2555 (port1->hub6 == port2->hub6);
2556 case UPIO_MEM:
Sergei Shtylyovd21b55d2006-08-28 19:49:03 +04002557 case UPIO_MEM32:
2558 case UPIO_AU:
2559 case UPIO_TSI:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002560 case UPIO_DWAPB:
Benjamin Herrenschmidt1624f002006-01-04 18:09:44 +00002561 return (port1->mapbase == port2->mapbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 }
2563 return 0;
2564}
2565EXPORT_SYMBOL(uart_match_port);
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567EXPORT_SYMBOL(uart_write_wakeup);
2568EXPORT_SYMBOL(uart_register_driver);
2569EXPORT_SYMBOL(uart_unregister_driver);
2570EXPORT_SYMBOL(uart_suspend_port);
2571EXPORT_SYMBOL(uart_resume_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572EXPORT_SYMBOL(uart_add_one_port);
2573EXPORT_SYMBOL(uart_remove_one_port);
2574
2575MODULE_DESCRIPTION("Serial driver core");
2576MODULE_LICENSE("GPL");