blob: 977ce820ce303914dc7e998940cdf524dec52586 [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
425static void
Alan Cox606d0992006-12-08 02:38:45 -0800426uart_change_speed(struct uart_state *state, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 struct tty_struct *tty = state->info->tty;
429 struct uart_port *port = state->port;
Alan Cox606d0992006-12-08 02:38:45 -0800430 struct ktermios *termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /*
433 * If we have no tty, termios, or the port does not exist,
434 * then we can't set the parameters for this port.
435 */
436 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
437 return;
438
439 termios = tty->termios;
440
441 /*
442 * Set flags based on termios cflag
443 */
444 if (termios->c_cflag & CRTSCTS)
445 state->info->flags |= UIF_CTS_FLOW;
446 else
447 state->info->flags &= ~UIF_CTS_FLOW;
448
449 if (termios->c_cflag & CLOCAL)
450 state->info->flags &= ~UIF_CHECK_CD;
451 else
452 state->info->flags |= UIF_CHECK_CD;
453
454 port->ops->set_termios(port, termios, old_termios);
455}
456
457static inline void
458__uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
459{
460 unsigned long flags;
461
462 if (!circ->buf)
463 return;
464
465 spin_lock_irqsave(&port->lock, flags);
466 if (uart_circ_chars_free(circ) != 0) {
467 circ->buf[circ->head] = c;
468 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
469 }
470 spin_unlock_irqrestore(&port->lock, flags);
471}
472
473static void uart_put_char(struct tty_struct *tty, unsigned char ch)
474{
475 struct uart_state *state = tty->driver_data;
476
477 __uart_put_char(state->port, &state->info->xmit, ch);
478}
479
480static void uart_flush_chars(struct tty_struct *tty)
481{
482 uart_start(tty);
483}
484
485static int
Pavel Machekd5f735e2006-03-07 21:55:20 -0800486uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 struct uart_state *state = tty->driver_data;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800489 struct uart_port *port;
490 struct circ_buf *circ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 unsigned long flags;
492 int c, ret = 0;
493
Pavel Machekd5f735e2006-03-07 21:55:20 -0800494 /*
495 * This means you called this function _after_ the port was
496 * closed. No cookie for you.
497 */
498 if (!state || !state->info) {
499 WARN_ON(1);
500 return -EL3HLT;
501 }
502
503 port = state->port;
504 circ = &state->info->xmit;
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (!circ->buf)
507 return 0;
508
509 spin_lock_irqsave(&port->lock, flags);
510 while (1) {
511 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
512 if (count < c)
513 c = count;
514 if (c <= 0)
515 break;
516 memcpy(circ->buf + circ->head, buf, c);
517 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
518 buf += c;
519 count -= c;
520 ret += c;
521 }
522 spin_unlock_irqrestore(&port->lock, flags);
523
524 uart_start(tty);
525 return ret;
526}
527
528static int uart_write_room(struct tty_struct *tty)
529{
530 struct uart_state *state = tty->driver_data;
531
532 return uart_circ_chars_free(&state->info->xmit);
533}
534
535static int uart_chars_in_buffer(struct tty_struct *tty)
536{
537 struct uart_state *state = tty->driver_data;
538
539 return uart_circ_chars_pending(&state->info->xmit);
540}
541
542static void uart_flush_buffer(struct tty_struct *tty)
543{
544 struct uart_state *state = tty->driver_data;
545 struct uart_port *port = state->port;
546 unsigned long flags;
547
Pavel Machekd5f735e2006-03-07 21:55:20 -0800548 /*
549 * This means you called this function _after_ the port was
550 * closed. No cookie for you.
551 */
552 if (!state || !state->info) {
553 WARN_ON(1);
554 return;
555 }
556
Jiri Slabyeb3a1e12007-05-06 14:48:52 -0700557 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 spin_lock_irqsave(&port->lock, flags);
560 uart_circ_clear(&state->info->xmit);
561 spin_unlock_irqrestore(&port->lock, flags);
562 tty_wakeup(tty);
563}
564
565/*
566 * This function is used to send a high-priority XON/XOFF character to
567 * the device
568 */
569static void uart_send_xchar(struct tty_struct *tty, char ch)
570{
571 struct uart_state *state = tty->driver_data;
572 struct uart_port *port = state->port;
573 unsigned long flags;
574
575 if (port->ops->send_xchar)
576 port->ops->send_xchar(port, ch);
577 else {
578 port->x_char = ch;
579 if (ch) {
580 spin_lock_irqsave(&port->lock, flags);
Russell Kingb129a8c2005-08-31 10:12:14 +0100581 port->ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 spin_unlock_irqrestore(&port->lock, flags);
583 }
584 }
585}
586
587static void uart_throttle(struct tty_struct *tty)
588{
589 struct uart_state *state = tty->driver_data;
590
591 if (I_IXOFF(tty))
592 uart_send_xchar(tty, STOP_CHAR(tty));
593
594 if (tty->termios->c_cflag & CRTSCTS)
595 uart_clear_mctrl(state->port, TIOCM_RTS);
596}
597
598static void uart_unthrottle(struct tty_struct *tty)
599{
600 struct uart_state *state = tty->driver_data;
601 struct uart_port *port = state->port;
602
603 if (I_IXOFF(tty)) {
604 if (port->x_char)
605 port->x_char = 0;
606 else
607 uart_send_xchar(tty, START_CHAR(tty));
608 }
609
610 if (tty->termios->c_cflag & CRTSCTS)
611 uart_set_mctrl(port, TIOCM_RTS);
612}
613
614static int uart_get_info(struct uart_state *state,
615 struct serial_struct __user *retinfo)
616{
617 struct uart_port *port = state->port;
618 struct serial_struct tmp;
619
620 memset(&tmp, 0, sizeof(tmp));
621 tmp.type = port->type;
622 tmp.line = port->line;
623 tmp.port = port->iobase;
624 if (HIGH_BITS_OFFSET)
625 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
626 tmp.irq = port->irq;
627 tmp.flags = port->flags;
628 tmp.xmit_fifo_size = port->fifosize;
629 tmp.baud_base = port->uartclk / 16;
630 tmp.close_delay = state->close_delay / 10;
631 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
632 ASYNC_CLOSING_WAIT_NONE :
Alan Coxa46c9992008-02-08 04:18:53 -0800633 state->closing_wait / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 tmp.custom_divisor = port->custom_divisor;
635 tmp.hub6 = port->hub6;
636 tmp.io_type = port->iotype;
637 tmp.iomem_reg_shift = port->regshift;
Josh Boyer4f640ef2007-07-23 18:43:44 -0700638 tmp.iomem_base = (void *)(unsigned long)port->mapbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
641 return -EFAULT;
642 return 0;
643}
644
645static int uart_set_info(struct uart_state *state,
646 struct serial_struct __user *newinfo)
647{
648 struct serial_struct new_serial;
649 struct uart_port *port = state->port;
650 unsigned long new_port;
Russell King0077d452006-01-21 23:03:28 +0000651 unsigned int change_irq, change_port, closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 unsigned int old_custom_divisor, close_delay;
Russell King0077d452006-01-21 23:03:28 +0000653 upf_t old_flags, new_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 int retval = 0;
655
656 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
657 return -EFAULT;
658
659 new_port = new_serial.port;
660 if (HIGH_BITS_OFFSET)
661 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
662
663 new_serial.irq = irq_canonicalize(new_serial.irq);
664 close_delay = new_serial.close_delay * 10;
665 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
666 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
667
668 /*
669 * This semaphore protects state->count. It is also
670 * very useful to prevent opens. Also, take the
671 * port configuration semaphore to make sure that a
672 * module insertion/removal doesn't change anything
673 * under us.
674 */
Ingo Molnare2862f62006-01-13 21:37:07 +0000675 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
David Gibsonabb4a232007-05-06 14:48:49 -0700677 change_irq = !(port->flags & UPF_FIXED_PORT)
678 && new_serial.irq != port->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 /*
681 * Since changing the 'type' of the port changes its resource
682 * allocations, we should treat type changes the same as
683 * IO port changes.
684 */
David Gibsonabb4a232007-05-06 14:48:49 -0700685 change_port = !(port->flags & UPF_FIXED_PORT)
686 && (new_port != port->iobase ||
687 (unsigned long)new_serial.iomem_base != port->mapbase ||
688 new_serial.hub6 != port->hub6 ||
689 new_serial.io_type != port->iotype ||
690 new_serial.iomem_reg_shift != port->regshift ||
691 new_serial.type != port->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 old_flags = port->flags;
Russell King0077d452006-01-21 23:03:28 +0000694 new_flags = new_serial.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 old_custom_divisor = port->custom_divisor;
696
697 if (!capable(CAP_SYS_ADMIN)) {
698 retval = -EPERM;
699 if (change_irq || change_port ||
700 (new_serial.baud_base != port->uartclk / 16) ||
701 (close_delay != state->close_delay) ||
702 (closing_wait != state->closing_wait) ||
Russell King947deee2006-07-02 20:45:51 +0100703 (new_serial.xmit_fifo_size &&
704 new_serial.xmit_fifo_size != port->fifosize) ||
Russell King0077d452006-01-21 23:03:28 +0000705 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 goto exit;
707 port->flags = ((port->flags & ~UPF_USR_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000708 (new_flags & UPF_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 port->custom_divisor = new_serial.custom_divisor;
710 goto check_and_exit;
711 }
712
713 /*
714 * Ask the low level driver to verify the settings.
715 */
716 if (port->ops->verify_port)
717 retval = port->ops->verify_port(port, &new_serial);
718
719 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
720 (new_serial.baud_base < 9600))
721 retval = -EINVAL;
722
723 if (retval)
724 goto exit;
725
726 if (change_port || change_irq) {
727 retval = -EBUSY;
728
729 /*
730 * Make sure that we are the sole user of this port.
731 */
732 if (uart_users(state) > 1)
733 goto exit;
734
735 /*
736 * We need to shutdown the serial port at the old
737 * port/type/irq combination.
738 */
739 uart_shutdown(state);
740 }
741
742 if (change_port) {
743 unsigned long old_iobase, old_mapbase;
744 unsigned int old_type, old_iotype, old_hub6, old_shift;
745
746 old_iobase = port->iobase;
747 old_mapbase = port->mapbase;
748 old_type = port->type;
749 old_hub6 = port->hub6;
750 old_iotype = port->iotype;
751 old_shift = port->regshift;
752
753 /*
754 * Free and release old regions
755 */
756 if (old_type != PORT_UNKNOWN)
757 port->ops->release_port(port);
758
759 port->iobase = new_port;
760 port->type = new_serial.type;
761 port->hub6 = new_serial.hub6;
762 port->iotype = new_serial.io_type;
763 port->regshift = new_serial.iomem_reg_shift;
764 port->mapbase = (unsigned long)new_serial.iomem_base;
765
766 /*
767 * Claim and map the new regions
768 */
769 if (port->type != PORT_UNKNOWN) {
770 retval = port->ops->request_port(port);
771 } else {
772 /* Always success - Jean II */
773 retval = 0;
774 }
775
776 /*
777 * If we fail to request resources for the
778 * new port, try to restore the old settings.
779 */
780 if (retval && old_type != PORT_UNKNOWN) {
781 port->iobase = old_iobase;
782 port->type = old_type;
783 port->hub6 = old_hub6;
784 port->iotype = old_iotype;
785 port->regshift = old_shift;
786 port->mapbase = old_mapbase;
787 retval = port->ops->request_port(port);
788 /*
789 * If we failed to restore the old settings,
790 * we fail like this.
791 */
792 if (retval)
793 port->type = PORT_UNKNOWN;
794
795 /*
796 * We failed anyway.
797 */
798 retval = -EBUSY;
Alan Coxa46c9992008-02-08 04:18:53 -0800799 /* Added to return the correct error -Ram Gupta */
800 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802 }
803
David Gibsonabb4a232007-05-06 14:48:49 -0700804 if (change_irq)
805 port->irq = new_serial.irq;
806 if (!(port->flags & UPF_FIXED_PORT))
807 port->uartclk = new_serial.baud_base * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000809 (new_flags & UPF_CHANGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 port->custom_divisor = new_serial.custom_divisor;
811 state->close_delay = close_delay;
812 state->closing_wait = closing_wait;
Russell King947deee2006-07-02 20:45:51 +0100813 if (new_serial.xmit_fifo_size)
814 port->fifosize = new_serial.xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (state->info->tty)
816 state->info->tty->low_latency =
817 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
818
819 check_and_exit:
820 retval = 0;
821 if (port->type == PORT_UNKNOWN)
822 goto exit;
823 if (state->info->flags & UIF_INITIALIZED) {
824 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
825 old_custom_divisor != port->custom_divisor) {
826 /*
827 * If they're setting up a custom divisor or speed,
828 * instead of clearing it, then bitch about it. No
829 * need to rate-limit; it's CAP_SYS_ADMIN only.
830 */
831 if (port->flags & UPF_SPD_MASK) {
832 char buf[64];
833 printk(KERN_NOTICE
834 "%s sets custom speed on %s. This "
835 "is deprecated.\n", current->comm,
836 tty_name(state->info->tty, buf));
837 }
838 uart_change_speed(state, NULL);
839 }
840 } else
841 retval = uart_startup(state, 1);
842 exit:
Ingo Molnare2862f62006-01-13 21:37:07 +0000843 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return retval;
845}
846
847
848/*
849 * uart_get_lsr_info - get line status register info.
850 * Note: uart_ioctl protects us against hangups.
851 */
852static int uart_get_lsr_info(struct uart_state *state,
853 unsigned int __user *value)
854{
855 struct uart_port *port = state->port;
856 unsigned int result;
857
858 result = port->ops->tx_empty(port);
859
860 /*
861 * If we're about to load something into the transmit
862 * register, we'll pretend the transmitter isn't empty to
863 * avoid a race condition (depending on when the transmit
864 * interrupt happens).
865 */
866 if (port->x_char ||
867 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
868 !state->info->tty->stopped && !state->info->tty->hw_stopped))
869 result &= ~TIOCSER_TEMT;
Alan Coxa46c9992008-02-08 04:18:53 -0800870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return put_user(result, value);
872}
873
874static int uart_tiocmget(struct tty_struct *tty, struct file *file)
875{
876 struct uart_state *state = tty->driver_data;
877 struct uart_port *port = state->port;
878 int result = -EIO;
879
Ingo Molnare2862f62006-01-13 21:37:07 +0000880 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if ((!file || !tty_hung_up_p(file)) &&
882 !(tty->flags & (1 << TTY_IO_ERROR))) {
883 result = port->mctrl;
Russell Kingc5f46442005-06-29 09:42:38 +0100884
885 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 result |= port->ops->get_mctrl(port);
Russell Kingc5f46442005-06-29 09:42:38 +0100887 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000889 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 return result;
892}
893
894static int
895uart_tiocmset(struct tty_struct *tty, struct file *file,
896 unsigned int set, unsigned int clear)
897{
898 struct uart_state *state = tty->driver_data;
899 struct uart_port *port = state->port;
900 int ret = -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 uart_update_mctrl(port, set, clear);
906 ret = 0;
907 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000908 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return ret;
910}
911
912static void uart_break_ctl(struct tty_struct *tty, int break_state)
913{
914 struct uart_state *state = tty->driver_data;
915 struct uart_port *port = state->port;
916
917 BUG_ON(!kernel_locked());
918
Ingo Molnare2862f62006-01-13 21:37:07 +0000919 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (port->type != PORT_UNKNOWN)
922 port->ops->break_ctl(port, break_state);
923
Ingo Molnare2862f62006-01-13 21:37:07 +0000924 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
927static int uart_do_autoconfig(struct uart_state *state)
928{
929 struct uart_port *port = state->port;
930 int flags, ret;
931
932 if (!capable(CAP_SYS_ADMIN))
933 return -EPERM;
934
935 /*
936 * Take the per-port semaphore. This prevents count from
937 * changing, and hence any extra opens of the port while
938 * we're auto-configuring.
939 */
Ingo Molnare2862f62006-01-13 21:37:07 +0000940 if (mutex_lock_interruptible(&state->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return -ERESTARTSYS;
942
943 ret = -EBUSY;
944 if (uart_users(state) == 1) {
945 uart_shutdown(state);
946
947 /*
948 * If we already have a port type configured,
949 * we must release its resources.
950 */
951 if (port->type != PORT_UNKNOWN)
952 port->ops->release_port(port);
953
954 flags = UART_CONFIG_TYPE;
955 if (port->flags & UPF_AUTO_IRQ)
956 flags |= UART_CONFIG_IRQ;
957
958 /*
959 * This will claim the ports resources if
960 * a port is found.
961 */
962 port->ops->config_port(port, flags);
963
964 ret = uart_startup(state, 1);
965 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000966 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return ret;
968}
969
970/*
971 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
972 * - mask passed in arg for lines of interest
973 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
974 * Caller should use TIOCGICOUNT to see which one it was
975 */
976static int
977uart_wait_modem_status(struct uart_state *state, unsigned long arg)
978{
979 struct uart_port *port = state->port;
980 DECLARE_WAITQUEUE(wait, current);
981 struct uart_icount cprev, cnow;
982 int ret;
983
984 /*
985 * note the counters on entry
986 */
987 spin_lock_irq(&port->lock);
988 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
989
990 /*
991 * Force modem status interrupts on
992 */
993 port->ops->enable_ms(port);
994 spin_unlock_irq(&port->lock);
995
996 add_wait_queue(&state->info->delta_msr_wait, &wait);
997 for (;;) {
998 spin_lock_irq(&port->lock);
999 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1000 spin_unlock_irq(&port->lock);
1001
1002 set_current_state(TASK_INTERRUPTIBLE);
1003
1004 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1005 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1006 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1007 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Alan Coxa46c9992008-02-08 04:18:53 -08001008 ret = 0;
1009 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011
1012 schedule();
1013
1014 /* see if a signal did it */
1015 if (signal_pending(current)) {
1016 ret = -ERESTARTSYS;
1017 break;
1018 }
1019
1020 cprev = cnow;
1021 }
1022
1023 current->state = TASK_RUNNING;
1024 remove_wait_queue(&state->info->delta_msr_wait, &wait);
1025
1026 return ret;
1027}
1028
1029/*
1030 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1031 * Return: write counters to the user passed counter struct
1032 * NB: both 1->0 and 0->1 transitions are counted except for
1033 * RI where only 0->1 is counted.
1034 */
1035static int uart_get_count(struct uart_state *state,
1036 struct serial_icounter_struct __user *icnt)
1037{
1038 struct serial_icounter_struct icount;
1039 struct uart_icount cnow;
1040 struct uart_port *port = state->port;
1041
1042 spin_lock_irq(&port->lock);
1043 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1044 spin_unlock_irq(&port->lock);
1045
1046 icount.cts = cnow.cts;
1047 icount.dsr = cnow.dsr;
1048 icount.rng = cnow.rng;
1049 icount.dcd = cnow.dcd;
1050 icount.rx = cnow.rx;
1051 icount.tx = cnow.tx;
1052 icount.frame = cnow.frame;
1053 icount.overrun = cnow.overrun;
1054 icount.parity = cnow.parity;
1055 icount.brk = cnow.brk;
1056 icount.buf_overrun = cnow.buf_overrun;
1057
1058 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1059}
1060
1061/*
1062 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
1063 */
1064static int
1065uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1066 unsigned long arg)
1067{
1068 struct uart_state *state = tty->driver_data;
1069 void __user *uarg = (void __user *)arg;
1070 int ret = -ENOIOCTLCMD;
1071
1072 BUG_ON(!kernel_locked());
1073
1074 /*
1075 * These ioctls don't rely on the hardware to be present.
1076 */
1077 switch (cmd) {
1078 case TIOCGSERIAL:
1079 ret = uart_get_info(state, uarg);
1080 break;
1081
1082 case TIOCSSERIAL:
1083 ret = uart_set_info(state, uarg);
1084 break;
1085
1086 case TIOCSERCONFIG:
1087 ret = uart_do_autoconfig(state);
1088 break;
1089
1090 case TIOCSERGWILD: /* obsolete */
1091 case TIOCSERSWILD: /* obsolete */
1092 ret = 0;
1093 break;
1094 }
1095
1096 if (ret != -ENOIOCTLCMD)
1097 goto out;
1098
1099 if (tty->flags & (1 << TTY_IO_ERROR)) {
1100 ret = -EIO;
1101 goto out;
1102 }
1103
1104 /*
1105 * The following should only be used when hardware is present.
1106 */
1107 switch (cmd) {
1108 case TIOCMIWAIT:
1109 ret = uart_wait_modem_status(state, arg);
1110 break;
1111
1112 case TIOCGICOUNT:
1113 ret = uart_get_count(state, uarg);
1114 break;
1115 }
1116
1117 if (ret != -ENOIOCTLCMD)
1118 goto out;
1119
Ingo Molnare2862f62006-01-13 21:37:07 +00001120 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 if (tty_hung_up_p(filp)) {
1123 ret = -EIO;
1124 goto out_up;
1125 }
1126
1127 /*
1128 * All these rely on hardware being present and need to be
1129 * protected against the tty being hung up.
1130 */
1131 switch (cmd) {
1132 case TIOCSERGETLSR: /* Get line status register */
1133 ret = uart_get_lsr_info(state, uarg);
1134 break;
1135
1136 default: {
1137 struct uart_port *port = state->port;
1138 if (port->ops->ioctl)
1139 ret = port->ops->ioctl(port, cmd, arg);
1140 break;
1141 }
1142 }
1143 out_up:
Ingo Molnare2862f62006-01-13 21:37:07 +00001144 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 out:
1146 return ret;
1147}
1148
Alan Coxa46c9992008-02-08 04:18:53 -08001149static void uart_set_termios(struct tty_struct *tty,
1150 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 struct uart_state *state = tty->driver_data;
1153 unsigned long flags;
1154 unsigned int cflag = tty->termios->c_cflag;
1155
1156 BUG_ON(!kernel_locked());
1157
1158 /*
1159 * These are the bits that are used to setup various
David Woodhouse20620d62007-08-22 14:01:11 -07001160 * flags in the low level driver. We can ignore the Bfoo
1161 * bits in c_cflag; c_[io]speed will always be set
1162 * appropriately by set_termios() in tty_ioctl.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 */
1164#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if ((cflag ^ old_termios->c_cflag) == 0 &&
David Woodhouse20620d62007-08-22 14:01:11 -07001166 tty->termios->c_ospeed == old_termios->c_ospeed &&
1167 tty->termios->c_ispeed == old_termios->c_ispeed &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1169 return;
1170
1171 uart_change_speed(state, old_termios);
1172
1173 /* Handle transition to B0 status */
1174 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1175 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1176
1177 /* Handle transition away from B0 status */
1178 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1179 unsigned int mask = TIOCM_DTR;
1180 if (!(cflag & CRTSCTS) ||
1181 !test_bit(TTY_THROTTLED, &tty->flags))
1182 mask |= TIOCM_RTS;
1183 uart_set_mctrl(state->port, mask);
1184 }
1185
1186 /* Handle turning off CRTSCTS */
1187 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1188 spin_lock_irqsave(&state->port->lock, flags);
1189 tty->hw_stopped = 0;
1190 __uart_start(tty);
1191 spin_unlock_irqrestore(&state->port->lock, flags);
1192 }
1193
Russell King0dd7a1a2005-06-29 18:40:53 +01001194 /* Handle turning on CRTSCTS */
1195 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1196 spin_lock_irqsave(&state->port->lock, flags);
1197 if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
1198 tty->hw_stopped = 1;
Russell Kingb129a8c2005-08-31 10:12:14 +01001199 state->port->ops->stop_tx(state->port);
Russell King0dd7a1a2005-06-29 18:40:53 +01001200 }
1201 spin_unlock_irqrestore(&state->port->lock, flags);
1202 }
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204#if 0
1205 /*
1206 * No need to wake up processes in open wait, since they
1207 * sample the CLOCAL flag once, and don't recheck it.
1208 * XXX It's not clear whether the current behavior is correct
1209 * or not. Hence, this may change.....
1210 */
1211 if (!(old_termios->c_cflag & CLOCAL) &&
1212 (tty->termios->c_cflag & CLOCAL))
1213 wake_up_interruptible(&state->info->open_wait);
1214#endif
1215}
1216
1217/*
1218 * In 2.4.5, calls to this will be serialized via the BKL in
1219 * linux/drivers/char/tty_io.c:tty_release()
1220 * linux/drivers/char/tty_io.c:do_tty_handup()
1221 */
1222static void uart_close(struct tty_struct *tty, struct file *filp)
1223{
1224 struct uart_state *state = tty->driver_data;
1225 struct uart_port *port;
Alan Coxa46c9992008-02-08 04:18:53 -08001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 BUG_ON(!kernel_locked());
1228
1229 if (!state || !state->port)
1230 return;
1231
1232 port = state->port;
1233
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001234 pr_debug("uart_close(%d) called\n", port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Ingo Molnare2862f62006-01-13 21:37:07 +00001236 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 if (tty_hung_up_p(filp))
1239 goto done;
1240
1241 if ((tty->count == 1) && (state->count != 1)) {
1242 /*
1243 * Uh, oh. tty->count is 1, which means that the tty
1244 * structure will be freed. state->count should always
1245 * be one in these conditions. If it's greater than
1246 * one, we've got real problems, since it means the
1247 * serial port won't be shutdown.
1248 */
1249 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1250 "state->count is %d\n", state->count);
1251 state->count = 1;
1252 }
1253 if (--state->count < 0) {
1254 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1255 tty->name, state->count);
1256 state->count = 0;
1257 }
1258 if (state->count)
1259 goto done;
1260
1261 /*
1262 * Now we wait for the transmit buffer to clear; and we notify
1263 * the line discipline to only process XON/XOFF characters by
1264 * setting tty->closing.
1265 */
1266 tty->closing = 1;
1267
1268 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1269 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1270
1271 /*
1272 * At this point, we stop accepting input. To do this, we
1273 * disable the receive line status interrupts.
1274 */
1275 if (state->info->flags & UIF_INITIALIZED) {
1276 unsigned long flags;
1277 spin_lock_irqsave(&port->lock, flags);
1278 port->ops->stop_rx(port);
1279 spin_unlock_irqrestore(&port->lock, flags);
1280 /*
1281 * Before we drop DTR, make sure the UART transmitter
1282 * has completely drained; this is especially
1283 * important if there is a transmit FIFO!
1284 */
1285 uart_wait_until_sent(tty, port->timeout);
1286 }
1287
1288 uart_shutdown(state);
1289 uart_flush_buffer(tty);
1290
Alan Coxa46c9992008-02-08 04:18:53 -08001291 tty_ldisc_flush(tty);
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 tty->closing = 0;
1294 state->info->tty = NULL;
1295
1296 if (state->info->blocked_open) {
1297 if (state->close_delay)
1298 msleep_interruptible(state->close_delay);
1299 } else if (!uart_console(port)) {
1300 uart_change_pm(state, 3);
1301 }
1302
1303 /*
1304 * Wake up anyone trying to open this port.
1305 */
1306 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1307 wake_up_interruptible(&state->info->open_wait);
1308
1309 done:
Ingo Molnare2862f62006-01-13 21:37:07 +00001310 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
1313static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1314{
1315 struct uart_state *state = tty->driver_data;
1316 struct uart_port *port = state->port;
1317 unsigned long char_time, expire;
1318
1319 BUG_ON(!kernel_locked());
1320
1321 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1322 return;
1323
1324 /*
1325 * Set the check interval to be 1/5 of the estimated time to
1326 * send a single character, and make it at least 1. The check
1327 * interval should also be less than the timeout.
1328 *
1329 * Note: we have to use pretty tight timings here to satisfy
1330 * the NIST-PCTS.
1331 */
1332 char_time = (port->timeout - HZ/50) / port->fifosize;
1333 char_time = char_time / 5;
1334 if (char_time == 0)
1335 char_time = 1;
1336 if (timeout && timeout < char_time)
1337 char_time = timeout;
1338
1339 /*
1340 * If the transmitter hasn't cleared in twice the approximate
1341 * amount of time to send the entire FIFO, it probably won't
1342 * ever clear. This assumes the UART isn't doing flow
1343 * control, which is currently the case. Hence, if it ever
1344 * takes longer than port->timeout, this is probably due to a
1345 * UART bug of some kind. So, we clamp the timeout parameter at
1346 * 2*port->timeout.
1347 */
1348 if (timeout == 0 || timeout > 2 * port->timeout)
1349 timeout = 2 * port->timeout;
1350
1351 expire = jiffies + timeout;
1352
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001353 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
Alan Coxa46c9992008-02-08 04:18:53 -08001354 port->line, jiffies, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 /*
1357 * Check whether the transmitter is empty every 'char_time'.
1358 * 'timeout' / 'expire' give us the maximum amount of time
1359 * we wait.
1360 */
1361 while (!port->ops->tx_empty(port)) {
1362 msleep_interruptible(jiffies_to_msecs(char_time));
1363 if (signal_pending(current))
1364 break;
1365 if (time_after(jiffies, expire))
1366 break;
1367 }
1368 set_current_state(TASK_RUNNING); /* might not be needed */
1369}
1370
1371/*
1372 * This is called with the BKL held in
1373 * linux/drivers/char/tty_io.c:do_tty_hangup()
1374 * We're called from the eventd thread, so we can sleep for
1375 * a _short_ time only.
1376 */
1377static void uart_hangup(struct tty_struct *tty)
1378{
1379 struct uart_state *state = tty->driver_data;
1380
1381 BUG_ON(!kernel_locked());
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001382 pr_debug("uart_hangup(%d)\n", state->port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Ingo Molnare2862f62006-01-13 21:37:07 +00001384 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1386 uart_flush_buffer(tty);
1387 uart_shutdown(state);
1388 state->count = 0;
1389 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1390 state->info->tty = NULL;
1391 wake_up_interruptible(&state->info->open_wait);
1392 wake_up_interruptible(&state->info->delta_msr_wait);
1393 }
Ingo Molnare2862f62006-01-13 21:37:07 +00001394 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
1397/*
1398 * Copy across the serial console cflag setting into the termios settings
1399 * for the initial open of the port. This allows continuity between the
1400 * kernel settings, and the settings init adopts when it opens the port
1401 * for the first time.
1402 */
1403static void uart_update_termios(struct uart_state *state)
1404{
1405 struct tty_struct *tty = state->info->tty;
1406 struct uart_port *port = state->port;
1407
1408 if (uart_console(port) && port->cons->cflag) {
1409 tty->termios->c_cflag = port->cons->cflag;
1410 port->cons->cflag = 0;
1411 }
1412
1413 /*
1414 * If the device failed to grab its irq resources,
1415 * or some other error occurred, don't try to talk
1416 * to the port hardware.
1417 */
1418 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1419 /*
1420 * Make termios settings take effect.
1421 */
1422 uart_change_speed(state, NULL);
1423
1424 /*
1425 * And finally enable the RTS and DTR signals.
1426 */
1427 if (tty->termios->c_cflag & CBAUD)
1428 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1429 }
1430}
1431
1432/*
1433 * Block the open until the port is ready. We must be called with
1434 * the per-port semaphore held.
1435 */
1436static int
1437uart_block_til_ready(struct file *filp, struct uart_state *state)
1438{
1439 DECLARE_WAITQUEUE(wait, current);
1440 struct uart_info *info = state->info;
1441 struct uart_port *port = state->port;
Russell Kingc5f46442005-06-29 09:42:38 +01001442 unsigned int mctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 info->blocked_open++;
1445 state->count--;
1446
1447 add_wait_queue(&info->open_wait, &wait);
1448 while (1) {
1449 set_current_state(TASK_INTERRUPTIBLE);
1450
1451 /*
1452 * If we have been hung up, tell userspace/restart open.
1453 */
1454 if (tty_hung_up_p(filp) || info->tty == NULL)
1455 break;
1456
1457 /*
1458 * If the port has been closed, tell userspace/restart open.
1459 */
1460 if (!(info->flags & UIF_INITIALIZED))
1461 break;
1462
1463 /*
1464 * If non-blocking mode is set, or CLOCAL mode is set,
1465 * we don't want to wait for the modem status lines to
1466 * indicate that the port is ready.
1467 *
1468 * Also, if the port is not enabled/configured, we want
1469 * to allow the open to succeed here. Note that we will
1470 * have set TTY_IO_ERROR for a non-existant port.
1471 */
1472 if ((filp->f_flags & O_NONBLOCK) ||
Alan Coxa46c9992008-02-08 04:18:53 -08001473 (info->tty->termios->c_cflag & CLOCAL) ||
1474 (info->tty->flags & (1 << TTY_IO_ERROR)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 /*
1478 * Set DTR to allow modem to know we're waiting. Do
1479 * not set RTS here - we want to make sure we catch
1480 * the data from the modem.
1481 */
1482 if (info->tty->termios->c_cflag & CBAUD)
1483 uart_set_mctrl(port, TIOCM_DTR);
1484
1485 /*
1486 * and wait for the carrier to indicate that the
1487 * modem is ready for us.
1488 */
Russell Kingc5f46442005-06-29 09:42:38 +01001489 spin_lock_irq(&port->lock);
Russell Kingf61051c2006-01-07 23:11:23 +00001490 port->ops->enable_ms(port);
Russell Kingc5f46442005-06-29 09:42:38 +01001491 mctrl = port->ops->get_mctrl(port);
1492 spin_unlock_irq(&port->lock);
1493 if (mctrl & TIOCM_CAR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 break;
1495
Ingo Molnare2862f62006-01-13 21:37:07 +00001496 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 schedule();
Ingo Molnare2862f62006-01-13 21:37:07 +00001498 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 if (signal_pending(current))
1501 break;
1502 }
1503 set_current_state(TASK_RUNNING);
1504 remove_wait_queue(&info->open_wait, &wait);
1505
1506 state->count++;
1507 info->blocked_open--;
1508
1509 if (signal_pending(current))
1510 return -ERESTARTSYS;
1511
1512 if (!info->tty || tty_hung_up_p(filp))
1513 return -EAGAIN;
1514
1515 return 0;
1516}
1517
1518static struct uart_state *uart_get(struct uart_driver *drv, int line)
1519{
1520 struct uart_state *state;
Russell King68ac64c2006-04-30 11:13:50 +01001521 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 state = drv->state + line;
Ingo Molnare2862f62006-01-13 21:37:07 +00001524 if (mutex_lock_interruptible(&state->mutex)) {
Russell King68ac64c2006-04-30 11:13:50 +01001525 ret = -ERESTARTSYS;
1526 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528
1529 state->count++;
Russell King68ac64c2006-04-30 11:13:50 +01001530 if (!state->port || state->port->flags & UPF_DEAD) {
1531 ret = -ENXIO;
1532 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534
1535 if (!state->info) {
Burman Yan8f31bb32007-02-14 00:33:07 -08001536 state->info = kzalloc(sizeof(struct uart_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (state->info) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 init_waitqueue_head(&state->info->open_wait);
1539 init_waitqueue_head(&state->info->delta_msr_wait);
1540
1541 /*
1542 * Link the info into the other structures.
1543 */
1544 state->port->info = state->info;
1545
1546 tasklet_init(&state->info->tlet, uart_tasklet_action,
1547 (unsigned long)state);
1548 } else {
Russell King68ac64c2006-04-30 11:13:50 +01001549 ret = -ENOMEM;
1550 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return state;
Russell King68ac64c2006-04-30 11:13:50 +01001554
1555 err_unlock:
1556 state->count--;
1557 mutex_unlock(&state->mutex);
1558 err:
1559 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
1562/*
Denis Cheng922f9cf2008-02-08 04:22:00 -08001563 * calls to uart_open are serialised by the BKL in
1564 * fs/char_dev.c:chrdev_open()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 * Note that if this fails, then uart_close() _will_ be called.
1566 *
1567 * In time, we want to scrap the "opening nonpresent ports"
1568 * behaviour and implement an alternative way for setserial
1569 * to set base addresses/ports/types. This will allow us to
1570 * get rid of a certain amount of extra tests.
1571 */
1572static int uart_open(struct tty_struct *tty, struct file *filp)
1573{
1574 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1575 struct uart_state *state;
1576 int retval, line = tty->index;
1577
1578 BUG_ON(!kernel_locked());
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001579 pr_debug("uart_open(%d) called\n", line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 /*
1582 * tty->driver->num won't change, so we won't fail here with
1583 * tty->driver_data set to something non-NULL (and therefore
1584 * we won't get caught by uart_close()).
1585 */
1586 retval = -ENODEV;
1587 if (line >= tty->driver->num)
1588 goto fail;
1589
1590 /*
1591 * We take the semaphore inside uart_get to guarantee that we won't
1592 * be re-entered while allocating the info structure, or while we
1593 * request any IRQs that the driver may need. This also has the nice
1594 * side-effect that it delays the action of uart_hangup, so we can
1595 * guarantee that info->tty will always contain something reasonable.
1596 */
1597 state = uart_get(drv, line);
1598 if (IS_ERR(state)) {
1599 retval = PTR_ERR(state);
1600 goto fail;
1601 }
1602
1603 /*
1604 * Once we set tty->driver_data here, we are guaranteed that
1605 * uart_close() will decrement the driver module use count.
1606 * Any failures from here onwards should not touch the count.
1607 */
1608 tty->driver_data = state;
1609 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1610 tty->alt_speed = 0;
1611 state->info->tty = tty;
1612
1613 /*
1614 * If the port is in the middle of closing, bail out now.
1615 */
1616 if (tty_hung_up_p(filp)) {
1617 retval = -EAGAIN;
1618 state->count--;
Ingo Molnare2862f62006-01-13 21:37:07 +00001619 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 goto fail;
1621 }
1622
1623 /*
1624 * Make sure the device is in D0 state.
1625 */
1626 if (state->count == 1)
1627 uart_change_pm(state, 0);
1628
1629 /*
1630 * Start up the serial port.
1631 */
1632 retval = uart_startup(state, 0);
1633
1634 /*
1635 * If we succeeded, wait until the port is ready.
1636 */
1637 if (retval == 0)
1638 retval = uart_block_til_ready(filp, state);
Ingo Molnare2862f62006-01-13 21:37:07 +00001639 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 /*
1642 * If this is the first open to succeed, adjust things to suit.
1643 */
1644 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1645 state->info->flags |= UIF_NORMAL_ACTIVE;
1646
1647 uart_update_termios(state);
1648 }
1649
1650 fail:
1651 return retval;
1652}
1653
1654static const char *uart_type(struct uart_port *port)
1655{
1656 const char *str = NULL;
1657
1658 if (port->ops->type)
1659 str = port->ops->type(port);
1660
1661 if (!str)
1662 str = "unknown";
1663
1664 return str;
1665}
1666
1667#ifdef CONFIG_PROC_FS
1668
1669static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1670{
1671 struct uart_state *state = drv->state + i;
George G. Davis3689a0e2007-02-14 00:33:06 -08001672 int pm_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 struct uart_port *port = state->port;
1674 char stat_buf[32];
1675 unsigned int status;
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001676 int mmio, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 if (!port)
1679 return 0;
1680
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001681 mmio = port->iotype >= UPIO_MEM;
Josh Boyer4f640ef2007-07-23 18:43:44 -07001682 ret = sprintf(buf, "%d: uart:%s %s%08llX irq:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 port->line, uart_type(port),
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001684 mmio ? "mmio:0x" : "port:",
Josh Boyer4f640ef2007-07-23 18:43:44 -07001685 mmio ? (unsigned long long)port->mapbase
Alan Coxa46c9992008-02-08 04:18:53 -08001686 : (unsigned long long) port->iobase,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 port->irq);
1688
1689 if (port->type == PORT_UNKNOWN) {
1690 strcat(buf, "\n");
1691 return ret + 1;
1692 }
1693
Alan Coxa46c9992008-02-08 04:18:53 -08001694 if (capable(CAP_SYS_ADMIN)) {
George G. Davis3689a0e2007-02-14 00:33:06 -08001695 mutex_lock(&state->mutex);
1696 pm_state = state->pm_state;
1697 if (pm_state)
1698 uart_change_pm(state, 0);
Russell Kingc5f46442005-06-29 09:42:38 +01001699 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 status = port->ops->get_mctrl(port);
Russell Kingc5f46442005-06-29 09:42:38 +01001701 spin_unlock_irq(&port->lock);
George G. Davis3689a0e2007-02-14 00:33:06 -08001702 if (pm_state)
1703 uart_change_pm(state, pm_state);
1704 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 ret += sprintf(buf + ret, " tx:%d rx:%d",
1707 port->icount.tx, port->icount.rx);
1708 if (port->icount.frame)
1709 ret += sprintf(buf + ret, " fe:%d",
1710 port->icount.frame);
1711 if (port->icount.parity)
1712 ret += sprintf(buf + ret, " pe:%d",
1713 port->icount.parity);
1714 if (port->icount.brk)
1715 ret += sprintf(buf + ret, " brk:%d",
1716 port->icount.brk);
1717 if (port->icount.overrun)
1718 ret += sprintf(buf + ret, " oe:%d",
1719 port->icount.overrun);
Alan Coxa46c9992008-02-08 04:18:53 -08001720
1721#define INFOBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 if (port->mctrl & (bit)) \
1723 strncat(stat_buf, (str), sizeof(stat_buf) - \
1724 strlen(stat_buf) - 2)
Alan Coxa46c9992008-02-08 04:18:53 -08001725#define STATBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (status & (bit)) \
1727 strncat(stat_buf, (str), sizeof(stat_buf) - \
1728 strlen(stat_buf) - 2)
1729
1730 stat_buf[0] = '\0';
1731 stat_buf[1] = '\0';
1732 INFOBIT(TIOCM_RTS, "|RTS");
1733 STATBIT(TIOCM_CTS, "|CTS");
1734 INFOBIT(TIOCM_DTR, "|DTR");
1735 STATBIT(TIOCM_DSR, "|DSR");
1736 STATBIT(TIOCM_CAR, "|CD");
1737 STATBIT(TIOCM_RNG, "|RI");
1738 if (stat_buf[0])
1739 stat_buf[0] = ' ';
1740 strcat(stat_buf, "\n");
Alan Coxa46c9992008-02-08 04:18:53 -08001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 ret += sprintf(buf + ret, stat_buf);
1743 } else {
1744 strcat(buf, "\n");
1745 ret++;
1746 }
1747#undef STATBIT
1748#undef INFOBIT
1749 return ret;
1750}
1751
1752static int uart_read_proc(char *page, char **start, off_t off,
1753 int count, int *eof, void *data)
1754{
1755 struct tty_driver *ttydrv = data;
1756 struct uart_driver *drv = ttydrv->driver_state;
1757 int i, len = 0, l;
1758 off_t begin = 0;
1759
1760 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1761 "", "", "");
1762 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1763 l = uart_line_info(page + len, drv, i);
1764 len += l;
1765 if (len + begin > off + count)
1766 goto done;
1767 if (len + begin < off) {
1768 begin += len;
1769 len = 0;
1770 }
1771 }
1772 *eof = 1;
1773 done:
1774 if (off >= len + begin)
1775 return 0;
1776 *start = page + (off - begin);
1777 return (count < begin + len - off) ? count : (begin + len - off);
1778}
1779#endif
1780
Andrew Morton4a1b5502008-03-07 15:51:16 -08001781#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782/*
Russell Kingd3587882006-03-20 20:00:09 +00001783 * uart_console_write - write a console message to a serial port
1784 * @port: the port to write the message
1785 * @s: array of characters
1786 * @count: number of characters in string to write
1787 * @write: function to write character to port
1788 */
1789void uart_console_write(struct uart_port *port, const char *s,
1790 unsigned int count,
1791 void (*putchar)(struct uart_port *, int))
1792{
1793 unsigned int i;
1794
1795 for (i = 0; i < count; i++, s++) {
1796 if (*s == '\n')
1797 putchar(port, '\r');
1798 putchar(port, *s);
1799 }
1800}
1801EXPORT_SYMBOL_GPL(uart_console_write);
1802
1803/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 * Check whether an invalid uart number has been specified, and
1805 * if so, search for the first available port that does have
1806 * console support.
1807 */
1808struct uart_port * __init
1809uart_get_console(struct uart_port *ports, int nr, struct console *co)
1810{
1811 int idx = co->index;
1812
1813 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1814 ports[idx].membase == NULL))
1815 for (idx = 0; idx < nr; idx++)
1816 if (ports[idx].iobase != 0 ||
1817 ports[idx].membase != NULL)
1818 break;
1819
1820 co->index = idx;
1821
1822 return ports + idx;
1823}
1824
1825/**
1826 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1827 * @options: pointer to option string
1828 * @baud: pointer to an 'int' variable for the baud rate.
1829 * @parity: pointer to an 'int' variable for the parity.
1830 * @bits: pointer to an 'int' variable for the number of data bits.
1831 * @flow: pointer to an 'int' variable for the flow control character.
1832 *
1833 * uart_parse_options decodes a string containing the serial console
1834 * options. The format of the string is <baud><parity><bits><flow>,
1835 * eg: 115200n8r
1836 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001837void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1839{
1840 char *s = options;
1841
1842 *baud = simple_strtoul(s, NULL, 10);
1843 while (*s >= '0' && *s <= '9')
1844 s++;
1845 if (*s)
1846 *parity = *s++;
1847 if (*s)
1848 *bits = *s++ - '0';
1849 if (*s)
1850 *flow = *s;
1851}
Jason Wesself2d937f2008-04-17 20:05:37 +02001852EXPORT_SYMBOL_GPL(uart_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854struct baud_rates {
1855 unsigned int rate;
1856 unsigned int cflag;
1857};
1858
Arjan van de Vencb3592b2005-11-28 21:04:11 +00001859static const struct baud_rates baud_rates[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 { 921600, B921600 },
1861 { 460800, B460800 },
1862 { 230400, B230400 },
1863 { 115200, B115200 },
1864 { 57600, B57600 },
1865 { 38400, B38400 },
1866 { 19200, B19200 },
1867 { 9600, B9600 },
1868 { 4800, B4800 },
1869 { 2400, B2400 },
1870 { 1200, B1200 },
1871 { 0, B38400 }
1872};
1873
1874/**
1875 * uart_set_options - setup the serial console parameters
1876 * @port: pointer to the serial ports uart_port structure
1877 * @co: console pointer
1878 * @baud: baud rate
1879 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1880 * @bits: number of data bits
1881 * @flow: flow control character - 'r' (rts)
1882 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001883int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884uart_set_options(struct uart_port *port, struct console *co,
1885 int baud, int parity, int bits, int flow)
1886{
Alan Cox606d0992006-12-08 02:38:45 -08001887 struct ktermios termios;
Alan Cox149b36e2007-10-18 01:24:16 -07001888 static struct ktermios dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 int i;
1890
Russell King976ecd12005-07-03 21:05:45 +01001891 /*
1892 * Ensure that the serial console lock is initialised
1893 * early.
1894 */
1895 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07001896 lockdep_set_class(&port->lock, &port_lock_key);
Russell King976ecd12005-07-03 21:05:45 +01001897
Alan Cox606d0992006-12-08 02:38:45 -08001898 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1901
1902 /*
1903 * Construct a cflag setting.
1904 */
1905 for (i = 0; baud_rates[i].rate; i++)
1906 if (baud_rates[i].rate <= baud)
1907 break;
1908
1909 termios.c_cflag |= baud_rates[i].cflag;
1910
1911 if (bits == 7)
1912 termios.c_cflag |= CS7;
1913 else
1914 termios.c_cflag |= CS8;
1915
1916 switch (parity) {
1917 case 'o': case 'O':
1918 termios.c_cflag |= PARODD;
1919 /*fall through*/
1920 case 'e': case 'E':
1921 termios.c_cflag |= PARENB;
1922 break;
1923 }
1924
1925 if (flow == 'r')
1926 termios.c_cflag |= CRTSCTS;
1927
Yinghai Lu79492682007-07-15 23:37:25 -07001928 /*
1929 * some uarts on other side don't support no flow control.
1930 * So we set * DTR in host uart to make them happy
1931 */
1932 port->mctrl |= TIOCM_DTR;
1933
Alan Cox149b36e2007-10-18 01:24:16 -07001934 port->ops->set_termios(port, &termios, &dummy);
Jason Wesself2d937f2008-04-17 20:05:37 +02001935 /*
1936 * Allow the setting of the UART parameters with a NULL console
1937 * too:
1938 */
1939 if (co)
1940 co->cflag = termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 return 0;
1943}
Jason Wesself2d937f2008-04-17 20:05:37 +02001944EXPORT_SYMBOL_GPL(uart_set_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945#endif /* CONFIG_SERIAL_CORE_CONSOLE */
1946
1947static void uart_change_pm(struct uart_state *state, int pm_state)
1948{
1949 struct uart_port *port = state->port;
Andrew Victor1281e362006-05-16 11:28:49 +01001950
1951 if (state->pm_state != pm_state) {
1952 if (port->ops->pm)
1953 port->ops->pm(port, pm_state, state->pm_state);
1954 state->pm_state = pm_state;
1955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001958struct uart_match {
1959 struct uart_port *port;
1960 struct uart_driver *driver;
1961};
1962
1963static int serial_match_port(struct device *dev, void *data)
1964{
1965 struct uart_match *match = data;
1966 dev_t devt = MKDEV(match->driver->major, match->driver->minor) + match->port->line;
1967
1968 return dev->devt == devt; /* Actually, only one tty per port */
1969}
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1972{
1973 struct uart_state *state = drv->state + port->line;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001974 struct device *tty_dev;
1975 struct uart_match match = {port, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Ingo Molnare2862f62006-01-13 21:37:07 +00001977 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001979 if (!console_suspend_enabled && uart_console(port)) {
1980 /* we're going to avoid suspending serial console */
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07001981 mutex_unlock(&state->mutex);
1982 return 0;
1983 }
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07001984
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001985 tty_dev = device_find_child(port->dev, &match, serial_match_port);
1986 if (device_may_wakeup(tty_dev)) {
1987 enable_irq_wake(port->irq);
1988 put_device(tty_dev);
1989 mutex_unlock(&state->mutex);
1990 return 0;
1991 }
1992 port->suspended = 1;
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 if (state->info && state->info->flags & UIF_INITIALIZED) {
Russell Kingba899db2006-01-21 22:45:50 +00001995 const struct uart_ops *ops = port->ops;
Russell Kingc8c6bfa2008-02-04 22:27:52 -08001996 int tries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Russell Kinga6b93a92006-10-01 17:17:40 +01001998 state->info->flags = (state->info->flags & ~UIF_INITIALIZED)
1999 | UIF_SUSPENDED;
2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 spin_lock_irq(&port->lock);
Russell Kingb129a8c2005-08-31 10:12:14 +01002002 ops->stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 ops->set_mctrl(port, 0);
2004 ops->stop_rx(port);
2005 spin_unlock_irq(&port->lock);
2006
2007 /*
2008 * Wait for the transmitter to empty.
2009 */
Alan Coxa46c9992008-02-08 04:18:53 -08002010 for (tries = 3; !ops->tx_empty(port) && tries; tries--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 msleep(10);
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002012 if (!tries)
Alan Coxa46c9992008-02-08 04:18:53 -08002013 printk(KERN_ERR "%s%s%s%d: Unable to drain "
2014 "transmitter\n",
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002015 port->dev ? port->dev->bus_id : "",
2016 port->dev ? ": " : "",
2017 drv->dev_name, port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 ops->shutdown(port);
2020 }
2021
2022 /*
2023 * Disable the console device before suspending.
2024 */
2025 if (uart_console(port))
2026 console_stop(port->cons);
2027
2028 uart_change_pm(state, 3);
2029
Ingo Molnare2862f62006-01-13 21:37:07 +00002030 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 return 0;
2033}
2034
2035int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
2036{
2037 struct uart_state *state = drv->state + port->line;
2038
Ingo Molnare2862f62006-01-13 21:37:07 +00002039 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002041 if (!console_suspend_enabled && uart_console(port)) {
2042 /* no need to resume serial console, it wasn't suspended */
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002043 mutex_unlock(&state->mutex);
2044 return 0;
2045 }
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07002046
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002047 if (!port->suspended) {
2048 disable_irq_wake(port->irq);
2049 mutex_unlock(&state->mutex);
2050 return 0;
2051 }
2052 port->suspended = 0;
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 /*
2055 * Re-enable the console device after suspending.
2056 */
2057 if (uart_console(port)) {
Alan Cox606d0992006-12-08 02:38:45 -08002058 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 /*
2061 * First try to use the console cflag setting.
2062 */
Alan Cox606d0992006-12-08 02:38:45 -08002063 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 termios.c_cflag = port->cons->cflag;
2065
2066 /*
2067 * If that's unset, use the tty termios setting.
2068 */
2069 if (state->info && state->info->tty && termios.c_cflag == 0)
2070 termios = *state->info->tty->termios;
2071
Russell King9d778a62008-02-04 22:27:51 -08002072 uart_change_pm(state, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 port->ops->set_termios(port, &termios, NULL);
2074 console_start(port->cons);
2075 }
2076
Russell Kinga6b93a92006-10-01 17:17:40 +01002077 if (state->info && state->info->flags & UIF_SUSPENDED) {
Russell Kingba899db2006-01-21 22:45:50 +00002078 const struct uart_ops *ops = port->ops;
Russell Kingee31b332005-11-13 15:28:51 +00002079 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Russell King9d778a62008-02-04 22:27:51 -08002081 uart_change_pm(state, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 ops->set_mctrl(port, 0);
Russell Kingee31b332005-11-13 15:28:51 +00002083 ret = ops->startup(port);
2084 if (ret == 0) {
2085 uart_change_speed(state, NULL);
2086 spin_lock_irq(&port->lock);
2087 ops->set_mctrl(port, port->mctrl);
2088 ops->start_tx(port);
2089 spin_unlock_irq(&port->lock);
Russell Kinga6b93a92006-10-01 17:17:40 +01002090 state->info->flags |= UIF_INITIALIZED;
Russell Kingee31b332005-11-13 15:28:51 +00002091 } else {
2092 /*
2093 * Failed to resume - maybe hardware went away?
2094 * Clear the "initialized" flag so we won't try
2095 * to call the low level drivers shutdown method.
2096 */
Russell Kingee31b332005-11-13 15:28:51 +00002097 uart_shutdown(state);
2098 }
Russell Kinga6b93a92006-10-01 17:17:40 +01002099
2100 state->info->flags &= ~UIF_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
2102
Ingo Molnare2862f62006-01-13 21:37:07 +00002103 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 return 0;
2106}
2107
2108static inline void
2109uart_report_port(struct uart_driver *drv, struct uart_port *port)
2110{
Russell King30b7a3b2005-09-03 15:30:21 +01002111 char address[64];
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 switch (port->iotype) {
2114 case UPIO_PORT:
Russell King30b7a3b2005-09-03 15:30:21 +01002115 snprintf(address, sizeof(address),
2116 "I/O 0x%x", port->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 break;
2118 case UPIO_HUB6:
Russell King30b7a3b2005-09-03 15:30:21 +01002119 snprintf(address, sizeof(address),
2120 "I/O 0x%x offset 0x%x", port->iobase, port->hub6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 break;
2122 case UPIO_MEM:
2123 case UPIO_MEM32:
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002124 case UPIO_AU:
Zang Roy-r619113be91ec2006-06-30 02:29:58 -07002125 case UPIO_TSI:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002126 case UPIO_DWAPB:
Russell King30b7a3b2005-09-03 15:30:21 +01002127 snprintf(address, sizeof(address),
Josh Boyer4f640ef2007-07-23 18:43:44 -07002128 "MMIO 0x%llx", (unsigned long long)port->mapbase);
Russell King30b7a3b2005-09-03 15:30:21 +01002129 break;
2130 default:
2131 strlcpy(address, "*unknown*", sizeof(address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 break;
2133 }
Russell King30b7a3b2005-09-03 15:30:21 +01002134
Russell King0cf669d2005-10-31 11:42:22 +00002135 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2136 port->dev ? port->dev->bus_id : "",
2137 port->dev ? ": " : "",
Russell King30b7a3b2005-09-03 15:30:21 +01002138 drv->dev_name, port->line, address, port->irq, uart_type(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
2141static void
2142uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2143 struct uart_port *port)
2144{
2145 unsigned int flags;
2146
2147 /*
2148 * If there isn't a port here, don't do anything further.
2149 */
2150 if (!port->iobase && !port->mapbase && !port->membase)
2151 return;
2152
2153 /*
2154 * Now do the auto configuration stuff. Note that config_port
2155 * is expected to claim the resources and map the port for us.
2156 */
2157 flags = UART_CONFIG_TYPE;
2158 if (port->flags & UPF_AUTO_IRQ)
2159 flags |= UART_CONFIG_IRQ;
2160 if (port->flags & UPF_BOOT_AUTOCONF) {
2161 port->type = PORT_UNKNOWN;
2162 port->ops->config_port(port, flags);
2163 }
2164
2165 if (port->type != PORT_UNKNOWN) {
2166 unsigned long flags;
2167
2168 uart_report_port(drv, port);
2169
George G. Davis3689a0e2007-02-14 00:33:06 -08002170 /* Power up port for set_mctrl() */
2171 uart_change_pm(state, 0);
2172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 /*
2174 * Ensure that the modem control lines are de-activated.
Yinghai Luc3e46422008-02-04 22:27:46 -08002175 * keep the DTR setting that is set in uart_set_options()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 * We probably don't need a spinlock around this, but
2177 */
2178 spin_lock_irqsave(&port->lock, flags);
Yinghai Luc3e46422008-02-04 22:27:46 -08002179 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 spin_unlock_irqrestore(&port->lock, flags);
2181
2182 /*
Russell King97d97222007-09-01 21:25:09 +01002183 * If this driver supports console, and it hasn't been
2184 * successfully registered yet, try to re-register it.
2185 * It may be that the port was not available.
2186 */
2187 if (port->cons && !(port->cons->flags & CON_ENABLED))
2188 register_console(port->cons);
2189
2190 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 * Power down all ports by default, except the
2192 * console if we have one.
2193 */
2194 if (!uart_console(port))
2195 uart_change_pm(state, 3);
2196 }
2197}
2198
Jason Wesself2d937f2008-04-17 20:05:37 +02002199#ifdef CONFIG_CONSOLE_POLL
2200
2201static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2202{
2203 struct uart_driver *drv = driver->driver_state;
2204 struct uart_state *state = drv->state + line;
2205 struct uart_port *port;
2206 int baud = 9600;
2207 int bits = 8;
2208 int parity = 'n';
2209 int flow = 'n';
2210
2211 if (!state || !state->port)
2212 return -1;
2213
2214 port = state->port;
2215 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2216 return -1;
2217
2218 if (options) {
2219 uart_parse_options(options, &baud, &parity, &bits, &flow);
2220 return uart_set_options(port, NULL, baud, parity, bits, flow);
2221 }
2222
2223 return 0;
2224}
2225
2226static int uart_poll_get_char(struct tty_driver *driver, int line)
2227{
2228 struct uart_driver *drv = driver->driver_state;
2229 struct uart_state *state = drv->state + line;
2230 struct uart_port *port;
2231
2232 if (!state || !state->port)
2233 return -1;
2234
2235 port = state->port;
2236 return port->ops->poll_get_char(port);
2237}
2238
2239static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2240{
2241 struct uart_driver *drv = driver->driver_state;
2242 struct uart_state *state = drv->state + line;
2243 struct uart_port *port;
2244
2245 if (!state || !state->port)
2246 return;
2247
2248 port = state->port;
2249 port->ops->poll_put_char(port, ch);
2250}
2251#endif
2252
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002253static const struct tty_operations uart_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 .open = uart_open,
2255 .close = uart_close,
2256 .write = uart_write,
2257 .put_char = uart_put_char,
2258 .flush_chars = uart_flush_chars,
2259 .write_room = uart_write_room,
2260 .chars_in_buffer= uart_chars_in_buffer,
2261 .flush_buffer = uart_flush_buffer,
2262 .ioctl = uart_ioctl,
2263 .throttle = uart_throttle,
2264 .unthrottle = uart_unthrottle,
2265 .send_xchar = uart_send_xchar,
2266 .set_termios = uart_set_termios,
2267 .stop = uart_stop,
2268 .start = uart_start,
2269 .hangup = uart_hangup,
2270 .break_ctl = uart_break_ctl,
2271 .wait_until_sent= uart_wait_until_sent,
2272#ifdef CONFIG_PROC_FS
2273 .read_proc = uart_read_proc,
2274#endif
2275 .tiocmget = uart_tiocmget,
2276 .tiocmset = uart_tiocmset,
Jason Wesself2d937f2008-04-17 20:05:37 +02002277#ifdef CONFIG_CONSOLE_POLL
2278 .poll_init = uart_poll_init,
2279 .poll_get_char = uart_poll_get_char,
2280 .poll_put_char = uart_poll_put_char,
2281#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282};
2283
2284/**
2285 * uart_register_driver - register a driver with the uart core layer
2286 * @drv: low level driver structure
2287 *
2288 * Register a uart driver with the core driver. We in turn register
2289 * with the tty layer, and initialise the core driver per-port state.
2290 *
2291 * We have a proc file in /proc/tty/driver which is named after the
2292 * normal driver.
2293 *
2294 * drv->port should be NULL, and the per-port structures should be
2295 * registered using uart_add_one_port after this call has succeeded.
2296 */
2297int uart_register_driver(struct uart_driver *drv)
2298{
2299 struct tty_driver *normal = NULL;
2300 int i, retval;
2301
2302 BUG_ON(drv->state);
2303
2304 /*
2305 * Maybe we should be using a slab cache for this, especially if
2306 * we have a large number of ports to handle.
2307 */
Burman Yan8f31bb32007-02-14 00:33:07 -08002308 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 retval = -ENOMEM;
2310 if (!drv->state)
2311 goto out;
2312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 normal = alloc_tty_driver(drv->nr);
2314 if (!normal)
2315 goto out;
2316
2317 drv->tty_driver = normal;
2318
2319 normal->owner = drv->owner;
2320 normal->driver_name = drv->driver_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 normal->name = drv->dev_name;
2322 normal->major = drv->major;
2323 normal->minor_start = drv->minor;
2324 normal->type = TTY_DRIVER_TYPE_SERIAL;
2325 normal->subtype = SERIAL_TYPE_NORMAL;
2326 normal->init_termios = tty_std_termios;
2327 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -08002328 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07002329 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 normal->driver_state = drv;
2331 tty_set_operations(normal, &uart_ops);
2332
2333 /*
2334 * Initialise the UART state(s).
2335 */
2336 for (i = 0; i < drv->nr; i++) {
2337 struct uart_state *state = drv->state + i;
2338
2339 state->close_delay = 500; /* .5 seconds */
2340 state->closing_wait = 30000; /* 30 seconds */
2341
Ingo Molnare2862f62006-01-13 21:37:07 +00002342 mutex_init(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
2344
2345 retval = tty_register_driver(normal);
2346 out:
2347 if (retval < 0) {
2348 put_tty_driver(normal);
2349 kfree(drv->state);
2350 }
2351 return retval;
2352}
2353
2354/**
2355 * uart_unregister_driver - remove a driver from the uart core layer
2356 * @drv: low level driver structure
2357 *
2358 * Remove all references to a driver from the core driver. The low
2359 * level driver must have removed all its ports via the
2360 * uart_remove_one_port() if it registered them with uart_add_one_port().
2361 * (ie, drv->port == NULL)
2362 */
2363void uart_unregister_driver(struct uart_driver *drv)
2364{
2365 struct tty_driver *p = drv->tty_driver;
2366 tty_unregister_driver(p);
2367 put_tty_driver(p);
2368 kfree(drv->state);
2369 drv->tty_driver = NULL;
2370}
2371
2372struct tty_driver *uart_console_device(struct console *co, int *index)
2373{
2374 struct uart_driver *p = co->data;
2375 *index = co->index;
2376 return p->tty_driver;
2377}
2378
2379/**
2380 * uart_add_one_port - attach a driver-defined port structure
2381 * @drv: pointer to the uart low level driver structure for this port
2382 * @port: uart port structure to use for this port.
2383 *
2384 * This allows the driver to register its own uart_port structure
2385 * with the core driver. The main purpose is to allow the low
2386 * level uart drivers to expand uart_port, rather than having yet
2387 * more levels of structures.
2388 */
2389int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2390{
2391 struct uart_state *state;
2392 int ret = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002393 struct device *tty_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395 BUG_ON(in_interrupt());
2396
2397 if (port->line >= drv->nr)
2398 return -EINVAL;
2399
2400 state = drv->state + port->line;
2401
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002402 mutex_lock(&port_mutex);
Russell King68ac64c2006-04-30 11:13:50 +01002403 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (state->port) {
2405 ret = -EINVAL;
2406 goto out;
2407 }
2408
2409 state->port = port;
Russell King97d97222007-09-01 21:25:09 +01002410 state->pm_state = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 port->cons = drv->cons;
2413 port->info = state->info;
2414
Russell King976ecd12005-07-03 21:05:45 +01002415 /*
2416 * If this port is a console, then the spinlock is already
2417 * initialised.
2418 */
Ingo Molnar13e83592006-07-03 00:25:03 -07002419 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
Russell King976ecd12005-07-03 21:05:45 +01002420 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07002421 lockdep_set_class(&port->lock, &port_lock_key);
2422 }
Russell King976ecd12005-07-03 21:05:45 +01002423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 uart_configure_port(drv, state, port);
2425
2426 /*
2427 * Register the port whether it's detected or not. This allows
2428 * setserial to be used to alter this ports parameters.
2429 */
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002430 tty_dev = tty_register_device(drv->tty_driver, port->line, port->dev);
2431 if (likely(!IS_ERR(tty_dev))) {
Alan Stern74081f82008-03-19 22:35:13 +01002432 device_init_wakeup(tty_dev, 1);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002433 device_set_wakeup_enable(tty_dev, 0);
2434 } else
2435 printk(KERN_ERR "Cannot register tty device on line %d\n",
2436 port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438 /*
Russell King68ac64c2006-04-30 11:13:50 +01002439 * Ensure UPF_DEAD is not set.
2440 */
2441 port->flags &= ~UPF_DEAD;
2442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 out:
Russell King68ac64c2006-04-30 11:13:50 +01002444 mutex_unlock(&state->mutex);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002445 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
2447 return ret;
2448}
2449
2450/**
2451 * uart_remove_one_port - detach a driver defined port structure
2452 * @drv: pointer to the uart low level driver structure for this port
2453 * @port: uart port structure for this port
2454 *
2455 * This unhooks (and hangs up) the specified port structure from the
2456 * core driver. No further calls will be made to the low-level code
2457 * for this port.
2458 */
2459int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2460{
2461 struct uart_state *state = drv->state + port->line;
Russell King68ac64c2006-04-30 11:13:50 +01002462 struct uart_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 BUG_ON(in_interrupt());
2465
2466 if (state->port != port)
2467 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2468 state->port, port);
2469
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002470 mutex_lock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 /*
Russell King68ac64c2006-04-30 11:13:50 +01002473 * Mark the port "dead" - this prevents any opens from
2474 * succeeding while we shut down the port.
2475 */
2476 mutex_lock(&state->mutex);
2477 port->flags |= UPF_DEAD;
2478 mutex_unlock(&state->mutex);
2479
2480 /*
Greg Kroah-Hartmanaa4148c2005-06-20 21:15:16 -07002481 * Remove the devices from the tty layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 */
2483 tty_unregister_device(drv->tty_driver, port->line);
2484
Russell King68ac64c2006-04-30 11:13:50 +01002485 info = state->info;
2486 if (info && info->tty)
2487 tty_vhangup(info->tty);
2488
2489 /*
2490 * All users of this port should now be disconnected from
2491 * this driver, and the port shut down. We should be the
2492 * only thread fiddling with this port from now on.
2493 */
2494 state->info = NULL;
2495
2496 /*
2497 * Free the port IO and memory resources, if any.
2498 */
2499 if (port->type != PORT_UNKNOWN)
2500 port->ops->release_port(port);
2501
2502 /*
2503 * Indicate that there isn't a port here anymore.
2504 */
2505 port->type = PORT_UNKNOWN;
2506
2507 /*
2508 * Kill the tasklet, and free resources.
2509 */
2510 if (info) {
2511 tasklet_kill(&info->tlet);
2512 kfree(info);
2513 }
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 state->port = NULL;
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002516 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 return 0;
2519}
2520
2521/*
2522 * Are the two ports equivalent?
2523 */
2524int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2525{
2526 if (port1->iotype != port2->iotype)
2527 return 0;
2528
2529 switch (port1->iotype) {
2530 case UPIO_PORT:
2531 return (port1->iobase == port2->iobase);
2532 case UPIO_HUB6:
2533 return (port1->iobase == port2->iobase) &&
2534 (port1->hub6 == port2->hub6);
2535 case UPIO_MEM:
Sergei Shtylyovd21b55d2006-08-28 19:49:03 +04002536 case UPIO_MEM32:
2537 case UPIO_AU:
2538 case UPIO_TSI:
Marc St-Jeanbeab6972007-05-06 14:48:45 -07002539 case UPIO_DWAPB:
Benjamin Herrenschmidt1624f002006-01-04 18:09:44 +00002540 return (port1->mapbase == port2->mapbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 }
2542 return 0;
2543}
2544EXPORT_SYMBOL(uart_match_port);
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546EXPORT_SYMBOL(uart_write_wakeup);
2547EXPORT_SYMBOL(uart_register_driver);
2548EXPORT_SYMBOL(uart_unregister_driver);
2549EXPORT_SYMBOL(uart_suspend_port);
2550EXPORT_SYMBOL(uart_resume_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551EXPORT_SYMBOL(uart_add_one_port);
2552EXPORT_SYMBOL(uart_remove_one_port);
2553
2554MODULE_DESCRIPTION("Serial driver core");
2555MODULE_LICENSE("GPL");