1 /*
2 * Driver core for serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
8 *
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.
13 *
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.
18 *
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
22 */
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>
29 #include <linux/proc_fs.h>
30 #include <linux/seq_file.h>
31 #include <linux/device.h>
32 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
33 #include <linux/serial_core.h>
34 #include <linux/delay.h>
35 #include <linux/mutex.h>
37 #include <asm/irq.h>
38 #include <asm/uaccess.h>
40 /*
41 * This is used to lock changes in serial line configuration.
42 */
43 static DEFINE_MUTEX(port_mutex);
45 /*
46 * lockdep: port->lock is initialized in two places, but we
47 * want only one lock-class:
48 */
49 static struct lock_class_key port_lock_key;
51 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
53 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
54 struct ktermios *old_termios);
55 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
56 static void uart_change_pm(struct uart_state *state, int pm_state);
58 static void uart_port_shutdown(struct tty_port *port);
60 /*
61 * This routine is used by the interrupt handler to schedule processing in
62 * the software interrupt portion of the driver.
63 */
64 void uart_write_wakeup(struct uart_port *port)
65 {
66 struct uart_state *state = port->state;
67 /*
68 * This means you called this function _after_ the port was
69 * closed. No cookie for you.
70 */
71 BUG_ON(!state);
72 tty_wakeup(state->port.tty);
73 }
75 static void uart_stop(struct tty_struct *tty)
76 {
77 struct uart_state *state = tty->driver_data;
78 struct uart_port *port = state->uart_port;
79 unsigned long flags;
81 spin_lock_irqsave(&port->lock, flags);
82 port->ops->stop_tx(port);
83 spin_unlock_irqrestore(&port->lock, flags);
84 }
86 static void __uart_start(struct tty_struct *tty)
87 {
88 struct uart_state *state = tty->driver_data;
89 struct uart_port *port = state->uart_port;
91 if (!uart_circ_empty(&state->xmit) && state->xmit.buf &&
92 !tty->stopped && !tty->hw_stopped)
93 port->ops->start_tx(port);
94 }
96 static void uart_start(struct tty_struct *tty)
97 {
98 struct uart_state *state = tty->driver_data;
99 struct uart_port *port = state->uart_port;
100 unsigned long flags;
102 spin_lock_irqsave(&port->lock, flags);
103 __uart_start(tty);
104 spin_unlock_irqrestore(&port->lock, flags);
105 }
107 static inline void
108 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
109 {
110 unsigned long flags;
111 unsigned int old;
113 spin_lock_irqsave(&port->lock, flags);
114 old = port->mctrl;
115 port->mctrl = (old & ~clear) | set;
116 if (old != port->mctrl)
117 port->ops->set_mctrl(port, port->mctrl);
118 spin_unlock_irqrestore(&port->lock, flags);
119 }
121 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
122 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
124 /*
125 * Startup the port. This will be called once per open. All calls
126 * will be serialised by the per-port mutex.
127 */
128 static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
129 int init_hw)
130 {
131 struct uart_port *uport = state->uart_port;
132 struct tty_port *port = &state->port;
133 unsigned long page;
134 int retval = 0;
136 if (uport->type == PORT_UNKNOWN)
137 return 1;
139 /*
140 * Initialise and allocate the transmit and temporary
141 * buffer.
142 */
143 if (!state->xmit.buf) {
144 /* This is protected by the per port mutex */
145 page = get_zeroed_page(GFP_KERNEL);
146 if (!page)
147 return -ENOMEM;
149 state->xmit.buf = (unsigned char *) page;
150 uart_circ_clear(&state->xmit);
151 }
153 retval = uport->ops->startup(uport);
154 if (retval == 0) {
155 if (uart_console(uport) && uport->cons->cflag) {
156 tty->termios.c_cflag = uport->cons->cflag;
157 uport->cons->cflag = 0;
158 }
159 /*
160 * Initialise the hardware port settings.
161 */
162 uart_change_speed(tty, state, NULL);
164 if (init_hw) {
165 /*
166 * Setup the RTS and DTR signals once the
167 * port is open and ready to respond.
168 */
169 if (tty->termios.c_cflag & CBAUD)
170 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
171 }
173 if (tty_port_cts_enabled(port)) {
174 spin_lock_irq(&uport->lock);
175 if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS))
176 tty->hw_stopped = 1;
177 spin_unlock_irq(&uport->lock);
178 }
179 }
181 /*
182 * This is to allow setserial on this port. People may want to set
183 * port/irq/type and then reconfigure the port properly if it failed
184 * now.
185 */
186 if (retval && capable(CAP_SYS_ADMIN))
187 return 1;
189 return retval;
190 }
192 static int uart_startup(struct tty_struct *tty, struct uart_state *state,
193 int init_hw)
194 {
195 struct tty_port *port = &state->port;
196 int retval;
198 if (port->flags & ASYNC_INITIALIZED)
199 return 0;
201 /*
202 * Set the TTY IO error marker - we will only clear this
203 * once we have successfully opened the port.
204 */
205 set_bit(TTY_IO_ERROR, &tty->flags);
207 retval = uart_port_startup(tty, state, init_hw);
208 if (!retval) {
209 set_bit(ASYNCB_INITIALIZED, &port->flags);
210 clear_bit(TTY_IO_ERROR, &tty->flags);
211 } else if (retval > 0)
212 retval = 0;
214 return retval;
215 }
217 /*
218 * This routine will shutdown a serial port; interrupts are disabled, and
219 * DTR is dropped if the hangup on close termio flag is on. Calls to
220 * uart_shutdown are serialised by the per-port semaphore.
221 */
222 static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
223 {
224 struct uart_port *uport = state->uart_port;
225 struct tty_port *port = &state->port;
227 /*
228 * Set the TTY IO error marker
229 */
230 if (tty)
231 set_bit(TTY_IO_ERROR, &tty->flags);
233 if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
234 /*
235 * Turn off DTR and RTS early.
236 */
237 if (!tty || (tty->termios.c_cflag & HUPCL))
238 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
240 uart_port_shutdown(port);
241 }
243 /*
244 * It's possible for shutdown to be called after suspend if we get
245 * a DCD drop (hangup) at just the right time. Clear suspended bit so
246 * we don't try to resume a port that has been shutdown.
247 */
248 clear_bit(ASYNCB_SUSPENDED, &port->flags);
250 /*
251 * Free the transmit buffer page.
252 */
253 if (state->xmit.buf) {
254 free_page((unsigned long)state->xmit.buf);
255 state->xmit.buf = NULL;
256 }
257 }
259 /**
260 * uart_update_timeout - update per-port FIFO timeout.
261 * @port: uart_port structure describing the port
262 * @cflag: termios cflag value
263 * @baud: speed of the port
264 *
265 * Set the port FIFO timeout value. The @cflag value should
266 * reflect the actual hardware settings.
267 */
268 void
269 uart_update_timeout(struct uart_port *port, unsigned int cflag,
270 unsigned int baud)
271 {
272 unsigned int bits;
274 /* byte size and parity */
275 switch (cflag & CSIZE) {
276 case CS5:
277 bits = 7;
278 break;
279 case CS6:
280 bits = 8;
281 break;
282 case CS7:
283 bits = 9;
284 break;
285 default:
286 bits = 10;
287 break; /* CS8 */
288 }
290 if (cflag & CSTOPB)
291 bits++;
292 if (cflag & PARENB)
293 bits++;
295 /*
296 * The total number of bits to be transmitted in the fifo.
297 */
298 bits = bits * port->fifosize;
300 /*
301 * Figure the timeout to send the above number of bits.
302 * Add .02 seconds of slop
303 */
304 port->timeout = (HZ * bits) / baud + HZ/50;
305 }
307 EXPORT_SYMBOL(uart_update_timeout);
309 /**
310 * uart_get_baud_rate - return baud rate for a particular port
311 * @port: uart_port structure describing the port in question.
312 * @termios: desired termios settings.
313 * @old: old termios (or NULL)
314 * @min: minimum acceptable baud rate
315 * @max: maximum acceptable baud rate
316 *
317 * Decode the termios structure into a numeric baud rate,
318 * taking account of the magic 38400 baud rate (with spd_*
319 * flags), and mapping the %B0 rate to 9600 baud.
320 *
321 * If the new baud rate is invalid, try the old termios setting.
322 * If it's still invalid, we try 9600 baud.
323 *
324 * Update the @termios structure to reflect the baud rate
325 * we're actually going to be using. Don't do this for the case
326 * where B0 is requested ("hang up").
327 */
328 unsigned int
329 uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
330 struct ktermios *old, unsigned int min, unsigned int max)
331 {
332 unsigned int try, baud, altbaud = 38400;
333 int hung_up = 0;
334 upf_t flags = port->flags & UPF_SPD_MASK;
336 if (flags == UPF_SPD_HI)
337 altbaud = 57600;
338 else if (flags == UPF_SPD_VHI)
339 altbaud = 115200;
340 else if (flags == UPF_SPD_SHI)
341 altbaud = 230400;
342 else if (flags == UPF_SPD_WARP)
343 altbaud = 460800;
345 for (try = 0; try < 2; try++) {
346 baud = tty_termios_baud_rate(termios);
348 /*
349 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
350 * Die! Die! Die!
351 */
352 if (baud == 38400)
353 baud = altbaud;
355 /*
356 * Special case: B0 rate.
357 */
358 if (baud == 0) {
359 hung_up = 1;
360 baud = 9600;
361 }
363 if (baud >= min && baud <= max)
364 return baud;
366 /*
367 * Oops, the quotient was zero. Try again with
368 * the old baud rate if possible.
369 */
370 termios->c_cflag &= ~CBAUD;
371 if (old) {
372 baud = tty_termios_baud_rate(old);
373 if (!hung_up)
374 tty_termios_encode_baud_rate(termios,
375 baud, baud);
376 old = NULL;
377 continue;
378 }
380 /*
381 * As a last resort, if the range cannot be met then clip to
382 * the nearest chip supported rate.
383 */
384 if (!hung_up) {
385 if (baud <= min)
386 tty_termios_encode_baud_rate(termios,
387 min + 1, min + 1);
388 else
389 tty_termios_encode_baud_rate(termios,
390 max - 1, max - 1);
391 }
392 }
393 /* Should never happen */
394 WARN_ON(1);
395 return 0;
396 }
398 EXPORT_SYMBOL(uart_get_baud_rate);
400 /**
401 * uart_get_divisor - return uart clock divisor
402 * @port: uart_port structure describing the port.
403 * @baud: desired baud rate
404 *
405 * Calculate the uart clock divisor for the port.
406 */
407 unsigned int
408 uart_get_divisor(struct uart_port *port, unsigned int baud)
409 {
410 unsigned int quot;
412 /*
413 * Old custom speed handling.
414 */
415 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
416 quot = port->custom_divisor;
417 else
418 quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
420 return quot;
421 }
423 EXPORT_SYMBOL(uart_get_divisor);
425 /* FIXME: Consistent locking policy */
426 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
427 struct ktermios *old_termios)
428 {
429 struct tty_port *port = &state->port;
430 struct uart_port *uport = state->uart_port;
431 struct ktermios *termios;
433 /*
434 * If we have no tty, termios, or the port does not exist,
435 * then we can't set the parameters for this port.
436 */
437 if (!tty || uport->type == PORT_UNKNOWN)
438 return;
440 termios = &tty->termios;
442 /*
443 * Set flags based on termios cflag
444 */
445 if (termios->c_cflag & CRTSCTS)
446 set_bit(ASYNCB_CTS_FLOW, &port->flags);
447 else
448 clear_bit(ASYNCB_CTS_FLOW, &port->flags);
450 if (termios->c_cflag & CLOCAL)
451 clear_bit(ASYNCB_CHECK_CD, &port->flags);
452 else
453 set_bit(ASYNCB_CHECK_CD, &port->flags);
455 uport->ops->set_termios(uport, termios, old_termios);
456 }
458 static inline int __uart_put_char(struct uart_port *port,
459 struct circ_buf *circ, unsigned char c)
460 {
461 unsigned long flags;
462 int ret = 0;
464 if (!circ->buf)
465 return 0;
467 spin_lock_irqsave(&port->lock, flags);
468 if (uart_circ_chars_free(circ) != 0) {
469 circ->buf[circ->head] = c;
470 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
471 ret = 1;
472 }
473 spin_unlock_irqrestore(&port->lock, flags);
474 return ret;
475 }
477 static int uart_put_char(struct tty_struct *tty, unsigned char ch)
478 {
479 struct uart_state *state = tty->driver_data;
481 return __uart_put_char(state->uart_port, &state->xmit, ch);
482 }
484 static void uart_flush_chars(struct tty_struct *tty)
485 {
486 uart_start(tty);
487 }
489 static int uart_write(struct tty_struct *tty,
490 const unsigned char *buf, int count)
491 {
492 struct uart_state *state = tty->driver_data;
493 struct uart_port *port;
494 struct circ_buf *circ;
495 unsigned long flags;
496 int c, ret = 0;
498 /*
499 * This means you called this function _after_ the port was
500 * closed. No cookie for you.
501 */
502 if (!state) {
503 WARN_ON(1);
504 return -EL3HLT;
505 }
507 port = state->uart_port;
508 circ = &state->xmit;
510 if (!circ->buf)
511 return 0;
513 spin_lock_irqsave(&port->lock, flags);
514 while (1) {
515 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
516 if (count < c)
517 c = count;
518 if (c <= 0)
519 break;
520 memcpy(circ->buf + circ->head, buf, c);
521 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
522 buf += c;
523 count -= c;
524 ret += c;
525 }
526 spin_unlock_irqrestore(&port->lock, flags);
528 uart_start(tty);
529 return ret;
530 }
532 static int uart_write_room(struct tty_struct *tty)
533 {
534 struct uart_state *state = tty->driver_data;
535 unsigned long flags;
536 int ret;
538 spin_lock_irqsave(&state->uart_port->lock, flags);
539 ret = uart_circ_chars_free(&state->xmit);
540 spin_unlock_irqrestore(&state->uart_port->lock, flags);
541 return ret;
542 }
544 static int uart_chars_in_buffer(struct tty_struct *tty)
545 {
546 struct uart_state *state = tty->driver_data;
547 unsigned long flags;
548 int ret;
550 spin_lock_irqsave(&state->uart_port->lock, flags);
551 ret = uart_circ_chars_pending(&state->xmit);
552 spin_unlock_irqrestore(&state->uart_port->lock, flags);
553 return ret;
554 }
556 static void uart_flush_buffer(struct tty_struct *tty)
557 {
558 struct uart_state *state = tty->driver_data;
559 struct uart_port *port;
560 unsigned long flags;
562 /*
563 * This means you called this function _after_ the port was
564 * closed. No cookie for you.
565 */
566 if (!state) {
567 WARN_ON(1);
568 return;
569 }
571 port = state->uart_port;
572 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
574 spin_lock_irqsave(&port->lock, flags);
575 uart_circ_clear(&state->xmit);
576 if (port->ops->flush_buffer)
577 port->ops->flush_buffer(port);
578 spin_unlock_irqrestore(&port->lock, flags);
579 tty_wakeup(tty);
580 }
582 /*
583 * This function is used to send a high-priority XON/XOFF character to
584 * the device
585 */
586 static void uart_send_xchar(struct tty_struct *tty, char ch)
587 {
588 struct uart_state *state = tty->driver_data;
589 struct uart_port *port = state->uart_port;
590 unsigned long flags;
592 if (port->ops->send_xchar)
593 port->ops->send_xchar(port, ch);
594 else {
595 port->x_char = ch;
596 if (ch) {
597 spin_lock_irqsave(&port->lock, flags);
598 port->ops->start_tx(port);
599 spin_unlock_irqrestore(&port->lock, flags);
600 }
601 }
602 }
604 static void uart_throttle(struct tty_struct *tty)
605 {
606 struct uart_state *state = tty->driver_data;
607 struct uart_port *port = state->uart_port;
608 uint32_t mask = 0;
610 if (I_IXOFF(tty))
611 mask |= UPF_SOFT_FLOW;
612 if (tty->termios.c_cflag & CRTSCTS)
613 mask |= UPF_HARD_FLOW;
615 if (port->flags & mask) {
616 port->ops->throttle(port);
617 mask &= ~port->flags;
618 }
620 if (mask & UPF_SOFT_FLOW)
621 uart_send_xchar(tty, STOP_CHAR(tty));
623 if (mask & UPF_HARD_FLOW)
624 uart_clear_mctrl(port, TIOCM_RTS);
625 }
627 static void uart_unthrottle(struct tty_struct *tty)
628 {
629 struct uart_state *state = tty->driver_data;
630 struct uart_port *port = state->uart_port;
631 uint32_t mask = 0;
633 if (I_IXOFF(tty))
634 mask |= UPF_SOFT_FLOW;
635 if (tty->termios.c_cflag & CRTSCTS)
636 mask |= UPF_HARD_FLOW;
638 if (port->flags & mask) {
639 port->ops->unthrottle(port);
640 mask &= ~port->flags;
641 }
643 if (mask & UPF_SOFT_FLOW) {
644 if (port->x_char)
645 port->x_char = 0;
646 else
647 uart_send_xchar(tty, START_CHAR(tty));
648 }
650 if (mask & UPF_HARD_FLOW)
651 uart_set_mctrl(port, TIOCM_RTS);
652 }
654 static void do_uart_get_info(struct tty_port *port,
655 struct serial_struct *retinfo)
656 {
657 struct uart_state *state = container_of(port, struct uart_state, port);
658 struct uart_port *uport = state->uart_port;
660 memset(retinfo, 0, sizeof(*retinfo));
662 retinfo->type = uport->type;
663 retinfo->line = uport->line;
664 retinfo->port = uport->iobase;
665 if (HIGH_BITS_OFFSET)
666 retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
667 retinfo->irq = uport->irq;
668 retinfo->flags = uport->flags;
669 retinfo->xmit_fifo_size = uport->fifosize;
670 retinfo->baud_base = uport->uartclk / 16;
671 retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10;
672 retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
673 ASYNC_CLOSING_WAIT_NONE :
674 jiffies_to_msecs(port->closing_wait) / 10;
675 retinfo->custom_divisor = uport->custom_divisor;
676 retinfo->hub6 = uport->hub6;
677 retinfo->io_type = uport->iotype;
678 retinfo->iomem_reg_shift = uport->regshift;
679 retinfo->iomem_base = (void *)(unsigned long)uport->mapbase;
680 }
682 static void uart_get_info(struct tty_port *port,
683 struct serial_struct *retinfo)
684 {
685 /* Ensure the state we copy is consistent and no hardware changes
686 occur as we go */
687 mutex_lock(&port->mutex);
688 do_uart_get_info(port, retinfo);
689 mutex_unlock(&port->mutex);
690 }
692 static int uart_get_info_user(struct tty_port *port,
693 struct serial_struct __user *retinfo)
694 {
695 struct serial_struct tmp;
696 uart_get_info(port, &tmp);
698 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
699 return -EFAULT;
700 return 0;
701 }
703 static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
704 struct uart_state *state,
705 struct serial_struct *new_info)
706 {
707 struct uart_port *uport = state->uart_port;
708 unsigned long new_port;
709 unsigned int change_irq, change_port, closing_wait;
710 unsigned int old_custom_divisor, close_delay;
711 upf_t old_flags, new_flags;
712 int retval = 0;
714 new_port = new_info->port;
715 if (HIGH_BITS_OFFSET)
716 new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
718 new_info->irq = irq_canonicalize(new_info->irq);
719 close_delay = msecs_to_jiffies(new_info->close_delay * 10);
720 closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
721 ASYNC_CLOSING_WAIT_NONE :
722 msecs_to_jiffies(new_info->closing_wait * 10);
725 change_irq = !(uport->flags & UPF_FIXED_PORT)
726 && new_info->irq != uport->irq;
728 /*
729 * Since changing the 'type' of the port changes its resource
730 * allocations, we should treat type changes the same as
731 * IO port changes.
732 */
733 change_port = !(uport->flags & UPF_FIXED_PORT)
734 && (new_port != uport->iobase ||
735 (unsigned long)new_info->iomem_base != uport->mapbase ||
736 new_info->hub6 != uport->hub6 ||
737 new_info->io_type != uport->iotype ||
738 new_info->iomem_reg_shift != uport->regshift ||
739 new_info->type != uport->type);
741 old_flags = uport->flags;
742 new_flags = new_info->flags;
743 old_custom_divisor = uport->custom_divisor;
745 if (!capable(CAP_SYS_ADMIN)) {
746 retval = -EPERM;
747 if (change_irq || change_port ||
748 (new_info->baud_base != uport->uartclk / 16) ||
749 (close_delay != port->close_delay) ||
750 (closing_wait != port->closing_wait) ||
751 (new_info->xmit_fifo_size &&
752 new_info->xmit_fifo_size != uport->fifosize) ||
753 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
754 goto exit;
755 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
756 (new_flags & UPF_USR_MASK));
757 uport->custom_divisor = new_info->custom_divisor;
758 goto check_and_exit;
759 }
761 /*
762 * Ask the low level driver to verify the settings.
763 */
764 if (uport->ops->verify_port)
765 retval = uport->ops->verify_port(uport, new_info);
767 if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
768 (new_info->baud_base < 9600))
769 retval = -EINVAL;
771 if (retval)
772 goto exit;
774 if (change_port || change_irq) {
775 retval = -EBUSY;
777 /*
778 * Make sure that we are the sole user of this port.
779 */
780 if (tty_port_users(port) > 1)
781 goto exit;
783 /*
784 * We need to shutdown the serial port at the old
785 * port/type/irq combination.
786 */
787 uart_shutdown(tty, state);
788 }
790 if (change_port) {
791 unsigned long old_iobase, old_mapbase;
792 unsigned int old_type, old_iotype, old_hub6, old_shift;
794 old_iobase = uport->iobase;
795 old_mapbase = uport->mapbase;
796 old_type = uport->type;
797 old_hub6 = uport->hub6;
798 old_iotype = uport->iotype;
799 old_shift = uport->regshift;
801 /*
802 * Free and release old regions
803 */
804 if (old_type != PORT_UNKNOWN)
805 uport->ops->release_port(uport);
807 uport->iobase = new_port;
808 uport->type = new_info->type;
809 uport->hub6 = new_info->hub6;
810 uport->iotype = new_info->io_type;
811 uport->regshift = new_info->iomem_reg_shift;
812 uport->mapbase = (unsigned long)new_info->iomem_base;
814 /*
815 * Claim and map the new regions
816 */
817 if (uport->type != PORT_UNKNOWN) {
818 retval = uport->ops->request_port(uport);
819 } else {
820 /* Always success - Jean II */
821 retval = 0;
822 }
824 /*
825 * If we fail to request resources for the
826 * new port, try to restore the old settings.
827 */
828 if (retval && old_type != PORT_UNKNOWN) {
829 uport->iobase = old_iobase;
830 uport->type = old_type;
831 uport->hub6 = old_hub6;
832 uport->iotype = old_iotype;
833 uport->regshift = old_shift;
834 uport->mapbase = old_mapbase;
835 retval = uport->ops->request_port(uport);
836 /*
837 * If we failed to restore the old settings,
838 * we fail like this.
839 */
840 if (retval)
841 uport->type = PORT_UNKNOWN;
843 /*
844 * We failed anyway.
845 */
846 retval = -EBUSY;
847 /* Added to return the correct error -Ram Gupta */
848 goto exit;
849 }
850 }
852 if (change_irq)
853 uport->irq = new_info->irq;
854 if (!(uport->flags & UPF_FIXED_PORT))
855 uport->uartclk = new_info->baud_base * 16;
856 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
857 (new_flags & UPF_CHANGE_MASK);
858 uport->custom_divisor = new_info->custom_divisor;
859 port->close_delay = close_delay;
860 port->closing_wait = closing_wait;
861 if (new_info->xmit_fifo_size)
862 uport->fifosize = new_info->xmit_fifo_size;
863 if (port->tty)
864 port->tty->low_latency =
865 (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
867 check_and_exit:
868 retval = 0;
869 if (uport->type == PORT_UNKNOWN)
870 goto exit;
871 if (port->flags & ASYNC_INITIALIZED) {
872 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
873 old_custom_divisor != uport->custom_divisor) {
874 /*
875 * If they're setting up a custom divisor or speed,
876 * instead of clearing it, then bitch about it. No
877 * need to rate-limit; it's CAP_SYS_ADMIN only.
878 */
879 if (uport->flags & UPF_SPD_MASK) {
880 char buf[64];
881 printk(KERN_NOTICE
882 "%s sets custom speed on %s. This "
883 "is deprecated.\n", current->comm,
884 tty_name(port->tty, buf));
885 }
886 uart_change_speed(tty, state, NULL);
887 }
888 } else
889 retval = uart_startup(tty, state, 1);
890 exit:
891 return retval;
892 }
894 static int uart_set_info_user(struct tty_struct *tty, struct uart_state *state,
895 struct serial_struct __user *newinfo)
896 {
897 struct serial_struct new_serial;
898 struct tty_port *port = &state->port;
899 int retval;
901 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
902 return -EFAULT;
904 /*
905 * This semaphore protects port->count. It is also
906 * very useful to prevent opens. Also, take the
907 * port configuration semaphore to make sure that a
908 * module insertion/removal doesn't change anything
909 * under us.
910 */
911 mutex_lock(&port->mutex);
912 retval = uart_set_info(tty, port, state, &new_serial);
913 mutex_unlock(&port->mutex);
914 return retval;
915 }
917 /**
918 * uart_get_lsr_info - get line status register info
919 * @tty: tty associated with the UART
920 * @state: UART being queried
921 * @value: returned modem value
922 *
923 * Note: uart_ioctl protects us against hangups.
924 */
925 static int uart_get_lsr_info(struct tty_struct *tty,
926 struct uart_state *state, unsigned int __user *value)
927 {
928 struct uart_port *uport = state->uart_port;
929 unsigned int result;
931 result = uport->ops->tx_empty(uport);
933 /*
934 * If we're about to load something into the transmit
935 * register, we'll pretend the transmitter isn't empty to
936 * avoid a race condition (depending on when the transmit
937 * interrupt happens).
938 */
939 if (uport->x_char ||
940 ((uart_circ_chars_pending(&state->xmit) > 0) &&
941 !tty->stopped && !tty->hw_stopped))
942 result &= ~TIOCSER_TEMT;
944 return put_user(result, value);
945 }
947 static int uart_tiocmget(struct tty_struct *tty)
948 {
949 struct uart_state *state = tty->driver_data;
950 struct tty_port *port = &state->port;
951 struct uart_port *uport = state->uart_port;
952 int result = -EIO;
954 mutex_lock(&port->mutex);
955 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
956 result = uport->mctrl;
957 spin_lock_irq(&uport->lock);
958 result |= uport->ops->get_mctrl(uport);
959 spin_unlock_irq(&uport->lock);
960 }
961 mutex_unlock(&port->mutex);
963 return result;
964 }
966 static int
967 uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
968 {
969 struct uart_state *state = tty->driver_data;
970 struct uart_port *uport = state->uart_port;
971 struct tty_port *port = &state->port;
972 int ret = -EIO;
974 mutex_lock(&port->mutex);
975 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
976 uart_update_mctrl(uport, set, clear);
977 ret = 0;
978 }
979 mutex_unlock(&port->mutex);
980 return ret;
981 }
983 static int uart_break_ctl(struct tty_struct *tty, int break_state)
984 {
985 struct uart_state *state = tty->driver_data;
986 struct tty_port *port = &state->port;
987 struct uart_port *uport = state->uart_port;
989 mutex_lock(&port->mutex);
991 if (uport->type != PORT_UNKNOWN)
992 uport->ops->break_ctl(uport, break_state);
994 mutex_unlock(&port->mutex);
995 return 0;
996 }
998 static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
999 {
1000 struct uart_port *uport = state->uart_port;
1001 struct tty_port *port = &state->port;
1002 int flags, ret;
1004 if (!capable(CAP_SYS_ADMIN))
1005 return -EPERM;
1007 /*
1008 * Take the per-port semaphore. This prevents count from
1009 * changing, and hence any extra opens of the port while
1010 * we're auto-configuring.
1011 */
1012 if (mutex_lock_interruptible(&port->mutex))
1013 return -ERESTARTSYS;
1015 ret = -EBUSY;
1016 if (tty_port_users(port) == 1) {
1017 uart_shutdown(tty, state);
1019 /*
1020 * If we already have a port type configured,
1021 * we must release its resources.
1022 */
1023 if (uport->type != PORT_UNKNOWN)
1024 uport->ops->release_port(uport);
1026 flags = UART_CONFIG_TYPE;
1027 if (uport->flags & UPF_AUTO_IRQ)
1028 flags |= UART_CONFIG_IRQ;
1030 /*
1031 * This will claim the ports resources if
1032 * a port is found.
1033 */
1034 uport->ops->config_port(uport, flags);
1036 ret = uart_startup(tty, state, 1);
1037 }
1038 mutex_unlock(&port->mutex);
1039 return ret;
1040 }
1042 /*
1043 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1044 * - mask passed in arg for lines of interest
1045 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1046 * Caller should use TIOCGICOUNT to see which one it was
1047 *
1048 * FIXME: This wants extracting into a common all driver implementation
1049 * of TIOCMWAIT using tty_port.
1050 */
1051 static int
1052 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1053 {
1054 struct uart_port *uport = state->uart_port;
1055 struct tty_port *port = &state->port;
1056 DECLARE_WAITQUEUE(wait, current);
1057 struct uart_icount cprev, cnow;
1058 int ret;
1060 /*
1061 * note the counters on entry
1062 */
1063 spin_lock_irq(&uport->lock);
1064 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1066 /*
1067 * Force modem status interrupts on
1068 */
1069 uport->ops->enable_ms(uport);
1070 spin_unlock_irq(&uport->lock);
1072 add_wait_queue(&port->delta_msr_wait, &wait);
1073 for (;;) {
1074 spin_lock_irq(&uport->lock);
1075 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1076 spin_unlock_irq(&uport->lock);
1078 set_current_state(TASK_INTERRUPTIBLE);
1080 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1081 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1082 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1083 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1084 ret = 0;
1085 break;
1086 }
1088 schedule();
1090 /* see if a signal did it */
1091 if (signal_pending(current)) {
1092 ret = -ERESTARTSYS;
1093 break;
1094 }
1096 cprev = cnow;
1097 }
1099 current->state = TASK_RUNNING;
1100 remove_wait_queue(&port->delta_msr_wait, &wait);
1102 return ret;
1103 }
1105 /*
1106 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1107 * Return: write counters to the user passed counter struct
1108 * NB: both 1->0 and 0->1 transitions are counted except for
1109 * RI where only 0->1 is counted.
1110 */
1111 static int uart_get_icount(struct tty_struct *tty,
1112 struct serial_icounter_struct *icount)
1113 {
1114 struct uart_state *state = tty->driver_data;
1115 struct uart_icount cnow;
1116 struct uart_port *uport = state->uart_port;
1118 spin_lock_irq(&uport->lock);
1119 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1120 spin_unlock_irq(&uport->lock);
1122 icount->cts = cnow.cts;
1123 icount->dsr = cnow.dsr;
1124 icount->rng = cnow.rng;
1125 icount->dcd = cnow.dcd;
1126 icount->rx = cnow.rx;
1127 icount->tx = cnow.tx;
1128 icount->frame = cnow.frame;
1129 icount->overrun = cnow.overrun;
1130 icount->parity = cnow.parity;
1131 icount->brk = cnow.brk;
1132 icount->buf_overrun = cnow.buf_overrun;
1134 return 0;
1135 }
1137 /*
1138 * Called via sys_ioctl. We can use spin_lock_irq() here.
1139 */
1140 static int
1141 uart_ioctl(struct tty_struct *tty, unsigned int cmd,
1142 unsigned long arg)
1143 {
1144 struct uart_state *state = tty->driver_data;
1145 struct tty_port *port = &state->port;
1146 void __user *uarg = (void __user *)arg;
1147 int ret = -ENOIOCTLCMD;
1150 /*
1151 * These ioctls don't rely on the hardware to be present.
1152 */
1153 switch (cmd) {
1154 case TIOCGSERIAL:
1155 ret = uart_get_info_user(port, uarg);
1156 break;
1158 case TIOCSSERIAL:
1159 ret = uart_set_info_user(tty, state, uarg);
1160 break;
1162 case TIOCSERCONFIG:
1163 ret = uart_do_autoconfig(tty, state);
1164 break;
1166 case TIOCSERGWILD: /* obsolete */
1167 case TIOCSERSWILD: /* obsolete */
1168 ret = 0;
1169 break;
1170 }
1172 if (ret != -ENOIOCTLCMD)
1173 goto out;
1175 if (tty->flags & (1 << TTY_IO_ERROR)) {
1176 ret = -EIO;
1177 goto out;
1178 }
1180 /*
1181 * The following should only be used when hardware is present.
1182 */
1183 switch (cmd) {
1184 case TIOCMIWAIT:
1185 ret = uart_wait_modem_status(state, arg);
1186 break;
1187 }
1189 if (ret != -ENOIOCTLCMD)
1190 goto out;
1192 mutex_lock(&port->mutex);
1194 if (tty->flags & (1 << TTY_IO_ERROR)) {
1195 ret = -EIO;
1196 goto out_up;
1197 }
1199 /*
1200 * All these rely on hardware being present and need to be
1201 * protected against the tty being hung up.
1202 */
1203 switch (cmd) {
1204 case TIOCSERGETLSR: /* Get line status register */
1205 ret = uart_get_lsr_info(tty, state, uarg);
1206 break;
1208 default: {
1209 struct uart_port *uport = state->uart_port;
1210 if (uport->ops->ioctl)
1211 ret = uport->ops->ioctl(uport, cmd, arg);
1212 break;
1213 }
1214 }
1215 out_up:
1216 mutex_unlock(&port->mutex);
1217 out:
1218 return ret;
1219 }
1221 static void uart_set_ldisc(struct tty_struct *tty)
1222 {
1223 struct uart_state *state = tty->driver_data;
1224 struct uart_port *uport = state->uart_port;
1226 if (uport->ops->set_ldisc)
1227 uport->ops->set_ldisc(uport, tty->termios.c_line);
1228 }
1230 static void uart_set_termios(struct tty_struct *tty,
1231 struct ktermios *old_termios)
1232 {
1233 struct uart_state *state = tty->driver_data;
1234 struct uart_port *uport = state->uart_port;
1235 unsigned long flags;
1236 unsigned int cflag = tty->termios.c_cflag;
1237 unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
1238 bool sw_changed = false;
1240 /*
1241 * Drivers doing software flow control also need to know
1242 * about changes to these input settings.
1243 */
1244 if (uport->flags & UPF_SOFT_FLOW) {
1245 iflag_mask |= IXANY|IXON|IXOFF;
1246 sw_changed =
1247 tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
1248 tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
1249 }
1251 /*
1252 * These are the bits that are used to setup various
1253 * flags in the low level driver. We can ignore the Bfoo
1254 * bits in c_cflag; c_[io]speed will always be set
1255 * appropriately by set_termios() in tty_ioctl.c
1256 */
1257 if ((cflag ^ old_termios->c_cflag) == 0 &&
1258 tty->termios.c_ospeed == old_termios->c_ospeed &&
1259 tty->termios.c_ispeed == old_termios->c_ispeed &&
1260 ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
1261 !sw_changed) {
1262 return;
1263 }
1265 uart_change_speed(tty, state, old_termios);
1267 /* Handle transition to B0 status */
1268 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1269 uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1270 /* Handle transition away from B0 status */
1271 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1272 unsigned int mask = TIOCM_DTR;
1273 if (!(cflag & CRTSCTS) ||
1274 !test_bit(TTY_THROTTLED, &tty->flags))
1275 mask |= TIOCM_RTS;
1276 uart_set_mctrl(uport, mask);
1277 }
1279 /*
1280 * If the port is doing h/w assisted flow control, do nothing.
1281 * We assume that tty->hw_stopped has never been set.
1282 */
1283 if (uport->flags & UPF_HARD_FLOW)
1284 return;
1286 /* Handle turning off CRTSCTS */
1287 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1288 spin_lock_irqsave(&uport->lock, flags);
1289 tty->hw_stopped = 0;
1290 __uart_start(tty);
1291 spin_unlock_irqrestore(&uport->lock, flags);
1292 }
1293 /* Handle turning on CRTSCTS */
1294 else if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1295 spin_lock_irqsave(&uport->lock, flags);
1296 if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS)) {
1297 tty->hw_stopped = 1;
1298 uport->ops->stop_tx(uport);
1299 }
1300 spin_unlock_irqrestore(&uport->lock, flags);
1301 }
1302 }
1304 /*
1305 * In 2.4.5, calls to this will be serialized via the BKL in
1306 * linux/drivers/char/tty_io.c:tty_release()
1307 * linux/drivers/char/tty_io.c:do_tty_handup()
1308 */
1309 static void uart_close(struct tty_struct *tty, struct file *filp)
1310 {
1311 struct uart_state *state = tty->driver_data;
1312 struct tty_port *port;
1313 struct uart_port *uport;
1314 unsigned long flags;
1316 if (!state)
1317 return;
1319 uport = state->uart_port;
1320 port = &state->port;
1322 pr_debug("uart_close(%d) called\n", uport->line);
1324 if (tty_port_close_start(port, tty, filp) == 0)
1325 return;
1327 /*
1328 * At this point, we stop accepting input. To do this, we
1329 * disable the receive line status interrupts.
1330 */
1331 if (port->flags & ASYNC_INITIALIZED) {
1332 unsigned long flags;
1333 spin_lock_irqsave(&uport->lock, flags);
1334 uport->ops->stop_rx(uport);
1335 spin_unlock_irqrestore(&uport->lock, flags);
1336 /*
1337 * Before we drop DTR, make sure the UART transmitter
1338 * has completely drained; this is especially
1339 * important if there is a transmit FIFO!
1340 */
1341 uart_wait_until_sent(tty, uport->timeout);
1342 }
1344 mutex_lock(&port->mutex);
1345 uart_shutdown(tty, state);
1346 uart_flush_buffer(tty);
1348 tty_ldisc_flush(tty);
1350 tty_port_tty_set(port, NULL);
1351 spin_lock_irqsave(&port->lock, flags);
1352 tty->closing = 0;
1354 if (port->blocked_open) {
1355 spin_unlock_irqrestore(&port->lock, flags);
1356 if (port->close_delay)
1357 msleep_interruptible(
1358 jiffies_to_msecs(port->close_delay));
1359 spin_lock_irqsave(&port->lock, flags);
1360 } else if (!uart_console(uport)) {
1361 spin_unlock_irqrestore(&port->lock, flags);
1362 uart_change_pm(state, 3);
1363 spin_lock_irqsave(&port->lock, flags);
1364 }
1366 /*
1367 * Wake up anyone trying to open this port.
1368 */
1369 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1370 clear_bit(ASYNCB_CLOSING, &port->flags);
1371 spin_unlock_irqrestore(&port->lock, flags);
1372 wake_up_interruptible(&port->open_wait);
1373 wake_up_interruptible(&port->close_wait);
1375 mutex_unlock(&port->mutex);
1376 }
1378 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1379 {
1380 struct uart_state *state = tty->driver_data;
1381 struct uart_port *port = state->uart_port;
1382 unsigned long char_time, expire;
1384 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1385 return;
1387 /*
1388 * Set the check interval to be 1/5 of the estimated time to
1389 * send a single character, and make it at least 1. The check
1390 * interval should also be less than the timeout.
1391 *
1392 * Note: we have to use pretty tight timings here to satisfy
1393 * the NIST-PCTS.
1394 */
1395 char_time = (port->timeout - HZ/50) / port->fifosize;
1396 char_time = char_time / 5;
1397 if (char_time == 0)
1398 char_time = 1;
1399 if (timeout && timeout < char_time)
1400 char_time = timeout;
1402 /*
1403 * If the transmitter hasn't cleared in twice the approximate
1404 * amount of time to send the entire FIFO, it probably won't
1405 * ever clear. This assumes the UART isn't doing flow
1406 * control, which is currently the case. Hence, if it ever
1407 * takes longer than port->timeout, this is probably due to a
1408 * UART bug of some kind. So, we clamp the timeout parameter at
1409 * 2*port->timeout.
1410 */
1411 if (timeout == 0 || timeout > 2 * port->timeout)
1412 timeout = 2 * port->timeout;
1414 expire = jiffies + timeout;
1416 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1417 port->line, jiffies, expire);
1419 /*
1420 * Check whether the transmitter is empty every 'char_time'.
1421 * 'timeout' / 'expire' give us the maximum amount of time
1422 * we wait.
1423 */
1424 while (!port->ops->tx_empty(port)) {
1425 msleep_interruptible(jiffies_to_msecs(char_time));
1426 if (signal_pending(current))
1427 break;
1428 if (time_after(jiffies, expire))
1429 break;
1430 }
1431 }
1433 /*
1434 * This is called with the BKL held in
1435 * linux/drivers/char/tty_io.c:do_tty_hangup()
1436 * We're called from the eventd thread, so we can sleep for
1437 * a _short_ time only.
1438 */
1439 static void uart_hangup(struct tty_struct *tty)
1440 {
1441 struct uart_state *state = tty->driver_data;
1442 struct tty_port *port = &state->port;
1443 unsigned long flags;
1445 pr_debug("uart_hangup(%d)\n", state->uart_port->line);
1447 mutex_lock(&port->mutex);
1448 if (port->flags & ASYNC_NORMAL_ACTIVE) {
1449 uart_flush_buffer(tty);
1450 uart_shutdown(tty, state);
1451 spin_lock_irqsave(&port->lock, flags);
1452 port->count = 0;
1453 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1454 spin_unlock_irqrestore(&port->lock, flags);
1455 tty_port_tty_set(port, NULL);
1456 wake_up_interruptible(&port->open_wait);
1457 wake_up_interruptible(&port->delta_msr_wait);
1458 }
1459 mutex_unlock(&port->mutex);
1460 }
1462 static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
1463 {
1464 return 0;
1465 }
1467 static void uart_port_shutdown(struct tty_port *port)
1468 {
1469 struct uart_state *state = container_of(port, struct uart_state, port);
1470 struct uart_port *uport = state->uart_port;
1472 /*
1473 * clear delta_msr_wait queue to avoid mem leaks: we may free
1474 * the irq here so the queue might never be woken up. Note
1475 * that we won't end up waiting on delta_msr_wait again since
1476 * any outstanding file descriptors should be pointing at
1477 * hung_up_tty_fops now.
1478 */
1479 wake_up_interruptible(&port->delta_msr_wait);
1481 /*
1482 * Free the IRQ and disable the port.
1483 */
1484 uport->ops->shutdown(uport);
1486 /*
1487 * Ensure that the IRQ handler isn't running on another CPU.
1488 */
1489 synchronize_irq(uport->irq);
1490 }
1492 static int uart_carrier_raised(struct tty_port *port)
1493 {
1494 struct uart_state *state = container_of(port, struct uart_state, port);
1495 struct uart_port *uport = state->uart_port;
1496 int mctrl;
1497 spin_lock_irq(&uport->lock);
1498 uport->ops->enable_ms(uport);
1499 mctrl = uport->ops->get_mctrl(uport);
1500 spin_unlock_irq(&uport->lock);
1501 if (mctrl & TIOCM_CAR)
1502 return 1;
1503 return 0;
1504 }
1506 static void uart_dtr_rts(struct tty_port *port, int onoff)
1507 {
1508 struct uart_state *state = container_of(port, struct uart_state, port);
1509 struct uart_port *uport = state->uart_port;
1511 if (onoff)
1512 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1513 else
1514 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1515 }
1517 /*
1518 * calls to uart_open are serialised by the BKL in
1519 * fs/char_dev.c:chrdev_open()
1520 * Note that if this fails, then uart_close() _will_ be called.
1521 *
1522 * In time, we want to scrap the "opening nonpresent ports"
1523 * behaviour and implement an alternative way for setserial
1524 * to set base addresses/ports/types. This will allow us to
1525 * get rid of a certain amount of extra tests.
1526 */
1527 static int uart_open(struct tty_struct *tty, struct file *filp)
1528 {
1529 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1530 int retval, line = tty->index;
1531 struct uart_state *state = drv->state + line;
1532 struct tty_port *port = &state->port;
1534 pr_debug("uart_open(%d) called\n", line);
1536 /*
1537 * We take the semaphore here to guarantee that we won't be re-entered
1538 * while allocating the state structure, or while we request any IRQs
1539 * that the driver may need. This also has the nice side-effect that
1540 * it delays the action of uart_hangup, so we can guarantee that
1541 * state->port.tty will always contain something reasonable.
1542 */
1543 if (mutex_lock_interruptible(&port->mutex)) {
1544 retval = -ERESTARTSYS;
1545 goto end;
1546 }
1548 port->count++;
1549 if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
1550 retval = -ENXIO;
1551 goto err_dec_count;
1552 }
1554 /*
1555 * Once we set tty->driver_data here, we are guaranteed that
1556 * uart_close() will decrement the driver module use count.
1557 * Any failures from here onwards should not touch the count.
1558 */
1559 tty->driver_data = state;
1560 state->uart_port->state = state;
1561 tty->low_latency = (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1562 tty_port_tty_set(port, tty);
1564 /*
1565 * If the port is in the middle of closing, bail out now.
1566 */
1567 if (tty_hung_up_p(filp)) {
1568 retval = -EAGAIN;
1569 goto err_dec_count;
1570 }
1572 /*
1573 * Make sure the device is in D0 state.
1574 */
1575 if (port->count == 1)
1576 uart_change_pm(state, 0);
1578 /*
1579 * Start up the serial port.
1580 */
1581 retval = uart_startup(tty, state, 0);
1583 /*
1584 * If we succeeded, wait until the port is ready.
1585 */
1586 mutex_unlock(&port->mutex);
1587 if (retval == 0)
1588 retval = tty_port_block_til_ready(port, tty, filp);
1590 end:
1591 return retval;
1592 err_dec_count:
1593 port->count--;
1594 mutex_unlock(&port->mutex);
1595 goto end;
1596 }
1598 static const char *uart_type(struct uart_port *port)
1599 {
1600 const char *str = NULL;
1602 if (port->ops->type)
1603 str = port->ops->type(port);
1605 if (!str)
1606 str = "unknown";
1608 return str;
1609 }
1611 #ifdef CONFIG_PROC_FS
1613 static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1614 {
1615 struct uart_state *state = drv->state + i;
1616 struct tty_port *port = &state->port;
1617 int pm_state;
1618 struct uart_port *uport = state->uart_port;
1619 char stat_buf[32];
1620 unsigned int status;
1621 int mmio;
1623 if (!uport)
1624 return;
1626 mmio = uport->iotype >= UPIO_MEM;
1627 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1628 uport->line, uart_type(uport),
1629 mmio ? "mmio:0x" : "port:",
1630 mmio ? (unsigned long long)uport->mapbase
1631 : (unsigned long long)uport->iobase,
1632 uport->irq);
1634 if (uport->type == PORT_UNKNOWN) {
1635 seq_putc(m, '\n');
1636 return;
1637 }
1639 if (capable(CAP_SYS_ADMIN)) {
1640 mutex_lock(&port->mutex);
1641 pm_state = state->pm_state;
1642 if (pm_state)
1643 uart_change_pm(state, 0);
1644 spin_lock_irq(&uport->lock);
1645 status = uport->ops->get_mctrl(uport);
1646 spin_unlock_irq(&uport->lock);
1647 if (pm_state)
1648 uart_change_pm(state, pm_state);
1649 mutex_unlock(&port->mutex);
1651 seq_printf(m, " tx:%d rx:%d",
1652 uport->icount.tx, uport->icount.rx);
1653 if (uport->icount.frame)
1654 seq_printf(m, " fe:%d",
1655 uport->icount.frame);
1656 if (uport->icount.parity)
1657 seq_printf(m, " pe:%d",
1658 uport->icount.parity);
1659 if (uport->icount.brk)
1660 seq_printf(m, " brk:%d",
1661 uport->icount.brk);
1662 if (uport->icount.overrun)
1663 seq_printf(m, " oe:%d",
1664 uport->icount.overrun);
1666 #define INFOBIT(bit, str) \
1667 if (uport->mctrl & (bit)) \
1668 strncat(stat_buf, (str), sizeof(stat_buf) - \
1669 strlen(stat_buf) - 2)
1670 #define STATBIT(bit, str) \
1671 if (status & (bit)) \
1672 strncat(stat_buf, (str), sizeof(stat_buf) - \
1673 strlen(stat_buf) - 2)
1675 stat_buf[0] = '\0';
1676 stat_buf[1] = '\0';
1677 INFOBIT(TIOCM_RTS, "|RTS");
1678 STATBIT(TIOCM_CTS, "|CTS");
1679 INFOBIT(TIOCM_DTR, "|DTR");
1680 STATBIT(TIOCM_DSR, "|DSR");
1681 STATBIT(TIOCM_CAR, "|CD");
1682 STATBIT(TIOCM_RNG, "|RI");
1683 if (stat_buf[0])
1684 stat_buf[0] = ' ';
1686 seq_puts(m, stat_buf);
1687 }
1688 seq_putc(m, '\n');
1689 #undef STATBIT
1690 #undef INFOBIT
1691 }
1693 static int uart_proc_show(struct seq_file *m, void *v)
1694 {
1695 struct tty_driver *ttydrv = m->private;
1696 struct uart_driver *drv = ttydrv->driver_state;
1697 int i;
1699 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
1700 "", "", "");
1701 for (i = 0; i < drv->nr; i++)
1702 uart_line_info(m, drv, i);
1703 return 0;
1704 }
1706 static int uart_proc_open(struct inode *inode, struct file *file)
1707 {
1708 return single_open(file, uart_proc_show, PDE(inode)->data);
1709 }
1711 static const struct file_operations uart_proc_fops = {
1712 .owner = THIS_MODULE,
1713 .open = uart_proc_open,
1714 .read = seq_read,
1715 .llseek = seq_lseek,
1716 .release = single_release,
1717 };
1718 #endif
1720 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1721 /*
1722 * uart_console_write - write a console message to a serial port
1723 * @port: the port to write the message
1724 * @s: array of characters
1725 * @count: number of characters in string to write
1726 * @write: function to write character to port
1727 */
1728 void uart_console_write(struct uart_port *port, const char *s,
1729 unsigned int count,
1730 void (*putchar)(struct uart_port *, int))
1731 {
1732 unsigned int i;
1734 for (i = 0; i < count; i++, s++) {
1735 if (*s == '\n')
1736 putchar(port, '\r');
1737 putchar(port, *s);
1738 }
1739 }
1740 EXPORT_SYMBOL_GPL(uart_console_write);
1742 /*
1743 * Check whether an invalid uart number has been specified, and
1744 * if so, search for the first available port that does have
1745 * console support.
1746 */
1747 struct uart_port * __init
1748 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1749 {
1750 int idx = co->index;
1752 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1753 ports[idx].membase == NULL))
1754 for (idx = 0; idx < nr; idx++)
1755 if (ports[idx].iobase != 0 ||
1756 ports[idx].membase != NULL)
1757 break;
1759 co->index = idx;
1761 return ports + idx;
1762 }
1764 /**
1765 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1766 * @options: pointer to option string
1767 * @baud: pointer to an 'int' variable for the baud rate.
1768 * @parity: pointer to an 'int' variable for the parity.
1769 * @bits: pointer to an 'int' variable for the number of data bits.
1770 * @flow: pointer to an 'int' variable for the flow control character.
1771 *
1772 * uart_parse_options decodes a string containing the serial console
1773 * options. The format of the string is <baud><parity><bits><flow>,
1774 * eg: 115200n8r
1775 */
1776 void
1777 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1778 {
1779 char *s = options;
1781 *baud = simple_strtoul(s, NULL, 10);
1782 while (*s >= '0' && *s <= '9')
1783 s++;
1784 if (*s)
1785 *parity = *s++;
1786 if (*s)
1787 *bits = *s++ - '0';
1788 if (*s)
1789 *flow = *s;
1790 }
1791 EXPORT_SYMBOL_GPL(uart_parse_options);
1793 struct baud_rates {
1794 unsigned int rate;
1795 unsigned int cflag;
1796 };
1798 static const struct baud_rates baud_rates[] = {
1799 { 921600, B921600 },
1800 { 460800, B460800 },
1801 { 230400, B230400 },
1802 { 115200, B115200 },
1803 { 57600, B57600 },
1804 { 38400, B38400 },
1805 { 19200, B19200 },
1806 { 9600, B9600 },
1807 { 4800, B4800 },
1808 { 2400, B2400 },
1809 { 1200, B1200 },
1810 { 0, B38400 }
1811 };
1813 /**
1814 * uart_set_options - setup the serial console parameters
1815 * @port: pointer to the serial ports uart_port structure
1816 * @co: console pointer
1817 * @baud: baud rate
1818 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1819 * @bits: number of data bits
1820 * @flow: flow control character - 'r' (rts)
1821 */
1822 int
1823 uart_set_options(struct uart_port *port, struct console *co,
1824 int baud, int parity, int bits, int flow)
1825 {
1826 struct ktermios termios;
1827 static struct ktermios dummy;
1828 int i;
1830 /*
1831 * Ensure that the serial console lock is initialised
1832 * early.
1833 */
1834 spin_lock_init(&port->lock);
1835 lockdep_set_class(&port->lock, &port_lock_key);
1837 memset(&termios, 0, sizeof(struct ktermios));
1839 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1841 /*
1842 * Construct a cflag setting.
1843 */
1844 for (i = 0; baud_rates[i].rate; i++)
1845 if (baud_rates[i].rate <= baud)
1846 break;
1848 termios.c_cflag |= baud_rates[i].cflag;
1850 if (bits == 7)
1851 termios.c_cflag |= CS7;
1852 else
1853 termios.c_cflag |= CS8;
1855 switch (parity) {
1856 case 'o': case 'O':
1857 termios.c_cflag |= PARODD;
1858 /*fall through*/
1859 case 'e': case 'E':
1860 termios.c_cflag |= PARENB;
1861 break;
1862 }
1864 if (flow == 'r')
1865 termios.c_cflag |= CRTSCTS;
1867 /*
1868 * some uarts on other side don't support no flow control.
1869 * So we set * DTR in host uart to make them happy
1870 */
1871 port->mctrl |= TIOCM_DTR;
1873 port->ops->set_termios(port, &termios, &dummy);
1874 /*
1875 * Allow the setting of the UART parameters with a NULL console
1876 * too:
1877 */
1878 if (co)
1879 co->cflag = termios.c_cflag;
1881 return 0;
1882 }
1883 EXPORT_SYMBOL_GPL(uart_set_options);
1884 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1886 /**
1887 * uart_change_pm - set power state of the port
1888 *
1889 * @state: port descriptor
1890 * @pm_state: new state
1891 *
1892 * Locking: port->mutex has to be held
1893 */
1894 static void uart_change_pm(struct uart_state *state, int pm_state)
1895 {
1896 struct uart_port *port = state->uart_port;
1898 if (state->pm_state != pm_state) {
1899 if (port->ops->pm)
1900 port->ops->pm(port, pm_state, state->pm_state);
1901 state->pm_state = pm_state;
1902 }
1903 }
1905 struct uart_match {
1906 struct uart_port *port;
1907 struct uart_driver *driver;
1908 };
1910 static int serial_match_port(struct device *dev, void *data)
1911 {
1912 struct uart_match *match = data;
1913 struct tty_driver *tty_drv = match->driver->tty_driver;
1914 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1915 match->port->line;
1917 return dev->devt == devt; /* Actually, only one tty per port */
1918 }
1920 int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
1921 {
1922 struct uart_state *state = drv->state + uport->line;
1923 struct tty_port *port = &state->port;
1924 struct device *tty_dev;
1925 struct uart_match match = {uport, drv};
1927 mutex_lock(&port->mutex);
1929 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1930 if (device_may_wakeup(tty_dev)) {
1931 if (!enable_irq_wake(uport->irq))
1932 uport->irq_wake = 1;
1933 put_device(tty_dev);
1934 mutex_unlock(&port->mutex);
1935 return 0;
1936 }
1937 put_device(tty_dev);
1939 if (console_suspend_enabled || !uart_console(uport))
1940 uport->suspended = 1;
1942 if (port->flags & ASYNC_INITIALIZED) {
1943 const struct uart_ops *ops = uport->ops;
1944 int tries;
1946 if (console_suspend_enabled || !uart_console(uport)) {
1947 set_bit(ASYNCB_SUSPENDED, &port->flags);
1948 clear_bit(ASYNCB_INITIALIZED, &port->flags);
1950 spin_lock_irq(&uport->lock);
1951 ops->stop_tx(uport);
1952 ops->set_mctrl(uport, 0);
1953 ops->stop_rx(uport);
1954 spin_unlock_irq(&uport->lock);
1955 }
1957 /*
1958 * Wait for the transmitter to empty.
1959 */
1960 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
1961 msleep(10);
1962 if (!tries)
1963 printk(KERN_ERR "%s%s%s%d: Unable to drain "
1964 "transmitter\n",
1965 uport->dev ? dev_name(uport->dev) : "",
1966 uport->dev ? ": " : "",
1967 drv->dev_name,
1968 drv->tty_driver->name_base + uport->line);
1970 if (console_suspend_enabled || !uart_console(uport))
1971 ops->shutdown(uport);
1972 }
1974 /*
1975 * Disable the console device before suspending.
1976 */
1977 if (console_suspend_enabled && uart_console(uport))
1978 console_stop(uport->cons);
1980 if (console_suspend_enabled || !uart_console(uport))
1981 uart_change_pm(state, 3);
1983 mutex_unlock(&port->mutex);
1985 return 0;
1986 }
1988 int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
1989 {
1990 struct uart_state *state = drv->state + uport->line;
1991 struct tty_port *port = &state->port;
1992 struct device *tty_dev;
1993 struct uart_match match = {uport, drv};
1994 struct ktermios termios;
1996 mutex_lock(&port->mutex);
1998 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1999 if (!uport->suspended && device_may_wakeup(tty_dev)) {
2000 if (uport->irq_wake) {
2001 disable_irq_wake(uport->irq);
2002 uport->irq_wake = 0;
2003 }
2004 put_device(tty_dev);
2005 mutex_unlock(&port->mutex);
2006 return 0;
2007 }
2008 put_device(tty_dev);
2009 uport->suspended = 0;
2011 /*
2012 * Re-enable the console device after suspending.
2013 */
2014 if (uart_console(uport)) {
2015 /*
2016 * First try to use the console cflag setting.
2017 */
2018 memset(&termios, 0, sizeof(struct ktermios));
2019 termios.c_cflag = uport->cons->cflag;
2021 /*
2022 * If that's unset, use the tty termios setting.
2023 */
2024 if (port->tty && termios.c_cflag == 0)
2025 termios = port->tty->termios;
2027 if (console_suspend_enabled)
2028 uart_change_pm(state, 0);
2029 uport->ops->set_termios(uport, &termios, NULL);
2030 if (console_suspend_enabled)
2031 console_start(uport->cons);
2032 }
2034 if (port->flags & ASYNC_SUSPENDED) {
2035 const struct uart_ops *ops = uport->ops;
2036 int ret;
2038 uart_change_pm(state, 0);
2039 spin_lock_irq(&uport->lock);
2040 ops->set_mctrl(uport, 0);
2041 spin_unlock_irq(&uport->lock);
2042 if (console_suspend_enabled || !uart_console(uport)) {
2043 /* Protected by port mutex for now */
2044 struct tty_struct *tty = port->tty;
2045 ret = ops->startup(uport);
2046 if (ret == 0) {
2047 if (tty)
2048 uart_change_speed(tty, state, NULL);
2049 spin_lock_irq(&uport->lock);
2050 ops->set_mctrl(uport, uport->mctrl);
2051 ops->start_tx(uport);
2052 spin_unlock_irq(&uport->lock);
2053 set_bit(ASYNCB_INITIALIZED, &port->flags);
2054 } else {
2055 /*
2056 * Failed to resume - maybe hardware went away?
2057 * Clear the "initialized" flag so we won't try
2058 * to call the low level drivers shutdown method.
2059 */
2060 uart_shutdown(tty, state);
2061 }
2062 }
2064 clear_bit(ASYNCB_SUSPENDED, &port->flags);
2065 }
2067 mutex_unlock(&port->mutex);
2069 return 0;
2070 }
2072 static inline void
2073 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2074 {
2075 char address[64];
2077 switch (port->iotype) {
2078 case UPIO_PORT:
2079 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2080 break;
2081 case UPIO_HUB6:
2082 snprintf(address, sizeof(address),
2083 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2084 break;
2085 case UPIO_MEM:
2086 case UPIO_MEM32:
2087 case UPIO_AU:
2088 case UPIO_TSI:
2089 snprintf(address, sizeof(address),
2090 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2091 break;
2092 default:
2093 strlcpy(address, "*unknown*", sizeof(address));
2094 break;
2095 }
2097 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2098 port->dev ? dev_name(port->dev) : "",
2099 port->dev ? ": " : "",
2100 drv->dev_name,
2101 drv->tty_driver->name_base + port->line,
2102 address, port->irq, uart_type(port));
2103 }
2105 static void
2106 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2107 struct uart_port *port)
2108 {
2109 unsigned int flags;
2111 /*
2112 * If there isn't a port here, don't do anything further.
2113 */
2114 if (!port->iobase && !port->mapbase && !port->membase)
2115 return;
2117 /*
2118 * Now do the auto configuration stuff. Note that config_port
2119 * is expected to claim the resources and map the port for us.
2120 */
2121 flags = 0;
2122 if (port->flags & UPF_AUTO_IRQ)
2123 flags |= UART_CONFIG_IRQ;
2124 if (port->flags & UPF_BOOT_AUTOCONF) {
2125 if (!(port->flags & UPF_FIXED_TYPE)) {
2126 port->type = PORT_UNKNOWN;
2127 flags |= UART_CONFIG_TYPE;
2128 }
2129 port->ops->config_port(port, flags);
2130 }
2132 if (port->type != PORT_UNKNOWN) {
2133 unsigned long flags;
2135 uart_report_port(drv, port);
2137 /* Power up port for set_mctrl() */
2138 uart_change_pm(state, 0);
2140 /*
2141 * Ensure that the modem control lines are de-activated.
2142 * keep the DTR setting that is set in uart_set_options()
2143 * We probably don't need a spinlock around this, but
2144 */
2145 spin_lock_irqsave(&port->lock, flags);
2146 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2147 spin_unlock_irqrestore(&port->lock, flags);
2149 /*
2150 * If this driver supports console, and it hasn't been
2151 * successfully registered yet, try to re-register it.
2152 * It may be that the port was not available.
2153 */
2154 if (port->cons && !(port->cons->flags & CON_ENABLED))
2155 register_console(port->cons);
2157 /*
2158 * Power down all ports by default, except the
2159 * console if we have one.
2160 */
2161 if (!uart_console(port))
2162 uart_change_pm(state, 3);
2163 }
2164 }
2166 #ifdef CONFIG_CONSOLE_POLL
2168 static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2169 {
2170 struct uart_driver *drv = driver->driver_state;
2171 struct uart_state *state = drv->state + line;
2172 struct uart_port *port;
2173 int baud = 9600;
2174 int bits = 8;
2175 int parity = 'n';
2176 int flow = 'n';
2177 int ret;
2179 if (!state || !state->uart_port)
2180 return -1;
2182 port = state->uart_port;
2183 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2184 return -1;
2186 if (port->ops->poll_init) {
2187 struct tty_port *tport = &state->port;
2189 ret = 0;
2190 mutex_lock(&tport->mutex);
2191 /*
2192 * We don't set ASYNCB_INITIALIZED as we only initialized the
2193 * hw, e.g. state->xmit is still uninitialized.
2194 */
2195 if (!test_bit(ASYNCB_INITIALIZED, &tport->flags))
2196 ret = port->ops->poll_init(port);
2197 mutex_unlock(&tport->mutex);
2198 if (ret)
2199 return ret;
2200 }
2202 if (options) {
2203 uart_parse_options(options, &baud, &parity, &bits, &flow);
2204 return uart_set_options(port, NULL, baud, parity, bits, flow);
2205 }
2207 return 0;
2208 }
2210 static int uart_poll_get_char(struct tty_driver *driver, int line)
2211 {
2212 struct uart_driver *drv = driver->driver_state;
2213 struct uart_state *state = drv->state + line;
2214 struct uart_port *port;
2216 if (!state || !state->uart_port)
2217 return -1;
2219 port = state->uart_port;
2220 return port->ops->poll_get_char(port);
2221 }
2223 static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2224 {
2225 struct uart_driver *drv = driver->driver_state;
2226 struct uart_state *state = drv->state + line;
2227 struct uart_port *port;
2229 if (!state || !state->uart_port)
2230 return;
2232 port = state->uart_port;
2233 port->ops->poll_put_char(port, ch);
2234 }
2235 #endif
2237 static const struct tty_operations uart_ops = {
2238 .open = uart_open,
2239 .close = uart_close,
2240 .write = uart_write,
2241 .put_char = uart_put_char,
2242 .flush_chars = uart_flush_chars,
2243 .write_room = uart_write_room,
2244 .chars_in_buffer= uart_chars_in_buffer,
2245 .flush_buffer = uart_flush_buffer,
2246 .ioctl = uart_ioctl,
2247 .throttle = uart_throttle,
2248 .unthrottle = uart_unthrottle,
2249 .send_xchar = uart_send_xchar,
2250 .set_termios = uart_set_termios,
2251 .set_ldisc = uart_set_ldisc,
2252 .stop = uart_stop,
2253 .start = uart_start,
2254 .hangup = uart_hangup,
2255 .break_ctl = uart_break_ctl,
2256 .wait_until_sent= uart_wait_until_sent,
2257 #ifdef CONFIG_PROC_FS
2258 .proc_fops = &uart_proc_fops,
2259 #endif
2260 .tiocmget = uart_tiocmget,
2261 .tiocmset = uart_tiocmset,
2262 .get_icount = uart_get_icount,
2263 #ifdef CONFIG_CONSOLE_POLL
2264 .poll_init = uart_poll_init,
2265 .poll_get_char = uart_poll_get_char,
2266 .poll_put_char = uart_poll_put_char,
2267 #endif
2268 };
2270 static const struct tty_port_operations uart_port_ops = {
2271 .activate = uart_port_activate,
2272 .shutdown = uart_port_shutdown,
2273 .carrier_raised = uart_carrier_raised,
2274 .dtr_rts = uart_dtr_rts,
2275 };
2277 /**
2278 * uart_register_driver - register a driver with the uart core layer
2279 * @drv: low level driver structure
2280 *
2281 * Register a uart driver with the core driver. We in turn register
2282 * with the tty layer, and initialise the core driver per-port state.
2283 *
2284 * We have a proc file in /proc/tty/driver which is named after the
2285 * normal driver.
2286 *
2287 * drv->port should be NULL, and the per-port structures should be
2288 * registered using uart_add_one_port after this call has succeeded.
2289 */
2290 int uart_register_driver(struct uart_driver *drv)
2291 {
2292 struct tty_driver *normal;
2293 int i, retval;
2295 BUG_ON(drv->state);
2297 /*
2298 * Maybe we should be using a slab cache for this, especially if
2299 * we have a large number of ports to handle.
2300 */
2301 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2302 if (!drv->state)
2303 goto out;
2305 normal = alloc_tty_driver(drv->nr);
2306 if (!normal)
2307 goto out_kfree;
2309 drv->tty_driver = normal;
2311 normal->driver_name = drv->driver_name;
2312 normal->name = drv->dev_name;
2313 normal->major = drv->major;
2314 normal->minor_start = drv->minor;
2315 normal->type = TTY_DRIVER_TYPE_SERIAL;
2316 normal->subtype = SERIAL_TYPE_NORMAL;
2317 normal->init_termios = tty_std_termios;
2318 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2319 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2320 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2321 normal->driver_state = drv;
2322 tty_set_operations(normal, &uart_ops);
2324 /*
2325 * Initialise the UART state(s).
2326 */
2327 for (i = 0; i < drv->nr; i++) {
2328 struct uart_state *state = drv->state + i;
2329 struct tty_port *port = &state->port;
2331 tty_port_init(port);
2332 port->ops = &uart_port_ops;
2333 port->close_delay = HZ / 2; /* .5 seconds */
2334 port->closing_wait = 30 * HZ;/* 30 seconds */
2335 }
2337 retval = tty_register_driver(normal);
2338 if (retval >= 0)
2339 return retval;
2341 for (i = 0; i < drv->nr; i++)
2342 tty_port_destroy(&drv->state[i].port);
2343 put_tty_driver(normal);
2344 out_kfree:
2345 kfree(drv->state);
2346 out:
2347 return -ENOMEM;
2348 }
2350 /**
2351 * uart_unregister_driver - remove a driver from the uart core layer
2352 * @drv: low level driver structure
2353 *
2354 * Remove all references to a driver from the core driver. The low
2355 * level driver must have removed all its ports via the
2356 * uart_remove_one_port() if it registered them with uart_add_one_port().
2357 * (ie, drv->port == NULL)
2358 */
2359 void uart_unregister_driver(struct uart_driver *drv)
2360 {
2361 struct tty_driver *p = drv->tty_driver;
2362 unsigned int i;
2364 tty_unregister_driver(p);
2365 put_tty_driver(p);
2366 for (i = 0; i < drv->nr; i++)
2367 tty_port_destroy(&drv->state[i].port);
2368 kfree(drv->state);
2369 drv->state = NULL;
2370 drv->tty_driver = NULL;
2371 }
2373 struct tty_driver *uart_console_device(struct console *co, int *index)
2374 {
2375 struct uart_driver *p = co->data;
2376 *index = co->index;
2377 return p->tty_driver;
2378 }
2380 static ssize_t uart_get_attr_uartclk(struct device *dev,
2381 struct device_attribute *attr, char *buf)
2382 {
2383 struct serial_struct tmp;
2384 struct tty_port *port = dev_get_drvdata(dev);
2386 uart_get_info(port, &tmp);
2387 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16);
2388 }
2390 static ssize_t uart_get_attr_type(struct device *dev,
2391 struct device_attribute *attr, char *buf)
2392 {
2393 struct serial_struct tmp;
2394 struct tty_port *port = dev_get_drvdata(dev);
2396 uart_get_info(port, &tmp);
2397 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type);
2398 }
2399 static ssize_t uart_get_attr_line(struct device *dev,
2400 struct device_attribute *attr, char *buf)
2401 {
2402 struct serial_struct tmp;
2403 struct tty_port *port = dev_get_drvdata(dev);
2405 uart_get_info(port, &tmp);
2406 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line);
2407 }
2409 static ssize_t uart_get_attr_port(struct device *dev,
2410 struct device_attribute *attr, char *buf)
2411 {
2412 struct serial_struct tmp;
2413 struct tty_port *port = dev_get_drvdata(dev);
2414 unsigned long ioaddr;
2416 uart_get_info(port, &tmp);
2417 ioaddr = tmp.port;
2418 if (HIGH_BITS_OFFSET)
2419 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
2420 return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr);
2421 }
2423 static ssize_t uart_get_attr_irq(struct device *dev,
2424 struct device_attribute *attr, char *buf)
2425 {
2426 struct serial_struct tmp;
2427 struct tty_port *port = dev_get_drvdata(dev);
2429 uart_get_info(port, &tmp);
2430 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq);
2431 }
2433 static ssize_t uart_get_attr_flags(struct device *dev,
2434 struct device_attribute *attr, char *buf)
2435 {
2436 struct serial_struct tmp;
2437 struct tty_port *port = dev_get_drvdata(dev);
2439 uart_get_info(port, &tmp);
2440 return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags);
2441 }
2443 static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev,
2444 struct device_attribute *attr, char *buf)
2445 {
2446 struct serial_struct tmp;
2447 struct tty_port *port = dev_get_drvdata(dev);
2449 uart_get_info(port, &tmp);
2450 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size);
2451 }
2454 static ssize_t uart_get_attr_close_delay(struct device *dev,
2455 struct device_attribute *attr, char *buf)
2456 {
2457 struct serial_struct tmp;
2458 struct tty_port *port = dev_get_drvdata(dev);
2460 uart_get_info(port, &tmp);
2461 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay);
2462 }
2465 static ssize_t uart_get_attr_closing_wait(struct device *dev,
2466 struct device_attribute *attr, char *buf)
2467 {
2468 struct serial_struct tmp;
2469 struct tty_port *port = dev_get_drvdata(dev);
2471 uart_get_info(port, &tmp);
2472 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait);
2473 }
2475 static ssize_t uart_get_attr_custom_divisor(struct device *dev,
2476 struct device_attribute *attr, char *buf)
2477 {
2478 struct serial_struct tmp;
2479 struct tty_port *port = dev_get_drvdata(dev);
2481 uart_get_info(port, &tmp);
2482 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor);
2483 }
2485 static ssize_t uart_get_attr_io_type(struct device *dev,
2486 struct device_attribute *attr, char *buf)
2487 {
2488 struct serial_struct tmp;
2489 struct tty_port *port = dev_get_drvdata(dev);
2491 uart_get_info(port, &tmp);
2492 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type);
2493 }
2495 static ssize_t uart_get_attr_iomem_base(struct device *dev,
2496 struct device_attribute *attr, char *buf)
2497 {
2498 struct serial_struct tmp;
2499 struct tty_port *port = dev_get_drvdata(dev);
2501 uart_get_info(port, &tmp);
2502 return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base);
2503 }
2505 static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev,
2506 struct device_attribute *attr, char *buf)
2507 {
2508 struct serial_struct tmp;
2509 struct tty_port *port = dev_get_drvdata(dev);
2511 uart_get_info(port, &tmp);
2512 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift);
2513 }
2515 static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL);
2516 static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL);
2517 static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL);
2518 static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL);
2519 static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL);
2520 static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL);
2521 static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL);
2522 static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL);
2523 static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL);
2524 static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL);
2525 static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL);
2526 static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL);
2527 static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL);
2529 static struct attribute *tty_dev_attrs[] = {
2530 &dev_attr_type.attr,
2531 &dev_attr_line.attr,
2532 &dev_attr_port.attr,
2533 &dev_attr_irq.attr,
2534 &dev_attr_flags.attr,
2535 &dev_attr_xmit_fifo_size.attr,
2536 &dev_attr_uartclk.attr,
2537 &dev_attr_close_delay.attr,
2538 &dev_attr_closing_wait.attr,
2539 &dev_attr_custom_divisor.attr,
2540 &dev_attr_io_type.attr,
2541 &dev_attr_iomem_base.attr,
2542 &dev_attr_iomem_reg_shift.attr,
2543 NULL,
2544 };
2546 static const struct attribute_group tty_dev_attr_group = {
2547 .attrs = tty_dev_attrs,
2548 };
2550 static const struct attribute_group *tty_dev_attr_groups[] = {
2551 &tty_dev_attr_group,
2552 NULL
2553 };
2556 /**
2557 * uart_add_one_port - attach a driver-defined port structure
2558 * @drv: pointer to the uart low level driver structure for this port
2559 * @uport: uart port structure to use for this port.
2560 *
2561 * This allows the driver to register its own uart_port structure
2562 * with the core driver. The main purpose is to allow the low
2563 * level uart drivers to expand uart_port, rather than having yet
2564 * more levels of structures.
2565 */
2566 int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
2567 {
2568 struct uart_state *state;
2569 struct tty_port *port;
2570 int ret = 0;
2571 struct device *tty_dev;
2573 BUG_ON(in_interrupt());
2575 if (uport->line >= drv->nr)
2576 return -EINVAL;
2578 state = drv->state + uport->line;
2579 port = &state->port;
2581 mutex_lock(&port_mutex);
2582 mutex_lock(&port->mutex);
2583 if (state->uart_port) {
2584 ret = -EINVAL;
2585 goto out;
2586 }
2588 state->uart_port = uport;
2589 state->pm_state = -1;
2591 uport->cons = drv->cons;
2592 uport->state = state;
2594 /*
2595 * If this port is a console, then the spinlock is already
2596 * initialised.
2597 */
2598 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2599 spin_lock_init(&uport->lock);
2600 lockdep_set_class(&uport->lock, &port_lock_key);
2601 }
2603 uart_configure_port(drv, state, uport);
2605 /*
2606 * Register the port whether it's detected or not. This allows
2607 * setserial to be used to alter this ports parameters.
2608 */
2609 tty_dev = tty_port_register_device_attr(port, drv->tty_driver,
2610 uport->line, uport->dev, port, tty_dev_attr_groups);
2611 if (likely(!IS_ERR(tty_dev))) {
2612 device_set_wakeup_capable(tty_dev, 1);
2613 } else {
2614 printk(KERN_ERR "Cannot register tty device on line %d\n",
2615 uport->line);
2616 }
2618 /*
2619 * Ensure UPF_DEAD is not set.
2620 */
2621 uport->flags &= ~UPF_DEAD;
2623 out:
2624 mutex_unlock(&port->mutex);
2625 mutex_unlock(&port_mutex);
2627 return ret;
2628 }
2630 /**
2631 * uart_remove_one_port - detach a driver defined port structure
2632 * @drv: pointer to the uart low level driver structure for this port
2633 * @uport: uart port structure for this port
2634 *
2635 * This unhooks (and hangs up) the specified port structure from the
2636 * core driver. No further calls will be made to the low-level code
2637 * for this port.
2638 */
2639 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
2640 {
2641 struct uart_state *state = drv->state + uport->line;
2642 struct tty_port *port = &state->port;
2644 BUG_ON(in_interrupt());
2646 if (state->uart_port != uport)
2647 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2648 state->uart_port, uport);
2650 mutex_lock(&port_mutex);
2652 /*
2653 * Mark the port "dead" - this prevents any opens from
2654 * succeeding while we shut down the port.
2655 */
2656 mutex_lock(&port->mutex);
2657 uport->flags |= UPF_DEAD;
2658 mutex_unlock(&port->mutex);
2660 /*
2661 * Remove the devices from the tty layer
2662 */
2663 tty_unregister_device(drv->tty_driver, uport->line);
2665 if (port->tty)
2666 tty_vhangup(port->tty);
2668 /*
2669 * Free the port IO and memory resources, if any.
2670 */
2671 if (uport->type != PORT_UNKNOWN)
2672 uport->ops->release_port(uport);
2674 /*
2675 * Indicate that there isn't a port here anymore.
2676 */
2677 uport->type = PORT_UNKNOWN;
2679 state->uart_port = NULL;
2680 mutex_unlock(&port_mutex);
2682 return 0;
2683 }
2685 /*
2686 * Are the two ports equivalent?
2687 */
2688 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2689 {
2690 if (port1->iotype != port2->iotype)
2691 return 0;
2693 switch (port1->iotype) {
2694 case UPIO_PORT:
2695 return (port1->iobase == port2->iobase);
2696 case UPIO_HUB6:
2697 return (port1->iobase == port2->iobase) &&
2698 (port1->hub6 == port2->hub6);
2699 case UPIO_MEM:
2700 case UPIO_MEM32:
2701 case UPIO_AU:
2702 case UPIO_TSI:
2703 return (port1->mapbase == port2->mapbase);
2704 }
2705 return 0;
2706 }
2707 EXPORT_SYMBOL(uart_match_port);
2709 /**
2710 * uart_handle_dcd_change - handle a change of carrier detect state
2711 * @uport: uart_port structure for the open port
2712 * @status: new carrier detect status, nonzero if active
2713 */
2714 void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
2715 {
2716 struct uart_state *state = uport->state;
2717 struct tty_port *port = &state->port;
2718 struct tty_ldisc *ld = NULL;
2719 struct pps_event_time ts;
2720 struct tty_struct *tty = port->tty;
2722 if (tty)
2723 ld = tty_ldisc_ref(tty);
2724 if (ld && ld->ops->dcd_change)
2725 pps_get_ts(&ts);
2727 uport->icount.dcd++;
2728 #ifdef CONFIG_HARD_PPS
2729 if ((uport->flags & UPF_HARDPPS_CD) && status)
2730 hardpps();
2731 #endif
2733 if (port->flags & ASYNC_CHECK_CD) {
2734 if (status)
2735 wake_up_interruptible(&port->open_wait);
2736 else if (tty)
2737 tty_hangup(tty);
2738 }
2740 if (ld && ld->ops->dcd_change)
2741 ld->ops->dcd_change(tty, status, &ts);
2742 if (ld)
2743 tty_ldisc_deref(ld);
2744 }
2745 EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
2747 /**
2748 * uart_handle_cts_change - handle a change of clear-to-send state
2749 * @uport: uart_port structure for the open port
2750 * @status: new clear to send status, nonzero if active
2751 */
2752 void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
2753 {
2754 struct tty_port *port = &uport->state->port;
2755 struct tty_struct *tty = port->tty;
2757 uport->icount.cts++;
2759 if (tty_port_cts_enabled(port)) {
2760 if (tty->hw_stopped) {
2761 if (status) {
2762 tty->hw_stopped = 0;
2763 uport->ops->start_tx(uport);
2764 uart_write_wakeup(uport);
2765 }
2766 } else {
2767 if (!status) {
2768 tty->hw_stopped = 1;
2769 uport->ops->stop_tx(uport);
2770 }
2771 }
2772 }
2773 }
2774 EXPORT_SYMBOL_GPL(uart_handle_cts_change);
2776 /**
2777 * uart_insert_char - push a char to the uart layer
2778 *
2779 * User is responsible to call tty_flip_buffer_push when they are done with
2780 * insertion.
2781 *
2782 * @port: corresponding port
2783 * @status: state of the serial port RX buffer (LSR for 8250)
2784 * @overrun: mask of overrun bits in @status
2785 * @ch: character to push
2786 * @flag: flag for the character (see TTY_NORMAL and friends)
2787 */
2788 void uart_insert_char(struct uart_port *port, unsigned int status,
2789 unsigned int overrun, unsigned int ch, unsigned int flag)
2790 {
2791 struct tty_struct *tty = port->state->port.tty;
2793 if ((status & port->ignore_status_mask & ~overrun) == 0)
2794 if (tty_insert_flip_char(tty, ch, flag) == 0)
2795 ++port->icount.buf_overrun;
2797 /*
2798 * Overrun is special. Since it's reported immediately,
2799 * it doesn't affect the current character.
2800 */
2801 if (status & ~port->ignore_status_mask & overrun)
2802 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN) == 0)
2803 ++port->icount.buf_overrun;
2804 }
2805 EXPORT_SYMBOL_GPL(uart_insert_char);
2807 EXPORT_SYMBOL(uart_write_wakeup);
2808 EXPORT_SYMBOL(uart_register_driver);
2809 EXPORT_SYMBOL(uart_unregister_driver);
2810 EXPORT_SYMBOL(uart_suspend_port);
2811 EXPORT_SYMBOL(uart_resume_port);
2812 EXPORT_SYMBOL(uart_add_one_port);
2813 EXPORT_SYMBOL(uart_remove_one_port);
2815 MODULE_DESCRIPTION("Serial driver core");
2816 MODULE_LICENSE("GPL");