blob: eab032733790d5524ac9f14e8596c1a5a62e462c [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
53#define uart_users(state) ((state)->count + ((state)->info ? (state)->info->blocked_open : 0))
54
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;
116 tty_wakeup(state->info->tty);
117}
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
138 * will be serialised by the per-port semaphore.
139 */
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 */
Jayachandran Ca2436b22005-10-30 23:26:16 +0000155 set_bit(TTY_IO_ERROR, &info->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) {
165 page = get_zeroed_page(GFP_KERNEL);
166 if (!page)
167 return -ENOMEM;
168
169 info->xmit.buf = (unsigned char *) page;
170 uart_circ_clear(&info->xmit);
171 }
172
173 retval = port->ops->startup(port);
174 if (retval == 0) {
175 if (init_hw) {
176 /*
177 * Initialise the hardware port settings.
178 */
179 uart_change_speed(state, NULL);
180
181 /*
182 * Setup the RTS and DTR signals once the
183 * port is open and ready to respond.
184 */
185 if (info->tty->termios->c_cflag & CBAUD)
186 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
187 }
188
Russell King0dd7a1a2005-06-29 18:40:53 +0100189 if (info->flags & UIF_CTS_FLOW) {
190 spin_lock_irq(&port->lock);
191 if (!(port->ops->get_mctrl(port) & TIOCM_CTS))
192 info->tty->hw_stopped = 1;
193 spin_unlock_irq(&port->lock);
194 }
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 info->flags |= UIF_INITIALIZED;
197
198 clear_bit(TTY_IO_ERROR, &info->tty->flags);
199 }
200
201 if (retval && capable(CAP_SYS_ADMIN))
202 retval = 0;
203
204 return retval;
205}
206
207/*
208 * This routine will shutdown a serial port; interrupts are disabled, and
209 * DTR is dropped if the hangup on close termio flag is on. Calls to
210 * uart_shutdown are serialised by the per-port semaphore.
211 */
212static void uart_shutdown(struct uart_state *state)
213{
214 struct uart_info *info = state->info;
215 struct uart_port *port = state->port;
216
Russell Kingee31b332005-11-13 15:28:51 +0000217 /*
218 * Set the TTY IO error marker
219 */
220 if (info->tty)
221 set_bit(TTY_IO_ERROR, &info->tty->flags);
222
223 if (info->flags & UIF_INITIALIZED) {
224 info->flags &= ~UIF_INITIALIZED;
225
226 /*
227 * Turn off DTR and RTS early.
228 */
229 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
230 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
231
232 /*
233 * clear delta_msr_wait queue to avoid mem leaks: we may free
234 * the irq here so the queue might never be woken up. Note
235 * that we won't end up waiting on delta_msr_wait again since
236 * any outstanding file descriptors should be pointing at
237 * hung_up_tty_fops now.
238 */
239 wake_up_interruptible(&info->delta_msr_wait);
240
241 /*
242 * Free the IRQ and disable the port.
243 */
244 port->ops->shutdown(port);
245
246 /*
247 * Ensure that the IRQ handler isn't running on another CPU.
248 */
249 synchronize_irq(port->irq);
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /*
Russell Kingee31b332005-11-13 15:28:51 +0000253 * kill off our tasklet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
Russell Kingee31b332005-11-13 15:28:51 +0000255 tasklet_kill(&info->tlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /*
258 * Free the transmit buffer page.
259 */
260 if (info->xmit.buf) {
261 free_page((unsigned long)info->xmit.buf);
262 info->xmit.buf = NULL;
263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/**
267 * uart_update_timeout - update per-port FIFO timeout.
268 * @port: uart_port structure describing the port
269 * @cflag: termios cflag value
270 * @baud: speed of the port
271 *
272 * Set the port FIFO timeout value. The @cflag value should
273 * reflect the actual hardware settings.
274 */
275void
276uart_update_timeout(struct uart_port *port, unsigned int cflag,
277 unsigned int baud)
278{
279 unsigned int bits;
280
281 /* byte size and parity */
282 switch (cflag & CSIZE) {
283 case CS5:
284 bits = 7;
285 break;
286 case CS6:
287 bits = 8;
288 break;
289 case CS7:
290 bits = 9;
291 break;
292 default:
293 bits = 10;
Alan Coxa46c9992008-02-08 04:18:53 -0800294 break; /* CS8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
297 if (cflag & CSTOPB)
298 bits++;
299 if (cflag & PARENB)
300 bits++;
301
302 /*
303 * The total number of bits to be transmitted in the fifo.
304 */
305 bits = bits * port->fifosize;
306
307 /*
308 * Figure the timeout to send the above number of bits.
309 * Add .02 seconds of slop
310 */
311 port->timeout = (HZ * bits) / baud + HZ/50;
312}
313
314EXPORT_SYMBOL(uart_update_timeout);
315
316/**
317 * uart_get_baud_rate - return baud rate for a particular port
318 * @port: uart_port structure describing the port in question.
319 * @termios: desired termios settings.
320 * @old: old termios (or NULL)
321 * @min: minimum acceptable baud rate
322 * @max: maximum acceptable baud rate
323 *
324 * Decode the termios structure into a numeric baud rate,
325 * taking account of the magic 38400 baud rate (with spd_*
326 * flags), and mapping the %B0 rate to 9600 baud.
327 *
328 * If the new baud rate is invalid, try the old termios setting.
329 * If it's still invalid, we try 9600 baud.
330 *
331 * Update the @termios structure to reflect the baud rate
Alan Coxeb424fd2008-04-28 02:14:07 -0700332 * we're actually going to be using. Don't do this for the case
333 * where B0 is requested ("hang up").
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
335unsigned int
Alan Cox606d0992006-12-08 02:38:45 -0800336uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
337 struct ktermios *old, unsigned int min, unsigned int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 unsigned int try, baud, altbaud = 38400;
Alan Coxeb424fd2008-04-28 02:14:07 -0700340 int hung_up = 0;
Russell King0077d452006-01-21 23:03:28 +0000341 upf_t flags = port->flags & UPF_SPD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 if (flags == UPF_SPD_HI)
344 altbaud = 57600;
345 if (flags == UPF_SPD_VHI)
346 altbaud = 115200;
347 if (flags == UPF_SPD_SHI)
348 altbaud = 230400;
349 if (flags == UPF_SPD_WARP)
350 altbaud = 460800;
351
352 for (try = 0; try < 2; try++) {
353 baud = tty_termios_baud_rate(termios);
354
355 /*
356 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
357 * Die! Die! Die!
358 */
359 if (baud == 38400)
360 baud = altbaud;
361
362 /*
363 * Special case: B0 rate.
364 */
Alan Coxeb424fd2008-04-28 02:14:07 -0700365 if (baud == 0) {
366 hung_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 baud = 9600;
Alan Coxeb424fd2008-04-28 02:14:07 -0700368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 if (baud >= min && baud <= max)
371 return baud;
372
373 /*
374 * Oops, the quotient was zero. Try again with
375 * the old baud rate if possible.
376 */
377 termios->c_cflag &= ~CBAUD;
378 if (old) {
Alan Cox6d4d67b2008-02-04 22:27:53 -0800379 baud = tty_termios_baud_rate(old);
Alan Coxeb424fd2008-04-28 02:14:07 -0700380 if (!hung_up)
381 tty_termios_encode_baud_rate(termios,
382 baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 old = NULL;
384 continue;
385 }
386
387 /*
388 * As a last resort, if the quotient is zero,
389 * default to 9600 bps
390 */
Alan Coxeb424fd2008-04-28 02:14:07 -0700391 if (!hung_up)
392 tty_termios_encode_baud_rate(termios, 9600, 9600);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
395 return 0;
396}
397
398EXPORT_SYMBOL(uart_get_baud_rate);
399
400/**
401 * uart_get_divisor - return uart clock divisor
402 * @port: uart_port structure describing the port.
403 * @baud: desired baud rate
404 *
405 * Calculate the uart clock divisor for the port.
406 */
407unsigned int
408uart_get_divisor(struct uart_port *port, unsigned int baud)
409{
410 unsigned int quot;
411
412 /*
413 * Old custom speed handling.
414 */
415 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
416 quot = port->custom_divisor;
417 else
418 quot = (port->uartclk + (8 * baud)) / (16 * baud);
419
420 return quot;
421}
422
423EXPORT_SYMBOL(uart_get_divisor);
424
Alan Cox23d22ce2008-04-30 00:54:11 -0700425/* FIXME: Consistent locking policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426static void
Alan Cox606d0992006-12-08 02:38:45 -0800427uart_change_speed(struct uart_state *state, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct tty_struct *tty = state->info->tty;
430 struct uart_port *port = state->port;
Alan Cox606d0992006-12-08 02:38:45 -0800431 struct ktermios *termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 /*
434 * If we have no tty, termios, or the port does not exist,
435 * then we can't set the parameters for this port.
436 */
437 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
438 return;
439
440 termios = tty->termios;
441
442 /*
443 * Set flags based on termios cflag
444 */
445 if (termios->c_cflag & CRTSCTS)
446 state->info->flags |= UIF_CTS_FLOW;
447 else
448 state->info->flags &= ~UIF_CTS_FLOW;
449
450 if (termios->c_cflag & CLOCAL)
451 state->info->flags &= ~UIF_CHECK_CD;
452 else
453 state->info->flags |= UIF_CHECK_CD;
454
455 port->ops->set_termios(port, termios, old_termios);
456}
457
Alan Cox23d22ce2008-04-30 00:54:11 -0700458static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459__uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
460{
461 unsigned long flags;
Alan Cox23d22ce2008-04-30 00:54:11 -0700462 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 if (!circ->buf)
Alan Cox23d22ce2008-04-30 00:54:11 -0700465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 spin_lock_irqsave(&port->lock, flags);
468 if (uart_circ_chars_free(circ) != 0) {
469 circ->buf[circ->head] = c;
470 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
Alan Cox23d22ce2008-04-30 00:54:11 -0700471 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473 spin_unlock_irqrestore(&port->lock, flags);
Alan Cox23d22ce2008-04-30 00:54:11 -0700474 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Alan Cox23d22ce2008-04-30 00:54:11 -0700477static int uart_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct uart_state *state = tty->driver_data;
480
Alan Cox23d22ce2008-04-30 00:54:11 -0700481 return __uart_put_char(state->port, &state->info->xmit, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484static void uart_flush_chars(struct tty_struct *tty)
485{
486 uart_start(tty);
487}
488
489static int
Pavel Machekd5f735e2006-03-07 21:55:20 -0800490uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct uart_state *state = tty->driver_data;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800493 struct uart_port *port;
494 struct circ_buf *circ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 unsigned long flags;
496 int c, ret = 0;
497
Pavel Machekd5f735e2006-03-07 21:55:20 -0800498 /*
499 * This means you called this function _after_ the port was
500 * closed. No cookie for you.
501 */
502 if (!state || !state->info) {
503 WARN_ON(1);
504 return -EL3HLT;
505 }
506
507 port = state->port;
508 circ = &state->info->xmit;
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (!circ->buf)
511 return 0;
512
513 spin_lock_irqsave(&port->lock, flags);
514 while (1) {
515 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
516 if (count < c)
517 c = count;
518 if (c <= 0)
519 break;
520 memcpy(circ->buf + circ->head, buf, c);
521 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
522 buf += c;
523 count -= c;
524 ret += c;
525 }
526 spin_unlock_irqrestore(&port->lock, flags);
527
528 uart_start(tty);
529 return ret;
530}
531
532static int uart_write_room(struct tty_struct *tty)
533{
534 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700535 unsigned long flags;
536 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Alan Coxf34d7a52008-04-30 00:54:13 -0700538 spin_lock_irqsave(&state->port->lock, flags);
539 ret = uart_circ_chars_free(&state->info->xmit);
540 spin_unlock_irqrestore(&state->port->lock, flags);
541 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544static int uart_chars_in_buffer(struct tty_struct *tty)
545{
546 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700547 unsigned long flags;
548 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Alan Coxf34d7a52008-04-30 00:54:13 -0700550 spin_lock_irqsave(&state->port->lock, flags);
551 ret = uart_circ_chars_pending(&state->info->xmit);
552 spin_unlock_irqrestore(&state->port->lock, flags);
553 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
556static void uart_flush_buffer(struct tty_struct *tty)
557{
558 struct uart_state *state = tty->driver_data;
Tetsuo Handa55d7b682008-05-06 20:42:27 -0700559 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 unsigned long flags;
561
Pavel Machekd5f735e2006-03-07 21:55:20 -0800562 /*
563 * This means you called this function _after_ the port was
564 * closed. No cookie for you.
565 */
566 if (!state || !state->info) {
567 WARN_ON(1);
568 return;
569 }
570
Tetsuo Handa55d7b682008-05-06 20:42:27 -0700571 port = state->port;
Jiri Slabyeb3a1e12007-05-06 14:48:52 -0700572 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 spin_lock_irqsave(&port->lock, flags);
575 uart_circ_clear(&state->info->xmit);
576 spin_unlock_irqrestore(&port->lock, flags);
577 tty_wakeup(tty);
578}
579
580/*
581 * This function is used to send a high-priority XON/XOFF character to
582 * the device
583 */
584static void uart_send_xchar(struct tty_struct *tty, char ch)
585{
586 struct uart_state *state = tty->driver_data;
587 struct uart_port *port = state->port;
588 unsigned long flags;
589
590 if (port->ops->send_xchar)
591 port->ops->send_xchar(port, ch);
592 else {
593 port->x_char = ch;
594 if (ch) {
595 spin_lock_irqsave(&port->lock, flags);
Russell Kingb129a8c2005-08-31 10:12:14 +0100596 port->ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 spin_unlock_irqrestore(&port->lock, flags);
598 }
599 }
600}
601
602static void uart_throttle(struct tty_struct *tty)
603{
604 struct uart_state *state = tty->driver_data;
605
606 if (I_IXOFF(tty))
607 uart_send_xchar(tty, STOP_CHAR(tty));
608
609 if (tty->termios->c_cflag & CRTSCTS)
610 uart_clear_mctrl(state->port, TIOCM_RTS);
611}
612
613static void uart_unthrottle(struct tty_struct *tty)
614{
615 struct uart_state *state = tty->driver_data;
616 struct uart_port *port = state->port;
617
618 if (I_IXOFF(tty)) {
619 if (port->x_char)
620 port->x_char = 0;
621 else
622 uart_send_xchar(tty, START_CHAR(tty));
623 }
624
625 if (tty->termios->c_cflag & CRTSCTS)
626 uart_set_mctrl(port, TIOCM_RTS);
627}
628
629static int uart_get_info(struct uart_state *state,
630 struct serial_struct __user *retinfo)
631{
632 struct uart_port *port = state->port;
633 struct serial_struct tmp;
634
635 memset(&tmp, 0, sizeof(tmp));
Alan Coxf34d7a52008-04-30 00:54:13 -0700636
637 /* Ensure the state we copy is consistent and no hardware changes
638 occur as we go */
639 mutex_lock(&state->mutex);
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 tmp.type = port->type;
642 tmp.line = port->line;
643 tmp.port = port->iobase;
644 if (HIGH_BITS_OFFSET)
645 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
646 tmp.irq = port->irq;
647 tmp.flags = port->flags;
648 tmp.xmit_fifo_size = port->fifosize;
649 tmp.baud_base = port->uartclk / 16;
650 tmp.close_delay = state->close_delay / 10;
651 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
652 ASYNC_CLOSING_WAIT_NONE :
Alan Coxa46c9992008-02-08 04:18:53 -0800653 state->closing_wait / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 tmp.custom_divisor = port->custom_divisor;
655 tmp.hub6 = port->hub6;
656 tmp.io_type = port->iotype;
657 tmp.iomem_reg_shift = port->regshift;
Josh Boyer4f640ef2007-07-23 18:43:44 -0700658 tmp.iomem_base = (void *)(unsigned long)port->mapbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Alan Coxf34d7a52008-04-30 00:54:13 -0700660 mutex_unlock(&state->mutex);
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
663 return -EFAULT;
664 return 0;
665}
666
667static int uart_set_info(struct uart_state *state,
668 struct serial_struct __user *newinfo)
669{
670 struct serial_struct new_serial;
671 struct uart_port *port = state->port;
672 unsigned long new_port;
Russell King0077d452006-01-21 23:03:28 +0000673 unsigned int change_irq, change_port, closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 unsigned int old_custom_divisor, close_delay;
Russell King0077d452006-01-21 23:03:28 +0000675 upf_t old_flags, new_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int retval = 0;
677
678 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
679 return -EFAULT;
680
681 new_port = new_serial.port;
682 if (HIGH_BITS_OFFSET)
683 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
684
685 new_serial.irq = irq_canonicalize(new_serial.irq);
686 close_delay = new_serial.close_delay * 10;
687 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
688 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
689
690 /*
691 * This semaphore protects state->count. It is also
692 * very useful to prevent opens. Also, take the
693 * port configuration semaphore to make sure that a
694 * module insertion/removal doesn't change anything
695 * under us.
696 */
Ingo Molnare2862f62006-01-13 21:37:07 +0000697 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
David Gibsonabb4a232007-05-06 14:48:49 -0700699 change_irq = !(port->flags & UPF_FIXED_PORT)
700 && new_serial.irq != port->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 /*
703 * Since changing the 'type' of the port changes its resource
704 * allocations, we should treat type changes the same as
705 * IO port changes.
706 */
David Gibsonabb4a232007-05-06 14:48:49 -0700707 change_port = !(port->flags & UPF_FIXED_PORT)
708 && (new_port != port->iobase ||
709 (unsigned long)new_serial.iomem_base != port->mapbase ||
710 new_serial.hub6 != port->hub6 ||
711 new_serial.io_type != port->iotype ||
712 new_serial.iomem_reg_shift != port->regshift ||
713 new_serial.type != port->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 old_flags = port->flags;
Russell King0077d452006-01-21 23:03:28 +0000716 new_flags = new_serial.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 old_custom_divisor = port->custom_divisor;
718
719 if (!capable(CAP_SYS_ADMIN)) {
720 retval = -EPERM;
721 if (change_irq || change_port ||
722 (new_serial.baud_base != port->uartclk / 16) ||
723 (close_delay != state->close_delay) ||
724 (closing_wait != state->closing_wait) ||
Russell King947deee2006-07-02 20:45:51 +0100725 (new_serial.xmit_fifo_size &&
726 new_serial.xmit_fifo_size != port->fifosize) ||
Russell King0077d452006-01-21 23:03:28 +0000727 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 goto exit;
729 port->flags = ((port->flags & ~UPF_USR_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000730 (new_flags & UPF_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 port->custom_divisor = new_serial.custom_divisor;
732 goto check_and_exit;
733 }
734
735 /*
736 * Ask the low level driver to verify the settings.
737 */
738 if (port->ops->verify_port)
739 retval = port->ops->verify_port(port, &new_serial);
740
741 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
742 (new_serial.baud_base < 9600))
743 retval = -EINVAL;
744
745 if (retval)
746 goto exit;
747
748 if (change_port || change_irq) {
749 retval = -EBUSY;
750
751 /*
752 * Make sure that we are the sole user of this port.
753 */
754 if (uart_users(state) > 1)
755 goto exit;
756
757 /*
758 * We need to shutdown the serial port at the old
759 * port/type/irq combination.
760 */
761 uart_shutdown(state);
762 }
763
764 if (change_port) {
765 unsigned long old_iobase, old_mapbase;
766 unsigned int old_type, old_iotype, old_hub6, old_shift;
767
768 old_iobase = port->iobase;
769 old_mapbase = port->mapbase;
770 old_type = port->type;
771 old_hub6 = port->hub6;
772 old_iotype = port->iotype;
773 old_shift = port->regshift;
774
775 /*
776 * Free and release old regions
777 */
778 if (old_type != PORT_UNKNOWN)
779 port->ops->release_port(port);
780
781 port->iobase = new_port;
782 port->type = new_serial.type;
783 port->hub6 = new_serial.hub6;
784 port->iotype = new_serial.io_type;
785 port->regshift = new_serial.iomem_reg_shift;
786 port->mapbase = (unsigned long)new_serial.iomem_base;
787
788 /*
789 * Claim and map the new regions
790 */
791 if (port->type != PORT_UNKNOWN) {
792 retval = port->ops->request_port(port);
793 } else {
794 /* Always success - Jean II */
795 retval = 0;
796 }
797
798 /*
799 * If we fail to request resources for the
800 * new port, try to restore the old settings.
801 */
802 if (retval && old_type != PORT_UNKNOWN) {
803 port->iobase = old_iobase;
804 port->type = old_type;
805 port->hub6 = old_hub6;
806 port->iotype = old_iotype;
807 port->regshift = old_shift;
808 port->mapbase = old_mapbase;
809 retval = port->ops->request_port(port);
810 /*
811 * If we failed to restore the old settings,
812 * we fail like this.
813 */
814 if (retval)
815 port->type = PORT_UNKNOWN;
816
817 /*
818 * We failed anyway.
819 */
820 retval = -EBUSY;
Alan Coxa46c9992008-02-08 04:18:53 -0800821 /* Added to return the correct error -Ram Gupta */
822 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824 }
825
David Gibsonabb4a232007-05-06 14:48:49 -0700826 if (change_irq)
827 port->irq = new_serial.irq;
828 if (!(port->flags & UPF_FIXED_PORT))
829 port->uartclk = new_serial.baud_base * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000831 (new_flags & UPF_CHANGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 port->custom_divisor = new_serial.custom_divisor;
833 state->close_delay = close_delay;
834 state->closing_wait = closing_wait;
Russell King947deee2006-07-02 20:45:51 +0100835 if (new_serial.xmit_fifo_size)
836 port->fifosize = new_serial.xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (state->info->tty)
838 state->info->tty->low_latency =
839 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
840
841 check_and_exit:
842 retval = 0;
843 if (port->type == PORT_UNKNOWN)
844 goto exit;
845 if (state->info->flags & UIF_INITIALIZED) {
846 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
847 old_custom_divisor != port->custom_divisor) {
848 /*
849 * If they're setting up a custom divisor or speed,
850 * instead of clearing it, then bitch about it. No
851 * need to rate-limit; it's CAP_SYS_ADMIN only.
852 */
853 if (port->flags & UPF_SPD_MASK) {
854 char buf[64];
855 printk(KERN_NOTICE
856 "%s sets custom speed on %s. This "
857 "is deprecated.\n", current->comm,
858 tty_name(state->info->tty, buf));
859 }
860 uart_change_speed(state, NULL);
861 }
862 } else
863 retval = uart_startup(state, 1);
864 exit:
Ingo Molnare2862f62006-01-13 21:37:07 +0000865 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return retval;
867}
868
869
870/*
871 * uart_get_lsr_info - get line status register info.
872 * Note: uart_ioctl protects us against hangups.
873 */
874static int uart_get_lsr_info(struct uart_state *state,
875 unsigned int __user *value)
876{
877 struct uart_port *port = state->port;
878 unsigned int result;
879
880 result = port->ops->tx_empty(port);
881
882 /*
883 * If we're about to load something into the transmit
884 * register, we'll pretend the transmitter isn't empty to
885 * avoid a race condition (depending on when the transmit
886 * interrupt happens).
887 */
888 if (port->x_char ||
889 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
890 !state->info->tty->stopped && !state->info->tty->hw_stopped))
891 result &= ~TIOCSER_TEMT;
Alan Coxa46c9992008-02-08 04:18:53 -0800892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return put_user(result, value);
894}
895
896static int uart_tiocmget(struct tty_struct *tty, struct file *file)
897{
898 struct uart_state *state = tty->driver_data;
899 struct uart_port *port = state->port;
900 int result = -EIO;
901
Ingo Molnare2862f62006-01-13 21:37:07 +0000902 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if ((!file || !tty_hung_up_p(file)) &&
904 !(tty->flags & (1 << TTY_IO_ERROR))) {
905 result = port->mctrl;
Russell Kingc5f46442005-06-29 09:42:38 +0100906
907 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 result |= port->ops->get_mctrl(port);
Russell Kingc5f46442005-06-29 09:42:38 +0100909 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000911 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 return result;
914}
915
916static int
917uart_tiocmset(struct tty_struct *tty, struct file *file,
918 unsigned int set, unsigned int clear)
919{
920 struct uart_state *state = tty->driver_data;
921 struct uart_port *port = state->port;
922 int ret = -EIO;
923
Ingo Molnare2862f62006-01-13 21:37:07 +0000924 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if ((!file || !tty_hung_up_p(file)) &&
926 !(tty->flags & (1 << TTY_IO_ERROR))) {
927 uart_update_mctrl(port, set, clear);
928 ret = 0;
929 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000930 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return ret;
932}
933
934static void uart_break_ctl(struct tty_struct *tty, int break_state)
935{
936 struct uart_state *state = tty->driver_data;
937 struct uart_port *port = state->port;
938
Ingo Molnare2862f62006-01-13 21:37:07 +0000939 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 if (port->type != PORT_UNKNOWN)
942 port->ops->break_ctl(port, break_state);
943
Ingo Molnare2862f62006-01-13 21:37:07 +0000944 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947static int uart_do_autoconfig(struct uart_state *state)
948{
949 struct uart_port *port = state->port;
950 int flags, ret;
951
952 if (!capable(CAP_SYS_ADMIN))
953 return -EPERM;
954
955 /*
956 * Take the per-port semaphore. This prevents count from
957 * changing, and hence any extra opens of the port while
958 * we're auto-configuring.
959 */
Ingo Molnare2862f62006-01-13 21:37:07 +0000960 if (mutex_lock_interruptible(&state->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return -ERESTARTSYS;
962
963 ret = -EBUSY;
964 if (uart_users(state) == 1) {
965 uart_shutdown(state);
966
967 /*
968 * If we already have a port type configured,
969 * we must release its resources.
970 */
971 if (port->type != PORT_UNKNOWN)
972 port->ops->release_port(port);
973
974 flags = UART_CONFIG_TYPE;
975 if (port->flags & UPF_AUTO_IRQ)
976 flags |= UART_CONFIG_IRQ;
977
978 /*
979 * This will claim the ports resources if
980 * a port is found.
981 */
982 port->ops->config_port(port, flags);
983
984 ret = uart_startup(state, 1);
985 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000986 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return ret;
988}
989
990/*
991 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
992 * - mask passed in arg for lines of interest
993 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
994 * Caller should use TIOCGICOUNT to see which one it was
995 */
996static int
997uart_wait_modem_status(struct uart_state *state, unsigned long arg)
998{
999 struct uart_port *port = state->port;
1000 DECLARE_WAITQUEUE(wait, current);
1001 struct uart_icount cprev, cnow;
1002 int ret;
1003
1004 /*
1005 * note the counters on entry
1006 */
1007 spin_lock_irq(&port->lock);
1008 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
1009
1010 /*
1011 * Force modem status interrupts on
1012 */
1013 port->ops->enable_ms(port);
1014 spin_unlock_irq(&port->lock);
1015
1016 add_wait_queue(&state->info->delta_msr_wait, &wait);
1017 for (;;) {
1018 spin_lock_irq(&port->lock);
1019 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1020 spin_unlock_irq(&port->lock);
1021
1022 set_current_state(TASK_INTERRUPTIBLE);
1023
1024 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1025 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1026 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1027 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Alan Coxa46c9992008-02-08 04:18:53 -08001028 ret = 0;
1029 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031
1032 schedule();
1033
1034 /* see if a signal did it */
1035 if (signal_pending(current)) {
1036 ret = -ERESTARTSYS;
1037 break;
1038 }
1039
1040 cprev = cnow;
1041 }
1042
1043 current->state = TASK_RUNNING;
1044 remove_wait_queue(&state->info->delta_msr_wait, &wait);
1045
1046 return ret;
1047}
1048
1049/*
1050 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1051 * Return: write counters to the user passed counter struct
1052 * NB: both 1->0 and 0->1 transitions are counted except for
1053 * RI where only 0->1 is counted.
1054 */
1055static int uart_get_count(struct uart_state *state,
1056 struct serial_icounter_struct __user *icnt)
1057{
1058 struct serial_icounter_struct icount;
1059 struct uart_icount cnow;
1060 struct uart_port *port = state->port;
1061
1062 spin_lock_irq(&port->lock);
1063 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1064 spin_unlock_irq(&port->lock);
1065
1066 icount.cts = cnow.cts;
1067 icount.dsr = cnow.dsr;
1068 icount.rng = cnow.rng;
1069 icount.dcd = cnow.dcd;
1070 icount.rx = cnow.rx;
1071 icount.tx = cnow.tx;
1072 icount.frame = cnow.frame;
1073 icount.overrun = cnow.overrun;
1074 icount.parity = cnow.parity;
1075 icount.brk = cnow.brk;
1076 icount.buf_overrun = cnow.buf_overrun;
1077
1078 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1079}
1080
1081/*
Alan Coxe5238442008-04-30 00:53:28 -07001082 * Called via sys_ioctl. We can use spin_lock_irq() here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 */
1084static int
1085uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1086 unsigned long arg)
1087{
1088 struct uart_state *state = tty->driver_data;
1089 void __user *uarg = (void __user *)arg;
1090 int ret = -ENOIOCTLCMD;
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 /*
1094 * These ioctls don't rely on the hardware to be present.
1095 */
1096 switch (cmd) {
1097 case TIOCGSERIAL:
1098 ret = uart_get_info(state, uarg);
1099 break;
1100
1101 case TIOCSSERIAL:
1102 ret = uart_set_info(state, uarg);
1103 break;
1104
1105 case TIOCSERCONFIG:
1106 ret = uart_do_autoconfig(state);
1107 break;
1108
1109 case TIOCSERGWILD: /* obsolete */
1110 case TIOCSERSWILD: /* obsolete */
1111 ret = 0;
1112 break;
1113 }
1114
1115 if (ret != -ENOIOCTLCMD)
1116 goto out;
1117
1118 if (tty->flags & (1 << TTY_IO_ERROR)) {
1119 ret = -EIO;
1120 goto out;
1121 }
1122
1123 /*
1124 * The following should only be used when hardware is present.
1125 */
1126 switch (cmd) {
1127 case TIOCMIWAIT:
1128 ret = uart_wait_modem_status(state, arg);
1129 break;
1130
1131 case TIOCGICOUNT:
1132 ret = uart_get_count(state, uarg);
1133 break;
1134 }
1135
1136 if (ret != -ENOIOCTLCMD)
1137 goto out;
1138
Ingo Molnare2862f62006-01-13 21:37:07 +00001139 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 if (tty_hung_up_p(filp)) {
1142 ret = -EIO;
1143 goto out_up;
1144 }
1145
1146 /*
1147 * All these rely on hardware being present and need to be
1148 * protected against the tty being hung up.
1149 */
1150 switch (cmd) {
1151 case TIOCSERGETLSR: /* Get line status register */
1152 ret = uart_get_lsr_info(state, uarg);
1153 break;
1154
1155 default: {
1156 struct uart_port *port = state->port;
1157 if (port->ops->ioctl)
1158 ret = port->ops->ioctl(port, cmd, arg);
1159 break;
1160 }
1161 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001162out_up:
Ingo Molnare2862f62006-01-13 21:37:07 +00001163 mutex_unlock(&state->mutex);
Alan Coxf34d7a52008-04-30 00:54:13 -07001164out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return ret;
1166}
1167
Alan Coxa46c9992008-02-08 04:18:53 -08001168static void uart_set_termios(struct tty_struct *tty,
1169 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 struct uart_state *state = tty->driver_data;
1172 unsigned long flags;
1173 unsigned int cflag = tty->termios->c_cflag;
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 /*
1177 * These are the bits that are used to setup various
David Woodhouse20620d62007-08-22 14:01:11 -07001178 * flags in the low level driver. We can ignore the Bfoo
1179 * bits in c_cflag; c_[io]speed will always be set
1180 * appropriately by set_termios() in tty_ioctl.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 */
1182#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if ((cflag ^ old_termios->c_cflag) == 0 &&
David Woodhouse20620d62007-08-22 14:01:11 -07001184 tty->termios->c_ospeed == old_termios->c_ospeed &&
1185 tty->termios->c_ispeed == old_termios->c_ispeed &&
Alan Coxe5238442008-04-30 00:53:28 -07001186 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return;
Alan Coxe5238442008-04-30 00:53:28 -07001188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 uart_change_speed(state, old_termios);
1191
1192 /* Handle transition to B0 status */
1193 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1194 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1195
1196 /* Handle transition away from B0 status */
1197 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1198 unsigned int mask = TIOCM_DTR;
1199 if (!(cflag & CRTSCTS) ||
1200 !test_bit(TTY_THROTTLED, &tty->flags))
1201 mask |= TIOCM_RTS;
1202 uart_set_mctrl(state->port, mask);
1203 }
1204
1205 /* Handle turning off CRTSCTS */
1206 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1207 spin_lock_irqsave(&state->port->lock, flags);
1208 tty->hw_stopped = 0;
1209 __uart_start(tty);
1210 spin_unlock_irqrestore(&state->port->lock, flags);
1211 }
1212
Russell King0dd7a1a2005-06-29 18:40:53 +01001213 /* Handle turning on CRTSCTS */
1214 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1215 spin_lock_irqsave(&state->port->lock, flags);
1216 if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
1217 tty->hw_stopped = 1;
Russell Kingb129a8c2005-08-31 10:12:14 +01001218 state->port->ops->stop_tx(state->port);
Russell King0dd7a1a2005-06-29 18:40:53 +01001219 }
1220 spin_unlock_irqrestore(&state->port->lock, flags);
1221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222#if 0
1223 /*
1224 * No need to wake up processes in open wait, since they
1225 * sample the CLOCAL flag once, and don't recheck it.
1226 * XXX It's not clear whether the current behavior is correct
1227 * or not. Hence, this may change.....
1228 */
1229 if (!(old_termios->c_cflag & CLOCAL) &&
1230 (tty->termios->c_cflag & CLOCAL))
1231 wake_up_interruptible(&state->info->open_wait);
1232#endif
1233}
1234
1235/*
1236 * In 2.4.5, calls to this will be serialized via the BKL in
1237 * linux/drivers/char/tty_io.c:tty_release()
1238 * linux/drivers/char/tty_io.c:do_tty_handup()
1239 */
1240static void uart_close(struct tty_struct *tty, struct file *filp)
1241{
1242 struct uart_state *state = tty->driver_data;
1243 struct uart_port *port;
Alan Coxa46c9992008-02-08 04:18:53 -08001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 BUG_ON(!kernel_locked());
1246
1247 if (!state || !state->port)
1248 return;
1249
1250 port = state->port;
1251
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001252 pr_debug("uart_close(%d) called\n", port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Ingo Molnare2862f62006-01-13 21:37:07 +00001254 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 if (tty_hung_up_p(filp))
1257 goto done;
1258
1259 if ((tty->count == 1) && (state->count != 1)) {
1260 /*
1261 * Uh, oh. tty->count is 1, which means that the tty
1262 * structure will be freed. state->count should always
1263 * be one in these conditions. If it's greater than
1264 * one, we've got real problems, since it means the
1265 * serial port won't be shutdown.
1266 */
1267 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1268 "state->count is %d\n", state->count);
1269 state->count = 1;
1270 }
1271 if (--state->count < 0) {
1272 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1273 tty->name, state->count);
1274 state->count = 0;
1275 }
1276 if (state->count)
1277 goto done;
1278
1279 /*
1280 * Now we wait for the transmit buffer to clear; and we notify
1281 * the line discipline to only process XON/XOFF characters by
1282 * setting tty->closing.
1283 */
1284 tty->closing = 1;
1285
1286 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1287 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1288
1289 /*
1290 * At this point, we stop accepting input. To do this, we
1291 * disable the receive line status interrupts.
1292 */
1293 if (state->info->flags & UIF_INITIALIZED) {
1294 unsigned long flags;
1295 spin_lock_irqsave(&port->lock, flags);
1296 port->ops->stop_rx(port);
1297 spin_unlock_irqrestore(&port->lock, flags);
1298 /*
1299 * Before we drop DTR, make sure the UART transmitter
1300 * has completely drained; this is especially
1301 * important if there is a transmit FIFO!
1302 */
1303 uart_wait_until_sent(tty, port->timeout);
1304 }
1305
1306 uart_shutdown(state);
1307 uart_flush_buffer(tty);
1308
Alan Coxa46c9992008-02-08 04:18:53 -08001309 tty_ldisc_flush(tty);
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 tty->closing = 0;
1312 state->info->tty = NULL;
1313
1314 if (state->info->blocked_open) {
1315 if (state->close_delay)
1316 msleep_interruptible(state->close_delay);
1317 } else if (!uart_console(port)) {
1318 uart_change_pm(state, 3);
1319 }
1320
1321 /*
1322 * Wake up anyone trying to open this port.
1323 */
1324 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1325 wake_up_interruptible(&state->info->open_wait);
1326
1327 done:
Ingo Molnare2862f62006-01-13 21:37:07 +00001328 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
1331static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1332{
1333 struct uart_state *state = tty->driver_data;
1334 struct uart_port *port = state->port;
1335 unsigned long char_time, expire;
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1338 return;
1339
Alan Coxf34d7a52008-04-30 00:54:13 -07001340 lock_kernel();
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 /*
1343 * Set the check interval to be 1/5 of the estimated time to
1344 * send a single character, and make it at least 1. The check
1345 * interval should also be less than the timeout.
1346 *
1347 * Note: we have to use pretty tight timings here to satisfy
1348 * the NIST-PCTS.
1349 */
1350 char_time = (port->timeout - HZ/50) / port->fifosize;
1351 char_time = char_time / 5;
1352 if (char_time == 0)
1353 char_time = 1;
1354 if (timeout && timeout < char_time)
1355 char_time = timeout;
1356
1357 /*
1358 * If the transmitter hasn't cleared in twice the approximate
1359 * amount of time to send the entire FIFO, it probably won't
1360 * ever clear. This assumes the UART isn't doing flow
1361 * control, which is currently the case. Hence, if it ever
1362 * takes longer than port->timeout, this is probably due to a
1363 * UART bug of some kind. So, we clamp the timeout parameter at
1364 * 2*port->timeout.
1365 */
1366 if (timeout == 0 || timeout > 2 * port->timeout)
1367 timeout = 2 * port->timeout;
1368
1369 expire = jiffies + timeout;
1370
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001371 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
Alan Coxa46c9992008-02-08 04:18:53 -08001372 port->line, jiffies, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 /*
1375 * Check whether the transmitter is empty every 'char_time'.
1376 * 'timeout' / 'expire' give us the maximum amount of time
1377 * we wait.
1378 */
1379 while (!port->ops->tx_empty(port)) {
1380 msleep_interruptible(jiffies_to_msecs(char_time));
1381 if (signal_pending(current))
1382 break;
1383 if (time_after(jiffies, expire))
1384 break;
1385 }
1386 set_current_state(TASK_RUNNING); /* might not be needed */
Alan Coxf34d7a52008-04-30 00:54:13 -07001387 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
1390/*
1391 * This is called with the BKL held in
1392 * linux/drivers/char/tty_io.c:do_tty_hangup()
1393 * We're called from the eventd thread, so we can sleep for
1394 * a _short_ time only.
1395 */
1396static void uart_hangup(struct tty_struct *tty)
1397{
1398 struct uart_state *state = tty->driver_data;
1399
1400 BUG_ON(!kernel_locked());
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001401 pr_debug("uart_hangup(%d)\n", state->port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Ingo Molnare2862f62006-01-13 21:37:07 +00001403 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1405 uart_flush_buffer(tty);
1406 uart_shutdown(state);
1407 state->count = 0;
1408 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1409 state->info->tty = NULL;
1410 wake_up_interruptible(&state->info->open_wait);
1411 wake_up_interruptible(&state->info->delta_msr_wait);
1412 }
Ingo Molnare2862f62006-01-13 21:37:07 +00001413 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
1416/*
1417 * Copy across the serial console cflag setting into the termios settings
1418 * for the initial open of the port. This allows continuity between the
1419 * kernel settings, and the settings init adopts when it opens the port
1420 * for the first time.
1421 */
1422static void uart_update_termios(struct uart_state *state)
1423{
1424 struct tty_struct *tty = state->info->tty;
1425 struct uart_port *port = state->port;
1426
1427 if (uart_console(port) && port->cons->cflag) {
1428 tty->termios->c_cflag = port->cons->cflag;
1429 port->cons->cflag = 0;
1430 }
1431
1432 /*
1433 * If the device failed to grab its irq resources,
1434 * or some other error occurred, don't try to talk
1435 * to the port hardware.
1436 */
1437 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1438 /*
1439 * Make termios settings take effect.
1440 */
1441 uart_change_speed(state, NULL);
1442
1443 /*
1444 * And finally enable the RTS and DTR signals.
1445 */
1446 if (tty->termios->c_cflag & CBAUD)
1447 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1448 }
1449}
1450
1451/*
1452 * Block the open until the port is ready. We must be called with
1453 * the per-port semaphore held.
1454 */
1455static int
1456uart_block_til_ready(struct file *filp, struct uart_state *state)
1457{
1458 DECLARE_WAITQUEUE(wait, current);
1459 struct uart_info *info = state->info;
1460 struct uart_port *port = state->port;
Russell Kingc5f46442005-06-29 09:42:38 +01001461 unsigned int mctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 info->blocked_open++;
1464 state->count--;
1465
1466 add_wait_queue(&info->open_wait, &wait);
1467 while (1) {
1468 set_current_state(TASK_INTERRUPTIBLE);
1469
1470 /*
1471 * If we have been hung up, tell userspace/restart open.
1472 */
1473 if (tty_hung_up_p(filp) || info->tty == NULL)
1474 break;
1475
1476 /*
1477 * If the port has been closed, tell userspace/restart open.
1478 */
1479 if (!(info->flags & UIF_INITIALIZED))
1480 break;
1481
1482 /*
1483 * If non-blocking mode is set, or CLOCAL mode is set,
1484 * we don't want to wait for the modem status lines to
1485 * indicate that the port is ready.
1486 *
1487 * Also, if the port is not enabled/configured, we want
1488 * to allow the open to succeed here. Note that we will
1489 * have set TTY_IO_ERROR for a non-existant port.
1490 */
1491 if ((filp->f_flags & O_NONBLOCK) ||
Alan Coxa46c9992008-02-08 04:18:53 -08001492 (info->tty->termios->c_cflag & CLOCAL) ||
1493 (info->tty->flags & (1 << TTY_IO_ERROR)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 /*
1497 * Set DTR to allow modem to know we're waiting. Do
1498 * not set RTS here - we want to make sure we catch
1499 * the data from the modem.
1500 */
1501 if (info->tty->termios->c_cflag & CBAUD)
1502 uart_set_mctrl(port, TIOCM_DTR);
1503
1504 /*
1505 * and wait for the carrier to indicate that the
1506 * modem is ready for us.
1507 */
Russell Kingc5f46442005-06-29 09:42:38 +01001508 spin_lock_irq(&port->lock);
Russell Kingf61051c2006-01-07 23:11:23 +00001509 port->ops->enable_ms(port);
Russell Kingc5f46442005-06-29 09:42:38 +01001510 mctrl = port->ops->get_mctrl(port);
1511 spin_unlock_irq(&port->lock);
1512 if (mctrl & TIOCM_CAR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 break;
1514
Ingo Molnare2862f62006-01-13 21:37:07 +00001515 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 schedule();
Ingo Molnare2862f62006-01-13 21:37:07 +00001517 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 if (signal_pending(current))
1520 break;
1521 }
1522 set_current_state(TASK_RUNNING);
1523 remove_wait_queue(&info->open_wait, &wait);
1524
1525 state->count++;
1526 info->blocked_open--;
1527
1528 if (signal_pending(current))
1529 return -ERESTARTSYS;
1530
1531 if (!info->tty || tty_hung_up_p(filp))
1532 return -EAGAIN;
1533
1534 return 0;
1535}
1536
1537static struct uart_state *uart_get(struct uart_driver *drv, int line)
1538{
1539 struct uart_state *state;
Russell King68ac64c2006-04-30 11:13:50 +01001540 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 state = drv->state + line;
Ingo Molnare2862f62006-01-13 21:37:07 +00001543 if (mutex_lock_interruptible(&state->mutex)) {
Russell King68ac64c2006-04-30 11:13:50 +01001544 ret = -ERESTARTSYS;
1545 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
1547
1548 state->count++;
Russell King68ac64c2006-04-30 11:13:50 +01001549 if (!state->port || state->port->flags & UPF_DEAD) {
1550 ret = -ENXIO;
1551 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553
1554 if (!state->info) {
Burman Yan8f31bb32007-02-14 00:33:07 -08001555 state->info = kzalloc(sizeof(struct uart_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (state->info) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 init_waitqueue_head(&state->info->open_wait);
1558 init_waitqueue_head(&state->info->delta_msr_wait);
1559
1560 /*
1561 * Link the info into the other structures.
1562 */
1563 state->port->info = state->info;
1564
1565 tasklet_init(&state->info->tlet, uart_tasklet_action,
1566 (unsigned long)state);
1567 } else {
Russell King68ac64c2006-04-30 11:13:50 +01001568 ret = -ENOMEM;
1569 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return state;
Russell King68ac64c2006-04-30 11:13:50 +01001573
1574 err_unlock:
1575 state->count--;
1576 mutex_unlock(&state->mutex);
1577 err:
1578 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
1581/*
Denis Cheng922f9cf2008-02-08 04:22:00 -08001582 * calls to uart_open are serialised by the BKL in
1583 * fs/char_dev.c:chrdev_open()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 * Note that if this fails, then uart_close() _will_ be called.
1585 *
1586 * In time, we want to scrap the "opening nonpresent ports"
1587 * behaviour and implement an alternative way for setserial
1588 * to set base addresses/ports/types. This will allow us to
1589 * get rid of a certain amount of extra tests.
1590 */
1591static int uart_open(struct tty_struct *tty, struct file *filp)
1592{
1593 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1594 struct uart_state *state;
1595 int retval, line = tty->index;
1596
1597 BUG_ON(!kernel_locked());
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001598 pr_debug("uart_open(%d) called\n", line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 /*
1601 * tty->driver->num won't change, so we won't fail here with
1602 * tty->driver_data set to something non-NULL (and therefore
1603 * we won't get caught by uart_close()).
1604 */
1605 retval = -ENODEV;
1606 if (line >= tty->driver->num)
1607 goto fail;
1608
1609 /*
1610 * We take the semaphore inside uart_get to guarantee that we won't
1611 * be re-entered while allocating the info structure, or while we
1612 * request any IRQs that the driver may need. This also has the nice
1613 * side-effect that it delays the action of uart_hangup, so we can
1614 * guarantee that info->tty will always contain something reasonable.
1615 */
1616 state = uart_get(drv, line);
1617 if (IS_ERR(state)) {
1618 retval = PTR_ERR(state);
1619 goto fail;
1620 }
1621
1622 /*
1623 * Once we set tty->driver_data here, we are guaranteed that
1624 * uart_close() will decrement the driver module use count.
1625 * Any failures from here onwards should not touch the count.
1626 */
1627 tty->driver_data = state;
1628 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1629 tty->alt_speed = 0;
1630 state->info->tty = tty;
1631
1632 /*
1633 * If the port is in the middle of closing, bail out now.
1634 */
1635 if (tty_hung_up_p(filp)) {
1636 retval = -EAGAIN;
1637 state->count--;
Ingo Molnare2862f62006-01-13 21:37:07 +00001638 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 goto fail;
1640 }
1641
1642 /*
1643 * Make sure the device is in D0 state.
1644 */
1645 if (state->count == 1)
1646 uart_change_pm(state, 0);
1647
1648 /*
1649 * Start up the serial port.
1650 */
1651 retval = uart_startup(state, 0);
1652
1653 /*
1654 * If we succeeded, wait until the port is ready.
1655 */
1656 if (retval == 0)
1657 retval = uart_block_til_ready(filp, state);
Ingo Molnare2862f62006-01-13 21:37:07 +00001658 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 /*
1661 * If this is the first open to succeed, adjust things to suit.
1662 */
1663 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1664 state->info->flags |= UIF_NORMAL_ACTIVE;
1665
1666 uart_update_termios(state);
1667 }
1668
1669 fail:
1670 return retval;
1671}
1672
1673static const char *uart_type(struct uart_port *port)
1674{
1675 const char *str = NULL;
1676
1677 if (port->ops->type)
1678 str = port->ops->type(port);
1679
1680 if (!str)
1681 str = "unknown";
1682
1683 return str;
1684}
1685
1686#ifdef CONFIG_PROC_FS
1687
1688static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1689{
1690 struct uart_state *state = drv->state + i;
George G. Davis3689a0e2007-02-14 00:33:06 -08001691 int pm_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 struct uart_port *port = state->port;
1693 char stat_buf[32];
1694 unsigned int status;
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001695 int mmio, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 if (!port)
1698 return 0;
1699
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001700 mmio = port->iotype >= UPIO_MEM;
Josh Boyer4f640ef2007-07-23 18:43:44 -07001701 ret = sprintf(buf, "%d: uart:%s %s%08llX irq:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 port->line, uart_type(port),
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001703 mmio ? "mmio:0x" : "port:",
Josh Boyer4f640ef2007-07-23 18:43:44 -07001704 mmio ? (unsigned long long)port->mapbase
Alan Coxa46c9992008-02-08 04:18:53 -08001705 : (unsigned long long) port->iobase,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 port->irq);
1707
1708 if (port->type == PORT_UNKNOWN) {
1709 strcat(buf, "\n");
1710 return ret + 1;
1711 }
1712
Alan Coxa46c9992008-02-08 04:18:53 -08001713 if (capable(CAP_SYS_ADMIN)) {
George G. Davis3689a0e2007-02-14 00:33:06 -08001714 mutex_lock(&state->mutex);
1715 pm_state = state->pm_state;
1716 if (pm_state)
1717 uart_change_pm(state, 0);
Russell Kingc5f46442005-06-29 09:42:38 +01001718 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 status = port->ops->get_mctrl(port);
Russell Kingc5f46442005-06-29 09:42:38 +01001720 spin_unlock_irq(&port->lock);
George G. Davis3689a0e2007-02-14 00:33:06 -08001721 if (pm_state)
1722 uart_change_pm(state, pm_state);
1723 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 ret += sprintf(buf + ret, " tx:%d rx:%d",
1726 port->icount.tx, port->icount.rx);
1727 if (port->icount.frame)
1728 ret += sprintf(buf + ret, " fe:%d",
1729 port->icount.frame);
1730 if (port->icount.parity)
1731 ret += sprintf(buf + ret, " pe:%d",
1732 port->icount.parity);
1733 if (port->icount.brk)
1734 ret += sprintf(buf + ret, " brk:%d",
1735 port->icount.brk);
1736 if (port->icount.overrun)
1737 ret += sprintf(buf + ret, " oe:%d",
1738 port->icount.overrun);
Alan Coxa46c9992008-02-08 04:18:53 -08001739
1740#define INFOBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if (port->mctrl & (bit)) \
1742 strncat(stat_buf, (str), sizeof(stat_buf) - \
1743 strlen(stat_buf) - 2)
Alan Coxa46c9992008-02-08 04:18:53 -08001744#define STATBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (status & (bit)) \
1746 strncat(stat_buf, (str), sizeof(stat_buf) - \
1747 strlen(stat_buf) - 2)
1748
1749 stat_buf[0] = '\0';
1750 stat_buf[1] = '\0';
1751 INFOBIT(TIOCM_RTS, "|RTS");
1752 STATBIT(TIOCM_CTS, "|CTS");
1753 INFOBIT(TIOCM_DTR, "|DTR");
1754 STATBIT(TIOCM_DSR, "|DSR");
1755 STATBIT(TIOCM_CAR, "|CD");
1756 STATBIT(TIOCM_RNG, "|RI");
1757 if (stat_buf[0])
1758 stat_buf[0] = ' ';
1759 strcat(stat_buf, "\n");
Alan Coxa46c9992008-02-08 04:18:53 -08001760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 ret += sprintf(buf + ret, stat_buf);
1762 } else {
1763 strcat(buf, "\n");
1764 ret++;
1765 }
1766#undef STATBIT
1767#undef INFOBIT
1768 return ret;
1769}
1770
1771static int uart_read_proc(char *page, char **start, off_t off,
1772 int count, int *eof, void *data)
1773{
1774 struct tty_driver *ttydrv = data;
1775 struct uart_driver *drv = ttydrv->driver_state;
1776 int i, len = 0, l;
1777 off_t begin = 0;
1778
1779 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1780 "", "", "");
1781 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1782 l = uart_line_info(page + len, drv, i);
1783 len += l;
1784 if (len + begin > off + count)
1785 goto done;
1786 if (len + begin < off) {
1787 begin += len;
1788 len = 0;
1789 }
1790 }
1791 *eof = 1;
1792 done:
1793 if (off >= len + begin)
1794 return 0;
1795 *start = page + (off - begin);
1796 return (count < begin + len - off) ? count : (begin + len - off);
1797}
1798#endif
1799
Andrew Morton4a1b5502008-03-07 15:51:16 -08001800#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801/*
Russell Kingd3587882006-03-20 20:00:09 +00001802 * uart_console_write - write a console message to a serial port
1803 * @port: the port to write the message
1804 * @s: array of characters
1805 * @count: number of characters in string to write
1806 * @write: function to write character to port
1807 */
1808void uart_console_write(struct uart_port *port, const char *s,
1809 unsigned int count,
1810 void (*putchar)(struct uart_port *, int))
1811{
1812 unsigned int i;
1813
1814 for (i = 0; i < count; i++, s++) {
1815 if (*s == '\n')
1816 putchar(port, '\r');
1817 putchar(port, *s);
1818 }
1819}
1820EXPORT_SYMBOL_GPL(uart_console_write);
1821
1822/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 * Check whether an invalid uart number has been specified, and
1824 * if so, search for the first available port that does have
1825 * console support.
1826 */
1827struct uart_port * __init
1828uart_get_console(struct uart_port *ports, int nr, struct console *co)
1829{
1830 int idx = co->index;
1831
1832 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1833 ports[idx].membase == NULL))
1834 for (idx = 0; idx < nr; idx++)
1835 if (ports[idx].iobase != 0 ||
1836 ports[idx].membase != NULL)
1837 break;
1838
1839 co->index = idx;
1840
1841 return ports + idx;
1842}
1843
1844/**
1845 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1846 * @options: pointer to option string
1847 * @baud: pointer to an 'int' variable for the baud rate.
1848 * @parity: pointer to an 'int' variable for the parity.
1849 * @bits: pointer to an 'int' variable for the number of data bits.
1850 * @flow: pointer to an 'int' variable for the flow control character.
1851 *
1852 * uart_parse_options decodes a string containing the serial console
1853 * options. The format of the string is <baud><parity><bits><flow>,
1854 * eg: 115200n8r
1855 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001856void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1858{
1859 char *s = options;
1860
1861 *baud = simple_strtoul(s, NULL, 10);
1862 while (*s >= '0' && *s <= '9')
1863 s++;
1864 if (*s)
1865 *parity = *s++;
1866 if (*s)
1867 *bits = *s++ - '0';
1868 if (*s)
1869 *flow = *s;
1870}
Jason Wesself2d937f2008-04-17 20:05:37 +02001871EXPORT_SYMBOL_GPL(uart_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873struct baud_rates {
1874 unsigned int rate;
1875 unsigned int cflag;
1876};
1877
Arjan van de Vencb3592b2005-11-28 21:04:11 +00001878static const struct baud_rates baud_rates[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 { 921600, B921600 },
1880 { 460800, B460800 },
1881 { 230400, B230400 },
1882 { 115200, B115200 },
1883 { 57600, B57600 },
1884 { 38400, B38400 },
1885 { 19200, B19200 },
1886 { 9600, B9600 },
1887 { 4800, B4800 },
1888 { 2400, B2400 },
1889 { 1200, B1200 },
1890 { 0, B38400 }
1891};
1892
1893/**
1894 * uart_set_options - setup the serial console parameters
1895 * @port: pointer to the serial ports uart_port structure
1896 * @co: console pointer
1897 * @baud: baud rate
1898 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1899 * @bits: number of data bits
1900 * @flow: flow control character - 'r' (rts)
1901 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001902int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903uart_set_options(struct uart_port *port, struct console *co,
1904 int baud, int parity, int bits, int flow)
1905{
Alan Cox606d0992006-12-08 02:38:45 -08001906 struct ktermios termios;
Alan Cox149b36e2007-10-18 01:24:16 -07001907 static struct ktermios dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 int i;
1909
Russell King976ecd12005-07-03 21:05:45 +01001910 /*
1911 * Ensure that the serial console lock is initialised
1912 * early.
1913 */
1914 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07001915 lockdep_set_class(&port->lock, &port_lock_key);
Russell King976ecd12005-07-03 21:05:45 +01001916
Alan Cox606d0992006-12-08 02:38:45 -08001917 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1920
1921 /*
1922 * Construct a cflag setting.
1923 */
1924 for (i = 0; baud_rates[i].rate; i++)
1925 if (baud_rates[i].rate <= baud)
1926 break;
1927
1928 termios.c_cflag |= baud_rates[i].cflag;
1929
1930 if (bits == 7)
1931 termios.c_cflag |= CS7;
1932 else
1933 termios.c_cflag |= CS8;
1934
1935 switch (parity) {
1936 case 'o': case 'O':
1937 termios.c_cflag |= PARODD;
1938 /*fall through*/
1939 case 'e': case 'E':
1940 termios.c_cflag |= PARENB;
1941 break;
1942 }
1943
1944 if (flow == 'r')
1945 termios.c_cflag |= CRTSCTS;
1946
Yinghai Lu79492682007-07-15 23:37:25 -07001947 /*
1948 * some uarts on other side don't support no flow control.
1949 * So we set * DTR in host uart to make them happy
1950 */
1951 port->mctrl |= TIOCM_DTR;
1952
Alan Cox149b36e2007-10-18 01:24:16 -07001953 port->ops->set_termios(port, &termios, &dummy);
Jason Wesself2d937f2008-04-17 20:05:37 +02001954 /*
1955 * Allow the setting of the UART parameters with a NULL console
1956 * too:
1957 */
1958 if (co)
1959 co->cflag = termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 return 0;
1962}
Jason Wesself2d937f2008-04-17 20:05:37 +02001963EXPORT_SYMBOL_GPL(uart_set_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964#endif /* CONFIG_SERIAL_CORE_CONSOLE */
1965
1966static void uart_change_pm(struct uart_state *state, int pm_state)
1967{
1968 struct uart_port *port = state->port;
Andrew Victor1281e362006-05-16 11:28:49 +01001969
1970 if (state->pm_state != pm_state) {
1971 if (port->ops->pm)
1972 port->ops->pm(port, pm_state, state->pm_state);
1973 state->pm_state = pm_state;
1974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
1976
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001977struct uart_match {
1978 struct uart_port *port;
1979 struct uart_driver *driver;
1980};
1981
1982static int serial_match_port(struct device *dev, void *data)
1983{
1984 struct uart_match *match = data;
1985 dev_t devt = MKDEV(match->driver->major, match->driver->minor) + match->port->line;
1986
1987 return dev->devt == devt; /* Actually, only one tty per port */
1988}
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1991{
1992 struct uart_state *state = drv->state + port->line;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001993 struct device *tty_dev;
1994 struct uart_match match = {port, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Ingo Molnare2862f62006-01-13 21:37:07 +00001996 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001998 if (!console_suspend_enabled && uart_console(port)) {
1999 /* we're going to avoid suspending serial console */
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002000 mutex_unlock(&state->mutex);
2001 return 0;
2002 }
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002003
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002004 tty_dev = device_find_child(port->dev, &match, serial_match_port);
2005 if (device_may_wakeup(tty_dev)) {
2006 enable_irq_wake(port->irq);
2007 put_device(tty_dev);
2008 mutex_unlock(&state->mutex);
2009 return 0;
2010 }
2011 port->suspended = 1;
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (state->info && state->info->flags & UIF_INITIALIZED) {
Russell Kingba899db2006-01-21 22:45:50 +00002014 const struct uart_ops *ops = port->ops;
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002015 int tries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Russell Kinga6b93a92006-10-01 17:17:40 +01002017 state->info->flags = (state->info->flags & ~UIF_INITIALIZED)
2018 | UIF_SUSPENDED;
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 spin_lock_irq(&port->lock);
Russell Kingb129a8c2005-08-31 10:12:14 +01002021 ops->stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 ops->set_mctrl(port, 0);
2023 ops->stop_rx(port);
2024 spin_unlock_irq(&port->lock);
2025
2026 /*
2027 * Wait for the transmitter to empty.
2028 */
Alan Coxa46c9992008-02-08 04:18:53 -08002029 for (tries = 3; !ops->tx_empty(port) && tries; tries--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 msleep(10);
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002031 if (!tries)
Alan Coxa46c9992008-02-08 04:18:53 -08002032 printk(KERN_ERR "%s%s%s%d: Unable to drain "
2033 "transmitter\n",
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002034 port->dev ? port->dev->bus_id : "",
2035 port->dev ? ": " : "",
2036 drv->dev_name, port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 ops->shutdown(port);
2039 }
2040
2041 /*
2042 * Disable the console device before suspending.
2043 */
2044 if (uart_console(port))
2045 console_stop(port->cons);
2046
2047 uart_change_pm(state, 3);
2048
Ingo Molnare2862f62006-01-13 21:37:07 +00002049 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 return 0;
2052}
2053
2054int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
2055{
2056 struct uart_state *state = drv->state + port->line;
2057
Ingo Molnare2862f62006-01-13 21:37:07 +00002058 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002060 if (!console_suspend_enabled && uart_console(port)) {
2061 /* no need to resume serial console, it wasn't suspended */
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002062 mutex_unlock(&state->mutex);
2063 return 0;
2064 }
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002065
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002066 if (!port->suspended) {
2067 disable_irq_wake(port->irq);
2068 mutex_unlock(&state->mutex);
2069 return 0;
2070 }
2071 port->suspended = 0;
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 /*
2074 * Re-enable the console device after suspending.
2075 */
2076 if (uart_console(port)) {
Alan Cox606d0992006-12-08 02:38:45 -08002077 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079 /*
2080 * First try to use the console cflag setting.
2081 */
Alan Cox606d0992006-12-08 02:38:45 -08002082 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 termios.c_cflag = port->cons->cflag;
2084
2085 /*
2086 * If that's unset, use the tty termios setting.
2087 */
2088 if (state->info && state->info->tty && termios.c_cflag == 0)
2089 termios = *state->info->tty->termios;
2090
Russell King9d778a62008-02-04 22:27:51 -08002091 uart_change_pm(state, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 port->ops->set_termios(port, &termios, NULL);
2093 console_start(port->cons);
2094 }
2095
Russell Kinga6b93a92006-10-01 17:17:40 +01002096 if (state->info && state->info->flags & UIF_SUSPENDED) {
Russell Kingba899db2006-01-21 22:45:50 +00002097 const struct uart_ops *ops = port->ops;
Russell Kingee31b332005-11-13 15:28:51 +00002098 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Russell King9d778a62008-02-04 22:27:51 -08002100 uart_change_pm(state, 0);
Alan Coxf34d7a52008-04-30 00:54:13 -07002101 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 ops->set_mctrl(port, 0);
Alan Coxf34d7a52008-04-30 00:54:13 -07002103 spin_unlock_irq(&port->lock);
Russell Kingee31b332005-11-13 15:28:51 +00002104 ret = ops->startup(port);
2105 if (ret == 0) {
2106 uart_change_speed(state, NULL);
2107 spin_lock_irq(&port->lock);
2108 ops->set_mctrl(port, port->mctrl);
2109 ops->start_tx(port);
2110 spin_unlock_irq(&port->lock);
Russell Kinga6b93a92006-10-01 17:17:40 +01002111 state->info->flags |= UIF_INITIALIZED;
Russell Kingee31b332005-11-13 15:28:51 +00002112 } else {
2113 /*
2114 * Failed to resume - maybe hardware went away?
2115 * Clear the "initialized" flag so we won't try
2116 * to call the low level drivers shutdown method.
2117 */
Russell Kingee31b332005-11-13 15:28:51 +00002118 uart_shutdown(state);
2119 }
Russell Kinga6b93a92006-10-01 17:17:40 +01002120
2121 state->info->flags &= ~UIF_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 }
2123
Ingo Molnare2862f62006-01-13 21:37:07 +00002124 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 return 0;
2127}
2128
2129static inline void
2130uart_report_port(struct uart_driver *drv, struct uart_port *port)
2131{
Russell King30b7a3b2005-09-03 15:30:21 +01002132 char address[64];
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 switch (port->iotype) {
2135 case UPIO_PORT:
Russell King30b7a3b2005-09-03 15:30:21 +01002136 snprintf(address, sizeof(address),
2137 "I/O 0x%x", port->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 break;
2139 case UPIO_HUB6:
Russell King30b7a3b2005-09-03 15:30:21 +01002140 snprintf(address, sizeof(address),
2141 "I/O 0x%x offset 0x%x", port->iobase, port->hub6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 break;
2143 case UPIO_MEM:
2144 case UPIO_MEM32:
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002145 case UPIO_AU:
Zang Roy-r619113be91ec2006-06-30 02:29:58 -07002146 case UPIO_TSI:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002147 case UPIO_DWAPB:
Russell King30b7a3b2005-09-03 15:30:21 +01002148 snprintf(address, sizeof(address),
Josh Boyer4f640ef2007-07-23 18:43:44 -07002149 "MMIO 0x%llx", (unsigned long long)port->mapbase);
Russell King30b7a3b2005-09-03 15:30:21 +01002150 break;
2151 default:
2152 strlcpy(address, "*unknown*", sizeof(address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 break;
2154 }
Russell King30b7a3b2005-09-03 15:30:21 +01002155
Russell King0cf669d2005-10-31 11:42:22 +00002156 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2157 port->dev ? port->dev->bus_id : "",
2158 port->dev ? ": " : "",
Russell King30b7a3b2005-09-03 15:30:21 +01002159 drv->dev_name, port->line, address, port->irq, uart_type(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
2162static void
2163uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2164 struct uart_port *port)
2165{
2166 unsigned int flags;
2167
2168 /*
2169 * If there isn't a port here, don't do anything further.
2170 */
2171 if (!port->iobase && !port->mapbase && !port->membase)
2172 return;
2173
2174 /*
2175 * Now do the auto configuration stuff. Note that config_port
2176 * is expected to claim the resources and map the port for us.
2177 */
2178 flags = UART_CONFIG_TYPE;
2179 if (port->flags & UPF_AUTO_IRQ)
2180 flags |= UART_CONFIG_IRQ;
2181 if (port->flags & UPF_BOOT_AUTOCONF) {
2182 port->type = PORT_UNKNOWN;
2183 port->ops->config_port(port, flags);
2184 }
2185
2186 if (port->type != PORT_UNKNOWN) {
2187 unsigned long flags;
2188
2189 uart_report_port(drv, port);
2190
George G. Davis3689a0e2007-02-14 00:33:06 -08002191 /* Power up port for set_mctrl() */
2192 uart_change_pm(state, 0);
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 /*
2195 * Ensure that the modem control lines are de-activated.
Yinghai Luc3e46422008-02-04 22:27:46 -08002196 * keep the DTR setting that is set in uart_set_options()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 * We probably don't need a spinlock around this, but
2198 */
2199 spin_lock_irqsave(&port->lock, flags);
Yinghai Luc3e46422008-02-04 22:27:46 -08002200 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 spin_unlock_irqrestore(&port->lock, flags);
2202
2203 /*
Russell King97d97222007-09-01 21:25:09 +01002204 * If this driver supports console, and it hasn't been
2205 * successfully registered yet, try to re-register it.
2206 * It may be that the port was not available.
2207 */
2208 if (port->cons && !(port->cons->flags & CON_ENABLED))
2209 register_console(port->cons);
2210
2211 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 * Power down all ports by default, except the
2213 * console if we have one.
2214 */
2215 if (!uart_console(port))
2216 uart_change_pm(state, 3);
2217 }
2218}
2219
Jason Wesself2d937f2008-04-17 20:05:37 +02002220#ifdef CONFIG_CONSOLE_POLL
2221
2222static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2223{
2224 struct uart_driver *drv = driver->driver_state;
2225 struct uart_state *state = drv->state + line;
2226 struct uart_port *port;
2227 int baud = 9600;
2228 int bits = 8;
2229 int parity = 'n';
2230 int flow = 'n';
2231
2232 if (!state || !state->port)
2233 return -1;
2234
2235 port = state->port;
2236 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2237 return -1;
2238
2239 if (options) {
2240 uart_parse_options(options, &baud, &parity, &bits, &flow);
2241 return uart_set_options(port, NULL, baud, parity, bits, flow);
2242 }
2243
2244 return 0;
2245}
2246
2247static int uart_poll_get_char(struct tty_driver *driver, int line)
2248{
2249 struct uart_driver *drv = driver->driver_state;
2250 struct uart_state *state = drv->state + line;
2251 struct uart_port *port;
2252
2253 if (!state || !state->port)
2254 return -1;
2255
2256 port = state->port;
2257 return port->ops->poll_get_char(port);
2258}
2259
2260static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2261{
2262 struct uart_driver *drv = driver->driver_state;
2263 struct uart_state *state = drv->state + line;
2264 struct uart_port *port;
2265
2266 if (!state || !state->port)
2267 return;
2268
2269 port = state->port;
2270 port->ops->poll_put_char(port, ch);
2271}
2272#endif
2273
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002274static const struct tty_operations uart_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 .open = uart_open,
2276 .close = uart_close,
2277 .write = uart_write,
2278 .put_char = uart_put_char,
2279 .flush_chars = uart_flush_chars,
2280 .write_room = uart_write_room,
2281 .chars_in_buffer= uart_chars_in_buffer,
2282 .flush_buffer = uart_flush_buffer,
2283 .ioctl = uart_ioctl,
2284 .throttle = uart_throttle,
2285 .unthrottle = uart_unthrottle,
2286 .send_xchar = uart_send_xchar,
2287 .set_termios = uart_set_termios,
2288 .stop = uart_stop,
2289 .start = uart_start,
2290 .hangup = uart_hangup,
2291 .break_ctl = uart_break_ctl,
2292 .wait_until_sent= uart_wait_until_sent,
2293#ifdef CONFIG_PROC_FS
2294 .read_proc = uart_read_proc,
2295#endif
2296 .tiocmget = uart_tiocmget,
2297 .tiocmset = uart_tiocmset,
Jason Wesself2d937f2008-04-17 20:05:37 +02002298#ifdef CONFIG_CONSOLE_POLL
2299 .poll_init = uart_poll_init,
2300 .poll_get_char = uart_poll_get_char,
2301 .poll_put_char = uart_poll_put_char,
2302#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303};
2304
2305/**
2306 * uart_register_driver - register a driver with the uart core layer
2307 * @drv: low level driver structure
2308 *
2309 * Register a uart driver with the core driver. We in turn register
2310 * with the tty layer, and initialise the core driver per-port state.
2311 *
2312 * We have a proc file in /proc/tty/driver which is named after the
2313 * normal driver.
2314 *
2315 * drv->port should be NULL, and the per-port structures should be
2316 * registered using uart_add_one_port after this call has succeeded.
2317 */
2318int uart_register_driver(struct uart_driver *drv)
2319{
2320 struct tty_driver *normal = NULL;
2321 int i, retval;
2322
2323 BUG_ON(drv->state);
2324
2325 /*
2326 * Maybe we should be using a slab cache for this, especially if
2327 * we have a large number of ports to handle.
2328 */
Burman Yan8f31bb32007-02-14 00:33:07 -08002329 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 retval = -ENOMEM;
2331 if (!drv->state)
2332 goto out;
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 normal = alloc_tty_driver(drv->nr);
2335 if (!normal)
2336 goto out;
2337
2338 drv->tty_driver = normal;
2339
2340 normal->owner = drv->owner;
2341 normal->driver_name = drv->driver_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 normal->name = drv->dev_name;
2343 normal->major = drv->major;
2344 normal->minor_start = drv->minor;
2345 normal->type = TTY_DRIVER_TYPE_SERIAL;
2346 normal->subtype = SERIAL_TYPE_NORMAL;
2347 normal->init_termios = tty_std_termios;
2348 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -08002349 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07002350 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 normal->driver_state = drv;
2352 tty_set_operations(normal, &uart_ops);
2353
2354 /*
2355 * Initialise the UART state(s).
2356 */
2357 for (i = 0; i < drv->nr; i++) {
2358 struct uart_state *state = drv->state + i;
2359
2360 state->close_delay = 500; /* .5 seconds */
2361 state->closing_wait = 30000; /* 30 seconds */
2362
Ingo Molnare2862f62006-01-13 21:37:07 +00002363 mutex_init(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 }
2365
2366 retval = tty_register_driver(normal);
2367 out:
2368 if (retval < 0) {
2369 put_tty_driver(normal);
2370 kfree(drv->state);
2371 }
2372 return retval;
2373}
2374
2375/**
2376 * uart_unregister_driver - remove a driver from the uart core layer
2377 * @drv: low level driver structure
2378 *
2379 * Remove all references to a driver from the core driver. The low
2380 * level driver must have removed all its ports via the
2381 * uart_remove_one_port() if it registered them with uart_add_one_port().
2382 * (ie, drv->port == NULL)
2383 */
2384void uart_unregister_driver(struct uart_driver *drv)
2385{
2386 struct tty_driver *p = drv->tty_driver;
2387 tty_unregister_driver(p);
2388 put_tty_driver(p);
2389 kfree(drv->state);
2390 drv->tty_driver = NULL;
2391}
2392
2393struct tty_driver *uart_console_device(struct console *co, int *index)
2394{
2395 struct uart_driver *p = co->data;
2396 *index = co->index;
2397 return p->tty_driver;
2398}
2399
2400/**
2401 * uart_add_one_port - attach a driver-defined port structure
2402 * @drv: pointer to the uart low level driver structure for this port
2403 * @port: uart port structure to use for this port.
2404 *
2405 * This allows the driver to register its own uart_port structure
2406 * with the core driver. The main purpose is to allow the low
2407 * level uart drivers to expand uart_port, rather than having yet
2408 * more levels of structures.
2409 */
2410int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2411{
2412 struct uart_state *state;
2413 int ret = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002414 struct device *tty_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 BUG_ON(in_interrupt());
2417
2418 if (port->line >= drv->nr)
2419 return -EINVAL;
2420
2421 state = drv->state + port->line;
2422
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002423 mutex_lock(&port_mutex);
Russell King68ac64c2006-04-30 11:13:50 +01002424 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 if (state->port) {
2426 ret = -EINVAL;
2427 goto out;
2428 }
2429
2430 state->port = port;
Russell King97d97222007-09-01 21:25:09 +01002431 state->pm_state = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 port->cons = drv->cons;
2434 port->info = state->info;
2435
Russell King976ecd12005-07-03 21:05:45 +01002436 /*
2437 * If this port is a console, then the spinlock is already
2438 * initialised.
2439 */
Ingo Molnar13e83592006-07-03 00:25:03 -07002440 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
Russell King976ecd12005-07-03 21:05:45 +01002441 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07002442 lockdep_set_class(&port->lock, &port_lock_key);
2443 }
Russell King976ecd12005-07-03 21:05:45 +01002444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 uart_configure_port(drv, state, port);
2446
2447 /*
2448 * Register the port whether it's detected or not. This allows
2449 * setserial to be used to alter this ports parameters.
2450 */
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002451 tty_dev = tty_register_device(drv->tty_driver, port->line, port->dev);
2452 if (likely(!IS_ERR(tty_dev))) {
Alan Stern74081f82008-03-19 22:35:13 +01002453 device_init_wakeup(tty_dev, 1);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002454 device_set_wakeup_enable(tty_dev, 0);
2455 } else
2456 printk(KERN_ERR "Cannot register tty device on line %d\n",
2457 port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
2459 /*
Russell King68ac64c2006-04-30 11:13:50 +01002460 * Ensure UPF_DEAD is not set.
2461 */
2462 port->flags &= ~UPF_DEAD;
2463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 out:
Russell King68ac64c2006-04-30 11:13:50 +01002465 mutex_unlock(&state->mutex);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002466 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 return ret;
2469}
2470
2471/**
2472 * uart_remove_one_port - detach a driver defined port structure
2473 * @drv: pointer to the uart low level driver structure for this port
2474 * @port: uart port structure for this port
2475 *
2476 * This unhooks (and hangs up) the specified port structure from the
2477 * core driver. No further calls will be made to the low-level code
2478 * for this port.
2479 */
2480int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2481{
2482 struct uart_state *state = drv->state + port->line;
Russell King68ac64c2006-04-30 11:13:50 +01002483 struct uart_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 BUG_ON(in_interrupt());
2486
2487 if (state->port != port)
2488 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2489 state->port, port);
2490
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002491 mutex_lock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 /*
Russell King68ac64c2006-04-30 11:13:50 +01002494 * Mark the port "dead" - this prevents any opens from
2495 * succeeding while we shut down the port.
2496 */
2497 mutex_lock(&state->mutex);
2498 port->flags |= UPF_DEAD;
2499 mutex_unlock(&state->mutex);
2500
2501 /*
Greg Kroah-Hartmanaa4148c2005-06-20 21:15:16 -07002502 * Remove the devices from the tty layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 */
2504 tty_unregister_device(drv->tty_driver, port->line);
2505
Russell King68ac64c2006-04-30 11:13:50 +01002506 info = state->info;
2507 if (info && info->tty)
2508 tty_vhangup(info->tty);
2509
2510 /*
2511 * All users of this port should now be disconnected from
2512 * this driver, and the port shut down. We should be the
2513 * only thread fiddling with this port from now on.
2514 */
2515 state->info = NULL;
2516
2517 /*
2518 * Free the port IO and memory resources, if any.
2519 */
2520 if (port->type != PORT_UNKNOWN)
2521 port->ops->release_port(port);
2522
2523 /*
2524 * Indicate that there isn't a port here anymore.
2525 */
2526 port->type = PORT_UNKNOWN;
2527
2528 /*
2529 * Kill the tasklet, and free resources.
2530 */
2531 if (info) {
2532 tasklet_kill(&info->tlet);
2533 kfree(info);
2534 }
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 state->port = NULL;
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002537 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 return 0;
2540}
2541
2542/*
2543 * Are the two ports equivalent?
2544 */
2545int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2546{
2547 if (port1->iotype != port2->iotype)
2548 return 0;
2549
2550 switch (port1->iotype) {
2551 case UPIO_PORT:
2552 return (port1->iobase == port2->iobase);
2553 case UPIO_HUB6:
2554 return (port1->iobase == port2->iobase) &&
2555 (port1->hub6 == port2->hub6);
2556 case UPIO_MEM:
Sergei Shtylyovd21b55d2006-08-28 19:49:03 +04002557 case UPIO_MEM32:
2558 case UPIO_AU:
2559 case UPIO_TSI:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002560 case UPIO_DWAPB:
Benjamin Herrenschmidt1624f002006-01-04 18:09:44 +00002561 return (port1->mapbase == port2->mapbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 }
2563 return 0;
2564}
2565EXPORT_SYMBOL(uart_match_port);
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567EXPORT_SYMBOL(uart_write_wakeup);
2568EXPORT_SYMBOL(uart_register_driver);
2569EXPORT_SYMBOL(uart_unregister_driver);
2570EXPORT_SYMBOL(uart_suspend_port);
2571EXPORT_SYMBOL(uart_resume_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572EXPORT_SYMBOL(uart_add_one_port);
2573EXPORT_SYMBOL(uart_remove_one_port);
2574
2575MODULE_DESCRIPTION("Serial driver core");
2576MODULE_LICENSE("GPL");