2 * Driver core for serial ports
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/tty.h>
25 #include <linux/tty_flip.h>
26 #include <linux/slab.h>
27 #include <linux/init.h>
28 #include <linux/console.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/device.h>
33 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
34 #include <linux/serial_core.h>
35 #include <linux/delay.h>
36 #include <linux/mutex.h>
39 #include <asm/uaccess.h>
42 * This is used to lock changes in serial line configuration.
44 static DEFINE_MUTEX(port_mutex);
47 * lockdep: port->lock is initialized in two places, but we
48 * want only one lock-class:
50 static struct lock_class_key port_lock_key;
52 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
54 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
55 struct ktermios *old_termios);
56 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
57 static void uart_change_pm(struct uart_state *state,
58 enum uart_pm_state pm_state);
60 static void uart_port_shutdown(struct tty_port *port);
62 static int uart_dcd_enabled(struct uart_port *uport)
64 return !!(uport->status & UPSTAT_DCD_ENABLE);
67 static inline struct uart_port *uart_port_ref(struct uart_state *state)
69 if (atomic_add_unless(&state->refcount, 1, 0))
70 return state->uart_port;
74 static inline void uart_port_deref(struct uart_port *uport)
76 if (uport && atomic_dec_and_test(&uport->state->refcount))
77 wake_up(&uport->state->remove_wait);
80 #define uart_port_lock(state, flags) \
82 struct uart_port *__uport = uart_port_ref(state); \
84 spin_lock_irqsave(&__uport->lock, flags); \
88 #define uart_port_unlock(uport, flags) \
90 struct uart_port *__uport = uport; \
92 spin_unlock_irqrestore(&__uport->lock, flags); \
93 uart_port_deref(__uport); \
96 static inline struct uart_port *uart_port_check(struct uart_state *state)
98 lockdep_assert_held(&state->port.mutex);
99 return state->uart_port;
103 * This routine is used by the interrupt handler to schedule processing in
104 * the software interrupt portion of the driver.
106 void uart_write_wakeup(struct uart_port *port)
108 struct uart_state *state = port->state;
110 * This means you called this function _after_ the port was
111 * closed. No cookie for you.
114 tty_wakeup(state->port.tty);
117 static void uart_stop(struct tty_struct *tty)
119 struct uart_state *state = tty->driver_data;
120 struct uart_port *port;
123 port = uart_port_lock(state, flags);
125 port->ops->stop_tx(port);
126 uart_port_unlock(port, flags);
129 static void __uart_start(struct tty_struct *tty)
131 struct uart_state *state = tty->driver_data;
132 struct uart_port *port = state->uart_port;
134 if (port && !uart_tx_stopped(port))
135 port->ops->start_tx(port);
138 static void uart_start(struct tty_struct *tty)
140 struct uart_state *state = tty->driver_data;
141 struct uart_port *port;
144 port = uart_port_lock(state, flags);
146 uart_port_unlock(port, flags);
150 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
155 spin_lock_irqsave(&port->lock, flags);
157 port->mctrl = (old & ~clear) | set;
158 if (old != port->mctrl)
159 port->ops->set_mctrl(port, port->mctrl);
160 spin_unlock_irqrestore(&port->lock, flags);
163 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
164 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
167 * Startup the port. This will be called once per open. All calls
168 * will be serialised by the per-port mutex.
170 static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
173 struct uart_port *uport = uart_port_check(state);
177 if (uport->type == PORT_UNKNOWN)
181 * Make sure the device is in D0 state.
183 uart_change_pm(state, UART_PM_STATE_ON);
186 * Initialise and allocate the transmit and temporary
189 if (!state->xmit.buf) {
190 /* This is protected by the per port mutex */
191 page = get_zeroed_page(GFP_KERNEL);
195 state->xmit.buf = (unsigned char *) page;
196 uart_circ_clear(&state->xmit);
199 retval = uport->ops->startup(uport);
201 if (uart_console(uport) && uport->cons->cflag) {
202 tty->termios.c_cflag = uport->cons->cflag;
203 uport->cons->cflag = 0;
206 * Initialise the hardware port settings.
208 uart_change_speed(tty, state, NULL);
211 * Setup the RTS and DTR signals once the
212 * port is open and ready to respond.
214 if (init_hw && C_BAUD(tty))
215 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
219 * This is to allow setserial on this port. People may want to set
220 * port/irq/type and then reconfigure the port properly if it failed
223 if (retval && capable(CAP_SYS_ADMIN))
229 static int uart_startup(struct tty_struct *tty, struct uart_state *state,
232 struct tty_port *port = &state->port;
235 if (tty_port_initialized(port))
239 * Set the TTY IO error marker - we will only clear this
240 * once we have successfully opened the port.
242 set_bit(TTY_IO_ERROR, &tty->flags);
244 retval = uart_port_startup(tty, state, init_hw);
246 tty_port_set_initialized(port, 1);
247 clear_bit(TTY_IO_ERROR, &tty->flags);
248 } else if (retval > 0)
255 * This routine will shutdown a serial port; interrupts are disabled, and
256 * DTR is dropped if the hangup on close termio flag is on. Calls to
257 * uart_shutdown are serialised by the per-port semaphore.
259 * uport == NULL if uart_port has already been removed
261 static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
263 struct uart_port *uport = uart_port_check(state);
264 struct tty_port *port = &state->port;
267 * Set the TTY IO error marker
270 set_bit(TTY_IO_ERROR, &tty->flags);
272 if (tty_port_initialized(port)) {
273 tty_port_set_initialized(port, 0);
276 * Turn off DTR and RTS early.
278 if (uport && uart_console(uport) && tty)
279 uport->cons->cflag = tty->termios.c_cflag;
281 if (!tty || C_HUPCL(tty))
282 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
284 uart_port_shutdown(port);
288 * It's possible for shutdown to be called after suspend if we get
289 * a DCD drop (hangup) at just the right time. Clear suspended bit so
290 * we don't try to resume a port that has been shutdown.
292 tty_port_set_suspended(port, 0);
295 * Free the transmit buffer page.
297 if (state->xmit.buf) {
298 free_page((unsigned long)state->xmit.buf);
299 state->xmit.buf = NULL;
304 * uart_update_timeout - update per-port FIFO timeout.
305 * @port: uart_port structure describing the port
306 * @cflag: termios cflag value
307 * @baud: speed of the port
309 * Set the port FIFO timeout value. The @cflag value should
310 * reflect the actual hardware settings.
313 uart_update_timeout(struct uart_port *port, unsigned int cflag,
318 /* byte size and parity */
319 switch (cflag & CSIZE) {
340 * The total number of bits to be transmitted in the fifo.
342 bits = bits * port->fifosize;
345 * Figure the timeout to send the above number of bits.
346 * Add .02 seconds of slop
348 port->timeout = (HZ * bits) / baud + HZ/50;
351 EXPORT_SYMBOL(uart_update_timeout);
354 * uart_get_baud_rate - return baud rate for a particular port
355 * @port: uart_port structure describing the port in question.
356 * @termios: desired termios settings.
357 * @old: old termios (or NULL)
358 * @min: minimum acceptable baud rate
359 * @max: maximum acceptable baud rate
361 * Decode the termios structure into a numeric baud rate,
362 * taking account of the magic 38400 baud rate (with spd_*
363 * flags), and mapping the %B0 rate to 9600 baud.
365 * If the new baud rate is invalid, try the old termios setting.
366 * If it's still invalid, we try 9600 baud.
368 * Update the @termios structure to reflect the baud rate
369 * we're actually going to be using. Don't do this for the case
370 * where B0 is requested ("hang up").
373 uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
374 struct ktermios *old, unsigned int min, unsigned int max)
378 unsigned int altbaud;
380 upf_t flags = port->flags & UPF_SPD_MASK;
400 for (try = 0; try < 2; try++) {
401 baud = tty_termios_baud_rate(termios);
404 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
407 if (try == 0 && baud == 38400)
411 * Special case: B0 rate.
418 if (baud >= min && baud <= max)
422 * Oops, the quotient was zero. Try again with
423 * the old baud rate if possible.
425 termios->c_cflag &= ~CBAUD;
427 baud = tty_termios_baud_rate(old);
429 tty_termios_encode_baud_rate(termios,
436 * As a last resort, if the range cannot be met then clip to
437 * the nearest chip supported rate.
441 tty_termios_encode_baud_rate(termios,
444 tty_termios_encode_baud_rate(termios,
448 /* Should never happen */
453 EXPORT_SYMBOL(uart_get_baud_rate);
456 * uart_get_divisor - return uart clock divisor
457 * @port: uart_port structure describing the port.
458 * @baud: desired baud rate
460 * Calculate the uart clock divisor for the port.
463 uart_get_divisor(struct uart_port *port, unsigned int baud)
468 * Old custom speed handling.
470 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
471 quot = port->custom_divisor;
473 quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
478 EXPORT_SYMBOL(uart_get_divisor);
480 /* Caller holds port mutex */
481 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
482 struct ktermios *old_termios)
484 struct uart_port *uport = uart_port_check(state);
485 struct ktermios *termios;
489 * If we have no tty, termios, or the port does not exist,
490 * then we can't set the parameters for this port.
492 if (!tty || uport->type == PORT_UNKNOWN)
495 termios = &tty->termios;
496 uport->ops->set_termios(uport, termios, old_termios);
499 * Set modem status enables based on termios cflag
501 spin_lock_irq(&uport->lock);
502 if (termios->c_cflag & CRTSCTS)
503 uport->status |= UPSTAT_CTS_ENABLE;
505 uport->status &= ~UPSTAT_CTS_ENABLE;
507 if (termios->c_cflag & CLOCAL)
508 uport->status &= ~UPSTAT_DCD_ENABLE;
510 uport->status |= UPSTAT_DCD_ENABLE;
512 /* reset sw-assisted CTS flow control based on (possibly) new mode */
513 hw_stopped = uport->hw_stopped;
514 uport->hw_stopped = uart_softcts_mode(uport) &&
515 !(uport->ops->get_mctrl(uport) & TIOCM_CTS);
516 if (uport->hw_stopped) {
518 uport->ops->stop_tx(uport);
523 spin_unlock_irq(&uport->lock);
526 static int uart_put_char(struct tty_struct *tty, unsigned char c)
528 struct uart_state *state = tty->driver_data;
529 struct uart_port *port;
530 struct circ_buf *circ;
538 port = uart_port_lock(state, flags);
539 if (port && uart_circ_chars_free(circ) != 0) {
540 circ->buf[circ->head] = c;
541 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
544 uart_port_unlock(port, flags);
548 static void uart_flush_chars(struct tty_struct *tty)
553 static int uart_write(struct tty_struct *tty,
554 const unsigned char *buf, int count)
556 struct uart_state *state = tty->driver_data;
557 struct uart_port *port;
558 struct circ_buf *circ;
563 * This means you called this function _after_ the port was
564 * closed. No cookie for you.
575 port = uart_port_lock(state, flags);
577 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
582 memcpy(circ->buf + circ->head, buf, c);
583 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
590 uart_port_unlock(port, flags);
594 static int uart_write_room(struct tty_struct *tty)
596 struct uart_state *state = tty->driver_data;
597 struct uart_port *port;
601 port = uart_port_lock(state, flags);
602 ret = uart_circ_chars_free(&state->xmit);
603 uart_port_unlock(port, flags);
607 static int uart_chars_in_buffer(struct tty_struct *tty)
609 struct uart_state *state = tty->driver_data;
610 struct uart_port *port;
614 port = uart_port_lock(state, flags);
615 ret = uart_circ_chars_pending(&state->xmit);
616 uart_port_unlock(port, flags);
620 static void uart_flush_buffer(struct tty_struct *tty)
622 struct uart_state *state = tty->driver_data;
623 struct uart_port *port;
627 * This means you called this function _after_ the port was
628 * closed. No cookie for you.
635 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
637 port = uart_port_lock(state, flags);
640 uart_circ_clear(&state->xmit);
641 if (port->ops->flush_buffer)
642 port->ops->flush_buffer(port);
643 uart_port_unlock(port, flags);
648 * This function is used to send a high-priority XON/XOFF character to
651 static void uart_send_xchar(struct tty_struct *tty, char ch)
653 struct uart_state *state = tty->driver_data;
654 struct uart_port *port;
657 port = uart_port_ref(state);
661 if (port->ops->send_xchar)
662 port->ops->send_xchar(port, ch);
664 spin_lock_irqsave(&port->lock, flags);
667 port->ops->start_tx(port);
668 spin_unlock_irqrestore(&port->lock, flags);
670 uart_port_deref(port);
673 static void uart_throttle(struct tty_struct *tty)
675 struct uart_state *state = tty->driver_data;
676 struct uart_port *port;
679 port = uart_port_ref(state);
684 mask |= UPSTAT_AUTOXOFF;
686 mask |= UPSTAT_AUTORTS;
688 if (port->status & mask) {
689 port->ops->throttle(port);
690 mask &= ~port->status;
693 if (mask & UPSTAT_AUTORTS)
694 uart_clear_mctrl(port, TIOCM_RTS);
696 if (mask & UPSTAT_AUTOXOFF)
697 uart_send_xchar(tty, STOP_CHAR(tty));
699 uart_port_deref(port);
702 static void uart_unthrottle(struct tty_struct *tty)
704 struct uart_state *state = tty->driver_data;
705 struct uart_port *port;
708 port = uart_port_ref(state);
713 mask |= UPSTAT_AUTOXOFF;
715 mask |= UPSTAT_AUTORTS;
717 if (port->status & mask) {
718 port->ops->unthrottle(port);
719 mask &= ~port->status;
722 if (mask & UPSTAT_AUTORTS)
723 uart_set_mctrl(port, TIOCM_RTS);
725 if (mask & UPSTAT_AUTOXOFF)
726 uart_send_xchar(tty, START_CHAR(tty));
728 uart_port_deref(port);
731 static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
733 struct uart_state *state = container_of(port, struct uart_state, port);
734 struct uart_port *uport;
737 memset(retinfo, 0, sizeof(*retinfo));
740 * Ensure the state we copy is consistent and no hardware changes
743 mutex_lock(&port->mutex);
744 uport = uart_port_check(state);
748 retinfo->type = uport->type;
749 retinfo->line = uport->line;
750 retinfo->port = uport->iobase;
751 if (HIGH_BITS_OFFSET)
752 retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
753 retinfo->irq = uport->irq;
754 retinfo->flags = uport->flags;
755 retinfo->xmit_fifo_size = uport->fifosize;
756 retinfo->baud_base = uport->uartclk / 16;
757 retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10;
758 retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
759 ASYNC_CLOSING_WAIT_NONE :
760 jiffies_to_msecs(port->closing_wait) / 10;
761 retinfo->custom_divisor = uport->custom_divisor;
762 retinfo->hub6 = uport->hub6;
763 retinfo->io_type = uport->iotype;
764 retinfo->iomem_reg_shift = uport->regshift;
765 retinfo->iomem_base = (void *)(unsigned long)uport->mapbase;
769 mutex_unlock(&port->mutex);
773 static int uart_get_info_user(struct tty_port *port,
774 struct serial_struct __user *retinfo)
776 struct serial_struct tmp;
778 if (uart_get_info(port, &tmp) < 0)
781 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
786 static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
787 struct uart_state *state,
788 struct serial_struct *new_info)
790 struct uart_port *uport = uart_port_check(state);
791 unsigned long new_port;
792 unsigned int change_irq, change_port, closing_wait;
793 unsigned int old_custom_divisor, close_delay;
794 upf_t old_flags, new_flags;
800 new_port = new_info->port;
801 if (HIGH_BITS_OFFSET)
802 new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
804 new_info->irq = irq_canonicalize(new_info->irq);
805 close_delay = msecs_to_jiffies(new_info->close_delay * 10);
806 closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
807 ASYNC_CLOSING_WAIT_NONE :
808 msecs_to_jiffies(new_info->closing_wait * 10);
811 change_irq = !(uport->flags & UPF_FIXED_PORT)
812 && new_info->irq != uport->irq;
815 * Since changing the 'type' of the port changes its resource
816 * allocations, we should treat type changes the same as
819 change_port = !(uport->flags & UPF_FIXED_PORT)
820 && (new_port != uport->iobase ||
821 (unsigned long)new_info->iomem_base != uport->mapbase ||
822 new_info->hub6 != uport->hub6 ||
823 new_info->io_type != uport->iotype ||
824 new_info->iomem_reg_shift != uport->regshift ||
825 new_info->type != uport->type);
827 old_flags = uport->flags;
828 new_flags = new_info->flags;
829 old_custom_divisor = uport->custom_divisor;
831 if (!capable(CAP_SYS_ADMIN)) {
833 if (change_irq || change_port ||
834 (new_info->baud_base != uport->uartclk / 16) ||
835 (close_delay != port->close_delay) ||
836 (closing_wait != port->closing_wait) ||
837 (new_info->xmit_fifo_size &&
838 new_info->xmit_fifo_size != uport->fifosize) ||
839 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
841 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
842 (new_flags & UPF_USR_MASK));
843 uport->custom_divisor = new_info->custom_divisor;
848 * Ask the low level driver to verify the settings.
850 if (uport->ops->verify_port)
851 retval = uport->ops->verify_port(uport, new_info);
853 if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
854 (new_info->baud_base < 9600))
860 if (change_port || change_irq) {
864 * Make sure that we are the sole user of this port.
866 if (tty_port_users(port) > 1)
870 * We need to shutdown the serial port at the old
871 * port/type/irq combination.
873 uart_shutdown(tty, state);
877 unsigned long old_iobase, old_mapbase;
878 unsigned int old_type, old_iotype, old_hub6, old_shift;
880 old_iobase = uport->iobase;
881 old_mapbase = uport->mapbase;
882 old_type = uport->type;
883 old_hub6 = uport->hub6;
884 old_iotype = uport->iotype;
885 old_shift = uport->regshift;
888 * Free and release old regions
890 if (old_type != PORT_UNKNOWN && uport->ops->release_port)
891 uport->ops->release_port(uport);
893 uport->iobase = new_port;
894 uport->type = new_info->type;
895 uport->hub6 = new_info->hub6;
896 uport->iotype = new_info->io_type;
897 uport->regshift = new_info->iomem_reg_shift;
898 uport->mapbase = (unsigned long)new_info->iomem_base;
901 * Claim and map the new regions
903 if (uport->type != PORT_UNKNOWN && uport->ops->request_port) {
904 retval = uport->ops->request_port(uport);
906 /* Always success - Jean II */
911 * If we fail to request resources for the
912 * new port, try to restore the old settings.
915 uport->iobase = old_iobase;
916 uport->type = old_type;
917 uport->hub6 = old_hub6;
918 uport->iotype = old_iotype;
919 uport->regshift = old_shift;
920 uport->mapbase = old_mapbase;
922 if (old_type != PORT_UNKNOWN) {
923 retval = uport->ops->request_port(uport);
925 * If we failed to restore the old settings,
929 uport->type = PORT_UNKNOWN;
937 /* Added to return the correct error -Ram Gupta */
943 uport->irq = new_info->irq;
944 if (!(uport->flags & UPF_FIXED_PORT))
945 uport->uartclk = new_info->baud_base * 16;
946 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
947 (new_flags & UPF_CHANGE_MASK);
948 uport->custom_divisor = new_info->custom_divisor;
949 port->close_delay = close_delay;
950 port->closing_wait = closing_wait;
951 if (new_info->xmit_fifo_size)
952 uport->fifosize = new_info->xmit_fifo_size;
953 port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
957 if (uport->type == PORT_UNKNOWN)
959 if (tty_port_initialized(port)) {
960 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
961 old_custom_divisor != uport->custom_divisor) {
963 * If they're setting up a custom divisor or speed,
964 * instead of clearing it, then bitch about it. No
965 * need to rate-limit; it's CAP_SYS_ADMIN only.
967 if (uport->flags & UPF_SPD_MASK) {
968 dev_notice(uport->dev,
969 "%s sets custom speed on %s. This is deprecated.\n",
971 tty_name(port->tty));
973 uart_change_speed(tty, state, NULL);
976 retval = uart_startup(tty, state, 1);
981 static int uart_set_info_user(struct tty_struct *tty, struct uart_state *state,
982 struct serial_struct __user *newinfo)
984 struct serial_struct new_serial;
985 struct tty_port *port = &state->port;
988 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
992 * This semaphore protects port->count. It is also
993 * very useful to prevent opens. Also, take the
994 * port configuration semaphore to make sure that a
995 * module insertion/removal doesn't change anything
998 mutex_lock(&port->mutex);
999 retval = uart_set_info(tty, port, state, &new_serial);
1000 mutex_unlock(&port->mutex);
1005 * uart_get_lsr_info - get line status register info
1006 * @tty: tty associated with the UART
1007 * @state: UART being queried
1008 * @value: returned modem value
1010 static int uart_get_lsr_info(struct tty_struct *tty,
1011 struct uart_state *state, unsigned int __user *value)
1013 struct uart_port *uport = uart_port_check(state);
1014 unsigned int result;
1016 result = uport->ops->tx_empty(uport);
1019 * If we're about to load something into the transmit
1020 * register, we'll pretend the transmitter isn't empty to
1021 * avoid a race condition (depending on when the transmit
1022 * interrupt happens).
1024 if (uport->x_char ||
1025 ((uart_circ_chars_pending(&state->xmit) > 0) &&
1026 !uart_tx_stopped(uport)))
1027 result &= ~TIOCSER_TEMT;
1029 return put_user(result, value);
1032 static int uart_tiocmget(struct tty_struct *tty)
1034 struct uart_state *state = tty->driver_data;
1035 struct tty_port *port = &state->port;
1036 struct uart_port *uport;
1039 mutex_lock(&port->mutex);
1040 uport = uart_port_check(state);
1044 if (!tty_io_error(tty)) {
1045 result = uport->mctrl;
1046 spin_lock_irq(&uport->lock);
1047 result |= uport->ops->get_mctrl(uport);
1048 spin_unlock_irq(&uport->lock);
1051 mutex_unlock(&port->mutex);
1056 uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
1058 struct uart_state *state = tty->driver_data;
1059 struct tty_port *port = &state->port;
1060 struct uart_port *uport;
1063 mutex_lock(&port->mutex);
1064 uport = uart_port_check(state);
1068 if (!tty_io_error(tty)) {
1069 uart_update_mctrl(uport, set, clear);
1073 mutex_unlock(&port->mutex);
1077 static int uart_break_ctl(struct tty_struct *tty, int break_state)
1079 struct uart_state *state = tty->driver_data;
1080 struct tty_port *port = &state->port;
1081 struct uart_port *uport;
1084 mutex_lock(&port->mutex);
1085 uport = uart_port_check(state);
1089 if (uport->type != PORT_UNKNOWN)
1090 uport->ops->break_ctl(uport, break_state);
1093 mutex_unlock(&port->mutex);
1097 static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
1099 struct tty_port *port = &state->port;
1100 struct uart_port *uport;
1103 if (!capable(CAP_SYS_ADMIN))
1107 * Take the per-port semaphore. This prevents count from
1108 * changing, and hence any extra opens of the port while
1109 * we're auto-configuring.
1111 if (mutex_lock_interruptible(&port->mutex))
1112 return -ERESTARTSYS;
1114 uport = uart_port_check(state);
1121 if (tty_port_users(port) == 1) {
1122 uart_shutdown(tty, state);
1125 * If we already have a port type configured,
1126 * we must release its resources.
1128 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
1129 uport->ops->release_port(uport);
1131 flags = UART_CONFIG_TYPE;
1132 if (uport->flags & UPF_AUTO_IRQ)
1133 flags |= UART_CONFIG_IRQ;
1136 * This will claim the ports resources if
1139 uport->ops->config_port(uport, flags);
1141 ret = uart_startup(tty, state, 1);
1144 mutex_unlock(&port->mutex);
1148 static void uart_enable_ms(struct uart_port *uport)
1151 * Force modem status interrupts on
1153 if (uport->ops->enable_ms)
1154 uport->ops->enable_ms(uport);
1158 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1159 * - mask passed in arg for lines of interest
1160 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1161 * Caller should use TIOCGICOUNT to see which one it was
1163 * FIXME: This wants extracting into a common all driver implementation
1164 * of TIOCMWAIT using tty_port.
1166 static int uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1168 struct uart_port *uport;
1169 struct tty_port *port = &state->port;
1170 DECLARE_WAITQUEUE(wait, current);
1171 struct uart_icount cprev, cnow;
1175 * note the counters on entry
1177 uport = uart_port_ref(state);
1180 spin_lock_irq(&uport->lock);
1181 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1182 uart_enable_ms(uport);
1183 spin_unlock_irq(&uport->lock);
1185 add_wait_queue(&port->delta_msr_wait, &wait);
1187 spin_lock_irq(&uport->lock);
1188 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1189 spin_unlock_irq(&uport->lock);
1191 set_current_state(TASK_INTERRUPTIBLE);
1193 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1194 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1195 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1196 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1203 /* see if a signal did it */
1204 if (signal_pending(current)) {
1211 __set_current_state(TASK_RUNNING);
1212 remove_wait_queue(&port->delta_msr_wait, &wait);
1213 uart_port_deref(uport);
1219 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1220 * Return: write counters to the user passed counter struct
1221 * NB: both 1->0 and 0->1 transitions are counted except for
1222 * RI where only 0->1 is counted.
1224 static int uart_get_icount(struct tty_struct *tty,
1225 struct serial_icounter_struct *icount)
1227 struct uart_state *state = tty->driver_data;
1228 struct uart_icount cnow;
1229 struct uart_port *uport;
1231 uport = uart_port_ref(state);
1234 spin_lock_irq(&uport->lock);
1235 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1236 spin_unlock_irq(&uport->lock);
1237 uart_port_deref(uport);
1239 icount->cts = cnow.cts;
1240 icount->dsr = cnow.dsr;
1241 icount->rng = cnow.rng;
1242 icount->dcd = cnow.dcd;
1243 icount->rx = cnow.rx;
1244 icount->tx = cnow.tx;
1245 icount->frame = cnow.frame;
1246 icount->overrun = cnow.overrun;
1247 icount->parity = cnow.parity;
1248 icount->brk = cnow.brk;
1249 icount->buf_overrun = cnow.buf_overrun;
1254 static int uart_get_rs485_config(struct uart_port *port,
1255 struct serial_rs485 __user *rs485)
1257 unsigned long flags;
1258 struct serial_rs485 aux;
1260 spin_lock_irqsave(&port->lock, flags);
1262 spin_unlock_irqrestore(&port->lock, flags);
1264 if (copy_to_user(rs485, &aux, sizeof(aux)))
1270 static int uart_set_rs485_config(struct uart_port *port,
1271 struct serial_rs485 __user *rs485_user)
1273 struct serial_rs485 rs485;
1275 unsigned long flags;
1277 if (!port->rs485_config)
1278 return -ENOIOCTLCMD;
1280 if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
1283 spin_lock_irqsave(&port->lock, flags);
1284 ret = port->rs485_config(port, &rs485);
1285 spin_unlock_irqrestore(&port->lock, flags);
1289 if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
1296 * Called via sys_ioctl. We can use spin_lock_irq() here.
1299 uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
1301 struct uart_state *state = tty->driver_data;
1302 struct tty_port *port = &state->port;
1303 struct uart_port *uport;
1304 void __user *uarg = (void __user *)arg;
1305 int ret = -ENOIOCTLCMD;
1309 * These ioctls don't rely on the hardware to be present.
1313 ret = uart_get_info_user(port, uarg);
1317 down_write(&tty->termios_rwsem);
1318 ret = uart_set_info_user(tty, state, uarg);
1319 up_write(&tty->termios_rwsem);
1323 down_write(&tty->termios_rwsem);
1324 ret = uart_do_autoconfig(tty, state);
1325 up_write(&tty->termios_rwsem);
1328 case TIOCSERGWILD: /* obsolete */
1329 case TIOCSERSWILD: /* obsolete */
1334 if (ret != -ENOIOCTLCMD)
1337 if (tty_io_error(tty)) {
1343 * The following should only be used when hardware is present.
1347 ret = uart_wait_modem_status(state, arg);
1351 if (ret != -ENOIOCTLCMD)
1354 mutex_lock(&port->mutex);
1355 uport = uart_port_check(state);
1357 if (!uport || tty_io_error(tty)) {
1363 * All these rely on hardware being present and need to be
1364 * protected against the tty being hung up.
1368 case TIOCSERGETLSR: /* Get line status register */
1369 ret = uart_get_lsr_info(tty, state, uarg);
1373 ret = uart_get_rs485_config(uport, uarg);
1377 ret = uart_set_rs485_config(uport, uarg);
1380 if (uport->ops->ioctl)
1381 ret = uport->ops->ioctl(uport, cmd, arg);
1385 mutex_unlock(&port->mutex);
1390 static void uart_set_ldisc(struct tty_struct *tty)
1392 struct uart_state *state = tty->driver_data;
1393 struct uart_port *uport;
1395 mutex_lock(&state->port.mutex);
1396 uport = uart_port_check(state);
1397 if (uport && uport->ops->set_ldisc)
1398 uport->ops->set_ldisc(uport, &tty->termios);
1399 mutex_unlock(&state->port.mutex);
1402 static void uart_set_termios(struct tty_struct *tty,
1403 struct ktermios *old_termios)
1405 struct uart_state *state = tty->driver_data;
1406 struct uart_port *uport;
1407 unsigned int cflag = tty->termios.c_cflag;
1408 unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
1409 bool sw_changed = false;
1411 mutex_lock(&state->port.mutex);
1412 uport = uart_port_check(state);
1417 * Drivers doing software flow control also need to know
1418 * about changes to these input settings.
1420 if (uport->flags & UPF_SOFT_FLOW) {
1421 iflag_mask |= IXANY|IXON|IXOFF;
1423 tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
1424 tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
1428 * These are the bits that are used to setup various
1429 * flags in the low level driver. We can ignore the Bfoo
1430 * bits in c_cflag; c_[io]speed will always be set
1431 * appropriately by set_termios() in tty_ioctl.c
1433 if ((cflag ^ old_termios->c_cflag) == 0 &&
1434 tty->termios.c_ospeed == old_termios->c_ospeed &&
1435 tty->termios.c_ispeed == old_termios->c_ispeed &&
1436 ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
1441 uart_change_speed(tty, state, old_termios);
1442 /* reload cflag from termios; port driver may have overriden flags */
1443 cflag = tty->termios.c_cflag;
1445 /* Handle transition to B0 status */
1446 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1447 uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1448 /* Handle transition away from B0 status */
1449 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1450 unsigned int mask = TIOCM_DTR;
1451 if (!(cflag & CRTSCTS) || !tty_throttled(tty))
1453 uart_set_mctrl(uport, mask);
1456 mutex_unlock(&state->port.mutex);
1460 * Calls to uart_close() are serialised via the tty_lock in
1461 * drivers/tty/tty_io.c:tty_release()
1462 * drivers/tty/tty_io.c:do_tty_hangup()
1464 static void uart_close(struct tty_struct *tty, struct file *filp)
1466 struct uart_state *state = tty->driver_data;
1467 struct tty_port *port;
1468 struct uart_port *uport;
1471 struct uart_driver *drv = tty->driver->driver_state;
1473 state = drv->state + tty->index;
1474 port = &state->port;
1475 spin_lock_irq(&port->lock);
1477 spin_unlock_irq(&port->lock);
1481 port = &state->port;
1482 pr_debug("uart_close(%d) called\n", tty->index);
1484 if (tty_port_close_start(port, tty, filp) == 0)
1487 mutex_lock(&port->mutex);
1488 uport = uart_port_check(state);
1491 * At this point, we stop accepting input. To do this, we
1492 * disable the receive line status interrupts.
1494 if (tty_port_initialized(port) &&
1495 !WARN(!uport, "detached port still initialized!\n")) {
1496 spin_lock_irq(&uport->lock);
1497 uport->ops->stop_rx(uport);
1498 spin_unlock_irq(&uport->lock);
1500 * Before we drop DTR, make sure the UART transmitter
1501 * has completely drained; this is especially
1502 * important if there is a transmit FIFO!
1504 uart_wait_until_sent(tty, uport->timeout);
1507 uart_shutdown(tty, state);
1508 tty_port_tty_set(port, NULL);
1510 spin_lock_irq(&port->lock);
1512 if (port->blocked_open) {
1513 spin_unlock_irq(&port->lock);
1514 if (port->close_delay)
1515 msleep_interruptible(jiffies_to_msecs(port->close_delay));
1516 spin_lock_irq(&port->lock);
1517 } else if (uport && !uart_console(uport)) {
1518 spin_unlock_irq(&port->lock);
1519 uart_change_pm(state, UART_PM_STATE_OFF);
1520 spin_lock_irq(&port->lock);
1522 spin_unlock_irq(&port->lock);
1523 tty_port_set_active(port, 0);
1526 * Wake up anyone trying to open this port.
1528 wake_up_interruptible(&port->open_wait);
1530 mutex_unlock(&port->mutex);
1532 tty_ldisc_flush(tty);
1536 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1538 struct uart_state *state = tty->driver_data;
1539 struct uart_port *port;
1540 unsigned long char_time, expire;
1542 port = uart_port_ref(state);
1543 if (!port || port->type == PORT_UNKNOWN || port->fifosize == 0) {
1544 uart_port_deref(port);
1549 * Set the check interval to be 1/5 of the estimated time to
1550 * send a single character, and make it at least 1. The check
1551 * interval should also be less than the timeout.
1553 * Note: we have to use pretty tight timings here to satisfy
1556 char_time = (port->timeout - HZ/50) / port->fifosize;
1557 char_time = char_time / 5;
1560 if (timeout && timeout < char_time)
1561 char_time = timeout;
1564 * If the transmitter hasn't cleared in twice the approximate
1565 * amount of time to send the entire FIFO, it probably won't
1566 * ever clear. This assumes the UART isn't doing flow
1567 * control, which is currently the case. Hence, if it ever
1568 * takes longer than port->timeout, this is probably due to a
1569 * UART bug of some kind. So, we clamp the timeout parameter at
1572 if (timeout == 0 || timeout > 2 * port->timeout)
1573 timeout = 2 * port->timeout;
1575 expire = jiffies + timeout;
1577 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1578 port->line, jiffies, expire);
1581 * Check whether the transmitter is empty every 'char_time'.
1582 * 'timeout' / 'expire' give us the maximum amount of time
1585 while (!port->ops->tx_empty(port)) {
1586 msleep_interruptible(jiffies_to_msecs(char_time));
1587 if (signal_pending(current))
1589 if (time_after(jiffies, expire))
1592 uart_port_deref(port);
1596 * Calls to uart_hangup() are serialised by the tty_lock in
1597 * drivers/tty/tty_io.c:do_tty_hangup()
1598 * This runs from a workqueue and can sleep for a _short_ time only.
1600 static void uart_hangup(struct tty_struct *tty)
1602 struct uart_state *state = tty->driver_data;
1603 struct tty_port *port = &state->port;
1604 struct uart_port *uport;
1605 unsigned long flags;
1607 pr_debug("uart_hangup(%d)\n", tty->index);
1609 mutex_lock(&port->mutex);
1610 uport = uart_port_check(state);
1611 WARN(!uport, "hangup of detached port!\n");
1613 if (tty_port_active(port)) {
1614 uart_flush_buffer(tty);
1615 uart_shutdown(tty, state);
1616 spin_lock_irqsave(&port->lock, flags);
1618 spin_unlock_irqrestore(&port->lock, flags);
1619 tty_port_set_active(port, 0);
1620 tty_port_tty_set(port, NULL);
1621 if (uport && !uart_console(uport))
1622 uart_change_pm(state, UART_PM_STATE_OFF);
1623 wake_up_interruptible(&port->open_wait);
1624 wake_up_interruptible(&port->delta_msr_wait);
1626 mutex_unlock(&port->mutex);
1629 /* uport == NULL if uart_port has already been removed */
1630 static void uart_port_shutdown(struct tty_port *port)
1632 struct uart_state *state = container_of(port, struct uart_state, port);
1633 struct uart_port *uport = uart_port_check(state);
1636 * clear delta_msr_wait queue to avoid mem leaks: we may free
1637 * the irq here so the queue might never be woken up. Note
1638 * that we won't end up waiting on delta_msr_wait again since
1639 * any outstanding file descriptors should be pointing at
1640 * hung_up_tty_fops now.
1642 wake_up_interruptible(&port->delta_msr_wait);
1645 * Free the IRQ and disable the port.
1648 uport->ops->shutdown(uport);
1651 * Ensure that the IRQ handler isn't running on another CPU.
1654 synchronize_irq(uport->irq);
1657 static int uart_carrier_raised(struct tty_port *port)
1659 struct uart_state *state = container_of(port, struct uart_state, port);
1660 struct uart_port *uport;
1663 uport = uart_port_ref(state);
1665 * Should never observe uport == NULL since checks for hangup should
1666 * abort the tty_port_block_til_ready() loop before checking for carrier
1667 * raised -- but report carrier raised if it does anyway so open will
1668 * continue and not sleep
1670 if (WARN_ON(!uport))
1672 spin_lock_irq(&uport->lock);
1673 uart_enable_ms(uport);
1674 mctrl = uport->ops->get_mctrl(uport);
1675 spin_unlock_irq(&uport->lock);
1676 uart_port_deref(uport);
1677 if (mctrl & TIOCM_CAR)
1682 static void uart_dtr_rts(struct tty_port *port, int onoff)
1684 struct uart_state *state = container_of(port, struct uart_state, port);
1685 struct uart_port *uport;
1687 uport = uart_port_ref(state);
1692 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1694 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1696 uart_port_deref(uport);
1700 * Calls to uart_open are serialised by the tty_lock in
1701 * drivers/tty/tty_io.c:tty_open()
1702 * Note that if this fails, then uart_close() _will_ be called.
1704 * In time, we want to scrap the "opening nonpresent ports"
1705 * behaviour and implement an alternative way for setserial
1706 * to set base addresses/ports/types. This will allow us to
1707 * get rid of a certain amount of extra tests.
1709 static int uart_open(struct tty_struct *tty, struct file *filp)
1711 struct uart_driver *drv = tty->driver->driver_state;
1712 int retval, line = tty->index;
1713 struct uart_state *state = drv->state + line;
1714 struct tty_port *port = &state->port;
1715 struct uart_port *uport;
1717 pr_debug("uart_open(%d) called\n", line);
1719 spin_lock_irq(&port->lock);
1721 spin_unlock_irq(&port->lock);
1724 * We take the semaphore here to guarantee that we won't be re-entered
1725 * while allocating the state structure, or while we request any IRQs
1726 * that the driver may need. This also has the nice side-effect that
1727 * it delays the action of uart_hangup, so we can guarantee that
1728 * state->port.tty will always contain something reasonable.
1730 if (mutex_lock_interruptible(&port->mutex)) {
1731 retval = -ERESTARTSYS;
1735 uport = uart_port_check(state);
1736 if (!uport || uport->flags & UPF_DEAD) {
1741 tty->driver_data = state;
1742 uport->state = state;
1743 port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
1744 tty_port_tty_set(port, tty);
1747 * Start up the serial port.
1749 retval = uart_startup(tty, state, 0);
1752 * If we succeeded, wait until the port is ready.
1755 mutex_unlock(&port->mutex);
1757 retval = tty_port_block_til_ready(port, tty, filp);
1762 static const char *uart_type(struct uart_port *port)
1764 const char *str = NULL;
1766 if (port->ops->type)
1767 str = port->ops->type(port);
1775 #ifdef CONFIG_PROC_FS
1777 static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1779 struct uart_state *state = drv->state + i;
1780 struct tty_port *port = &state->port;
1781 enum uart_pm_state pm_state;
1782 struct uart_port *uport;
1784 unsigned int status;
1787 mutex_lock(&port->mutex);
1788 uport = uart_port_check(state);
1792 mmio = uport->iotype >= UPIO_MEM;
1793 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1794 uport->line, uart_type(uport),
1795 mmio ? "mmio:0x" : "port:",
1796 mmio ? (unsigned long long)uport->mapbase
1797 : (unsigned long long)uport->iobase,
1800 if (uport->type == PORT_UNKNOWN) {
1805 if (capable(CAP_SYS_ADMIN)) {
1806 pm_state = state->pm_state;
1807 if (pm_state != UART_PM_STATE_ON)
1808 uart_change_pm(state, UART_PM_STATE_ON);
1809 spin_lock_irq(&uport->lock);
1810 status = uport->ops->get_mctrl(uport);
1811 spin_unlock_irq(&uport->lock);
1812 if (pm_state != UART_PM_STATE_ON)
1813 uart_change_pm(state, pm_state);
1815 seq_printf(m, " tx:%d rx:%d",
1816 uport->icount.tx, uport->icount.rx);
1817 if (uport->icount.frame)
1818 seq_printf(m, " fe:%d", uport->icount.frame);
1819 if (uport->icount.parity)
1820 seq_printf(m, " pe:%d", uport->icount.parity);
1821 if (uport->icount.brk)
1822 seq_printf(m, " brk:%d", uport->icount.brk);
1823 if (uport->icount.overrun)
1824 seq_printf(m, " oe:%d", uport->icount.overrun);
1826 #define INFOBIT(bit, str) \
1827 if (uport->mctrl & (bit)) \
1828 strncat(stat_buf, (str), sizeof(stat_buf) - \
1829 strlen(stat_buf) - 2)
1830 #define STATBIT(bit, str) \
1831 if (status & (bit)) \
1832 strncat(stat_buf, (str), sizeof(stat_buf) - \
1833 strlen(stat_buf) - 2)
1837 INFOBIT(TIOCM_RTS, "|RTS");
1838 STATBIT(TIOCM_CTS, "|CTS");
1839 INFOBIT(TIOCM_DTR, "|DTR");
1840 STATBIT(TIOCM_DSR, "|DSR");
1841 STATBIT(TIOCM_CAR, "|CD");
1842 STATBIT(TIOCM_RNG, "|RI");
1846 seq_puts(m, stat_buf);
1852 mutex_unlock(&port->mutex);
1855 static int uart_proc_show(struct seq_file *m, void *v)
1857 struct tty_driver *ttydrv = m->private;
1858 struct uart_driver *drv = ttydrv->driver_state;
1861 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
1862 for (i = 0; i < drv->nr; i++)
1863 uart_line_info(m, drv, i);
1867 static int uart_proc_open(struct inode *inode, struct file *file)
1869 return single_open(file, uart_proc_show, PDE_DATA(inode));
1872 static const struct file_operations uart_proc_fops = {
1873 .owner = THIS_MODULE,
1874 .open = uart_proc_open,
1876 .llseek = seq_lseek,
1877 .release = single_release,
1881 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1883 * uart_console_write - write a console message to a serial port
1884 * @port: the port to write the message
1885 * @s: array of characters
1886 * @count: number of characters in string to write
1887 * @putchar: function to write character to port
1889 void uart_console_write(struct uart_port *port, const char *s,
1891 void (*putchar)(struct uart_port *, int))
1895 for (i = 0; i < count; i++, s++) {
1897 putchar(port, '\r');
1901 EXPORT_SYMBOL_GPL(uart_console_write);
1904 * Check whether an invalid uart number has been specified, and
1905 * if so, search for the first available port that does have
1908 struct uart_port * __init
1909 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1911 int idx = co->index;
1913 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1914 ports[idx].membase == NULL))
1915 for (idx = 0; idx < nr; idx++)
1916 if (ports[idx].iobase != 0 ||
1917 ports[idx].membase != NULL)
1926 * uart_parse_earlycon - Parse earlycon options
1927 * @p: ptr to 2nd field (ie., just beyond '<name>,')
1928 * @iotype: ptr for decoded iotype (out)
1929 * @addr: ptr for decoded mapbase/iobase (out)
1930 * @options: ptr for <options> field; NULL if not present (out)
1932 * Decodes earlycon kernel command line parameters of the form
1933 * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
1934 * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
1937 * earlycon=<name>,0x<addr>,<options>
1938 * console=<name>,0x<addr>,<options>
1939 * is also accepted; the returned @iotype will be UPIO_MEM.
1941 * Returns 0 on success or -EINVAL on failure
1943 int uart_parse_earlycon(char *p, unsigned char *iotype, unsigned long *addr,
1946 if (strncmp(p, "mmio,", 5) == 0) {
1949 } else if (strncmp(p, "mmio16,", 7) == 0) {
1950 *iotype = UPIO_MEM16;
1952 } else if (strncmp(p, "mmio32,", 7) == 0) {
1953 *iotype = UPIO_MEM32;
1955 } else if (strncmp(p, "mmio32be,", 9) == 0) {
1956 *iotype = UPIO_MEM32BE;
1958 } else if (strncmp(p, "mmio32native,", 13) == 0) {
1959 *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
1960 UPIO_MEM32BE : UPIO_MEM32;
1962 } else if (strncmp(p, "io,", 3) == 0) {
1963 *iotype = UPIO_PORT;
1965 } else if (strncmp(p, "0x", 2) == 0) {
1971 *addr = simple_strtoul(p, NULL, 0);
1979 EXPORT_SYMBOL_GPL(uart_parse_earlycon);
1982 * uart_parse_options - Parse serial port baud/parity/bits/flow control.
1983 * @options: pointer to option string
1984 * @baud: pointer to an 'int' variable for the baud rate.
1985 * @parity: pointer to an 'int' variable for the parity.
1986 * @bits: pointer to an 'int' variable for the number of data bits.
1987 * @flow: pointer to an 'int' variable for the flow control character.
1989 * uart_parse_options decodes a string containing the serial console
1990 * options. The format of the string is <baud><parity><bits><flow>,
1994 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1998 *baud = simple_strtoul(s, NULL, 10);
1999 while (*s >= '0' && *s <= '9')
2008 EXPORT_SYMBOL_GPL(uart_parse_options);
2011 * uart_set_options - setup the serial console parameters
2012 * @port: pointer to the serial ports uart_port structure
2013 * @co: console pointer
2015 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
2016 * @bits: number of data bits
2017 * @flow: flow control character - 'r' (rts)
2020 uart_set_options(struct uart_port *port, struct console *co,
2021 int baud, int parity, int bits, int flow)
2023 struct ktermios termios;
2024 static struct ktermios dummy;
2027 * Ensure that the serial console lock is initialised
2029 * If this port is a console, then the spinlock is already
2032 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
2033 spin_lock_init(&port->lock);
2034 lockdep_set_class(&port->lock, &port_lock_key);
2037 memset(&termios, 0, sizeof(struct ktermios));
2039 termios.c_cflag |= CREAD | HUPCL | CLOCAL;
2040 tty_termios_encode_baud_rate(&termios, baud, baud);
2043 termios.c_cflag |= CS7;
2045 termios.c_cflag |= CS8;
2049 termios.c_cflag |= PARODD;
2052 termios.c_cflag |= PARENB;
2057 termios.c_cflag |= CRTSCTS;
2060 * some uarts on other side don't support no flow control.
2061 * So we set * DTR in host uart to make them happy
2063 port->mctrl |= TIOCM_DTR;
2065 port->ops->set_termios(port, &termios, &dummy);
2067 * Allow the setting of the UART parameters with a NULL console
2071 co->cflag = termios.c_cflag;
2075 EXPORT_SYMBOL_GPL(uart_set_options);
2076 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
2079 * uart_change_pm - set power state of the port
2081 * @state: port descriptor
2082 * @pm_state: new state
2084 * Locking: port->mutex has to be held
2086 static void uart_change_pm(struct uart_state *state,
2087 enum uart_pm_state pm_state)
2089 struct uart_port *port = uart_port_check(state);
2091 if (state->pm_state != pm_state) {
2092 if (port && port->ops->pm)
2093 port->ops->pm(port, pm_state, state->pm_state);
2094 state->pm_state = pm_state;
2099 struct uart_port *port;
2100 struct uart_driver *driver;
2103 static int serial_match_port(struct device *dev, void *data)
2105 struct uart_match *match = data;
2106 struct tty_driver *tty_drv = match->driver->tty_driver;
2107 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
2110 return dev->devt == devt; /* Actually, only one tty per port */
2113 int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
2115 struct uart_state *state = drv->state + uport->line;
2116 struct tty_port *port = &state->port;
2117 struct device *tty_dev;
2118 struct uart_match match = {uport, drv};
2120 mutex_lock(&port->mutex);
2122 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2123 if (device_may_wakeup(tty_dev)) {
2124 if (!enable_irq_wake(uport->irq))
2125 uport->irq_wake = 1;
2126 put_device(tty_dev);
2127 mutex_unlock(&port->mutex);
2130 put_device(tty_dev);
2132 /* Nothing to do if the console is not suspending */
2133 if (!console_suspend_enabled && uart_console(uport))
2136 uport->suspended = 1;
2138 if (tty_port_initialized(port)) {
2139 const struct uart_ops *ops = uport->ops;
2142 tty_port_set_suspended(port, 1);
2143 tty_port_set_initialized(port, 0);
2145 spin_lock_irq(&uport->lock);
2146 ops->stop_tx(uport);
2147 ops->set_mctrl(uport, 0);
2148 ops->stop_rx(uport);
2149 spin_unlock_irq(&uport->lock);
2152 * Wait for the transmitter to empty.
2154 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
2157 dev_err(uport->dev, "%s%d: Unable to drain transmitter\n",
2159 drv->tty_driver->name_base + uport->line);
2161 ops->shutdown(uport);
2165 * Disable the console device before suspending.
2167 if (uart_console(uport))
2168 console_stop(uport->cons);
2170 uart_change_pm(state, UART_PM_STATE_OFF);
2172 mutex_unlock(&port->mutex);
2177 int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2179 struct uart_state *state = drv->state + uport->line;
2180 struct tty_port *port = &state->port;
2181 struct device *tty_dev;
2182 struct uart_match match = {uport, drv};
2183 struct ktermios termios;
2185 mutex_lock(&port->mutex);
2187 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2188 if (!uport->suspended && device_may_wakeup(tty_dev)) {
2189 if (uport->irq_wake) {
2190 disable_irq_wake(uport->irq);
2191 uport->irq_wake = 0;
2193 put_device(tty_dev);
2194 mutex_unlock(&port->mutex);
2197 put_device(tty_dev);
2198 uport->suspended = 0;
2201 * Re-enable the console device after suspending.
2203 if (uart_console(uport)) {
2205 * First try to use the console cflag setting.
2207 memset(&termios, 0, sizeof(struct ktermios));
2208 termios.c_cflag = uport->cons->cflag;
2211 * If that's unset, use the tty termios setting.
2213 if (port->tty && termios.c_cflag == 0)
2214 termios = port->tty->termios;
2216 if (console_suspend_enabled)
2217 uart_change_pm(state, UART_PM_STATE_ON);
2218 uport->ops->set_termios(uport, &termios, NULL);
2219 if (console_suspend_enabled)
2220 console_start(uport->cons);
2223 if (tty_port_suspended(port)) {
2224 const struct uart_ops *ops = uport->ops;
2227 uart_change_pm(state, UART_PM_STATE_ON);
2228 spin_lock_irq(&uport->lock);
2229 ops->set_mctrl(uport, 0);
2230 spin_unlock_irq(&uport->lock);
2231 if (console_suspend_enabled || !uart_console(uport)) {
2232 /* Protected by port mutex for now */
2233 struct tty_struct *tty = port->tty;
2234 ret = ops->startup(uport);
2237 uart_change_speed(tty, state, NULL);
2238 spin_lock_irq(&uport->lock);
2239 ops->set_mctrl(uport, uport->mctrl);
2240 ops->start_tx(uport);
2241 spin_unlock_irq(&uport->lock);
2242 tty_port_set_initialized(port, 1);
2245 * Failed to resume - maybe hardware went away?
2246 * Clear the "initialized" flag so we won't try
2247 * to call the low level drivers shutdown method.
2249 uart_shutdown(tty, state);
2253 tty_port_set_suspended(port, 0);
2256 mutex_unlock(&port->mutex);
2262 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2266 switch (port->iotype) {
2268 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2271 snprintf(address, sizeof(address),
2272 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2280 snprintf(address, sizeof(address),
2281 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2284 strlcpy(address, "*unknown*", sizeof(address));
2288 printk(KERN_INFO "%s%s%s%d at %s (irq = %d, base_baud = %d) is a %s\n",
2289 port->dev ? dev_name(port->dev) : "",
2290 port->dev ? ": " : "",
2292 drv->tty_driver->name_base + port->line,
2293 address, port->irq, port->uartclk / 16, uart_type(port));
2297 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2298 struct uart_port *port)
2303 * If there isn't a port here, don't do anything further.
2305 if (!port->iobase && !port->mapbase && !port->membase)
2309 * Now do the auto configuration stuff. Note that config_port
2310 * is expected to claim the resources and map the port for us.
2313 if (port->flags & UPF_AUTO_IRQ)
2314 flags |= UART_CONFIG_IRQ;
2315 if (port->flags & UPF_BOOT_AUTOCONF) {
2316 if (!(port->flags & UPF_FIXED_TYPE)) {
2317 port->type = PORT_UNKNOWN;
2318 flags |= UART_CONFIG_TYPE;
2320 port->ops->config_port(port, flags);
2323 if (port->type != PORT_UNKNOWN) {
2324 unsigned long flags;
2326 uart_report_port(drv, port);
2328 /* Power up port for set_mctrl() */
2329 uart_change_pm(state, UART_PM_STATE_ON);
2332 * Ensure that the modem control lines are de-activated.
2333 * keep the DTR setting that is set in uart_set_options()
2334 * We probably don't need a spinlock around this, but
2336 spin_lock_irqsave(&port->lock, flags);
2337 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2338 spin_unlock_irqrestore(&port->lock, flags);
2341 * If this driver supports console, and it hasn't been
2342 * successfully registered yet, try to re-register it.
2343 * It may be that the port was not available.
2345 if (port->cons && !(port->cons->flags & CON_ENABLED))
2346 register_console(port->cons);
2349 * Power down all ports by default, except the
2350 * console if we have one.
2352 if (!uart_console(port))
2353 uart_change_pm(state, UART_PM_STATE_OFF);
2357 #ifdef CONFIG_CONSOLE_POLL
2359 static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2361 struct uart_driver *drv = driver->driver_state;
2362 struct uart_state *state = drv->state + line;
2363 struct tty_port *tport;
2364 struct uart_port *port;
2374 tport = &state->port;
2375 mutex_lock(&tport->mutex);
2377 port = uart_port_check(state);
2378 if (!port || !(port->ops->poll_get_char && port->ops->poll_put_char)) {
2383 if (port->ops->poll_init) {
2385 * We don't set initialized as we only initialized the hw,
2386 * e.g. state->xmit is still uninitialized.
2388 if (!tty_port_initialized(tport))
2389 ret = port->ops->poll_init(port);
2392 if (!ret && options) {
2393 uart_parse_options(options, &baud, &parity, &bits, &flow);
2394 ret = uart_set_options(port, NULL, baud, parity, bits, flow);
2397 mutex_unlock(&tport->mutex);
2401 static int uart_poll_get_char(struct tty_driver *driver, int line)
2403 struct uart_driver *drv = driver->driver_state;
2404 struct uart_state *state = drv->state + line;
2405 struct uart_port *port;
2409 port = uart_port_ref(state);
2411 ret = port->ops->poll_get_char(port);
2412 uart_port_deref(port);
2417 static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2419 struct uart_driver *drv = driver->driver_state;
2420 struct uart_state *state = drv->state + line;
2421 struct uart_port *port;
2426 port = uart_port_ref(state);
2431 port->ops->poll_put_char(port, '\r');
2432 port->ops->poll_put_char(port, ch);
2433 uart_port_deref(port);
2437 static const struct tty_operations uart_ops = {
2439 .close = uart_close,
2440 .write = uart_write,
2441 .put_char = uart_put_char,
2442 .flush_chars = uart_flush_chars,
2443 .write_room = uart_write_room,
2444 .chars_in_buffer= uart_chars_in_buffer,
2445 .flush_buffer = uart_flush_buffer,
2446 .ioctl = uart_ioctl,
2447 .throttle = uart_throttle,
2448 .unthrottle = uart_unthrottle,
2449 .send_xchar = uart_send_xchar,
2450 .set_termios = uart_set_termios,
2451 .set_ldisc = uart_set_ldisc,
2453 .start = uart_start,
2454 .hangup = uart_hangup,
2455 .break_ctl = uart_break_ctl,
2456 .wait_until_sent= uart_wait_until_sent,
2457 #ifdef CONFIG_PROC_FS
2458 .proc_fops = &uart_proc_fops,
2460 .tiocmget = uart_tiocmget,
2461 .tiocmset = uart_tiocmset,
2462 .get_icount = uart_get_icount,
2463 #ifdef CONFIG_CONSOLE_POLL
2464 .poll_init = uart_poll_init,
2465 .poll_get_char = uart_poll_get_char,
2466 .poll_put_char = uart_poll_put_char,
2470 static const struct tty_port_operations uart_port_ops = {
2471 .carrier_raised = uart_carrier_raised,
2472 .dtr_rts = uart_dtr_rts,
2476 * uart_register_driver - register a driver with the uart core layer
2477 * @drv: low level driver structure
2479 * Register a uart driver with the core driver. We in turn register
2480 * with the tty layer, and initialise the core driver per-port state.
2482 * We have a proc file in /proc/tty/driver which is named after the
2485 * drv->port should be NULL, and the per-port structures should be
2486 * registered using uart_add_one_port after this call has succeeded.
2488 int uart_register_driver(struct uart_driver *drv)
2490 struct tty_driver *normal;
2496 * Maybe we should be using a slab cache for this, especially if
2497 * we have a large number of ports to handle.
2499 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2503 normal = alloc_tty_driver(drv->nr);
2507 drv->tty_driver = normal;
2509 normal->driver_name = drv->driver_name;
2510 normal->name = drv->dev_name;
2511 normal->major = drv->major;
2512 normal->minor_start = drv->minor;
2513 normal->type = TTY_DRIVER_TYPE_SERIAL;
2514 normal->subtype = SERIAL_TYPE_NORMAL;
2515 normal->init_termios = tty_std_termios;
2516 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2517 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2518 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2519 normal->driver_state = drv;
2520 tty_set_operations(normal, &uart_ops);
2523 * Initialise the UART state(s).
2525 for (i = 0; i < drv->nr; i++) {
2526 struct uart_state *state = drv->state + i;
2527 struct tty_port *port = &state->port;
2529 tty_port_init(port);
2530 port->ops = &uart_port_ops;
2533 retval = tty_register_driver(normal);
2537 for (i = 0; i < drv->nr; i++)
2538 tty_port_destroy(&drv->state[i].port);
2539 put_tty_driver(normal);
2547 * uart_unregister_driver - remove a driver from the uart core layer
2548 * @drv: low level driver structure
2550 * Remove all references to a driver from the core driver. The low
2551 * level driver must have removed all its ports via the
2552 * uart_remove_one_port() if it registered them with uart_add_one_port().
2553 * (ie, drv->port == NULL)
2555 void uart_unregister_driver(struct uart_driver *drv)
2557 struct tty_driver *p = drv->tty_driver;
2560 tty_unregister_driver(p);
2562 for (i = 0; i < drv->nr; i++)
2563 tty_port_destroy(&drv->state[i].port);
2566 drv->tty_driver = NULL;
2569 struct tty_driver *uart_console_device(struct console *co, int *index)
2571 struct uart_driver *p = co->data;
2573 return p->tty_driver;
2576 static ssize_t uart_get_attr_uartclk(struct device *dev,
2577 struct device_attribute *attr, char *buf)
2579 struct serial_struct tmp;
2580 struct tty_port *port = dev_get_drvdata(dev);
2582 uart_get_info(port, &tmp);
2583 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16);
2586 static ssize_t uart_get_attr_type(struct device *dev,
2587 struct device_attribute *attr, char *buf)
2589 struct serial_struct tmp;
2590 struct tty_port *port = dev_get_drvdata(dev);
2592 uart_get_info(port, &tmp);
2593 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type);
2595 static ssize_t uart_get_attr_line(struct device *dev,
2596 struct device_attribute *attr, char *buf)
2598 struct serial_struct tmp;
2599 struct tty_port *port = dev_get_drvdata(dev);
2601 uart_get_info(port, &tmp);
2602 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line);
2605 static ssize_t uart_get_attr_port(struct device *dev,
2606 struct device_attribute *attr, char *buf)
2608 struct serial_struct tmp;
2609 struct tty_port *port = dev_get_drvdata(dev);
2610 unsigned long ioaddr;
2612 uart_get_info(port, &tmp);
2614 if (HIGH_BITS_OFFSET)
2615 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
2616 return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr);
2619 static ssize_t uart_get_attr_irq(struct device *dev,
2620 struct device_attribute *attr, char *buf)
2622 struct serial_struct tmp;
2623 struct tty_port *port = dev_get_drvdata(dev);
2625 uart_get_info(port, &tmp);
2626 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq);
2629 static ssize_t uart_get_attr_flags(struct device *dev,
2630 struct device_attribute *attr, char *buf)
2632 struct serial_struct tmp;
2633 struct tty_port *port = dev_get_drvdata(dev);
2635 uart_get_info(port, &tmp);
2636 return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags);
2639 static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev,
2640 struct device_attribute *attr, char *buf)
2642 struct serial_struct tmp;
2643 struct tty_port *port = dev_get_drvdata(dev);
2645 uart_get_info(port, &tmp);
2646 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size);
2650 static ssize_t uart_get_attr_close_delay(struct device *dev,
2651 struct device_attribute *attr, char *buf)
2653 struct serial_struct tmp;
2654 struct tty_port *port = dev_get_drvdata(dev);
2656 uart_get_info(port, &tmp);
2657 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay);
2661 static ssize_t uart_get_attr_closing_wait(struct device *dev,
2662 struct device_attribute *attr, char *buf)
2664 struct serial_struct tmp;
2665 struct tty_port *port = dev_get_drvdata(dev);
2667 uart_get_info(port, &tmp);
2668 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait);
2671 static ssize_t uart_get_attr_custom_divisor(struct device *dev,
2672 struct device_attribute *attr, char *buf)
2674 struct serial_struct tmp;
2675 struct tty_port *port = dev_get_drvdata(dev);
2677 uart_get_info(port, &tmp);
2678 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor);
2681 static ssize_t uart_get_attr_io_type(struct device *dev,
2682 struct device_attribute *attr, char *buf)
2684 struct serial_struct tmp;
2685 struct tty_port *port = dev_get_drvdata(dev);
2687 uart_get_info(port, &tmp);
2688 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type);
2691 static ssize_t uart_get_attr_iomem_base(struct device *dev,
2692 struct device_attribute *attr, char *buf)
2694 struct serial_struct tmp;
2695 struct tty_port *port = dev_get_drvdata(dev);
2697 uart_get_info(port, &tmp);
2698 return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base);
2701 static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev,
2702 struct device_attribute *attr, char *buf)
2704 struct serial_struct tmp;
2705 struct tty_port *port = dev_get_drvdata(dev);
2707 uart_get_info(port, &tmp);
2708 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift);
2711 static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL);
2712 static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL);
2713 static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL);
2714 static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL);
2715 static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL);
2716 static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL);
2717 static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL);
2718 static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL);
2719 static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL);
2720 static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL);
2721 static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL);
2722 static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL);
2723 static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL);
2725 static struct attribute *tty_dev_attrs[] = {
2726 &dev_attr_type.attr,
2727 &dev_attr_line.attr,
2728 &dev_attr_port.attr,
2730 &dev_attr_flags.attr,
2731 &dev_attr_xmit_fifo_size.attr,
2732 &dev_attr_uartclk.attr,
2733 &dev_attr_close_delay.attr,
2734 &dev_attr_closing_wait.attr,
2735 &dev_attr_custom_divisor.attr,
2736 &dev_attr_io_type.attr,
2737 &dev_attr_iomem_base.attr,
2738 &dev_attr_iomem_reg_shift.attr,
2742 static const struct attribute_group tty_dev_attr_group = {
2743 .attrs = tty_dev_attrs,
2747 * uart_add_one_port - attach a driver-defined port structure
2748 * @drv: pointer to the uart low level driver structure for this port
2749 * @uport: uart port structure to use for this port.
2751 * This allows the driver to register its own uart_port structure
2752 * with the core driver. The main purpose is to allow the low
2753 * level uart drivers to expand uart_port, rather than having yet
2754 * more levels of structures.
2756 int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
2758 struct uart_state *state;
2759 struct tty_port *port;
2761 struct device *tty_dev;
2764 BUG_ON(in_interrupt());
2766 if (uport->line >= drv->nr)
2769 state = drv->state + uport->line;
2770 port = &state->port;
2772 mutex_lock(&port_mutex);
2773 mutex_lock(&port->mutex);
2774 if (state->uart_port) {
2779 /* Link the port to the driver state table and vice versa */
2780 atomic_set(&state->refcount, 1);
2781 init_waitqueue_head(&state->remove_wait);
2782 state->uart_port = uport;
2783 uport->state = state;
2785 state->pm_state = UART_PM_STATE_UNDEFINED;
2786 uport->cons = drv->cons;
2787 uport->minor = drv->tty_driver->minor_start + uport->line;
2790 * If this port is a console, then the spinlock is already
2793 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2794 spin_lock_init(&uport->lock);
2795 lockdep_set_class(&uport->lock, &port_lock_key);
2797 if (uport->cons && uport->dev)
2798 of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
2800 uart_configure_port(drv, state, uport);
2803 if (uport->attr_group)
2806 uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
2808 if (!uport->tty_groups) {
2812 uport->tty_groups[0] = &tty_dev_attr_group;
2813 if (uport->attr_group)
2814 uport->tty_groups[1] = uport->attr_group;
2817 * Register the port whether it's detected or not. This allows
2818 * setserial to be used to alter this port's parameters.
2820 tty_dev = tty_port_register_device_attr(port, drv->tty_driver,
2821 uport->line, uport->dev, port, uport->tty_groups);
2822 if (likely(!IS_ERR(tty_dev))) {
2823 device_set_wakeup_capable(tty_dev, 1);
2825 dev_err(uport->dev, "Cannot register tty device on line %d\n",
2830 * Ensure UPF_DEAD is not set.
2832 uport->flags &= ~UPF_DEAD;
2835 mutex_unlock(&port->mutex);
2836 mutex_unlock(&port_mutex);
2842 * uart_remove_one_port - detach a driver defined port structure
2843 * @drv: pointer to the uart low level driver structure for this port
2844 * @uport: uart port structure for this port
2846 * This unhooks (and hangs up) the specified port structure from the
2847 * core driver. No further calls will be made to the low-level code
2850 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
2852 struct uart_state *state = drv->state + uport->line;
2853 struct tty_port *port = &state->port;
2854 struct uart_port *uart_port;
2855 struct tty_struct *tty;
2858 BUG_ON(in_interrupt());
2860 mutex_lock(&port_mutex);
2863 * Mark the port "dead" - this prevents any opens from
2864 * succeeding while we shut down the port.
2866 mutex_lock(&port->mutex);
2867 uart_port = uart_port_check(state);
2868 if (uart_port != uport)
2869 dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
2873 mutex_unlock(&port->mutex);
2877 uport->flags |= UPF_DEAD;
2878 mutex_unlock(&port->mutex);
2881 * Remove the devices from the tty layer
2883 tty_unregister_device(drv->tty_driver, uport->line);
2885 tty = tty_port_tty_get(port);
2887 tty_vhangup(port->tty);
2892 * If the port is used as a console, unregister it
2894 if (uart_console(uport))
2895 unregister_console(uport->cons);
2898 * Free the port IO and memory resources, if any.
2900 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
2901 uport->ops->release_port(uport);
2902 kfree(uport->tty_groups);
2905 * Indicate that there isn't a port here anymore.
2907 uport->type = PORT_UNKNOWN;
2909 mutex_lock(&port->mutex);
2910 WARN_ON(atomic_dec_return(&state->refcount) < 0);
2911 wait_event(state->remove_wait, !atomic_read(&state->refcount));
2912 state->uart_port = NULL;
2913 mutex_unlock(&port->mutex);
2915 mutex_unlock(&port_mutex);
2921 * Are the two ports equivalent?
2923 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2925 if (port1->iotype != port2->iotype)
2928 switch (port1->iotype) {
2930 return (port1->iobase == port2->iobase);
2932 return (port1->iobase == port2->iobase) &&
2933 (port1->hub6 == port2->hub6);
2940 return (port1->mapbase == port2->mapbase);
2944 EXPORT_SYMBOL(uart_match_port);
2947 * uart_handle_dcd_change - handle a change of carrier detect state
2948 * @uport: uart_port structure for the open port
2949 * @status: new carrier detect status, nonzero if active
2951 * Caller must hold uport->lock
2953 void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
2955 struct tty_port *port = &uport->state->port;
2956 struct tty_struct *tty = port->tty;
2957 struct tty_ldisc *ld;
2959 lockdep_assert_held_once(&uport->lock);
2962 ld = tty_ldisc_ref(tty);
2964 if (ld->ops->dcd_change)
2965 ld->ops->dcd_change(tty, status);
2966 tty_ldisc_deref(ld);
2970 uport->icount.dcd++;
2972 if (uart_dcd_enabled(uport)) {
2974 wake_up_interruptible(&port->open_wait);
2979 EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
2982 * uart_handle_cts_change - handle a change of clear-to-send state
2983 * @uport: uart_port structure for the open port
2984 * @status: new clear to send status, nonzero if active
2986 * Caller must hold uport->lock
2988 void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
2990 lockdep_assert_held_once(&uport->lock);
2992 uport->icount.cts++;
2994 if (uart_softcts_mode(uport)) {
2995 if (uport->hw_stopped) {
2997 uport->hw_stopped = 0;
2998 uport->ops->start_tx(uport);
2999 uart_write_wakeup(uport);
3003 uport->hw_stopped = 1;
3004 uport->ops->stop_tx(uport);
3010 EXPORT_SYMBOL_GPL(uart_handle_cts_change);
3013 * uart_insert_char - push a char to the uart layer
3015 * User is responsible to call tty_flip_buffer_push when they are done with
3018 * @port: corresponding port
3019 * @status: state of the serial port RX buffer (LSR for 8250)
3020 * @overrun: mask of overrun bits in @status
3021 * @ch: character to push
3022 * @flag: flag for the character (see TTY_NORMAL and friends)
3024 void uart_insert_char(struct uart_port *port, unsigned int status,
3025 unsigned int overrun, unsigned int ch, unsigned int flag)
3027 struct tty_port *tport = &port->state->port;
3029 if ((status & port->ignore_status_mask & ~overrun) == 0)
3030 if (tty_insert_flip_char(tport, ch, flag) == 0)
3031 ++port->icount.buf_overrun;
3034 * Overrun is special. Since it's reported immediately,
3035 * it doesn't affect the current character.
3037 if (status & ~port->ignore_status_mask & overrun)
3038 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
3039 ++port->icount.buf_overrun;
3041 EXPORT_SYMBOL_GPL(uart_insert_char);
3043 EXPORT_SYMBOL(uart_write_wakeup);
3044 EXPORT_SYMBOL(uart_register_driver);
3045 EXPORT_SYMBOL(uart_unregister_driver);
3046 EXPORT_SYMBOL(uart_suspend_port);
3047 EXPORT_SYMBOL(uart_resume_port);
3048 EXPORT_SYMBOL(uart_add_one_port);
3049 EXPORT_SYMBOL(uart_remove_one_port);
3051 MODULE_DESCRIPTION("Serial driver core");
3052 MODULE_LICENSE("GPL");