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
))
1493 return (port
->flags
& UPF_HUP_NOTIFY
) ?
1494 -EAGAIN
: -ERESTARTSYS
;
1499 static struct uart_state
*uart_get(struct uart_driver
*drv
, int line
)
1501 struct uart_state
*state
;
1504 state
= drv
->state
+ line
;
1505 if (down_interruptible(&state
->sem
)) {
1506 state
= ERR_PTR(-ERESTARTSYS
);
1514 state
= ERR_PTR(-ENXIO
);
1519 state
->info
= kmalloc(sizeof(struct uart_info
), GFP_KERNEL
);
1521 memset(state
->info
, 0, sizeof(struct uart_info
));
1522 init_waitqueue_head(&state
->info
->open_wait
);
1523 init_waitqueue_head(&state
->info
->delta_msr_wait
);
1526 * Link the info into the other structures.
1528 state
->port
->info
= state
->info
;
1530 tasklet_init(&state
->info
->tlet
, uart_tasklet_action
,
1531 (unsigned long)state
);
1535 state
= ERR_PTR(-ENOMEM
);
1545 * In 2.4.5, calls to uart_open are serialised by the BKL in
1546 * linux/fs/devices.c:chrdev_open()
1547 * Note that if this fails, then uart_close() _will_ be called.
1549 * In time, we want to scrap the "opening nonpresent ports"
1550 * behaviour and implement an alternative way for setserial
1551 * to set base addresses/ports/types. This will allow us to
1552 * get rid of a certain amount of extra tests.
1554 static int uart_open(struct tty_struct
*tty
, struct file
*filp
)
1556 struct uart_driver
*drv
= (struct uart_driver
*)tty
->driver
->driver_state
;
1557 struct uart_state
*state
;
1558 int retval
, line
= tty
->index
;
1560 BUG_ON(!kernel_locked());
1561 DPRINTK("uart_open(%d) called\n", line
);
1564 * tty->driver->num won't change, so we won't fail here with
1565 * tty->driver_data set to something non-NULL (and therefore
1566 * we won't get caught by uart_close()).
1569 if (line
>= tty
->driver
->num
)
1573 * We take the semaphore inside uart_get to guarantee that we won't
1574 * be re-entered while allocating the info structure, or while we
1575 * request any IRQs that the driver may need. This also has the nice
1576 * side-effect that it delays the action of uart_hangup, so we can
1577 * guarantee that info->tty will always contain something reasonable.
1579 state
= uart_get(drv
, line
);
1580 if (IS_ERR(state
)) {
1581 retval
= PTR_ERR(state
);
1586 * Once we set tty->driver_data here, we are guaranteed that
1587 * uart_close() will decrement the driver module use count.
1588 * Any failures from here onwards should not touch the count.
1590 tty
->driver_data
= state
;
1591 tty
->low_latency
= (state
->port
->flags
& UPF_LOW_LATENCY
) ? 1 : 0;
1593 state
->info
->tty
= tty
;
1596 * If the port is in the middle of closing, bail out now.
1598 if (tty_hung_up_p(filp
)) {
1599 retval
= (state
->port
->flags
& UPF_HUP_NOTIFY
) ?
1600 -EAGAIN
: -ERESTARTSYS
;
1607 * Make sure the device is in D0 state.
1609 if (state
->count
== 1)
1610 uart_change_pm(state
, 0);
1613 * Start up the serial port.
1615 retval
= uart_startup(state
, 0);
1618 * If we succeeded, wait until the port is ready.
1621 retval
= uart_block_til_ready(filp
, state
);
1625 * If this is the first open to succeed, adjust things to suit.
1627 if (retval
== 0 && !(state
->info
->flags
& UIF_NORMAL_ACTIVE
)) {
1628 state
->info
->flags
|= UIF_NORMAL_ACTIVE
;
1630 uart_update_termios(state
);
1637 static const char *uart_type(struct uart_port
*port
)
1639 const char *str
= NULL
;
1641 if (port
->ops
->type
)
1642 str
= port
->ops
->type(port
);
1650 #ifdef CONFIG_PROC_FS
1652 static int uart_line_info(char *buf
, struct uart_driver
*drv
, int i
)
1654 struct uart_state
*state
= drv
->state
+ i
;
1655 struct uart_port
*port
= state
->port
;
1657 unsigned int status
;
1663 ret
= sprintf(buf
, "%d: uart:%s port:%08X irq:%d",
1664 port
->line
, uart_type(port
),
1665 port
->iobase
, port
->irq
);
1667 if (port
->type
== PORT_UNKNOWN
) {
1672 status
= port
->ops
->get_mctrl(port
);
1674 ret
+= sprintf(buf
+ ret
, " tx:%d rx:%d",
1675 port
->icount
.tx
, port
->icount
.rx
);
1676 if (port
->icount
.frame
)
1677 ret
+= sprintf(buf
+ ret
, " fe:%d",
1678 port
->icount
.frame
);
1679 if (port
->icount
.parity
)
1680 ret
+= sprintf(buf
+ ret
, " pe:%d",
1681 port
->icount
.parity
);
1682 if (port
->icount
.brk
)
1683 ret
+= sprintf(buf
+ ret
, " brk:%d",
1685 if (port
->icount
.overrun
)
1686 ret
+= sprintf(buf
+ ret
, " oe:%d",
1687 port
->icount
.overrun
);
1689 #define INFOBIT(bit,str) \
1690 if (port->mctrl & (bit)) \
1691 strncat(stat_buf, (str), sizeof(stat_buf) - \
1692 strlen(stat_buf) - 2)
1693 #define STATBIT(bit,str) \
1694 if (status & (bit)) \
1695 strncat(stat_buf, (str), sizeof(stat_buf) - \
1696 strlen(stat_buf) - 2)
1700 INFOBIT(TIOCM_RTS
, "|RTS");
1701 STATBIT(TIOCM_CTS
, "|CTS");
1702 INFOBIT(TIOCM_DTR
, "|DTR");
1703 STATBIT(TIOCM_DSR
, "|DSR");
1704 STATBIT(TIOCM_CAR
, "|CD");
1705 STATBIT(TIOCM_RNG
, "|RI");
1708 strcat(stat_buf
, "\n");
1710 ret
+= sprintf(buf
+ ret
, stat_buf
);
1714 static int uart_read_proc(char *page
, char **start
, off_t off
,
1715 int count
, int *eof
, void *data
)
1717 struct tty_driver
*ttydrv
= data
;
1718 struct uart_driver
*drv
= ttydrv
->driver_state
;
1722 len
+= sprintf(page
, "serinfo:1.0 driver%s%s revision:%s\n",
1724 for (i
= 0; i
< drv
->nr
&& len
< PAGE_SIZE
- 96; i
++) {
1725 l
= uart_line_info(page
+ len
, drv
, i
);
1727 if (len
+ begin
> off
+ count
)
1729 if (len
+ begin
< off
) {
1736 if (off
>= len
+ begin
)
1738 *start
= page
+ (off
- begin
);
1739 return (count
< begin
+ len
- off
) ? count
: (begin
+ len
- off
);
1743 #ifdef CONFIG_SERIAL_CORE_CONSOLE
1745 * Check whether an invalid uart number has been specified, and
1746 * if so, search for the first available port that does have
1749 struct uart_port
* __init
1750 uart_get_console(struct uart_port
*ports
, int nr
, struct console
*co
)
1752 int idx
= co
->index
;
1754 if (idx
< 0 || idx
>= nr
|| (ports
[idx
].iobase
== 0 &&
1755 ports
[idx
].membase
== NULL
))
1756 for (idx
= 0; idx
< nr
; idx
++)
1757 if (ports
[idx
].iobase
!= 0 ||
1758 ports
[idx
].membase
!= NULL
)
1767 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1768 * @options: pointer to option string
1769 * @baud: pointer to an 'int' variable for the baud rate.
1770 * @parity: pointer to an 'int' variable for the parity.
1771 * @bits: pointer to an 'int' variable for the number of data bits.
1772 * @flow: pointer to an 'int' variable for the flow control character.
1774 * uart_parse_options decodes a string containing the serial console
1775 * options. The format of the string is <baud><parity><bits><flow>,
1779 uart_parse_options(char *options
, int *baud
, int *parity
, int *bits
, int *flow
)
1783 *baud
= simple_strtoul(s
, NULL
, 10);
1784 while (*s
>= '0' && *s
<= '9')
1799 static struct baud_rates baud_rates
[] = {
1800 { 921600, B921600
},
1801 { 460800, B460800
},
1802 { 230400, B230400
},
1803 { 115200, B115200
},
1815 * uart_set_options - setup the serial console parameters
1816 * @port: pointer to the serial ports uart_port structure
1817 * @co: console pointer
1819 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1820 * @bits: number of data bits
1821 * @flow: flow control character - 'r' (rts)
1824 uart_set_options(struct uart_port
*port
, struct console
*co
,
1825 int baud
, int parity
, int bits
, int flow
)
1827 struct termios termios
;
1830 memset(&termios
, 0, sizeof(struct termios
));
1832 termios
.c_cflag
= CREAD
| HUPCL
| CLOCAL
;
1835 * Construct a cflag setting.
1837 for (i
= 0; baud_rates
[i
].rate
; i
++)
1838 if (baud_rates
[i
].rate
<= baud
)
1841 termios
.c_cflag
|= baud_rates
[i
].cflag
;
1844 termios
.c_cflag
|= CS7
;
1846 termios
.c_cflag
|= CS8
;
1850 termios
.c_cflag
|= PARODD
;
1853 termios
.c_cflag
|= PARENB
;
1858 termios
.c_cflag
|= CRTSCTS
;
1860 port
->ops
->set_termios(port
, &termios
, NULL
);
1861 co
->cflag
= termios
.c_cflag
;
1865 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1867 static void uart_change_pm(struct uart_state
*state
, int pm_state
)
1869 struct uart_port
*port
= state
->port
;
1871 port
->ops
->pm(port
, pm_state
, state
->pm_state
);
1872 state
->pm_state
= pm_state
;
1875 int uart_suspend_port(struct uart_driver
*drv
, struct uart_port
*port
, u32 level
)
1877 struct uart_state
*state
= drv
->state
+ port
->line
;
1882 case SUSPEND_SAVE_STATE
:
1883 if (state
->info
&& state
->info
->flags
& UIF_INITIALIZED
) {
1884 struct uart_ops
*ops
= port
->ops
;
1886 spin_lock_irq(&port
->lock
);
1887 ops
->stop_tx(port
, 0);
1888 ops
->set_mctrl(port
, 0);
1890 spin_unlock_irq(&port
->lock
);
1893 * Wait for the transmitter to empty.
1895 while (!ops
->tx_empty(port
)) {
1896 set_current_state(TASK_UNINTERRUPTIBLE
);
1897 schedule_timeout(10*HZ
/1000);
1899 set_current_state(TASK_RUNNING
);
1901 ops
->shutdown(port
);
1905 case SUSPEND_POWER_DOWN
:
1907 * Disable the console device before suspending.
1909 if (uart_console(port
))
1910 port
->cons
->flags
&= ~CON_ENABLED
;
1912 uart_change_pm(state
, 3);
1921 int uart_resume_port(struct uart_driver
*drv
, struct uart_port
*port
, u32 level
)
1923 struct uart_state
*state
= drv
->state
+ port
->line
;
1928 case RESUME_POWER_ON
:
1929 uart_change_pm(state
, 0);
1932 * Re-enable the console device after suspending.
1934 if (uart_console(port
)) {
1935 uart_change_speed(state
, NULL
);
1936 port
->cons
->flags
|= CON_ENABLED
;
1940 case RESUME_RESTORE_STATE
:
1941 if (state
->info
&& state
->info
->flags
& UIF_INITIALIZED
) {
1942 struct uart_ops
*ops
= port
->ops
;
1944 ops
->set_mctrl(port
, 0);
1946 uart_change_speed(state
, NULL
);
1947 spin_lock_irq(&port
->lock
);
1948 ops
->set_mctrl(port
, port
->mctrl
);
1949 ops
->start_tx(port
, 0);
1950 spin_unlock_irq(&port
->lock
);
1961 uart_report_port(struct uart_driver
*drv
, struct uart_port
*port
)
1963 printk("%s%d", drv
->dev_name
, port
->line
);
1965 switch (port
->iotype
) {
1967 printk("I/O 0x%x", port
->iobase
);
1970 printk("I/O 0x%x offset 0x%x", port
->iobase
, port
->hub6
);
1973 printk("MMIO 0x%lx", port
->mapbase
);
1976 printk(" (irq = %d) is a %s\n", port
->irq
, uart_type(port
));
1980 uart_configure_port(struct uart_driver
*drv
, struct uart_state
*state
,
1981 struct uart_port
*port
)
1986 * If there isn't a port here, don't do anything further.
1988 if (!port
->iobase
&& !port
->mapbase
&& !port
->membase
)
1992 * Now do the auto configuration stuff. Note that config_port
1993 * is expected to claim the resources and map the port for us.
1995 flags
= UART_CONFIG_TYPE
;
1996 if (port
->flags
& UPF_AUTO_IRQ
)
1997 flags
|= UART_CONFIG_IRQ
;
1998 if (port
->flags
& UPF_BOOT_AUTOCONF
) {
1999 port
->type
= PORT_UNKNOWN
;
2000 port
->ops
->config_port(port
, flags
);
2003 if (port
->type
!= PORT_UNKNOWN
) {
2004 unsigned long flags
;
2006 uart_report_port(drv
, port
);
2009 * Ensure that the modem control lines are de-activated.
2010 * We probably don't need a spinlock around this, but
2012 spin_lock_irqsave(&port
->lock
, flags
);
2013 port
->ops
->set_mctrl(port
, 0);
2014 spin_unlock_irqrestore(&port
->lock
, flags
);
2017 * Power down all ports by default, except the
2018 * console if we have one.
2020 if (!uart_console(port
))
2021 uart_change_pm(state
, 3);
2026 * This reverses the effects of uart_configure_port, hanging up the
2027 * port before removal.
2030 uart_unconfigure_port(struct uart_driver
*drv
, struct uart_state
*state
)
2032 struct uart_port
*port
= state
->port
;
2033 struct uart_info
*info
= state
->info
;
2035 if (info
&& info
->tty
)
2036 tty_vhangup(info
->tty
);
2043 * Free the port IO and memory resources, if any.
2045 if (port
->type
!= PORT_UNKNOWN
)
2046 port
->ops
->release_port(port
);
2049 * Indicate that there isn't a port here anymore.
2051 port
->type
= PORT_UNKNOWN
;
2054 * Kill the tasklet, and free resources.
2057 tasklet_kill(&info
->tlet
);
2064 static struct tty_operations uart_ops
= {
2066 .close
= uart_close
,
2067 .write
= uart_write
,
2068 .put_char
= uart_put_char
,
2069 .flush_chars
= uart_flush_chars
,
2070 .write_room
= uart_write_room
,
2071 .chars_in_buffer
= uart_chars_in_buffer
,
2072 .flush_buffer
= uart_flush_buffer
,
2073 .ioctl
= uart_ioctl
,
2074 .throttle
= uart_throttle
,
2075 .unthrottle
= uart_unthrottle
,
2076 .send_xchar
= uart_send_xchar
,
2077 .set_termios
= uart_set_termios
,
2079 .start
= uart_start
,
2080 .hangup
= uart_hangup
,
2081 .break_ctl
= uart_break_ctl
,
2082 .wait_until_sent
= uart_wait_until_sent
,
2083 #ifdef CONFIG_PROC_FS
2084 .read_proc
= uart_read_proc
,
2086 .tiocmget
= uart_tiocmget
,
2087 .tiocmset
= uart_tiocmset
,
2091 * uart_register_driver - register a driver with the uart core layer
2092 * @drv: low level driver structure
2094 * Register a uart driver with the core driver. We in turn register
2095 * with the tty layer, and initialise the core driver per-port state.
2097 * We have a proc file in /proc/tty/driver which is named after the
2100 * drv->port should be NULL, and the per-port structures should be
2101 * registered using uart_add_one_port after this call has succeeded.
2103 int uart_register_driver(struct uart_driver
*drv
)
2105 struct tty_driver
*normal
= NULL
;
2111 * Maybe we should be using a slab cache for this, especially if
2112 * we have a large number of ports to handle.
2114 drv
->state
= kmalloc(sizeof(struct uart_state
) * drv
->nr
, GFP_KERNEL
);
2119 memset(drv
->state
, 0, sizeof(struct uart_state
) * drv
->nr
);
2121 normal
= alloc_tty_driver(drv
->nr
);
2125 drv
->tty_driver
= normal
;
2127 normal
->owner
= drv
->owner
;
2128 normal
->driver_name
= drv
->driver_name
;
2129 normal
->devfs_name
= drv
->devfs_name
;
2130 normal
->name
= drv
->dev_name
;
2131 normal
->major
= drv
->major
;
2132 normal
->minor_start
= drv
->minor
;
2133 normal
->type
= TTY_DRIVER_TYPE_SERIAL
;
2134 normal
->subtype
= SERIAL_TYPE_NORMAL
;
2135 normal
->init_termios
= tty_std_termios
;
2136 normal
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
2137 normal
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_NO_DEVFS
;
2138 normal
->driver_state
= drv
;
2139 tty_set_operations(normal
, &uart_ops
);
2142 * Initialise the UART state(s).
2144 for (i
= 0; i
< drv
->nr
; i
++) {
2145 struct uart_state
*state
= drv
->state
+ i
;
2147 state
->close_delay
= 5 * HZ
/ 10;
2148 state
->closing_wait
= 30 * HZ
;
2150 init_MUTEX(&state
->sem
);
2153 retval
= tty_register_driver(normal
);
2156 put_tty_driver(normal
);
2163 * uart_unregister_driver - remove a driver from the uart core layer
2164 * @drv: low level driver structure
2166 * Remove all references to a driver from the core driver. The low
2167 * level driver must have removed all its ports via the
2168 * uart_remove_one_port() if it registered them with uart_add_one_port().
2169 * (ie, drv->port == NULL)
2171 void uart_unregister_driver(struct uart_driver
*drv
)
2173 struct tty_driver
*p
= drv
->tty_driver
;
2174 tty_unregister_driver(p
);
2177 drv
->tty_driver
= NULL
;
2180 struct tty_driver
*uart_console_device(struct console
*co
, int *index
)
2182 struct uart_driver
*p
= co
->data
;
2184 return p
->tty_driver
;
2188 * uart_add_one_port - attach a driver-defined port structure
2189 * @drv: pointer to the uart low level driver structure for this port
2190 * @port: uart port structure to use for this port.
2192 * This allows the driver to register its own uart_port structure
2193 * with the core driver. The main purpose is to allow the low
2194 * level uart drivers to expand uart_port, rather than having yet
2195 * more levels of structures.
2197 int uart_add_one_port(struct uart_driver
*drv
, struct uart_port
*port
)
2199 struct uart_state
*state
;
2202 BUG_ON(in_interrupt());
2204 if (port
->line
>= drv
->nr
)
2207 state
= drv
->state
+ port
->line
;
2217 spin_lock_init(&port
->lock
);
2218 port
->cons
= drv
->cons
;
2219 port
->info
= state
->info
;
2221 uart_configure_port(drv
, state
, port
);
2224 * Register the port whether it's detected or not. This allows
2225 * setserial to be used to alter this ports parameters.
2227 tty_register_device(drv
->tty_driver
, port
->line
, NULL
);
2236 * uart_remove_one_port - detach a driver defined port structure
2237 * @drv: pointer to the uart low level driver structure for this port
2238 * @port: uart port structure for this port
2240 * This unhooks (and hangs up) the specified port structure from the
2241 * core driver. No further calls will be made to the low-level code
2244 int uart_remove_one_port(struct uart_driver
*drv
, struct uart_port
*port
)
2246 struct uart_state
*state
= drv
->state
+ port
->line
;
2248 BUG_ON(in_interrupt());
2250 if (state
->port
!= port
)
2251 printk(KERN_ALERT
"Removing wrong port: %p != %p\n",
2257 * Remove the devices from devfs
2259 tty_unregister_device(drv
->tty_driver
, port
->line
);
2261 uart_unconfigure_port(drv
, state
);
2269 * Are the two ports equivalent?
2271 static int uart_match_port(struct uart_port
*port1
, struct uart_port
*port2
)
2273 if (port1
->iotype
!= port2
->iotype
)
2276 switch (port1
->iotype
) {
2278 return (port1
->iobase
== port2
->iobase
);
2280 return (port1
->iobase
== port2
->iobase
) &&
2281 (port1
->hub6
== port2
->hub6
);
2283 return (port1
->membase
== port2
->membase
);
2289 * Try to find an unused uart_state slot for a port.
2291 static struct uart_state
*
2292 uart_find_match_or_unused(struct uart_driver
*drv
, struct uart_port
*port
)
2297 * First, find a port entry which matches. Note: if we do
2298 * find a matching entry, and it has a non-zero use count,
2299 * then we can't register the port.
2301 for (i
= 0; i
< drv
->nr
; i
++)
2302 if (uart_match_port(drv
->state
[i
].port
, port
))
2303 return &drv
->state
[i
];
2306 * We didn't find a matching entry, so look for the first
2307 * free entry. We look for one which hasn't been previously
2308 * used (indicated by zero iobase).
2310 for (i
= 0; i
< drv
->nr
; i
++)
2311 if (drv
->state
[i
].port
->type
== PORT_UNKNOWN
&&
2312 drv
->state
[i
].port
->iobase
== 0 &&
2313 drv
->state
[i
].count
== 0)
2314 return &drv
->state
[i
];
2317 * That also failed. Last resort is to find any currently
2318 * entry which doesn't have a real port associated with it.
2320 for (i
= 0; i
< drv
->nr
; i
++)
2321 if (drv
->state
[i
].port
->type
== PORT_UNKNOWN
&&
2322 drv
->state
[i
].count
== 0)
2323 return &drv
->state
[i
];
2329 * uart_register_port: register uart settings with a port
2330 * @drv: pointer to the uart low level driver structure for this port
2331 * @port: uart port structure describing the port
2333 * Register UART settings with the specified low level driver. Detect
2334 * the type of the port if UPF_BOOT_AUTOCONF is set, and detect the
2335 * IRQ if UPF_AUTO_IRQ is set.
2337 * We try to pick the same port for the same IO base address, so that
2338 * when a modem is plugged in, unplugged and plugged back in, it gets
2339 * allocated the same port.
2341 * Returns negative error, or positive line number.
2343 int uart_register_port(struct uart_driver
*drv
, struct uart_port
*port
)
2345 struct uart_state
*state
;
2350 state
= uart_find_match_or_unused(drv
, port
);
2354 * Ok, we've found a line that we can use.
2356 * If we find a port that matches this one, and it appears
2357 * to be in-use (even if it doesn't have a type) we shouldn't
2358 * alter it underneath itself - the port may be open and
2359 * trying to do useful work.
2361 if (uart_users(state
) != 0) {
2367 * If the port is already initialised, don't touch it.
2369 if (state
->port
->type
== PORT_UNKNOWN
) {
2370 state
->port
->iobase
= port
->iobase
;
2371 state
->port
->membase
= port
->membase
;
2372 state
->port
->irq
= port
->irq
;
2373 state
->port
->uartclk
= port
->uartclk
;
2374 state
->port
->fifosize
= port
->fifosize
;
2375 state
->port
->regshift
= port
->regshift
;
2376 state
->port
->iotype
= port
->iotype
;
2377 state
->port
->flags
= port
->flags
;
2378 state
->port
->line
= state
- drv
->state
;
2379 state
->port
->mapbase
= port
->mapbase
;
2381 uart_configure_port(drv
, state
, state
->port
);
2384 ret
= state
->port
->line
;
2393 * uart_unregister_port - de-allocate a port
2394 * @drv: pointer to the uart low level driver structure for this port
2395 * @line: line index previously returned from uart_register_port()
2397 * Hang up the specified line associated with the low level driver,
2398 * and mark the port as unused.
2400 void uart_unregister_port(struct uart_driver
*drv
, int line
)
2402 struct uart_state
*state
;
2404 if (line
< 0 || line
>= drv
->nr
) {
2405 printk(KERN_ERR
"Attempt to unregister ");
2406 printk("%s%d", drv
->dev_name
, line
);
2411 state
= drv
->state
+ line
;
2414 uart_unconfigure_port(drv
, state
);
2418 EXPORT_SYMBOL(uart_write_wakeup
);
2419 EXPORT_SYMBOL(uart_register_driver
);
2420 EXPORT_SYMBOL(uart_unregister_driver
);
2421 EXPORT_SYMBOL(uart_suspend_port
);
2422 EXPORT_SYMBOL(uart_resume_port
);
2423 EXPORT_SYMBOL(uart_register_port
);
2424 EXPORT_SYMBOL(uart_unregister_port
);
2425 EXPORT_SYMBOL(uart_add_one_port
);
2426 EXPORT_SYMBOL(uart_remove_one_port
);
2428 MODULE_DESCRIPTION("Serial driver core");
2429 MODULE_LICENSE("GPL");