blob: e16d15343dfdf629d91d816245bbfa2ed85845bf [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 Coxbdc04e32009-09-19 13:13:31 -0700218 struct tty_port *port = &state->port;
219 struct tty_struct *tty = port->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Russell Kingee31b332005-11-13 15:28:51 +0000221 /*
222 * Set the TTY IO error marker
223 */
Alan Coxf7519282009-01-02 13:49:21 +0000224 if (tty)
225 set_bit(TTY_IO_ERROR, &tty->flags);
Russell Kingee31b332005-11-13 15:28:51 +0000226
Alan Coxbdc04e32009-09-19 13:13:31 -0700227 if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
Russell Kingee31b332005-11-13 15:28:51 +0000228 /*
229 * Turn off DTR and RTS early.
230 */
Alan Coxf7519282009-01-02 13:49:21 +0000231 if (!tty || (tty->termios->c_cflag & HUPCL))
Alan Coxccce6de2009-09-19 13:13:30 -0700232 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
Russell Kingee31b332005-11-13 15:28:51 +0000233
234 /*
235 * clear delta_msr_wait queue to avoid mem leaks: we may free
236 * the irq here so the queue might never be woken up. Note
237 * that we won't end up waiting on delta_msr_wait again since
238 * any outstanding file descriptors should be pointing at
239 * hung_up_tty_fops now.
240 */
Alan Coxbdc04e32009-09-19 13:13:31 -0700241 wake_up_interruptible(&port->delta_msr_wait);
Russell Kingee31b332005-11-13 15:28:51 +0000242
243 /*
244 * Free the IRQ and disable the port.
245 */
Alan Coxccce6de2009-09-19 13:13:30 -0700246 uport->ops->shutdown(uport);
Russell Kingee31b332005-11-13 15:28:51 +0000247
248 /*
249 * Ensure that the IRQ handler isn't running on another CPU.
250 */
Alan Coxccce6de2009-09-19 13:13:30 -0700251 synchronize_irq(uport->irq);
Russell Kingee31b332005-11-13 15:28:51 +0000252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 /*
Russell Kingee31b332005-11-13 15:28:51 +0000255 * kill off our tasklet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700257 tasklet_kill(&state->tlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /*
260 * Free the transmit buffer page.
261 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700262 if (state->xmit.buf) {
263 free_page((unsigned long)state->xmit.buf);
264 state->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268/**
269 * uart_update_timeout - update per-port FIFO timeout.
270 * @port: uart_port structure describing the port
271 * @cflag: termios cflag value
272 * @baud: speed of the port
273 *
274 * Set the port FIFO timeout value. The @cflag value should
275 * reflect the actual hardware settings.
276 */
277void
278uart_update_timeout(struct uart_port *port, unsigned int cflag,
279 unsigned int baud)
280{
281 unsigned int bits;
282
283 /* byte size and parity */
284 switch (cflag & CSIZE) {
285 case CS5:
286 bits = 7;
287 break;
288 case CS6:
289 bits = 8;
290 break;
291 case CS7:
292 bits = 9;
293 break;
294 default:
295 bits = 10;
Alan Coxa46c9992008-02-08 04:18:53 -0800296 break; /* CS8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
299 if (cflag & CSTOPB)
300 bits++;
301 if (cflag & PARENB)
302 bits++;
303
304 /*
305 * The total number of bits to be transmitted in the fifo.
306 */
307 bits = bits * port->fifosize;
308
309 /*
310 * Figure the timeout to send the above number of bits.
311 * Add .02 seconds of slop
312 */
313 port->timeout = (HZ * bits) / baud + HZ/50;
314}
315
316EXPORT_SYMBOL(uart_update_timeout);
317
318/**
319 * uart_get_baud_rate - return baud rate for a particular port
320 * @port: uart_port structure describing the port in question.
321 * @termios: desired termios settings.
322 * @old: old termios (or NULL)
323 * @min: minimum acceptable baud rate
324 * @max: maximum acceptable baud rate
325 *
326 * Decode the termios structure into a numeric baud rate,
327 * taking account of the magic 38400 baud rate (with spd_*
328 * flags), and mapping the %B0 rate to 9600 baud.
329 *
330 * If the new baud rate is invalid, try the old termios setting.
331 * If it's still invalid, we try 9600 baud.
332 *
333 * Update the @termios structure to reflect the baud rate
Alan Coxeb424fd2008-04-28 02:14:07 -0700334 * we're actually going to be using. Don't do this for the case
335 * where B0 is requested ("hang up").
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
337unsigned int
Alan Cox606d0992006-12-08 02:38:45 -0800338uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
339 struct ktermios *old, unsigned int min, unsigned int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 unsigned int try, baud, altbaud = 38400;
Alan Coxeb424fd2008-04-28 02:14:07 -0700342 int hung_up = 0;
Russell King0077d452006-01-21 23:03:28 +0000343 upf_t flags = port->flags & UPF_SPD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 if (flags == UPF_SPD_HI)
346 altbaud = 57600;
347 if (flags == UPF_SPD_VHI)
348 altbaud = 115200;
349 if (flags == UPF_SPD_SHI)
350 altbaud = 230400;
351 if (flags == UPF_SPD_WARP)
352 altbaud = 460800;
353
354 for (try = 0; try < 2; try++) {
355 baud = tty_termios_baud_rate(termios);
356
357 /*
358 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
359 * Die! Die! Die!
360 */
361 if (baud == 38400)
362 baud = altbaud;
363
364 /*
365 * Special case: B0 rate.
366 */
Alan Coxeb424fd2008-04-28 02:14:07 -0700367 if (baud == 0) {
368 hung_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 baud = 9600;
Alan Coxeb424fd2008-04-28 02:14:07 -0700370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 if (baud >= min && baud <= max)
373 return baud;
374
375 /*
376 * Oops, the quotient was zero. Try again with
377 * the old baud rate if possible.
378 */
379 termios->c_cflag &= ~CBAUD;
380 if (old) {
Alan Cox6d4d67b2008-02-04 22:27:53 -0800381 baud = tty_termios_baud_rate(old);
Alan Coxeb424fd2008-04-28 02:14:07 -0700382 if (!hung_up)
383 tty_termios_encode_baud_rate(termios,
384 baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 old = NULL;
386 continue;
387 }
388
389 /*
390 * As a last resort, if the quotient is zero,
391 * default to 9600 bps
392 */
Alan Coxeb424fd2008-04-28 02:14:07 -0700393 if (!hung_up)
394 tty_termios_encode_baud_rate(termios, 9600, 9600);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
397 return 0;
398}
399
400EXPORT_SYMBOL(uart_get_baud_rate);
401
402/**
403 * uart_get_divisor - return uart clock divisor
404 * @port: uart_port structure describing the port.
405 * @baud: desired baud rate
406 *
407 * Calculate the uart clock divisor for the port.
408 */
409unsigned int
410uart_get_divisor(struct uart_port *port, unsigned int baud)
411{
412 unsigned int quot;
413
414 /*
415 * Old custom speed handling.
416 */
417 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
418 quot = port->custom_divisor;
419 else
420 quot = (port->uartclk + (8 * baud)) / (16 * baud);
421
422 return quot;
423}
424
425EXPORT_SYMBOL(uart_get_divisor);
426
Alan Cox23d22ce2008-04-30 00:54:11 -0700427/* FIXME: Consistent locking policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static void
Alan Cox606d0992006-12-08 02:38:45 -0800429uart_change_speed(struct uart_state *state, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Alan Coxccce6de2009-09-19 13:13:30 -0700431 struct tty_port *port = &state->port;
432 struct tty_struct *tty = port->tty;
433 struct uart_port *uport = state->uart_port;
Alan Cox606d0992006-12-08 02:38:45 -0800434 struct ktermios *termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /*
437 * If we have no tty, termios, or the port does not exist,
438 * then we can't set the parameters for this port.
439 */
Alan Coxccce6de2009-09-19 13:13:30 -0700440 if (!tty || !tty->termios || uport->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return;
442
443 termios = tty->termios;
444
445 /*
446 * Set flags based on termios cflag
447 */
448 if (termios->c_cflag & CRTSCTS)
Alan Coxccce6de2009-09-19 13:13:30 -0700449 set_bit(ASYNCB_CTS_FLOW, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 else
Alan Coxccce6de2009-09-19 13:13:30 -0700451 clear_bit(ASYNCB_CTS_FLOW, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 if (termios->c_cflag & CLOCAL)
Alan Coxccce6de2009-09-19 13:13:30 -0700454 clear_bit(ASYNCB_CHECK_CD, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 else
Alan Coxccce6de2009-09-19 13:13:30 -0700456 set_bit(ASYNCB_CHECK_CD, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Alan Coxccce6de2009-09-19 13:13:30 -0700458 uport->ops->set_termios(uport, termios, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Alan Cox23d22ce2008-04-30 00:54:11 -0700461static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462__uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
463{
464 unsigned long flags;
Alan Cox23d22ce2008-04-30 00:54:11 -0700465 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 if (!circ->buf)
Alan Cox23d22ce2008-04-30 00:54:11 -0700468 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 spin_lock_irqsave(&port->lock, flags);
471 if (uart_circ_chars_free(circ) != 0) {
472 circ->buf[circ->head] = c;
473 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
Alan Cox23d22ce2008-04-30 00:54:11 -0700474 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 spin_unlock_irqrestore(&port->lock, flags);
Alan Cox23d22ce2008-04-30 00:54:11 -0700477 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Alan Cox23d22ce2008-04-30 00:54:11 -0700480static int uart_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct uart_state *state = tty->driver_data;
483
Alan Coxebd2c8f2009-09-19 13:13:28 -0700484 return __uart_put_char(state->uart_port, &state->xmit, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
487static void uart_flush_chars(struct tty_struct *tty)
488{
489 uart_start(tty);
490}
491
492static int
Pavel Machekd5f735e2006-03-07 21:55:20 -0800493uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct uart_state *state = tty->driver_data;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800496 struct uart_port *port;
497 struct circ_buf *circ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 unsigned long flags;
499 int c, ret = 0;
500
Pavel Machekd5f735e2006-03-07 21:55:20 -0800501 /*
502 * This means you called this function _after_ the port was
503 * closed. No cookie for you.
504 */
Alan Coxf7519282009-01-02 13:49:21 +0000505 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800506 WARN_ON(1);
507 return -EL3HLT;
508 }
509
Alan Coxebd2c8f2009-09-19 13:13:28 -0700510 port = state->uart_port;
511 circ = &state->xmit;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (!circ->buf)
514 return 0;
515
516 spin_lock_irqsave(&port->lock, flags);
517 while (1) {
518 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
519 if (count < c)
520 c = count;
521 if (c <= 0)
522 break;
523 memcpy(circ->buf + circ->head, buf, c);
524 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
525 buf += c;
526 count -= c;
527 ret += c;
528 }
529 spin_unlock_irqrestore(&port->lock, flags);
530
531 uart_start(tty);
532 return ret;
533}
534
535static int uart_write_room(struct tty_struct *tty)
536{
537 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700538 unsigned long flags;
539 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Alan Coxebd2c8f2009-09-19 13:13:28 -0700541 spin_lock_irqsave(&state->uart_port->lock, flags);
542 ret = uart_circ_chars_free(&state->xmit);
543 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700544 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
547static int uart_chars_in_buffer(struct tty_struct *tty)
548{
549 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700550 unsigned long flags;
551 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Alan Coxebd2c8f2009-09-19 13:13:28 -0700553 spin_lock_irqsave(&state->uart_port->lock, flags);
554 ret = uart_circ_chars_pending(&state->xmit);
555 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700556 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
559static void uart_flush_buffer(struct tty_struct *tty)
560{
561 struct uart_state *state = tty->driver_data;
Tetsuo Handa55d7b682008-05-06 20:42:27 -0700562 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 unsigned long flags;
564
Pavel Machekd5f735e2006-03-07 21:55:20 -0800565 /*
566 * This means you called this function _after_ the port was
567 * closed. No cookie for you.
568 */
Alan Coxf7519282009-01-02 13:49:21 +0000569 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800570 WARN_ON(1);
571 return;
572 }
573
Alan Coxebd2c8f2009-09-19 13:13:28 -0700574 port = state->uart_port;
Jiri Slabyeb3a1e12007-05-06 14:48:52 -0700575 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 spin_lock_irqsave(&port->lock, flags);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700578 uart_circ_clear(&state->xmit);
Haavard Skinnemoen6bb0e3a2008-07-16 21:52:36 +0100579 if (port->ops->flush_buffer)
580 port->ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 spin_unlock_irqrestore(&port->lock, flags);
582 tty_wakeup(tty);
583}
584
585/*
586 * This function is used to send a high-priority XON/XOFF character to
587 * the device
588 */
589static void uart_send_xchar(struct tty_struct *tty, char ch)
590{
591 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700592 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 unsigned long flags;
594
595 if (port->ops->send_xchar)
596 port->ops->send_xchar(port, ch);
597 else {
598 port->x_char = ch;
599 if (ch) {
600 spin_lock_irqsave(&port->lock, flags);
Russell Kingb129a8c2005-08-31 10:12:14 +0100601 port->ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 spin_unlock_irqrestore(&port->lock, flags);
603 }
604 }
605}
606
607static void uart_throttle(struct tty_struct *tty)
608{
609 struct uart_state *state = tty->driver_data;
610
611 if (I_IXOFF(tty))
612 uart_send_xchar(tty, STOP_CHAR(tty));
613
614 if (tty->termios->c_cflag & CRTSCTS)
Alan Coxebd2c8f2009-09-19 13:13:28 -0700615 uart_clear_mctrl(state->uart_port, TIOCM_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
618static void uart_unthrottle(struct tty_struct *tty)
619{
620 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700621 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 if (I_IXOFF(tty)) {
624 if (port->x_char)
625 port->x_char = 0;
626 else
627 uart_send_xchar(tty, START_CHAR(tty));
628 }
629
630 if (tty->termios->c_cflag & CRTSCTS)
631 uart_set_mctrl(port, TIOCM_RTS);
632}
633
634static int uart_get_info(struct uart_state *state,
635 struct serial_struct __user *retinfo)
636{
Alan Coxa2bceae2009-09-19 13:13:31 -0700637 struct uart_port *uport = state->uart_port;
638 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 struct serial_struct tmp;
640
641 memset(&tmp, 0, sizeof(tmp));
Alan Coxf34d7a52008-04-30 00:54:13 -0700642
643 /* Ensure the state we copy is consistent and no hardware changes
644 occur as we go */
Alan Coxa2bceae2009-09-19 13:13:31 -0700645 mutex_lock(&port->mutex);
Alan Coxf34d7a52008-04-30 00:54:13 -0700646
Alan Coxa2bceae2009-09-19 13:13:31 -0700647 tmp.type = uport->type;
648 tmp.line = uport->line;
649 tmp.port = uport->iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (HIGH_BITS_OFFSET)
Alan Coxa2bceae2009-09-19 13:13:31 -0700651 tmp.port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
652 tmp.irq = uport->irq;
653 tmp.flags = uport->flags;
654 tmp.xmit_fifo_size = uport->fifosize;
655 tmp.baud_base = uport->uartclk / 16;
656 tmp.close_delay = port->close_delay / 10;
657 tmp.closing_wait = port->closing_wait == USF_CLOSING_WAIT_NONE ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 ASYNC_CLOSING_WAIT_NONE :
Alan Coxa2bceae2009-09-19 13:13:31 -0700659 port->closing_wait / 10;
660 tmp.custom_divisor = uport->custom_divisor;
661 tmp.hub6 = uport->hub6;
662 tmp.io_type = uport->iotype;
663 tmp.iomem_reg_shift = uport->regshift;
664 tmp.iomem_base = (void *)(unsigned long)uport->mapbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Alan Coxa2bceae2009-09-19 13:13:31 -0700666 mutex_unlock(&port->mutex);
Alan Coxf34d7a52008-04-30 00:54:13 -0700667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
669 return -EFAULT;
670 return 0;
671}
672
673static int uart_set_info(struct uart_state *state,
674 struct serial_struct __user *newinfo)
675{
676 struct serial_struct new_serial;
Alan Cox46d57a42009-09-19 13:13:29 -0700677 struct uart_port *uport = state->uart_port;
678 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 unsigned long new_port;
Russell King0077d452006-01-21 23:03:28 +0000680 unsigned int change_irq, change_port, closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 unsigned int old_custom_divisor, close_delay;
Russell King0077d452006-01-21 23:03:28 +0000682 upf_t old_flags, new_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 int retval = 0;
684
685 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
686 return -EFAULT;
687
688 new_port = new_serial.port;
689 if (HIGH_BITS_OFFSET)
690 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
691
692 new_serial.irq = irq_canonicalize(new_serial.irq);
693 close_delay = new_serial.close_delay * 10;
694 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
695 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
696
697 /*
Alan Cox91312cd2009-09-19 13:13:29 -0700698 * This semaphore protects port->count. It is also
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * very useful to prevent opens. Also, take the
700 * port configuration semaphore to make sure that a
701 * module insertion/removal doesn't change anything
702 * under us.
703 */
Alan Coxa2bceae2009-09-19 13:13:31 -0700704 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Alan Cox46d57a42009-09-19 13:13:29 -0700706 change_irq = !(uport->flags & UPF_FIXED_PORT)
707 && new_serial.irq != uport->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /*
710 * Since changing the 'type' of the port changes its resource
711 * allocations, we should treat type changes the same as
712 * IO port changes.
713 */
Alan Cox46d57a42009-09-19 13:13:29 -0700714 change_port = !(uport->flags & UPF_FIXED_PORT)
715 && (new_port != uport->iobase ||
716 (unsigned long)new_serial.iomem_base != uport->mapbase ||
717 new_serial.hub6 != uport->hub6 ||
718 new_serial.io_type != uport->iotype ||
719 new_serial.iomem_reg_shift != uport->regshift ||
720 new_serial.type != uport->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Alan Cox46d57a42009-09-19 13:13:29 -0700722 old_flags = uport->flags;
Russell King0077d452006-01-21 23:03:28 +0000723 new_flags = new_serial.flags;
Alan Cox46d57a42009-09-19 13:13:29 -0700724 old_custom_divisor = uport->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 if (!capable(CAP_SYS_ADMIN)) {
727 retval = -EPERM;
728 if (change_irq || change_port ||
Alan Cox46d57a42009-09-19 13:13:29 -0700729 (new_serial.baud_base != uport->uartclk / 16) ||
730 (close_delay != port->close_delay) ||
731 (closing_wait != port->closing_wait) ||
Russell King947deee2006-07-02 20:45:51 +0100732 (new_serial.xmit_fifo_size &&
Alan Cox46d57a42009-09-19 13:13:29 -0700733 new_serial.xmit_fifo_size != uport->fifosize) ||
Russell King0077d452006-01-21 23:03:28 +0000734 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 goto exit;
Alan Cox46d57a42009-09-19 13:13:29 -0700736 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000737 (new_flags & UPF_USR_MASK));
Alan Cox46d57a42009-09-19 13:13:29 -0700738 uport->custom_divisor = new_serial.custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 goto check_and_exit;
740 }
741
742 /*
743 * Ask the low level driver to verify the settings.
744 */
Alan Cox46d57a42009-09-19 13:13:29 -0700745 if (uport->ops->verify_port)
746 retval = uport->ops->verify_port(uport, &new_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Yinghai Lua62c4132008-08-19 20:49:55 -0700748 if ((new_serial.irq >= nr_irqs) || (new_serial.irq < 0) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 (new_serial.baud_base < 9600))
750 retval = -EINVAL;
751
752 if (retval)
753 goto exit;
754
755 if (change_port || change_irq) {
756 retval = -EBUSY;
757
758 /*
759 * Make sure that we are the sole user of this port.
760 */
761 if (uart_users(state) > 1)
762 goto exit;
763
764 /*
765 * We need to shutdown the serial port at the old
766 * port/type/irq combination.
767 */
768 uart_shutdown(state);
769 }
770
771 if (change_port) {
772 unsigned long old_iobase, old_mapbase;
773 unsigned int old_type, old_iotype, old_hub6, old_shift;
774
Alan Cox46d57a42009-09-19 13:13:29 -0700775 old_iobase = uport->iobase;
776 old_mapbase = uport->mapbase;
777 old_type = uport->type;
778 old_hub6 = uport->hub6;
779 old_iotype = uport->iotype;
780 old_shift = uport->regshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 /*
783 * Free and release old regions
784 */
785 if (old_type != PORT_UNKNOWN)
Alan Cox46d57a42009-09-19 13:13:29 -0700786 uport->ops->release_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Alan Cox46d57a42009-09-19 13:13:29 -0700788 uport->iobase = new_port;
789 uport->type = new_serial.type;
790 uport->hub6 = new_serial.hub6;
791 uport->iotype = new_serial.io_type;
792 uport->regshift = new_serial.iomem_reg_shift;
793 uport->mapbase = (unsigned long)new_serial.iomem_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 /*
796 * Claim and map the new regions
797 */
Alan Cox46d57a42009-09-19 13:13:29 -0700798 if (uport->type != PORT_UNKNOWN) {
799 retval = uport->ops->request_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 } else {
801 /* Always success - Jean II */
802 retval = 0;
803 }
804
805 /*
806 * If we fail to request resources for the
807 * new port, try to restore the old settings.
808 */
809 if (retval && old_type != PORT_UNKNOWN) {
Alan Cox46d57a42009-09-19 13:13:29 -0700810 uport->iobase = old_iobase;
811 uport->type = old_type;
812 uport->hub6 = old_hub6;
813 uport->iotype = old_iotype;
814 uport->regshift = old_shift;
815 uport->mapbase = old_mapbase;
816 retval = uport->ops->request_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /*
818 * If we failed to restore the old settings,
819 * we fail like this.
820 */
821 if (retval)
Alan Cox46d57a42009-09-19 13:13:29 -0700822 uport->type = PORT_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 /*
825 * We failed anyway.
826 */
827 retval = -EBUSY;
Alan Coxa46c9992008-02-08 04:18:53 -0800828 /* Added to return the correct error -Ram Gupta */
829 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831 }
832
David Gibsonabb4a232007-05-06 14:48:49 -0700833 if (change_irq)
Alan Cox46d57a42009-09-19 13:13:29 -0700834 uport->irq = new_serial.irq;
835 if (!(uport->flags & UPF_FIXED_PORT))
836 uport->uartclk = new_serial.baud_base * 16;
837 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000838 (new_flags & UPF_CHANGE_MASK);
Alan Cox46d57a42009-09-19 13:13:29 -0700839 uport->custom_divisor = new_serial.custom_divisor;
840 port->close_delay = close_delay;
841 port->closing_wait = closing_wait;
Russell King947deee2006-07-02 20:45:51 +0100842 if (new_serial.xmit_fifo_size)
Alan Cox46d57a42009-09-19 13:13:29 -0700843 uport->fifosize = new_serial.xmit_fifo_size;
844 if (port->tty)
845 port->tty->low_latency =
846 (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 check_and_exit:
849 retval = 0;
Alan Cox46d57a42009-09-19 13:13:29 -0700850 if (uport->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 goto exit;
Alan Coxccce6de2009-09-19 13:13:30 -0700852 if (port->flags & ASYNC_INITIALIZED) {
Alan Cox46d57a42009-09-19 13:13:29 -0700853 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
854 old_custom_divisor != uport->custom_divisor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /*
856 * If they're setting up a custom divisor or speed,
857 * instead of clearing it, then bitch about it. No
858 * need to rate-limit; it's CAP_SYS_ADMIN only.
859 */
Alan Cox46d57a42009-09-19 13:13:29 -0700860 if (uport->flags & UPF_SPD_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 char buf[64];
862 printk(KERN_NOTICE
863 "%s sets custom speed on %s. This "
864 "is deprecated.\n", current->comm,
Alan Cox46d57a42009-09-19 13:13:29 -0700865 tty_name(port->tty, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867 uart_change_speed(state, NULL);
868 }
869 } else
870 retval = uart_startup(state, 1);
871 exit:
Alan Coxa2bceae2009-09-19 13:13:31 -0700872 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return retval;
874}
875
876
877/*
878 * uart_get_lsr_info - get line status register info.
879 * Note: uart_ioctl protects us against hangups.
880 */
881static int uart_get_lsr_info(struct uart_state *state,
882 unsigned int __user *value)
883{
Alan Cox46d57a42009-09-19 13:13:29 -0700884 struct uart_port *uport = state->uart_port;
885 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 unsigned int result;
887
Alan Cox46d57a42009-09-19 13:13:29 -0700888 result = uport->ops->tx_empty(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 /*
891 * If we're about to load something into the transmit
892 * register, we'll pretend the transmitter isn't empty to
893 * avoid a race condition (depending on when the transmit
894 * interrupt happens).
895 */
Alan Cox46d57a42009-09-19 13:13:29 -0700896 if (uport->x_char ||
Alan Coxebd2c8f2009-09-19 13:13:28 -0700897 ((uart_circ_chars_pending(&state->xmit) > 0) &&
Alan Cox46d57a42009-09-19 13:13:29 -0700898 !port->tty->stopped && !port->tty->hw_stopped))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 result &= ~TIOCSER_TEMT;
Alan Coxa46c9992008-02-08 04:18:53 -0800900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return put_user(result, value);
902}
903
904static int uart_tiocmget(struct tty_struct *tty, struct file *file)
905{
906 struct uart_state *state = tty->driver_data;
Alan Coxa2bceae2009-09-19 13:13:31 -0700907 struct tty_port *port = &state->port;
Alan Cox46d57a42009-09-19 13:13:29 -0700908 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 int result = -EIO;
910
Alan Coxa2bceae2009-09-19 13:13:31 -0700911 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if ((!file || !tty_hung_up_p(file)) &&
913 !(tty->flags & (1 << TTY_IO_ERROR))) {
Alan Cox46d57a42009-09-19 13:13:29 -0700914 result = uport->mctrl;
Russell Kingc5f46442005-06-29 09:42:38 +0100915
Alan Cox46d57a42009-09-19 13:13:29 -0700916 spin_lock_irq(&uport->lock);
917 result |= uport->ops->get_mctrl(uport);
918 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
Alan Coxa2bceae2009-09-19 13:13:31 -0700920 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 return result;
923}
924
925static int
926uart_tiocmset(struct tty_struct *tty, struct file *file,
927 unsigned int set, unsigned int clear)
928{
929 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -0700930 struct uart_port *uport = state->uart_port;
Alan Coxa2bceae2009-09-19 13:13:31 -0700931 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 int ret = -EIO;
933
Alan Coxa2bceae2009-09-19 13:13:31 -0700934 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if ((!file || !tty_hung_up_p(file)) &&
936 !(tty->flags & (1 << TTY_IO_ERROR))) {
Alan Cox46d57a42009-09-19 13:13:29 -0700937 uart_update_mctrl(uport, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 ret = 0;
939 }
Alan Coxa2bceae2009-09-19 13:13:31 -0700940 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return ret;
942}
943
Alan Cox9e989662008-07-22 11:18:03 +0100944static int uart_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 struct uart_state *state = tty->driver_data;
Alan Coxa2bceae2009-09-19 13:13:31 -0700947 struct tty_port *port = &state->port;
Alan Cox46d57a42009-09-19 13:13:29 -0700948 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Alan Coxa2bceae2009-09-19 13:13:31 -0700950 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Alan Cox46d57a42009-09-19 13:13:29 -0700952 if (uport->type != PORT_UNKNOWN)
953 uport->ops->break_ctl(uport, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Alan Coxa2bceae2009-09-19 13:13:31 -0700955 mutex_unlock(&port->mutex);
Alan Cox9e989662008-07-22 11:18:03 +0100956 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
959static int uart_do_autoconfig(struct uart_state *state)
960{
Alan Cox46d57a42009-09-19 13:13:29 -0700961 struct uart_port *uport = state->uart_port;
Alan Coxa2bceae2009-09-19 13:13:31 -0700962 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 int flags, ret;
964
965 if (!capable(CAP_SYS_ADMIN))
966 return -EPERM;
967
968 /*
969 * Take the per-port semaphore. This prevents count from
970 * changing, and hence any extra opens of the port while
971 * we're auto-configuring.
972 */
Alan Coxa2bceae2009-09-19 13:13:31 -0700973 if (mutex_lock_interruptible(&port->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return -ERESTARTSYS;
975
976 ret = -EBUSY;
977 if (uart_users(state) == 1) {
978 uart_shutdown(state);
979
980 /*
981 * If we already have a port type configured,
982 * we must release its resources.
983 */
Alan Cox46d57a42009-09-19 13:13:29 -0700984 if (uport->type != PORT_UNKNOWN)
985 uport->ops->release_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 flags = UART_CONFIG_TYPE;
Alan Cox46d57a42009-09-19 13:13:29 -0700988 if (uport->flags & UPF_AUTO_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 flags |= UART_CONFIG_IRQ;
990
991 /*
992 * This will claim the ports resources if
993 * a port is found.
994 */
Alan Cox46d57a42009-09-19 13:13:29 -0700995 uport->ops->config_port(uport, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 ret = uart_startup(state, 1);
998 }
Alan Coxa2bceae2009-09-19 13:13:31 -0700999 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return ret;
1001}
1002
1003/*
1004 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1005 * - mask passed in arg for lines of interest
1006 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1007 * Caller should use TIOCGICOUNT to see which one it was
Alan Coxbdc04e32009-09-19 13:13:31 -07001008 *
1009 * FIXME: This wants extracting into a common all driver implementation
1010 * of TIOCMWAIT using tty_port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 */
1012static int
1013uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1014{
Alan Cox46d57a42009-09-19 13:13:29 -07001015 struct uart_port *uport = state->uart_port;
Alan Coxbdc04e32009-09-19 13:13:31 -07001016 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 DECLARE_WAITQUEUE(wait, current);
1018 struct uart_icount cprev, cnow;
1019 int ret;
1020
1021 /*
1022 * note the counters on entry
1023 */
Alan Cox46d57a42009-09-19 13:13:29 -07001024 spin_lock_irq(&uport->lock);
1025 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 /*
1028 * Force modem status interrupts on
1029 */
Alan Cox46d57a42009-09-19 13:13:29 -07001030 uport->ops->enable_ms(uport);
1031 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Alan Coxbdc04e32009-09-19 13:13:31 -07001033 add_wait_queue(&port->delta_msr_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 for (;;) {
Alan Cox46d57a42009-09-19 13:13:29 -07001035 spin_lock_irq(&uport->lock);
1036 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1037 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 set_current_state(TASK_INTERRUPTIBLE);
1040
1041 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1042 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1043 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1044 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Alan Coxa46c9992008-02-08 04:18:53 -08001045 ret = 0;
1046 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048
1049 schedule();
1050
1051 /* see if a signal did it */
1052 if (signal_pending(current)) {
1053 ret = -ERESTARTSYS;
1054 break;
1055 }
1056
1057 cprev = cnow;
1058 }
1059
1060 current->state = TASK_RUNNING;
Alan Coxbdc04e32009-09-19 13:13:31 -07001061 remove_wait_queue(&port->delta_msr_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 return ret;
1064}
1065
1066/*
1067 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1068 * Return: write counters to the user passed counter struct
1069 * NB: both 1->0 and 0->1 transitions are counted except for
1070 * RI where only 0->1 is counted.
1071 */
1072static int uart_get_count(struct uart_state *state,
1073 struct serial_icounter_struct __user *icnt)
1074{
1075 struct serial_icounter_struct icount;
1076 struct uart_icount cnow;
Alan Cox46d57a42009-09-19 13:13:29 -07001077 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Alan Cox46d57a42009-09-19 13:13:29 -07001079 spin_lock_irq(&uport->lock);
1080 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1081 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 icount.cts = cnow.cts;
1084 icount.dsr = cnow.dsr;
1085 icount.rng = cnow.rng;
1086 icount.dcd = cnow.dcd;
1087 icount.rx = cnow.rx;
1088 icount.tx = cnow.tx;
1089 icount.frame = cnow.frame;
1090 icount.overrun = cnow.overrun;
1091 icount.parity = cnow.parity;
1092 icount.brk = cnow.brk;
1093 icount.buf_overrun = cnow.buf_overrun;
1094
1095 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1096}
1097
1098/*
Alan Coxe5238442008-04-30 00:53:28 -07001099 * Called via sys_ioctl. We can use spin_lock_irq() here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 */
1101static int
1102uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1103 unsigned long arg)
1104{
1105 struct uart_state *state = tty->driver_data;
Alan Coxa2bceae2009-09-19 13:13:31 -07001106 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 void __user *uarg = (void __user *)arg;
1108 int ret = -ENOIOCTLCMD;
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 /*
1112 * These ioctls don't rely on the hardware to be present.
1113 */
1114 switch (cmd) {
1115 case TIOCGSERIAL:
1116 ret = uart_get_info(state, uarg);
1117 break;
1118
1119 case TIOCSSERIAL:
1120 ret = uart_set_info(state, uarg);
1121 break;
1122
1123 case TIOCSERCONFIG:
1124 ret = uart_do_autoconfig(state);
1125 break;
1126
1127 case TIOCSERGWILD: /* obsolete */
1128 case TIOCSERSWILD: /* obsolete */
1129 ret = 0;
1130 break;
1131 }
1132
1133 if (ret != -ENOIOCTLCMD)
1134 goto out;
1135
1136 if (tty->flags & (1 << TTY_IO_ERROR)) {
1137 ret = -EIO;
1138 goto out;
1139 }
1140
1141 /*
1142 * The following should only be used when hardware is present.
1143 */
1144 switch (cmd) {
1145 case TIOCMIWAIT:
1146 ret = uart_wait_modem_status(state, arg);
1147 break;
1148
1149 case TIOCGICOUNT:
1150 ret = uart_get_count(state, uarg);
1151 break;
1152 }
1153
1154 if (ret != -ENOIOCTLCMD)
1155 goto out;
1156
Alan Coxa2bceae2009-09-19 13:13:31 -07001157 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 if (tty_hung_up_p(filp)) {
1160 ret = -EIO;
1161 goto out_up;
1162 }
1163
1164 /*
1165 * All these rely on hardware being present and need to be
1166 * protected against the tty being hung up.
1167 */
1168 switch (cmd) {
1169 case TIOCSERGETLSR: /* Get line status register */
1170 ret = uart_get_lsr_info(state, uarg);
1171 break;
1172
1173 default: {
Alan Cox46d57a42009-09-19 13:13:29 -07001174 struct uart_port *uport = state->uart_port;
1175 if (uport->ops->ioctl)
1176 ret = uport->ops->ioctl(uport, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 break;
1178 }
1179 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001180out_up:
Alan Coxa2bceae2009-09-19 13:13:31 -07001181 mutex_unlock(&port->mutex);
Alan Coxf34d7a52008-04-30 00:54:13 -07001182out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return ret;
1184}
1185
Linus Torvaldsedeb2802008-06-04 10:35:03 -07001186static void uart_set_ldisc(struct tty_struct *tty)
Alan Cox64e91592008-06-03 15:18:54 +01001187{
1188 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -07001189 struct uart_port *uport = state->uart_port;
Alan Cox64e91592008-06-03 15:18:54 +01001190
Alan Cox46d57a42009-09-19 13:13:29 -07001191 if (uport->ops->set_ldisc)
1192 uport->ops->set_ldisc(uport);
Alan Cox64e91592008-06-03 15:18:54 +01001193}
1194
Alan Coxa46c9992008-02-08 04:18:53 -08001195static void uart_set_termios(struct tty_struct *tty,
1196 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
1198 struct uart_state *state = tty->driver_data;
1199 unsigned long flags;
1200 unsigned int cflag = tty->termios->c_cflag;
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 /*
1204 * These are the bits that are used to setup various
David Woodhouse20620d62007-08-22 14:01:11 -07001205 * flags in the low level driver. We can ignore the Bfoo
1206 * bits in c_cflag; c_[io]speed will always be set
1207 * appropriately by set_termios() in tty_ioctl.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 */
1209#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if ((cflag ^ old_termios->c_cflag) == 0 &&
David Woodhouse20620d62007-08-22 14:01:11 -07001211 tty->termios->c_ospeed == old_termios->c_ospeed &&
1212 tty->termios->c_ispeed == old_termios->c_ispeed &&
Alan Coxe5238442008-04-30 00:53:28 -07001213 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return;
Alan Coxe5238442008-04-30 00:53:28 -07001215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 uart_change_speed(state, old_termios);
1218
1219 /* Handle transition to B0 status */
1220 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
Alan Coxebd2c8f2009-09-19 13:13:28 -07001221 uart_clear_mctrl(state->uart_port, TIOCM_RTS | TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 /* Handle transition away from B0 status */
1224 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1225 unsigned int mask = TIOCM_DTR;
1226 if (!(cflag & CRTSCTS) ||
1227 !test_bit(TTY_THROTTLED, &tty->flags))
1228 mask |= TIOCM_RTS;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001229 uart_set_mctrl(state->uart_port, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231
1232 /* Handle turning off CRTSCTS */
1233 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
Alan Coxebd2c8f2009-09-19 13:13:28 -07001234 spin_lock_irqsave(&state->uart_port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 tty->hw_stopped = 0;
1236 __uart_start(tty);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001237 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239
Russell King0dd7a1a2005-06-29 18:40:53 +01001240 /* Handle turning on CRTSCTS */
1241 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
Alan Coxebd2c8f2009-09-19 13:13:28 -07001242 spin_lock_irqsave(&state->uart_port->lock, flags);
1243 if (!(state->uart_port->ops->get_mctrl(state->uart_port) & TIOCM_CTS)) {
Russell King0dd7a1a2005-06-29 18:40:53 +01001244 tty->hw_stopped = 1;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001245 state->uart_port->ops->stop_tx(state->uart_port);
Russell King0dd7a1a2005-06-29 18:40:53 +01001246 }
Alan Coxebd2c8f2009-09-19 13:13:28 -07001247 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Russell King0dd7a1a2005-06-29 18:40:53 +01001248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249#if 0
1250 /*
1251 * No need to wake up processes in open wait, since they
1252 * sample the CLOCAL flag once, and don't recheck it.
1253 * XXX It's not clear whether the current behavior is correct
1254 * or not. Hence, this may change.....
1255 */
1256 if (!(old_termios->c_cflag & CLOCAL) &&
1257 (tty->termios->c_cflag & CLOCAL))
Alan Coxebd2c8f2009-09-19 13:13:28 -07001258 wake_up_interruptible(&state->uart_port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259#endif
1260}
1261
1262/*
1263 * In 2.4.5, calls to this will be serialized via the BKL in
1264 * linux/drivers/char/tty_io.c:tty_release()
1265 * linux/drivers/char/tty_io.c:do_tty_handup()
1266 */
1267static void uart_close(struct tty_struct *tty, struct file *filp)
1268{
1269 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -07001270 struct tty_port *port;
1271 struct uart_port *uport;
Alan Coxa46c9992008-02-08 04:18:53 -08001272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 BUG_ON(!kernel_locked());
1274
Alan Cox46d57a42009-09-19 13:13:29 -07001275 uport = state->uart_port;
1276 port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Alan Cox46d57a42009-09-19 13:13:29 -07001278 pr_debug("uart_close(%d) called\n", uport->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Alan Coxa2bceae2009-09-19 13:13:31 -07001280 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 if (tty_hung_up_p(filp))
1283 goto done;
1284
Alan Cox91312cd2009-09-19 13:13:29 -07001285 if ((tty->count == 1) && (port->count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 /*
1287 * Uh, oh. tty->count is 1, which means that the tty
Alan Cox91312cd2009-09-19 13:13:29 -07001288 * structure will be freed. port->count should always
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 * be one in these conditions. If it's greater than
1290 * one, we've got real problems, since it means the
1291 * serial port won't be shutdown.
1292 */
1293 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
Alan Cox91312cd2009-09-19 13:13:29 -07001294 "port->count is %d\n", port->count);
1295 port->count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Alan Cox91312cd2009-09-19 13:13:29 -07001297 if (--port->count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
Alan Cox91312cd2009-09-19 13:13:29 -07001299 tty->name, port->count);
1300 port->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
Alan Cox91312cd2009-09-19 13:13:29 -07001302 if (port->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 goto done;
1304
1305 /*
1306 * Now we wait for the transmit buffer to clear; and we notify
1307 * the line discipline to only process XON/XOFF characters by
1308 * setting tty->closing.
1309 */
1310 tty->closing = 1;
1311
Alan Cox46d57a42009-09-19 13:13:29 -07001312 if (port->closing_wait != USF_CLOSING_WAIT_NONE)
1313 tty_wait_until_sent(tty, msecs_to_jiffies(port->closing_wait));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 /*
1316 * At this point, we stop accepting input. To do this, we
1317 * disable the receive line status interrupts.
1318 */
Alan Coxccce6de2009-09-19 13:13:30 -07001319 if (port->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 unsigned long flags;
1321 spin_lock_irqsave(&port->lock, flags);
Alan Cox46d57a42009-09-19 13:13:29 -07001322 uport->ops->stop_rx(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 spin_unlock_irqrestore(&port->lock, flags);
1324 /*
1325 * Before we drop DTR, make sure the UART transmitter
1326 * has completely drained; this is especially
1327 * important if there is a transmit FIFO!
1328 */
Alan Cox46d57a42009-09-19 13:13:29 -07001329 uart_wait_until_sent(tty, uport->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331
1332 uart_shutdown(state);
1333 uart_flush_buffer(tty);
1334
Alan Coxa46c9992008-02-08 04:18:53 -08001335 tty_ldisc_flush(tty);
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 tty->closing = 0;
Alan Cox46d57a42009-09-19 13:13:29 -07001338 port->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Alan Cox46d57a42009-09-19 13:13:29 -07001340 if (port->blocked_open) {
1341 if (port->close_delay)
1342 msleep_interruptible(port->close_delay);
1343 } else if (!uart_console(uport)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 uart_change_pm(state, 3);
1345 }
1346
1347 /*
1348 * Wake up anyone trying to open this port.
1349 */
Alan Coxccce6de2009-09-19 13:13:30 -07001350 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
Alan Cox46d57a42009-09-19 13:13:29 -07001351 wake_up_interruptible(&port->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Alan Cox46d57a42009-09-19 13:13:29 -07001353done:
Alan Coxa2bceae2009-09-19 13:13:31 -07001354 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
1357static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1358{
1359 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001360 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 unsigned long char_time, expire;
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1364 return;
1365
Alan Coxf34d7a52008-04-30 00:54:13 -07001366 lock_kernel();
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 /*
1369 * Set the check interval to be 1/5 of the estimated time to
1370 * send a single character, and make it at least 1. The check
1371 * interval should also be less than the timeout.
1372 *
1373 * Note: we have to use pretty tight timings here to satisfy
1374 * the NIST-PCTS.
1375 */
1376 char_time = (port->timeout - HZ/50) / port->fifosize;
1377 char_time = char_time / 5;
1378 if (char_time == 0)
1379 char_time = 1;
1380 if (timeout && timeout < char_time)
1381 char_time = timeout;
1382
1383 /*
1384 * If the transmitter hasn't cleared in twice the approximate
1385 * amount of time to send the entire FIFO, it probably won't
1386 * ever clear. This assumes the UART isn't doing flow
1387 * control, which is currently the case. Hence, if it ever
1388 * takes longer than port->timeout, this is probably due to a
1389 * UART bug of some kind. So, we clamp the timeout parameter at
1390 * 2*port->timeout.
1391 */
1392 if (timeout == 0 || timeout > 2 * port->timeout)
1393 timeout = 2 * port->timeout;
1394
1395 expire = jiffies + timeout;
1396
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001397 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
Alan Coxa46c9992008-02-08 04:18:53 -08001398 port->line, jiffies, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 /*
1401 * Check whether the transmitter is empty every 'char_time'.
1402 * 'timeout' / 'expire' give us the maximum amount of time
1403 * we wait.
1404 */
1405 while (!port->ops->tx_empty(port)) {
1406 msleep_interruptible(jiffies_to_msecs(char_time));
1407 if (signal_pending(current))
1408 break;
1409 if (time_after(jiffies, expire))
1410 break;
1411 }
1412 set_current_state(TASK_RUNNING); /* might not be needed */
Alan Coxf34d7a52008-04-30 00:54:13 -07001413 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
1416/*
1417 * This is called with the BKL held in
1418 * linux/drivers/char/tty_io.c:do_tty_hangup()
1419 * We're called from the eventd thread, so we can sleep for
1420 * a _short_ time only.
1421 */
1422static void uart_hangup(struct tty_struct *tty)
1423{
1424 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -07001425 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 BUG_ON(!kernel_locked());
Alan Coxebd2c8f2009-09-19 13:13:28 -07001428 pr_debug("uart_hangup(%d)\n", state->uart_port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Alan Coxa2bceae2009-09-19 13:13:31 -07001430 mutex_lock(&port->mutex);
Alan Coxccce6de2009-09-19 13:13:30 -07001431 if (port->flags & ASYNC_NORMAL_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 uart_flush_buffer(tty);
1433 uart_shutdown(state);
Alan Cox91312cd2009-09-19 13:13:29 -07001434 port->count = 0;
Alan Coxccce6de2009-09-19 13:13:30 -07001435 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
Alan Cox46d57a42009-09-19 13:13:29 -07001436 port->tty = NULL;
1437 wake_up_interruptible(&port->open_wait);
Alan Coxbdc04e32009-09-19 13:13:31 -07001438 wake_up_interruptible(&port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
Alan Coxa2bceae2009-09-19 13:13:31 -07001440 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
1443/*
1444 * Copy across the serial console cflag setting into the termios settings
1445 * for the initial open of the port. This allows continuity between the
1446 * kernel settings, and the settings init adopts when it opens the port
1447 * for the first time.
1448 */
1449static void uart_update_termios(struct uart_state *state)
1450{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001451 struct tty_struct *tty = state->port.tty;
1452 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 if (uart_console(port) && port->cons->cflag) {
1455 tty->termios->c_cflag = port->cons->cflag;
1456 port->cons->cflag = 0;
1457 }
1458
1459 /*
1460 * If the device failed to grab its irq resources,
1461 * or some other error occurred, don't try to talk
1462 * to the port hardware.
1463 */
1464 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1465 /*
1466 * Make termios settings take effect.
1467 */
1468 uart_change_speed(state, NULL);
1469
1470 /*
1471 * And finally enable the RTS and DTR signals.
1472 */
1473 if (tty->termios->c_cflag & CBAUD)
1474 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1475 }
1476}
1477
1478/*
1479 * Block the open until the port is ready. We must be called with
1480 * the per-port semaphore held.
1481 */
1482static int
1483uart_block_til_ready(struct file *filp, struct uart_state *state)
1484{
1485 DECLARE_WAITQUEUE(wait, current);
Alan Cox46d57a42009-09-19 13:13:29 -07001486 struct uart_port *uport = state->uart_port;
1487 struct tty_port *port = &state->port;
Russell Kingc5f46442005-06-29 09:42:38 +01001488 unsigned int mctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Alan Cox46d57a42009-09-19 13:13:29 -07001490 port->blocked_open++;
Alan Cox91312cd2009-09-19 13:13:29 -07001491 port->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Alan Cox46d57a42009-09-19 13:13:29 -07001493 add_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 while (1) {
1495 set_current_state(TASK_INTERRUPTIBLE);
1496
1497 /*
1498 * If we have been hung up, tell userspace/restart open.
1499 */
Alan Cox46d57a42009-09-19 13:13:29 -07001500 if (tty_hung_up_p(filp) || port->tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 break;
1502
1503 /*
1504 * If the port has been closed, tell userspace/restart open.
1505 */
Alan Coxccce6de2009-09-19 13:13:30 -07001506 if (!(port->flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 break;
1508
1509 /*
1510 * If non-blocking mode is set, or CLOCAL mode is set,
1511 * we don't want to wait for the modem status lines to
1512 * indicate that the port is ready.
1513 *
1514 * Also, if the port is not enabled/configured, we want
1515 * to allow the open to succeed here. Note that we will
1516 * have set TTY_IO_ERROR for a non-existant port.
1517 */
1518 if ((filp->f_flags & O_NONBLOCK) ||
Alan Cox46d57a42009-09-19 13:13:29 -07001519 (port->tty->termios->c_cflag & CLOCAL) ||
1520 (port->tty->flags & (1 << TTY_IO_ERROR)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 /*
1524 * Set DTR to allow modem to know we're waiting. Do
1525 * not set RTS here - we want to make sure we catch
1526 * the data from the modem.
1527 */
Alan Cox46d57a42009-09-19 13:13:29 -07001528 if (port->tty->termios->c_cflag & CBAUD)
1529 uart_set_mctrl(uport, TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 /*
1532 * and wait for the carrier to indicate that the
1533 * modem is ready for us.
1534 */
Alan Cox46d57a42009-09-19 13:13:29 -07001535 spin_lock_irq(&uport->lock);
1536 uport->ops->enable_ms(uport);
1537 mctrl = uport->ops->get_mctrl(uport);
1538 spin_unlock_irq(&uport->lock);
Russell Kingc5f46442005-06-29 09:42:38 +01001539 if (mctrl & TIOCM_CAR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 break;
1541
Alan Coxa2bceae2009-09-19 13:13:31 -07001542 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 schedule();
Alan Coxa2bceae2009-09-19 13:13:31 -07001544 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 if (signal_pending(current))
1547 break;
1548 }
1549 set_current_state(TASK_RUNNING);
Alan Cox46d57a42009-09-19 13:13:29 -07001550 remove_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Alan Cox91312cd2009-09-19 13:13:29 -07001552 port->count++;
Alan Cox46d57a42009-09-19 13:13:29 -07001553 port->blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 if (signal_pending(current))
1556 return -ERESTARTSYS;
1557
Alan Cox46d57a42009-09-19 13:13:29 -07001558 if (!port->tty || tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return -EAGAIN;
1560
1561 return 0;
1562}
1563
1564static struct uart_state *uart_get(struct uart_driver *drv, int line)
1565{
1566 struct uart_state *state;
Alan Coxa2bceae2009-09-19 13:13:31 -07001567 struct tty_port *port;
Russell King68ac64c2006-04-30 11:13:50 +01001568 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 state = drv->state + line;
Alan Coxa2bceae2009-09-19 13:13:31 -07001571 port = &state->port;
1572 if (mutex_lock_interruptible(&port->mutex)) {
Russell King68ac64c2006-04-30 11:13:50 +01001573 ret = -ERESTARTSYS;
1574 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
1576
Alan Coxa2bceae2009-09-19 13:13:31 -07001577 port->count++;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001578 if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
Russell King68ac64c2006-04-30 11:13:50 +01001579 ret = -ENXIO;
1580 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return state;
Russell King68ac64c2006-04-30 11:13:50 +01001583
1584 err_unlock:
Alan Coxa2bceae2009-09-19 13:13:31 -07001585 port->count--;
1586 mutex_unlock(&port->mutex);
Russell King68ac64c2006-04-30 11:13:50 +01001587 err:
1588 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
1591/*
Denis Cheng922f9cf2008-02-08 04:22:00 -08001592 * calls to uart_open are serialised by the BKL in
1593 * fs/char_dev.c:chrdev_open()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 * Note that if this fails, then uart_close() _will_ be called.
1595 *
1596 * In time, we want to scrap the "opening nonpresent ports"
1597 * behaviour and implement an alternative way for setserial
1598 * to set base addresses/ports/types. This will allow us to
1599 * get rid of a certain amount of extra tests.
1600 */
1601static int uart_open(struct tty_struct *tty, struct file *filp)
1602{
1603 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1604 struct uart_state *state;
Alan Cox91312cd2009-09-19 13:13:29 -07001605 struct tty_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 int retval, line = tty->index;
1607
1608 BUG_ON(!kernel_locked());
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001609 pr_debug("uart_open(%d) called\n", line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 /*
1612 * tty->driver->num won't change, so we won't fail here with
1613 * tty->driver_data set to something non-NULL (and therefore
1614 * we won't get caught by uart_close()).
1615 */
1616 retval = -ENODEV;
1617 if (line >= tty->driver->num)
1618 goto fail;
1619
1620 /*
1621 * We take the semaphore inside uart_get to guarantee that we won't
Alan Coxebd2c8f2009-09-19 13:13:28 -07001622 * be re-entered while allocating the state structure, or while we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 * request any IRQs that the driver may need. This also has the nice
1624 * side-effect that it delays the action of uart_hangup, so we can
Alan Coxebd2c8f2009-09-19 13:13:28 -07001625 * guarantee that state->port.tty will always contain something
1626 * reasonable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 */
1628 state = uart_get(drv, line);
1629 if (IS_ERR(state)) {
1630 retval = PTR_ERR(state);
1631 goto fail;
1632 }
Alan Cox91312cd2009-09-19 13:13:29 -07001633 port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 /*
1636 * Once we set tty->driver_data here, we are guaranteed that
1637 * uart_close() will decrement the driver module use count.
1638 * Any failures from here onwards should not touch the count.
1639 */
1640 tty->driver_data = state;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001641 state->uart_port->state = state;
1642 tty->low_latency = (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 tty->alt_speed = 0;
Alan Cox91312cd2009-09-19 13:13:29 -07001644 port->tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 /*
1647 * If the port is in the middle of closing, bail out now.
1648 */
1649 if (tty_hung_up_p(filp)) {
1650 retval = -EAGAIN;
Alan Cox91312cd2009-09-19 13:13:29 -07001651 port->count--;
Alan Coxa2bceae2009-09-19 13:13:31 -07001652 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 goto fail;
1654 }
1655
1656 /*
1657 * Make sure the device is in D0 state.
1658 */
Alan Cox91312cd2009-09-19 13:13:29 -07001659 if (port->count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 uart_change_pm(state, 0);
1661
1662 /*
1663 * Start up the serial port.
1664 */
1665 retval = uart_startup(state, 0);
1666
1667 /*
1668 * If we succeeded, wait until the port is ready.
1669 */
1670 if (retval == 0)
1671 retval = uart_block_til_ready(filp, state);
Alan Coxa2bceae2009-09-19 13:13:31 -07001672 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 /*
1675 * If this is the first open to succeed, adjust things to suit.
1676 */
Alan Coxccce6de2009-09-19 13:13:30 -07001677 if (retval == 0 && !(port->flags & ASYNC_NORMAL_ACTIVE)) {
1678 set_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 uart_update_termios(state);
1681 }
1682
Alan Coxa2bceae2009-09-19 13:13:31 -07001683fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 return retval;
1685}
1686
1687static const char *uart_type(struct uart_port *port)
1688{
1689 const char *str = NULL;
1690
1691 if (port->ops->type)
1692 str = port->ops->type(port);
1693
1694 if (!str)
1695 str = "unknown";
1696
1697 return str;
1698}
1699
1700#ifdef CONFIG_PROC_FS
1701
Alexey Dobriyand196a942009-03-31 15:19:21 -07001702static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
1704 struct uart_state *state = drv->state + i;
Alan Coxa2bceae2009-09-19 13:13:31 -07001705 struct tty_port *port = &state->port;
George G. Davis3689a0e2007-02-14 00:33:06 -08001706 int pm_state;
Alan Coxa2bceae2009-09-19 13:13:31 -07001707 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 char stat_buf[32];
1709 unsigned int status;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001710 int mmio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Alan Coxa2bceae2009-09-19 13:13:31 -07001712 if (!uport)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001713 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Alan Coxa2bceae2009-09-19 13:13:31 -07001715 mmio = uport->iotype >= UPIO_MEM;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001716 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001717 uport->line, uart_type(uport),
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001718 mmio ? "mmio:0x" : "port:",
Alan Coxa2bceae2009-09-19 13:13:31 -07001719 mmio ? (unsigned long long)uport->mapbase
1720 : (unsigned long long)uport->iobase,
1721 uport->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Alan Coxa2bceae2009-09-19 13:13:31 -07001723 if (uport->type == PORT_UNKNOWN) {
Alexey Dobriyand196a942009-03-31 15:19:21 -07001724 seq_putc(m, '\n');
1725 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
1727
Alan Coxa46c9992008-02-08 04:18:53 -08001728 if (capable(CAP_SYS_ADMIN)) {
Alan Coxa2bceae2009-09-19 13:13:31 -07001729 mutex_lock(&port->mutex);
George G. Davis3689a0e2007-02-14 00:33:06 -08001730 pm_state = state->pm_state;
1731 if (pm_state)
1732 uart_change_pm(state, 0);
Alan Coxa2bceae2009-09-19 13:13:31 -07001733 spin_lock_irq(&uport->lock);
1734 status = uport->ops->get_mctrl(uport);
1735 spin_unlock_irq(&uport->lock);
George G. Davis3689a0e2007-02-14 00:33:06 -08001736 if (pm_state)
1737 uart_change_pm(state, pm_state);
Alan Coxa2bceae2009-09-19 13:13:31 -07001738 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Alexey Dobriyand196a942009-03-31 15:19:21 -07001740 seq_printf(m, " tx:%d rx:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001741 uport->icount.tx, uport->icount.rx);
1742 if (uport->icount.frame)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001743 seq_printf(m, " fe:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001744 uport->icount.frame);
1745 if (uport->icount.parity)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001746 seq_printf(m, " pe:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001747 uport->icount.parity);
1748 if (uport->icount.brk)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001749 seq_printf(m, " brk:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001750 uport->icount.brk);
1751 if (uport->icount.overrun)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001752 seq_printf(m, " oe:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001753 uport->icount.overrun);
Alan Coxa46c9992008-02-08 04:18:53 -08001754
1755#define INFOBIT(bit, str) \
Alan Coxa2bceae2009-09-19 13:13:31 -07001756 if (uport->mctrl & (bit)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 strncat(stat_buf, (str), sizeof(stat_buf) - \
1758 strlen(stat_buf) - 2)
Alan Coxa46c9992008-02-08 04:18:53 -08001759#define STATBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (status & (bit)) \
1761 strncat(stat_buf, (str), sizeof(stat_buf) - \
1762 strlen(stat_buf) - 2)
1763
1764 stat_buf[0] = '\0';
1765 stat_buf[1] = '\0';
1766 INFOBIT(TIOCM_RTS, "|RTS");
1767 STATBIT(TIOCM_CTS, "|CTS");
1768 INFOBIT(TIOCM_DTR, "|DTR");
1769 STATBIT(TIOCM_DSR, "|DSR");
1770 STATBIT(TIOCM_CAR, "|CD");
1771 STATBIT(TIOCM_RNG, "|RI");
1772 if (stat_buf[0])
1773 stat_buf[0] = ' ';
Alan Coxa46c9992008-02-08 04:18:53 -08001774
Alexey Dobriyand196a942009-03-31 15:19:21 -07001775 seq_puts(m, stat_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
Alexey Dobriyand196a942009-03-31 15:19:21 -07001777 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778#undef STATBIT
1779#undef INFOBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
Alexey Dobriyand196a942009-03-31 15:19:21 -07001782static int uart_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Alexey Dobriyan833bb302009-04-02 01:30:04 +04001784 struct tty_driver *ttydrv = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 struct uart_driver *drv = ttydrv->driver_state;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001786 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Alexey Dobriyand196a942009-03-31 15:19:21 -07001788 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 "", "", "");
Alexey Dobriyand196a942009-03-31 15:19:21 -07001790 for (i = 0; i < drv->nr; i++)
1791 uart_line_info(m, drv, i);
1792 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
Alexey Dobriyand196a942009-03-31 15:19:21 -07001794
1795static int uart_proc_open(struct inode *inode, struct file *file)
1796{
1797 return single_open(file, uart_proc_show, PDE(inode)->data);
1798}
1799
1800static const struct file_operations uart_proc_fops = {
1801 .owner = THIS_MODULE,
1802 .open = uart_proc_open,
1803 .read = seq_read,
1804 .llseek = seq_lseek,
1805 .release = single_release,
1806};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807#endif
1808
Andrew Morton4a1b5502008-03-07 15:51:16 -08001809#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810/*
Russell Kingd3587882006-03-20 20:00:09 +00001811 * uart_console_write - write a console message to a serial port
1812 * @port: the port to write the message
1813 * @s: array of characters
1814 * @count: number of characters in string to write
1815 * @write: function to write character to port
1816 */
1817void uart_console_write(struct uart_port *port, const char *s,
1818 unsigned int count,
1819 void (*putchar)(struct uart_port *, int))
1820{
1821 unsigned int i;
1822
1823 for (i = 0; i < count; i++, s++) {
1824 if (*s == '\n')
1825 putchar(port, '\r');
1826 putchar(port, *s);
1827 }
1828}
1829EXPORT_SYMBOL_GPL(uart_console_write);
1830
1831/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 * Check whether an invalid uart number has been specified, and
1833 * if so, search for the first available port that does have
1834 * console support.
1835 */
1836struct uart_port * __init
1837uart_get_console(struct uart_port *ports, int nr, struct console *co)
1838{
1839 int idx = co->index;
1840
1841 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1842 ports[idx].membase == NULL))
1843 for (idx = 0; idx < nr; idx++)
1844 if (ports[idx].iobase != 0 ||
1845 ports[idx].membase != NULL)
1846 break;
1847
1848 co->index = idx;
1849
1850 return ports + idx;
1851}
1852
1853/**
1854 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1855 * @options: pointer to option string
1856 * @baud: pointer to an 'int' variable for the baud rate.
1857 * @parity: pointer to an 'int' variable for the parity.
1858 * @bits: pointer to an 'int' variable for the number of data bits.
1859 * @flow: pointer to an 'int' variable for the flow control character.
1860 *
1861 * uart_parse_options decodes a string containing the serial console
1862 * options. The format of the string is <baud><parity><bits><flow>,
1863 * eg: 115200n8r
1864 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001865void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1867{
1868 char *s = options;
1869
1870 *baud = simple_strtoul(s, NULL, 10);
1871 while (*s >= '0' && *s <= '9')
1872 s++;
1873 if (*s)
1874 *parity = *s++;
1875 if (*s)
1876 *bits = *s++ - '0';
1877 if (*s)
1878 *flow = *s;
1879}
Jason Wesself2d937f2008-04-17 20:05:37 +02001880EXPORT_SYMBOL_GPL(uart_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882struct baud_rates {
1883 unsigned int rate;
1884 unsigned int cflag;
1885};
1886
Arjan van de Vencb3592b2005-11-28 21:04:11 +00001887static const struct baud_rates baud_rates[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 { 921600, B921600 },
1889 { 460800, B460800 },
1890 { 230400, B230400 },
1891 { 115200, B115200 },
1892 { 57600, B57600 },
1893 { 38400, B38400 },
1894 { 19200, B19200 },
1895 { 9600, B9600 },
1896 { 4800, B4800 },
1897 { 2400, B2400 },
1898 { 1200, B1200 },
1899 { 0, B38400 }
1900};
1901
1902/**
1903 * uart_set_options - setup the serial console parameters
1904 * @port: pointer to the serial ports uart_port structure
1905 * @co: console pointer
1906 * @baud: baud rate
1907 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1908 * @bits: number of data bits
1909 * @flow: flow control character - 'r' (rts)
1910 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001911int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912uart_set_options(struct uart_port *port, struct console *co,
1913 int baud, int parity, int bits, int flow)
1914{
Alan Cox606d0992006-12-08 02:38:45 -08001915 struct ktermios termios;
Alan Cox149b36e2007-10-18 01:24:16 -07001916 static struct ktermios dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 int i;
1918
Russell King976ecd12005-07-03 21:05:45 +01001919 /*
1920 * Ensure that the serial console lock is initialised
1921 * early.
1922 */
1923 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07001924 lockdep_set_class(&port->lock, &port_lock_key);
Russell King976ecd12005-07-03 21:05:45 +01001925
Alan Cox606d0992006-12-08 02:38:45 -08001926 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1929
1930 /*
1931 * Construct a cflag setting.
1932 */
1933 for (i = 0; baud_rates[i].rate; i++)
1934 if (baud_rates[i].rate <= baud)
1935 break;
1936
1937 termios.c_cflag |= baud_rates[i].cflag;
1938
1939 if (bits == 7)
1940 termios.c_cflag |= CS7;
1941 else
1942 termios.c_cflag |= CS8;
1943
1944 switch (parity) {
1945 case 'o': case 'O':
1946 termios.c_cflag |= PARODD;
1947 /*fall through*/
1948 case 'e': case 'E':
1949 termios.c_cflag |= PARENB;
1950 break;
1951 }
1952
1953 if (flow == 'r')
1954 termios.c_cflag |= CRTSCTS;
1955
Yinghai Lu79492682007-07-15 23:37:25 -07001956 /*
1957 * some uarts on other side don't support no flow control.
1958 * So we set * DTR in host uart to make them happy
1959 */
1960 port->mctrl |= TIOCM_DTR;
1961
Alan Cox149b36e2007-10-18 01:24:16 -07001962 port->ops->set_termios(port, &termios, &dummy);
Jason Wesself2d937f2008-04-17 20:05:37 +02001963 /*
1964 * Allow the setting of the UART parameters with a NULL console
1965 * too:
1966 */
1967 if (co)
1968 co->cflag = termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 return 0;
1971}
Jason Wesself2d937f2008-04-17 20:05:37 +02001972EXPORT_SYMBOL_GPL(uart_set_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973#endif /* CONFIG_SERIAL_CORE_CONSOLE */
1974
1975static void uart_change_pm(struct uart_state *state, int pm_state)
1976{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001977 struct uart_port *port = state->uart_port;
Andrew Victor1281e362006-05-16 11:28:49 +01001978
1979 if (state->pm_state != pm_state) {
1980 if (port->ops->pm)
1981 port->ops->pm(port, pm_state, state->pm_state);
1982 state->pm_state = pm_state;
1983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984}
1985
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001986struct uart_match {
1987 struct uart_port *port;
1988 struct uart_driver *driver;
1989};
1990
1991static int serial_match_port(struct device *dev, void *data)
1992{
1993 struct uart_match *match = data;
Guennadi Liakhovetski7ca796f2008-07-04 09:59:28 -07001994 struct tty_driver *tty_drv = match->driver->tty_driver;
1995 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1996 match->port->line;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001997
1998 return dev->devt == devt; /* Actually, only one tty per port */
1999}
2000
Alan Coxccce6de2009-09-19 13:13:30 -07002001int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
Alan Coxccce6de2009-09-19 13:13:30 -07002003 struct uart_state *state = drv->state + uport->line;
2004 struct tty_port *port = &state->port;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002005 struct device *tty_dev;
Alan Coxccce6de2009-09-19 13:13:30 -07002006 struct uart_match match = {uport, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Alan Coxa2bceae2009-09-19 13:13:31 -07002008 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Alan Coxccce6de2009-09-19 13:13:30 -07002010 if (!console_suspend_enabled && uart_console(uport)) {
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002011 /* we're going to avoid suspending serial console */
Alan Coxa2bceae2009-09-19 13:13:31 -07002012 mutex_unlock(&port->mutex);
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002013 return 0;
2014 }
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002015
Alan Coxccce6de2009-09-19 13:13:30 -07002016 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002017 if (device_may_wakeup(tty_dev)) {
Alan Coxccce6de2009-09-19 13:13:30 -07002018 enable_irq_wake(uport->irq);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002019 put_device(tty_dev);
Alan Coxa2bceae2009-09-19 13:13:31 -07002020 mutex_unlock(&port->mutex);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002021 return 0;
2022 }
Alan Coxccce6de2009-09-19 13:13:30 -07002023 uport->suspended = 1;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002024
Alan Coxccce6de2009-09-19 13:13:30 -07002025 if (port->flags & ASYNC_INITIALIZED) {
2026 const struct uart_ops *ops = uport->ops;
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002027 int tries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Alan Coxccce6de2009-09-19 13:13:30 -07002029 set_bit(ASYNCB_SUSPENDED, &port->flags);
2030 clear_bit(ASYNCB_INITIALIZED, &port->flags);
Russell Kinga6b93a92006-10-01 17:17:40 +01002031
Alan Coxccce6de2009-09-19 13:13:30 -07002032 spin_lock_irq(&uport->lock);
2033 ops->stop_tx(uport);
2034 ops->set_mctrl(uport, 0);
2035 ops->stop_rx(uport);
2036 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 /*
2039 * Wait for the transmitter to empty.
2040 */
Alan Coxccce6de2009-09-19 13:13:30 -07002041 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 msleep(10);
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002043 if (!tries)
Alan Coxa46c9992008-02-08 04:18:53 -08002044 printk(KERN_ERR "%s%s%s%d: Unable to drain "
2045 "transmitter\n",
Alan Coxccce6de2009-09-19 13:13:30 -07002046 uport->dev ? dev_name(uport->dev) : "",
2047 uport->dev ? ": " : "",
David S. Miller84408382008-10-13 10:45:26 +01002048 drv->dev_name,
Alan Coxccce6de2009-09-19 13:13:30 -07002049 drv->tty_driver->name_base + uport->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Alan Coxccce6de2009-09-19 13:13:30 -07002051 ops->shutdown(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
2053
2054 /*
2055 * Disable the console device before suspending.
2056 */
Alan Coxccce6de2009-09-19 13:13:30 -07002057 if (uart_console(uport))
2058 console_stop(uport->cons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 uart_change_pm(state, 3);
2061
Alan Coxa2bceae2009-09-19 13:13:31 -07002062 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 return 0;
2065}
2066
Alan Coxccce6de2009-09-19 13:13:30 -07002067int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
Alan Coxccce6de2009-09-19 13:13:30 -07002069 struct uart_state *state = drv->state + uport->line;
2070 struct tty_port *port = &state->port;
Arjan van de Ven03a74dc2008-05-23 13:04:49 -07002071 struct device *tty_dev;
Alan Coxccce6de2009-09-19 13:13:30 -07002072 struct uart_match match = {uport, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Alan Coxa2bceae2009-09-19 13:13:31 -07002074 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Alan Coxccce6de2009-09-19 13:13:30 -07002076 if (!console_suspend_enabled && uart_console(uport)) {
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002077 /* no need to resume serial console, it wasn't suspended */
Alan Coxa2bceae2009-09-19 13:13:31 -07002078 mutex_unlock(&port->mutex);
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002079 return 0;
2080 }
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002081
Alan Coxccce6de2009-09-19 13:13:30 -07002082 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2083 if (!uport->suspended && device_may_wakeup(tty_dev)) {
2084 disable_irq_wake(uport->irq);
Alan Coxa2bceae2009-09-19 13:13:31 -07002085 mutex_unlock(&port->mutex);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002086 return 0;
2087 }
Alan Coxccce6de2009-09-19 13:13:30 -07002088 uport->suspended = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 /*
2091 * Re-enable the console device after suspending.
2092 */
Alan Coxccce6de2009-09-19 13:13:30 -07002093 if (uart_console(uport)) {
Alan Cox606d0992006-12-08 02:38:45 -08002094 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096 /*
2097 * First try to use the console cflag setting.
2098 */
Alan Cox606d0992006-12-08 02:38:45 -08002099 memset(&termios, 0, sizeof(struct ktermios));
Alan Coxccce6de2009-09-19 13:13:30 -07002100 termios.c_cflag = uport->cons->cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 /*
2103 * If that's unset, use the tty termios setting.
2104 */
Alan Coxccce6de2009-09-19 13:13:30 -07002105 if (port->tty && termios.c_cflag == 0)
2106 termios = *port->tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Russell King9d778a62008-02-04 22:27:51 -08002108 uart_change_pm(state, 0);
Alan Coxccce6de2009-09-19 13:13:30 -07002109 uport->ops->set_termios(uport, &termios, NULL);
2110 console_start(uport->cons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
2112
Alan Coxccce6de2009-09-19 13:13:30 -07002113 if (port->flags & ASYNC_SUSPENDED) {
2114 const struct uart_ops *ops = uport->ops;
Russell Kingee31b332005-11-13 15:28:51 +00002115 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Russell King9d778a62008-02-04 22:27:51 -08002117 uart_change_pm(state, 0);
Alan Coxccce6de2009-09-19 13:13:30 -07002118 spin_lock_irq(&uport->lock);
2119 ops->set_mctrl(uport, 0);
2120 spin_unlock_irq(&uport->lock);
2121 ret = ops->startup(uport);
Russell Kingee31b332005-11-13 15:28:51 +00002122 if (ret == 0) {
2123 uart_change_speed(state, NULL);
Alan Coxccce6de2009-09-19 13:13:30 -07002124 spin_lock_irq(&uport->lock);
2125 ops->set_mctrl(uport, uport->mctrl);
2126 ops->start_tx(uport);
2127 spin_unlock_irq(&uport->lock);
2128 set_bit(ASYNCB_INITIALIZED, &port->flags);
Russell Kingee31b332005-11-13 15:28:51 +00002129 } else {
2130 /*
2131 * Failed to resume - maybe hardware went away?
2132 * Clear the "initialized" flag so we won't try
2133 * to call the low level drivers shutdown method.
2134 */
Russell Kingee31b332005-11-13 15:28:51 +00002135 uart_shutdown(state);
2136 }
Russell Kinga6b93a92006-10-01 17:17:40 +01002137
Alan Coxccce6de2009-09-19 13:13:30 -07002138 clear_bit(ASYNCB_SUSPENDED, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140
Alan Coxa2bceae2009-09-19 13:13:31 -07002141 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 return 0;
2144}
2145
2146static inline void
2147uart_report_port(struct uart_driver *drv, struct uart_port *port)
2148{
Russell King30b7a3b2005-09-03 15:30:21 +01002149 char address[64];
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 switch (port->iotype) {
2152 case UPIO_PORT:
Andrew Morton9bde10a2008-10-13 10:35:42 +01002153 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 break;
2155 case UPIO_HUB6:
Russell King30b7a3b2005-09-03 15:30:21 +01002156 snprintf(address, sizeof(address),
Andrew Morton9bde10a2008-10-13 10:35:42 +01002157 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 break;
2159 case UPIO_MEM:
2160 case UPIO_MEM32:
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002161 case UPIO_AU:
Zang Roy-r619113be91ec2006-06-30 02:29:58 -07002162 case UPIO_TSI:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002163 case UPIO_DWAPB:
Russell King30b7a3b2005-09-03 15:30:21 +01002164 snprintf(address, sizeof(address),
Josh Boyer4f640ef2007-07-23 18:43:44 -07002165 "MMIO 0x%llx", (unsigned long long)port->mapbase);
Russell King30b7a3b2005-09-03 15:30:21 +01002166 break;
2167 default:
2168 strlcpy(address, "*unknown*", sizeof(address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 break;
2170 }
Russell King30b7a3b2005-09-03 15:30:21 +01002171
Russell King0cf669d2005-10-31 11:42:22 +00002172 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
Kay Sievers4bfe0902009-01-06 10:44:37 -08002173 port->dev ? dev_name(port->dev) : "",
Russell King0cf669d2005-10-31 11:42:22 +00002174 port->dev ? ": " : "",
David S. Miller84408382008-10-13 10:45:26 +01002175 drv->dev_name,
2176 drv->tty_driver->name_base + port->line,
2177 address, port->irq, uart_type(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
2180static void
2181uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2182 struct uart_port *port)
2183{
2184 unsigned int flags;
2185
2186 /*
2187 * If there isn't a port here, don't do anything further.
2188 */
2189 if (!port->iobase && !port->mapbase && !port->membase)
2190 return;
2191
2192 /*
2193 * Now do the auto configuration stuff. Note that config_port
2194 * is expected to claim the resources and map the port for us.
2195 */
David Daney8e23fcc2009-01-02 13:49:54 +00002196 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (port->flags & UPF_AUTO_IRQ)
2198 flags |= UART_CONFIG_IRQ;
2199 if (port->flags & UPF_BOOT_AUTOCONF) {
David Daney8e23fcc2009-01-02 13:49:54 +00002200 if (!(port->flags & UPF_FIXED_TYPE)) {
2201 port->type = PORT_UNKNOWN;
2202 flags |= UART_CONFIG_TYPE;
2203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 port->ops->config_port(port, flags);
2205 }
2206
2207 if (port->type != PORT_UNKNOWN) {
2208 unsigned long flags;
2209
2210 uart_report_port(drv, port);
2211
George G. Davis3689a0e2007-02-14 00:33:06 -08002212 /* Power up port for set_mctrl() */
2213 uart_change_pm(state, 0);
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 /*
2216 * Ensure that the modem control lines are de-activated.
Yinghai Luc3e46422008-02-04 22:27:46 -08002217 * keep the DTR setting that is set in uart_set_options()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 * We probably don't need a spinlock around this, but
2219 */
2220 spin_lock_irqsave(&port->lock, flags);
Yinghai Luc3e46422008-02-04 22:27:46 -08002221 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 spin_unlock_irqrestore(&port->lock, flags);
2223
2224 /*
Russell King97d97222007-09-01 21:25:09 +01002225 * If this driver supports console, and it hasn't been
2226 * successfully registered yet, try to re-register it.
2227 * It may be that the port was not available.
2228 */
2229 if (port->cons && !(port->cons->flags & CON_ENABLED))
2230 register_console(port->cons);
2231
2232 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 * Power down all ports by default, except the
2234 * console if we have one.
2235 */
2236 if (!uart_console(port))
2237 uart_change_pm(state, 3);
2238 }
2239}
2240
Jason Wesself2d937f2008-04-17 20:05:37 +02002241#ifdef CONFIG_CONSOLE_POLL
2242
2243static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2244{
2245 struct uart_driver *drv = driver->driver_state;
2246 struct uart_state *state = drv->state + line;
2247 struct uart_port *port;
2248 int baud = 9600;
2249 int bits = 8;
2250 int parity = 'n';
2251 int flow = 'n';
2252
Alan Coxebd2c8f2009-09-19 13:13:28 -07002253 if (!state || !state->uart_port)
Jason Wesself2d937f2008-04-17 20:05:37 +02002254 return -1;
2255
Alan Coxebd2c8f2009-09-19 13:13:28 -07002256 port = state->uart_port;
Jason Wesself2d937f2008-04-17 20:05:37 +02002257 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2258 return -1;
2259
2260 if (options) {
2261 uart_parse_options(options, &baud, &parity, &bits, &flow);
2262 return uart_set_options(port, NULL, baud, parity, bits, flow);
2263 }
2264
2265 return 0;
2266}
2267
2268static int uart_poll_get_char(struct tty_driver *driver, int line)
2269{
2270 struct uart_driver *drv = driver->driver_state;
2271 struct uart_state *state = drv->state + line;
2272 struct uart_port *port;
2273
Alan Coxebd2c8f2009-09-19 13:13:28 -07002274 if (!state || !state->uart_port)
Jason Wesself2d937f2008-04-17 20:05:37 +02002275 return -1;
2276
Alan Coxebd2c8f2009-09-19 13:13:28 -07002277 port = state->uart_port;
Jason Wesself2d937f2008-04-17 20:05:37 +02002278 return port->ops->poll_get_char(port);
2279}
2280
2281static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2282{
2283 struct uart_driver *drv = driver->driver_state;
2284 struct uart_state *state = drv->state + line;
2285 struct uart_port *port;
2286
Alan Coxebd2c8f2009-09-19 13:13:28 -07002287 if (!state || !state->uart_port)
Jason Wesself2d937f2008-04-17 20:05:37 +02002288 return;
2289
Alan Coxebd2c8f2009-09-19 13:13:28 -07002290 port = state->uart_port;
Jason Wesself2d937f2008-04-17 20:05:37 +02002291 port->ops->poll_put_char(port, ch);
2292}
2293#endif
2294
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002295static const struct tty_operations uart_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 .open = uart_open,
2297 .close = uart_close,
2298 .write = uart_write,
2299 .put_char = uart_put_char,
2300 .flush_chars = uart_flush_chars,
2301 .write_room = uart_write_room,
2302 .chars_in_buffer= uart_chars_in_buffer,
2303 .flush_buffer = uart_flush_buffer,
2304 .ioctl = uart_ioctl,
2305 .throttle = uart_throttle,
2306 .unthrottle = uart_unthrottle,
2307 .send_xchar = uart_send_xchar,
2308 .set_termios = uart_set_termios,
Alan Cox64e91592008-06-03 15:18:54 +01002309 .set_ldisc = uart_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 .stop = uart_stop,
2311 .start = uart_start,
2312 .hangup = uart_hangup,
2313 .break_ctl = uart_break_ctl,
2314 .wait_until_sent= uart_wait_until_sent,
2315#ifdef CONFIG_PROC_FS
Alexey Dobriyand196a942009-03-31 15:19:21 -07002316 .proc_fops = &uart_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317#endif
2318 .tiocmget = uart_tiocmget,
2319 .tiocmset = uart_tiocmset,
Jason Wesself2d937f2008-04-17 20:05:37 +02002320#ifdef CONFIG_CONSOLE_POLL
2321 .poll_init = uart_poll_init,
2322 .poll_get_char = uart_poll_get_char,
2323 .poll_put_char = uart_poll_put_char,
2324#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325};
2326
2327/**
2328 * uart_register_driver - register a driver with the uart core layer
2329 * @drv: low level driver structure
2330 *
2331 * Register a uart driver with the core driver. We in turn register
2332 * with the tty layer, and initialise the core driver per-port state.
2333 *
2334 * We have a proc file in /proc/tty/driver which is named after the
2335 * normal driver.
2336 *
2337 * drv->port should be NULL, and the per-port structures should be
2338 * registered using uart_add_one_port after this call has succeeded.
2339 */
2340int uart_register_driver(struct uart_driver *drv)
2341{
2342 struct tty_driver *normal = NULL;
2343 int i, retval;
2344
2345 BUG_ON(drv->state);
2346
2347 /*
2348 * Maybe we should be using a slab cache for this, especially if
2349 * we have a large number of ports to handle.
2350 */
Burman Yan8f31bb32007-02-14 00:33:07 -08002351 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 retval = -ENOMEM;
2353 if (!drv->state)
2354 goto out;
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 normal = alloc_tty_driver(drv->nr);
2357 if (!normal)
2358 goto out;
2359
2360 drv->tty_driver = normal;
2361
2362 normal->owner = drv->owner;
2363 normal->driver_name = drv->driver_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 normal->name = drv->dev_name;
2365 normal->major = drv->major;
2366 normal->minor_start = drv->minor;
2367 normal->type = TTY_DRIVER_TYPE_SERIAL;
2368 normal->subtype = SERIAL_TYPE_NORMAL;
2369 normal->init_termios = tty_std_termios;
2370 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -08002371 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07002372 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 normal->driver_state = drv;
2374 tty_set_operations(normal, &uart_ops);
2375
2376 /*
2377 * Initialise the UART state(s).
2378 */
2379 for (i = 0; i < drv->nr; i++) {
2380 struct uart_state *state = drv->state + i;
Alan Coxa2bceae2009-09-19 13:13:31 -07002381 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Alan Coxa2bceae2009-09-19 13:13:31 -07002383 tty_port_init(port);
2384 port->close_delay = 500; /* .5 seconds */
2385 port->closing_wait = 30000; /* 30 seconds */
Alan Coxebd2c8f2009-09-19 13:13:28 -07002386 tasklet_init(&state->tlet, uart_tasklet_action,
Alan Coxf7519282009-01-02 13:49:21 +00002387 (unsigned long)state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 }
2389
2390 retval = tty_register_driver(normal);
2391 out:
2392 if (retval < 0) {
2393 put_tty_driver(normal);
2394 kfree(drv->state);
2395 }
2396 return retval;
2397}
2398
2399/**
2400 * uart_unregister_driver - remove a driver from the uart core layer
2401 * @drv: low level driver structure
2402 *
2403 * Remove all references to a driver from the core driver. The low
2404 * level driver must have removed all its ports via the
2405 * uart_remove_one_port() if it registered them with uart_add_one_port().
2406 * (ie, drv->port == NULL)
2407 */
2408void uart_unregister_driver(struct uart_driver *drv)
2409{
2410 struct tty_driver *p = drv->tty_driver;
2411 tty_unregister_driver(p);
2412 put_tty_driver(p);
2413 kfree(drv->state);
2414 drv->tty_driver = NULL;
2415}
2416
2417struct tty_driver *uart_console_device(struct console *co, int *index)
2418{
2419 struct uart_driver *p = co->data;
2420 *index = co->index;
2421 return p->tty_driver;
2422}
2423
2424/**
2425 * uart_add_one_port - attach a driver-defined port structure
2426 * @drv: pointer to the uart low level driver structure for this port
2427 * @port: uart port structure to use for this port.
2428 *
2429 * This allows the driver to register its own uart_port structure
2430 * with the core driver. The main purpose is to allow the low
2431 * level uart drivers to expand uart_port, rather than having yet
2432 * more levels of structures.
2433 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002434int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
2436 struct uart_state *state;
Alan Coxa2bceae2009-09-19 13:13:31 -07002437 struct tty_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 int ret = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002439 struct device *tty_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
2441 BUG_ON(in_interrupt());
2442
Alan Coxa2bceae2009-09-19 13:13:31 -07002443 if (uport->line >= drv->nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 return -EINVAL;
2445
Alan Coxa2bceae2009-09-19 13:13:31 -07002446 state = drv->state + uport->line;
2447 port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002449 mutex_lock(&port_mutex);
Alan Coxa2bceae2009-09-19 13:13:31 -07002450 mutex_lock(&port->mutex);
Alan Coxebd2c8f2009-09-19 13:13:28 -07002451 if (state->uart_port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 ret = -EINVAL;
2453 goto out;
2454 }
2455
Alan Coxa2bceae2009-09-19 13:13:31 -07002456 state->uart_port = uport;
Russell King97d97222007-09-01 21:25:09 +01002457 state->pm_state = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Alan Coxa2bceae2009-09-19 13:13:31 -07002459 uport->cons = drv->cons;
2460 uport->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Russell King976ecd12005-07-03 21:05:45 +01002462 /*
2463 * If this port is a console, then the spinlock is already
2464 * initialised.
2465 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002466 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2467 spin_lock_init(&uport->lock);
2468 lockdep_set_class(&uport->lock, &port_lock_key);
Ingo Molnar13e83592006-07-03 00:25:03 -07002469 }
Russell King976ecd12005-07-03 21:05:45 +01002470
Alan Coxa2bceae2009-09-19 13:13:31 -07002471 uart_configure_port(drv, state, uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473 /*
2474 * Register the port whether it's detected or not. This allows
2475 * setserial to be used to alter this ports parameters.
2476 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002477 tty_dev = tty_register_device(drv->tty_driver, uport->line, uport->dev);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002478 if (likely(!IS_ERR(tty_dev))) {
Alan Stern74081f82008-03-19 22:35:13 +01002479 device_init_wakeup(tty_dev, 1);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002480 device_set_wakeup_enable(tty_dev, 0);
2481 } else
2482 printk(KERN_ERR "Cannot register tty device on line %d\n",
Alan Coxa2bceae2009-09-19 13:13:31 -07002483 uport->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 /*
Russell King68ac64c2006-04-30 11:13:50 +01002486 * Ensure UPF_DEAD is not set.
2487 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002488 uport->flags &= ~UPF_DEAD;
Russell King68ac64c2006-04-30 11:13:50 +01002489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 out:
Alan Coxa2bceae2009-09-19 13:13:31 -07002491 mutex_unlock(&port->mutex);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002492 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 return ret;
2495}
2496
2497/**
2498 * uart_remove_one_port - detach a driver defined port structure
2499 * @drv: pointer to the uart low level driver structure for this port
2500 * @port: uart port structure for this port
2501 *
2502 * This unhooks (and hangs up) the specified port structure from the
2503 * core driver. No further calls will be made to the low-level code
2504 * for this port.
2505 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002506int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
Alan Coxa2bceae2009-09-19 13:13:31 -07002508 struct uart_state *state = drv->state + uport->line;
2509 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 BUG_ON(in_interrupt());
2512
Alan Coxa2bceae2009-09-19 13:13:31 -07002513 if (state->uart_port != uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
Alan Coxa2bceae2009-09-19 13:13:31 -07002515 state->uart_port, uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002517 mutex_lock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 /*
Russell King68ac64c2006-04-30 11:13:50 +01002520 * Mark the port "dead" - this prevents any opens from
2521 * succeeding while we shut down the port.
2522 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002523 mutex_lock(&port->mutex);
2524 uport->flags |= UPF_DEAD;
2525 mutex_unlock(&port->mutex);
Russell King68ac64c2006-04-30 11:13:50 +01002526
2527 /*
Greg Kroah-Hartmanaa4148c2005-06-20 21:15:16 -07002528 * Remove the devices from the tty layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002530 tty_unregister_device(drv->tty_driver, uport->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Alan Coxa2bceae2009-09-19 13:13:31 -07002532 if (port->tty)
2533 tty_vhangup(port->tty);
Russell King68ac64c2006-04-30 11:13:50 +01002534
2535 /*
Russell King68ac64c2006-04-30 11:13:50 +01002536 * Free the port IO and memory resources, if any.
2537 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002538 if (uport->type != PORT_UNKNOWN)
2539 uport->ops->release_port(uport);
Russell King68ac64c2006-04-30 11:13:50 +01002540
2541 /*
2542 * Indicate that there isn't a port here anymore.
2543 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002544 uport->type = PORT_UNKNOWN;
Russell King68ac64c2006-04-30 11:13:50 +01002545
2546 /*
2547 * Kill the tasklet, and free resources.
2548 */
Alan Coxebd2c8f2009-09-19 13:13:28 -07002549 tasklet_kill(&state->tlet);
Russell King68ac64c2006-04-30 11:13:50 +01002550
Alan Coxebd2c8f2009-09-19 13:13:28 -07002551 state->uart_port = NULL;
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002552 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
2554 return 0;
2555}
2556
2557/*
2558 * Are the two ports equivalent?
2559 */
2560int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2561{
2562 if (port1->iotype != port2->iotype)
2563 return 0;
2564
2565 switch (port1->iotype) {
2566 case UPIO_PORT:
2567 return (port1->iobase == port2->iobase);
2568 case UPIO_HUB6:
2569 return (port1->iobase == port2->iobase) &&
2570 (port1->hub6 == port2->hub6);
2571 case UPIO_MEM:
Sergei Shtylyovd21b55d2006-08-28 19:49:03 +04002572 case UPIO_MEM32:
2573 case UPIO_AU:
2574 case UPIO_TSI:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002575 case UPIO_DWAPB:
Benjamin Herrenschmidt1624f002006-01-04 18:09:44 +00002576 return (port1->mapbase == port2->mapbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
2578 return 0;
2579}
2580EXPORT_SYMBOL(uart_match_port);
2581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582EXPORT_SYMBOL(uart_write_wakeup);
2583EXPORT_SYMBOL(uart_register_driver);
2584EXPORT_SYMBOL(uart_unregister_driver);
2585EXPORT_SYMBOL(uart_suspend_port);
2586EXPORT_SYMBOL(uart_resume_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587EXPORT_SYMBOL(uart_add_one_port);
2588EXPORT_SYMBOL(uart_remove_one_port);
2589
2590MODULE_DESCRIPTION("Serial driver core");
2591MODULE_LICENSE("GPL");