blob: 6bdf3362e3b1e5f743ae8bad1f0bc8c9f682bf08 [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 Coxdf4f4dd2008-07-16 21:53:50 +010053#define uart_users(state) ((state)->count + ((state)->info ? (state)->info->port.blocked_open : 0))
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
97 if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&
98 !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 Coxdf4f4dd2008-07-16 21:53:50 +0100116 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{
142 struct uart_info *info = state->info;
143 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{
215 struct uart_info *info = state->info;
216 struct uart_port *port = state->port;
217
Russell Kingee31b332005-11-13 15:28:51 +0000218 /*
219 * Set the TTY IO error marker
220 */
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100221 if (info->port.tty)
222 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Russell Kingee31b332005-11-13 15:28:51 +0000223
224 if (info->flags & UIF_INITIALIZED) {
225 info->flags &= ~UIF_INITIALIZED;
226
227 /*
228 * Turn off DTR and RTS early.
229 */
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100230 if (!info->port.tty || (info->port.tty->termios->c_cflag & HUPCL))
Russell Kingee31b332005-11-13 15:28:51 +0000231 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
232
233 /*
234 * clear delta_msr_wait queue to avoid mem leaks: we may free
235 * the irq here so the queue might never be woken up. Note
236 * that we won't end up waiting on delta_msr_wait again since
237 * any outstanding file descriptors should be pointing at
238 * hung_up_tty_fops now.
239 */
240 wake_up_interruptible(&info->delta_msr_wait);
241
242 /*
243 * Free the IRQ and disable the port.
244 */
245 port->ops->shutdown(port);
246
247 /*
248 * Ensure that the IRQ handler isn't running on another CPU.
249 */
250 synchronize_irq(port->irq);
251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /*
Russell Kingee31b332005-11-13 15:28:51 +0000254 * kill off our tasklet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
Russell Kingee31b332005-11-13 15:28:51 +0000256 tasklet_kill(&info->tlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /*
259 * Free the transmit buffer page.
260 */
261 if (info->xmit.buf) {
262 free_page((unsigned long)info->xmit.buf);
263 info->xmit.buf = NULL;
264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/**
268 * uart_update_timeout - update per-port FIFO timeout.
269 * @port: uart_port structure describing the port
270 * @cflag: termios cflag value
271 * @baud: speed of the port
272 *
273 * Set the port FIFO timeout value. The @cflag value should
274 * reflect the actual hardware settings.
275 */
276void
277uart_update_timeout(struct uart_port *port, unsigned int cflag,
278 unsigned int baud)
279{
280 unsigned int bits;
281
282 /* byte size and parity */
283 switch (cflag & CSIZE) {
284 case CS5:
285 bits = 7;
286 break;
287 case CS6:
288 bits = 8;
289 break;
290 case CS7:
291 bits = 9;
292 break;
293 default:
294 bits = 10;
Alan Coxa46c9992008-02-08 04:18:53 -0800295 break; /* CS8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297
298 if (cflag & CSTOPB)
299 bits++;
300 if (cflag & PARENB)
301 bits++;
302
303 /*
304 * The total number of bits to be transmitted in the fifo.
305 */
306 bits = bits * port->fifosize;
307
308 /*
309 * Figure the timeout to send the above number of bits.
310 * Add .02 seconds of slop
311 */
312 port->timeout = (HZ * bits) / baud + HZ/50;
313}
314
315EXPORT_SYMBOL(uart_update_timeout);
316
317/**
318 * uart_get_baud_rate - return baud rate for a particular port
319 * @port: uart_port structure describing the port in question.
320 * @termios: desired termios settings.
321 * @old: old termios (or NULL)
322 * @min: minimum acceptable baud rate
323 * @max: maximum acceptable baud rate
324 *
325 * Decode the termios structure into a numeric baud rate,
326 * taking account of the magic 38400 baud rate (with spd_*
327 * flags), and mapping the %B0 rate to 9600 baud.
328 *
329 * If the new baud rate is invalid, try the old termios setting.
330 * If it's still invalid, we try 9600 baud.
331 *
332 * Update the @termios structure to reflect the baud rate
Alan Coxeb424fd2008-04-28 02:14:07 -0700333 * we're actually going to be using. Don't do this for the case
334 * where B0 is requested ("hang up").
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
336unsigned int
Alan Cox606d0992006-12-08 02:38:45 -0800337uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
338 struct ktermios *old, unsigned int min, unsigned int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 unsigned int try, baud, altbaud = 38400;
Alan Coxeb424fd2008-04-28 02:14:07 -0700341 int hung_up = 0;
Russell King0077d452006-01-21 23:03:28 +0000342 upf_t flags = port->flags & UPF_SPD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (flags == UPF_SPD_HI)
345 altbaud = 57600;
346 if (flags == UPF_SPD_VHI)
347 altbaud = 115200;
348 if (flags == UPF_SPD_SHI)
349 altbaud = 230400;
350 if (flags == UPF_SPD_WARP)
351 altbaud = 460800;
352
353 for (try = 0; try < 2; try++) {
354 baud = tty_termios_baud_rate(termios);
355
356 /*
357 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
358 * Die! Die! Die!
359 */
360 if (baud == 38400)
361 baud = altbaud;
362
363 /*
364 * Special case: B0 rate.
365 */
Alan Coxeb424fd2008-04-28 02:14:07 -0700366 if (baud == 0) {
367 hung_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 baud = 9600;
Alan Coxeb424fd2008-04-28 02:14:07 -0700369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 if (baud >= min && baud <= max)
372 return baud;
373
374 /*
375 * Oops, the quotient was zero. Try again with
376 * the old baud rate if possible.
377 */
378 termios->c_cflag &= ~CBAUD;
379 if (old) {
Alan Cox6d4d67b2008-02-04 22:27:53 -0800380 baud = tty_termios_baud_rate(old);
Alan Coxeb424fd2008-04-28 02:14:07 -0700381 if (!hung_up)
382 tty_termios_encode_baud_rate(termios,
383 baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 old = NULL;
385 continue;
386 }
387
388 /*
389 * As a last resort, if the quotient is zero,
390 * default to 9600 bps
391 */
Alan Coxeb424fd2008-04-28 02:14:07 -0700392 if (!hung_up)
393 tty_termios_encode_baud_rate(termios, 9600, 9600);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
396 return 0;
397}
398
399EXPORT_SYMBOL(uart_get_baud_rate);
400
401/**
402 * uart_get_divisor - return uart clock divisor
403 * @port: uart_port structure describing the port.
404 * @baud: desired baud rate
405 *
406 * Calculate the uart clock divisor for the port.
407 */
408unsigned int
409uart_get_divisor(struct uart_port *port, unsigned int baud)
410{
411 unsigned int quot;
412
413 /*
414 * Old custom speed handling.
415 */
416 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
417 quot = port->custom_divisor;
418 else
419 quot = (port->uartclk + (8 * baud)) / (16 * baud);
420
421 return quot;
422}
423
424EXPORT_SYMBOL(uart_get_divisor);
425
Alan Cox23d22ce2008-04-30 00:54:11 -0700426/* FIXME: Consistent locking policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427static void
Alan Cox606d0992006-12-08 02:38:45 -0800428uart_change_speed(struct uart_state *state, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100430 struct tty_struct *tty = state->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct uart_port *port = state->port;
Alan Cox606d0992006-12-08 02:38:45 -0800432 struct ktermios *termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /*
435 * If we have no tty, termios, or the port does not exist,
436 * then we can't set the parameters for this port.
437 */
438 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
439 return;
440
441 termios = tty->termios;
442
443 /*
444 * Set flags based on termios cflag
445 */
446 if (termios->c_cflag & CRTSCTS)
447 state->info->flags |= UIF_CTS_FLOW;
448 else
449 state->info->flags &= ~UIF_CTS_FLOW;
450
451 if (termios->c_cflag & CLOCAL)
452 state->info->flags &= ~UIF_CHECK_CD;
453 else
454 state->info->flags |= UIF_CHECK_CD;
455
456 port->ops->set_termios(port, termios, old_termios);
457}
458
Alan Cox23d22ce2008-04-30 00:54:11 -0700459static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460__uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
461{
462 unsigned long flags;
Alan Cox23d22ce2008-04-30 00:54:11 -0700463 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 if (!circ->buf)
Alan Cox23d22ce2008-04-30 00:54:11 -0700466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 spin_lock_irqsave(&port->lock, flags);
469 if (uart_circ_chars_free(circ) != 0) {
470 circ->buf[circ->head] = c;
471 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
Alan Cox23d22ce2008-04-30 00:54:11 -0700472 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474 spin_unlock_irqrestore(&port->lock, flags);
Alan Cox23d22ce2008-04-30 00:54:11 -0700475 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Alan Cox23d22ce2008-04-30 00:54:11 -0700478static int uart_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 struct uart_state *state = tty->driver_data;
481
Alan Cox23d22ce2008-04-30 00:54:11 -0700482 return __uart_put_char(state->port, &state->info->xmit, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485static void uart_flush_chars(struct tty_struct *tty)
486{
487 uart_start(tty);
488}
489
490static int
Pavel Machekd5f735e2006-03-07 21:55:20 -0800491uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct uart_state *state = tty->driver_data;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800494 struct uart_port *port;
495 struct circ_buf *circ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 unsigned long flags;
497 int c, ret = 0;
498
Pavel Machekd5f735e2006-03-07 21:55:20 -0800499 /*
500 * This means you called this function _after_ the port was
501 * closed. No cookie for you.
502 */
503 if (!state || !state->info) {
504 WARN_ON(1);
505 return -EL3HLT;
506 }
507
508 port = state->port;
509 circ = &state->info->xmit;
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!circ->buf)
512 return 0;
513
514 spin_lock_irqsave(&port->lock, flags);
515 while (1) {
516 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
517 if (count < c)
518 c = count;
519 if (c <= 0)
520 break;
521 memcpy(circ->buf + circ->head, buf, c);
522 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
523 buf += c;
524 count -= c;
525 ret += c;
526 }
527 spin_unlock_irqrestore(&port->lock, flags);
528
529 uart_start(tty);
530 return ret;
531}
532
533static int uart_write_room(struct tty_struct *tty)
534{
535 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700536 unsigned long flags;
537 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Alan Coxf34d7a52008-04-30 00:54:13 -0700539 spin_lock_irqsave(&state->port->lock, flags);
540 ret = uart_circ_chars_free(&state->info->xmit);
541 spin_unlock_irqrestore(&state->port->lock, flags);
542 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545static int uart_chars_in_buffer(struct tty_struct *tty)
546{
547 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700548 unsigned long flags;
549 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Alan Coxf34d7a52008-04-30 00:54:13 -0700551 spin_lock_irqsave(&state->port->lock, flags);
552 ret = uart_circ_chars_pending(&state->info->xmit);
553 spin_unlock_irqrestore(&state->port->lock, flags);
554 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557static void uart_flush_buffer(struct tty_struct *tty)
558{
559 struct uart_state *state = tty->driver_data;
Tetsuo Handa55d7b682008-05-06 20:42:27 -0700560 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 unsigned long flags;
562
Pavel Machekd5f735e2006-03-07 21:55:20 -0800563 /*
564 * This means you called this function _after_ the port was
565 * closed. No cookie for you.
566 */
567 if (!state || !state->info) {
568 WARN_ON(1);
569 return;
570 }
571
Tetsuo Handa55d7b682008-05-06 20:42:27 -0700572 port = state->port;
Jiri Slabyeb3a1e12007-05-06 14:48:52 -0700573 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 spin_lock_irqsave(&port->lock, flags);
576 uart_circ_clear(&state->info->xmit);
Haavard Skinnemoen6bb0e3a2008-07-16 21:52:36 +0100577 if (port->ops->flush_buffer)
578 port->ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 spin_unlock_irqrestore(&port->lock, flags);
580 tty_wakeup(tty);
581}
582
583/*
584 * This function is used to send a high-priority XON/XOFF character to
585 * the device
586 */
587static void uart_send_xchar(struct tty_struct *tty, char ch)
588{
589 struct uart_state *state = tty->driver_data;
590 struct uart_port *port = state->port;
591 unsigned long flags;
592
593 if (port->ops->send_xchar)
594 port->ops->send_xchar(port, ch);
595 else {
596 port->x_char = ch;
597 if (ch) {
598 spin_lock_irqsave(&port->lock, flags);
Russell Kingb129a8c2005-08-31 10:12:14 +0100599 port->ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 spin_unlock_irqrestore(&port->lock, flags);
601 }
602 }
603}
604
605static void uart_throttle(struct tty_struct *tty)
606{
607 struct uart_state *state = tty->driver_data;
608
609 if (I_IXOFF(tty))
610 uart_send_xchar(tty, STOP_CHAR(tty));
611
612 if (tty->termios->c_cflag & CRTSCTS)
613 uart_clear_mctrl(state->port, TIOCM_RTS);
614}
615
616static void uart_unthrottle(struct tty_struct *tty)
617{
618 struct uart_state *state = tty->driver_data;
619 struct uart_port *port = state->port;
620
621 if (I_IXOFF(tty)) {
622 if (port->x_char)
623 port->x_char = 0;
624 else
625 uart_send_xchar(tty, START_CHAR(tty));
626 }
627
628 if (tty->termios->c_cflag & CRTSCTS)
629 uart_set_mctrl(port, TIOCM_RTS);
630}
631
632static int uart_get_info(struct uart_state *state,
633 struct serial_struct __user *retinfo)
634{
635 struct uart_port *port = state->port;
636 struct serial_struct tmp;
637
638 memset(&tmp, 0, sizeof(tmp));
Alan Coxf34d7a52008-04-30 00:54:13 -0700639
640 /* Ensure the state we copy is consistent and no hardware changes
641 occur as we go */
642 mutex_lock(&state->mutex);
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 tmp.type = port->type;
645 tmp.line = port->line;
646 tmp.port = port->iobase;
647 if (HIGH_BITS_OFFSET)
648 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
649 tmp.irq = port->irq;
650 tmp.flags = port->flags;
651 tmp.xmit_fifo_size = port->fifosize;
652 tmp.baud_base = port->uartclk / 16;
653 tmp.close_delay = state->close_delay / 10;
654 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
655 ASYNC_CLOSING_WAIT_NONE :
Alan Coxa46c9992008-02-08 04:18:53 -0800656 state->closing_wait / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 tmp.custom_divisor = port->custom_divisor;
658 tmp.hub6 = port->hub6;
659 tmp.io_type = port->iotype;
660 tmp.iomem_reg_shift = port->regshift;
Josh Boyer4f640ef2007-07-23 18:43:44 -0700661 tmp.iomem_base = (void *)(unsigned long)port->mapbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Alan Coxf34d7a52008-04-30 00:54:13 -0700663 mutex_unlock(&state->mutex);
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
666 return -EFAULT;
667 return 0;
668}
669
670static int uart_set_info(struct uart_state *state,
671 struct serial_struct __user *newinfo)
672{
673 struct serial_struct new_serial;
674 struct uart_port *port = state->port;
675 unsigned long new_port;
Russell King0077d452006-01-21 23:03:28 +0000676 unsigned int change_irq, change_port, closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 unsigned int old_custom_divisor, close_delay;
Russell King0077d452006-01-21 23:03:28 +0000678 upf_t old_flags, new_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 int retval = 0;
680
681 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
682 return -EFAULT;
683
684 new_port = new_serial.port;
685 if (HIGH_BITS_OFFSET)
686 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
687
688 new_serial.irq = irq_canonicalize(new_serial.irq);
689 close_delay = new_serial.close_delay * 10;
690 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
691 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
692
693 /*
694 * This semaphore protects state->count. It is also
695 * very useful to prevent opens. Also, take the
696 * port configuration semaphore to make sure that a
697 * module insertion/removal doesn't change anything
698 * under us.
699 */
Ingo Molnare2862f62006-01-13 21:37:07 +0000700 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
David Gibsonabb4a232007-05-06 14:48:49 -0700702 change_irq = !(port->flags & UPF_FIXED_PORT)
703 && new_serial.irq != port->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 /*
706 * Since changing the 'type' of the port changes its resource
707 * allocations, we should treat type changes the same as
708 * IO port changes.
709 */
David Gibsonabb4a232007-05-06 14:48:49 -0700710 change_port = !(port->flags & UPF_FIXED_PORT)
711 && (new_port != port->iobase ||
712 (unsigned long)new_serial.iomem_base != port->mapbase ||
713 new_serial.hub6 != port->hub6 ||
714 new_serial.io_type != port->iotype ||
715 new_serial.iomem_reg_shift != port->regshift ||
716 new_serial.type != port->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 old_flags = port->flags;
Russell King0077d452006-01-21 23:03:28 +0000719 new_flags = new_serial.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 old_custom_divisor = port->custom_divisor;
721
722 if (!capable(CAP_SYS_ADMIN)) {
723 retval = -EPERM;
724 if (change_irq || change_port ||
725 (new_serial.baud_base != port->uartclk / 16) ||
726 (close_delay != state->close_delay) ||
727 (closing_wait != state->closing_wait) ||
Russell King947deee2006-07-02 20:45:51 +0100728 (new_serial.xmit_fifo_size &&
729 new_serial.xmit_fifo_size != port->fifosize) ||
Russell King0077d452006-01-21 23:03:28 +0000730 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 goto exit;
732 port->flags = ((port->flags & ~UPF_USR_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000733 (new_flags & UPF_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 port->custom_divisor = new_serial.custom_divisor;
735 goto check_and_exit;
736 }
737
738 /*
739 * Ask the low level driver to verify the settings.
740 */
741 if (port->ops->verify_port)
742 retval = port->ops->verify_port(port, &new_serial);
743
744 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
745 (new_serial.baud_base < 9600))
746 retval = -EINVAL;
747
748 if (retval)
749 goto exit;
750
751 if (change_port || change_irq) {
752 retval = -EBUSY;
753
754 /*
755 * Make sure that we are the sole user of this port.
756 */
757 if (uart_users(state) > 1)
758 goto exit;
759
760 /*
761 * We need to shutdown the serial port at the old
762 * port/type/irq combination.
763 */
764 uart_shutdown(state);
765 }
766
767 if (change_port) {
768 unsigned long old_iobase, old_mapbase;
769 unsigned int old_type, old_iotype, old_hub6, old_shift;
770
771 old_iobase = port->iobase;
772 old_mapbase = port->mapbase;
773 old_type = port->type;
774 old_hub6 = port->hub6;
775 old_iotype = port->iotype;
776 old_shift = port->regshift;
777
778 /*
779 * Free and release old regions
780 */
781 if (old_type != PORT_UNKNOWN)
782 port->ops->release_port(port);
783
784 port->iobase = new_port;
785 port->type = new_serial.type;
786 port->hub6 = new_serial.hub6;
787 port->iotype = new_serial.io_type;
788 port->regshift = new_serial.iomem_reg_shift;
789 port->mapbase = (unsigned long)new_serial.iomem_base;
790
791 /*
792 * Claim and map the new regions
793 */
794 if (port->type != PORT_UNKNOWN) {
795 retval = port->ops->request_port(port);
796 } else {
797 /* Always success - Jean II */
798 retval = 0;
799 }
800
801 /*
802 * If we fail to request resources for the
803 * new port, try to restore the old settings.
804 */
805 if (retval && old_type != PORT_UNKNOWN) {
806 port->iobase = old_iobase;
807 port->type = old_type;
808 port->hub6 = old_hub6;
809 port->iotype = old_iotype;
810 port->regshift = old_shift;
811 port->mapbase = old_mapbase;
812 retval = port->ops->request_port(port);
813 /*
814 * If we failed to restore the old settings,
815 * we fail like this.
816 */
817 if (retval)
818 port->type = PORT_UNKNOWN;
819
820 /*
821 * We failed anyway.
822 */
823 retval = -EBUSY;
Alan Coxa46c9992008-02-08 04:18:53 -0800824 /* Added to return the correct error -Ram Gupta */
825 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827 }
828
David Gibsonabb4a232007-05-06 14:48:49 -0700829 if (change_irq)
830 port->irq = new_serial.irq;
831 if (!(port->flags & UPF_FIXED_PORT))
832 port->uartclk = new_serial.baud_base * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000834 (new_flags & UPF_CHANGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 port->custom_divisor = new_serial.custom_divisor;
836 state->close_delay = close_delay;
837 state->closing_wait = closing_wait;
Russell King947deee2006-07-02 20:45:51 +0100838 if (new_serial.xmit_fifo_size)
839 port->fifosize = new_serial.xmit_fifo_size;
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100840 if (state->info->port.tty)
841 state->info->port.tty->low_latency =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
843
844 check_and_exit:
845 retval = 0;
846 if (port->type == PORT_UNKNOWN)
847 goto exit;
848 if (state->info->flags & UIF_INITIALIZED) {
849 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
850 old_custom_divisor != port->custom_divisor) {
851 /*
852 * If they're setting up a custom divisor or speed,
853 * instead of clearing it, then bitch about it. No
854 * need to rate-limit; it's CAP_SYS_ADMIN only.
855 */
856 if (port->flags & UPF_SPD_MASK) {
857 char buf[64];
858 printk(KERN_NOTICE
859 "%s sets custom speed on %s. This "
860 "is deprecated.\n", current->comm,
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100861 tty_name(state->info->port.tty, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863 uart_change_speed(state, NULL);
864 }
865 } else
866 retval = uart_startup(state, 1);
867 exit:
Ingo Molnare2862f62006-01-13 21:37:07 +0000868 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return retval;
870}
871
872
873/*
874 * uart_get_lsr_info - get line status register info.
875 * Note: uart_ioctl protects us against hangups.
876 */
877static int uart_get_lsr_info(struct uart_state *state,
878 unsigned int __user *value)
879{
880 struct uart_port *port = state->port;
881 unsigned int result;
882
883 result = port->ops->tx_empty(port);
884
885 /*
886 * If we're about to load something into the transmit
887 * register, we'll pretend the transmitter isn't empty to
888 * avoid a race condition (depending on when the transmit
889 * interrupt happens).
890 */
891 if (port->x_char ||
892 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100893 !state->info->port.tty->stopped && !state->info->port.tty->hw_stopped))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 result &= ~TIOCSER_TEMT;
Alan Coxa46c9992008-02-08 04:18:53 -0800895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return put_user(result, value);
897}
898
899static int uart_tiocmget(struct tty_struct *tty, struct file *file)
900{
901 struct uart_state *state = tty->driver_data;
902 struct uart_port *port = state->port;
903 int result = -EIO;
904
Ingo Molnare2862f62006-01-13 21:37:07 +0000905 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if ((!file || !tty_hung_up_p(file)) &&
907 !(tty->flags & (1 << TTY_IO_ERROR))) {
908 result = port->mctrl;
Russell Kingc5f46442005-06-29 09:42:38 +0100909
910 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 result |= port->ops->get_mctrl(port);
Russell Kingc5f46442005-06-29 09:42:38 +0100912 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000914 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 return result;
917}
918
919static int
920uart_tiocmset(struct tty_struct *tty, struct file *file,
921 unsigned int set, unsigned int clear)
922{
923 struct uart_state *state = tty->driver_data;
924 struct uart_port *port = state->port;
925 int ret = -EIO;
926
Ingo Molnare2862f62006-01-13 21:37:07 +0000927 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if ((!file || !tty_hung_up_p(file)) &&
929 !(tty->flags & (1 << TTY_IO_ERROR))) {
930 uart_update_mctrl(port, set, clear);
931 ret = 0;
932 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000933 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return ret;
935}
936
Alan Cox9e989662008-07-22 11:18:03 +0100937static int uart_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 struct uart_state *state = tty->driver_data;
940 struct uart_port *port = state->port;
941
Ingo Molnare2862f62006-01-13 21:37:07 +0000942 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 if (port->type != PORT_UNKNOWN)
945 port->ops->break_ctl(port, break_state);
946
Ingo Molnare2862f62006-01-13 21:37:07 +0000947 mutex_unlock(&state->mutex);
Alan Cox9e989662008-07-22 11:18:03 +0100948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
951static int uart_do_autoconfig(struct uart_state *state)
952{
953 struct uart_port *port = state->port;
954 int flags, ret;
955
956 if (!capable(CAP_SYS_ADMIN))
957 return -EPERM;
958
959 /*
960 * Take the per-port semaphore. This prevents count from
961 * changing, and hence any extra opens of the port while
962 * we're auto-configuring.
963 */
Ingo Molnare2862f62006-01-13 21:37:07 +0000964 if (mutex_lock_interruptible(&state->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return -ERESTARTSYS;
966
967 ret = -EBUSY;
968 if (uart_users(state) == 1) {
969 uart_shutdown(state);
970
971 /*
972 * If we already have a port type configured,
973 * we must release its resources.
974 */
975 if (port->type != PORT_UNKNOWN)
976 port->ops->release_port(port);
977
978 flags = UART_CONFIG_TYPE;
979 if (port->flags & UPF_AUTO_IRQ)
980 flags |= UART_CONFIG_IRQ;
981
982 /*
983 * This will claim the ports resources if
984 * a port is found.
985 */
986 port->ops->config_port(port, flags);
987
988 ret = uart_startup(state, 1);
989 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000990 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return ret;
992}
993
994/*
995 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
996 * - mask passed in arg for lines of interest
997 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
998 * Caller should use TIOCGICOUNT to see which one it was
999 */
1000static int
1001uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1002{
1003 struct uart_port *port = state->port;
1004 DECLARE_WAITQUEUE(wait, current);
1005 struct uart_icount cprev, cnow;
1006 int ret;
1007
1008 /*
1009 * note the counters on entry
1010 */
1011 spin_lock_irq(&port->lock);
1012 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
1013
1014 /*
1015 * Force modem status interrupts on
1016 */
1017 port->ops->enable_ms(port);
1018 spin_unlock_irq(&port->lock);
1019
1020 add_wait_queue(&state->info->delta_msr_wait, &wait);
1021 for (;;) {
1022 spin_lock_irq(&port->lock);
1023 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1024 spin_unlock_irq(&port->lock);
1025
1026 set_current_state(TASK_INTERRUPTIBLE);
1027
1028 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1029 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1030 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1031 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Alan Coxa46c9992008-02-08 04:18:53 -08001032 ret = 0;
1033 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035
1036 schedule();
1037
1038 /* see if a signal did it */
1039 if (signal_pending(current)) {
1040 ret = -ERESTARTSYS;
1041 break;
1042 }
1043
1044 cprev = cnow;
1045 }
1046
1047 current->state = TASK_RUNNING;
1048 remove_wait_queue(&state->info->delta_msr_wait, &wait);
1049
1050 return ret;
1051}
1052
1053/*
1054 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1055 * Return: write counters to the user passed counter struct
1056 * NB: both 1->0 and 0->1 transitions are counted except for
1057 * RI where only 0->1 is counted.
1058 */
1059static int uart_get_count(struct uart_state *state,
1060 struct serial_icounter_struct __user *icnt)
1061{
1062 struct serial_icounter_struct icount;
1063 struct uart_icount cnow;
1064 struct uart_port *port = state->port;
1065
1066 spin_lock_irq(&port->lock);
1067 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1068 spin_unlock_irq(&port->lock);
1069
1070 icount.cts = cnow.cts;
1071 icount.dsr = cnow.dsr;
1072 icount.rng = cnow.rng;
1073 icount.dcd = cnow.dcd;
1074 icount.rx = cnow.rx;
1075 icount.tx = cnow.tx;
1076 icount.frame = cnow.frame;
1077 icount.overrun = cnow.overrun;
1078 icount.parity = cnow.parity;
1079 icount.brk = cnow.brk;
1080 icount.buf_overrun = cnow.buf_overrun;
1081
1082 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1083}
1084
1085/*
Alan Coxe5238442008-04-30 00:53:28 -07001086 * Called via sys_ioctl. We can use spin_lock_irq() here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 */
1088static int
1089uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1090 unsigned long arg)
1091{
1092 struct uart_state *state = tty->driver_data;
1093 void __user *uarg = (void __user *)arg;
1094 int ret = -ENOIOCTLCMD;
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 /*
1098 * These ioctls don't rely on the hardware to be present.
1099 */
1100 switch (cmd) {
1101 case TIOCGSERIAL:
1102 ret = uart_get_info(state, uarg);
1103 break;
1104
1105 case TIOCSSERIAL:
1106 ret = uart_set_info(state, uarg);
1107 break;
1108
1109 case TIOCSERCONFIG:
1110 ret = uart_do_autoconfig(state);
1111 break;
1112
1113 case TIOCSERGWILD: /* obsolete */
1114 case TIOCSERSWILD: /* obsolete */
1115 ret = 0;
1116 break;
1117 }
1118
1119 if (ret != -ENOIOCTLCMD)
1120 goto out;
1121
1122 if (tty->flags & (1 << TTY_IO_ERROR)) {
1123 ret = -EIO;
1124 goto out;
1125 }
1126
1127 /*
1128 * The following should only be used when hardware is present.
1129 */
1130 switch (cmd) {
1131 case TIOCMIWAIT:
1132 ret = uart_wait_modem_status(state, arg);
1133 break;
1134
1135 case TIOCGICOUNT:
1136 ret = uart_get_count(state, uarg);
1137 break;
1138 }
1139
1140 if (ret != -ENOIOCTLCMD)
1141 goto out;
1142
Ingo Molnare2862f62006-01-13 21:37:07 +00001143 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 if (tty_hung_up_p(filp)) {
1146 ret = -EIO;
1147 goto out_up;
1148 }
1149
1150 /*
1151 * All these rely on hardware being present and need to be
1152 * protected against the tty being hung up.
1153 */
1154 switch (cmd) {
1155 case TIOCSERGETLSR: /* Get line status register */
1156 ret = uart_get_lsr_info(state, uarg);
1157 break;
1158
1159 default: {
1160 struct uart_port *port = state->port;
1161 if (port->ops->ioctl)
1162 ret = port->ops->ioctl(port, cmd, arg);
1163 break;
1164 }
1165 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001166out_up:
Ingo Molnare2862f62006-01-13 21:37:07 +00001167 mutex_unlock(&state->mutex);
Alan Coxf34d7a52008-04-30 00:54:13 -07001168out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return ret;
1170}
1171
Linus Torvaldsedeb2802008-06-04 10:35:03 -07001172static void uart_set_ldisc(struct tty_struct *tty)
Alan Cox64e91592008-06-03 15:18:54 +01001173{
1174 struct uart_state *state = tty->driver_data;
1175 struct uart_port *port = state->port;
1176
1177 if (port->ops->set_ldisc)
1178 port->ops->set_ldisc(port);
1179}
1180
Alan Coxa46c9992008-02-08 04:18:53 -08001181static void uart_set_termios(struct tty_struct *tty,
1182 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 struct uart_state *state = tty->driver_data;
1185 unsigned long flags;
1186 unsigned int cflag = tty->termios->c_cflag;
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 /*
1190 * These are the bits that are used to setup various
David Woodhouse20620d62007-08-22 14:01:11 -07001191 * flags in the low level driver. We can ignore the Bfoo
1192 * bits in c_cflag; c_[io]speed will always be set
1193 * appropriately by set_termios() in tty_ioctl.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 */
1195#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if ((cflag ^ old_termios->c_cflag) == 0 &&
David Woodhouse20620d62007-08-22 14:01:11 -07001197 tty->termios->c_ospeed == old_termios->c_ospeed &&
1198 tty->termios->c_ispeed == old_termios->c_ispeed &&
Alan Coxe5238442008-04-30 00:53:28 -07001199 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return;
Alan Coxe5238442008-04-30 00:53:28 -07001201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 uart_change_speed(state, old_termios);
1204
1205 /* Handle transition to B0 status */
1206 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1207 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1208
1209 /* Handle transition away from B0 status */
1210 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1211 unsigned int mask = TIOCM_DTR;
1212 if (!(cflag & CRTSCTS) ||
1213 !test_bit(TTY_THROTTLED, &tty->flags))
1214 mask |= TIOCM_RTS;
1215 uart_set_mctrl(state->port, mask);
1216 }
1217
1218 /* Handle turning off CRTSCTS */
1219 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1220 spin_lock_irqsave(&state->port->lock, flags);
1221 tty->hw_stopped = 0;
1222 __uart_start(tty);
1223 spin_unlock_irqrestore(&state->port->lock, flags);
1224 }
1225
Russell King0dd7a1a2005-06-29 18:40:53 +01001226 /* Handle turning on CRTSCTS */
1227 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1228 spin_lock_irqsave(&state->port->lock, flags);
1229 if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
1230 tty->hw_stopped = 1;
Russell Kingb129a8c2005-08-31 10:12:14 +01001231 state->port->ops->stop_tx(state->port);
Russell King0dd7a1a2005-06-29 18:40:53 +01001232 }
1233 spin_unlock_irqrestore(&state->port->lock, flags);
1234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235#if 0
1236 /*
1237 * No need to wake up processes in open wait, since they
1238 * sample the CLOCAL flag once, and don't recheck it.
1239 * XXX It's not clear whether the current behavior is correct
1240 * or not. Hence, this may change.....
1241 */
1242 if (!(old_termios->c_cflag & CLOCAL) &&
1243 (tty->termios->c_cflag & CLOCAL))
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001244 wake_up_interruptible(&state->info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245#endif
1246}
1247
1248/*
1249 * In 2.4.5, calls to this will be serialized via the BKL in
1250 * linux/drivers/char/tty_io.c:tty_release()
1251 * linux/drivers/char/tty_io.c:do_tty_handup()
1252 */
1253static void uart_close(struct tty_struct *tty, struct file *filp)
1254{
1255 struct uart_state *state = tty->driver_data;
1256 struct uart_port *port;
Alan Coxa46c9992008-02-08 04:18:53 -08001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 BUG_ON(!kernel_locked());
1259
1260 if (!state || !state->port)
1261 return;
1262
1263 port = state->port;
1264
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001265 pr_debug("uart_close(%d) called\n", port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Ingo Molnare2862f62006-01-13 21:37:07 +00001267 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 if (tty_hung_up_p(filp))
1270 goto done;
1271
1272 if ((tty->count == 1) && (state->count != 1)) {
1273 /*
1274 * Uh, oh. tty->count is 1, which means that the tty
1275 * structure will be freed. state->count should always
1276 * be one in these conditions. If it's greater than
1277 * one, we've got real problems, since it means the
1278 * serial port won't be shutdown.
1279 */
1280 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1281 "state->count is %d\n", state->count);
1282 state->count = 1;
1283 }
1284 if (--state->count < 0) {
1285 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1286 tty->name, state->count);
1287 state->count = 0;
1288 }
1289 if (state->count)
1290 goto done;
1291
1292 /*
1293 * Now we wait for the transmit buffer to clear; and we notify
1294 * the line discipline to only process XON/XOFF characters by
1295 * setting tty->closing.
1296 */
1297 tty->closing = 1;
1298
1299 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1300 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1301
1302 /*
1303 * At this point, we stop accepting input. To do this, we
1304 * disable the receive line status interrupts.
1305 */
1306 if (state->info->flags & UIF_INITIALIZED) {
1307 unsigned long flags;
1308 spin_lock_irqsave(&port->lock, flags);
1309 port->ops->stop_rx(port);
1310 spin_unlock_irqrestore(&port->lock, flags);
1311 /*
1312 * Before we drop DTR, make sure the UART transmitter
1313 * has completely drained; this is especially
1314 * important if there is a transmit FIFO!
1315 */
1316 uart_wait_until_sent(tty, port->timeout);
1317 }
1318
1319 uart_shutdown(state);
1320 uart_flush_buffer(tty);
1321
Alan Coxa46c9992008-02-08 04:18:53 -08001322 tty_ldisc_flush(tty);
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 tty->closing = 0;
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001325 state->info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001327 if (state->info->port.blocked_open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (state->close_delay)
1329 msleep_interruptible(state->close_delay);
1330 } else if (!uart_console(port)) {
1331 uart_change_pm(state, 3);
1332 }
1333
1334 /*
1335 * Wake up anyone trying to open this port.
1336 */
1337 state->info->flags &= ~UIF_NORMAL_ACTIVE;
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001338 wake_up_interruptible(&state->info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 done:
Ingo Molnare2862f62006-01-13 21:37:07 +00001341 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
1344static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1345{
1346 struct uart_state *state = tty->driver_data;
1347 struct uart_port *port = state->port;
1348 unsigned long char_time, expire;
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1351 return;
1352
Alan Coxf34d7a52008-04-30 00:54:13 -07001353 lock_kernel();
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 /*
1356 * Set the check interval to be 1/5 of the estimated time to
1357 * send a single character, and make it at least 1. The check
1358 * interval should also be less than the timeout.
1359 *
1360 * Note: we have to use pretty tight timings here to satisfy
1361 * the NIST-PCTS.
1362 */
1363 char_time = (port->timeout - HZ/50) / port->fifosize;
1364 char_time = char_time / 5;
1365 if (char_time == 0)
1366 char_time = 1;
1367 if (timeout && timeout < char_time)
1368 char_time = timeout;
1369
1370 /*
1371 * If the transmitter hasn't cleared in twice the approximate
1372 * amount of time to send the entire FIFO, it probably won't
1373 * ever clear. This assumes the UART isn't doing flow
1374 * control, which is currently the case. Hence, if it ever
1375 * takes longer than port->timeout, this is probably due to a
1376 * UART bug of some kind. So, we clamp the timeout parameter at
1377 * 2*port->timeout.
1378 */
1379 if (timeout == 0 || timeout > 2 * port->timeout)
1380 timeout = 2 * port->timeout;
1381
1382 expire = jiffies + timeout;
1383
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001384 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
Alan Coxa46c9992008-02-08 04:18:53 -08001385 port->line, jiffies, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 /*
1388 * Check whether the transmitter is empty every 'char_time'.
1389 * 'timeout' / 'expire' give us the maximum amount of time
1390 * we wait.
1391 */
1392 while (!port->ops->tx_empty(port)) {
1393 msleep_interruptible(jiffies_to_msecs(char_time));
1394 if (signal_pending(current))
1395 break;
1396 if (time_after(jiffies, expire))
1397 break;
1398 }
1399 set_current_state(TASK_RUNNING); /* might not be needed */
Alan Coxf34d7a52008-04-30 00:54:13 -07001400 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
1403/*
1404 * This is called with the BKL held in
1405 * linux/drivers/char/tty_io.c:do_tty_hangup()
1406 * We're called from the eventd thread, so we can sleep for
1407 * a _short_ time only.
1408 */
1409static void uart_hangup(struct tty_struct *tty)
1410{
1411 struct uart_state *state = tty->driver_data;
1412
1413 BUG_ON(!kernel_locked());
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001414 pr_debug("uart_hangup(%d)\n", state->port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Ingo Molnare2862f62006-01-13 21:37:07 +00001416 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1418 uart_flush_buffer(tty);
1419 uart_shutdown(state);
1420 state->count = 0;
1421 state->info->flags &= ~UIF_NORMAL_ACTIVE;
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001422 state->info->port.tty = NULL;
1423 wake_up_interruptible(&state->info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 wake_up_interruptible(&state->info->delta_msr_wait);
1425 }
Ingo Molnare2862f62006-01-13 21:37:07 +00001426 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
1429/*
1430 * Copy across the serial console cflag setting into the termios settings
1431 * for the initial open of the port. This allows continuity between the
1432 * kernel settings, and the settings init adopts when it opens the port
1433 * for the first time.
1434 */
1435static void uart_update_termios(struct uart_state *state)
1436{
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001437 struct tty_struct *tty = state->info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 struct uart_port *port = state->port;
1439
1440 if (uart_console(port) && port->cons->cflag) {
1441 tty->termios->c_cflag = port->cons->cflag;
1442 port->cons->cflag = 0;
1443 }
1444
1445 /*
1446 * If the device failed to grab its irq resources,
1447 * or some other error occurred, don't try to talk
1448 * to the port hardware.
1449 */
1450 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1451 /*
1452 * Make termios settings take effect.
1453 */
1454 uart_change_speed(state, NULL);
1455
1456 /*
1457 * And finally enable the RTS and DTR signals.
1458 */
1459 if (tty->termios->c_cflag & CBAUD)
1460 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1461 }
1462}
1463
1464/*
1465 * Block the open until the port is ready. We must be called with
1466 * the per-port semaphore held.
1467 */
1468static int
1469uart_block_til_ready(struct file *filp, struct uart_state *state)
1470{
1471 DECLARE_WAITQUEUE(wait, current);
1472 struct uart_info *info = state->info;
1473 struct uart_port *port = state->port;
Russell Kingc5f46442005-06-29 09:42:38 +01001474 unsigned int mctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001476 info->port.blocked_open++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 state->count--;
1478
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001479 add_wait_queue(&info->port.open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 while (1) {
1481 set_current_state(TASK_INTERRUPTIBLE);
1482
1483 /*
1484 * If we have been hung up, tell userspace/restart open.
1485 */
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001486 if (tty_hung_up_p(filp) || info->port.tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 break;
1488
1489 /*
1490 * If the port has been closed, tell userspace/restart open.
1491 */
1492 if (!(info->flags & UIF_INITIALIZED))
1493 break;
1494
1495 /*
1496 * If non-blocking mode is set, or CLOCAL mode is set,
1497 * we don't want to wait for the modem status lines to
1498 * indicate that the port is ready.
1499 *
1500 * Also, if the port is not enabled/configured, we want
1501 * to allow the open to succeed here. Note that we will
1502 * have set TTY_IO_ERROR for a non-existant port.
1503 */
1504 if ((filp->f_flags & O_NONBLOCK) ||
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001505 (info->port.tty->termios->c_cflag & CLOCAL) ||
1506 (info->port.tty->flags & (1 << TTY_IO_ERROR)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 /*
1510 * Set DTR to allow modem to know we're waiting. Do
1511 * not set RTS here - we want to make sure we catch
1512 * the data from the modem.
1513 */
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001514 if (info->port.tty->termios->c_cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 uart_set_mctrl(port, TIOCM_DTR);
1516
1517 /*
1518 * and wait for the carrier to indicate that the
1519 * modem is ready for us.
1520 */
Russell Kingc5f46442005-06-29 09:42:38 +01001521 spin_lock_irq(&port->lock);
Russell Kingf61051c2006-01-07 23:11:23 +00001522 port->ops->enable_ms(port);
Russell Kingc5f46442005-06-29 09:42:38 +01001523 mctrl = port->ops->get_mctrl(port);
1524 spin_unlock_irq(&port->lock);
1525 if (mctrl & TIOCM_CAR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 break;
1527
Ingo Molnare2862f62006-01-13 21:37:07 +00001528 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 schedule();
Ingo Molnare2862f62006-01-13 21:37:07 +00001530 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 if (signal_pending(current))
1533 break;
1534 }
1535 set_current_state(TASK_RUNNING);
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001536 remove_wait_queue(&info->port.open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 state->count++;
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001539 info->port.blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 if (signal_pending(current))
1542 return -ERESTARTSYS;
1543
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001544 if (!info->port.tty || tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return -EAGAIN;
1546
1547 return 0;
1548}
1549
1550static struct uart_state *uart_get(struct uart_driver *drv, int line)
1551{
1552 struct uart_state *state;
Russell King68ac64c2006-04-30 11:13:50 +01001553 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 state = drv->state + line;
Ingo Molnare2862f62006-01-13 21:37:07 +00001556 if (mutex_lock_interruptible(&state->mutex)) {
Russell King68ac64c2006-04-30 11:13:50 +01001557 ret = -ERESTARTSYS;
1558 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
1560
1561 state->count++;
Russell King68ac64c2006-04-30 11:13:50 +01001562 if (!state->port || state->port->flags & UPF_DEAD) {
1563 ret = -ENXIO;
1564 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
1566
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001567 /* BKL: RACE HERE - LEAK */
1568 /* We should move this into the uart_state structure and kill off
1569 this whole complexity */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (!state->info) {
Burman Yan8f31bb32007-02-14 00:33:07 -08001571 state->info = kzalloc(sizeof(struct uart_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (state->info) {
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001573 init_waitqueue_head(&state->info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 init_waitqueue_head(&state->info->delta_msr_wait);
1575
1576 /*
1577 * Link the info into the other structures.
1578 */
1579 state->port->info = state->info;
1580
1581 tasklet_init(&state->info->tlet, uart_tasklet_action,
1582 (unsigned long)state);
1583 } else {
Russell King68ac64c2006-04-30 11:13:50 +01001584 ret = -ENOMEM;
1585 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return state;
Russell King68ac64c2006-04-30 11:13:50 +01001589
1590 err_unlock:
1591 state->count--;
1592 mutex_unlock(&state->mutex);
1593 err:
1594 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
1597/*
Denis Cheng922f9cf2008-02-08 04:22:00 -08001598 * calls to uart_open are serialised by the BKL in
1599 * fs/char_dev.c:chrdev_open()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 * Note that if this fails, then uart_close() _will_ be called.
1601 *
1602 * In time, we want to scrap the "opening nonpresent ports"
1603 * behaviour and implement an alternative way for setserial
1604 * to set base addresses/ports/types. This will allow us to
1605 * get rid of a certain amount of extra tests.
1606 */
1607static int uart_open(struct tty_struct *tty, struct file *filp)
1608{
1609 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1610 struct uart_state *state;
1611 int retval, line = tty->index;
1612
1613 BUG_ON(!kernel_locked());
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001614 pr_debug("uart_open(%d) called\n", line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 /*
1617 * tty->driver->num won't change, so we won't fail here with
1618 * tty->driver_data set to something non-NULL (and therefore
1619 * we won't get caught by uart_close()).
1620 */
1621 retval = -ENODEV;
1622 if (line >= tty->driver->num)
1623 goto fail;
1624
1625 /*
1626 * We take the semaphore inside uart_get to guarantee that we won't
1627 * be re-entered while allocating the info structure, or while we
1628 * request any IRQs that the driver may need. This also has the nice
1629 * side-effect that it delays the action of uart_hangup, so we can
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001630 * guarantee that info->port.tty will always contain something reasonable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 */
1632 state = uart_get(drv, line);
1633 if (IS_ERR(state)) {
1634 retval = PTR_ERR(state);
1635 goto fail;
1636 }
1637
1638 /*
1639 * Once we set tty->driver_data here, we are guaranteed that
1640 * uart_close() will decrement the driver module use count.
1641 * Any failures from here onwards should not touch the count.
1642 */
1643 tty->driver_data = state;
1644 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1645 tty->alt_speed = 0;
Alan Coxdf4f4dd2008-07-16 21:53:50 +01001646 state->info->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 /*
1649 * If the port is in the middle of closing, bail out now.
1650 */
1651 if (tty_hung_up_p(filp)) {
1652 retval = -EAGAIN;
1653 state->count--;
Ingo Molnare2862f62006-01-13 21:37:07 +00001654 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 goto fail;
1656 }
1657
1658 /*
1659 * Make sure the device is in D0 state.
1660 */
1661 if (state->count == 1)
1662 uart_change_pm(state, 0);
1663
1664 /*
1665 * Start up the serial port.
1666 */
1667 retval = uart_startup(state, 0);
1668
1669 /*
1670 * If we succeeded, wait until the port is ready.
1671 */
1672 if (retval == 0)
1673 retval = uart_block_til_ready(filp, state);
Ingo Molnare2862f62006-01-13 21:37:07 +00001674 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 /*
1677 * If this is the first open to succeed, adjust things to suit.
1678 */
1679 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1680 state->info->flags |= UIF_NORMAL_ACTIVE;
1681
1682 uart_update_termios(state);
1683 }
1684
1685 fail:
1686 return retval;
1687}
1688
1689static const char *uart_type(struct uart_port *port)
1690{
1691 const char *str = NULL;
1692
1693 if (port->ops->type)
1694 str = port->ops->type(port);
1695
1696 if (!str)
1697 str = "unknown";
1698
1699 return str;
1700}
1701
1702#ifdef CONFIG_PROC_FS
1703
1704static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1705{
1706 struct uart_state *state = drv->state + i;
George G. Davis3689a0e2007-02-14 00:33:06 -08001707 int pm_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 struct uart_port *port = state->port;
1709 char stat_buf[32];
1710 unsigned int status;
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001711 int mmio, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 if (!port)
1714 return 0;
1715
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001716 mmio = port->iotype >= UPIO_MEM;
Josh Boyer4f640ef2007-07-23 18:43:44 -07001717 ret = sprintf(buf, "%d: uart:%s %s%08llX irq:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 port->line, uart_type(port),
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001719 mmio ? "mmio:0x" : "port:",
Josh Boyer4f640ef2007-07-23 18:43:44 -07001720 mmio ? (unsigned long long)port->mapbase
Alan Coxa46c9992008-02-08 04:18:53 -08001721 : (unsigned long long) port->iobase,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 port->irq);
1723
1724 if (port->type == PORT_UNKNOWN) {
1725 strcat(buf, "\n");
1726 return ret + 1;
1727 }
1728
Alan Coxa46c9992008-02-08 04:18:53 -08001729 if (capable(CAP_SYS_ADMIN)) {
George G. Davis3689a0e2007-02-14 00:33:06 -08001730 mutex_lock(&state->mutex);
1731 pm_state = state->pm_state;
1732 if (pm_state)
1733 uart_change_pm(state, 0);
Russell Kingc5f46442005-06-29 09:42:38 +01001734 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 status = port->ops->get_mctrl(port);
Russell Kingc5f46442005-06-29 09:42:38 +01001736 spin_unlock_irq(&port->lock);
George G. Davis3689a0e2007-02-14 00:33:06 -08001737 if (pm_state)
1738 uart_change_pm(state, pm_state);
1739 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 ret += sprintf(buf + ret, " tx:%d rx:%d",
1742 port->icount.tx, port->icount.rx);
1743 if (port->icount.frame)
1744 ret += sprintf(buf + ret, " fe:%d",
1745 port->icount.frame);
1746 if (port->icount.parity)
1747 ret += sprintf(buf + ret, " pe:%d",
1748 port->icount.parity);
1749 if (port->icount.brk)
1750 ret += sprintf(buf + ret, " brk:%d",
1751 port->icount.brk);
1752 if (port->icount.overrun)
1753 ret += sprintf(buf + ret, " oe:%d",
1754 port->icount.overrun);
Alan Coxa46c9992008-02-08 04:18:53 -08001755
1756#define INFOBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (port->mctrl & (bit)) \
1758 strncat(stat_buf, (str), sizeof(stat_buf) - \
1759 strlen(stat_buf) - 2)
Alan Coxa46c9992008-02-08 04:18:53 -08001760#define STATBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (status & (bit)) \
1762 strncat(stat_buf, (str), sizeof(stat_buf) - \
1763 strlen(stat_buf) - 2)
1764
1765 stat_buf[0] = '\0';
1766 stat_buf[1] = '\0';
1767 INFOBIT(TIOCM_RTS, "|RTS");
1768 STATBIT(TIOCM_CTS, "|CTS");
1769 INFOBIT(TIOCM_DTR, "|DTR");
1770 STATBIT(TIOCM_DSR, "|DSR");
1771 STATBIT(TIOCM_CAR, "|CD");
1772 STATBIT(TIOCM_RNG, "|RI");
1773 if (stat_buf[0])
1774 stat_buf[0] = ' ';
1775 strcat(stat_buf, "\n");
Alan Coxa46c9992008-02-08 04:18:53 -08001776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 ret += sprintf(buf + ret, stat_buf);
1778 } else {
1779 strcat(buf, "\n");
1780 ret++;
1781 }
1782#undef STATBIT
1783#undef INFOBIT
1784 return ret;
1785}
1786
1787static int uart_read_proc(char *page, char **start, off_t off,
1788 int count, int *eof, void *data)
1789{
1790 struct tty_driver *ttydrv = data;
1791 struct uart_driver *drv = ttydrv->driver_state;
1792 int i, len = 0, l;
1793 off_t begin = 0;
1794
1795 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1796 "", "", "");
1797 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1798 l = uart_line_info(page + len, drv, i);
1799 len += l;
1800 if (len + begin > off + count)
1801 goto done;
1802 if (len + begin < off) {
1803 begin += len;
1804 len = 0;
1805 }
1806 }
1807 *eof = 1;
1808 done:
1809 if (off >= len + begin)
1810 return 0;
1811 *start = page + (off - begin);
1812 return (count < begin + len - off) ? count : (begin + len - off);
1813}
1814#endif
1815
Andrew Morton4a1b5502008-03-07 15:51:16 -08001816#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817/*
Russell Kingd3587882006-03-20 20:00:09 +00001818 * uart_console_write - write a console message to a serial port
1819 * @port: the port to write the message
1820 * @s: array of characters
1821 * @count: number of characters in string to write
1822 * @write: function to write character to port
1823 */
1824void uart_console_write(struct uart_port *port, const char *s,
1825 unsigned int count,
1826 void (*putchar)(struct uart_port *, int))
1827{
1828 unsigned int i;
1829
1830 for (i = 0; i < count; i++, s++) {
1831 if (*s == '\n')
1832 putchar(port, '\r');
1833 putchar(port, *s);
1834 }
1835}
1836EXPORT_SYMBOL_GPL(uart_console_write);
1837
1838/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 * Check whether an invalid uart number has been specified, and
1840 * if so, search for the first available port that does have
1841 * console support.
1842 */
1843struct uart_port * __init
1844uart_get_console(struct uart_port *ports, int nr, struct console *co)
1845{
1846 int idx = co->index;
1847
1848 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1849 ports[idx].membase == NULL))
1850 for (idx = 0; idx < nr; idx++)
1851 if (ports[idx].iobase != 0 ||
1852 ports[idx].membase != NULL)
1853 break;
1854
1855 co->index = idx;
1856
1857 return ports + idx;
1858}
1859
1860/**
1861 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1862 * @options: pointer to option string
1863 * @baud: pointer to an 'int' variable for the baud rate.
1864 * @parity: pointer to an 'int' variable for the parity.
1865 * @bits: pointer to an 'int' variable for the number of data bits.
1866 * @flow: pointer to an 'int' variable for the flow control character.
1867 *
1868 * uart_parse_options decodes a string containing the serial console
1869 * options. The format of the string is <baud><parity><bits><flow>,
1870 * eg: 115200n8r
1871 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001872void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1874{
1875 char *s = options;
1876
1877 *baud = simple_strtoul(s, NULL, 10);
1878 while (*s >= '0' && *s <= '9')
1879 s++;
1880 if (*s)
1881 *parity = *s++;
1882 if (*s)
1883 *bits = *s++ - '0';
1884 if (*s)
1885 *flow = *s;
1886}
Jason Wesself2d937f2008-04-17 20:05:37 +02001887EXPORT_SYMBOL_GPL(uart_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889struct baud_rates {
1890 unsigned int rate;
1891 unsigned int cflag;
1892};
1893
Arjan van de Vencb3592b2005-11-28 21:04:11 +00001894static const struct baud_rates baud_rates[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 { 921600, B921600 },
1896 { 460800, B460800 },
1897 { 230400, B230400 },
1898 { 115200, B115200 },
1899 { 57600, B57600 },
1900 { 38400, B38400 },
1901 { 19200, B19200 },
1902 { 9600, B9600 },
1903 { 4800, B4800 },
1904 { 2400, B2400 },
1905 { 1200, B1200 },
1906 { 0, B38400 }
1907};
1908
1909/**
1910 * uart_set_options - setup the serial console parameters
1911 * @port: pointer to the serial ports uart_port structure
1912 * @co: console pointer
1913 * @baud: baud rate
1914 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1915 * @bits: number of data bits
1916 * @flow: flow control character - 'r' (rts)
1917 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001918int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919uart_set_options(struct uart_port *port, struct console *co,
1920 int baud, int parity, int bits, int flow)
1921{
Alan Cox606d0992006-12-08 02:38:45 -08001922 struct ktermios termios;
Alan Cox149b36e2007-10-18 01:24:16 -07001923 static struct ktermios dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 int i;
1925
Russell King976ecd12005-07-03 21:05:45 +01001926 /*
1927 * Ensure that the serial console lock is initialised
1928 * early.
1929 */
1930 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07001931 lockdep_set_class(&port->lock, &port_lock_key);
Russell King976ecd12005-07-03 21:05:45 +01001932
Alan Cox606d0992006-12-08 02:38:45 -08001933 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1936
1937 /*
1938 * Construct a cflag setting.
1939 */
1940 for (i = 0; baud_rates[i].rate; i++)
1941 if (baud_rates[i].rate <= baud)
1942 break;
1943
1944 termios.c_cflag |= baud_rates[i].cflag;
1945
1946 if (bits == 7)
1947 termios.c_cflag |= CS7;
1948 else
1949 termios.c_cflag |= CS8;
1950
1951 switch (parity) {
1952 case 'o': case 'O':
1953 termios.c_cflag |= PARODD;
1954 /*fall through*/
1955 case 'e': case 'E':
1956 termios.c_cflag |= PARENB;
1957 break;
1958 }
1959
1960 if (flow == 'r')
1961 termios.c_cflag |= CRTSCTS;
1962
Yinghai Lu79492682007-07-15 23:37:25 -07001963 /*
1964 * some uarts on other side don't support no flow control.
1965 * So we set * DTR in host uart to make them happy
1966 */
1967 port->mctrl |= TIOCM_DTR;
1968
Alan Cox149b36e2007-10-18 01:24:16 -07001969 port->ops->set_termios(port, &termios, &dummy);
Jason Wesself2d937f2008-04-17 20:05:37 +02001970 /*
1971 * Allow the setting of the UART parameters with a NULL console
1972 * too:
1973 */
1974 if (co)
1975 co->cflag = termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 return 0;
1978}
Jason Wesself2d937f2008-04-17 20:05:37 +02001979EXPORT_SYMBOL_GPL(uart_set_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980#endif /* CONFIG_SERIAL_CORE_CONSOLE */
1981
1982static void uart_change_pm(struct uart_state *state, int pm_state)
1983{
1984 struct uart_port *port = state->port;
Andrew Victor1281e362006-05-16 11:28:49 +01001985
1986 if (state->pm_state != pm_state) {
1987 if (port->ops->pm)
1988 port->ops->pm(port, pm_state, state->pm_state);
1989 state->pm_state = pm_state;
1990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001993struct uart_match {
1994 struct uart_port *port;
1995 struct uart_driver *driver;
1996};
1997
1998static int serial_match_port(struct device *dev, void *data)
1999{
2000 struct uart_match *match = data;
Guennadi Liakhovetski7ca796f2008-07-04 09:59:28 -07002001 struct tty_driver *tty_drv = match->driver->tty_driver;
2002 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
2003 match->port->line;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002004
2005 return dev->devt == devt; /* Actually, only one tty per port */
2006}
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
2009{
2010 struct uart_state *state = drv->state + port->line;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002011 struct device *tty_dev;
2012 struct uart_match match = {port, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Ingo Molnare2862f62006-01-13 21:37:07 +00002014 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002016 if (!console_suspend_enabled && uart_console(port)) {
2017 /* we're going to avoid suspending serial console */
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002018 mutex_unlock(&state->mutex);
2019 return 0;
2020 }
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002021
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002022 tty_dev = device_find_child(port->dev, &match, serial_match_port);
2023 if (device_may_wakeup(tty_dev)) {
2024 enable_irq_wake(port->irq);
2025 put_device(tty_dev);
2026 mutex_unlock(&state->mutex);
2027 return 0;
2028 }
2029 port->suspended = 1;
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (state->info && state->info->flags & UIF_INITIALIZED) {
Russell Kingba899db2006-01-21 22:45:50 +00002032 const struct uart_ops *ops = port->ops;
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002033 int tries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Russell Kinga6b93a92006-10-01 17:17:40 +01002035 state->info->flags = (state->info->flags & ~UIF_INITIALIZED)
2036 | UIF_SUSPENDED;
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 spin_lock_irq(&port->lock);
Russell Kingb129a8c2005-08-31 10:12:14 +01002039 ops->stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 ops->set_mctrl(port, 0);
2041 ops->stop_rx(port);
2042 spin_unlock_irq(&port->lock);
2043
2044 /*
2045 * Wait for the transmitter to empty.
2046 */
Alan Coxa46c9992008-02-08 04:18:53 -08002047 for (tries = 3; !ops->tx_empty(port) && tries; tries--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 msleep(10);
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002049 if (!tries)
Alan Coxa46c9992008-02-08 04:18:53 -08002050 printk(KERN_ERR "%s%s%s%d: Unable to drain "
2051 "transmitter\n",
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002052 port->dev ? port->dev->bus_id : "",
2053 port->dev ? ": " : "",
David S. Miller84408382008-10-13 10:45:26 +01002054 drv->dev_name,
2055 drv->tty_driver->name_base + port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 ops->shutdown(port);
2058 }
2059
2060 /*
2061 * Disable the console device before suspending.
2062 */
2063 if (uart_console(port))
2064 console_stop(port->cons);
2065
2066 uart_change_pm(state, 3);
2067
Ingo Molnare2862f62006-01-13 21:37:07 +00002068 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 return 0;
2071}
2072
2073int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
2074{
2075 struct uart_state *state = drv->state + port->line;
Arjan van de Ven03a74dc2008-05-23 13:04:49 -07002076 struct device *tty_dev;
2077 struct uart_match match = {port, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Ingo Molnare2862f62006-01-13 21:37:07 +00002079 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002081 if (!console_suspend_enabled && uart_console(port)) {
2082 /* no need to resume serial console, it wasn't suspended */
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002083 mutex_unlock(&state->mutex);
2084 return 0;
2085 }
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002086
Arjan van de Ven03a74dc2008-05-23 13:04:49 -07002087 tty_dev = device_find_child(port->dev, &match, serial_match_port);
2088 if (!port->suspended && device_may_wakeup(tty_dev)) {
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002089 disable_irq_wake(port->irq);
2090 mutex_unlock(&state->mutex);
2091 return 0;
2092 }
2093 port->suspended = 0;
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 /*
2096 * Re-enable the console device after suspending.
2097 */
2098 if (uart_console(port)) {
Alan Cox606d0992006-12-08 02:38:45 -08002099 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 /*
2102 * First try to use the console cflag setting.
2103 */
Alan Cox606d0992006-12-08 02:38:45 -08002104 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 termios.c_cflag = port->cons->cflag;
2106
2107 /*
2108 * If that's unset, use the tty termios setting.
2109 */
Alan Coxdf4f4dd2008-07-16 21:53:50 +01002110 if (state->info && state->info->port.tty && termios.c_cflag == 0)
2111 termios = *state->info->port.tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Russell King9d778a62008-02-04 22:27:51 -08002113 uart_change_pm(state, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 port->ops->set_termios(port, &termios, NULL);
2115 console_start(port->cons);
2116 }
2117
Russell Kinga6b93a92006-10-01 17:17:40 +01002118 if (state->info && state->info->flags & UIF_SUSPENDED) {
Russell Kingba899db2006-01-21 22:45:50 +00002119 const struct uart_ops *ops = port->ops;
Russell Kingee31b332005-11-13 15:28:51 +00002120 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Russell King9d778a62008-02-04 22:27:51 -08002122 uart_change_pm(state, 0);
Alan Coxf34d7a52008-04-30 00:54:13 -07002123 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 ops->set_mctrl(port, 0);
Alan Coxf34d7a52008-04-30 00:54:13 -07002125 spin_unlock_irq(&port->lock);
Russell Kingee31b332005-11-13 15:28:51 +00002126 ret = ops->startup(port);
2127 if (ret == 0) {
2128 uart_change_speed(state, NULL);
2129 spin_lock_irq(&port->lock);
2130 ops->set_mctrl(port, port->mctrl);
2131 ops->start_tx(port);
2132 spin_unlock_irq(&port->lock);
Russell Kinga6b93a92006-10-01 17:17:40 +01002133 state->info->flags |= UIF_INITIALIZED;
Russell Kingee31b332005-11-13 15:28:51 +00002134 } else {
2135 /*
2136 * Failed to resume - maybe hardware went away?
2137 * Clear the "initialized" flag so we won't try
2138 * to call the low level drivers shutdown method.
2139 */
Russell Kingee31b332005-11-13 15:28:51 +00002140 uart_shutdown(state);
2141 }
Russell Kinga6b93a92006-10-01 17:17:40 +01002142
2143 state->info->flags &= ~UIF_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145
Ingo Molnare2862f62006-01-13 21:37:07 +00002146 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 return 0;
2149}
2150
2151static inline void
2152uart_report_port(struct uart_driver *drv, struct uart_port *port)
2153{
Russell King30b7a3b2005-09-03 15:30:21 +01002154 char address[64];
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 switch (port->iotype) {
2157 case UPIO_PORT:
Andrew Morton9bde10a2008-10-13 10:35:42 +01002158 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 break;
2160 case UPIO_HUB6:
Russell King30b7a3b2005-09-03 15:30:21 +01002161 snprintf(address, sizeof(address),
Andrew Morton9bde10a2008-10-13 10:35:42 +01002162 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 break;
2164 case UPIO_MEM:
2165 case UPIO_MEM32:
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002166 case UPIO_AU:
Zang Roy-r619113be91ec2006-06-30 02:29:58 -07002167 case UPIO_TSI:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002168 case UPIO_DWAPB:
Russell King30b7a3b2005-09-03 15:30:21 +01002169 snprintf(address, sizeof(address),
Josh Boyer4f640ef2007-07-23 18:43:44 -07002170 "MMIO 0x%llx", (unsigned long long)port->mapbase);
Russell King30b7a3b2005-09-03 15:30:21 +01002171 break;
2172 default:
2173 strlcpy(address, "*unknown*", sizeof(address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 break;
2175 }
Russell King30b7a3b2005-09-03 15:30:21 +01002176
Russell King0cf669d2005-10-31 11:42:22 +00002177 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2178 port->dev ? port->dev->bus_id : "",
2179 port->dev ? ": " : "",
David S. Miller84408382008-10-13 10:45:26 +01002180 drv->dev_name,
2181 drv->tty_driver->name_base + port->line,
2182 address, port->irq, uart_type(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
2185static void
2186uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2187 struct uart_port *port)
2188{
2189 unsigned int flags;
2190
2191 /*
2192 * If there isn't a port here, don't do anything further.
2193 */
2194 if (!port->iobase && !port->mapbase && !port->membase)
2195 return;
2196
2197 /*
2198 * Now do the auto configuration stuff. Note that config_port
2199 * is expected to claim the resources and map the port for us.
2200 */
2201 flags = UART_CONFIG_TYPE;
2202 if (port->flags & UPF_AUTO_IRQ)
2203 flags |= UART_CONFIG_IRQ;
2204 if (port->flags & UPF_BOOT_AUTOCONF) {
2205 port->type = PORT_UNKNOWN;
2206 port->ops->config_port(port, flags);
2207 }
2208
2209 if (port->type != PORT_UNKNOWN) {
2210 unsigned long flags;
2211
2212 uart_report_port(drv, port);
2213
George G. Davis3689a0e2007-02-14 00:33:06 -08002214 /* Power up port for set_mctrl() */
2215 uart_change_pm(state, 0);
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 /*
2218 * Ensure that the modem control lines are de-activated.
Yinghai Luc3e46422008-02-04 22:27:46 -08002219 * keep the DTR setting that is set in uart_set_options()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 * We probably don't need a spinlock around this, but
2221 */
2222 spin_lock_irqsave(&port->lock, flags);
Yinghai Luc3e46422008-02-04 22:27:46 -08002223 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 spin_unlock_irqrestore(&port->lock, flags);
2225
2226 /*
Russell King97d97222007-09-01 21:25:09 +01002227 * If this driver supports console, and it hasn't been
2228 * successfully registered yet, try to re-register it.
2229 * It may be that the port was not available.
2230 */
2231 if (port->cons && !(port->cons->flags & CON_ENABLED))
2232 register_console(port->cons);
2233
2234 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 * Power down all ports by default, except the
2236 * console if we have one.
2237 */
2238 if (!uart_console(port))
2239 uart_change_pm(state, 3);
2240 }
2241}
2242
Jason Wesself2d937f2008-04-17 20:05:37 +02002243#ifdef CONFIG_CONSOLE_POLL
2244
2245static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2246{
2247 struct uart_driver *drv = driver->driver_state;
2248 struct uart_state *state = drv->state + line;
2249 struct uart_port *port;
2250 int baud = 9600;
2251 int bits = 8;
2252 int parity = 'n';
2253 int flow = 'n';
2254
2255 if (!state || !state->port)
2256 return -1;
2257
2258 port = state->port;
2259 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2260 return -1;
2261
2262 if (options) {
2263 uart_parse_options(options, &baud, &parity, &bits, &flow);
2264 return uart_set_options(port, NULL, baud, parity, bits, flow);
2265 }
2266
2267 return 0;
2268}
2269
2270static int uart_poll_get_char(struct tty_driver *driver, int line)
2271{
2272 struct uart_driver *drv = driver->driver_state;
2273 struct uart_state *state = drv->state + line;
2274 struct uart_port *port;
2275
2276 if (!state || !state->port)
2277 return -1;
2278
2279 port = state->port;
2280 return port->ops->poll_get_char(port);
2281}
2282
2283static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2284{
2285 struct uart_driver *drv = driver->driver_state;
2286 struct uart_state *state = drv->state + line;
2287 struct uart_port *port;
2288
2289 if (!state || !state->port)
2290 return;
2291
2292 port = state->port;
2293 port->ops->poll_put_char(port, ch);
2294}
2295#endif
2296
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002297static const struct tty_operations uart_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 .open = uart_open,
2299 .close = uart_close,
2300 .write = uart_write,
2301 .put_char = uart_put_char,
2302 .flush_chars = uart_flush_chars,
2303 .write_room = uart_write_room,
2304 .chars_in_buffer= uart_chars_in_buffer,
2305 .flush_buffer = uart_flush_buffer,
2306 .ioctl = uart_ioctl,
2307 .throttle = uart_throttle,
2308 .unthrottle = uart_unthrottle,
2309 .send_xchar = uart_send_xchar,
2310 .set_termios = uart_set_termios,
Alan Cox64e91592008-06-03 15:18:54 +01002311 .set_ldisc = uart_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 .stop = uart_stop,
2313 .start = uart_start,
2314 .hangup = uart_hangup,
2315 .break_ctl = uart_break_ctl,
2316 .wait_until_sent= uart_wait_until_sent,
2317#ifdef CONFIG_PROC_FS
2318 .read_proc = uart_read_proc,
2319#endif
2320 .tiocmget = uart_tiocmget,
2321 .tiocmset = uart_tiocmset,
Jason Wesself2d937f2008-04-17 20:05:37 +02002322#ifdef CONFIG_CONSOLE_POLL
2323 .poll_init = uart_poll_init,
2324 .poll_get_char = uart_poll_get_char,
2325 .poll_put_char = uart_poll_put_char,
2326#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327};
2328
2329/**
2330 * uart_register_driver - register a driver with the uart core layer
2331 * @drv: low level driver structure
2332 *
2333 * Register a uart driver with the core driver. We in turn register
2334 * with the tty layer, and initialise the core driver per-port state.
2335 *
2336 * We have a proc file in /proc/tty/driver which is named after the
2337 * normal driver.
2338 *
2339 * drv->port should be NULL, and the per-port structures should be
2340 * registered using uart_add_one_port after this call has succeeded.
2341 */
2342int uart_register_driver(struct uart_driver *drv)
2343{
2344 struct tty_driver *normal = NULL;
2345 int i, retval;
2346
2347 BUG_ON(drv->state);
2348
2349 /*
2350 * Maybe we should be using a slab cache for this, especially if
2351 * we have a large number of ports to handle.
2352 */
Burman Yan8f31bb32007-02-14 00:33:07 -08002353 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 retval = -ENOMEM;
2355 if (!drv->state)
2356 goto out;
2357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 normal = alloc_tty_driver(drv->nr);
2359 if (!normal)
2360 goto out;
2361
2362 drv->tty_driver = normal;
2363
2364 normal->owner = drv->owner;
2365 normal->driver_name = drv->driver_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 normal->name = drv->dev_name;
2367 normal->major = drv->major;
2368 normal->minor_start = drv->minor;
2369 normal->type = TTY_DRIVER_TYPE_SERIAL;
2370 normal->subtype = SERIAL_TYPE_NORMAL;
2371 normal->init_termios = tty_std_termios;
2372 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -08002373 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07002374 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 normal->driver_state = drv;
2376 tty_set_operations(normal, &uart_ops);
2377
2378 /*
2379 * Initialise the UART state(s).
2380 */
2381 for (i = 0; i < drv->nr; i++) {
2382 struct uart_state *state = drv->state + i;
2383
2384 state->close_delay = 500; /* .5 seconds */
2385 state->closing_wait = 30000; /* 30 seconds */
2386
Ingo Molnare2862f62006-01-13 21:37:07 +00002387 mutex_init(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 }
2389
2390 retval = tty_register_driver(normal);
2391 out:
2392 if (retval < 0) {
2393 put_tty_driver(normal);
2394 kfree(drv->state);
2395 }
2396 return retval;
2397}
2398
2399/**
2400 * uart_unregister_driver - remove a driver from the uart core layer
2401 * @drv: low level driver structure
2402 *
2403 * Remove all references to a driver from the core driver. The low
2404 * level driver must have removed all its ports via the
2405 * uart_remove_one_port() if it registered them with uart_add_one_port().
2406 * (ie, drv->port == NULL)
2407 */
2408void uart_unregister_driver(struct uart_driver *drv)
2409{
2410 struct tty_driver *p = drv->tty_driver;
2411 tty_unregister_driver(p);
2412 put_tty_driver(p);
2413 kfree(drv->state);
2414 drv->tty_driver = NULL;
2415}
2416
2417struct tty_driver *uart_console_device(struct console *co, int *index)
2418{
2419 struct uart_driver *p = co->data;
2420 *index = co->index;
2421 return p->tty_driver;
2422}
2423
2424/**
2425 * uart_add_one_port - attach a driver-defined port structure
2426 * @drv: pointer to the uart low level driver structure for this port
2427 * @port: uart port structure to use for this port.
2428 *
2429 * This allows the driver to register its own uart_port structure
2430 * with the core driver. The main purpose is to allow the low
2431 * level uart drivers to expand uart_port, rather than having yet
2432 * more levels of structures.
2433 */
2434int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2435{
2436 struct uart_state *state;
2437 int ret = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002438 struct device *tty_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 BUG_ON(in_interrupt());
2441
2442 if (port->line >= drv->nr)
2443 return -EINVAL;
2444
2445 state = drv->state + port->line;
2446
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002447 mutex_lock(&port_mutex);
Russell King68ac64c2006-04-30 11:13:50 +01002448 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 if (state->port) {
2450 ret = -EINVAL;
2451 goto out;
2452 }
2453
2454 state->port = port;
Russell King97d97222007-09-01 21:25:09 +01002455 state->pm_state = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 port->cons = drv->cons;
2458 port->info = state->info;
2459
Russell King976ecd12005-07-03 21:05:45 +01002460 /*
2461 * If this port is a console, then the spinlock is already
2462 * initialised.
2463 */
Ingo Molnar13e83592006-07-03 00:25:03 -07002464 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
Russell King976ecd12005-07-03 21:05:45 +01002465 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07002466 lockdep_set_class(&port->lock, &port_lock_key);
2467 }
Russell King976ecd12005-07-03 21:05:45 +01002468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 uart_configure_port(drv, state, port);
2470
2471 /*
2472 * Register the port whether it's detected or not. This allows
2473 * setserial to be used to alter this ports parameters.
2474 */
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002475 tty_dev = tty_register_device(drv->tty_driver, port->line, port->dev);
2476 if (likely(!IS_ERR(tty_dev))) {
Alan Stern74081f82008-03-19 22:35:13 +01002477 device_init_wakeup(tty_dev, 1);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002478 device_set_wakeup_enable(tty_dev, 0);
2479 } else
2480 printk(KERN_ERR "Cannot register tty device on line %d\n",
2481 port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 /*
Russell King68ac64c2006-04-30 11:13:50 +01002484 * Ensure UPF_DEAD is not set.
2485 */
2486 port->flags &= ~UPF_DEAD;
2487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 out:
Russell King68ac64c2006-04-30 11:13:50 +01002489 mutex_unlock(&state->mutex);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002490 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 return ret;
2493}
2494
2495/**
2496 * uart_remove_one_port - detach a driver defined port structure
2497 * @drv: pointer to the uart low level driver structure for this port
2498 * @port: uart port structure for this port
2499 *
2500 * This unhooks (and hangs up) the specified port structure from the
2501 * core driver. No further calls will be made to the low-level code
2502 * for this port.
2503 */
2504int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2505{
2506 struct uart_state *state = drv->state + port->line;
Russell King68ac64c2006-04-30 11:13:50 +01002507 struct uart_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 BUG_ON(in_interrupt());
2510
2511 if (state->port != port)
2512 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2513 state->port, port);
2514
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002515 mutex_lock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517 /*
Russell King68ac64c2006-04-30 11:13:50 +01002518 * Mark the port "dead" - this prevents any opens from
2519 * succeeding while we shut down the port.
2520 */
2521 mutex_lock(&state->mutex);
2522 port->flags |= UPF_DEAD;
2523 mutex_unlock(&state->mutex);
2524
2525 /*
Greg Kroah-Hartmanaa4148c2005-06-20 21:15:16 -07002526 * Remove the devices from the tty layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 */
2528 tty_unregister_device(drv->tty_driver, port->line);
2529
Russell King68ac64c2006-04-30 11:13:50 +01002530 info = state->info;
Alan Coxdf4f4dd2008-07-16 21:53:50 +01002531 if (info && info->port.tty)
2532 tty_vhangup(info->port.tty);
Russell King68ac64c2006-04-30 11:13:50 +01002533
2534 /*
2535 * All users of this port should now be disconnected from
2536 * this driver, and the port shut down. We should be the
2537 * only thread fiddling with this port from now on.
2538 */
2539 state->info = NULL;
2540
2541 /*
2542 * Free the port IO and memory resources, if any.
2543 */
2544 if (port->type != PORT_UNKNOWN)
2545 port->ops->release_port(port);
2546
2547 /*
2548 * Indicate that there isn't a port here anymore.
2549 */
2550 port->type = PORT_UNKNOWN;
2551
2552 /*
2553 * Kill the tasklet, and free resources.
2554 */
2555 if (info) {
2556 tasklet_kill(&info->tlet);
2557 kfree(info);
2558 }
2559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 state->port = NULL;
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002561 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
2563 return 0;
2564}
2565
2566/*
2567 * Are the two ports equivalent?
2568 */
2569int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2570{
2571 if (port1->iotype != port2->iotype)
2572 return 0;
2573
2574 switch (port1->iotype) {
2575 case UPIO_PORT:
2576 return (port1->iobase == port2->iobase);
2577 case UPIO_HUB6:
2578 return (port1->iobase == port2->iobase) &&
2579 (port1->hub6 == port2->hub6);
2580 case UPIO_MEM:
Sergei Shtylyovd21b55d2006-08-28 19:49:03 +04002581 case UPIO_MEM32:
2582 case UPIO_AU:
2583 case UPIO_TSI:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002584 case UPIO_DWAPB:
Benjamin Herrenschmidt1624f002006-01-04 18:09:44 +00002585 return (port1->mapbase == port2->mapbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 }
2587 return 0;
2588}
2589EXPORT_SYMBOL(uart_match_port);
2590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591EXPORT_SYMBOL(uart_write_wakeup);
2592EXPORT_SYMBOL(uart_register_driver);
2593EXPORT_SYMBOL(uart_unregister_driver);
2594EXPORT_SYMBOL(uart_suspend_port);
2595EXPORT_SYMBOL(uart_resume_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596EXPORT_SYMBOL(uart_add_one_port);
2597EXPORT_SYMBOL(uart_remove_one_port);
2598
2599MODULE_DESCRIPTION("Serial driver core");
2600MODULE_LICENSE("GPL");