blob: d7e28ab4c3132a37d959e268629f5b5a5a1545ec [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
40#undef DEBUG
41#ifdef DEBUG
42#define DPRINTK(x...) printk(x)
43#else
44#define DPRINTK(x...) do { } while (0)
45#endif
46
47/*
48 * This is used to lock changes in serial line configuration.
49 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +000050static DEFINE_MUTEX(port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Ingo Molnar13e83592006-07-03 00:25:03 -070052/*
53 * lockdep: port->lock is initialized in two places, but we
54 * want only one lock-class:
55 */
56static struct lock_class_key port_lock_key;
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
59
60#define uart_users(state) ((state)->count + ((state)->info ? (state)->info->blocked_open : 0))
61
62#ifdef CONFIG_SERIAL_CORE_CONSOLE
63#define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
64#else
65#define uart_console(port) (0)
66#endif
67
68static void uart_change_speed(struct uart_state *state, struct termios *old_termios);
69static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
70static void uart_change_pm(struct uart_state *state, int pm_state);
71
72/*
73 * This routine is used by the interrupt handler to schedule processing in
74 * the software interrupt portion of the driver.
75 */
76void uart_write_wakeup(struct uart_port *port)
77{
78 struct uart_info *info = port->info;
Pavel Machekd5f735e2006-03-07 21:55:20 -080079 /*
80 * This means you called this function _after_ the port was
81 * closed. No cookie for you.
82 */
83 BUG_ON(!info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 tasklet_schedule(&info->tlet);
85}
86
87static void uart_stop(struct tty_struct *tty)
88{
89 struct uart_state *state = tty->driver_data;
90 struct uart_port *port = state->port;
91 unsigned long flags;
92
93 spin_lock_irqsave(&port->lock, flags);
Russell Kingb129a8c2005-08-31 10:12:14 +010094 port->ops->stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 spin_unlock_irqrestore(&port->lock, flags);
96}
97
98static void __uart_start(struct tty_struct *tty)
99{
100 struct uart_state *state = tty->driver_data;
101 struct uart_port *port = state->port;
102
103 if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&
104 !tty->stopped && !tty->hw_stopped)
Russell Kingb129a8c2005-08-31 10:12:14 +0100105 port->ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108static void uart_start(struct tty_struct *tty)
109{
110 struct uart_state *state = tty->driver_data;
111 struct uart_port *port = state->port;
112 unsigned long flags;
113
114 spin_lock_irqsave(&port->lock, flags);
115 __uart_start(tty);
116 spin_unlock_irqrestore(&port->lock, flags);
117}
118
119static void uart_tasklet_action(unsigned long data)
120{
121 struct uart_state *state = (struct uart_state *)data;
122 tty_wakeup(state->info->tty);
123}
124
125static inline void
126uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
127{
128 unsigned long flags;
129 unsigned int old;
130
131 spin_lock_irqsave(&port->lock, flags);
132 old = port->mctrl;
133 port->mctrl = (old & ~clear) | set;
134 if (old != port->mctrl)
135 port->ops->set_mctrl(port, port->mctrl);
136 spin_unlock_irqrestore(&port->lock, flags);
137}
138
139#define uart_set_mctrl(port,set) uart_update_mctrl(port,set,0)
140#define uart_clear_mctrl(port,clear) uart_update_mctrl(port,0,clear)
141
142/*
143 * Startup the port. This will be called once per open. All calls
144 * will be serialised by the per-port semaphore.
145 */
146static int uart_startup(struct uart_state *state, int init_hw)
147{
148 struct uart_info *info = state->info;
149 struct uart_port *port = state->port;
150 unsigned long page;
151 int retval = 0;
152
153 if (info->flags & UIF_INITIALIZED)
154 return 0;
155
156 /*
157 * Set the TTY IO error marker - we will only clear this
158 * once we have successfully opened the port. Also set
159 * up the tty->alt_speed kludge
160 */
Jayachandran Ca2436b22005-10-30 23:26:16 +0000161 set_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 if (port->type == PORT_UNKNOWN)
164 return 0;
165
166 /*
167 * Initialise and allocate the transmit and temporary
168 * buffer.
169 */
170 if (!info->xmit.buf) {
171 page = get_zeroed_page(GFP_KERNEL);
172 if (!page)
173 return -ENOMEM;
174
175 info->xmit.buf = (unsigned char *) page;
176 uart_circ_clear(&info->xmit);
177 }
178
179 retval = port->ops->startup(port);
180 if (retval == 0) {
181 if (init_hw) {
182 /*
183 * Initialise the hardware port settings.
184 */
185 uart_change_speed(state, NULL);
186
187 /*
188 * Setup the RTS and DTR signals once the
189 * port is open and ready to respond.
190 */
191 if (info->tty->termios->c_cflag & CBAUD)
192 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
193 }
194
Russell King0dd7a1a2005-06-29 18:40:53 +0100195 if (info->flags & UIF_CTS_FLOW) {
196 spin_lock_irq(&port->lock);
197 if (!(port->ops->get_mctrl(port) & TIOCM_CTS))
198 info->tty->hw_stopped = 1;
199 spin_unlock_irq(&port->lock);
200 }
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 info->flags |= UIF_INITIALIZED;
203
204 clear_bit(TTY_IO_ERROR, &info->tty->flags);
205 }
206
207 if (retval && capable(CAP_SYS_ADMIN))
208 retval = 0;
209
210 return retval;
211}
212
213/*
214 * This routine will shutdown a serial port; interrupts are disabled, and
215 * DTR is dropped if the hangup on close termio flag is on. Calls to
216 * uart_shutdown are serialised by the per-port semaphore.
217 */
218static void uart_shutdown(struct uart_state *state)
219{
220 struct uart_info *info = state->info;
221 struct uart_port *port = state->port;
222
Russell Kingee31b332005-11-13 15:28:51 +0000223 /*
224 * Set the TTY IO error marker
225 */
226 if (info->tty)
227 set_bit(TTY_IO_ERROR, &info->tty->flags);
228
229 if (info->flags & UIF_INITIALIZED) {
230 info->flags &= ~UIF_INITIALIZED;
231
232 /*
233 * Turn off DTR and RTS early.
234 */
235 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
236 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
237
238 /*
239 * clear delta_msr_wait queue to avoid mem leaks: we may free
240 * the irq here so the queue might never be woken up. Note
241 * that we won't end up waiting on delta_msr_wait again since
242 * any outstanding file descriptors should be pointing at
243 * hung_up_tty_fops now.
244 */
245 wake_up_interruptible(&info->delta_msr_wait);
246
247 /*
248 * Free the IRQ and disable the port.
249 */
250 port->ops->shutdown(port);
251
252 /*
253 * Ensure that the IRQ handler isn't running on another CPU.
254 */
255 synchronize_irq(port->irq);
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /*
Russell Kingee31b332005-11-13 15:28:51 +0000259 * kill off our tasklet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
Russell Kingee31b332005-11-13 15:28:51 +0000261 tasklet_kill(&info->tlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /*
264 * Free the transmit buffer page.
265 */
266 if (info->xmit.buf) {
267 free_page((unsigned long)info->xmit.buf);
268 info->xmit.buf = NULL;
269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272/**
273 * uart_update_timeout - update per-port FIFO timeout.
274 * @port: uart_port structure describing the port
275 * @cflag: termios cflag value
276 * @baud: speed of the port
277 *
278 * Set the port FIFO timeout value. The @cflag value should
279 * reflect the actual hardware settings.
280 */
281void
282uart_update_timeout(struct uart_port *port, unsigned int cflag,
283 unsigned int baud)
284{
285 unsigned int bits;
286
287 /* byte size and parity */
288 switch (cflag & CSIZE) {
289 case CS5:
290 bits = 7;
291 break;
292 case CS6:
293 bits = 8;
294 break;
295 case CS7:
296 bits = 9;
297 break;
298 default:
299 bits = 10;
300 break; // CS8
301 }
302
303 if (cflag & CSTOPB)
304 bits++;
305 if (cflag & PARENB)
306 bits++;
307
308 /*
309 * The total number of bits to be transmitted in the fifo.
310 */
311 bits = bits * port->fifosize;
312
313 /*
314 * Figure the timeout to send the above number of bits.
315 * Add .02 seconds of slop
316 */
317 port->timeout = (HZ * bits) / baud + HZ/50;
318}
319
320EXPORT_SYMBOL(uart_update_timeout);
321
322/**
323 * uart_get_baud_rate - return baud rate for a particular port
324 * @port: uart_port structure describing the port in question.
325 * @termios: desired termios settings.
326 * @old: old termios (or NULL)
327 * @min: minimum acceptable baud rate
328 * @max: maximum acceptable baud rate
329 *
330 * Decode the termios structure into a numeric baud rate,
331 * taking account of the magic 38400 baud rate (with spd_*
332 * flags), and mapping the %B0 rate to 9600 baud.
333 *
334 * If the new baud rate is invalid, try the old termios setting.
335 * If it's still invalid, we try 9600 baud.
336 *
337 * Update the @termios structure to reflect the baud rate
338 * we're actually going to be using.
339 */
340unsigned int
341uart_get_baud_rate(struct uart_port *port, struct termios *termios,
342 struct termios *old, unsigned int min, unsigned int max)
343{
344 unsigned int try, baud, altbaud = 38400;
Russell King0077d452006-01-21 23:03:28 +0000345 upf_t flags = port->flags & UPF_SPD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if (flags == UPF_SPD_HI)
348 altbaud = 57600;
349 if (flags == UPF_SPD_VHI)
350 altbaud = 115200;
351 if (flags == UPF_SPD_SHI)
352 altbaud = 230400;
353 if (flags == UPF_SPD_WARP)
354 altbaud = 460800;
355
356 for (try = 0; try < 2; try++) {
357 baud = tty_termios_baud_rate(termios);
358
359 /*
360 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
361 * Die! Die! Die!
362 */
363 if (baud == 38400)
364 baud = altbaud;
365
366 /*
367 * Special case: B0 rate.
368 */
369 if (baud == 0)
370 baud = 9600;
371
372 if (baud >= min && baud <= max)
373 return baud;
374
375 /*
376 * Oops, the quotient was zero. Try again with
377 * the old baud rate if possible.
378 */
379 termios->c_cflag &= ~CBAUD;
380 if (old) {
381 termios->c_cflag |= old->c_cflag & CBAUD;
382 old = NULL;
383 continue;
384 }
385
386 /*
387 * As a last resort, if the quotient is zero,
388 * default to 9600 bps
389 */
390 termios->c_cflag |= B9600;
391 }
392
393 return 0;
394}
395
396EXPORT_SYMBOL(uart_get_baud_rate);
397
398/**
399 * uart_get_divisor - return uart clock divisor
400 * @port: uart_port structure describing the port.
401 * @baud: desired baud rate
402 *
403 * Calculate the uart clock divisor for the port.
404 */
405unsigned int
406uart_get_divisor(struct uart_port *port, unsigned int baud)
407{
408 unsigned int quot;
409
410 /*
411 * Old custom speed handling.
412 */
413 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
414 quot = port->custom_divisor;
415 else
416 quot = (port->uartclk + (8 * baud)) / (16 * baud);
417
418 return quot;
419}
420
421EXPORT_SYMBOL(uart_get_divisor);
422
423static void
424uart_change_speed(struct uart_state *state, struct termios *old_termios)
425{
426 struct tty_struct *tty = state->info->tty;
427 struct uart_port *port = state->port;
428 struct termios *termios;
429
430 /*
431 * If we have no tty, termios, or the port does not exist,
432 * then we can't set the parameters for this port.
433 */
434 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
435 return;
436
437 termios = tty->termios;
438
439 /*
440 * Set flags based on termios cflag
441 */
442 if (termios->c_cflag & CRTSCTS)
443 state->info->flags |= UIF_CTS_FLOW;
444 else
445 state->info->flags &= ~UIF_CTS_FLOW;
446
447 if (termios->c_cflag & CLOCAL)
448 state->info->flags &= ~UIF_CHECK_CD;
449 else
450 state->info->flags |= UIF_CHECK_CD;
451
452 port->ops->set_termios(port, termios, old_termios);
453}
454
455static inline void
456__uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
457{
458 unsigned long flags;
459
460 if (!circ->buf)
461 return;
462
463 spin_lock_irqsave(&port->lock, flags);
464 if (uart_circ_chars_free(circ) != 0) {
465 circ->buf[circ->head] = c;
466 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
467 }
468 spin_unlock_irqrestore(&port->lock, flags);
469}
470
471static void uart_put_char(struct tty_struct *tty, unsigned char ch)
472{
473 struct uart_state *state = tty->driver_data;
474
475 __uart_put_char(state->port, &state->info->xmit, ch);
476}
477
478static void uart_flush_chars(struct tty_struct *tty)
479{
480 uart_start(tty);
481}
482
483static int
Pavel Machekd5f735e2006-03-07 21:55:20 -0800484uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct uart_state *state = tty->driver_data;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800487 struct uart_port *port;
488 struct circ_buf *circ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 unsigned long flags;
490 int c, ret = 0;
491
Pavel Machekd5f735e2006-03-07 21:55:20 -0800492 /*
493 * This means you called this function _after_ the port was
494 * closed. No cookie for you.
495 */
496 if (!state || !state->info) {
497 WARN_ON(1);
498 return -EL3HLT;
499 }
500
501 port = state->port;
502 circ = &state->info->xmit;
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (!circ->buf)
505 return 0;
506
507 spin_lock_irqsave(&port->lock, flags);
508 while (1) {
509 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
510 if (count < c)
511 c = count;
512 if (c <= 0)
513 break;
514 memcpy(circ->buf + circ->head, buf, c);
515 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
516 buf += c;
517 count -= c;
518 ret += c;
519 }
520 spin_unlock_irqrestore(&port->lock, flags);
521
522 uart_start(tty);
523 return ret;
524}
525
526static int uart_write_room(struct tty_struct *tty)
527{
528 struct uart_state *state = tty->driver_data;
529
530 return uart_circ_chars_free(&state->info->xmit);
531}
532
533static int uart_chars_in_buffer(struct tty_struct *tty)
534{
535 struct uart_state *state = tty->driver_data;
536
537 return uart_circ_chars_pending(&state->info->xmit);
538}
539
540static void uart_flush_buffer(struct tty_struct *tty)
541{
542 struct uart_state *state = tty->driver_data;
543 struct uart_port *port = state->port;
544 unsigned long flags;
545
Pavel Machekd5f735e2006-03-07 21:55:20 -0800546 /*
547 * This means you called this function _after_ the port was
548 * closed. No cookie for you.
549 */
550 if (!state || !state->info) {
551 WARN_ON(1);
552 return;
553 }
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 DPRINTK("uart_flush_buffer(%d) called\n", tty->index);
556
557 spin_lock_irqsave(&port->lock, flags);
558 uart_circ_clear(&state->info->xmit);
559 spin_unlock_irqrestore(&port->lock, flags);
560 tty_wakeup(tty);
561}
562
563/*
564 * This function is used to send a high-priority XON/XOFF character to
565 * the device
566 */
567static void uart_send_xchar(struct tty_struct *tty, char ch)
568{
569 struct uart_state *state = tty->driver_data;
570 struct uart_port *port = state->port;
571 unsigned long flags;
572
573 if (port->ops->send_xchar)
574 port->ops->send_xchar(port, ch);
575 else {
576 port->x_char = ch;
577 if (ch) {
578 spin_lock_irqsave(&port->lock, flags);
Russell Kingb129a8c2005-08-31 10:12:14 +0100579 port->ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 spin_unlock_irqrestore(&port->lock, flags);
581 }
582 }
583}
584
585static void uart_throttle(struct tty_struct *tty)
586{
587 struct uart_state *state = tty->driver_data;
588
589 if (I_IXOFF(tty))
590 uart_send_xchar(tty, STOP_CHAR(tty));
591
592 if (tty->termios->c_cflag & CRTSCTS)
593 uart_clear_mctrl(state->port, TIOCM_RTS);
594}
595
596static void uart_unthrottle(struct tty_struct *tty)
597{
598 struct uart_state *state = tty->driver_data;
599 struct uart_port *port = state->port;
600
601 if (I_IXOFF(tty)) {
602 if (port->x_char)
603 port->x_char = 0;
604 else
605 uart_send_xchar(tty, START_CHAR(tty));
606 }
607
608 if (tty->termios->c_cflag & CRTSCTS)
609 uart_set_mctrl(port, TIOCM_RTS);
610}
611
612static int uart_get_info(struct uart_state *state,
613 struct serial_struct __user *retinfo)
614{
615 struct uart_port *port = state->port;
616 struct serial_struct tmp;
617
618 memset(&tmp, 0, sizeof(tmp));
619 tmp.type = port->type;
620 tmp.line = port->line;
621 tmp.port = port->iobase;
622 if (HIGH_BITS_OFFSET)
623 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
624 tmp.irq = port->irq;
625 tmp.flags = port->flags;
626 tmp.xmit_fifo_size = port->fifosize;
627 tmp.baud_base = port->uartclk / 16;
628 tmp.close_delay = state->close_delay / 10;
629 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
630 ASYNC_CLOSING_WAIT_NONE :
631 state->closing_wait / 10;
632 tmp.custom_divisor = port->custom_divisor;
633 tmp.hub6 = port->hub6;
634 tmp.io_type = port->iotype;
635 tmp.iomem_reg_shift = port->regshift;
636 tmp.iomem_base = (void *)port->mapbase;
637
638 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
639 return -EFAULT;
640 return 0;
641}
642
643static int uart_set_info(struct uart_state *state,
644 struct serial_struct __user *newinfo)
645{
646 struct serial_struct new_serial;
647 struct uart_port *port = state->port;
648 unsigned long new_port;
Russell King0077d452006-01-21 23:03:28 +0000649 unsigned int change_irq, change_port, closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 unsigned int old_custom_divisor, close_delay;
Russell King0077d452006-01-21 23:03:28 +0000651 upf_t old_flags, new_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 int retval = 0;
653
654 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
655 return -EFAULT;
656
657 new_port = new_serial.port;
658 if (HIGH_BITS_OFFSET)
659 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
660
661 new_serial.irq = irq_canonicalize(new_serial.irq);
662 close_delay = new_serial.close_delay * 10;
663 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
664 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
665
666 /*
667 * This semaphore protects state->count. It is also
668 * very useful to prevent opens. Also, take the
669 * port configuration semaphore to make sure that a
670 * module insertion/removal doesn't change anything
671 * under us.
672 */
Ingo Molnare2862f62006-01-13 21:37:07 +0000673 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 change_irq = new_serial.irq != port->irq;
676
677 /*
678 * Since changing the 'type' of the port changes its resource
679 * allocations, we should treat type changes the same as
680 * IO port changes.
681 */
682 change_port = new_port != port->iobase ||
683 (unsigned long)new_serial.iomem_base != port->mapbase ||
684 new_serial.hub6 != port->hub6 ||
685 new_serial.io_type != port->iotype ||
686 new_serial.iomem_reg_shift != port->regshift ||
687 new_serial.type != port->type;
688
689 old_flags = port->flags;
Russell King0077d452006-01-21 23:03:28 +0000690 new_flags = new_serial.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 old_custom_divisor = port->custom_divisor;
692
693 if (!capable(CAP_SYS_ADMIN)) {
694 retval = -EPERM;
695 if (change_irq || change_port ||
696 (new_serial.baud_base != port->uartclk / 16) ||
697 (close_delay != state->close_delay) ||
698 (closing_wait != state->closing_wait) ||
Russell King947deee2006-07-02 20:45:51 +0100699 (new_serial.xmit_fifo_size &&
700 new_serial.xmit_fifo_size != port->fifosize) ||
Russell King0077d452006-01-21 23:03:28 +0000701 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 goto exit;
703 port->flags = ((port->flags & ~UPF_USR_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000704 (new_flags & UPF_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 port->custom_divisor = new_serial.custom_divisor;
706 goto check_and_exit;
707 }
708
709 /*
710 * Ask the low level driver to verify the settings.
711 */
712 if (port->ops->verify_port)
713 retval = port->ops->verify_port(port, &new_serial);
714
715 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
716 (new_serial.baud_base < 9600))
717 retval = -EINVAL;
718
719 if (retval)
720 goto exit;
721
722 if (change_port || change_irq) {
723 retval = -EBUSY;
724
725 /*
726 * Make sure that we are the sole user of this port.
727 */
728 if (uart_users(state) > 1)
729 goto exit;
730
731 /*
732 * We need to shutdown the serial port at the old
733 * port/type/irq combination.
734 */
735 uart_shutdown(state);
736 }
737
738 if (change_port) {
739 unsigned long old_iobase, old_mapbase;
740 unsigned int old_type, old_iotype, old_hub6, old_shift;
741
742 old_iobase = port->iobase;
743 old_mapbase = port->mapbase;
744 old_type = port->type;
745 old_hub6 = port->hub6;
746 old_iotype = port->iotype;
747 old_shift = port->regshift;
748
749 /*
750 * Free and release old regions
751 */
752 if (old_type != PORT_UNKNOWN)
753 port->ops->release_port(port);
754
755 port->iobase = new_port;
756 port->type = new_serial.type;
757 port->hub6 = new_serial.hub6;
758 port->iotype = new_serial.io_type;
759 port->regshift = new_serial.iomem_reg_shift;
760 port->mapbase = (unsigned long)new_serial.iomem_base;
761
762 /*
763 * Claim and map the new regions
764 */
765 if (port->type != PORT_UNKNOWN) {
766 retval = port->ops->request_port(port);
767 } else {
768 /* Always success - Jean II */
769 retval = 0;
770 }
771
772 /*
773 * If we fail to request resources for the
774 * new port, try to restore the old settings.
775 */
776 if (retval && old_type != PORT_UNKNOWN) {
777 port->iobase = old_iobase;
778 port->type = old_type;
779 port->hub6 = old_hub6;
780 port->iotype = old_iotype;
781 port->regshift = old_shift;
782 port->mapbase = old_mapbase;
783 retval = port->ops->request_port(port);
784 /*
785 * If we failed to restore the old settings,
786 * we fail like this.
787 */
788 if (retval)
789 port->type = PORT_UNKNOWN;
790
791 /*
792 * We failed anyway.
793 */
794 retval = -EBUSY;
795 }
796 }
797
798 port->irq = new_serial.irq;
799 port->uartclk = new_serial.baud_base * 16;
800 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000801 (new_flags & UPF_CHANGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 port->custom_divisor = new_serial.custom_divisor;
803 state->close_delay = close_delay;
804 state->closing_wait = closing_wait;
Russell King947deee2006-07-02 20:45:51 +0100805 if (new_serial.xmit_fifo_size)
806 port->fifosize = new_serial.xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (state->info->tty)
808 state->info->tty->low_latency =
809 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
810
811 check_and_exit:
812 retval = 0;
813 if (port->type == PORT_UNKNOWN)
814 goto exit;
815 if (state->info->flags & UIF_INITIALIZED) {
816 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
817 old_custom_divisor != port->custom_divisor) {
818 /*
819 * If they're setting up a custom divisor or speed,
820 * instead of clearing it, then bitch about it. No
821 * need to rate-limit; it's CAP_SYS_ADMIN only.
822 */
823 if (port->flags & UPF_SPD_MASK) {
824 char buf[64];
825 printk(KERN_NOTICE
826 "%s sets custom speed on %s. This "
827 "is deprecated.\n", current->comm,
828 tty_name(state->info->tty, buf));
829 }
830 uart_change_speed(state, NULL);
831 }
832 } else
833 retval = uart_startup(state, 1);
834 exit:
Ingo Molnare2862f62006-01-13 21:37:07 +0000835 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return retval;
837}
838
839
840/*
841 * uart_get_lsr_info - get line status register info.
842 * Note: uart_ioctl protects us against hangups.
843 */
844static int uart_get_lsr_info(struct uart_state *state,
845 unsigned int __user *value)
846{
847 struct uart_port *port = state->port;
848 unsigned int result;
849
850 result = port->ops->tx_empty(port);
851
852 /*
853 * If we're about to load something into the transmit
854 * register, we'll pretend the transmitter isn't empty to
855 * avoid a race condition (depending on when the transmit
856 * interrupt happens).
857 */
858 if (port->x_char ||
859 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
860 !state->info->tty->stopped && !state->info->tty->hw_stopped))
861 result &= ~TIOCSER_TEMT;
862
863 return put_user(result, value);
864}
865
866static int uart_tiocmget(struct tty_struct *tty, struct file *file)
867{
868 struct uart_state *state = tty->driver_data;
869 struct uart_port *port = state->port;
870 int result = -EIO;
871
Ingo Molnare2862f62006-01-13 21:37:07 +0000872 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if ((!file || !tty_hung_up_p(file)) &&
874 !(tty->flags & (1 << TTY_IO_ERROR))) {
875 result = port->mctrl;
Russell Kingc5f46442005-06-29 09:42:38 +0100876
877 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 result |= port->ops->get_mctrl(port);
Russell Kingc5f46442005-06-29 09:42:38 +0100879 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000881 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 return result;
884}
885
886static int
887uart_tiocmset(struct tty_struct *tty, struct file *file,
888 unsigned int set, unsigned int clear)
889{
890 struct uart_state *state = tty->driver_data;
891 struct uart_port *port = state->port;
892 int ret = -EIO;
893
Ingo Molnare2862f62006-01-13 21:37:07 +0000894 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if ((!file || !tty_hung_up_p(file)) &&
896 !(tty->flags & (1 << TTY_IO_ERROR))) {
897 uart_update_mctrl(port, set, clear);
898 ret = 0;
899 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000900 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return ret;
902}
903
904static void uart_break_ctl(struct tty_struct *tty, int break_state)
905{
906 struct uart_state *state = tty->driver_data;
907 struct uart_port *port = state->port;
908
909 BUG_ON(!kernel_locked());
910
Ingo Molnare2862f62006-01-13 21:37:07 +0000911 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 if (port->type != PORT_UNKNOWN)
914 port->ops->break_ctl(port, break_state);
915
Ingo Molnare2862f62006-01-13 21:37:07 +0000916 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
919static int uart_do_autoconfig(struct uart_state *state)
920{
921 struct uart_port *port = state->port;
922 int flags, ret;
923
924 if (!capable(CAP_SYS_ADMIN))
925 return -EPERM;
926
927 /*
928 * Take the per-port semaphore. This prevents count from
929 * changing, and hence any extra opens of the port while
930 * we're auto-configuring.
931 */
Ingo Molnare2862f62006-01-13 21:37:07 +0000932 if (mutex_lock_interruptible(&state->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return -ERESTARTSYS;
934
935 ret = -EBUSY;
936 if (uart_users(state) == 1) {
937 uart_shutdown(state);
938
939 /*
940 * If we already have a port type configured,
941 * we must release its resources.
942 */
943 if (port->type != PORT_UNKNOWN)
944 port->ops->release_port(port);
945
946 flags = UART_CONFIG_TYPE;
947 if (port->flags & UPF_AUTO_IRQ)
948 flags |= UART_CONFIG_IRQ;
949
950 /*
951 * This will claim the ports resources if
952 * a port is found.
953 */
954 port->ops->config_port(port, flags);
955
956 ret = uart_startup(state, 1);
957 }
Ingo Molnare2862f62006-01-13 21:37:07 +0000958 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return ret;
960}
961
962/*
963 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
964 * - mask passed in arg for lines of interest
965 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
966 * Caller should use TIOCGICOUNT to see which one it was
967 */
968static int
969uart_wait_modem_status(struct uart_state *state, unsigned long arg)
970{
971 struct uart_port *port = state->port;
972 DECLARE_WAITQUEUE(wait, current);
973 struct uart_icount cprev, cnow;
974 int ret;
975
976 /*
977 * note the counters on entry
978 */
979 spin_lock_irq(&port->lock);
980 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
981
982 /*
983 * Force modem status interrupts on
984 */
985 port->ops->enable_ms(port);
986 spin_unlock_irq(&port->lock);
987
988 add_wait_queue(&state->info->delta_msr_wait, &wait);
989 for (;;) {
990 spin_lock_irq(&port->lock);
991 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
992 spin_unlock_irq(&port->lock);
993
994 set_current_state(TASK_INTERRUPTIBLE);
995
996 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
997 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
998 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
999 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1000 ret = 0;
1001 break;
1002 }
1003
1004 schedule();
1005
1006 /* see if a signal did it */
1007 if (signal_pending(current)) {
1008 ret = -ERESTARTSYS;
1009 break;
1010 }
1011
1012 cprev = cnow;
1013 }
1014
1015 current->state = TASK_RUNNING;
1016 remove_wait_queue(&state->info->delta_msr_wait, &wait);
1017
1018 return ret;
1019}
1020
1021/*
1022 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1023 * Return: write counters to the user passed counter struct
1024 * NB: both 1->0 and 0->1 transitions are counted except for
1025 * RI where only 0->1 is counted.
1026 */
1027static int uart_get_count(struct uart_state *state,
1028 struct serial_icounter_struct __user *icnt)
1029{
1030 struct serial_icounter_struct icount;
1031 struct uart_icount cnow;
1032 struct uart_port *port = state->port;
1033
1034 spin_lock_irq(&port->lock);
1035 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1036 spin_unlock_irq(&port->lock);
1037
1038 icount.cts = cnow.cts;
1039 icount.dsr = cnow.dsr;
1040 icount.rng = cnow.rng;
1041 icount.dcd = cnow.dcd;
1042 icount.rx = cnow.rx;
1043 icount.tx = cnow.tx;
1044 icount.frame = cnow.frame;
1045 icount.overrun = cnow.overrun;
1046 icount.parity = cnow.parity;
1047 icount.brk = cnow.brk;
1048 icount.buf_overrun = cnow.buf_overrun;
1049
1050 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1051}
1052
1053/*
1054 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
1055 */
1056static int
1057uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1058 unsigned long arg)
1059{
1060 struct uart_state *state = tty->driver_data;
1061 void __user *uarg = (void __user *)arg;
1062 int ret = -ENOIOCTLCMD;
1063
1064 BUG_ON(!kernel_locked());
1065
1066 /*
1067 * These ioctls don't rely on the hardware to be present.
1068 */
1069 switch (cmd) {
1070 case TIOCGSERIAL:
1071 ret = uart_get_info(state, uarg);
1072 break;
1073
1074 case TIOCSSERIAL:
1075 ret = uart_set_info(state, uarg);
1076 break;
1077
1078 case TIOCSERCONFIG:
1079 ret = uart_do_autoconfig(state);
1080 break;
1081
1082 case TIOCSERGWILD: /* obsolete */
1083 case TIOCSERSWILD: /* obsolete */
1084 ret = 0;
1085 break;
1086 }
1087
1088 if (ret != -ENOIOCTLCMD)
1089 goto out;
1090
1091 if (tty->flags & (1 << TTY_IO_ERROR)) {
1092 ret = -EIO;
1093 goto out;
1094 }
1095
1096 /*
1097 * The following should only be used when hardware is present.
1098 */
1099 switch (cmd) {
1100 case TIOCMIWAIT:
1101 ret = uart_wait_modem_status(state, arg);
1102 break;
1103
1104 case TIOCGICOUNT:
1105 ret = uart_get_count(state, uarg);
1106 break;
1107 }
1108
1109 if (ret != -ENOIOCTLCMD)
1110 goto out;
1111
Ingo Molnare2862f62006-01-13 21:37:07 +00001112 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 if (tty_hung_up_p(filp)) {
1115 ret = -EIO;
1116 goto out_up;
1117 }
1118
1119 /*
1120 * All these rely on hardware being present and need to be
1121 * protected against the tty being hung up.
1122 */
1123 switch (cmd) {
1124 case TIOCSERGETLSR: /* Get line status register */
1125 ret = uart_get_lsr_info(state, uarg);
1126 break;
1127
1128 default: {
1129 struct uart_port *port = state->port;
1130 if (port->ops->ioctl)
1131 ret = port->ops->ioctl(port, cmd, arg);
1132 break;
1133 }
1134 }
1135 out_up:
Ingo Molnare2862f62006-01-13 21:37:07 +00001136 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 out:
1138 return ret;
1139}
1140
1141static void uart_set_termios(struct tty_struct *tty, struct termios *old_termios)
1142{
1143 struct uart_state *state = tty->driver_data;
1144 unsigned long flags;
1145 unsigned int cflag = tty->termios->c_cflag;
1146
1147 BUG_ON(!kernel_locked());
1148
1149 /*
1150 * These are the bits that are used to setup various
1151 * flags in the low level driver.
1152 */
1153#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1154
1155 if ((cflag ^ old_termios->c_cflag) == 0 &&
1156 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1157 return;
1158
1159 uart_change_speed(state, old_termios);
1160
1161 /* Handle transition to B0 status */
1162 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1163 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1164
1165 /* Handle transition away from B0 status */
1166 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1167 unsigned int mask = TIOCM_DTR;
1168 if (!(cflag & CRTSCTS) ||
1169 !test_bit(TTY_THROTTLED, &tty->flags))
1170 mask |= TIOCM_RTS;
1171 uart_set_mctrl(state->port, mask);
1172 }
1173
1174 /* Handle turning off CRTSCTS */
1175 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1176 spin_lock_irqsave(&state->port->lock, flags);
1177 tty->hw_stopped = 0;
1178 __uart_start(tty);
1179 spin_unlock_irqrestore(&state->port->lock, flags);
1180 }
1181
Russell King0dd7a1a2005-06-29 18:40:53 +01001182 /* Handle turning on CRTSCTS */
1183 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1184 spin_lock_irqsave(&state->port->lock, flags);
1185 if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
1186 tty->hw_stopped = 1;
Russell Kingb129a8c2005-08-31 10:12:14 +01001187 state->port->ops->stop_tx(state->port);
Russell King0dd7a1a2005-06-29 18:40:53 +01001188 }
1189 spin_unlock_irqrestore(&state->port->lock, flags);
1190 }
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192#if 0
1193 /*
1194 * No need to wake up processes in open wait, since they
1195 * sample the CLOCAL flag once, and don't recheck it.
1196 * XXX It's not clear whether the current behavior is correct
1197 * or not. Hence, this may change.....
1198 */
1199 if (!(old_termios->c_cflag & CLOCAL) &&
1200 (tty->termios->c_cflag & CLOCAL))
1201 wake_up_interruptible(&state->info->open_wait);
1202#endif
1203}
1204
1205/*
1206 * In 2.4.5, calls to this will be serialized via the BKL in
1207 * linux/drivers/char/tty_io.c:tty_release()
1208 * linux/drivers/char/tty_io.c:do_tty_handup()
1209 */
1210static void uart_close(struct tty_struct *tty, struct file *filp)
1211{
1212 struct uart_state *state = tty->driver_data;
1213 struct uart_port *port;
1214
1215 BUG_ON(!kernel_locked());
1216
1217 if (!state || !state->port)
1218 return;
1219
1220 port = state->port;
1221
1222 DPRINTK("uart_close(%d) called\n", port->line);
1223
Ingo Molnare2862f62006-01-13 21:37:07 +00001224 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 if (tty_hung_up_p(filp))
1227 goto done;
1228
1229 if ((tty->count == 1) && (state->count != 1)) {
1230 /*
1231 * Uh, oh. tty->count is 1, which means that the tty
1232 * structure will be freed. state->count should always
1233 * be one in these conditions. If it's greater than
1234 * one, we've got real problems, since it means the
1235 * serial port won't be shutdown.
1236 */
1237 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1238 "state->count is %d\n", state->count);
1239 state->count = 1;
1240 }
1241 if (--state->count < 0) {
1242 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1243 tty->name, state->count);
1244 state->count = 0;
1245 }
1246 if (state->count)
1247 goto done;
1248
1249 /*
1250 * Now we wait for the transmit buffer to clear; and we notify
1251 * the line discipline to only process XON/XOFF characters by
1252 * setting tty->closing.
1253 */
1254 tty->closing = 1;
1255
1256 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1257 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1258
1259 /*
1260 * At this point, we stop accepting input. To do this, we
1261 * disable the receive line status interrupts.
1262 */
1263 if (state->info->flags & UIF_INITIALIZED) {
1264 unsigned long flags;
1265 spin_lock_irqsave(&port->lock, flags);
1266 port->ops->stop_rx(port);
1267 spin_unlock_irqrestore(&port->lock, flags);
1268 /*
1269 * Before we drop DTR, make sure the UART transmitter
1270 * has completely drained; this is especially
1271 * important if there is a transmit FIFO!
1272 */
1273 uart_wait_until_sent(tty, port->timeout);
1274 }
1275
1276 uart_shutdown(state);
1277 uart_flush_buffer(tty);
1278
1279 tty_ldisc_flush(tty);
1280
1281 tty->closing = 0;
1282 state->info->tty = NULL;
1283
1284 if (state->info->blocked_open) {
1285 if (state->close_delay)
1286 msleep_interruptible(state->close_delay);
1287 } else if (!uart_console(port)) {
1288 uart_change_pm(state, 3);
1289 }
1290
1291 /*
1292 * Wake up anyone trying to open this port.
1293 */
1294 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1295 wake_up_interruptible(&state->info->open_wait);
1296
1297 done:
Ingo Molnare2862f62006-01-13 21:37:07 +00001298 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
1301static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1302{
1303 struct uart_state *state = tty->driver_data;
1304 struct uart_port *port = state->port;
1305 unsigned long char_time, expire;
1306
1307 BUG_ON(!kernel_locked());
1308
1309 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1310 return;
1311
1312 /*
1313 * Set the check interval to be 1/5 of the estimated time to
1314 * send a single character, and make it at least 1. The check
1315 * interval should also be less than the timeout.
1316 *
1317 * Note: we have to use pretty tight timings here to satisfy
1318 * the NIST-PCTS.
1319 */
1320 char_time = (port->timeout - HZ/50) / port->fifosize;
1321 char_time = char_time / 5;
1322 if (char_time == 0)
1323 char_time = 1;
1324 if (timeout && timeout < char_time)
1325 char_time = timeout;
1326
1327 /*
1328 * If the transmitter hasn't cleared in twice the approximate
1329 * amount of time to send the entire FIFO, it probably won't
1330 * ever clear. This assumes the UART isn't doing flow
1331 * control, which is currently the case. Hence, if it ever
1332 * takes longer than port->timeout, this is probably due to a
1333 * UART bug of some kind. So, we clamp the timeout parameter at
1334 * 2*port->timeout.
1335 */
1336 if (timeout == 0 || timeout > 2 * port->timeout)
1337 timeout = 2 * port->timeout;
1338
1339 expire = jiffies + timeout;
1340
1341 DPRINTK("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1342 port->line, jiffies, expire);
1343
1344 /*
1345 * Check whether the transmitter is empty every 'char_time'.
1346 * 'timeout' / 'expire' give us the maximum amount of time
1347 * we wait.
1348 */
1349 while (!port->ops->tx_empty(port)) {
1350 msleep_interruptible(jiffies_to_msecs(char_time));
1351 if (signal_pending(current))
1352 break;
1353 if (time_after(jiffies, expire))
1354 break;
1355 }
1356 set_current_state(TASK_RUNNING); /* might not be needed */
1357}
1358
1359/*
1360 * This is called with the BKL held in
1361 * linux/drivers/char/tty_io.c:do_tty_hangup()
1362 * We're called from the eventd thread, so we can sleep for
1363 * a _short_ time only.
1364 */
1365static void uart_hangup(struct tty_struct *tty)
1366{
1367 struct uart_state *state = tty->driver_data;
1368
1369 BUG_ON(!kernel_locked());
1370 DPRINTK("uart_hangup(%d)\n", state->port->line);
1371
Ingo Molnare2862f62006-01-13 21:37:07 +00001372 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1374 uart_flush_buffer(tty);
1375 uart_shutdown(state);
1376 state->count = 0;
1377 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1378 state->info->tty = NULL;
1379 wake_up_interruptible(&state->info->open_wait);
1380 wake_up_interruptible(&state->info->delta_msr_wait);
1381 }
Ingo Molnare2862f62006-01-13 21:37:07 +00001382 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
1385/*
1386 * Copy across the serial console cflag setting into the termios settings
1387 * for the initial open of the port. This allows continuity between the
1388 * kernel settings, and the settings init adopts when it opens the port
1389 * for the first time.
1390 */
1391static void uart_update_termios(struct uart_state *state)
1392{
1393 struct tty_struct *tty = state->info->tty;
1394 struct uart_port *port = state->port;
1395
1396 if (uart_console(port) && port->cons->cflag) {
1397 tty->termios->c_cflag = port->cons->cflag;
1398 port->cons->cflag = 0;
1399 }
1400
1401 /*
1402 * If the device failed to grab its irq resources,
1403 * or some other error occurred, don't try to talk
1404 * to the port hardware.
1405 */
1406 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1407 /*
1408 * Make termios settings take effect.
1409 */
1410 uart_change_speed(state, NULL);
1411
1412 /*
1413 * And finally enable the RTS and DTR signals.
1414 */
1415 if (tty->termios->c_cflag & CBAUD)
1416 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1417 }
1418}
1419
1420/*
1421 * Block the open until the port is ready. We must be called with
1422 * the per-port semaphore held.
1423 */
1424static int
1425uart_block_til_ready(struct file *filp, struct uart_state *state)
1426{
1427 DECLARE_WAITQUEUE(wait, current);
1428 struct uart_info *info = state->info;
1429 struct uart_port *port = state->port;
Russell Kingc5f46442005-06-29 09:42:38 +01001430 unsigned int mctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 info->blocked_open++;
1433 state->count--;
1434
1435 add_wait_queue(&info->open_wait, &wait);
1436 while (1) {
1437 set_current_state(TASK_INTERRUPTIBLE);
1438
1439 /*
1440 * If we have been hung up, tell userspace/restart open.
1441 */
1442 if (tty_hung_up_p(filp) || info->tty == NULL)
1443 break;
1444
1445 /*
1446 * If the port has been closed, tell userspace/restart open.
1447 */
1448 if (!(info->flags & UIF_INITIALIZED))
1449 break;
1450
1451 /*
1452 * If non-blocking mode is set, or CLOCAL mode is set,
1453 * we don't want to wait for the modem status lines to
1454 * indicate that the port is ready.
1455 *
1456 * Also, if the port is not enabled/configured, we want
1457 * to allow the open to succeed here. Note that we will
1458 * have set TTY_IO_ERROR for a non-existant port.
1459 */
1460 if ((filp->f_flags & O_NONBLOCK) ||
1461 (info->tty->termios->c_cflag & CLOCAL) ||
1462 (info->tty->flags & (1 << TTY_IO_ERROR))) {
1463 break;
1464 }
1465
1466 /*
1467 * Set DTR to allow modem to know we're waiting. Do
1468 * not set RTS here - we want to make sure we catch
1469 * the data from the modem.
1470 */
1471 if (info->tty->termios->c_cflag & CBAUD)
1472 uart_set_mctrl(port, TIOCM_DTR);
1473
1474 /*
1475 * and wait for the carrier to indicate that the
1476 * modem is ready for us.
1477 */
Russell Kingc5f46442005-06-29 09:42:38 +01001478 spin_lock_irq(&port->lock);
Russell Kingf61051c2006-01-07 23:11:23 +00001479 port->ops->enable_ms(port);
Russell Kingc5f46442005-06-29 09:42:38 +01001480 mctrl = port->ops->get_mctrl(port);
1481 spin_unlock_irq(&port->lock);
1482 if (mctrl & TIOCM_CAR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 break;
1484
Ingo Molnare2862f62006-01-13 21:37:07 +00001485 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 schedule();
Ingo Molnare2862f62006-01-13 21:37:07 +00001487 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 if (signal_pending(current))
1490 break;
1491 }
1492 set_current_state(TASK_RUNNING);
1493 remove_wait_queue(&info->open_wait, &wait);
1494
1495 state->count++;
1496 info->blocked_open--;
1497
1498 if (signal_pending(current))
1499 return -ERESTARTSYS;
1500
1501 if (!info->tty || tty_hung_up_p(filp))
1502 return -EAGAIN;
1503
1504 return 0;
1505}
1506
1507static struct uart_state *uart_get(struct uart_driver *drv, int line)
1508{
1509 struct uart_state *state;
Russell King68ac64c2006-04-30 11:13:50 +01001510 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 state = drv->state + line;
Ingo Molnare2862f62006-01-13 21:37:07 +00001513 if (mutex_lock_interruptible(&state->mutex)) {
Russell King68ac64c2006-04-30 11:13:50 +01001514 ret = -ERESTARTSYS;
1515 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
1517
1518 state->count++;
Russell King68ac64c2006-04-30 11:13:50 +01001519 if (!state->port || state->port->flags & UPF_DEAD) {
1520 ret = -ENXIO;
1521 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523
1524 if (!state->info) {
1525 state->info = kmalloc(sizeof(struct uart_info), GFP_KERNEL);
1526 if (state->info) {
1527 memset(state->info, 0, sizeof(struct uart_info));
1528 init_waitqueue_head(&state->info->open_wait);
1529 init_waitqueue_head(&state->info->delta_msr_wait);
1530
1531 /*
1532 * Link the info into the other structures.
1533 */
1534 state->port->info = state->info;
1535
1536 tasklet_init(&state->info->tlet, uart_tasklet_action,
1537 (unsigned long)state);
1538 } else {
Russell King68ac64c2006-04-30 11:13:50 +01001539 ret = -ENOMEM;
1540 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return state;
Russell King68ac64c2006-04-30 11:13:50 +01001544
1545 err_unlock:
1546 state->count--;
1547 mutex_unlock(&state->mutex);
1548 err:
1549 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
1552/*
1553 * In 2.4.5, calls to uart_open are serialised by the BKL in
1554 * linux/fs/devices.c:chrdev_open()
1555 * Note that if this fails, then uart_close() _will_ be called.
1556 *
1557 * In time, we want to scrap the "opening nonpresent ports"
1558 * behaviour and implement an alternative way for setserial
1559 * to set base addresses/ports/types. This will allow us to
1560 * get rid of a certain amount of extra tests.
1561 */
1562static int uart_open(struct tty_struct *tty, struct file *filp)
1563{
1564 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1565 struct uart_state *state;
1566 int retval, line = tty->index;
1567
1568 BUG_ON(!kernel_locked());
1569 DPRINTK("uart_open(%d) called\n", line);
1570
1571 /*
1572 * tty->driver->num won't change, so we won't fail here with
1573 * tty->driver_data set to something non-NULL (and therefore
1574 * we won't get caught by uart_close()).
1575 */
1576 retval = -ENODEV;
1577 if (line >= tty->driver->num)
1578 goto fail;
1579
1580 /*
1581 * We take the semaphore inside uart_get to guarantee that we won't
1582 * be re-entered while allocating the info structure, or while we
1583 * request any IRQs that the driver may need. This also has the nice
1584 * side-effect that it delays the action of uart_hangup, so we can
1585 * guarantee that info->tty will always contain something reasonable.
1586 */
1587 state = uart_get(drv, line);
1588 if (IS_ERR(state)) {
1589 retval = PTR_ERR(state);
1590 goto fail;
1591 }
1592
1593 /*
1594 * Once we set tty->driver_data here, we are guaranteed that
1595 * uart_close() will decrement the driver module use count.
1596 * Any failures from here onwards should not touch the count.
1597 */
1598 tty->driver_data = state;
1599 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1600 tty->alt_speed = 0;
1601 state->info->tty = tty;
1602
1603 /*
1604 * If the port is in the middle of closing, bail out now.
1605 */
1606 if (tty_hung_up_p(filp)) {
1607 retval = -EAGAIN;
1608 state->count--;
Ingo Molnare2862f62006-01-13 21:37:07 +00001609 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 goto fail;
1611 }
1612
1613 /*
1614 * Make sure the device is in D0 state.
1615 */
1616 if (state->count == 1)
1617 uart_change_pm(state, 0);
1618
1619 /*
1620 * Start up the serial port.
1621 */
1622 retval = uart_startup(state, 0);
1623
1624 /*
1625 * If we succeeded, wait until the port is ready.
1626 */
1627 if (retval == 0)
1628 retval = uart_block_til_ready(filp, state);
Ingo Molnare2862f62006-01-13 21:37:07 +00001629 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 /*
1632 * If this is the first open to succeed, adjust things to suit.
1633 */
1634 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1635 state->info->flags |= UIF_NORMAL_ACTIVE;
1636
1637 uart_update_termios(state);
1638 }
1639
1640 fail:
1641 return retval;
1642}
1643
1644static const char *uart_type(struct uart_port *port)
1645{
1646 const char *str = NULL;
1647
1648 if (port->ops->type)
1649 str = port->ops->type(port);
1650
1651 if (!str)
1652 str = "unknown";
1653
1654 return str;
1655}
1656
1657#ifdef CONFIG_PROC_FS
1658
1659static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1660{
1661 struct uart_state *state = drv->state + i;
1662 struct uart_port *port = state->port;
1663 char stat_buf[32];
1664 unsigned int status;
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001665 int mmio, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 if (!port)
1668 return 0;
1669
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001670 mmio = port->iotype >= UPIO_MEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 ret = sprintf(buf, "%d: uart:%s %s%08lX irq:%d",
1672 port->line, uart_type(port),
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001673 mmio ? "mmio:0x" : "port:",
1674 mmio ? port->mapbase : (unsigned long) port->iobase,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 port->irq);
1676
1677 if (port->type == PORT_UNKNOWN) {
1678 strcat(buf, "\n");
1679 return ret + 1;
1680 }
1681
1682 if(capable(CAP_SYS_ADMIN))
1683 {
Russell Kingc5f46442005-06-29 09:42:38 +01001684 spin_lock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 status = port->ops->get_mctrl(port);
Russell Kingc5f46442005-06-29 09:42:38 +01001686 spin_unlock_irq(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 ret += sprintf(buf + ret, " tx:%d rx:%d",
1689 port->icount.tx, port->icount.rx);
1690 if (port->icount.frame)
1691 ret += sprintf(buf + ret, " fe:%d",
1692 port->icount.frame);
1693 if (port->icount.parity)
1694 ret += sprintf(buf + ret, " pe:%d",
1695 port->icount.parity);
1696 if (port->icount.brk)
1697 ret += sprintf(buf + ret, " brk:%d",
1698 port->icount.brk);
1699 if (port->icount.overrun)
1700 ret += sprintf(buf + ret, " oe:%d",
1701 port->icount.overrun);
1702
1703#define INFOBIT(bit,str) \
1704 if (port->mctrl & (bit)) \
1705 strncat(stat_buf, (str), sizeof(stat_buf) - \
1706 strlen(stat_buf) - 2)
1707#define STATBIT(bit,str) \
1708 if (status & (bit)) \
1709 strncat(stat_buf, (str), sizeof(stat_buf) - \
1710 strlen(stat_buf) - 2)
1711
1712 stat_buf[0] = '\0';
1713 stat_buf[1] = '\0';
1714 INFOBIT(TIOCM_RTS, "|RTS");
1715 STATBIT(TIOCM_CTS, "|CTS");
1716 INFOBIT(TIOCM_DTR, "|DTR");
1717 STATBIT(TIOCM_DSR, "|DSR");
1718 STATBIT(TIOCM_CAR, "|CD");
1719 STATBIT(TIOCM_RNG, "|RI");
1720 if (stat_buf[0])
1721 stat_buf[0] = ' ';
1722 strcat(stat_buf, "\n");
1723
1724 ret += sprintf(buf + ret, stat_buf);
1725 } else {
1726 strcat(buf, "\n");
1727 ret++;
1728 }
1729#undef STATBIT
1730#undef INFOBIT
1731 return ret;
1732}
1733
1734static int uart_read_proc(char *page, char **start, off_t off,
1735 int count, int *eof, void *data)
1736{
1737 struct tty_driver *ttydrv = data;
1738 struct uart_driver *drv = ttydrv->driver_state;
1739 int i, len = 0, l;
1740 off_t begin = 0;
1741
1742 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1743 "", "", "");
1744 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1745 l = uart_line_info(page + len, drv, i);
1746 len += l;
1747 if (len + begin > off + count)
1748 goto done;
1749 if (len + begin < off) {
1750 begin += len;
1751 len = 0;
1752 }
1753 }
1754 *eof = 1;
1755 done:
1756 if (off >= len + begin)
1757 return 0;
1758 *start = page + (off - begin);
1759 return (count < begin + len - off) ? count : (begin + len - off);
1760}
1761#endif
1762
1763#ifdef CONFIG_SERIAL_CORE_CONSOLE
1764/*
Russell Kingd3587882006-03-20 20:00:09 +00001765 * uart_console_write - write a console message to a serial port
1766 * @port: the port to write the message
1767 * @s: array of characters
1768 * @count: number of characters in string to write
1769 * @write: function to write character to port
1770 */
1771void uart_console_write(struct uart_port *port, const char *s,
1772 unsigned int count,
1773 void (*putchar)(struct uart_port *, int))
1774{
1775 unsigned int i;
1776
1777 for (i = 0; i < count; i++, s++) {
1778 if (*s == '\n')
1779 putchar(port, '\r');
1780 putchar(port, *s);
1781 }
1782}
1783EXPORT_SYMBOL_GPL(uart_console_write);
1784
1785/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 * Check whether an invalid uart number has been specified, and
1787 * if so, search for the first available port that does have
1788 * console support.
1789 */
1790struct uart_port * __init
1791uart_get_console(struct uart_port *ports, int nr, struct console *co)
1792{
1793 int idx = co->index;
1794
1795 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1796 ports[idx].membase == NULL))
1797 for (idx = 0; idx < nr; idx++)
1798 if (ports[idx].iobase != 0 ||
1799 ports[idx].membase != NULL)
1800 break;
1801
1802 co->index = idx;
1803
1804 return ports + idx;
1805}
1806
1807/**
1808 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1809 * @options: pointer to option string
1810 * @baud: pointer to an 'int' variable for the baud rate.
1811 * @parity: pointer to an 'int' variable for the parity.
1812 * @bits: pointer to an 'int' variable for the number of data bits.
1813 * @flow: pointer to an 'int' variable for the flow control character.
1814 *
1815 * uart_parse_options decodes a string containing the serial console
1816 * options. The format of the string is <baud><parity><bits><flow>,
1817 * eg: 115200n8r
1818 */
1819void __init
1820uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1821{
1822 char *s = options;
1823
1824 *baud = simple_strtoul(s, NULL, 10);
1825 while (*s >= '0' && *s <= '9')
1826 s++;
1827 if (*s)
1828 *parity = *s++;
1829 if (*s)
1830 *bits = *s++ - '0';
1831 if (*s)
1832 *flow = *s;
1833}
1834
1835struct baud_rates {
1836 unsigned int rate;
1837 unsigned int cflag;
1838};
1839
Arjan van de Vencb3592b2005-11-28 21:04:11 +00001840static const struct baud_rates baud_rates[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 { 921600, B921600 },
1842 { 460800, B460800 },
1843 { 230400, B230400 },
1844 { 115200, B115200 },
1845 { 57600, B57600 },
1846 { 38400, B38400 },
1847 { 19200, B19200 },
1848 { 9600, B9600 },
1849 { 4800, B4800 },
1850 { 2400, B2400 },
1851 { 1200, B1200 },
1852 { 0, B38400 }
1853};
1854
1855/**
1856 * uart_set_options - setup the serial console parameters
1857 * @port: pointer to the serial ports uart_port structure
1858 * @co: console pointer
1859 * @baud: baud rate
1860 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1861 * @bits: number of data bits
1862 * @flow: flow control character - 'r' (rts)
1863 */
1864int __init
1865uart_set_options(struct uart_port *port, struct console *co,
1866 int baud, int parity, int bits, int flow)
1867{
1868 struct termios termios;
1869 int i;
1870
Russell King976ecd12005-07-03 21:05:45 +01001871 /*
1872 * Ensure that the serial console lock is initialised
1873 * early.
1874 */
1875 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07001876 lockdep_set_class(&port->lock, &port_lock_key);
Russell King976ecd12005-07-03 21:05:45 +01001877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 memset(&termios, 0, sizeof(struct termios));
1879
1880 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1881
1882 /*
1883 * Construct a cflag setting.
1884 */
1885 for (i = 0; baud_rates[i].rate; i++)
1886 if (baud_rates[i].rate <= baud)
1887 break;
1888
1889 termios.c_cflag |= baud_rates[i].cflag;
1890
1891 if (bits == 7)
1892 termios.c_cflag |= CS7;
1893 else
1894 termios.c_cflag |= CS8;
1895
1896 switch (parity) {
1897 case 'o': case 'O':
1898 termios.c_cflag |= PARODD;
1899 /*fall through*/
1900 case 'e': case 'E':
1901 termios.c_cflag |= PARENB;
1902 break;
1903 }
1904
1905 if (flow == 'r')
1906 termios.c_cflag |= CRTSCTS;
1907
1908 port->ops->set_termios(port, &termios, NULL);
1909 co->cflag = termios.c_cflag;
1910
1911 return 0;
1912}
1913#endif /* CONFIG_SERIAL_CORE_CONSOLE */
1914
1915static void uart_change_pm(struct uart_state *state, int pm_state)
1916{
1917 struct uart_port *port = state->port;
Andrew Victor1281e362006-05-16 11:28:49 +01001918
1919 if (state->pm_state != pm_state) {
1920 if (port->ops->pm)
1921 port->ops->pm(port, pm_state, state->pm_state);
1922 state->pm_state = pm_state;
1923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
1926int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1927{
1928 struct uart_state *state = drv->state + port->line;
1929
Ingo Molnare2862f62006-01-13 21:37:07 +00001930 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07001932#ifdef CONFIG_DISABLE_CONSOLE_SUSPEND
1933 if (uart_console(port)) {
1934 mutex_unlock(&state->mutex);
1935 return 0;
1936 }
1937#endif
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (state->info && state->info->flags & UIF_INITIALIZED) {
Russell Kingba899db2006-01-21 22:45:50 +00001940 const struct uart_ops *ops = port->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 spin_lock_irq(&port->lock);
Russell Kingb129a8c2005-08-31 10:12:14 +01001943 ops->stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 ops->set_mctrl(port, 0);
1945 ops->stop_rx(port);
1946 spin_unlock_irq(&port->lock);
1947
1948 /*
1949 * Wait for the transmitter to empty.
1950 */
1951 while (!ops->tx_empty(port)) {
1952 msleep(10);
1953 }
1954
1955 ops->shutdown(port);
1956 }
1957
1958 /*
1959 * Disable the console device before suspending.
1960 */
1961 if (uart_console(port))
1962 console_stop(port->cons);
1963
1964 uart_change_pm(state, 3);
1965
Ingo Molnare2862f62006-01-13 21:37:07 +00001966 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 return 0;
1969}
1970
1971int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
1972{
1973 struct uart_state *state = drv->state + port->line;
1974
Ingo Molnare2862f62006-01-13 21:37:07 +00001975 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Rafael J. Wysockie1da95a2006-09-25 23:32:57 -07001977#ifdef CONFIG_DISABLE_CONSOLE_SUSPEND
1978 if (uart_console(port)) {
1979 mutex_unlock(&state->mutex);
1980 return 0;
1981 }
1982#endif
1983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 uart_change_pm(state, 0);
1985
1986 /*
1987 * Re-enable the console device after suspending.
1988 */
1989 if (uart_console(port)) {
1990 struct termios termios;
1991
1992 /*
1993 * First try to use the console cflag setting.
1994 */
1995 memset(&termios, 0, sizeof(struct termios));
1996 termios.c_cflag = port->cons->cflag;
1997
1998 /*
1999 * If that's unset, use the tty termios setting.
2000 */
2001 if (state->info && state->info->tty && termios.c_cflag == 0)
2002 termios = *state->info->tty->termios;
2003
2004 port->ops->set_termios(port, &termios, NULL);
2005 console_start(port->cons);
2006 }
2007
2008 if (state->info && state->info->flags & UIF_INITIALIZED) {
Russell Kingba899db2006-01-21 22:45:50 +00002009 const struct uart_ops *ops = port->ops;
Russell Kingee31b332005-11-13 15:28:51 +00002010 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 ops->set_mctrl(port, 0);
Russell Kingee31b332005-11-13 15:28:51 +00002013 ret = ops->startup(port);
2014 if (ret == 0) {
2015 uart_change_speed(state, NULL);
2016 spin_lock_irq(&port->lock);
2017 ops->set_mctrl(port, port->mctrl);
2018 ops->start_tx(port);
2019 spin_unlock_irq(&port->lock);
2020 } else {
2021 /*
2022 * Failed to resume - maybe hardware went away?
2023 * Clear the "initialized" flag so we won't try
2024 * to call the low level drivers shutdown method.
2025 */
2026 state->info->flags &= ~UIF_INITIALIZED;
2027 uart_shutdown(state);
2028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
2030
Ingo Molnare2862f62006-01-13 21:37:07 +00002031 mutex_unlock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 return 0;
2034}
2035
2036static inline void
2037uart_report_port(struct uart_driver *drv, struct uart_port *port)
2038{
Russell King30b7a3b2005-09-03 15:30:21 +01002039 char address[64];
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 switch (port->iotype) {
2042 case UPIO_PORT:
Russell King30b7a3b2005-09-03 15:30:21 +01002043 snprintf(address, sizeof(address),
2044 "I/O 0x%x", port->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 break;
2046 case UPIO_HUB6:
Russell King30b7a3b2005-09-03 15:30:21 +01002047 snprintf(address, sizeof(address),
2048 "I/O 0x%x offset 0x%x", port->iobase, port->hub6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 break;
2050 case UPIO_MEM:
2051 case UPIO_MEM32:
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002052 case UPIO_AU:
Zang Roy-r619113be91ec2006-06-30 02:29:58 -07002053 case UPIO_TSI:
Russell King30b7a3b2005-09-03 15:30:21 +01002054 snprintf(address, sizeof(address),
2055 "MMIO 0x%lx", port->mapbase);
2056 break;
2057 default:
2058 strlcpy(address, "*unknown*", sizeof(address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 break;
2060 }
Russell King30b7a3b2005-09-03 15:30:21 +01002061
Russell King0cf669d2005-10-31 11:42:22 +00002062 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2063 port->dev ? port->dev->bus_id : "",
2064 port->dev ? ": " : "",
Russell King30b7a3b2005-09-03 15:30:21 +01002065 drv->dev_name, port->line, address, port->irq, uart_type(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066}
2067
2068static void
2069uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2070 struct uart_port *port)
2071{
2072 unsigned int flags;
2073
2074 /*
2075 * If there isn't a port here, don't do anything further.
2076 */
2077 if (!port->iobase && !port->mapbase && !port->membase)
2078 return;
2079
2080 /*
2081 * Now do the auto configuration stuff. Note that config_port
2082 * is expected to claim the resources and map the port for us.
2083 */
2084 flags = UART_CONFIG_TYPE;
2085 if (port->flags & UPF_AUTO_IRQ)
2086 flags |= UART_CONFIG_IRQ;
2087 if (port->flags & UPF_BOOT_AUTOCONF) {
2088 port->type = PORT_UNKNOWN;
2089 port->ops->config_port(port, flags);
2090 }
2091
2092 if (port->type != PORT_UNKNOWN) {
2093 unsigned long flags;
2094
2095 uart_report_port(drv, port);
2096
2097 /*
2098 * Ensure that the modem control lines are de-activated.
2099 * We probably don't need a spinlock around this, but
2100 */
2101 spin_lock_irqsave(&port->lock, flags);
2102 port->ops->set_mctrl(port, 0);
2103 spin_unlock_irqrestore(&port->lock, flags);
2104
2105 /*
2106 * Power down all ports by default, except the
2107 * console if we have one.
2108 */
2109 if (!uart_console(port))
2110 uart_change_pm(state, 3);
2111 }
2112}
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114static struct tty_operations uart_ops = {
2115 .open = uart_open,
2116 .close = uart_close,
2117 .write = uart_write,
2118 .put_char = uart_put_char,
2119 .flush_chars = uart_flush_chars,
2120 .write_room = uart_write_room,
2121 .chars_in_buffer= uart_chars_in_buffer,
2122 .flush_buffer = uart_flush_buffer,
2123 .ioctl = uart_ioctl,
2124 .throttle = uart_throttle,
2125 .unthrottle = uart_unthrottle,
2126 .send_xchar = uart_send_xchar,
2127 .set_termios = uart_set_termios,
2128 .stop = uart_stop,
2129 .start = uart_start,
2130 .hangup = uart_hangup,
2131 .break_ctl = uart_break_ctl,
2132 .wait_until_sent= uart_wait_until_sent,
2133#ifdef CONFIG_PROC_FS
2134 .read_proc = uart_read_proc,
2135#endif
2136 .tiocmget = uart_tiocmget,
2137 .tiocmset = uart_tiocmset,
2138};
2139
2140/**
2141 * uart_register_driver - register a driver with the uart core layer
2142 * @drv: low level driver structure
2143 *
2144 * Register a uart driver with the core driver. We in turn register
2145 * with the tty layer, and initialise the core driver per-port state.
2146 *
2147 * We have a proc file in /proc/tty/driver which is named after the
2148 * normal driver.
2149 *
2150 * drv->port should be NULL, and the per-port structures should be
2151 * registered using uart_add_one_port after this call has succeeded.
2152 */
2153int uart_register_driver(struct uart_driver *drv)
2154{
2155 struct tty_driver *normal = NULL;
2156 int i, retval;
2157
2158 BUG_ON(drv->state);
2159
2160 /*
2161 * Maybe we should be using a slab cache for this, especially if
2162 * we have a large number of ports to handle.
2163 */
2164 drv->state = kmalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2165 retval = -ENOMEM;
2166 if (!drv->state)
2167 goto out;
2168
2169 memset(drv->state, 0, sizeof(struct uart_state) * drv->nr);
2170
2171 normal = alloc_tty_driver(drv->nr);
2172 if (!normal)
2173 goto out;
2174
2175 drv->tty_driver = normal;
2176
2177 normal->owner = drv->owner;
2178 normal->driver_name = drv->driver_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 normal->name = drv->dev_name;
2180 normal->major = drv->major;
2181 normal->minor_start = drv->minor;
2182 normal->type = TTY_DRIVER_TYPE_SERIAL;
2183 normal->subtype = SERIAL_TYPE_NORMAL;
2184 normal->init_termios = tty_std_termios;
2185 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07002186 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 normal->driver_state = drv;
2188 tty_set_operations(normal, &uart_ops);
2189
2190 /*
2191 * Initialise the UART state(s).
2192 */
2193 for (i = 0; i < drv->nr; i++) {
2194 struct uart_state *state = drv->state + i;
2195
2196 state->close_delay = 500; /* .5 seconds */
2197 state->closing_wait = 30000; /* 30 seconds */
2198
Ingo Molnare2862f62006-01-13 21:37:07 +00002199 mutex_init(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
2201
2202 retval = tty_register_driver(normal);
2203 out:
2204 if (retval < 0) {
2205 put_tty_driver(normal);
2206 kfree(drv->state);
2207 }
2208 return retval;
2209}
2210
2211/**
2212 * uart_unregister_driver - remove a driver from the uart core layer
2213 * @drv: low level driver structure
2214 *
2215 * Remove all references to a driver from the core driver. The low
2216 * level driver must have removed all its ports via the
2217 * uart_remove_one_port() if it registered them with uart_add_one_port().
2218 * (ie, drv->port == NULL)
2219 */
2220void uart_unregister_driver(struct uart_driver *drv)
2221{
2222 struct tty_driver *p = drv->tty_driver;
2223 tty_unregister_driver(p);
2224 put_tty_driver(p);
2225 kfree(drv->state);
2226 drv->tty_driver = NULL;
2227}
2228
2229struct tty_driver *uart_console_device(struct console *co, int *index)
2230{
2231 struct uart_driver *p = co->data;
2232 *index = co->index;
2233 return p->tty_driver;
2234}
2235
2236/**
2237 * uart_add_one_port - attach a driver-defined port structure
2238 * @drv: pointer to the uart low level driver structure for this port
2239 * @port: uart port structure to use for this port.
2240 *
2241 * This allows the driver to register its own uart_port structure
2242 * with the core driver. The main purpose is to allow the low
2243 * level uart drivers to expand uart_port, rather than having yet
2244 * more levels of structures.
2245 */
2246int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2247{
2248 struct uart_state *state;
2249 int ret = 0;
2250
2251 BUG_ON(in_interrupt());
2252
2253 if (port->line >= drv->nr)
2254 return -EINVAL;
2255
2256 state = drv->state + port->line;
2257
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002258 mutex_lock(&port_mutex);
Russell King68ac64c2006-04-30 11:13:50 +01002259 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 if (state->port) {
2261 ret = -EINVAL;
2262 goto out;
2263 }
2264
2265 state->port = port;
2266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 port->cons = drv->cons;
2268 port->info = state->info;
2269
Russell King976ecd12005-07-03 21:05:45 +01002270 /*
2271 * If this port is a console, then the spinlock is already
2272 * initialised.
2273 */
Ingo Molnar13e83592006-07-03 00:25:03 -07002274 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
Russell King976ecd12005-07-03 21:05:45 +01002275 spin_lock_init(&port->lock);
Ingo Molnar13e83592006-07-03 00:25:03 -07002276 lockdep_set_class(&port->lock, &port_lock_key);
2277 }
Russell King976ecd12005-07-03 21:05:45 +01002278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 uart_configure_port(drv, state, port);
2280
2281 /*
2282 * Register the port whether it's detected or not. This allows
2283 * setserial to be used to alter this ports parameters.
2284 */
2285 tty_register_device(drv->tty_driver, port->line, port->dev);
2286
2287 /*
2288 * If this driver supports console, and it hasn't been
2289 * successfully registered yet, try to re-register it.
2290 * It may be that the port was not available.
2291 */
2292 if (port->type != PORT_UNKNOWN &&
2293 port->cons && !(port->cons->flags & CON_ENABLED))
2294 register_console(port->cons);
2295
Russell King68ac64c2006-04-30 11:13:50 +01002296 /*
2297 * Ensure UPF_DEAD is not set.
2298 */
2299 port->flags &= ~UPF_DEAD;
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 out:
Russell King68ac64c2006-04-30 11:13:50 +01002302 mutex_unlock(&state->mutex);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002303 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 return ret;
2306}
2307
2308/**
2309 * uart_remove_one_port - detach a driver defined port structure
2310 * @drv: pointer to the uart low level driver structure for this port
2311 * @port: uart port structure for this port
2312 *
2313 * This unhooks (and hangs up) the specified port structure from the
2314 * core driver. No further calls will be made to the low-level code
2315 * for this port.
2316 */
2317int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2318{
2319 struct uart_state *state = drv->state + port->line;
Russell King68ac64c2006-04-30 11:13:50 +01002320 struct uart_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 BUG_ON(in_interrupt());
2323
2324 if (state->port != port)
2325 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2326 state->port, port);
2327
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002328 mutex_lock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 /*
Russell King68ac64c2006-04-30 11:13:50 +01002331 * Mark the port "dead" - this prevents any opens from
2332 * succeeding while we shut down the port.
2333 */
2334 mutex_lock(&state->mutex);
2335 port->flags |= UPF_DEAD;
2336 mutex_unlock(&state->mutex);
2337
2338 /*
Greg Kroah-Hartmanaa4148c2005-06-20 21:15:16 -07002339 * Remove the devices from the tty layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 */
2341 tty_unregister_device(drv->tty_driver, port->line);
2342
Russell King68ac64c2006-04-30 11:13:50 +01002343 info = state->info;
2344 if (info && info->tty)
2345 tty_vhangup(info->tty);
2346
2347 /*
2348 * All users of this port should now be disconnected from
2349 * this driver, and the port shut down. We should be the
2350 * only thread fiddling with this port from now on.
2351 */
2352 state->info = NULL;
2353
2354 /*
2355 * Free the port IO and memory resources, if any.
2356 */
2357 if (port->type != PORT_UNKNOWN)
2358 port->ops->release_port(port);
2359
2360 /*
2361 * Indicate that there isn't a port here anymore.
2362 */
2363 port->type = PORT_UNKNOWN;
2364
2365 /*
2366 * Kill the tasklet, and free resources.
2367 */
2368 if (info) {
2369 tasklet_kill(&info->tlet);
2370 kfree(info);
2371 }
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 state->port = NULL;
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002374 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376 return 0;
2377}
2378
2379/*
2380 * Are the two ports equivalent?
2381 */
2382int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2383{
2384 if (port1->iotype != port2->iotype)
2385 return 0;
2386
2387 switch (port1->iotype) {
2388 case UPIO_PORT:
2389 return (port1->iobase == port2->iobase);
2390 case UPIO_HUB6:
2391 return (port1->iobase == port2->iobase) &&
2392 (port1->hub6 == port2->hub6);
2393 case UPIO_MEM:
Sergei Shtylyovd21b55d2006-08-28 19:49:03 +04002394 case UPIO_MEM32:
2395 case UPIO_AU:
2396 case UPIO_TSI:
Benjamin Herrenschmidt1624f002006-01-04 18:09:44 +00002397 return (port1->mapbase == port2->mapbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
2399 return 0;
2400}
2401EXPORT_SYMBOL(uart_match_port);
2402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403EXPORT_SYMBOL(uart_write_wakeup);
2404EXPORT_SYMBOL(uart_register_driver);
2405EXPORT_SYMBOL(uart_unregister_driver);
2406EXPORT_SYMBOL(uart_suspend_port);
2407EXPORT_SYMBOL(uart_resume_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408EXPORT_SYMBOL(uart_add_one_port);
2409EXPORT_SYMBOL(uart_remove_one_port);
2410
2411MODULE_DESCRIPTION("Serial driver core");
2412MODULE_LICENSE("GPL");