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