blob: 9425ed69e0f7cb3f354463e15105ef99363efe5b [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>
30#include <linux/serial_core.h>
31#include <linux/smp_lock.h>
32#include <linux/device.h>
33#include <linux/serial.h> /* for serial_state and serial_icounter_struct */
34#include <linux/delay.h>
Arjan van de Venf392ecf2006-01-12 18:44:32 +000035#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/irq.h>
38#include <asm/uaccess.h>
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
41 * This is used to lock changes in serial line configuration.
42 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +000043static DEFINE_MUTEX(port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Ingo Molnar13e83592006-07-03 00:25:03 -070045/*
46 * lockdep: port->lock is initialized in two places, but we
47 * want only one lock-class:
48 */
49static struct lock_class_key port_lock_key;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
52
Alan Coxf7519282009-01-02 13:49:21 +000053#define uart_users(state) ((state)->count + (state)->info.port.blocked_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#ifdef CONFIG_SERIAL_CORE_CONSOLE
56#define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
57#else
58#define uart_console(port) (0)
59#endif
60
Alan Coxa46c9992008-02-08 04:18:53 -080061static void uart_change_speed(struct uart_state *state,
62 struct ktermios *old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
64static void uart_change_pm(struct uart_state *state, int pm_state);
65
66/*
67 * This routine is used by the interrupt handler to schedule processing in
68 * the software interrupt portion of the driver.
69 */
70void uart_write_wakeup(struct uart_port *port)
71{
72 struct uart_info *info = port->info;
Pavel Machekd5f735e2006-03-07 21:55:20 -080073 /*
74 * This means you called this function _after_ the port was
75 * closed. No cookie for you.
76 */
77 BUG_ON(!info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 tasklet_schedule(&info->tlet);
79}
80
81static void uart_stop(struct tty_struct *tty)
82{
83 struct uart_state *state = tty->driver_data;
84 struct uart_port *port = state->port;
85 unsigned long flags;
86
87 spin_lock_irqsave(&port->lock, flags);
Russell Kingb129a8c2005-08-31 10:12:14 +010088 port->ops->stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 spin_unlock_irqrestore(&port->lock, flags);
90}
91
92static void __uart_start(struct tty_struct *tty)
93{
94 struct uart_state *state = tty->driver_data;
95 struct uart_port *port = state->port;
96
Alan Coxf7519282009-01-02 13:49:21 +000097 if (!uart_circ_empty(&state->info.xmit) && state->info.xmit.buf &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 !tty->stopped && !tty->hw_stopped)
Russell Kingb129a8c2005-08-31 10:12:14 +010099 port->ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102static void uart_start(struct tty_struct *tty)
103{
104 struct uart_state *state = tty->driver_data;
105 struct uart_port *port = state->port;
106 unsigned long flags;
107
108 spin_lock_irqsave(&port->lock, flags);
109 __uart_start(tty);
110 spin_unlock_irqrestore(&port->lock, flags);
111}
112
113static void uart_tasklet_action(unsigned long data)
114{
115 struct uart_state *state = (struct uart_state *)data;
Alan Coxf7519282009-01-02 13:49:21 +0000116 tty_wakeup(state->info.port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119static inline void
120uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
121{
122 unsigned long flags;
123 unsigned int old;
124
125 spin_lock_irqsave(&port->lock, flags);
126 old = port->mctrl;
127 port->mctrl = (old & ~clear) | set;
128 if (old != port->mctrl)
129 port->ops->set_mctrl(port, port->mctrl);
130 spin_unlock_irqrestore(&port->lock, flags);
131}
132
Alan Coxa46c9992008-02-08 04:18:53 -0800133#define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
134#define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136/*
137 * Startup the port. This will be called once per open. All calls
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100138 * will be serialised by the per-port mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 */
140static int uart_startup(struct uart_state *state, int init_hw)
141{
Alan Coxf7519282009-01-02 13:49:21 +0000142 struct uart_info *info = &state->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 struct uart_port *port = state->port;
144 unsigned long page;
145 int retval = 0;
146
147 if (info->flags & UIF_INITIALIZED)
148 return 0;
149
150 /*
151 * Set the TTY IO error marker - we will only clear this
152 * once we have successfully opened the port. Also set
153 * up the tty->alt_speed kludge
154 */
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100155 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 if (port->type == PORT_UNKNOWN)
158 return 0;
159
160 /*
161 * Initialise and allocate the transmit and temporary
162 * buffer.
163 */
164 if (!info->xmit.buf) {
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100165 /* This is protected by the per port mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 page = get_zeroed_page(GFP_KERNEL);
167 if (!page)
168 return -ENOMEM;
169
170 info->xmit.buf = (unsigned char *) page;
171 uart_circ_clear(&info->xmit);
172 }
173
174 retval = port->ops->startup(port);
175 if (retval == 0) {
176 if (init_hw) {
177 /*
178 * Initialise the hardware port settings.
179 */
180 uart_change_speed(state, NULL);
181
182 /*
183 * Setup the RTS and DTR signals once the
184 * port is open and ready to respond.
185 */
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100186 if (info->port.tty->termios->c_cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
188 }
189
Russell King0dd7a1a2005-06-29 18:40:53 +0100190 if (info->flags & UIF_CTS_FLOW) {
191 spin_lock_irq(&port->lock);
192 if (!(port->ops->get_mctrl(port) & TIOCM_CTS))
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100193 info->port.tty->hw_stopped = 1;
Russell King0dd7a1a2005-06-29 18:40:53 +0100194 spin_unlock_irq(&port->lock);
195 }
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 info->flags |= UIF_INITIALIZED;
198
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100199 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201
202 if (retval && capable(CAP_SYS_ADMIN))
203 retval = 0;
204
205 return retval;
206}
207
208/*
209 * This routine will shutdown a serial port; interrupts are disabled, and
210 * DTR is dropped if the hangup on close termio flag is on. Calls to
211 * uart_shutdown are serialised by the per-port semaphore.
212 */
213static void uart_shutdown(struct uart_state *state)
214{
Alan Coxf7519282009-01-02 13:49:21 +0000215 struct uart_info *info = &state->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 struct uart_port *port = state->port;
Alan Coxf7519282009-01-02 13:49:21 +0000217 struct tty_struct *tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Russell Kingee31b332005-11-13 15:28:51 +0000219 /*
220 * Set the TTY IO error marker
221 */
Alan Coxf7519282009-01-02 13:49:21 +0000222 if (tty)
223 set_bit(TTY_IO_ERROR, &tty->flags);
Russell Kingee31b332005-11-13 15:28:51 +0000224
225 if (info->flags & UIF_INITIALIZED) {
226 info->flags &= ~UIF_INITIALIZED;
227
228 /*
229 * Turn off DTR and RTS early.
230 */
Alan Coxf7519282009-01-02 13:49:21 +0000231 if (!tty || (tty->termios->c_cflag & HUPCL))
Russell Kingee31b332005-11-13 15:28:51 +0000232 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
233
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 */
241 wake_up_interruptible(&info->delta_msr_wait);
242
243 /*
244 * Free the IRQ and disable the port.
245 */
246 port->ops->shutdown(port);
247
248 /*
249 * Ensure that the IRQ handler isn't running on another CPU.
250 */
251 synchronize_irq(port->irq);
252 }
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 */
Russell Kingee31b332005-11-13 15:28:51 +0000257 tasklet_kill(&info->tlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /*
260 * Free the transmit buffer page.
261 */
262 if (info->xmit.buf) {
263 free_page((unsigned long)info->xmit.buf);
264 info->xmit.buf = NULL;
265 }
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 Coxf7519282009-01-02 13:49:21 +0000431 struct tty_struct *tty = state->info.port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 struct uart_port *port = state->port;
Alan Cox606d0992006-12-08 02:38:45 -0800433 struct ktermios *termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /*
436 * If we have no tty, termios, or the port does not exist,
437 * then we can't set the parameters for this port.
438 */
439 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
440 return;
441
442 termios = tty->termios;
443
444 /*
445 * Set flags based on termios cflag
446 */
447 if (termios->c_cflag & CRTSCTS)
Alan Coxf7519282009-01-02 13:49:21 +0000448 state->info.flags |= UIF_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 else
Alan Coxf7519282009-01-02 13:49:21 +0000450 state->info.flags &= ~UIF_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 if (termios->c_cflag & CLOCAL)
Alan Coxf7519282009-01-02 13:49:21 +0000453 state->info.flags &= ~UIF_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 else
Alan Coxf7519282009-01-02 13:49:21 +0000455 state->info.flags |= UIF_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 port->ops->set_termios(port, termios, old_termios);
458}
459
Alan Cox23d22ce2008-04-30 00:54:11 -0700460static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461__uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
462{
463 unsigned long flags;
Alan Cox23d22ce2008-04-30 00:54:11 -0700464 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 if (!circ->buf)
Alan Cox23d22ce2008-04-30 00:54:11 -0700467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 spin_lock_irqsave(&port->lock, flags);
470 if (uart_circ_chars_free(circ) != 0) {
471 circ->buf[circ->head] = c;
472 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
Alan Cox23d22ce2008-04-30 00:54:11 -0700473 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475 spin_unlock_irqrestore(&port->lock, flags);
Alan Cox23d22ce2008-04-30 00:54:11 -0700476 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Alan Cox23d22ce2008-04-30 00:54:11 -0700479static int uart_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 struct uart_state *state = tty->driver_data;
482
Alan Coxf7519282009-01-02 13:49:21 +0000483 return __uart_put_char(state->port, &state->info.xmit, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486static void uart_flush_chars(struct tty_struct *tty)
487{
488 uart_start(tty);
489}
490
491static int
Pavel Machekd5f735e2006-03-07 21:55:20 -0800492uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 struct uart_state *state = tty->driver_data;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800495 struct uart_port *port;
496 struct circ_buf *circ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 unsigned long flags;
498 int c, ret = 0;
499
Pavel Machekd5f735e2006-03-07 21:55:20 -0800500 /*
501 * This means you called this function _after_ the port was
502 * closed. No cookie for you.
503 */
Alan Coxf7519282009-01-02 13:49:21 +0000504 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800505 WARN_ON(1);
506 return -EL3HLT;
507 }
508
509 port = state->port;
Alan Coxf7519282009-01-02 13:49:21 +0000510 circ = &state->info.xmit;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (!circ->buf)
513 return 0;
514
515 spin_lock_irqsave(&port->lock, flags);
516 while (1) {
517 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
518 if (count < c)
519 c = count;
520 if (c <= 0)
521 break;
522 memcpy(circ->buf + circ->head, buf, c);
523 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
524 buf += c;
525 count -= c;
526 ret += c;
527 }
528 spin_unlock_irqrestore(&port->lock, flags);
529
530 uart_start(tty);
531 return ret;
532}
533
534static int uart_write_room(struct tty_struct *tty)
535{
536 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700537 unsigned long flags;
538 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Alan Coxf34d7a52008-04-30 00:54:13 -0700540 spin_lock_irqsave(&state->port->lock, flags);
Alan Coxf7519282009-01-02 13:49:21 +0000541 ret = uart_circ_chars_free(&state->info.xmit);
Alan Coxf34d7a52008-04-30 00:54:13 -0700542 spin_unlock_irqrestore(&state->port->lock, flags);
543 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
546static int uart_chars_in_buffer(struct tty_struct *tty)
547{
548 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700549 unsigned long flags;
550 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Alan Coxf34d7a52008-04-30 00:54:13 -0700552 spin_lock_irqsave(&state->port->lock, flags);
Alan Coxf7519282009-01-02 13:49:21 +0000553 ret = uart_circ_chars_pending(&state->info.xmit);
Alan Coxf34d7a52008-04-30 00:54:13 -0700554 spin_unlock_irqrestore(&state->port->lock, flags);
555 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
558static void uart_flush_buffer(struct tty_struct *tty)
559{
560 struct uart_state *state = tty->driver_data;
Tetsuo Handa55d7b682008-05-06 20:42:27 -0700561 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 unsigned long flags;
563
Pavel Machekd5f735e2006-03-07 21:55:20 -0800564 /*
565 * This means you called this function _after_ the port was
566 * closed. No cookie for you.
567 */
Alan Coxf7519282009-01-02 13:49:21 +0000568 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800569 WARN_ON(1);
570 return;
571 }
572
Tetsuo Handa55d7b682008-05-06 20:42:27 -0700573 port = state->port;
Jiri Slabyeb3a1e12007-05-06 14:48:52 -0700574 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 spin_lock_irqsave(&port->lock, flags);
Alan Coxf7519282009-01-02 13:49:21 +0000577 uart_circ_clear(&state->info.xmit);
Haavard Skinnemoen6bb0e3a2008-07-16 21:52:36 +0100578 if (port->ops->flush_buffer)
579 port->ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 spin_unlock_irqrestore(&port->lock, flags);
581 tty_wakeup(tty);
582}
583
584/*
585 * This function is used to send a high-priority XON/XOFF character to
586 * the device
587 */
588static void uart_send_xchar(struct tty_struct *tty, char ch)
589{
590 struct uart_state *state = tty->driver_data;
591 struct uart_port *port = state->port;
592 unsigned long flags;
593
594 if (port->ops->send_xchar)
595 port->ops->send_xchar(port, ch);
596 else {
597 port->x_char = ch;
598 if (ch) {
599 spin_lock_irqsave(&port->lock, flags);
Russell Kingb129a8c2005-08-31 10:12:14 +0100600 port->ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 spin_unlock_irqrestore(&port->lock, flags);
602 }
603 }
604}
605
606static void uart_throttle(struct tty_struct *tty)
607{
608 struct uart_state *state = tty->driver_data;
609
610 if (I_IXOFF(tty))
611 uart_send_xchar(tty, STOP_CHAR(tty));
612
613 if (tty->termios->c_cflag & CRTSCTS)
614 uart_clear_mctrl(state->port, TIOCM_RTS);
615}
616
617static void uart_unthrottle(struct tty_struct *tty)
618{
619 struct uart_state *state = tty->driver_data;
620 struct uart_port *port = state->port;
621
622 if (I_IXOFF(tty)) {
623 if (port->x_char)
624 port->x_char = 0;
625 else
626 uart_send_xchar(tty, START_CHAR(tty));
627 }
628
629 if (tty->termios->c_cflag & CRTSCTS)
630 uart_set_mctrl(port, TIOCM_RTS);
631}
632
633static int uart_get_info(struct uart_state *state,
634 struct serial_struct __user *retinfo)
635{
636 struct uart_port *port = state->port;
637 struct serial_struct tmp;
638
639 memset(&tmp, 0, sizeof(tmp));
Alan Coxf34d7a52008-04-30 00:54:13 -0700640
641 /* Ensure the state we copy is consistent and no hardware changes
642 occur as we go */
643 mutex_lock(&state->mutex);
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 tmp.type = port->type;
646 tmp.line = port->line;
647 tmp.port = port->iobase;
648 if (HIGH_BITS_OFFSET)
649 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
650 tmp.irq = port->irq;
651 tmp.flags = port->flags;
652 tmp.xmit_fifo_size = port->fifosize;
653 tmp.baud_base = port->uartclk / 16;
654 tmp.close_delay = state->close_delay / 10;
655 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
656 ASYNC_CLOSING_WAIT_NONE :
Alan Coxa46c9992008-02-08 04:18:53 -0800657 state->closing_wait / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 tmp.custom_divisor = port->custom_divisor;
659 tmp.hub6 = port->hub6;
660 tmp.io_type = port->iotype;
661 tmp.iomem_reg_shift = port->regshift;
Josh Boyer4f640ef2007-07-23 18:43:44 -0700662 tmp.iomem_base = (void *)(unsigned long)port->mapbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Alan Coxf34d7a52008-04-30 00:54:13 -0700664 mutex_unlock(&state->mutex);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
667 return -EFAULT;
668 return 0;
669}
670
671static int uart_set_info(struct uart_state *state,
672 struct serial_struct __user *newinfo)
673{
674 struct serial_struct new_serial;
675 struct uart_port *port = state->port;
676 unsigned long new_port;
Russell King0077d452006-01-21 23:03:28 +0000677 unsigned int change_irq, change_port, closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 unsigned int old_custom_divisor, close_delay;
Russell King0077d452006-01-21 23:03:28 +0000679 upf_t old_flags, new_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int retval = 0;
681
682 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
683 return -EFAULT;
684
685 new_port = new_serial.port;
686 if (HIGH_BITS_OFFSET)
687 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
688
689 new_serial.irq = irq_canonicalize(new_serial.irq);
690 close_delay = new_serial.close_delay * 10;
691 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
692 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
693
694 /*
695 * This semaphore protects state->count. It is also
696 * very useful to prevent opens. Also, take the
697 * port configuration semaphore to make sure that a
698 * module insertion/removal doesn't change anything
699 * under us.
700 */
Ingo Molnare2862f62006-01-13 21:37:07 +0000701 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
David Gibsonabb4a232007-05-06 14:48:49 -0700703 change_irq = !(port->flags & UPF_FIXED_PORT)
704 && new_serial.irq != port->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 /*
707 * Since changing the 'type' of the port changes its resource
708 * allocations, we should treat type changes the same as
709 * IO port changes.
710 */
David Gibsonabb4a232007-05-06 14:48:49 -0700711 change_port = !(port->flags & UPF_FIXED_PORT)
712 && (new_port != port->iobase ||
713 (unsigned long)new_serial.iomem_base != port->mapbase ||
714 new_serial.hub6 != port->hub6 ||
715 new_serial.io_type != port->iotype ||
716 new_serial.iomem_reg_shift != port->regshift ||
717 new_serial.type != port->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 old_flags = port->flags;
Russell King0077d452006-01-21 23:03:28 +0000720 new_flags = new_serial.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 old_custom_divisor = port->custom_divisor;
722
723 if (!capable(CAP_SYS_ADMIN)) {
724 retval = -EPERM;
725 if (change_irq || change_port ||
726 (new_serial.baud_base != port->uartclk / 16) ||
727 (close_delay != state->close_delay) ||
728 (closing_wait != state->closing_wait) ||
Russell King947deee2006-07-02 20:45:51 +0100729 (new_serial.xmit_fifo_size &&
730 new_serial.xmit_fifo_size != port->fifosize) ||
Russell King0077d452006-01-21 23:03:28 +0000731 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 goto exit;
733 port->flags = ((port->flags & ~UPF_USR_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000734 (new_flags & UPF_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 port->custom_divisor = new_serial.custom_divisor;
736 goto check_and_exit;
737 }
738
739 /*
740 * Ask the low level driver to verify the settings.
741 */
742 if (port->ops->verify_port)
743 retval = port->ops->verify_port(port, &new_serial);
744
Yinghai Lua62c4132008-08-19 20:49:55 -0700745 if ((new_serial.irq >= nr_irqs) || (new_serial.irq < 0) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 (new_serial.baud_base < 9600))
747 retval = -EINVAL;
748
749 if (retval)
750 goto exit;
751
752 if (change_port || change_irq) {
753 retval = -EBUSY;
754
755 /*
756 * Make sure that we are the sole user of this port.
757 */
758 if (uart_users(state) > 1)
759 goto exit;
760
761 /*
762 * We need to shutdown the serial port at the old
763 * port/type/irq combination.
764 */
765 uart_shutdown(state);
766 }
767
768 if (change_port) {
769 unsigned long old_iobase, old_mapbase;
770 unsigned int old_type, old_iotype, old_hub6, old_shift;
771
772 old_iobase = port->iobase;
773 old_mapbase = port->mapbase;
774 old_type = port->type;
775 old_hub6 = port->hub6;
776 old_iotype = port->iotype;
777 old_shift = port->regshift;
778
779 /*
780 * Free and release old regions
781 */
782 if (old_type != PORT_UNKNOWN)
783 port->ops->release_port(port);
784
785 port->iobase = new_port;
786 port->type = new_serial.type;
787 port->hub6 = new_serial.hub6;
788 port->iotype = new_serial.io_type;
789 port->regshift = new_serial.iomem_reg_shift;
790 port->mapbase = (unsigned long)new_serial.iomem_base;
791
792 /*
793 * Claim and map the new regions
794 */
795 if (port->type != PORT_UNKNOWN) {
796 retval = port->ops->request_port(port);
797 } else {
798 /* Always success - Jean II */
799 retval = 0;
800 }
801
802 /*
803 * If we fail to request resources for the
804 * new port, try to restore the old settings.
805 */
806 if (retval && old_type != PORT_UNKNOWN) {
807 port->iobase = old_iobase;
808 port->type = old_type;
809 port->hub6 = old_hub6;
810 port->iotype = old_iotype;
811 port->regshift = old_shift;
812 port->mapbase = old_mapbase;
813 retval = port->ops->request_port(port);
814 /*
815 * If we failed to restore the old settings,
816 * we fail like this.
817 */
818 if (retval)
819 port->type = PORT_UNKNOWN;
820
821 /*
822 * We failed anyway.
823 */
824 retval = -EBUSY;
Alan Coxa46c9992008-02-08 04:18:53 -0800825 /* Added to return the correct error -Ram Gupta */
826 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828 }
829
David Gibsonabb4a232007-05-06 14:48:49 -0700830 if (change_irq)
831 port->irq = new_serial.irq;
832 if (!(port->flags & UPF_FIXED_PORT))
833 port->uartclk = new_serial.baud_base * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000835 (new_flags & UPF_CHANGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 port->custom_divisor = new_serial.custom_divisor;
837 state->close_delay = close_delay;
838 state->closing_wait = closing_wait;
Russell King947deee2006-07-02 20:45:51 +0100839 if (new_serial.xmit_fifo_size)
840 port->fifosize = new_serial.xmit_fifo_size;
Alan Coxf7519282009-01-02 13:49:21 +0000841 if (state->info.port.tty)
842 state->info.port.tty->low_latency =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
844
845 check_and_exit:
846 retval = 0;
847 if (port->type == PORT_UNKNOWN)
848 goto exit;
Alan Coxf7519282009-01-02 13:49:21 +0000849 if (state->info.flags & UIF_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
851 old_custom_divisor != port->custom_divisor) {
852 /*
853 * If they're setting up a custom divisor or speed,
854 * instead of clearing it, then bitch about it. No
855 * need to rate-limit; it's CAP_SYS_ADMIN only.
856 */
857 if (port->flags & UPF_SPD_MASK) {
858 char buf[64];
859 printk(KERN_NOTICE
860 "%s sets custom speed on %s. This "
861 "is deprecated.\n", current->comm,
Alan Coxf7519282009-01-02 13:49:21 +0000862 tty_name(state->info.port.tty, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864 uart_change_speed(state, NULL);
865 }
866 } else
867 retval = uart_startup(state, 1);
868 exit:
Ingo Molnare2862f62006-01-13 21:37:07 +0000869 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return retval;
871}
872
873
874/*
875 * uart_get_lsr_info - get line status register info.
876 * Note: uart_ioctl protects us against hangups.
877 */
878static int uart_get_lsr_info(struct uart_state *state,
879 unsigned int __user *value)
880{
881 struct uart_port *port = state->port;
882 unsigned int result;
883
884 result = port->ops->tx_empty(port);
885
886 /*
887 * If we're about to load something into the transmit
888 * register, we'll pretend the transmitter isn't empty to
889 * avoid a race condition (depending on when the transmit
890 * interrupt happens).
891 */
892 if (port->x_char ||
Alan Coxf7519282009-01-02 13:49:21 +0000893 ((uart_circ_chars_pending(&state->info.xmit) > 0) &&
894 !state->info.port.tty->stopped && !state->info.port.tty->hw_stopped))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 result &= ~TIOCSER_TEMT;
Alan Coxa46c9992008-02-08 04:18:53 -0800896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return put_user(result, value);
898}
899
900static int uart_tiocmget(struct tty_struct *tty, struct file *file)
901{
902 struct uart_state *state = tty->driver_data;
903 struct uart_port *port = state->port;
904 int result = -EIO;
905
Ingo Molnare2862f62006-01-13 21:37:07 +0000906 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if ((!file || !tty_hung_up_p(file)) &&
908 !(tty->flags & (1 << TTY_IO_ERROR))) {
909 result = port->mctrl;
Russell Kingc5f46442005-06-29 09:42:38 +0100910
911 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 result |= port->ops->get_mctrl(port);
Russell Kingc5f46442005-06-29 09:42:38 +0100913 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000915 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 return result;
918}
919
920static int
921uart_tiocmset(struct tty_struct *tty, struct file *file,
922 unsigned int set, unsigned int clear)
923{
924 struct uart_state *state = tty->driver_data;
925 struct uart_port *port = state->port;
926 int ret = -EIO;
927
Ingo Molnare2862f62006-01-13 21:37:07 +0000928 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if ((!file || !tty_hung_up_p(file)) &&
930 !(tty->flags & (1 << TTY_IO_ERROR))) {
931 uart_update_mctrl(port, set, clear);
932 ret = 0;
933 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000934 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return ret;
936}
937
Alan Cox9e989662008-07-22 11:18:03 +0100938static int uart_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 struct uart_state *state = tty->driver_data;
941 struct uart_port *port = state->port;
942
Ingo Molnare2862f62006-01-13 21:37:07 +0000943 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 if (port->type != PORT_UNKNOWN)
946 port->ops->break_ctl(port, break_state);
947
Ingo Molnare2862f62006-01-13 21:37:07 +0000948 mutex_unlock(&state->mutex);
Alan Cox9e989662008-07-22 11:18:03 +0100949 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
952static int uart_do_autoconfig(struct uart_state *state)
953{
954 struct uart_port *port = state->port;
955 int flags, ret;
956
957 if (!capable(CAP_SYS_ADMIN))
958 return -EPERM;
959
960 /*
961 * Take the per-port semaphore. This prevents count from
962 * changing, and hence any extra opens of the port while
963 * we're auto-configuring.
964 */
Ingo Molnare2862f62006-01-13 21:37:07 +0000965 if (mutex_lock_interruptible(&state->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return -ERESTARTSYS;
967
968 ret = -EBUSY;
969 if (uart_users(state) == 1) {
970 uart_shutdown(state);
971
972 /*
973 * If we already have a port type configured,
974 * we must release its resources.
975 */
976 if (port->type != PORT_UNKNOWN)
977 port->ops->release_port(port);
978
979 flags = UART_CONFIG_TYPE;
980 if (port->flags & UPF_AUTO_IRQ)
981 flags |= UART_CONFIG_IRQ;
982
983 /*
984 * This will claim the ports resources if
985 * a port is found.
986 */
987 port->ops->config_port(port, flags);
988
989 ret = uart_startup(state, 1);
990 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000991 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return ret;
993}
994
995/*
996 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
997 * - mask passed in arg for lines of interest
998 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
999 * Caller should use TIOCGICOUNT to see which one it was
1000 */
1001static int
1002uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1003{
1004 struct uart_port *port = state->port;
1005 DECLARE_WAITQUEUE(wait, current);
1006 struct uart_icount cprev, cnow;
1007 int ret;
1008
1009 /*
1010 * note the counters on entry
1011 */
1012 spin_lock_irq(&port->lock);
1013 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
1014
1015 /*
1016 * Force modem status interrupts on
1017 */
1018 port->ops->enable_ms(port);
1019 spin_unlock_irq(&port->lock);
1020
Alan Coxf7519282009-01-02 13:49:21 +00001021 add_wait_queue(&state->info.delta_msr_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 for (;;) {
1023 spin_lock_irq(&port->lock);
1024 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1025 spin_unlock_irq(&port->lock);
1026
1027 set_current_state(TASK_INTERRUPTIBLE);
1028
1029 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1030 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1031 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1032 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Alan Coxa46c9992008-02-08 04:18:53 -08001033 ret = 0;
1034 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036
1037 schedule();
1038
1039 /* see if a signal did it */
1040 if (signal_pending(current)) {
1041 ret = -ERESTARTSYS;
1042 break;
1043 }
1044
1045 cprev = cnow;
1046 }
1047
1048 current->state = TASK_RUNNING;
Alan Coxf7519282009-01-02 13:49:21 +00001049 remove_wait_queue(&state->info.delta_msr_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 return ret;
1052}
1053
1054/*
1055 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1056 * Return: write counters to the user passed counter struct
1057 * NB: both 1->0 and 0->1 transitions are counted except for
1058 * RI where only 0->1 is counted.
1059 */
1060static int uart_get_count(struct uart_state *state,
1061 struct serial_icounter_struct __user *icnt)
1062{
1063 struct serial_icounter_struct icount;
1064 struct uart_icount cnow;
1065 struct uart_port *port = state->port;
1066
1067 spin_lock_irq(&port->lock);
1068 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1069 spin_unlock_irq(&port->lock);
1070
1071 icount.cts = cnow.cts;
1072 icount.dsr = cnow.dsr;
1073 icount.rng = cnow.rng;
1074 icount.dcd = cnow.dcd;
1075 icount.rx = cnow.rx;
1076 icount.tx = cnow.tx;
1077 icount.frame = cnow.frame;
1078 icount.overrun = cnow.overrun;
1079 icount.parity = cnow.parity;
1080 icount.brk = cnow.brk;
1081 icount.buf_overrun = cnow.buf_overrun;
1082
1083 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1084}
1085
1086/*
Alan Coxe5238442008-04-30 00:53:28 -07001087 * Called via sys_ioctl. We can use spin_lock_irq() here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 */
1089static int
1090uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1091 unsigned long arg)
1092{
1093 struct uart_state *state = tty->driver_data;
1094 void __user *uarg = (void __user *)arg;
1095 int ret = -ENOIOCTLCMD;
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 /*
1099 * These ioctls don't rely on the hardware to be present.
1100 */
1101 switch (cmd) {
1102 case TIOCGSERIAL:
1103 ret = uart_get_info(state, uarg);
1104 break;
1105
1106 case TIOCSSERIAL:
1107 ret = uart_set_info(state, uarg);
1108 break;
1109
1110 case TIOCSERCONFIG:
1111 ret = uart_do_autoconfig(state);
1112 break;
1113
1114 case TIOCSERGWILD: /* obsolete */
1115 case TIOCSERSWILD: /* obsolete */
1116 ret = 0;
1117 break;
1118 }
1119
1120 if (ret != -ENOIOCTLCMD)
1121 goto out;
1122
1123 if (tty->flags & (1 << TTY_IO_ERROR)) {
1124 ret = -EIO;
1125 goto out;
1126 }
1127
1128 /*
1129 * The following should only be used when hardware is present.
1130 */
1131 switch (cmd) {
1132 case TIOCMIWAIT:
1133 ret = uart_wait_modem_status(state, arg);
1134 break;
1135
1136 case TIOCGICOUNT:
1137 ret = uart_get_count(state, uarg);
1138 break;
1139 }
1140
1141 if (ret != -ENOIOCTLCMD)
1142 goto out;
1143
Ingo Molnare2862f62006-01-13 21:37:07 +00001144 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 if (tty_hung_up_p(filp)) {
1147 ret = -EIO;
1148 goto out_up;
1149 }
1150
1151 /*
1152 * All these rely on hardware being present and need to be
1153 * protected against the tty being hung up.
1154 */
1155 switch (cmd) {
1156 case TIOCSERGETLSR: /* Get line status register */
1157 ret = uart_get_lsr_info(state, uarg);
1158 break;
1159
1160 default: {
1161 struct uart_port *port = state->port;
1162 if (port->ops->ioctl)
1163 ret = port->ops->ioctl(port, cmd, arg);
1164 break;
1165 }
1166 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001167out_up:
Ingo Molnare2862f62006-01-13 21:37:07 +00001168 mutex_unlock(&state->mutex);
Alan Coxf34d7a52008-04-30 00:54:13 -07001169out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return ret;
1171}
1172
Linus Torvaldsedeb2802008-06-04 10:35:03 -07001173static void uart_set_ldisc(struct tty_struct *tty)
Alan Cox64e91592008-06-03 15:18:54 +01001174{
1175 struct uart_state *state = tty->driver_data;
1176 struct uart_port *port = state->port;
1177
1178 if (port->ops->set_ldisc)
1179 port->ops->set_ldisc(port);
1180}
1181
Alan Coxa46c9992008-02-08 04:18:53 -08001182static void uart_set_termios(struct tty_struct *tty,
1183 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 struct uart_state *state = tty->driver_data;
1186 unsigned long flags;
1187 unsigned int cflag = tty->termios->c_cflag;
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /*
1191 * These are the bits that are used to setup various
David Woodhouse20620d62007-08-22 14:01:11 -07001192 * flags in the low level driver. We can ignore the Bfoo
1193 * bits in c_cflag; c_[io]speed will always be set
1194 * appropriately by set_termios() in tty_ioctl.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 */
1196#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if ((cflag ^ old_termios->c_cflag) == 0 &&
David Woodhouse20620d62007-08-22 14:01:11 -07001198 tty->termios->c_ospeed == old_termios->c_ospeed &&
1199 tty->termios->c_ispeed == old_termios->c_ispeed &&
Alan Coxe5238442008-04-30 00:53:28 -07001200 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return;
Alan Coxe5238442008-04-30 00:53:28 -07001202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 uart_change_speed(state, old_termios);
1205
1206 /* Handle transition to B0 status */
1207 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1208 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1209
1210 /* Handle transition away from B0 status */
1211 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1212 unsigned int mask = TIOCM_DTR;
1213 if (!(cflag & CRTSCTS) ||
1214 !test_bit(TTY_THROTTLED, &tty->flags))
1215 mask |= TIOCM_RTS;
1216 uart_set_mctrl(state->port, mask);
1217 }
1218
1219 /* Handle turning off CRTSCTS */
1220 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1221 spin_lock_irqsave(&state->port->lock, flags);
1222 tty->hw_stopped = 0;
1223 __uart_start(tty);
1224 spin_unlock_irqrestore(&state->port->lock, flags);
1225 }
1226
Russell King0dd7a1a2005-06-29 18:40:53 +01001227 /* Handle turning on CRTSCTS */
1228 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1229 spin_lock_irqsave(&state->port->lock, flags);
1230 if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
1231 tty->hw_stopped = 1;
Russell Kingb129a8c2005-08-31 10:12:14 +01001232 state->port->ops->stop_tx(state->port);
Russell King0dd7a1a2005-06-29 18:40:53 +01001233 }
1234 spin_unlock_irqrestore(&state->port->lock, flags);
1235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236#if 0
1237 /*
1238 * No need to wake up processes in open wait, since they
1239 * sample the CLOCAL flag once, and don't recheck it.
1240 * XXX It's not clear whether the current behavior is correct
1241 * or not. Hence, this may change.....
1242 */
1243 if (!(old_termios->c_cflag & CLOCAL) &&
1244 (tty->termios->c_cflag & CLOCAL))
Alan Coxf7519282009-01-02 13:49:21 +00001245 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246#endif
1247}
1248
1249/*
1250 * In 2.4.5, calls to this will be serialized via the BKL in
1251 * linux/drivers/char/tty_io.c:tty_release()
1252 * linux/drivers/char/tty_io.c:do_tty_handup()
1253 */
1254static void uart_close(struct tty_struct *tty, struct file *filp)
1255{
1256 struct uart_state *state = tty->driver_data;
1257 struct uart_port *port;
Alan Coxa46c9992008-02-08 04:18:53 -08001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 BUG_ON(!kernel_locked());
1260
1261 if (!state || !state->port)
1262 return;
1263
1264 port = state->port;
1265
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001266 pr_debug("uart_close(%d) called\n", port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Ingo Molnare2862f62006-01-13 21:37:07 +00001268 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 if (tty_hung_up_p(filp))
1271 goto done;
1272
1273 if ((tty->count == 1) && (state->count != 1)) {
1274 /*
1275 * Uh, oh. tty->count is 1, which means that the tty
1276 * structure will be freed. state->count should always
1277 * be one in these conditions. If it's greater than
1278 * one, we've got real problems, since it means the
1279 * serial port won't be shutdown.
1280 */
1281 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1282 "state->count is %d\n", state->count);
1283 state->count = 1;
1284 }
1285 if (--state->count < 0) {
1286 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1287 tty->name, state->count);
1288 state->count = 0;
1289 }
1290 if (state->count)
1291 goto done;
1292
1293 /*
1294 * Now we wait for the transmit buffer to clear; and we notify
1295 * the line discipline to only process XON/XOFF characters by
1296 * setting tty->closing.
1297 */
1298 tty->closing = 1;
1299
1300 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1301 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1302
1303 /*
1304 * At this point, we stop accepting input. To do this, we
1305 * disable the receive line status interrupts.
1306 */
Alan Coxf7519282009-01-02 13:49:21 +00001307 if (state->info.flags & UIF_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 unsigned long flags;
1309 spin_lock_irqsave(&port->lock, flags);
1310 port->ops->stop_rx(port);
1311 spin_unlock_irqrestore(&port->lock, flags);
1312 /*
1313 * Before we drop DTR, make sure the UART transmitter
1314 * has completely drained; this is especially
1315 * important if there is a transmit FIFO!
1316 */
1317 uart_wait_until_sent(tty, port->timeout);
1318 }
1319
1320 uart_shutdown(state);
1321 uart_flush_buffer(tty);
1322
Alan Coxa46c9992008-02-08 04:18:53 -08001323 tty_ldisc_flush(tty);
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 tty->closing = 0;
Alan Coxf7519282009-01-02 13:49:21 +00001326 state->info.port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Alan Coxf7519282009-01-02 13:49:21 +00001328 if (state->info.port.blocked_open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (state->close_delay)
1330 msleep_interruptible(state->close_delay);
1331 } else if (!uart_console(port)) {
1332 uart_change_pm(state, 3);
1333 }
1334
1335 /*
1336 * Wake up anyone trying to open this port.
1337 */
Alan Coxf7519282009-01-02 13:49:21 +00001338 state->info.flags &= ~UIF_NORMAL_ACTIVE;
1339 wake_up_interruptible(&state->info.port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 done:
Ingo Molnare2862f62006-01-13 21:37:07 +00001342 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
1345static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1346{
1347 struct uart_state *state = tty->driver_data;
1348 struct uart_port *port = state->port;
1349 unsigned long char_time, expire;
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1352 return;
1353
Alan Coxf34d7a52008-04-30 00:54:13 -07001354 lock_kernel();
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /*
1357 * Set the check interval to be 1/5 of the estimated time to
1358 * send a single character, and make it at least 1. The check
1359 * interval should also be less than the timeout.
1360 *
1361 * Note: we have to use pretty tight timings here to satisfy
1362 * the NIST-PCTS.
1363 */
1364 char_time = (port->timeout - HZ/50) / port->fifosize;
1365 char_time = char_time / 5;
1366 if (char_time == 0)
1367 char_time = 1;
1368 if (timeout && timeout < char_time)
1369 char_time = timeout;
1370
1371 /*
1372 * If the transmitter hasn't cleared in twice the approximate
1373 * amount of time to send the entire FIFO, it probably won't
1374 * ever clear. This assumes the UART isn't doing flow
1375 * control, which is currently the case. Hence, if it ever
1376 * takes longer than port->timeout, this is probably due to a
1377 * UART bug of some kind. So, we clamp the timeout parameter at
1378 * 2*port->timeout.
1379 */
1380 if (timeout == 0 || timeout > 2 * port->timeout)
1381 timeout = 2 * port->timeout;
1382
1383 expire = jiffies + timeout;
1384
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001385 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
Alan Coxa46c9992008-02-08 04:18:53 -08001386 port->line, jiffies, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 /*
1389 * Check whether the transmitter is empty every 'char_time'.
1390 * 'timeout' / 'expire' give us the maximum amount of time
1391 * we wait.
1392 */
1393 while (!port->ops->tx_empty(port)) {
1394 msleep_interruptible(jiffies_to_msecs(char_time));
1395 if (signal_pending(current))
1396 break;
1397 if (time_after(jiffies, expire))
1398 break;
1399 }
1400 set_current_state(TASK_RUNNING); /* might not be needed */
Alan Coxf34d7a52008-04-30 00:54:13 -07001401 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
1404/*
1405 * This is called with the BKL held in
1406 * linux/drivers/char/tty_io.c:do_tty_hangup()
1407 * We're called from the eventd thread, so we can sleep for
1408 * a _short_ time only.
1409 */
1410static void uart_hangup(struct tty_struct *tty)
1411{
1412 struct uart_state *state = tty->driver_data;
Alan Coxf7519282009-01-02 13:49:21 +00001413 struct uart_info *info = &state->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 BUG_ON(!kernel_locked());
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001416 pr_debug("uart_hangup(%d)\n", state->port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Ingo Molnare2862f62006-01-13 21:37:07 +00001418 mutex_lock(&state->mutex);
Alan Coxf7519282009-01-02 13:49:21 +00001419 if (info->flags & UIF_NORMAL_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 uart_flush_buffer(tty);
1421 uart_shutdown(state);
1422 state->count = 0;
Alan Coxf7519282009-01-02 13:49:21 +00001423 info->flags &= ~UIF_NORMAL_ACTIVE;
1424 info->port.tty = NULL;
1425 wake_up_interruptible(&info->port.open_wait);
1426 wake_up_interruptible(&info->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
Ingo Molnare2862f62006-01-13 21:37:07 +00001428 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
1431/*
1432 * Copy across the serial console cflag setting into the termios settings
1433 * for the initial open of the port. This allows continuity between the
1434 * kernel settings, and the settings init adopts when it opens the port
1435 * for the first time.
1436 */
1437static void uart_update_termios(struct uart_state *state)
1438{
Alan Coxf7519282009-01-02 13:49:21 +00001439 struct tty_struct *tty = state->info.port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 struct uart_port *port = state->port;
1441
1442 if (uart_console(port) && port->cons->cflag) {
1443 tty->termios->c_cflag = port->cons->cflag;
1444 port->cons->cflag = 0;
1445 }
1446
1447 /*
1448 * If the device failed to grab its irq resources,
1449 * or some other error occurred, don't try to talk
1450 * to the port hardware.
1451 */
1452 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1453 /*
1454 * Make termios settings take effect.
1455 */
1456 uart_change_speed(state, NULL);
1457
1458 /*
1459 * And finally enable the RTS and DTR signals.
1460 */
1461 if (tty->termios->c_cflag & CBAUD)
1462 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1463 }
1464}
1465
1466/*
1467 * Block the open until the port is ready. We must be called with
1468 * the per-port semaphore held.
1469 */
1470static int
1471uart_block_til_ready(struct file *filp, struct uart_state *state)
1472{
1473 DECLARE_WAITQUEUE(wait, current);
Alan Coxf7519282009-01-02 13:49:21 +00001474 struct uart_info *info = &state->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 struct uart_port *port = state->port;
Russell Kingc5f46442005-06-29 09:42:38 +01001476 unsigned int mctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001478 info->port.blocked_open++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 state->count--;
1480
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001481 add_wait_queue(&info->port.open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 while (1) {
1483 set_current_state(TASK_INTERRUPTIBLE);
1484
1485 /*
1486 * If we have been hung up, tell userspace/restart open.
1487 */
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001488 if (tty_hung_up_p(filp) || info->port.tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 break;
1490
1491 /*
1492 * If the port has been closed, tell userspace/restart open.
1493 */
1494 if (!(info->flags & UIF_INITIALIZED))
1495 break;
1496
1497 /*
1498 * If non-blocking mode is set, or CLOCAL mode is set,
1499 * we don't want to wait for the modem status lines to
1500 * indicate that the port is ready.
1501 *
1502 * Also, if the port is not enabled/configured, we want
1503 * to allow the open to succeed here. Note that we will
1504 * have set TTY_IO_ERROR for a non-existant port.
1505 */
1506 if ((filp->f_flags & O_NONBLOCK) ||
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001507 (info->port.tty->termios->c_cflag & CLOCAL) ||
1508 (info->port.tty->flags & (1 << TTY_IO_ERROR)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 /*
1512 * Set DTR to allow modem to know we're waiting. Do
1513 * not set RTS here - we want to make sure we catch
1514 * the data from the modem.
1515 */
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001516 if (info->port.tty->termios->c_cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 uart_set_mctrl(port, TIOCM_DTR);
1518
1519 /*
1520 * and wait for the carrier to indicate that the
1521 * modem is ready for us.
1522 */
Russell Kingc5f46442005-06-29 09:42:38 +01001523 spin_lock_irq(&port->lock);
Russell Kingf61051c2006-01-07 23:11:23 +00001524 port->ops->enable_ms(port);
Russell Kingc5f46442005-06-29 09:42:38 +01001525 mctrl = port->ops->get_mctrl(port);
1526 spin_unlock_irq(&port->lock);
1527 if (mctrl & TIOCM_CAR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 break;
1529
Ingo Molnare2862f62006-01-13 21:37:07 +00001530 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 schedule();
Ingo Molnare2862f62006-01-13 21:37:07 +00001532 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 if (signal_pending(current))
1535 break;
1536 }
1537 set_current_state(TASK_RUNNING);
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001538 remove_wait_queue(&info->port.open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 state->count++;
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001541 info->port.blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 if (signal_pending(current))
1544 return -ERESTARTSYS;
1545
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001546 if (!info->port.tty || tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return -EAGAIN;
1548
1549 return 0;
1550}
1551
1552static struct uart_state *uart_get(struct uart_driver *drv, int line)
1553{
1554 struct uart_state *state;
Russell King68ac64c2006-04-30 11:13:50 +01001555 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 state = drv->state + line;
Ingo Molnare2862f62006-01-13 21:37:07 +00001558 if (mutex_lock_interruptible(&state->mutex)) {
Russell King68ac64c2006-04-30 11:13:50 +01001559 ret = -ERESTARTSYS;
1560 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
1562
1563 state->count++;
Russell King68ac64c2006-04-30 11:13:50 +01001564 if (!state->port || state->port->flags & UPF_DEAD) {
1565 ret = -ENXIO;
1566 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return state;
Russell King68ac64c2006-04-30 11:13:50 +01001569
1570 err_unlock:
1571 state->count--;
1572 mutex_unlock(&state->mutex);
1573 err:
1574 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
1577/*
Denis Cheng922f9cf2008-02-08 04:22:00 -08001578 * calls to uart_open are serialised by the BKL in
1579 * fs/char_dev.c:chrdev_open()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 * Note that if this fails, then uart_close() _will_ be called.
1581 *
1582 * In time, we want to scrap the "opening nonpresent ports"
1583 * behaviour and implement an alternative way for setserial
1584 * to set base addresses/ports/types. This will allow us to
1585 * get rid of a certain amount of extra tests.
1586 */
1587static int uart_open(struct tty_struct *tty, struct file *filp)
1588{
1589 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1590 struct uart_state *state;
1591 int retval, line = tty->index;
1592
1593 BUG_ON(!kernel_locked());
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001594 pr_debug("uart_open(%d) called\n", line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 /*
1597 * tty->driver->num won't change, so we won't fail here with
1598 * tty->driver_data set to something non-NULL (and therefore
1599 * we won't get caught by uart_close()).
1600 */
1601 retval = -ENODEV;
1602 if (line >= tty->driver->num)
1603 goto fail;
1604
1605 /*
1606 * We take the semaphore inside uart_get to guarantee that we won't
1607 * be re-entered while allocating the info structure, or while we
1608 * request any IRQs that the driver may need. This also has the nice
1609 * side-effect that it delays the action of uart_hangup, so we can
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001610 * guarantee that info->port.tty will always contain something reasonable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 */
1612 state = uart_get(drv, line);
1613 if (IS_ERR(state)) {
1614 retval = PTR_ERR(state);
1615 goto fail;
1616 }
1617
1618 /*
1619 * Once we set tty->driver_data here, we are guaranteed that
1620 * uart_close() will decrement the driver module use count.
1621 * Any failures from here onwards should not touch the count.
1622 */
1623 tty->driver_data = state;
Alan Coxf7519282009-01-02 13:49:21 +00001624 state->port->info = &state->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1626 tty->alt_speed = 0;
Alan Coxf7519282009-01-02 13:49:21 +00001627 state->info.port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 /*
1630 * If the port is in the middle of closing, bail out now.
1631 */
1632 if (tty_hung_up_p(filp)) {
1633 retval = -EAGAIN;
1634 state->count--;
Ingo Molnare2862f62006-01-13 21:37:07 +00001635 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 goto fail;
1637 }
1638
1639 /*
1640 * Make sure the device is in D0 state.
1641 */
1642 if (state->count == 1)
1643 uart_change_pm(state, 0);
1644
1645 /*
1646 * Start up the serial port.
1647 */
1648 retval = uart_startup(state, 0);
1649
1650 /*
1651 * If we succeeded, wait until the port is ready.
1652 */
1653 if (retval == 0)
1654 retval = uart_block_til_ready(filp, state);
Ingo Molnare2862f62006-01-13 21:37:07 +00001655 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 /*
1658 * If this is the first open to succeed, adjust things to suit.
1659 */
Alan Coxf7519282009-01-02 13:49:21 +00001660 if (retval == 0 && !(state->info.flags & UIF_NORMAL_ACTIVE)) {
1661 state->info.flags |= UIF_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 uart_update_termios(state);
1664 }
1665
1666 fail:
1667 return retval;
1668}
1669
1670static const char *uart_type(struct uart_port *port)
1671{
1672 const char *str = NULL;
1673
1674 if (port->ops->type)
1675 str = port->ops->type(port);
1676
1677 if (!str)
1678 str = "unknown";
1679
1680 return str;
1681}
1682
1683#ifdef CONFIG_PROC_FS
1684
1685static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1686{
1687 struct uart_state *state = drv->state + i;
George G. Davis3689a0e2007-02-14 00:33:06 -08001688 int pm_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 struct uart_port *port = state->port;
1690 char stat_buf[32];
1691 unsigned int status;
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001692 int mmio, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 if (!port)
1695 return 0;
1696
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001697 mmio = port->iotype >= UPIO_MEM;
Josh Boyer4f640ef2007-07-23 18:43:44 -07001698 ret = sprintf(buf, "%d: uart:%s %s%08llX irq:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 port->line, uart_type(port),
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001700 mmio ? "mmio:0x" : "port:",
Josh Boyer4f640ef2007-07-23 18:43:44 -07001701 mmio ? (unsigned long long)port->mapbase
Alan Coxa46c9992008-02-08 04:18:53 -08001702 : (unsigned long long) port->iobase,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 port->irq);
1704
1705 if (port->type == PORT_UNKNOWN) {
1706 strcat(buf, "\n");
1707 return ret + 1;
1708 }
1709
Alan Coxa46c9992008-02-08 04:18:53 -08001710 if (capable(CAP_SYS_ADMIN)) {
George G. Davis3689a0e2007-02-14 00:33:06 -08001711 mutex_lock(&state->mutex);
1712 pm_state = state->pm_state;
1713 if (pm_state)
1714 uart_change_pm(state, 0);
Russell Kingc5f46442005-06-29 09:42:38 +01001715 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 status = port->ops->get_mctrl(port);
Russell Kingc5f46442005-06-29 09:42:38 +01001717 spin_unlock_irq(&port->lock);
George G. Davis3689a0e2007-02-14 00:33:06 -08001718 if (pm_state)
1719 uart_change_pm(state, pm_state);
1720 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 ret += sprintf(buf + ret, " tx:%d rx:%d",
1723 port->icount.tx, port->icount.rx);
1724 if (port->icount.frame)
1725 ret += sprintf(buf + ret, " fe:%d",
1726 port->icount.frame);
1727 if (port->icount.parity)
1728 ret += sprintf(buf + ret, " pe:%d",
1729 port->icount.parity);
1730 if (port->icount.brk)
1731 ret += sprintf(buf + ret, " brk:%d",
1732 port->icount.brk);
1733 if (port->icount.overrun)
1734 ret += sprintf(buf + ret, " oe:%d",
1735 port->icount.overrun);
Alan Coxa46c9992008-02-08 04:18:53 -08001736
1737#define INFOBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (port->mctrl & (bit)) \
1739 strncat(stat_buf, (str), sizeof(stat_buf) - \
1740 strlen(stat_buf) - 2)
Alan Coxa46c9992008-02-08 04:18:53 -08001741#define STATBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (status & (bit)) \
1743 strncat(stat_buf, (str), sizeof(stat_buf) - \
1744 strlen(stat_buf) - 2)
1745
1746 stat_buf[0] = '\0';
1747 stat_buf[1] = '\0';
1748 INFOBIT(TIOCM_RTS, "|RTS");
1749 STATBIT(TIOCM_CTS, "|CTS");
1750 INFOBIT(TIOCM_DTR, "|DTR");
1751 STATBIT(TIOCM_DSR, "|DSR");
1752 STATBIT(TIOCM_CAR, "|CD");
1753 STATBIT(TIOCM_RNG, "|RI");
1754 if (stat_buf[0])
1755 stat_buf[0] = ' ';
1756 strcat(stat_buf, "\n");
Alan Coxa46c9992008-02-08 04:18:53 -08001757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 ret += sprintf(buf + ret, stat_buf);
1759 } else {
1760 strcat(buf, "\n");
1761 ret++;
1762 }
1763#undef STATBIT
1764#undef INFOBIT
1765 return ret;
1766}
1767
1768static int uart_read_proc(char *page, char **start, off_t off,
1769 int count, int *eof, void *data)
1770{
1771 struct tty_driver *ttydrv = data;
1772 struct uart_driver *drv = ttydrv->driver_state;
1773 int i, len = 0, l;
1774 off_t begin = 0;
1775
1776 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1777 "", "", "");
1778 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1779 l = uart_line_info(page + len, drv, i);
1780 len += l;
1781 if (len + begin > off + count)
1782 goto done;
1783 if (len + begin < off) {
1784 begin += len;
1785 len = 0;
1786 }
1787 }
1788 *eof = 1;
1789 done:
1790 if (off >= len + begin)
1791 return 0;
1792 *start = page + (off - begin);
1793 return (count < begin + len - off) ? count : (begin + len - off);
1794}
1795#endif
1796
Andrew Morton4a1b5502008-03-07 15:51:16 -08001797#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798/*
Russell Kingd3587882006-03-20 20:00:09 +00001799 * uart_console_write - write a console message to a serial port
1800 * @port: the port to write the message
1801 * @s: array of characters
1802 * @count: number of characters in string to write
1803 * @write: function to write character to port
1804 */
1805void uart_console_write(struct uart_port *port, const char *s,
1806 unsigned int count,
1807 void (*putchar)(struct uart_port *, int))
1808{
1809 unsigned int i;
1810
1811 for (i = 0; i < count; i++, s++) {
1812 if (*s == '\n')
1813 putchar(port, '\r');
1814 putchar(port, *s);
1815 }
1816}
1817EXPORT_SYMBOL_GPL(uart_console_write);
1818
1819/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 * Check whether an invalid uart number has been specified, and
1821 * if so, search for the first available port that does have
1822 * console support.
1823 */
1824struct uart_port * __init
1825uart_get_console(struct uart_port *ports, int nr, struct console *co)
1826{
1827 int idx = co->index;
1828
1829 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1830 ports[idx].membase == NULL))
1831 for (idx = 0; idx < nr; idx++)
1832 if (ports[idx].iobase != 0 ||
1833 ports[idx].membase != NULL)
1834 break;
1835
1836 co->index = idx;
1837
1838 return ports + idx;
1839}
1840
1841/**
1842 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1843 * @options: pointer to option string
1844 * @baud: pointer to an 'int' variable for the baud rate.
1845 * @parity: pointer to an 'int' variable for the parity.
1846 * @bits: pointer to an 'int' variable for the number of data bits.
1847 * @flow: pointer to an 'int' variable for the flow control character.
1848 *
1849 * uart_parse_options decodes a string containing the serial console
1850 * options. The format of the string is <baud><parity><bits><flow>,
1851 * eg: 115200n8r
1852 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001853void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1855{
1856 char *s = options;
1857
1858 *baud = simple_strtoul(s, NULL, 10);
1859 while (*s >= '0' && *s <= '9')
1860 s++;
1861 if (*s)
1862 *parity = *s++;
1863 if (*s)
1864 *bits = *s++ - '0';
1865 if (*s)
1866 *flow = *s;
1867}
Jason Wesself2d937f2008-04-17 20:05:37 +02001868EXPORT_SYMBOL_GPL(uart_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870struct baud_rates {
1871 unsigned int rate;
1872 unsigned int cflag;
1873};
1874
Arjan van de Vencb3592b2005-11-28 21:04:11 +00001875static const struct baud_rates baud_rates[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 { 921600, B921600 },
1877 { 460800, B460800 },
1878 { 230400, B230400 },
1879 { 115200, B115200 },
1880 { 57600, B57600 },
1881 { 38400, B38400 },
1882 { 19200, B19200 },
1883 { 9600, B9600 },
1884 { 4800, B4800 },
1885 { 2400, B2400 },
1886 { 1200, B1200 },
1887 { 0, B38400 }
1888};
1889
1890/**
1891 * uart_set_options - setup the serial console parameters
1892 * @port: pointer to the serial ports uart_port structure
1893 * @co: console pointer
1894 * @baud: baud rate
1895 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1896 * @bits: number of data bits
1897 * @flow: flow control character - 'r' (rts)
1898 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001899int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900uart_set_options(struct uart_port *port, struct console *co,
1901 int baud, int parity, int bits, int flow)
1902{
Alan Cox606d0992006-12-08 02:38:45 -08001903 struct ktermios termios;
Alan Cox149b36e2007-10-18 01:24:16 -07001904 static struct ktermios dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 int i;
1906
Russell King976ecd12005-07-03 21:05:45 +01001907 /*
1908 * Ensure that the serial console lock is initialised
1909 * early.
1910 */
1911 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07001912 lockdep_set_class(&port->lock, &port_lock_key);
Russell King976ecd12005-07-03 21:05:45 +01001913
Alan Cox606d0992006-12-08 02:38:45 -08001914 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1917
1918 /*
1919 * Construct a cflag setting.
1920 */
1921 for (i = 0; baud_rates[i].rate; i++)
1922 if (baud_rates[i].rate <= baud)
1923 break;
1924
1925 termios.c_cflag |= baud_rates[i].cflag;
1926
1927 if (bits == 7)
1928 termios.c_cflag |= CS7;
1929 else
1930 termios.c_cflag |= CS8;
1931
1932 switch (parity) {
1933 case 'o': case 'O':
1934 termios.c_cflag |= PARODD;
1935 /*fall through*/
1936 case 'e': case 'E':
1937 termios.c_cflag |= PARENB;
1938 break;
1939 }
1940
1941 if (flow == 'r')
1942 termios.c_cflag |= CRTSCTS;
1943
Yinghai Lu79492682007-07-15 23:37:25 -07001944 /*
1945 * some uarts on other side don't support no flow control.
1946 * So we set * DTR in host uart to make them happy
1947 */
1948 port->mctrl |= TIOCM_DTR;
1949
Alan Cox149b36e2007-10-18 01:24:16 -07001950 port->ops->set_termios(port, &termios, &dummy);
Jason Wesself2d937f2008-04-17 20:05:37 +02001951 /*
1952 * Allow the setting of the UART parameters with a NULL console
1953 * too:
1954 */
1955 if (co)
1956 co->cflag = termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 return 0;
1959}
Jason Wesself2d937f2008-04-17 20:05:37 +02001960EXPORT_SYMBOL_GPL(uart_set_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961#endif /* CONFIG_SERIAL_CORE_CONSOLE */
1962
1963static void uart_change_pm(struct uart_state *state, int pm_state)
1964{
1965 struct uart_port *port = state->port;
Andrew Victor1281e362006-05-16 11:28:49 +01001966
1967 if (state->pm_state != pm_state) {
1968 if (port->ops->pm)
1969 port->ops->pm(port, pm_state, state->pm_state);
1970 state->pm_state = pm_state;
1971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001974struct uart_match {
1975 struct uart_port *port;
1976 struct uart_driver *driver;
1977};
1978
1979static int serial_match_port(struct device *dev, void *data)
1980{
1981 struct uart_match *match = data;
Guennadi Liakhovetski7ca796f2008-07-04 09:59:28 -07001982 struct tty_driver *tty_drv = match->driver->tty_driver;
1983 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1984 match->port->line;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001985
1986 return dev->devt == devt; /* Actually, only one tty per port */
1987}
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1990{
1991 struct uart_state *state = drv->state + port->line;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001992 struct device *tty_dev;
1993 struct uart_match match = {port, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Ingo Molnare2862f62006-01-13 21:37:07 +00001995 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001997 if (!console_suspend_enabled && uart_console(port)) {
1998 /* we're going to avoid suspending serial console */
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07001999 mutex_unlock(&state->mutex);
2000 return 0;
2001 }
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002002
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002003 tty_dev = device_find_child(port->dev, &match, serial_match_port);
2004 if (device_may_wakeup(tty_dev)) {
2005 enable_irq_wake(port->irq);
2006 put_device(tty_dev);
2007 mutex_unlock(&state->mutex);
2008 return 0;
2009 }
2010 port->suspended = 1;
2011
Alan Coxf7519282009-01-02 13:49:21 +00002012 if (state->info.flags & UIF_INITIALIZED) {
Russell Kingba899db2006-01-21 22:45:50 +00002013 const struct uart_ops *ops = port->ops;
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002014 int tries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Alan Coxf7519282009-01-02 13:49:21 +00002016 state->info.flags = (state->info.flags & ~UIF_INITIALIZED)
Russell Kinga6b93a92006-10-01 17:17:40 +01002017 | UIF_SUSPENDED;
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 spin_lock_irq(&port->lock);
Russell Kingb129a8c2005-08-31 10:12:14 +01002020 ops->stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 ops->set_mctrl(port, 0);
2022 ops->stop_rx(port);
2023 spin_unlock_irq(&port->lock);
2024
2025 /*
2026 * Wait for the transmitter to empty.
2027 */
Alan Coxa46c9992008-02-08 04:18:53 -08002028 for (tries = 3; !ops->tx_empty(port) && tries; tries--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 msleep(10);
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002030 if (!tries)
Alan Coxa46c9992008-02-08 04:18:53 -08002031 printk(KERN_ERR "%s%s%s%d: Unable to drain "
2032 "transmitter\n",
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002033 port->dev ? port->dev->bus_id : "",
2034 port->dev ? ": " : "",
David S. Miller84408382008-10-13 10:45:26 +01002035 drv->dev_name,
2036 drv->tty_driver->name_base + port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 ops->shutdown(port);
2039 }
2040
2041 /*
2042 * Disable the console device before suspending.
2043 */
2044 if (uart_console(port))
2045 console_stop(port->cons);
2046
2047 uart_change_pm(state, 3);
2048
Ingo Molnare2862f62006-01-13 21:37:07 +00002049 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 return 0;
2052}
2053
2054int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
2055{
2056 struct uart_state *state = drv->state + port->line;
Arjan van de Ven03a74dc2008-05-23 13:04:49 -07002057 struct device *tty_dev;
2058 struct uart_match match = {port, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Ingo Molnare2862f62006-01-13 21:37:07 +00002060 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002062 if (!console_suspend_enabled && uart_console(port)) {
2063 /* no need to resume serial console, it wasn't suspended */
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002064 mutex_unlock(&state->mutex);
2065 return 0;
2066 }
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002067
Arjan van de Ven03a74dc2008-05-23 13:04:49 -07002068 tty_dev = device_find_child(port->dev, &match, serial_match_port);
2069 if (!port->suspended && device_may_wakeup(tty_dev)) {
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002070 disable_irq_wake(port->irq);
2071 mutex_unlock(&state->mutex);
2072 return 0;
2073 }
2074 port->suspended = 0;
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 /*
2077 * Re-enable the console device after suspending.
2078 */
2079 if (uart_console(port)) {
Alan Cox606d0992006-12-08 02:38:45 -08002080 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 /*
2083 * First try to use the console cflag setting.
2084 */
Alan Cox606d0992006-12-08 02:38:45 -08002085 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 termios.c_cflag = port->cons->cflag;
2087
2088 /*
2089 * If that's unset, use the tty termios setting.
2090 */
Alan Coxf7519282009-01-02 13:49:21 +00002091 if (state->info.port.tty && termios.c_cflag == 0)
2092 termios = *state->info.port.tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Russell King9d778a62008-02-04 22:27:51 -08002094 uart_change_pm(state, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 port->ops->set_termios(port, &termios, NULL);
2096 console_start(port->cons);
2097 }
2098
Alan Coxf7519282009-01-02 13:49:21 +00002099 if (state->info.flags & UIF_SUSPENDED) {
Russell Kingba899db2006-01-21 22:45:50 +00002100 const struct uart_ops *ops = port->ops;
Russell Kingee31b332005-11-13 15:28:51 +00002101 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Russell King9d778a62008-02-04 22:27:51 -08002103 uart_change_pm(state, 0);
Alan Coxf34d7a52008-04-30 00:54:13 -07002104 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 ops->set_mctrl(port, 0);
Alan Coxf34d7a52008-04-30 00:54:13 -07002106 spin_unlock_irq(&port->lock);
Russell Kingee31b332005-11-13 15:28:51 +00002107 ret = ops->startup(port);
2108 if (ret == 0) {
2109 uart_change_speed(state, NULL);
2110 spin_lock_irq(&port->lock);
2111 ops->set_mctrl(port, port->mctrl);
2112 ops->start_tx(port);
2113 spin_unlock_irq(&port->lock);
Alan Coxf7519282009-01-02 13:49:21 +00002114 state->info.flags |= UIF_INITIALIZED;
Russell Kingee31b332005-11-13 15:28:51 +00002115 } else {
2116 /*
2117 * Failed to resume - maybe hardware went away?
2118 * Clear the "initialized" flag so we won't try
2119 * to call the low level drivers shutdown method.
2120 */
Russell Kingee31b332005-11-13 15:28:51 +00002121 uart_shutdown(state);
2122 }
Russell Kinga6b93a92006-10-01 17:17:40 +01002123
Alan Coxf7519282009-01-02 13:49:21 +00002124 state->info.flags &= ~UIF_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
2126
Ingo Molnare2862f62006-01-13 21:37:07 +00002127 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 return 0;
2130}
2131
2132static inline void
2133uart_report_port(struct uart_driver *drv, struct uart_port *port)
2134{
Russell King30b7a3b2005-09-03 15:30:21 +01002135 char address[64];
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 switch (port->iotype) {
2138 case UPIO_PORT:
Andrew Morton9bde10a2008-10-13 10:35:42 +01002139 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 break;
2141 case UPIO_HUB6:
Russell King30b7a3b2005-09-03 15:30:21 +01002142 snprintf(address, sizeof(address),
Andrew Morton9bde10a2008-10-13 10:35:42 +01002143 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 break;
2145 case UPIO_MEM:
2146 case UPIO_MEM32:
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002147 case UPIO_AU:
Zang Roy-r619113be91ec2006-06-30 02:29:58 -07002148 case UPIO_TSI:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002149 case UPIO_DWAPB:
Russell King30b7a3b2005-09-03 15:30:21 +01002150 snprintf(address, sizeof(address),
Josh Boyer4f640ef2007-07-23 18:43:44 -07002151 "MMIO 0x%llx", (unsigned long long)port->mapbase);
Russell King30b7a3b2005-09-03 15:30:21 +01002152 break;
2153 default:
2154 strlcpy(address, "*unknown*", sizeof(address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 break;
2156 }
Russell King30b7a3b2005-09-03 15:30:21 +01002157
Russell King0cf669d2005-10-31 11:42:22 +00002158 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2159 port->dev ? port->dev->bus_id : "",
2160 port->dev ? ": " : "",
David S. Miller84408382008-10-13 10:45:26 +01002161 drv->dev_name,
2162 drv->tty_driver->name_base + port->line,
2163 address, port->irq, uart_type(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
2166static void
2167uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2168 struct uart_port *port)
2169{
2170 unsigned int flags;
2171
2172 /*
2173 * If there isn't a port here, don't do anything further.
2174 */
2175 if (!port->iobase && !port->mapbase && !port->membase)
2176 return;
2177
2178 /*
2179 * Now do the auto configuration stuff. Note that config_port
2180 * is expected to claim the resources and map the port for us.
2181 */
2182 flags = UART_CONFIG_TYPE;
2183 if (port->flags & UPF_AUTO_IRQ)
2184 flags |= UART_CONFIG_IRQ;
2185 if (port->flags & UPF_BOOT_AUTOCONF) {
2186 port->type = PORT_UNKNOWN;
2187 port->ops->config_port(port, flags);
2188 }
2189
2190 if (port->type != PORT_UNKNOWN) {
2191 unsigned long flags;
2192
2193 uart_report_port(drv, port);
2194
George G. Davis3689a0e2007-02-14 00:33:06 -08002195 /* Power up port for set_mctrl() */
2196 uart_change_pm(state, 0);
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 /*
2199 * Ensure that the modem control lines are de-activated.
Yinghai Luc3e46422008-02-04 22:27:46 -08002200 * keep the DTR setting that is set in uart_set_options()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 * We probably don't need a spinlock around this, but
2202 */
2203 spin_lock_irqsave(&port->lock, flags);
Yinghai Luc3e46422008-02-04 22:27:46 -08002204 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 spin_unlock_irqrestore(&port->lock, flags);
2206
2207 /*
Russell King97d97222007-09-01 21:25:09 +01002208 * If this driver supports console, and it hasn't been
2209 * successfully registered yet, try to re-register it.
2210 * It may be that the port was not available.
2211 */
2212 if (port->cons && !(port->cons->flags & CON_ENABLED))
2213 register_console(port->cons);
2214
2215 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 * Power down all ports by default, except the
2217 * console if we have one.
2218 */
2219 if (!uart_console(port))
2220 uart_change_pm(state, 3);
2221 }
2222}
2223
Jason Wesself2d937f2008-04-17 20:05:37 +02002224#ifdef CONFIG_CONSOLE_POLL
2225
2226static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2227{
2228 struct uart_driver *drv = driver->driver_state;
2229 struct uart_state *state = drv->state + line;
2230 struct uart_port *port;
2231 int baud = 9600;
2232 int bits = 8;
2233 int parity = 'n';
2234 int flow = 'n';
2235
2236 if (!state || !state->port)
2237 return -1;
2238
2239 port = state->port;
2240 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2241 return -1;
2242
2243 if (options) {
2244 uart_parse_options(options, &baud, &parity, &bits, &flow);
2245 return uart_set_options(port, NULL, baud, parity, bits, flow);
2246 }
2247
2248 return 0;
2249}
2250
2251static int uart_poll_get_char(struct tty_driver *driver, int line)
2252{
2253 struct uart_driver *drv = driver->driver_state;
2254 struct uart_state *state = drv->state + line;
2255 struct uart_port *port;
2256
2257 if (!state || !state->port)
2258 return -1;
2259
2260 port = state->port;
2261 return port->ops->poll_get_char(port);
2262}
2263
2264static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2265{
2266 struct uart_driver *drv = driver->driver_state;
2267 struct uart_state *state = drv->state + line;
2268 struct uart_port *port;
2269
2270 if (!state || !state->port)
2271 return;
2272
2273 port = state->port;
2274 port->ops->poll_put_char(port, ch);
2275}
2276#endif
2277
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002278static const struct tty_operations uart_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 .open = uart_open,
2280 .close = uart_close,
2281 .write = uart_write,
2282 .put_char = uart_put_char,
2283 .flush_chars = uart_flush_chars,
2284 .write_room = uart_write_room,
2285 .chars_in_buffer= uart_chars_in_buffer,
2286 .flush_buffer = uart_flush_buffer,
2287 .ioctl = uart_ioctl,
2288 .throttle = uart_throttle,
2289 .unthrottle = uart_unthrottle,
2290 .send_xchar = uart_send_xchar,
2291 .set_termios = uart_set_termios,
Alan Cox64e91592008-06-03 15:18:54 +01002292 .set_ldisc = uart_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 .stop = uart_stop,
2294 .start = uart_start,
2295 .hangup = uart_hangup,
2296 .break_ctl = uart_break_ctl,
2297 .wait_until_sent= uart_wait_until_sent,
2298#ifdef CONFIG_PROC_FS
2299 .read_proc = uart_read_proc,
2300#endif
2301 .tiocmget = uart_tiocmget,
2302 .tiocmset = uart_tiocmset,
Jason Wesself2d937f2008-04-17 20:05:37 +02002303#ifdef CONFIG_CONSOLE_POLL
2304 .poll_init = uart_poll_init,
2305 .poll_get_char = uart_poll_get_char,
2306 .poll_put_char = uart_poll_put_char,
2307#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308};
2309
2310/**
2311 * uart_register_driver - register a driver with the uart core layer
2312 * @drv: low level driver structure
2313 *
2314 * Register a uart driver with the core driver. We in turn register
2315 * with the tty layer, and initialise the core driver per-port state.
2316 *
2317 * We have a proc file in /proc/tty/driver which is named after the
2318 * normal driver.
2319 *
2320 * drv->port should be NULL, and the per-port structures should be
2321 * registered using uart_add_one_port after this call has succeeded.
2322 */
2323int uart_register_driver(struct uart_driver *drv)
2324{
2325 struct tty_driver *normal = NULL;
2326 int i, retval;
2327
2328 BUG_ON(drv->state);
2329
2330 /*
2331 * Maybe we should be using a slab cache for this, especially if
2332 * we have a large number of ports to handle.
2333 */
Burman Yan8f31bb32007-02-14 00:33:07 -08002334 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 retval = -ENOMEM;
2336 if (!drv->state)
2337 goto out;
2338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 normal = alloc_tty_driver(drv->nr);
2340 if (!normal)
2341 goto out;
2342
2343 drv->tty_driver = normal;
2344
2345 normal->owner = drv->owner;
2346 normal->driver_name = drv->driver_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 normal->name = drv->dev_name;
2348 normal->major = drv->major;
2349 normal->minor_start = drv->minor;
2350 normal->type = TTY_DRIVER_TYPE_SERIAL;
2351 normal->subtype = SERIAL_TYPE_NORMAL;
2352 normal->init_termios = tty_std_termios;
2353 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -08002354 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07002355 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 normal->driver_state = drv;
2357 tty_set_operations(normal, &uart_ops);
2358
2359 /*
2360 * Initialise the UART state(s).
2361 */
2362 for (i = 0; i < drv->nr; i++) {
2363 struct uart_state *state = drv->state + i;
2364
2365 state->close_delay = 500; /* .5 seconds */
2366 state->closing_wait = 30000; /* 30 seconds */
Ingo Molnare2862f62006-01-13 21:37:07 +00002367 mutex_init(&state->mutex);
Alan Coxf7519282009-01-02 13:49:21 +00002368
2369 tty_port_init(&state->info.port);
2370 init_waitqueue_head(&state->info.delta_msr_wait);
2371 tasklet_init(&state->info.tlet, uart_tasklet_action,
2372 (unsigned long)state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
2374
2375 retval = tty_register_driver(normal);
2376 out:
2377 if (retval < 0) {
2378 put_tty_driver(normal);
2379 kfree(drv->state);
2380 }
2381 return retval;
2382}
2383
2384/**
2385 * uart_unregister_driver - remove a driver from the uart core layer
2386 * @drv: low level driver structure
2387 *
2388 * Remove all references to a driver from the core driver. The low
2389 * level driver must have removed all its ports via the
2390 * uart_remove_one_port() if it registered them with uart_add_one_port().
2391 * (ie, drv->port == NULL)
2392 */
2393void uart_unregister_driver(struct uart_driver *drv)
2394{
2395 struct tty_driver *p = drv->tty_driver;
2396 tty_unregister_driver(p);
2397 put_tty_driver(p);
2398 kfree(drv->state);
2399 drv->tty_driver = NULL;
2400}
2401
2402struct tty_driver *uart_console_device(struct console *co, int *index)
2403{
2404 struct uart_driver *p = co->data;
2405 *index = co->index;
2406 return p->tty_driver;
2407}
2408
2409/**
2410 * uart_add_one_port - attach a driver-defined port structure
2411 * @drv: pointer to the uart low level driver structure for this port
2412 * @port: uart port structure to use for this port.
2413 *
2414 * This allows the driver to register its own uart_port structure
2415 * with the core driver. The main purpose is to allow the low
2416 * level uart drivers to expand uart_port, rather than having yet
2417 * more levels of structures.
2418 */
2419int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2420{
2421 struct uart_state *state;
2422 int ret = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002423 struct device *tty_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 BUG_ON(in_interrupt());
2426
2427 if (port->line >= drv->nr)
2428 return -EINVAL;
2429
2430 state = drv->state + port->line;
2431
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002432 mutex_lock(&port_mutex);
Russell King68ac64c2006-04-30 11:13:50 +01002433 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 if (state->port) {
2435 ret = -EINVAL;
2436 goto out;
2437 }
2438
2439 state->port = port;
Russell King97d97222007-09-01 21:25:09 +01002440 state->pm_state = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 port->cons = drv->cons;
Alan Coxf7519282009-01-02 13:49:21 +00002443 port->info = &state->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Russell King976ecd12005-07-03 21:05:45 +01002445 /*
2446 * If this port is a console, then the spinlock is already
2447 * initialised.
2448 */
Ingo Molnar13e83592006-07-03 00:25:03 -07002449 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
Russell King976ecd12005-07-03 21:05:45 +01002450 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07002451 lockdep_set_class(&port->lock, &port_lock_key);
2452 }
Russell King976ecd12005-07-03 21:05:45 +01002453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 uart_configure_port(drv, state, port);
2455
2456 /*
2457 * Register the port whether it's detected or not. This allows
2458 * setserial to be used to alter this ports parameters.
2459 */
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002460 tty_dev = tty_register_device(drv->tty_driver, port->line, port->dev);
2461 if (likely(!IS_ERR(tty_dev))) {
Alan Stern74081f82008-03-19 22:35:13 +01002462 device_init_wakeup(tty_dev, 1);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002463 device_set_wakeup_enable(tty_dev, 0);
2464 } else
2465 printk(KERN_ERR "Cannot register tty device on line %d\n",
2466 port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 /*
Russell King68ac64c2006-04-30 11:13:50 +01002469 * Ensure UPF_DEAD is not set.
2470 */
2471 port->flags &= ~UPF_DEAD;
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 out:
Russell King68ac64c2006-04-30 11:13:50 +01002474 mutex_unlock(&state->mutex);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002475 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 return ret;
2478}
2479
2480/**
2481 * uart_remove_one_port - detach a driver defined port structure
2482 * @drv: pointer to the uart low level driver structure for this port
2483 * @port: uart port structure for this port
2484 *
2485 * This unhooks (and hangs up) the specified port structure from the
2486 * core driver. No further calls will be made to the low-level code
2487 * for this port.
2488 */
2489int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2490{
2491 struct uart_state *state = drv->state + port->line;
Russell King68ac64c2006-04-30 11:13:50 +01002492 struct uart_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 BUG_ON(in_interrupt());
2495
2496 if (state->port != port)
2497 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2498 state->port, port);
2499
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002500 mutex_lock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
2502 /*
Russell King68ac64c2006-04-30 11:13:50 +01002503 * Mark the port "dead" - this prevents any opens from
2504 * succeeding while we shut down the port.
2505 */
2506 mutex_lock(&state->mutex);
2507 port->flags |= UPF_DEAD;
2508 mutex_unlock(&state->mutex);
2509
2510 /*
Greg Kroah-Hartmanaa4148c2005-06-20 21:15:16 -07002511 * Remove the devices from the tty layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 */
2513 tty_unregister_device(drv->tty_driver, port->line);
2514
Alan Coxf7519282009-01-02 13:49:21 +00002515 info = &state->info;
Alan Coxdf4f4dd2008-07-16 21:53:50 +01002516 if (info && info->port.tty)
2517 tty_vhangup(info->port.tty);
Russell King68ac64c2006-04-30 11:13:50 +01002518
2519 /*
Russell King68ac64c2006-04-30 11:13:50 +01002520 * Free the port IO and memory resources, if any.
2521 */
2522 if (port->type != PORT_UNKNOWN)
2523 port->ops->release_port(port);
2524
2525 /*
2526 * Indicate that there isn't a port here anymore.
2527 */
2528 port->type = PORT_UNKNOWN;
2529
2530 /*
2531 * Kill the tasklet, and free resources.
2532 */
Alexander Beregalovbc325622009-01-02 13:49:32 +00002533 if (info)
Russell King68ac64c2006-04-30 11:13:50 +01002534 tasklet_kill(&info->tlet);
Russell King68ac64c2006-04-30 11:13:50 +01002535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 state->port = NULL;
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002537 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 return 0;
2540}
2541
2542/*
2543 * Are the two ports equivalent?
2544 */
2545int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2546{
2547 if (port1->iotype != port2->iotype)
2548 return 0;
2549
2550 switch (port1->iotype) {
2551 case UPIO_PORT:
2552 return (port1->iobase == port2->iobase);
2553 case UPIO_HUB6:
2554 return (port1->iobase == port2->iobase) &&
2555 (port1->hub6 == port2->hub6);
2556 case UPIO_MEM:
Sergei Shtylyovd21b55d2006-08-28 19:49:03 +04002557 case UPIO_MEM32:
2558 case UPIO_AU:
2559 case UPIO_TSI:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002560 case UPIO_DWAPB:
Benjamin Herrenschmidt1624f002006-01-04 18:09:44 +00002561 return (port1->mapbase == port2->mapbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 }
2563 return 0;
2564}
2565EXPORT_SYMBOL(uart_match_port);
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567EXPORT_SYMBOL(uart_write_wakeup);
2568EXPORT_SYMBOL(uart_register_driver);
2569EXPORT_SYMBOL(uart_unregister_driver);
2570EXPORT_SYMBOL(uart_suspend_port);
2571EXPORT_SYMBOL(uart_resume_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572EXPORT_SYMBOL(uart_add_one_port);
2573EXPORT_SYMBOL(uart_remove_one_port);
2574
2575MODULE_DESCRIPTION("Serial driver core");
2576MODULE_LICENSE("GPL");