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