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 */
37 #include <asm/uaccess.h>
41 #define DPRINTK(x...) printk(x)
43 #define DPRINTK(x...) do { } while (0)
47 * This is used to lock changes in serial line configuration.
49 static DECLARE_MUTEX(port_sem
);
51 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
53 #define uart_users(state) ((state)->count + ((state)->info ? (state)->info->blocked_open : 0))
55 #ifdef CONFIG_SERIAL_CORE_CONSOLE
56 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
58 #define uart_console(port) (0)
61 static void uart_change_speed(struct uart_state
*state
, struct termios
*old_termios
);
62 static void uart_wait_until_sent(struct tty_struct
*tty
, int timeout
);
63 static void uart_change_pm(struct uart_state
*state
, int pm_state
);
66 * This routine is used by the interrupt handler to schedule processing in
67 * the software interrupt portion of the driver.
69 void uart_write_wakeup(struct uart_port
*port
)
71 struct uart_info
*info
= port
->info
;
72 tasklet_schedule(&info
->tlet
);
75 static void uart_stop(struct tty_struct
*tty
)
77 struct uart_state
*state
= tty
->driver_data
;
78 struct uart_port
*port
= state
->port
;
81 spin_lock_irqsave(&port
->lock
, flags
);
82 port
->ops
->stop_tx(port
, 1);
83 spin_unlock_irqrestore(&port
->lock
, flags
);
86 static void __uart_start(struct tty_struct
*tty
)
88 struct uart_state
*state
= tty
->driver_data
;
89 struct uart_port
*port
= state
->port
;
91 if (!uart_circ_empty(&state
->info
->xmit
) && state
->info
->xmit
.buf
&&
92 !tty
->stopped
&& !tty
->hw_stopped
)
93 port
->ops
->start_tx(port
, 1);
96 static void uart_start(struct tty_struct
*tty
)
98 struct uart_state
*state
= tty
->driver_data
;
99 struct uart_port
*port
= state
->port
;
102 spin_lock_irqsave(&port
->lock
, flags
);
104 spin_unlock_irqrestore(&port
->lock
, flags
);
107 static void uart_tasklet_action(unsigned long data
)
109 struct uart_state
*state
= (struct uart_state
*)data
;
110 struct tty_struct
*tty
;
112 tty
= state
->info
->tty
;
114 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
115 tty
->ldisc
.write_wakeup
)
116 tty
->ldisc
.write_wakeup(tty
);
117 wake_up_interruptible(&tty
->write_wait
);
122 uart_update_mctrl(struct uart_port
*port
, unsigned int set
, unsigned int clear
)
127 spin_lock_irqsave(&port
->lock
, flags
);
129 port
->mctrl
= (old
& ~clear
) | set
;
130 if (old
!= port
->mctrl
)
131 port
->ops
->set_mctrl(port
, port
->mctrl
);
132 spin_unlock_irqrestore(&port
->lock
, flags
);
135 #define uart_set_mctrl(port,set) uart_update_mctrl(port,set,0)
136 #define uart_clear_mctrl(port,clear) uart_update_mctrl(port,0,clear)
139 * Startup the port. This will be called once per open. All calls
140 * will be serialised by the per-port semaphore.
142 static int uart_startup(struct uart_state
*state
, int init_hw
)
144 struct uart_info
*info
= state
->info
;
145 struct uart_port
*port
= state
->port
;
149 if (info
->flags
& UIF_INITIALIZED
)
153 * Set the TTY IO error marker - we will only clear this
154 * once we have successfully opened the port. Also set
155 * up the tty->alt_speed kludge
158 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
160 if (port
->type
== PORT_UNKNOWN
)
164 * Initialise and allocate the transmit and temporary
167 if (!info
->xmit
.buf
) {
168 page
= get_zeroed_page(GFP_KERNEL
);
172 info
->xmit
.buf
= (unsigned char *) page
;
173 info
->tmpbuf
= info
->xmit
.buf
+ UART_XMIT_SIZE
;
174 init_MUTEX(&info
->tmpbuf_sem
);
175 uart_circ_clear(&info
->xmit
);
180 retval
= port
->ops
->startup(port
);
184 * Initialise the hardware port settings.
186 uart_change_speed(state
, NULL
);
189 * Setup the RTS and DTR signals once the
190 * port is open and ready to respond.
192 if (info
->tty
->termios
->c_cflag
& CBAUD
)
193 uart_set_mctrl(port
, TIOCM_RTS
| TIOCM_DTR
);
196 info
->flags
|= UIF_INITIALIZED
;
198 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
201 if (retval
&& capable(CAP_SYS_ADMIN
))
208 * This routine will shutdown a serial port; interrupts are disabled, and
209 * DTR is dropped if the hangup on close termio flag is on. Calls to
210 * uart_shutdown are serialised by the per-port semaphore.
212 static void uart_shutdown(struct uart_state
*state
)
214 struct uart_info
*info
= state
->info
;
215 struct uart_port
*port
= state
->port
;
217 if (!(info
->flags
& UIF_INITIALIZED
))
221 * Turn off DTR and RTS early.
223 if (!info
->tty
|| (info
->tty
->termios
->c_cflag
& HUPCL
))
224 uart_clear_mctrl(port
, TIOCM_DTR
| TIOCM_RTS
);
227 * clear delta_msr_wait queue to avoid mem leaks: we may free
228 * the irq here so the queue might never be woken up. Note
229 * that we won't end up waiting on delta_msr_wait again since
230 * any outstanding file descriptors should be pointing at
231 * hung_up_tty_fops now.
233 wake_up_interruptible(&info
->delta_msr_wait
);
236 * Free the IRQ and disable the port.
238 port
->ops
->shutdown(port
);
241 * Ensure that the IRQ handler isn't running on another CPU.
243 synchronize_irq(port
->irq
);
246 * Free the transmit buffer page.
248 if (info
->xmit
.buf
) {
249 free_page((unsigned long)info
->xmit
.buf
);
250 info
->xmit
.buf
= NULL
;
255 * kill off our tasklet
257 tasklet_kill(&info
->tlet
);
259 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
261 info
->flags
&= ~UIF_INITIALIZED
;
265 * uart_update_timeout - update per-port FIFO timeout.
266 * @port: uart_port structure describing the port.
267 * @cflag: termios cflag value
268 * @quot: uart clock divisor quotient
270 * Set the port FIFO timeout value. The @cflag value should
271 * reflect the actual hardware settings.
274 uart_update_timeout(struct uart_port
*port
, unsigned int cflag
,
279 /* byte size and parity */
280 switch (cflag
& CSIZE
) {
301 * The total number of bits to be transmitted in the fifo.
303 bits
= bits
* port
->fifosize
;
306 * Figure the timeout to send the above number of bits.
307 * Add .02 seconds of slop
309 port
->timeout
= (HZ
* bits
) / baud
+ HZ
/50;
312 EXPORT_SYMBOL(uart_update_timeout
);
315 * uart_get_baud_rate - return baud rate for a particular port
316 * @port: uart_port structure describing the port in question.
317 * @termios: desired termios settings.
318 * @old: old termios (or NULL)
319 * @min: minimum acceptable baud rate
320 * @max: maximum acceptable baud rate
322 * Decode the termios structure into a numeric baud rate,
323 * taking account of the magic 38400 baud rate (with spd_*
324 * flags), and mapping the %B0 rate to 9600 baud.
326 * If the new baud rate is invalid, try the old termios setting.
327 * If it's still invalid, we try 9600 baud.
329 * Update the @termios structure to reflect the baud rate
330 * we're actually going to be using.
333 uart_get_baud_rate(struct uart_port
*port
, struct termios
*termios
,
334 struct termios
*old
, unsigned int min
, unsigned int max
)
336 unsigned int try, baud
, altbaud
= 38400;
337 unsigned int flags
= port
->flags
& UPF_SPD_MASK
;
339 if (flags
== UPF_SPD_HI
)
341 if (flags
== UPF_SPD_VHI
)
343 if (flags
== UPF_SPD_SHI
)
345 if (flags
== UPF_SPD_WARP
)
348 for (try = 0; try < 2; try++) {
349 baud
= tty_termios_baud_rate(termios
);
352 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
359 * Special case: B0 rate.
364 if (baud
>= min
&& baud
<= max
)
368 * Oops, the quotient was zero. Try again with
369 * the old baud rate if possible.
371 termios
->c_cflag
&= ~CBAUD
;
373 termios
->c_cflag
|= old
->c_cflag
& CBAUD
;
379 * As a last resort, if the quotient is zero,
380 * default to 9600 bps
382 termios
->c_cflag
|= B9600
;
388 EXPORT_SYMBOL(uart_get_baud_rate
);
391 * uart_get_divisor - return uart clock divisor
392 * @port: uart_port structure describing the port.
393 * @baud: desired baud rate
395 * Calculate the uart clock divisor for the port.
398 uart_get_divisor(struct uart_port
*port
, unsigned int baud
)
403 * Old custom speed handling.
405 if (baud
== 38400 && (port
->flags
& UPF_SPD_MASK
) == UPF_SPD_CUST
)
406 quot
= port
->custom_divisor
;
408 quot
= port
->uartclk
/ (16 * baud
);
413 EXPORT_SYMBOL(uart_get_divisor
);
416 uart_change_speed(struct uart_state
*state
, struct termios
*old_termios
)
418 struct tty_struct
*tty
= state
->info
->tty
;
419 struct uart_port
*port
= state
->port
;
420 struct termios
*termios
;
423 * If we have no tty, termios, or the port does not exist,
424 * then we can't set the parameters for this port.
426 if (!tty
|| !tty
->termios
|| port
->type
== PORT_UNKNOWN
)
429 termios
= tty
->termios
;
432 * Set flags based on termios cflag
434 if (termios
->c_cflag
& CRTSCTS
)
435 state
->info
->flags
|= UIF_CTS_FLOW
;
437 state
->info
->flags
&= ~UIF_CTS_FLOW
;
439 if (termios
->c_cflag
& CLOCAL
)
440 state
->info
->flags
&= ~UIF_CHECK_CD
;
442 state
->info
->flags
|= UIF_CHECK_CD
;
444 port
->ops
->set_termios(port
, termios
, old_termios
);
448 __uart_put_char(struct uart_port
*port
, struct circ_buf
*circ
, unsigned char c
)
455 spin_lock_irqsave(&port
->lock
, flags
);
456 if (uart_circ_chars_free(circ
) != 0) {
457 circ
->buf
[circ
->head
] = c
;
458 circ
->head
= (circ
->head
+ 1) & (UART_XMIT_SIZE
- 1);
460 spin_unlock_irqrestore(&port
->lock
, flags
);
464 __uart_user_write(struct uart_port
*port
, struct circ_buf
*circ
,
465 const unsigned char *buf
, int count
)
470 if (down_interruptible(&port
->info
->tmpbuf_sem
))
475 c
= CIRC_SPACE_TO_END(circ
->head
, circ
->tail
, UART_XMIT_SIZE
);
481 c
-= copy_from_user(port
->info
->tmpbuf
, buf
, c
);
487 spin_lock_irqsave(&port
->lock
, flags
);
488 c1
= CIRC_SPACE_TO_END(circ
->head
, circ
->tail
, UART_XMIT_SIZE
);
491 memcpy(circ
->buf
+ circ
->head
, port
->info
->tmpbuf
, c
);
492 circ
->head
= (circ
->head
+ c
) & (UART_XMIT_SIZE
- 1);
493 spin_unlock_irqrestore(&port
->lock
, flags
);
498 up(&port
->info
->tmpbuf_sem
);
504 __uart_kern_write(struct uart_port
*port
, struct circ_buf
*circ
,
505 const unsigned char *buf
, int count
)
510 spin_lock_irqsave(&port
->lock
, flags
);
512 c
= CIRC_SPACE_TO_END(circ
->head
, circ
->tail
, UART_XMIT_SIZE
);
517 memcpy(circ
->buf
+ circ
->head
, buf
, c
);
518 circ
->head
= (circ
->head
+ c
) & (UART_XMIT_SIZE
- 1);
523 spin_unlock_irqrestore(&port
->lock
, flags
);
528 static void uart_put_char(struct tty_struct
*tty
, unsigned char ch
)
530 struct uart_state
*state
= tty
->driver_data
;
533 __uart_put_char(state
->port
, &state
->info
->xmit
, ch
);
536 static void uart_flush_chars(struct tty_struct
*tty
)
542 uart_write(struct tty_struct
*tty
, int from_user
, const unsigned char * buf
,
545 struct uart_state
*state
= tty
->driver_data
;
548 if (!tty
|| !state
->info
->xmit
.buf
)
552 ret
= __uart_user_write(state
->port
, &state
->info
->xmit
, buf
, count
);
554 ret
= __uart_kern_write(state
->port
, &state
->info
->xmit
, buf
, count
);
560 static int uart_write_room(struct tty_struct
*tty
)
562 struct uart_state
*state
= tty
->driver_data
;
564 return uart_circ_chars_free(&state
->info
->xmit
);
567 static int uart_chars_in_buffer(struct tty_struct
*tty
)
569 struct uart_state
*state
= tty
->driver_data
;
571 return uart_circ_chars_pending(&state
->info
->xmit
);
574 static void uart_flush_buffer(struct tty_struct
*tty
)
576 struct uart_state
*state
= tty
->driver_data
;
577 struct uart_port
*port
= state
->port
;
580 DPRINTK("uart_flush_buffer(%d) called\n", tty
->index
);
582 spin_lock_irqsave(&port
->lock
, flags
);
583 uart_circ_clear(&state
->info
->xmit
);
584 spin_unlock_irqrestore(&port
->lock
, flags
);
585 wake_up_interruptible(&tty
->write_wait
);
586 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
587 tty
->ldisc
.write_wakeup
)
588 (tty
->ldisc
.write_wakeup
)(tty
);
592 * This function is used to send a high-priority XON/XOFF character to
595 static void uart_send_xchar(struct tty_struct
*tty
, char ch
)
597 struct uart_state
*state
= tty
->driver_data
;
598 struct uart_port
*port
= state
->port
;
601 if (port
->ops
->send_xchar
)
602 port
->ops
->send_xchar(port
, ch
);
606 spin_lock_irqsave(&port
->lock
, flags
);
607 port
->ops
->start_tx(port
, 0);
608 spin_unlock_irqrestore(&port
->lock
, flags
);
613 static void uart_throttle(struct tty_struct
*tty
)
615 struct uart_state
*state
= tty
->driver_data
;
618 uart_send_xchar(tty
, STOP_CHAR(tty
));
620 if (tty
->termios
->c_cflag
& CRTSCTS
)
621 uart_clear_mctrl(state
->port
, TIOCM_RTS
);
624 static void uart_unthrottle(struct tty_struct
*tty
)
626 struct uart_state
*state
= tty
->driver_data
;
627 struct uart_port
*port
= state
->port
;
633 uart_send_xchar(tty
, START_CHAR(tty
));
636 if (tty
->termios
->c_cflag
& CRTSCTS
)
637 uart_set_mctrl(port
, TIOCM_RTS
);
640 static int uart_get_info(struct uart_state
*state
, struct serial_struct
*retinfo
)
642 struct uart_port
*port
= state
->port
;
643 struct serial_struct tmp
;
645 memset(&tmp
, 0, sizeof(tmp
));
646 tmp
.type
= port
->type
;
647 tmp
.line
= port
->line
;
648 tmp
.port
= port
->iobase
;
649 if (HIGH_BITS_OFFSET
)
650 tmp
.port_high
= (long) port
->iobase
>> HIGH_BITS_OFFSET
;
652 tmp
.flags
= port
->flags
;
653 tmp
.xmit_fifo_size
= port
->fifosize
;
654 tmp
.baud_base
= port
->uartclk
/ 16;
655 tmp
.close_delay
= state
->close_delay
;
656 tmp
.closing_wait
= state
->closing_wait
;
657 tmp
.custom_divisor
= port
->custom_divisor
;
658 tmp
.hub6
= port
->hub6
;
659 tmp
.io_type
= port
->iotype
;
660 tmp
.iomem_reg_shift
= port
->regshift
;
661 tmp
.iomem_base
= (void *)port
->mapbase
;
663 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
669 uart_set_info(struct uart_state
*state
, struct serial_struct
*newinfo
)
671 struct serial_struct new_serial
;
672 struct uart_port
*port
= state
->port
;
673 unsigned long new_port
;
674 unsigned int change_irq
, change_port
, old_flags
;
675 unsigned int old_custom_divisor
;
678 if (copy_from_user(&new_serial
, newinfo
, sizeof(new_serial
)))
681 new_port
= new_serial
.port
;
682 if (HIGH_BITS_OFFSET
)
683 new_port
+= (unsigned long) new_serial
.port_high
<< HIGH_BITS_OFFSET
;
685 new_serial
.irq
= irq_canonicalize(new_serial
.irq
);
688 * This semaphore protects state->count. It is also
689 * very useful to prevent opens. Also, take the
690 * port configuration semaphore to make sure that a
691 * module insertion/removal doesn't change anything
696 change_irq
= new_serial
.irq
!= port
->irq
;
699 * Since changing the 'type' of the port changes its resource
700 * allocations, we should treat type changes the same as
703 change_port
= new_port
!= port
->iobase
||
704 (unsigned long)new_serial
.iomem_base
!= port
->mapbase
||
705 new_serial
.hub6
!= port
->hub6
||
706 new_serial
.io_type
!= port
->iotype
||
707 new_serial
.iomem_reg_shift
!= port
->regshift
||
708 new_serial
.type
!= port
->type
;
710 old_flags
= port
->flags
;
711 old_custom_divisor
= port
->custom_divisor
;
713 if (!capable(CAP_SYS_ADMIN
)) {
715 if (change_irq
|| change_port
||
716 (new_serial
.baud_base
!= port
->uartclk
/ 16) ||
717 (new_serial
.close_delay
!= state
->close_delay
) ||
718 (new_serial
.closing_wait
!= state
->closing_wait
) ||
719 (new_serial
.xmit_fifo_size
!= port
->fifosize
) ||
720 (((new_serial
.flags
^ old_flags
) & ~UPF_USR_MASK
) != 0))
722 port
->flags
= ((port
->flags
& ~UPF_USR_MASK
) |
723 (new_serial
.flags
& UPF_USR_MASK
));
724 port
->custom_divisor
= new_serial
.custom_divisor
;
729 * Ask the low level driver to verify the settings.
731 if (port
->ops
->verify_port
)
732 retval
= port
->ops
->verify_port(port
, &new_serial
);
734 if ((new_serial
.irq
>= NR_IRQS
) || (new_serial
.irq
< 0) ||
735 (new_serial
.baud_base
< 9600))
741 if (change_port
|| change_irq
) {
745 * Make sure that we are the sole user of this port.
747 if (uart_users(state
) > 1)
751 * We need to shutdown the serial port at the old
752 * port/type/irq combination.
754 uart_shutdown(state
);
758 unsigned long old_iobase
, old_mapbase
;
759 unsigned int old_type
, old_iotype
, old_hub6
, old_shift
;
761 old_iobase
= port
->iobase
;
762 old_mapbase
= port
->mapbase
;
763 old_type
= port
->type
;
764 old_hub6
= port
->hub6
;
765 old_iotype
= port
->iotype
;
766 old_shift
= port
->regshift
;
769 * Free and release old regions
771 if (old_type
!= PORT_UNKNOWN
)
772 port
->ops
->release_port(port
);
774 port
->iobase
= new_port
;
775 port
->type
= new_serial
.type
;
776 port
->hub6
= new_serial
.hub6
;
777 port
->iotype
= new_serial
.io_type
;
778 port
->regshift
= new_serial
.iomem_reg_shift
;
779 port
->mapbase
= (unsigned long)new_serial
.iomem_base
;
782 * Claim and map the new regions
784 if (port
->type
!= PORT_UNKNOWN
) {
785 retval
= port
->ops
->request_port(port
);
787 /* Always success - Jean II */
792 * If we fail to request resources for the
793 * new port, try to restore the old settings.
795 if (retval
&& old_type
!= PORT_UNKNOWN
) {
796 port
->iobase
= old_iobase
;
797 port
->type
= old_type
;
798 port
->hub6
= old_hub6
;
799 port
->iotype
= old_iotype
;
800 port
->regshift
= old_shift
;
801 port
->mapbase
= old_mapbase
;
802 retval
= port
->ops
->request_port(port
);
804 * If we failed to restore the old settings,
808 port
->type
= PORT_UNKNOWN
;
817 port
->irq
= new_serial
.irq
;
818 port
->uartclk
= new_serial
.baud_base
* 16;
819 port
->flags
= (port
->flags
& ~UPF_CHANGE_MASK
) |
820 (new_serial
.flags
& UPF_CHANGE_MASK
);
821 port
->custom_divisor
= new_serial
.custom_divisor
;
822 state
->close_delay
= new_serial
.close_delay
* HZ
/ 100;
823 state
->closing_wait
= new_serial
.closing_wait
* HZ
/ 100;
824 port
->fifosize
= new_serial
.xmit_fifo_size
;
825 if (state
->info
->tty
)
826 state
->info
->tty
->low_latency
=
827 (port
->flags
& UPF_LOW_LATENCY
) ? 1 : 0;
831 if (port
->type
== PORT_UNKNOWN
)
833 if (state
->info
->flags
& UIF_INITIALIZED
) {
834 if (((old_flags
^ port
->flags
) & UPF_SPD_MASK
) ||
835 old_custom_divisor
!= port
->custom_divisor
) {
836 /* If they're setting up a custom divisor or speed,
837 * instead of clearing it, then bitch about it. No
838 * need to rate-limit; it's CAP_SYS_ADMIN only. */
839 if (port
->flags
& UPF_SPD_MASK
) {
840 printk(KERN_NOTICE
"%s sets custom speed on %s%d. This is deprecated.\n",
841 current
->comm
, state
->info
->tty
->driver
->name
,
844 uart_change_speed(state
, NULL
);
847 retval
= uart_startup(state
, 1);
855 * uart_get_lsr_info - get line status register info.
856 * Note: uart_ioctl protects us against hangups.
858 static int uart_get_lsr_info(struct uart_state
*state
, unsigned int *value
)
860 struct uart_port
*port
= state
->port
;
863 result
= port
->ops
->tx_empty(port
);
866 * If we're about to load something into the transmit
867 * register, we'll pretend the transmitter isn't empty to
868 * avoid a race condition (depending on when the transmit
869 * interrupt happens).
872 ((uart_circ_chars_pending(&state
->info
->xmit
) > 0) &&
873 !state
->info
->tty
->stopped
&& !state
->info
->tty
->hw_stopped
))
874 result
&= ~TIOCSER_TEMT
;
876 return put_user(result
, value
);
879 static int uart_tiocmget(struct tty_struct
*tty
, struct file
*file
)
881 struct uart_state
*state
= tty
->driver_data
;
882 struct uart_port
*port
= state
->port
;
886 if ((!file
|| !tty_hung_up_p(file
)) &&
887 !(tty
->flags
& (1 << TTY_IO_ERROR
))) {
888 result
= port
->mctrl
;
889 result
|= port
->ops
->get_mctrl(port
);
897 uart_tiocmset(struct tty_struct
*tty
, struct file
*file
,
898 unsigned int set
, unsigned int clear
)
900 struct uart_state
*state
= tty
->driver_data
;
901 struct uart_port
*port
= state
->port
;
905 if ((!file
|| !tty_hung_up_p(file
)) &&
906 !(tty
->flags
& (1 << TTY_IO_ERROR
))) {
907 uart_update_mctrl(port
, set
, clear
);
914 static void uart_break_ctl(struct tty_struct
*tty
, int break_state
)
916 struct uart_state
*state
= tty
->driver_data
;
917 struct uart_port
*port
= state
->port
;
919 BUG_ON(!kernel_locked());
923 if (port
->type
!= PORT_UNKNOWN
)
924 port
->ops
->break_ctl(port
, break_state
);
929 static int uart_do_autoconfig(struct uart_state
*state
)
931 struct uart_port
*port
= state
->port
;
934 if (!capable(CAP_SYS_ADMIN
))
938 * Take the per-port semaphore. This prevents count from
939 * changing, and hence any extra opens of the port while
940 * we're auto-configuring.
942 if (down_interruptible(&state
->sem
))
946 if (uart_users(state
) == 1) {
947 uart_shutdown(state
);
950 * If we already have a port type configured,
951 * we must release its resources.
953 if (port
->type
!= PORT_UNKNOWN
)
954 port
->ops
->release_port(port
);
956 flags
= UART_CONFIG_TYPE
;
957 if (port
->flags
& UPF_AUTO_IRQ
)
958 flags
|= UART_CONFIG_IRQ
;
961 * This will claim the ports resources if
964 port
->ops
->config_port(port
, flags
);
966 ret
= uart_startup(state
, 1);
973 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
974 * - mask passed in arg for lines of interest
975 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
976 * Caller should use TIOCGICOUNT to see which one it was
979 uart_wait_modem_status(struct uart_state
*state
, unsigned long arg
)
981 struct uart_port
*port
= state
->port
;
982 DECLARE_WAITQUEUE(wait
, current
);
983 struct uart_icount cprev
, cnow
;
987 * note the counters on entry
989 spin_lock_irq(&port
->lock
);
990 memcpy(&cprev
, &port
->icount
, sizeof(struct uart_icount
));
993 * Force modem status interrupts on
995 port
->ops
->enable_ms(port
);
996 spin_unlock_irq(&port
->lock
);
998 add_wait_queue(&state
->info
->delta_msr_wait
, &wait
);
1000 spin_lock_irq(&port
->lock
);
1001 memcpy(&cnow
, &port
->icount
, sizeof(struct uart_icount
));
1002 spin_unlock_irq(&port
->lock
);
1004 set_current_state(TASK_INTERRUPTIBLE
);
1006 if (((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
1007 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
1008 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
1009 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
))) {
1016 /* see if a signal did it */
1017 if (signal_pending(current
)) {
1025 current
->state
= TASK_RUNNING
;
1026 remove_wait_queue(&state
->info
->delta_msr_wait
, &wait
);
1032 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1033 * Return: write counters to the user passed counter struct
1034 * NB: both 1->0 and 0->1 transitions are counted except for
1035 * RI where only 0->1 is counted.
1038 uart_get_count(struct uart_state
*state
, struct serial_icounter_struct
*icnt
)
1040 struct serial_icounter_struct icount
;
1041 struct uart_icount cnow
;
1042 struct uart_port
*port
= state
->port
;
1044 spin_lock_irq(&port
->lock
);
1045 memcpy(&cnow
, &port
->icount
, sizeof(struct uart_icount
));
1046 spin_unlock_irq(&port
->lock
);
1048 icount
.cts
= cnow
.cts
;
1049 icount
.dsr
= cnow
.dsr
;
1050 icount
.rng
= cnow
.rng
;
1051 icount
.dcd
= cnow
.dcd
;
1052 icount
.rx
= cnow
.rx
;
1053 icount
.tx
= cnow
.tx
;
1054 icount
.frame
= cnow
.frame
;
1055 icount
.overrun
= cnow
.overrun
;
1056 icount
.parity
= cnow
.parity
;
1057 icount
.brk
= cnow
.brk
;
1058 icount
.buf_overrun
= cnow
.buf_overrun
;
1060 return copy_to_user(icnt
, &icount
, sizeof(icount
)) ? -EFAULT
: 0;
1064 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
1067 uart_ioctl(struct tty_struct
*tty
, struct file
*filp
, unsigned int cmd
,
1070 struct uart_state
*state
= tty
->driver_data
;
1071 int ret
= -ENOIOCTLCMD
;
1073 BUG_ON(!kernel_locked());
1076 * These ioctls don't rely on the hardware to be present.
1080 ret
= uart_get_info(state
, (struct serial_struct
*)arg
);
1084 ret
= uart_set_info(state
, (struct serial_struct
*)arg
);
1088 ret
= uart_do_autoconfig(state
);
1091 case TIOCSERGWILD
: /* obsolete */
1092 case TIOCSERSWILD
: /* obsolete */
1097 if (ret
!= -ENOIOCTLCMD
)
1100 if (tty
->flags
& (1 << TTY_IO_ERROR
)) {
1106 * The following should only be used when hardware is present.
1110 ret
= uart_wait_modem_status(state
, arg
);
1114 ret
= uart_get_count(state
, (struct serial_icounter_struct
*)arg
);
1118 if (ret
!= -ENOIOCTLCMD
)
1123 if (tty_hung_up_p(filp
)) {
1129 * All these rely on hardware being present and need to be
1130 * protected against the tty being hung up.
1133 case TIOCSERGETLSR
: /* Get line status register */
1134 ret
= uart_get_lsr_info(state
, (unsigned int *)arg
);
1138 struct uart_port
*port
= state
->port
;
1139 if (port
->ops
->ioctl
)
1140 ret
= port
->ops
->ioctl(port
, cmd
, arg
);
1150 static void uart_set_termios(struct tty_struct
*tty
, struct termios
*old_termios
)
1152 struct uart_state
*state
= tty
->driver_data
;
1153 unsigned long flags
;
1154 unsigned int cflag
= tty
->termios
->c_cflag
;
1156 BUG_ON(!kernel_locked());
1159 * These are the bits that are used to setup various
1160 * flags in the low level driver.
1162 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1164 if ((cflag
^ old_termios
->c_cflag
) == 0 &&
1165 RELEVANT_IFLAG(tty
->termios
->c_iflag
^ old_termios
->c_iflag
) == 0)
1168 uart_change_speed(state
, old_termios
);
1170 /* Handle transition to B0 status */
1171 if ((old_termios
->c_cflag
& CBAUD
) && !(cflag
& CBAUD
))
1172 uart_clear_mctrl(state
->port
, TIOCM_RTS
| TIOCM_DTR
);
1174 /* Handle transition away from B0 status */
1175 if (!(old_termios
->c_cflag
& CBAUD
) && (cflag
& CBAUD
)) {
1176 unsigned int mask
= TIOCM_DTR
;
1177 if (!(cflag
& CRTSCTS
) ||
1178 !test_bit(TTY_THROTTLED
, &tty
->flags
))
1180 uart_set_mctrl(state
->port
, mask
);
1183 /* Handle turning off CRTSCTS */
1184 if ((old_termios
->c_cflag
& CRTSCTS
) && !(cflag
& CRTSCTS
)) {
1185 spin_lock_irqsave(&state
->port
->lock
, flags
);
1186 tty
->hw_stopped
= 0;
1188 spin_unlock_irqrestore(&state
->port
->lock
, flags
);
1193 * No need to wake up processes in open wait, since they
1194 * sample the CLOCAL flag once, and don't recheck it.
1195 * XXX It's not clear whether the current behavior is correct
1196 * or not. Hence, this may change.....
1198 if (!(old_termios
->c_cflag
& CLOCAL
) &&
1199 (tty
->termios
->c_cflag
& CLOCAL
))
1200 wake_up_interruptible(&state
->info
->open_wait
);
1205 * In 2.4.5, calls to this will be serialized via the BKL in
1206 * linux/drivers/char/tty_io.c:tty_release()
1207 * linux/drivers/char/tty_io.c:do_tty_handup()
1209 static void uart_close(struct tty_struct
*tty
, struct file
*filp
)
1211 struct uart_state
*state
= tty
->driver_data
;
1212 struct uart_port
*port
= state
->port
;
1214 BUG_ON(!kernel_locked());
1215 DPRINTK("uart_close(%d) called\n", port
->line
);
1219 if (tty_hung_up_p(filp
))
1222 if ((tty
->count
== 1) && (state
->count
!= 1)) {
1224 * Uh, oh. tty->count is 1, which means that the tty
1225 * structure will be freed. state->count should always
1226 * be one in these conditions. If it's greater than
1227 * one, we've got real problems, since it means the
1228 * serial port won't be shutdown.
1230 printk("uart_close: bad serial port count; tty->count is 1, "
1231 "state->count is %d\n", state
->count
);
1234 if (--state
->count
< 0) {
1235 printk("rs_close: bad serial port count for %s: %d\n",
1236 tty
->name
, state
->count
);
1243 * Now we wait for the transmit buffer to clear; and we notify
1244 * the line discipline to only process XON/XOFF characters by
1245 * setting tty->closing.
1249 if (state
->closing_wait
!= USF_CLOSING_WAIT_NONE
)
1250 tty_wait_until_sent(tty
, state
->closing_wait
);
1253 * At this point, we stop accepting input. To do this, we
1254 * disable the receive line status interrupts.
1256 if (state
->info
->flags
& UIF_INITIALIZED
) {
1257 unsigned long flags
;
1258 spin_lock_irqsave(&port
->lock
, flags
);
1259 port
->ops
->stop_rx(port
);
1260 spin_unlock_irqrestore(&port
->lock
, flags
);
1262 * Before we drop DTR, make sure the UART transmitter
1263 * has completely drained; this is especially
1264 * important if there is a transmit FIFO!
1266 uart_wait_until_sent(tty
, port
->timeout
);
1269 uart_shutdown(state
);
1270 uart_flush_buffer(tty
);
1271 if (tty
->ldisc
.flush_buffer
)
1272 tty
->ldisc
.flush_buffer(tty
);
1274 state
->info
->tty
= NULL
;
1276 if (state
->info
->blocked_open
) {
1277 if (state
->close_delay
) {
1278 set_current_state(TASK_INTERRUPTIBLE
);
1279 schedule_timeout(state
->close_delay
);
1280 set_current_state(TASK_RUNNING
);
1282 } else if (!uart_console(port
)) {
1283 uart_change_pm(state
, 3);
1287 * Wake up anyone trying to open this port.
1289 state
->info
->flags
&= ~UIF_NORMAL_ACTIVE
;
1290 wake_up_interruptible(&state
->info
->open_wait
);
1296 static void uart_wait_until_sent(struct tty_struct
*tty
, int timeout
)
1298 struct uart_state
*state
= tty
->driver_data
;
1299 struct uart_port
*port
= state
->port
;
1300 unsigned long char_time
, expire
;
1302 BUG_ON(!kernel_locked());
1304 if (port
->type
== PORT_UNKNOWN
|| port
->fifosize
== 0)
1308 * Set the check interval to be 1/5 of the estimated time to
1309 * send a single character, and make it at least 1. The check
1310 * interval should also be less than the timeout.
1312 * Note: we have to use pretty tight timings here to satisfy
1315 char_time
= (port
->timeout
- HZ
/50) / port
->fifosize
;
1316 char_time
= char_time
/ 5;
1319 if (timeout
&& timeout
< char_time
)
1320 char_time
= timeout
;
1323 * If the transmitter hasn't cleared in twice the approximate
1324 * amount of time to send the entire FIFO, it probably won't
1325 * ever clear. This assumes the UART isn't doing flow
1326 * control, which is currently the case. Hence, if it ever
1327 * takes longer than port->timeout, this is probably due to a
1328 * UART bug of some kind. So, we clamp the timeout parameter at
1331 if (timeout
== 0 || timeout
> 2 * port
->timeout
)
1332 timeout
= 2 * port
->timeout
;
1334 expire
= jiffies
+ timeout
;
1336 DPRINTK("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1337 port
->line
, jiffies
, expire
);
1340 * Check whether the transmitter is empty every 'char_time'.
1341 * 'timeout' / 'expire' give us the maximum amount of time
1344 while (!port
->ops
->tx_empty(port
)) {
1345 set_current_state(TASK_INTERRUPTIBLE
);
1346 schedule_timeout(char_time
);
1347 if (signal_pending(current
))
1349 if (time_after(jiffies
, expire
))
1352 set_current_state(TASK_RUNNING
); /* might not be needed */
1356 * This is called with the BKL held in
1357 * linux/drivers/char/tty_io.c:do_tty_hangup()
1358 * We're called from the eventd thread, so we can sleep for
1359 * a _short_ time only.
1361 static void uart_hangup(struct tty_struct
*tty
)
1363 struct uart_state
*state
= tty
->driver_data
;
1365 BUG_ON(!kernel_locked());
1366 DPRINTK("uart_hangup(%d)\n", state
->port
->line
);
1369 if (state
->info
&& state
->info
->flags
& UIF_NORMAL_ACTIVE
) {
1370 uart_flush_buffer(tty
);
1371 uart_shutdown(state
);
1373 state
->info
->flags
&= ~UIF_NORMAL_ACTIVE
;
1374 state
->info
->tty
= NULL
;
1375 wake_up_interruptible(&state
->info
->open_wait
);
1376 wake_up_interruptible(&state
->info
->delta_msr_wait
);
1382 * Copy across the serial console cflag setting into the termios settings
1383 * for the initial open of the port. This allows continuity between the
1384 * kernel settings, and the settings init adopts when it opens the port
1385 * for the first time.
1387 static void uart_update_termios(struct uart_state
*state
)
1389 struct tty_struct
*tty
= state
->info
->tty
;
1390 struct uart_port
*port
= state
->port
;
1392 if (uart_console(port
) && port
->cons
->cflag
) {
1393 tty
->termios
->c_cflag
= port
->cons
->cflag
;
1394 port
->cons
->cflag
= 0;
1398 * If the device failed to grab its irq resources,
1399 * or some other error occurred, don't try to talk
1400 * to the port hardware.
1402 if (!(tty
->flags
& (1 << TTY_IO_ERROR
))) {
1404 * Make termios settings take effect.
1406 uart_change_speed(state
, NULL
);
1409 * And finally enable the RTS and DTR signals.
1411 if (tty
->termios
->c_cflag
& CBAUD
)
1412 uart_set_mctrl(port
, TIOCM_DTR
| TIOCM_RTS
);
1417 * Block the open until the port is ready. We must be called with
1418 * the per-port semaphore held.
1421 uart_block_til_ready(struct file
*filp
, struct uart_state
*state
)
1423 DECLARE_WAITQUEUE(wait
, current
);
1424 struct uart_info
*info
= state
->info
;
1425 struct uart_port
*port
= state
->port
;
1427 info
->blocked_open
++;
1430 add_wait_queue(&info
->open_wait
, &wait
);
1432 set_current_state(TASK_INTERRUPTIBLE
);
1435 * If we have been hung up, tell userspace/restart open.
1437 if (tty_hung_up_p(filp
) || info
->tty
== NULL
)
1441 * If the port has been closed, tell userspace/restart open.
1443 if (!(info
->flags
& UIF_INITIALIZED
))
1447 * If non-blocking mode is set, or CLOCAL mode is set,
1448 * we don't want to wait for the modem status lines to
1449 * indicate that the port is ready.
1451 * Also, if the port is not enabled/configured, we want
1452 * to allow the open to succeed here. Note that we will
1453 * have set TTY_IO_ERROR for a non-existant port.
1455 if ((filp
->f_flags
& O_NONBLOCK
) ||
1456 (info
->tty
->termios
->c_cflag
& CLOCAL
) ||
1457 (info
->tty
->flags
& (1 << TTY_IO_ERROR
))) {
1462 * Set DTR to allow modem to know we're waiting. Do
1463 * not set RTS here - we want to make sure we catch
1464 * the data from the modem.
1466 if (info
->tty
->termios
->c_cflag
& CBAUD
)
1467 uart_set_mctrl(port
, TIOCM_DTR
);
1470 * and wait for the carrier to indicate that the
1471 * modem is ready for us.
1473 if (port
->ops
->get_mctrl(port
) & TIOCM_CAR
)
1480 if (signal_pending(current
))
1483 set_current_state(TASK_RUNNING
);
1484 remove_wait_queue(&info
->open_wait
, &wait
);
1487 info
->blocked_open
--;
1489 if (signal_pending(current
))
1490 return -ERESTARTSYS
;
1492 if (!info
->tty
|| tty_hung_up_p(filp
))
1498 static struct uart_state
*uart_get(struct uart_driver
*drv
, int line
)
1500 struct uart_state
*state
;
1503 state
= drv
->state
+ line
;
1504 if (down_interruptible(&state
->sem
)) {
1505 state
= ERR_PTR(-ERESTARTSYS
);
1513 state
= ERR_PTR(-ENXIO
);
1518 state
->info
= kmalloc(sizeof(struct uart_info
), GFP_KERNEL
);
1520 memset(state
->info
, 0, sizeof(struct uart_info
));
1521 init_waitqueue_head(&state
->info
->open_wait
);
1522 init_waitqueue_head(&state
->info
->delta_msr_wait
);
1525 * Link the info into the other structures.
1527 state
->port
->info
= state
->info
;
1529 tasklet_init(&state
->info
->tlet
, uart_tasklet_action
,
1530 (unsigned long)state
);
1534 state
= ERR_PTR(-ENOMEM
);
1544 * In 2.4.5, calls to uart_open are serialised by the BKL in
1545 * linux/fs/devices.c:chrdev_open()
1546 * Note that if this fails, then uart_close() _will_ be called.
1548 * In time, we want to scrap the "opening nonpresent ports"
1549 * behaviour and implement an alternative way for setserial
1550 * to set base addresses/ports/types. This will allow us to
1551 * get rid of a certain amount of extra tests.
1553 static int uart_open(struct tty_struct
*tty
, struct file
*filp
)
1555 struct uart_driver
*drv
= (struct uart_driver
*)tty
->driver
->driver_state
;
1556 struct uart_state
*state
;
1557 int retval
, line
= tty
->index
;
1559 BUG_ON(!kernel_locked());
1560 DPRINTK("uart_open(%d) called\n", line
);
1563 * tty->driver->num won't change, so we won't fail here with
1564 * tty->driver_data set to something non-NULL (and therefore
1565 * we won't get caught by uart_close()).
1568 if (line
>= tty
->driver
->num
)
1572 * We take the semaphore inside uart_get to guarantee that we won't
1573 * be re-entered while allocating the info structure, or while we
1574 * request any IRQs that the driver may need. This also has the nice
1575 * side-effect that it delays the action of uart_hangup, so we can
1576 * guarantee that info->tty will always contain something reasonable.
1578 state
= uart_get(drv
, line
);
1579 if (IS_ERR(state
)) {
1580 retval
= PTR_ERR(state
);
1585 * Once we set tty->driver_data here, we are guaranteed that
1586 * uart_close() will decrement the driver module use count.
1587 * Any failures from here onwards should not touch the count.
1589 tty
->driver_data
= state
;
1590 tty
->low_latency
= (state
->port
->flags
& UPF_LOW_LATENCY
) ? 1 : 0;
1592 state
->info
->tty
= tty
;
1595 * If the port is in the middle of closing, bail out now.
1597 if (tty_hung_up_p(filp
)) {
1605 * Make sure the device is in D0 state.
1607 if (state
->count
== 1)
1608 uart_change_pm(state
, 0);
1611 * Start up the serial port.
1613 retval
= uart_startup(state
, 0);
1616 * If we succeeded, wait until the port is ready.
1619 retval
= uart_block_til_ready(filp
, state
);
1623 * If this is the first open to succeed, adjust things to suit.
1625 if (retval
== 0 && !(state
->info
->flags
& UIF_NORMAL_ACTIVE
)) {
1626 state
->info
->flags
|= UIF_NORMAL_ACTIVE
;
1628 uart_update_termios(state
);
1635 static const char *uart_type(struct uart_port
*port
)
1637 const char *str
= NULL
;
1639 if (port
->ops
->type
)
1640 str
= port
->ops
->type(port
);
1648 #ifdef CONFIG_PROC_FS
1650 static int uart_line_info(char *buf
, struct uart_driver
*drv
, int i
)
1652 struct uart_state
*state
= drv
->state
+ i
;
1653 struct uart_port
*port
= state
->port
;
1655 unsigned int status
;
1661 ret
= sprintf(buf
, "%d: uart:%s port:%08X irq:%d",
1662 port
->line
, uart_type(port
),
1663 port
->iobase
, port
->irq
);
1665 if (port
->type
== PORT_UNKNOWN
) {
1670 status
= port
->ops
->get_mctrl(port
);
1672 ret
+= sprintf(buf
+ ret
, " tx:%d rx:%d",
1673 port
->icount
.tx
, port
->icount
.rx
);
1674 if (port
->icount
.frame
)
1675 ret
+= sprintf(buf
+ ret
, " fe:%d",
1676 port
->icount
.frame
);
1677 if (port
->icount
.parity
)
1678 ret
+= sprintf(buf
+ ret
, " pe:%d",
1679 port
->icount
.parity
);
1680 if (port
->icount
.brk
)
1681 ret
+= sprintf(buf
+ ret
, " brk:%d",
1683 if (port
->icount
.overrun
)
1684 ret
+= sprintf(buf
+ ret
, " oe:%d",
1685 port
->icount
.overrun
);
1687 #define INFOBIT(bit,str) \
1688 if (port->mctrl & (bit)) \
1689 strncat(stat_buf, (str), sizeof(stat_buf) - \
1690 strlen(stat_buf) - 2)
1691 #define STATBIT(bit,str) \
1692 if (status & (bit)) \
1693 strncat(stat_buf, (str), sizeof(stat_buf) - \
1694 strlen(stat_buf) - 2)
1698 INFOBIT(TIOCM_RTS
, "|RTS");
1699 STATBIT(TIOCM_CTS
, "|CTS");
1700 INFOBIT(TIOCM_DTR
, "|DTR");
1701 STATBIT(TIOCM_DSR
, "|DSR");
1702 STATBIT(TIOCM_CAR
, "|CD");
1703 STATBIT(TIOCM_RNG
, "|RI");
1706 strcat(stat_buf
, "\n");
1708 ret
+= sprintf(buf
+ ret
, stat_buf
);
1712 static int uart_read_proc(char *page
, char **start
, off_t off
,
1713 int count
, int *eof
, void *data
)
1715 struct tty_driver
*ttydrv
= data
;
1716 struct uart_driver
*drv
= ttydrv
->driver_state
;
1720 len
+= sprintf(page
, "serinfo:1.0 driver%s%s revision:%s\n",
1722 for (i
= 0; i
< drv
->nr
&& len
< PAGE_SIZE
- 96; i
++) {
1723 l
= uart_line_info(page
+ len
, drv
, i
);
1725 if (len
+ begin
> off
+ count
)
1727 if (len
+ begin
< off
) {
1734 if (off
>= len
+ begin
)
1736 *start
= page
+ (off
- begin
);
1737 return (count
< begin
+ len
- off
) ? count
: (begin
+ len
- off
);
1741 #ifdef CONFIG_SERIAL_CORE_CONSOLE
1743 * Check whether an invalid uart number has been specified, and
1744 * if so, search for the first available port that does have
1747 struct uart_port
* __init
1748 uart_get_console(struct uart_port
*ports
, int nr
, struct console
*co
)
1750 int idx
= co
->index
;
1752 if (idx
< 0 || idx
>= nr
|| (ports
[idx
].iobase
== 0 &&
1753 ports
[idx
].membase
== NULL
))
1754 for (idx
= 0; idx
< nr
; idx
++)
1755 if (ports
[idx
].iobase
!= 0 ||
1756 ports
[idx
].membase
!= NULL
)
1765 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1766 * @options: pointer to option string
1767 * @baud: pointer to an 'int' variable for the baud rate.
1768 * @parity: pointer to an 'int' variable for the parity.
1769 * @bits: pointer to an 'int' variable for the number of data bits.
1770 * @flow: pointer to an 'int' variable for the flow control character.
1772 * uart_parse_options decodes a string containing the serial console
1773 * options. The format of the string is <baud><parity><bits><flow>,
1777 uart_parse_options(char *options
, int *baud
, int *parity
, int *bits
, int *flow
)
1781 *baud
= simple_strtoul(s
, NULL
, 10);
1782 while (*s
>= '0' && *s
<= '9')
1797 static struct baud_rates baud_rates
[] = {
1798 { 921600, B921600
},
1799 { 460800, B460800
},
1800 { 230400, B230400
},
1801 { 115200, B115200
},
1813 * uart_set_options - setup the serial console parameters
1814 * @port: pointer to the serial ports uart_port structure
1815 * @co: console pointer
1817 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1818 * @bits: number of data bits
1819 * @flow: flow control character - 'r' (rts)
1822 uart_set_options(struct uart_port
*port
, struct console
*co
,
1823 int baud
, int parity
, int bits
, int flow
)
1825 struct termios termios
;
1828 memset(&termios
, 0, sizeof(struct termios
));
1830 termios
.c_cflag
= CREAD
| HUPCL
| CLOCAL
;
1833 * Construct a cflag setting.
1835 for (i
= 0; baud_rates
[i
].rate
; i
++)
1836 if (baud_rates
[i
].rate
<= baud
)
1839 termios
.c_cflag
|= baud_rates
[i
].cflag
;
1842 termios
.c_cflag
|= CS7
;
1844 termios
.c_cflag
|= CS8
;
1848 termios
.c_cflag
|= PARODD
;
1851 termios
.c_cflag
|= PARENB
;
1856 termios
.c_cflag
|= CRTSCTS
;
1858 port
->ops
->set_termios(port
, &termios
, NULL
);
1859 co
->cflag
= termios
.c_cflag
;
1863 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1865 static void uart_change_pm(struct uart_state
*state
, int pm_state
)
1867 struct uart_port
*port
= state
->port
;
1869 port
->ops
->pm(port
, pm_state
, state
->pm_state
);
1870 state
->pm_state
= pm_state
;
1873 int uart_suspend_port(struct uart_driver
*drv
, struct uart_port
*port
, u32 level
)
1875 struct uart_state
*state
= drv
->state
+ port
->line
;
1880 case SUSPEND_SAVE_STATE
:
1881 if (state
->info
&& state
->info
->flags
& UIF_INITIALIZED
) {
1882 struct uart_ops
*ops
= port
->ops
;
1884 spin_lock_irq(&port
->lock
);
1885 ops
->stop_tx(port
, 0);
1886 ops
->set_mctrl(port
, 0);
1888 spin_unlock_irq(&port
->lock
);
1891 * Wait for the transmitter to empty.
1893 while (!ops
->tx_empty(port
)) {
1894 set_current_state(TASK_UNINTERRUPTIBLE
);
1895 schedule_timeout(10*HZ
/1000);
1897 set_current_state(TASK_RUNNING
);
1899 ops
->shutdown(port
);
1903 case SUSPEND_POWER_DOWN
:
1905 * Disable the console device before suspending.
1907 if (uart_console(port
))
1908 port
->cons
->flags
&= ~CON_ENABLED
;
1910 uart_change_pm(state
, 3);
1919 int uart_resume_port(struct uart_driver
*drv
, struct uart_port
*port
, u32 level
)
1921 struct uart_state
*state
= drv
->state
+ port
->line
;
1926 case RESUME_POWER_ON
:
1927 uart_change_pm(state
, 0);
1930 * Re-enable the console device after suspending.
1932 if (uart_console(port
)) {
1933 uart_change_speed(state
, NULL
);
1934 port
->cons
->flags
|= CON_ENABLED
;
1938 case RESUME_RESTORE_STATE
:
1939 if (state
->info
&& state
->info
->flags
& UIF_INITIALIZED
) {
1940 struct uart_ops
*ops
= port
->ops
;
1942 ops
->set_mctrl(port
, 0);
1944 uart_change_speed(state
, NULL
);
1945 spin_lock_irq(&port
->lock
);
1946 ops
->set_mctrl(port
, port
->mctrl
);
1947 ops
->start_tx(port
, 0);
1948 spin_unlock_irq(&port
->lock
);
1959 uart_report_port(struct uart_driver
*drv
, struct uart_port
*port
)
1961 printk("%s%d", drv
->dev_name
, port
->line
);
1963 switch (port
->iotype
) {
1965 printk("I/O 0x%x", port
->iobase
);
1968 printk("I/O 0x%x offset 0x%x", port
->iobase
, port
->hub6
);
1971 printk("MMIO 0x%lx", port
->mapbase
);
1974 printk(" (irq = %d) is a %s\n", port
->irq
, uart_type(port
));
1978 uart_configure_port(struct uart_driver
*drv
, struct uart_state
*state
,
1979 struct uart_port
*port
)
1984 * If there isn't a port here, don't do anything further.
1986 if (!port
->iobase
&& !port
->mapbase
&& !port
->membase
)
1990 * Now do the auto configuration stuff. Note that config_port
1991 * is expected to claim the resources and map the port for us.
1993 flags
= UART_CONFIG_TYPE
;
1994 if (port
->flags
& UPF_AUTO_IRQ
)
1995 flags
|= UART_CONFIG_IRQ
;
1996 if (port
->flags
& UPF_BOOT_AUTOCONF
) {
1997 port
->type
= PORT_UNKNOWN
;
1998 port
->ops
->config_port(port
, flags
);
2001 if (port
->type
!= PORT_UNKNOWN
) {
2002 unsigned long flags
;
2004 uart_report_port(drv
, port
);
2007 * Ensure that the modem control lines are de-activated.
2008 * We probably don't need a spinlock around this, but
2010 spin_lock_irqsave(&port
->lock
, flags
);
2011 port
->ops
->set_mctrl(port
, 0);
2012 spin_unlock_irqrestore(&port
->lock
, flags
);
2015 * Power down all ports by default, except the
2016 * console if we have one.
2018 if (!uart_console(port
))
2019 uart_change_pm(state
, 3);
2024 * This reverses the effects of uart_configure_port, hanging up the
2025 * port before removal.
2028 uart_unconfigure_port(struct uart_driver
*drv
, struct uart_state
*state
)
2030 struct uart_port
*port
= state
->port
;
2031 struct uart_info
*info
= state
->info
;
2033 if (info
&& info
->tty
)
2034 tty_vhangup(info
->tty
);
2041 * Free the port IO and memory resources, if any.
2043 if (port
->type
!= PORT_UNKNOWN
)
2044 port
->ops
->release_port(port
);
2047 * Indicate that there isn't a port here anymore.
2049 port
->type
= PORT_UNKNOWN
;
2052 * Kill the tasklet, and free resources.
2055 tasklet_kill(&info
->tlet
);
2062 static struct tty_operations uart_ops
= {
2064 .close
= uart_close
,
2065 .write
= uart_write
,
2066 .put_char
= uart_put_char
,
2067 .flush_chars
= uart_flush_chars
,
2068 .write_room
= uart_write_room
,
2069 .chars_in_buffer
= uart_chars_in_buffer
,
2070 .flush_buffer
= uart_flush_buffer
,
2071 .ioctl
= uart_ioctl
,
2072 .throttle
= uart_throttle
,
2073 .unthrottle
= uart_unthrottle
,
2074 .send_xchar
= uart_send_xchar
,
2075 .set_termios
= uart_set_termios
,
2077 .start
= uart_start
,
2078 .hangup
= uart_hangup
,
2079 .break_ctl
= uart_break_ctl
,
2080 .wait_until_sent
= uart_wait_until_sent
,
2081 #ifdef CONFIG_PROC_FS
2082 .read_proc
= uart_read_proc
,
2084 .tiocmget
= uart_tiocmget
,
2085 .tiocmset
= uart_tiocmset
,
2089 * uart_register_driver - register a driver with the uart core layer
2090 * @drv: low level driver structure
2092 * Register a uart driver with the core driver. We in turn register
2093 * with the tty layer, and initialise the core driver per-port state.
2095 * We have a proc file in /proc/tty/driver which is named after the
2098 * drv->port should be NULL, and the per-port structures should be
2099 * registered using uart_add_one_port after this call has succeeded.
2101 int uart_register_driver(struct uart_driver
*drv
)
2103 struct tty_driver
*normal
= NULL
;
2109 * Maybe we should be using a slab cache for this, especially if
2110 * we have a large number of ports to handle.
2112 drv
->state
= kmalloc(sizeof(struct uart_state
) * drv
->nr
, GFP_KERNEL
);
2117 memset(drv
->state
, 0, sizeof(struct uart_state
) * drv
->nr
);
2119 normal
= alloc_tty_driver(drv
->nr
);
2123 drv
->tty_driver
= normal
;
2125 normal
->owner
= drv
->owner
;
2126 normal
->driver_name
= drv
->driver_name
;
2127 normal
->devfs_name
= drv
->devfs_name
;
2128 normal
->name
= drv
->dev_name
;
2129 normal
->major
= drv
->major
;
2130 normal
->minor_start
= drv
->minor
;
2131 normal
->type
= TTY_DRIVER_TYPE_SERIAL
;
2132 normal
->subtype
= SERIAL_TYPE_NORMAL
;
2133 normal
->init_termios
= tty_std_termios
;
2134 normal
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
2135 normal
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_NO_DEVFS
;
2136 normal
->driver_state
= drv
;
2137 tty_set_operations(normal
, &uart_ops
);
2140 * Initialise the UART state(s).
2142 for (i
= 0; i
< drv
->nr
; i
++) {
2143 struct uart_state
*state
= drv
->state
+ i
;
2145 state
->close_delay
= 5 * HZ
/ 10;
2146 state
->closing_wait
= 30 * HZ
;
2148 init_MUTEX(&state
->sem
);
2151 retval
= tty_register_driver(normal
);
2154 put_tty_driver(normal
);
2161 * uart_unregister_driver - remove a driver from the uart core layer
2162 * @drv: low level driver structure
2164 * Remove all references to a driver from the core driver. The low
2165 * level driver must have removed all its ports via the
2166 * uart_remove_one_port() if it registered them with uart_add_one_port().
2167 * (ie, drv->port == NULL)
2169 void uart_unregister_driver(struct uart_driver
*drv
)
2171 struct tty_driver
*p
= drv
->tty_driver
;
2172 tty_unregister_driver(p
);
2175 drv
->tty_driver
= NULL
;
2178 struct tty_driver
*uart_console_device(struct console
*co
, int *index
)
2180 struct uart_driver
*p
= co
->data
;
2182 return p
->tty_driver
;
2186 * uart_add_one_port - attach a driver-defined port structure
2187 * @drv: pointer to the uart low level driver structure for this port
2188 * @port: uart port structure to use for this port.
2190 * This allows the driver to register its own uart_port structure
2191 * with the core driver. The main purpose is to allow the low
2192 * level uart drivers to expand uart_port, rather than having yet
2193 * more levels of structures.
2195 int uart_add_one_port(struct uart_driver
*drv
, struct uart_port
*port
)
2197 struct uart_state
*state
;
2200 BUG_ON(in_interrupt());
2202 if (port
->line
>= drv
->nr
)
2205 state
= drv
->state
+ port
->line
;
2215 spin_lock_init(&port
->lock
);
2216 port
->cons
= drv
->cons
;
2217 port
->info
= state
->info
;
2219 uart_configure_port(drv
, state
, port
);
2222 * Register the port whether it's detected or not. This allows
2223 * setserial to be used to alter this ports parameters.
2225 tty_register_device(drv
->tty_driver
, port
->line
, NULL
);
2234 * uart_remove_one_port - detach a driver defined port structure
2235 * @drv: pointer to the uart low level driver structure for this port
2236 * @port: uart port structure for this port
2238 * This unhooks (and hangs up) the specified port structure from the
2239 * core driver. No further calls will be made to the low-level code
2242 int uart_remove_one_port(struct uart_driver
*drv
, struct uart_port
*port
)
2244 struct uart_state
*state
= drv
->state
+ port
->line
;
2246 BUG_ON(in_interrupt());
2248 if (state
->port
!= port
)
2249 printk(KERN_ALERT
"Removing wrong port: %p != %p\n",
2255 * Remove the devices from devfs
2257 tty_unregister_device(drv
->tty_driver
, port
->line
);
2259 uart_unconfigure_port(drv
, state
);
2267 * Are the two ports equivalent?
2269 static int uart_match_port(struct uart_port
*port1
, struct uart_port
*port2
)
2271 if (port1
->iotype
!= port2
->iotype
)
2274 switch (port1
->iotype
) {
2276 return (port1
->iobase
== port2
->iobase
);
2278 return (port1
->iobase
== port2
->iobase
) &&
2279 (port1
->hub6
== port2
->hub6
);
2281 return (port1
->membase
== port2
->membase
);
2287 * Try to find an unused uart_state slot for a port.
2289 static struct uart_state
*
2290 uart_find_match_or_unused(struct uart_driver
*drv
, struct uart_port
*port
)
2295 * First, find a port entry which matches. Note: if we do
2296 * find a matching entry, and it has a non-zero use count,
2297 * then we can't register the port.
2299 for (i
= 0; i
< drv
->nr
; i
++)
2300 if (uart_match_port(drv
->state
[i
].port
, port
))
2301 return &drv
->state
[i
];
2304 * We didn't find a matching entry, so look for the first
2305 * free entry. We look for one which hasn't been previously
2306 * used (indicated by zero iobase).
2308 for (i
= 0; i
< drv
->nr
; i
++)
2309 if (drv
->state
[i
].port
->type
== PORT_UNKNOWN
&&
2310 drv
->state
[i
].port
->iobase
== 0 &&
2311 drv
->state
[i
].count
== 0)
2312 return &drv
->state
[i
];
2315 * That also failed. Last resort is to find any currently
2316 * entry which doesn't have a real port associated with it.
2318 for (i
= 0; i
< drv
->nr
; i
++)
2319 if (drv
->state
[i
].port
->type
== PORT_UNKNOWN
&&
2320 drv
->state
[i
].count
== 0)
2321 return &drv
->state
[i
];
2327 * uart_register_port: register uart settings with a port
2328 * @drv: pointer to the uart low level driver structure for this port
2329 * @port: uart port structure describing the port
2331 * Register UART settings with the specified low level driver. Detect
2332 * the type of the port if UPF_BOOT_AUTOCONF is set, and detect the
2333 * IRQ if UPF_AUTO_IRQ is set.
2335 * We try to pick the same port for the same IO base address, so that
2336 * when a modem is plugged in, unplugged and plugged back in, it gets
2337 * allocated the same port.
2339 * Returns negative error, or positive line number.
2341 int uart_register_port(struct uart_driver
*drv
, struct uart_port
*port
)
2343 struct uart_state
*state
;
2348 state
= uart_find_match_or_unused(drv
, port
);
2352 * Ok, we've found a line that we can use.
2354 * If we find a port that matches this one, and it appears
2355 * to be in-use (even if it doesn't have a type) we shouldn't
2356 * alter it underneath itself - the port may be open and
2357 * trying to do useful work.
2359 if (uart_users(state
) != 0) {
2365 * If the port is already initialised, don't touch it.
2367 if (state
->port
->type
== PORT_UNKNOWN
) {
2368 state
->port
->iobase
= port
->iobase
;
2369 state
->port
->membase
= port
->membase
;
2370 state
->port
->irq
= port
->irq
;
2371 state
->port
->uartclk
= port
->uartclk
;
2372 state
->port
->fifosize
= port
->fifosize
;
2373 state
->port
->regshift
= port
->regshift
;
2374 state
->port
->iotype
= port
->iotype
;
2375 state
->port
->flags
= port
->flags
;
2376 state
->port
->line
= state
- drv
->state
;
2377 state
->port
->mapbase
= port
->mapbase
;
2379 uart_configure_port(drv
, state
, state
->port
);
2382 ret
= state
->port
->line
;
2391 * uart_unregister_port - de-allocate a port
2392 * @drv: pointer to the uart low level driver structure for this port
2393 * @line: line index previously returned from uart_register_port()
2395 * Hang up the specified line associated with the low level driver,
2396 * and mark the port as unused.
2398 void uart_unregister_port(struct uart_driver
*drv
, int line
)
2400 struct uart_state
*state
;
2402 if (line
< 0 || line
>= drv
->nr
) {
2403 printk(KERN_ERR
"Attempt to unregister ");
2404 printk("%s%d", drv
->dev_name
, line
);
2409 state
= drv
->state
+ line
;
2412 uart_unconfigure_port(drv
, state
);
2416 EXPORT_SYMBOL(uart_write_wakeup
);
2417 EXPORT_SYMBOL(uart_register_driver
);
2418 EXPORT_SYMBOL(uart_unregister_driver
);
2419 EXPORT_SYMBOL(uart_suspend_port
);
2420 EXPORT_SYMBOL(uart_resume_port
);
2421 EXPORT_SYMBOL(uart_register_port
);
2422 EXPORT_SYMBOL(uart_unregister_port
);
2423 EXPORT_SYMBOL(uart_add_one_port
);
2424 EXPORT_SYMBOL(uart_remove_one_port
);
2426 MODULE_DESCRIPTION("Serial driver core");
2427 MODULE_LICENSE("GPL");