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