2 * linux/drivers/char/core.c
4 * Driver core for serial ports
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8 * Copyright 1999 ARM Limited
9 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
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.
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.
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
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 #include <linux/mutex.h>
39 #include <asm/uaccess.h>
43 #define DPRINTK(x...) printk(x)
45 #define DPRINTK(x...) do { } while (0)
49 * This is used to lock changes in serial line configuration.
51 static DEFINE_MUTEX(port_mutex
);
53 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
55 #define uart_users(state) ((state)->count + ((state)->info ? (state)->info->blocked_open : 0))
57 #ifdef CONFIG_SERIAL_CORE_CONSOLE
58 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
60 #define uart_console(port) (0)
63 static void uart_change_speed(struct uart_state
*state
, struct termios
*old_termios
);
64 static void uart_wait_until_sent(struct tty_struct
*tty
, int timeout
);
65 static void uart_change_pm(struct uart_state
*state
, int pm_state
);
68 * This routine is used by the interrupt handler to schedule processing in
69 * the software interrupt portion of the driver.
71 void uart_write_wakeup(struct uart_port
*port
)
73 struct uart_info
*info
= port
->info
;
74 tasklet_schedule(&info
->tlet
);
77 static void uart_stop(struct tty_struct
*tty
)
79 struct uart_state
*state
= tty
->driver_data
;
80 struct uart_port
*port
= state
->port
;
83 spin_lock_irqsave(&port
->lock
, flags
);
84 port
->ops
->stop_tx(port
);
85 spin_unlock_irqrestore(&port
->lock
, flags
);
88 static void __uart_start(struct tty_struct
*tty
)
90 struct uart_state
*state
= tty
->driver_data
;
91 struct uart_port
*port
= state
->port
;
93 if (!uart_circ_empty(&state
->info
->xmit
) && state
->info
->xmit
.buf
&&
94 !tty
->stopped
&& !tty
->hw_stopped
)
95 port
->ops
->start_tx(port
);
98 static void uart_start(struct tty_struct
*tty
)
100 struct uart_state
*state
= tty
->driver_data
;
101 struct uart_port
*port
= state
->port
;
104 spin_lock_irqsave(&port
->lock
, flags
);
106 spin_unlock_irqrestore(&port
->lock
, flags
);
109 static void uart_tasklet_action(unsigned long data
)
111 struct uart_state
*state
= (struct uart_state
*)data
;
112 tty_wakeup(state
->info
->tty
);
116 uart_update_mctrl(struct uart_port
*port
, unsigned int set
, unsigned int clear
)
121 spin_lock_irqsave(&port
->lock
, flags
);
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
);
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)
133 * Startup the port. This will be called once per open. All calls
134 * will be serialised by the per-port semaphore.
136 static int uart_startup(struct uart_state
*state
, int init_hw
)
138 struct uart_info
*info
= state
->info
;
139 struct uart_port
*port
= state
->port
;
143 if (info
->flags
& UIF_INITIALIZED
)
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
151 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
153 if (port
->type
== PORT_UNKNOWN
)
157 * Initialise and allocate the transmit and temporary
160 if (!info
->xmit
.buf
) {
161 page
= get_zeroed_page(GFP_KERNEL
);
165 info
->xmit
.buf
= (unsigned char *) page
;
166 uart_circ_clear(&info
->xmit
);
169 retval
= port
->ops
->startup(port
);
173 * Initialise the hardware port settings.
175 uart_change_speed(state
, NULL
);
178 * Setup the RTS and DTR signals once the
179 * port is open and ready to respond.
181 if (info
->tty
->termios
->c_cflag
& CBAUD
)
182 uart_set_mctrl(port
, TIOCM_RTS
| TIOCM_DTR
);
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
);
192 info
->flags
|= UIF_INITIALIZED
;
194 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
197 if (retval
&& capable(CAP_SYS_ADMIN
))
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.
208 static void uart_shutdown(struct uart_state
*state
)
210 struct uart_info
*info
= state
->info
;
211 struct uart_port
*port
= state
->port
;
214 * Set the TTY IO error marker
217 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
219 if (info
->flags
& UIF_INITIALIZED
) {
220 info
->flags
&= ~UIF_INITIALIZED
;
223 * Turn off DTR and RTS early.
225 if (!info
->tty
|| (info
->tty
->termios
->c_cflag
& HUPCL
))
226 uart_clear_mctrl(port
, TIOCM_DTR
| TIOCM_RTS
);
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.
235 wake_up_interruptible(&info
->delta_msr_wait
);
238 * Free the IRQ and disable the port.
240 port
->ops
->shutdown(port
);
243 * Ensure that the IRQ handler isn't running on another CPU.
245 synchronize_irq(port
->irq
);
249 * kill off our tasklet
251 tasklet_kill(&info
->tlet
);
254 * Free the transmit buffer page.
256 if (info
->xmit
.buf
) {
257 free_page((unsigned long)info
->xmit
.buf
);
258 info
->xmit
.buf
= NULL
;
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
268 * Set the port FIFO timeout value. The @cflag value should
269 * reflect the actual hardware settings.
272 uart_update_timeout(struct uart_port
*port
, unsigned int cflag
,
277 /* byte size and parity */
278 switch (cflag
& CSIZE
) {
299 * The total number of bits to be transmitted in the fifo.
301 bits
= bits
* port
->fifosize
;
304 * Figure the timeout to send the above number of bits.
305 * Add .02 seconds of slop
307 port
->timeout
= (HZ
* bits
) / baud
+ HZ
/50;
310 EXPORT_SYMBOL(uart_update_timeout
);
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
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.
324 * If the new baud rate is invalid, try the old termios setting.
325 * If it's still invalid, we try 9600 baud.
327 * Update the @termios structure to reflect the baud rate
328 * we're actually going to be using.
331 uart_get_baud_rate(struct uart_port
*port
, struct termios
*termios
,
332 struct termios
*old
, unsigned int min
, unsigned int max
)
334 unsigned int try, baud
, altbaud
= 38400;
335 upf_t flags
= port
->flags
& UPF_SPD_MASK
;
337 if (flags
== UPF_SPD_HI
)
339 if (flags
== UPF_SPD_VHI
)
341 if (flags
== UPF_SPD_SHI
)
343 if (flags
== UPF_SPD_WARP
)
346 for (try = 0; try < 2; try++) {
347 baud
= tty_termios_baud_rate(termios
);
350 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
357 * Special case: B0 rate.
362 if (baud
>= min
&& baud
<= max
)
366 * Oops, the quotient was zero. Try again with
367 * the old baud rate if possible.
369 termios
->c_cflag
&= ~CBAUD
;
371 termios
->c_cflag
|= old
->c_cflag
& CBAUD
;
377 * As a last resort, if the quotient is zero,
378 * default to 9600 bps
380 termios
->c_cflag
|= B9600
;
386 EXPORT_SYMBOL(uart_get_baud_rate
);
389 * uart_get_divisor - return uart clock divisor
390 * @port: uart_port structure describing the port.
391 * @baud: desired baud rate
393 * Calculate the uart clock divisor for the port.
396 uart_get_divisor(struct uart_port
*port
, unsigned int baud
)
401 * Old custom speed handling.
403 if (baud
== 38400 && (port
->flags
& UPF_SPD_MASK
) == UPF_SPD_CUST
)
404 quot
= port
->custom_divisor
;
406 quot
= (port
->uartclk
+ (8 * baud
)) / (16 * baud
);
411 EXPORT_SYMBOL(uart_get_divisor
);
414 uart_change_speed(struct uart_state
*state
, struct termios
*old_termios
)
416 struct tty_struct
*tty
= state
->info
->tty
;
417 struct uart_port
*port
= state
->port
;
418 struct termios
*termios
;
421 * If we have no tty, termios, or the port does not exist,
422 * then we can't set the parameters for this port.
424 if (!tty
|| !tty
->termios
|| port
->type
== PORT_UNKNOWN
)
427 termios
= tty
->termios
;
430 * Set flags based on termios cflag
432 if (termios
->c_cflag
& CRTSCTS
)
433 state
->info
->flags
|= UIF_CTS_FLOW
;
435 state
->info
->flags
&= ~UIF_CTS_FLOW
;
437 if (termios
->c_cflag
& CLOCAL
)
438 state
->info
->flags
&= ~UIF_CHECK_CD
;
440 state
->info
->flags
|= UIF_CHECK_CD
;
442 port
->ops
->set_termios(port
, termios
, old_termios
);
446 __uart_put_char(struct uart_port
*port
, struct circ_buf
*circ
, unsigned char c
)
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);
458 spin_unlock_irqrestore(&port
->lock
, flags
);
461 static void uart_put_char(struct tty_struct
*tty
, unsigned char ch
)
463 struct uart_state
*state
= tty
->driver_data
;
465 __uart_put_char(state
->port
, &state
->info
->xmit
, ch
);
468 static void uart_flush_chars(struct tty_struct
*tty
)
474 uart_write(struct tty_struct
*tty
, const unsigned char * buf
, int count
)
476 struct uart_state
*state
= tty
->driver_data
;
477 struct uart_port
*port
= state
->port
;
478 struct circ_buf
*circ
= &state
->info
->xmit
;
485 spin_lock_irqsave(&port
->lock
, flags
);
487 c
= CIRC_SPACE_TO_END(circ
->head
, circ
->tail
, UART_XMIT_SIZE
);
492 memcpy(circ
->buf
+ circ
->head
, buf
, c
);
493 circ
->head
= (circ
->head
+ c
) & (UART_XMIT_SIZE
- 1);
498 spin_unlock_irqrestore(&port
->lock
, flags
);
504 static int uart_write_room(struct tty_struct
*tty
)
506 struct uart_state
*state
= tty
->driver_data
;
508 return uart_circ_chars_free(&state
->info
->xmit
);
511 static int uart_chars_in_buffer(struct tty_struct
*tty
)
513 struct uart_state
*state
= tty
->driver_data
;
515 return uart_circ_chars_pending(&state
->info
->xmit
);
518 static void uart_flush_buffer(struct tty_struct
*tty
)
520 struct uart_state
*state
= tty
->driver_data
;
521 struct uart_port
*port
= state
->port
;
524 DPRINTK("uart_flush_buffer(%d) called\n", tty
->index
);
526 spin_lock_irqsave(&port
->lock
, flags
);
527 uart_circ_clear(&state
->info
->xmit
);
528 spin_unlock_irqrestore(&port
->lock
, flags
);
533 * This function is used to send a high-priority XON/XOFF character to
536 static void uart_send_xchar(struct tty_struct
*tty
, char ch
)
538 struct uart_state
*state
= tty
->driver_data
;
539 struct uart_port
*port
= state
->port
;
542 if (port
->ops
->send_xchar
)
543 port
->ops
->send_xchar(port
, ch
);
547 spin_lock_irqsave(&port
->lock
, flags
);
548 port
->ops
->start_tx(port
);
549 spin_unlock_irqrestore(&port
->lock
, flags
);
554 static void uart_throttle(struct tty_struct
*tty
)
556 struct uart_state
*state
= tty
->driver_data
;
559 uart_send_xchar(tty
, STOP_CHAR(tty
));
561 if (tty
->termios
->c_cflag
& CRTSCTS
)
562 uart_clear_mctrl(state
->port
, TIOCM_RTS
);
565 static void uart_unthrottle(struct tty_struct
*tty
)
567 struct uart_state
*state
= tty
->driver_data
;
568 struct uart_port
*port
= state
->port
;
574 uart_send_xchar(tty
, START_CHAR(tty
));
577 if (tty
->termios
->c_cflag
& CRTSCTS
)
578 uart_set_mctrl(port
, TIOCM_RTS
);
581 static int uart_get_info(struct uart_state
*state
,
582 struct serial_struct __user
*retinfo
)
584 struct uart_port
*port
= state
->port
;
585 struct serial_struct tmp
;
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
;
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
;
607 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
612 static int uart_set_info(struct uart_state
*state
,
613 struct serial_struct __user
*newinfo
)
615 struct serial_struct new_serial
;
616 struct uart_port
*port
= state
->port
;
617 unsigned long new_port
;
618 unsigned int change_irq
, change_port
, closing_wait
;
619 unsigned int old_custom_divisor
, close_delay
;
620 upf_t old_flags
, new_flags
;
623 if (copy_from_user(&new_serial
, newinfo
, sizeof(new_serial
)))
626 new_port
= new_serial
.port
;
627 if (HIGH_BITS_OFFSET
)
628 new_port
+= (unsigned long) new_serial
.port_high
<< HIGH_BITS_OFFSET
;
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;
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
642 mutex_lock(&state
->mutex
);
644 change_irq
= new_serial
.irq
!= port
->irq
;
647 * Since changing the 'type' of the port changes its resource
648 * allocations, we should treat type changes the same as
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
;
658 old_flags
= port
->flags
;
659 new_flags
= new_serial
.flags
;
660 old_custom_divisor
= port
->custom_divisor
;
662 if (!capable(CAP_SYS_ADMIN
)) {
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
) ||
669 (((new_flags
^ old_flags
) & ~UPF_USR_MASK
) != 0))
671 port
->flags
= ((port
->flags
& ~UPF_USR_MASK
) |
672 (new_flags
& UPF_USR_MASK
));
673 port
->custom_divisor
= new_serial
.custom_divisor
;
678 * Ask the low level driver to verify the settings.
680 if (port
->ops
->verify_port
)
681 retval
= port
->ops
->verify_port(port
, &new_serial
);
683 if ((new_serial
.irq
>= NR_IRQS
) || (new_serial
.irq
< 0) ||
684 (new_serial
.baud_base
< 9600))
690 if (change_port
|| change_irq
) {
694 * Make sure that we are the sole user of this port.
696 if (uart_users(state
) > 1)
700 * We need to shutdown the serial port at the old
701 * port/type/irq combination.
703 uart_shutdown(state
);
707 unsigned long old_iobase
, old_mapbase
;
708 unsigned int old_type
, old_iotype
, old_hub6
, old_shift
;
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
;
718 * Free and release old regions
720 if (old_type
!= PORT_UNKNOWN
)
721 port
->ops
->release_port(port
);
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
;
731 * Claim and map the new regions
733 if (port
->type
!= PORT_UNKNOWN
) {
734 retval
= port
->ops
->request_port(port
);
736 /* Always success - Jean II */
741 * If we fail to request resources for the
742 * new port, try to restore the old settings.
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
);
753 * If we failed to restore the old settings,
757 port
->type
= PORT_UNKNOWN
;
766 port
->irq
= new_serial
.irq
;
767 port
->uartclk
= new_serial
.baud_base
* 16;
768 port
->flags
= (port
->flags
& ~UPF_CHANGE_MASK
) |
769 (new_flags
& UPF_CHANGE_MASK
);
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;
780 if (port
->type
== PORT_UNKNOWN
)
782 if (state
->info
->flags
& UIF_INITIALIZED
) {
783 if (((old_flags
^ port
->flags
) & UPF_SPD_MASK
) ||
784 old_custom_divisor
!= port
->custom_divisor
) {
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.
790 if (port
->flags
& UPF_SPD_MASK
) {
793 "%s sets custom speed on %s. This "
794 "is deprecated.\n", current
->comm
,
795 tty_name(state
->info
->tty
, buf
));
797 uart_change_speed(state
, NULL
);
800 retval
= uart_startup(state
, 1);
802 mutex_unlock(&state
->mutex
);
808 * uart_get_lsr_info - get line status register info.
809 * Note: uart_ioctl protects us against hangups.
811 static int uart_get_lsr_info(struct uart_state
*state
,
812 unsigned int __user
*value
)
814 struct uart_port
*port
= state
->port
;
817 result
= port
->ops
->tx_empty(port
);
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).
826 ((uart_circ_chars_pending(&state
->info
->xmit
) > 0) &&
827 !state
->info
->tty
->stopped
&& !state
->info
->tty
->hw_stopped
))
828 result
&= ~TIOCSER_TEMT
;
830 return put_user(result
, value
);
833 static int uart_tiocmget(struct tty_struct
*tty
, struct file
*file
)
835 struct uart_state
*state
= tty
->driver_data
;
836 struct uart_port
*port
= state
->port
;
839 mutex_lock(&state
->mutex
);
840 if ((!file
|| !tty_hung_up_p(file
)) &&
841 !(tty
->flags
& (1 << TTY_IO_ERROR
))) {
842 result
= port
->mctrl
;
844 spin_lock_irq(&port
->lock
);
845 result
|= port
->ops
->get_mctrl(port
);
846 spin_unlock_irq(&port
->lock
);
848 mutex_unlock(&state
->mutex
);
854 uart_tiocmset(struct tty_struct
*tty
, struct file
*file
,
855 unsigned int set
, unsigned int clear
)
857 struct uart_state
*state
= tty
->driver_data
;
858 struct uart_port
*port
= state
->port
;
861 mutex_lock(&state
->mutex
);
862 if ((!file
|| !tty_hung_up_p(file
)) &&
863 !(tty
->flags
& (1 << TTY_IO_ERROR
))) {
864 uart_update_mctrl(port
, set
, clear
);
867 mutex_unlock(&state
->mutex
);
871 static void uart_break_ctl(struct tty_struct
*tty
, int break_state
)
873 struct uart_state
*state
= tty
->driver_data
;
874 struct uart_port
*port
= state
->port
;
876 BUG_ON(!kernel_locked());
878 mutex_lock(&state
->mutex
);
880 if (port
->type
!= PORT_UNKNOWN
)
881 port
->ops
->break_ctl(port
, break_state
);
883 mutex_unlock(&state
->mutex
);
886 static int uart_do_autoconfig(struct uart_state
*state
)
888 struct uart_port
*port
= state
->port
;
891 if (!capable(CAP_SYS_ADMIN
))
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.
899 if (mutex_lock_interruptible(&state
->mutex
))
903 if (uart_users(state
) == 1) {
904 uart_shutdown(state
);
907 * If we already have a port type configured,
908 * we must release its resources.
910 if (port
->type
!= PORT_UNKNOWN
)
911 port
->ops
->release_port(port
);
913 flags
= UART_CONFIG_TYPE
;
914 if (port
->flags
& UPF_AUTO_IRQ
)
915 flags
|= UART_CONFIG_IRQ
;
918 * This will claim the ports resources if
921 port
->ops
->config_port(port
, flags
);
923 ret
= uart_startup(state
, 1);
925 mutex_unlock(&state
->mutex
);
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
936 uart_wait_modem_status(struct uart_state
*state
, unsigned long arg
)
938 struct uart_port
*port
= state
->port
;
939 DECLARE_WAITQUEUE(wait
, current
);
940 struct uart_icount cprev
, cnow
;
944 * note the counters on entry
946 spin_lock_irq(&port
->lock
);
947 memcpy(&cprev
, &port
->icount
, sizeof(struct uart_icount
));
950 * Force modem status interrupts on
952 port
->ops
->enable_ms(port
);
953 spin_unlock_irq(&port
->lock
);
955 add_wait_queue(&state
->info
->delta_msr_wait
, &wait
);
957 spin_lock_irq(&port
->lock
);
958 memcpy(&cnow
, &port
->icount
, sizeof(struct uart_icount
));
959 spin_unlock_irq(&port
->lock
);
961 set_current_state(TASK_INTERRUPTIBLE
);
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
))) {
973 /* see if a signal did it */
974 if (signal_pending(current
)) {
982 current
->state
= TASK_RUNNING
;
983 remove_wait_queue(&state
->info
->delta_msr_wait
, &wait
);
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.
994 static int uart_get_count(struct uart_state
*state
,
995 struct serial_icounter_struct __user
*icnt
)
997 struct serial_icounter_struct icount
;
998 struct uart_icount cnow
;
999 struct uart_port
*port
= state
->port
;
1001 spin_lock_irq(&port
->lock
);
1002 memcpy(&cnow
, &port
->icount
, sizeof(struct uart_icount
));
1003 spin_unlock_irq(&port
->lock
);
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
;
1017 return copy_to_user(icnt
, &icount
, sizeof(icount
)) ? -EFAULT
: 0;
1021 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
1024 uart_ioctl(struct tty_struct
*tty
, struct file
*filp
, unsigned int cmd
,
1027 struct uart_state
*state
= tty
->driver_data
;
1028 void __user
*uarg
= (void __user
*)arg
;
1029 int ret
= -ENOIOCTLCMD
;
1031 BUG_ON(!kernel_locked());
1034 * These ioctls don't rely on the hardware to be present.
1038 ret
= uart_get_info(state
, uarg
);
1042 ret
= uart_set_info(state
, uarg
);
1046 ret
= uart_do_autoconfig(state
);
1049 case TIOCSERGWILD
: /* obsolete */
1050 case TIOCSERSWILD
: /* obsolete */
1055 if (ret
!= -ENOIOCTLCMD
)
1058 if (tty
->flags
& (1 << TTY_IO_ERROR
)) {
1064 * The following should only be used when hardware is present.
1068 ret
= uart_wait_modem_status(state
, arg
);
1072 ret
= uart_get_count(state
, uarg
);
1076 if (ret
!= -ENOIOCTLCMD
)
1079 mutex_lock(&state
->mutex
);
1081 if (tty_hung_up_p(filp
)) {
1087 * All these rely on hardware being present and need to be
1088 * protected against the tty being hung up.
1091 case TIOCSERGETLSR
: /* Get line status register */
1092 ret
= uart_get_lsr_info(state
, uarg
);
1096 struct uart_port
*port
= state
->port
;
1097 if (port
->ops
->ioctl
)
1098 ret
= port
->ops
->ioctl(port
, cmd
, arg
);
1103 mutex_unlock(&state
->mutex
);
1108 static void uart_set_termios(struct tty_struct
*tty
, struct termios
*old_termios
)
1110 struct uart_state
*state
= tty
->driver_data
;
1111 unsigned long flags
;
1112 unsigned int cflag
= tty
->termios
->c_cflag
;
1114 BUG_ON(!kernel_locked());
1117 * These are the bits that are used to setup various
1118 * flags in the low level driver.
1120 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1122 if ((cflag
^ old_termios
->c_cflag
) == 0 &&
1123 RELEVANT_IFLAG(tty
->termios
->c_iflag
^ old_termios
->c_iflag
) == 0)
1126 uart_change_speed(state
, old_termios
);
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
);
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
))
1138 uart_set_mctrl(state
->port
, mask
);
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;
1146 spin_unlock_irqrestore(&state
->port
->lock
, flags
);
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;
1154 state
->port
->ops
->stop_tx(state
->port
);
1156 spin_unlock_irqrestore(&state
->port
->lock
, flags
);
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.....
1166 if (!(old_termios
->c_cflag
& CLOCAL
) &&
1167 (tty
->termios
->c_cflag
& CLOCAL
))
1168 wake_up_interruptible(&state
->info
->open_wait
);
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()
1177 static void uart_close(struct tty_struct
*tty
, struct file
*filp
)
1179 struct uart_state
*state
= tty
->driver_data
;
1180 struct uart_port
*port
;
1182 BUG_ON(!kernel_locked());
1184 if (!state
|| !state
->port
)
1189 DPRINTK("uart_close(%d) called\n", port
->line
);
1191 mutex_lock(&state
->mutex
);
1193 if (tty_hung_up_p(filp
))
1196 if ((tty
->count
== 1) && (state
->count
!= 1)) {
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.
1204 printk(KERN_ERR
"uart_close: bad serial port count; tty->count is 1, "
1205 "state->count is %d\n", state
->count
);
1208 if (--state
->count
< 0) {
1209 printk(KERN_ERR
"uart_close: bad serial port count for %s: %d\n",
1210 tty
->name
, state
->count
);
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.
1223 if (state
->closing_wait
!= USF_CLOSING_WAIT_NONE
)
1224 tty_wait_until_sent(tty
, msecs_to_jiffies(state
->closing_wait
));
1227 * At this point, we stop accepting input. To do this, we
1228 * disable the receive line status interrupts.
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
);
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!
1240 uart_wait_until_sent(tty
, port
->timeout
);
1243 uart_shutdown(state
);
1244 uart_flush_buffer(tty
);
1246 tty_ldisc_flush(tty
);
1249 state
->info
->tty
= NULL
;
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);
1259 * Wake up anyone trying to open this port.
1261 state
->info
->flags
&= ~UIF_NORMAL_ACTIVE
;
1262 wake_up_interruptible(&state
->info
->open_wait
);
1265 mutex_unlock(&state
->mutex
);
1268 static void uart_wait_until_sent(struct tty_struct
*tty
, int timeout
)
1270 struct uart_state
*state
= tty
->driver_data
;
1271 struct uart_port
*port
= state
->port
;
1272 unsigned long char_time
, expire
;
1274 BUG_ON(!kernel_locked());
1276 if (port
->type
== PORT_UNKNOWN
|| port
->fifosize
== 0)
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.
1284 * Note: we have to use pretty tight timings here to satisfy
1287 char_time
= (port
->timeout
- HZ
/50) / port
->fifosize
;
1288 char_time
= char_time
/ 5;
1291 if (timeout
&& timeout
< char_time
)
1292 char_time
= timeout
;
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
1303 if (timeout
== 0 || timeout
> 2 * port
->timeout
)
1304 timeout
= 2 * port
->timeout
;
1306 expire
= jiffies
+ timeout
;
1308 DPRINTK("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1309 port
->line
, jiffies
, expire
);
1312 * Check whether the transmitter is empty every 'char_time'.
1313 * 'timeout' / 'expire' give us the maximum amount of time
1316 while (!port
->ops
->tx_empty(port
)) {
1317 msleep_interruptible(jiffies_to_msecs(char_time
));
1318 if (signal_pending(current
))
1320 if (time_after(jiffies
, expire
))
1323 set_current_state(TASK_RUNNING
); /* might not be needed */
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.
1332 static void uart_hangup(struct tty_struct
*tty
)
1334 struct uart_state
*state
= tty
->driver_data
;
1336 BUG_ON(!kernel_locked());
1337 DPRINTK("uart_hangup(%d)\n", state
->port
->line
);
1339 mutex_lock(&state
->mutex
);
1340 if (state
->info
&& state
->info
->flags
& UIF_NORMAL_ACTIVE
) {
1341 uart_flush_buffer(tty
);
1342 uart_shutdown(state
);
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
);
1349 mutex_unlock(&state
->mutex
);
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.
1358 static void uart_update_termios(struct uart_state
*state
)
1360 struct tty_struct
*tty
= state
->info
->tty
;
1361 struct uart_port
*port
= state
->port
;
1363 if (uart_console(port
) && port
->cons
->cflag
) {
1364 tty
->termios
->c_cflag
= port
->cons
->cflag
;
1365 port
->cons
->cflag
= 0;
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.
1373 if (!(tty
->flags
& (1 << TTY_IO_ERROR
))) {
1375 * Make termios settings take effect.
1377 uart_change_speed(state
, NULL
);
1380 * And finally enable the RTS and DTR signals.
1382 if (tty
->termios
->c_cflag
& CBAUD
)
1383 uart_set_mctrl(port
, TIOCM_DTR
| TIOCM_RTS
);
1388 * Block the open until the port is ready. We must be called with
1389 * the per-port semaphore held.
1392 uart_block_til_ready(struct file
*filp
, struct uart_state
*state
)
1394 DECLARE_WAITQUEUE(wait
, current
);
1395 struct uart_info
*info
= state
->info
;
1396 struct uart_port
*port
= state
->port
;
1399 info
->blocked_open
++;
1402 add_wait_queue(&info
->open_wait
, &wait
);
1404 set_current_state(TASK_INTERRUPTIBLE
);
1407 * If we have been hung up, tell userspace/restart open.
1409 if (tty_hung_up_p(filp
) || info
->tty
== NULL
)
1413 * If the port has been closed, tell userspace/restart open.
1415 if (!(info
->flags
& UIF_INITIALIZED
))
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.
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.
1427 if ((filp
->f_flags
& O_NONBLOCK
) ||
1428 (info
->tty
->termios
->c_cflag
& CLOCAL
) ||
1429 (info
->tty
->flags
& (1 << TTY_IO_ERROR
))) {
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.
1438 if (info
->tty
->termios
->c_cflag
& CBAUD
)
1439 uart_set_mctrl(port
, TIOCM_DTR
);
1442 * and wait for the carrier to indicate that the
1443 * modem is ready for us.
1445 spin_lock_irq(&port
->lock
);
1446 port
->ops
->enable_ms(port
);
1447 mctrl
= port
->ops
->get_mctrl(port
);
1448 spin_unlock_irq(&port
->lock
);
1449 if (mctrl
& TIOCM_CAR
)
1452 mutex_unlock(&state
->mutex
);
1454 mutex_lock(&state
->mutex
);
1456 if (signal_pending(current
))
1459 set_current_state(TASK_RUNNING
);
1460 remove_wait_queue(&info
->open_wait
, &wait
);
1463 info
->blocked_open
--;
1465 if (signal_pending(current
))
1466 return -ERESTARTSYS
;
1468 if (!info
->tty
|| tty_hung_up_p(filp
))
1474 static struct uart_state
*uart_get(struct uart_driver
*drv
, int line
)
1476 struct uart_state
*state
;
1478 mutex_lock(&port_mutex
);
1479 state
= drv
->state
+ line
;
1480 if (mutex_lock_interruptible(&state
->mutex
)) {
1481 state
= ERR_PTR(-ERESTARTSYS
);
1488 mutex_unlock(&state
->mutex
);
1489 state
= ERR_PTR(-ENXIO
);
1494 state
->info
= kmalloc(sizeof(struct uart_info
), GFP_KERNEL
);
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
);
1501 * Link the info into the other structures.
1503 state
->port
->info
= state
->info
;
1505 tasklet_init(&state
->info
->tlet
, uart_tasklet_action
,
1506 (unsigned long)state
);
1509 mutex_unlock(&state
->mutex
);
1510 state
= ERR_PTR(-ENOMEM
);
1515 mutex_unlock(&port_mutex
);
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.
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.
1529 static int uart_open(struct tty_struct
*tty
, struct file
*filp
)
1531 struct uart_driver
*drv
= (struct uart_driver
*)tty
->driver
->driver_state
;
1532 struct uart_state
*state
;
1533 int retval
, line
= tty
->index
;
1535 BUG_ON(!kernel_locked());
1536 DPRINTK("uart_open(%d) called\n", line
);
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()).
1544 if (line
>= tty
->driver
->num
)
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.
1554 state
= uart_get(drv
, line
);
1555 if (IS_ERR(state
)) {
1556 retval
= PTR_ERR(state
);
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.
1565 tty
->driver_data
= state
;
1566 tty
->low_latency
= (state
->port
->flags
& UPF_LOW_LATENCY
) ? 1 : 0;
1568 state
->info
->tty
= tty
;
1571 * If the port is in the middle of closing, bail out now.
1573 if (tty_hung_up_p(filp
)) {
1576 mutex_unlock(&state
->mutex
);
1581 * Make sure the device is in D0 state.
1583 if (state
->count
== 1)
1584 uart_change_pm(state
, 0);
1587 * Start up the serial port.
1589 retval
= uart_startup(state
, 0);
1592 * If we succeeded, wait until the port is ready.
1595 retval
= uart_block_til_ready(filp
, state
);
1596 mutex_unlock(&state
->mutex
);
1599 * If this is the first open to succeed, adjust things to suit.
1601 if (retval
== 0 && !(state
->info
->flags
& UIF_NORMAL_ACTIVE
)) {
1602 state
->info
->flags
|= UIF_NORMAL_ACTIVE
;
1604 uart_update_termios(state
);
1611 static const char *uart_type(struct uart_port
*port
)
1613 const char *str
= NULL
;
1615 if (port
->ops
->type
)
1616 str
= port
->ops
->type(port
);
1624 #ifdef CONFIG_PROC_FS
1626 static int uart_line_info(char *buf
, struct uart_driver
*drv
, int i
)
1628 struct uart_state
*state
= drv
->state
+ i
;
1629 struct uart_port
*port
= state
->port
;
1631 unsigned int status
;
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
,
1644 if (port
->type
== PORT_UNKNOWN
) {
1649 if(capable(CAP_SYS_ADMIN
))
1651 spin_lock_irq(&port
->lock
);
1652 status
= port
->ops
->get_mctrl(port
);
1653 spin_unlock_irq(&port
->lock
);
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",
1666 if (port
->icount
.overrun
)
1667 ret
+= sprintf(buf
+ ret
, " oe:%d",
1668 port
->icount
.overrun
);
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)
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");
1689 strcat(stat_buf
, "\n");
1691 ret
+= sprintf(buf
+ ret
, stat_buf
);
1701 static int uart_read_proc(char *page
, char **start
, off_t off
,
1702 int count
, int *eof
, void *data
)
1704 struct tty_driver
*ttydrv
= data
;
1705 struct uart_driver
*drv
= ttydrv
->driver_state
;
1709 len
+= sprintf(page
, "serinfo:1.0 driver%s%s revision:%s\n",
1711 for (i
= 0; i
< drv
->nr
&& len
< PAGE_SIZE
- 96; i
++) {
1712 l
= uart_line_info(page
+ len
, drv
, i
);
1714 if (len
+ begin
> off
+ count
)
1716 if (len
+ begin
< off
) {
1723 if (off
>= len
+ begin
)
1725 *start
= page
+ (off
- begin
);
1726 return (count
< begin
+ len
- off
) ? count
: (begin
+ len
- off
);
1730 #ifdef CONFIG_SERIAL_CORE_CONSOLE
1732 * Check whether an invalid uart number has been specified, and
1733 * if so, search for the first available port that does have
1736 struct uart_port
* __init
1737 uart_get_console(struct uart_port
*ports
, int nr
, struct console
*co
)
1739 int idx
= co
->index
;
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
)
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.
1761 * uart_parse_options decodes a string containing the serial console
1762 * options. The format of the string is <baud><parity><bits><flow>,
1766 uart_parse_options(char *options
, int *baud
, int *parity
, int *bits
, int *flow
)
1770 *baud
= simple_strtoul(s
, NULL
, 10);
1771 while (*s
>= '0' && *s
<= '9')
1786 static const struct baud_rates baud_rates
[] = {
1787 { 921600, B921600
},
1788 { 460800, B460800
},
1789 { 230400, B230400
},
1790 { 115200, B115200
},
1802 * uart_set_options - setup the serial console parameters
1803 * @port: pointer to the serial ports uart_port structure
1804 * @co: console pointer
1806 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1807 * @bits: number of data bits
1808 * @flow: flow control character - 'r' (rts)
1811 uart_set_options(struct uart_port
*port
, struct console
*co
,
1812 int baud
, int parity
, int bits
, int flow
)
1814 struct termios termios
;
1818 * Ensure that the serial console lock is initialised
1821 spin_lock_init(&port
->lock
);
1823 memset(&termios
, 0, sizeof(struct termios
));
1825 termios
.c_cflag
= CREAD
| HUPCL
| CLOCAL
;
1828 * Construct a cflag setting.
1830 for (i
= 0; baud_rates
[i
].rate
; i
++)
1831 if (baud_rates
[i
].rate
<= baud
)
1834 termios
.c_cflag
|= baud_rates
[i
].cflag
;
1837 termios
.c_cflag
|= CS7
;
1839 termios
.c_cflag
|= CS8
;
1843 termios
.c_cflag
|= PARODD
;
1846 termios
.c_cflag
|= PARENB
;
1851 termios
.c_cflag
|= CRTSCTS
;
1853 port
->ops
->set_termios(port
, &termios
, NULL
);
1854 co
->cflag
= termios
.c_cflag
;
1858 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1860 static void uart_change_pm(struct uart_state
*state
, int pm_state
)
1862 struct uart_port
*port
= state
->port
;
1864 port
->ops
->pm(port
, pm_state
, state
->pm_state
);
1865 state
->pm_state
= pm_state
;
1868 int uart_suspend_port(struct uart_driver
*drv
, struct uart_port
*port
)
1870 struct uart_state
*state
= drv
->state
+ port
->line
;
1872 mutex_lock(&state
->mutex
);
1874 if (state
->info
&& state
->info
->flags
& UIF_INITIALIZED
) {
1875 const struct uart_ops
*ops
= port
->ops
;
1877 spin_lock_irq(&port
->lock
);
1879 ops
->set_mctrl(port
, 0);
1881 spin_unlock_irq(&port
->lock
);
1884 * Wait for the transmitter to empty.
1886 while (!ops
->tx_empty(port
)) {
1890 ops
->shutdown(port
);
1894 * Disable the console device before suspending.
1896 if (uart_console(port
))
1897 console_stop(port
->cons
);
1899 uart_change_pm(state
, 3);
1901 mutex_unlock(&state
->mutex
);
1906 int uart_resume_port(struct uart_driver
*drv
, struct uart_port
*port
)
1908 struct uart_state
*state
= drv
->state
+ port
->line
;
1910 mutex_lock(&state
->mutex
);
1912 uart_change_pm(state
, 0);
1915 * Re-enable the console device after suspending.
1917 if (uart_console(port
)) {
1918 struct termios termios
;
1921 * First try to use the console cflag setting.
1923 memset(&termios
, 0, sizeof(struct termios
));
1924 termios
.c_cflag
= port
->cons
->cflag
;
1927 * If that's unset, use the tty termios setting.
1929 if (state
->info
&& state
->info
->tty
&& termios
.c_cflag
== 0)
1930 termios
= *state
->info
->tty
->termios
;
1932 port
->ops
->set_termios(port
, &termios
, NULL
);
1933 console_start(port
->cons
);
1936 if (state
->info
&& state
->info
->flags
& UIF_INITIALIZED
) {
1937 const struct uart_ops
*ops
= port
->ops
;
1940 ops
->set_mctrl(port
, 0);
1941 ret
= ops
->startup(port
);
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
);
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.
1954 state
->info
->flags
&= ~UIF_INITIALIZED
;
1955 uart_shutdown(state
);
1959 mutex_unlock(&state
->mutex
);
1965 uart_report_port(struct uart_driver
*drv
, struct uart_port
*port
)
1969 switch (port
->iotype
) {
1971 snprintf(address
, sizeof(address
),
1972 "I/O 0x%x", port
->iobase
);
1975 snprintf(address
, sizeof(address
),
1976 "I/O 0x%x offset 0x%x", port
->iobase
, port
->hub6
);
1981 snprintf(address
, sizeof(address
),
1982 "MMIO 0x%lx", port
->mapbase
);
1985 strlcpy(address
, "*unknown*", sizeof(address
));
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
? ": " : "",
1992 drv
->dev_name
, port
->line
, address
, port
->irq
, uart_type(port
));
1996 uart_configure_port(struct uart_driver
*drv
, struct uart_state
*state
,
1997 struct uart_port
*port
)
2002 * If there isn't a port here, don't do anything further.
2004 if (!port
->iobase
&& !port
->mapbase
&& !port
->membase
)
2008 * Now do the auto configuration stuff. Note that config_port
2009 * is expected to claim the resources and map the port for us.
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
);
2019 if (port
->type
!= PORT_UNKNOWN
) {
2020 unsigned long flags
;
2022 uart_report_port(drv
, port
);
2025 * Ensure that the modem control lines are de-activated.
2026 * We probably don't need a spinlock around this, but
2028 spin_lock_irqsave(&port
->lock
, flags
);
2029 port
->ops
->set_mctrl(port
, 0);
2030 spin_unlock_irqrestore(&port
->lock
, flags
);
2033 * Power down all ports by default, except the
2034 * console if we have one.
2036 if (!uart_console(port
))
2037 uart_change_pm(state
, 3);
2042 * This reverses the effects of uart_configure_port, hanging up the
2043 * port before removal.
2046 uart_unconfigure_port(struct uart_driver
*drv
, struct uart_state
*state
)
2048 struct uart_port
*port
= state
->port
;
2049 struct uart_info
*info
= state
->info
;
2051 if (info
&& info
->tty
)
2052 tty_vhangup(info
->tty
);
2054 mutex_lock(&state
->mutex
);
2059 * Free the port IO and memory resources, if any.
2061 if (port
->type
!= PORT_UNKNOWN
)
2062 port
->ops
->release_port(port
);
2065 * Indicate that there isn't a port here anymore.
2067 port
->type
= PORT_UNKNOWN
;
2070 * Kill the tasklet, and free resources.
2073 tasklet_kill(&info
->tlet
);
2077 mutex_unlock(&state
->mutex
);
2080 static struct tty_operations uart_ops
= {
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
,
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
,
2102 .tiocmget
= uart_tiocmget
,
2103 .tiocmset
= uart_tiocmset
,
2107 * uart_register_driver - register a driver with the uart core layer
2108 * @drv: low level driver structure
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.
2113 * We have a proc file in /proc/tty/driver which is named after the
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.
2119 int uart_register_driver(struct uart_driver
*drv
)
2121 struct tty_driver
*normal
= NULL
;
2127 * Maybe we should be using a slab cache for this, especially if
2128 * we have a large number of ports to handle.
2130 drv
->state
= kmalloc(sizeof(struct uart_state
) * drv
->nr
, GFP_KERNEL
);
2135 memset(drv
->state
, 0, sizeof(struct uart_state
) * drv
->nr
);
2137 normal
= alloc_tty_driver(drv
->nr
);
2141 drv
->tty_driver
= normal
;
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
);
2158 * Initialise the UART state(s).
2160 for (i
= 0; i
< drv
->nr
; i
++) {
2161 struct uart_state
*state
= drv
->state
+ i
;
2163 state
->close_delay
= 500; /* .5 seconds */
2164 state
->closing_wait
= 30000; /* 30 seconds */
2166 mutex_init(&state
->mutex
);
2169 retval
= tty_register_driver(normal
);
2172 put_tty_driver(normal
);
2179 * uart_unregister_driver - remove a driver from the uart core layer
2180 * @drv: low level driver structure
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)
2187 void uart_unregister_driver(struct uart_driver
*drv
)
2189 struct tty_driver
*p
= drv
->tty_driver
;
2190 tty_unregister_driver(p
);
2193 drv
->tty_driver
= NULL
;
2196 struct tty_driver
*uart_console_device(struct console
*co
, int *index
)
2198 struct uart_driver
*p
= co
->data
;
2200 return p
->tty_driver
;
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.
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.
2213 int uart_add_one_port(struct uart_driver
*drv
, struct uart_port
*port
)
2215 struct uart_state
*state
;
2218 BUG_ON(in_interrupt());
2220 if (port
->line
>= drv
->nr
)
2223 state
= drv
->state
+ port
->line
;
2225 mutex_lock(&port_mutex
);
2233 port
->cons
= drv
->cons
;
2234 port
->info
= state
->info
;
2237 * If this port is a console, then the spinlock is already
2240 if (!(uart_console(port
) && (port
->cons
->flags
& CON_ENABLED
)))
2241 spin_lock_init(&port
->lock
);
2243 uart_configure_port(drv
, state
, port
);
2246 * Register the port whether it's detected or not. This allows
2247 * setserial to be used to alter this ports parameters.
2249 tty_register_device(drv
->tty_driver
, port
->line
, port
->dev
);
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.
2256 if (port
->type
!= PORT_UNKNOWN
&&
2257 port
->cons
&& !(port
->cons
->flags
& CON_ENABLED
))
2258 register_console(port
->cons
);
2261 mutex_unlock(&port_mutex
);
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
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
2275 int uart_remove_one_port(struct uart_driver
*drv
, struct uart_port
*port
)
2277 struct uart_state
*state
= drv
->state
+ port
->line
;
2279 BUG_ON(in_interrupt());
2281 if (state
->port
!= port
)
2282 printk(KERN_ALERT
"Removing wrong port: %p != %p\n",
2285 mutex_lock(&port_mutex
);
2288 * Remove the devices from devfs
2290 tty_unregister_device(drv
->tty_driver
, port
->line
);
2292 uart_unconfigure_port(drv
, state
);
2294 mutex_unlock(&port_mutex
);
2300 * Are the two ports equivalent?
2302 int uart_match_port(struct uart_port
*port1
, struct uart_port
*port2
)
2304 if (port1
->iotype
!= port2
->iotype
)
2307 switch (port1
->iotype
) {
2309 return (port1
->iobase
== port2
->iobase
);
2311 return (port1
->iobase
== port2
->iobase
) &&
2312 (port1
->hub6
== port2
->hub6
);
2314 return (port1
->mapbase
== port2
->mapbase
);
2318 EXPORT_SYMBOL(uart_match_port
);
2320 EXPORT_SYMBOL(uart_write_wakeup
);
2321 EXPORT_SYMBOL(uart_register_driver
);
2322 EXPORT_SYMBOL(uart_unregister_driver
);
2323 EXPORT_SYMBOL(uart_suspend_port
);
2324 EXPORT_SYMBOL(uart_resume_port
);
2325 EXPORT_SYMBOL(uart_add_one_port
);
2326 EXPORT_SYMBOL(uart_remove_one_port
);
2328 MODULE_DESCRIPTION("Serial driver core");
2329 MODULE_LICENSE("GPL");