9d42e57e197135b5b2978acb921c2d418daaa67c
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / serial / serial_core.c
1 /*
2 * linux/drivers/char/core.c
3 *
4 * Driver core for serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * Copyright 1999 ARM Limited
9 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25 #include <linux/module.h>
26 #include <linux/tty.h>
27 #include <linux/slab.h>
28 #include <linux/init.h>
29 #include <linux/console.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/smp_lock.h>
33 #include <linux/device.h>
34 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
35 #include <linux/serial_core.h>
36 #include <linux/delay.h>
37 #include <linux/mutex.h>
38
39 #include <asm/irq.h>
40 #include <asm/uaccess.h>
41
42 /*
43 * This is used to lock changes in serial line configuration.
44 */
45 static DEFINE_MUTEX(port_mutex);
46
47 /*
48 * lockdep: port->lock is initialized in two places, but we
49 * want only one lock-class:
50 */
51 static struct lock_class_key port_lock_key;
52
53 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
54
55 #define uart_users(state) ((state)->port.count + (state)->port.blocked_open)
56
57 #ifdef CONFIG_SERIAL_CORE_CONSOLE
58 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
59 #else
60 #define uart_console(port) (0)
61 #endif
62
63 static void uart_change_speed(struct uart_state *state,
64 struct ktermios *old_termios);
65 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
66 static void uart_change_pm(struct uart_state *state, int pm_state);
67
68 /*
69 * This routine is used by the interrupt handler to schedule processing in
70 * the software interrupt portion of the driver.
71 */
72 void uart_write_wakeup(struct uart_port *port)
73 {
74 struct uart_state *state = port->state;
75 /*
76 * This means you called this function _after_ the port was
77 * closed. No cookie for you.
78 */
79 BUG_ON(!state);
80 tasklet_schedule(&state->tlet);
81 }
82
83 static void uart_stop(struct tty_struct *tty)
84 {
85 struct uart_state *state = tty->driver_data;
86 struct uart_port *port = state->uart_port;
87 unsigned long flags;
88
89 spin_lock_irqsave(&port->lock, flags);
90 port->ops->stop_tx(port);
91 spin_unlock_irqrestore(&port->lock, flags);
92 }
93
94 static void __uart_start(struct tty_struct *tty)
95 {
96 struct uart_state *state = tty->driver_data;
97 struct uart_port *port = state->uart_port;
98
99 if (!uart_circ_empty(&state->xmit) && state->xmit.buf &&
100 !tty->stopped && !tty->hw_stopped)
101 port->ops->start_tx(port);
102 }
103
104 static void uart_start(struct tty_struct *tty)
105 {
106 struct uart_state *state = tty->driver_data;
107 struct uart_port *port = state->uart_port;
108 unsigned long flags;
109
110 spin_lock_irqsave(&port->lock, flags);
111 __uart_start(tty);
112 spin_unlock_irqrestore(&port->lock, flags);
113 }
114
115 static void uart_tasklet_action(unsigned long data)
116 {
117 struct uart_state *state = (struct uart_state *)data;
118 tty_wakeup(state->port.tty);
119 }
120
121 static inline void
122 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
123 {
124 unsigned long flags;
125 unsigned int old;
126
127 spin_lock_irqsave(&port->lock, flags);
128 old = port->mctrl;
129 port->mctrl = (old & ~clear) | set;
130 if (old != port->mctrl)
131 port->ops->set_mctrl(port, port->mctrl);
132 spin_unlock_irqrestore(&port->lock, flags);
133 }
134
135 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
136 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
137
138 /*
139 * Startup the port. This will be called once per open. All calls
140 * will be serialised by the per-port mutex.
141 */
142 static int uart_startup(struct uart_state *state, int init_hw)
143 {
144 struct uart_port *uport = state->uart_port;
145 struct tty_port *port = &state->port;
146 unsigned long page;
147 int retval = 0;
148
149 if (port->flags & ASYNC_INITIALIZED)
150 return 0;
151
152 /*
153 * Set the TTY IO error marker - we will only clear this
154 * once we have successfully opened the port. Also set
155 * up the tty->alt_speed kludge
156 */
157 set_bit(TTY_IO_ERROR, &port->tty->flags);
158
159 if (uport->type == PORT_UNKNOWN)
160 return 0;
161
162 /*
163 * Initialise and allocate the transmit and temporary
164 * buffer.
165 */
166 if (!state->xmit.buf) {
167 /* This is protected by the per port mutex */
168 page = get_zeroed_page(GFP_KERNEL);
169 if (!page)
170 return -ENOMEM;
171
172 state->xmit.buf = (unsigned char *) page;
173 uart_circ_clear(&state->xmit);
174 }
175
176 retval = uport->ops->startup(uport);
177 if (retval == 0) {
178 if (init_hw) {
179 /*
180 * Initialise the hardware port settings.
181 */
182 uart_change_speed(state, NULL);
183
184 /*
185 * Setup the RTS and DTR signals once the
186 * port is open and ready to respond.
187 */
188 if (port->tty->termios->c_cflag & CBAUD)
189 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
190 }
191
192 if (port->flags & ASYNC_CTS_FLOW) {
193 spin_lock_irq(&uport->lock);
194 if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS))
195 port->tty->hw_stopped = 1;
196 spin_unlock_irq(&uport->lock);
197 }
198
199 set_bit(ASYNCB_INITIALIZED, &port->flags);
200
201 clear_bit(TTY_IO_ERROR, &port->tty->flags);
202 }
203
204 if (retval && capable(CAP_SYS_ADMIN))
205 retval = 0;
206
207 return retval;
208 }
209
210 /*
211 * This routine will shutdown a serial port; interrupts are disabled, and
212 * DTR is dropped if the hangup on close termio flag is on. Calls to
213 * uart_shutdown are serialised by the per-port semaphore.
214 */
215 static void uart_shutdown(struct uart_state *state)
216 {
217 struct uart_port *uport = state->uart_port;
218 struct tty_struct *tty = state->port.tty;
219
220 /*
221 * Set the TTY IO error marker
222 */
223 if (tty)
224 set_bit(TTY_IO_ERROR, &tty->flags);
225
226 if (test_and_clear_bit(ASYNCB_INITIALIZED, &state->port.flags)) {
227 /*
228 * Turn off DTR and RTS early.
229 */
230 if (!tty || (tty->termios->c_cflag & HUPCL))
231 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
232
233 /*
234 * clear delta_msr_wait queue to avoid mem leaks: we may free
235 * the irq here so the queue might never be woken up. Note
236 * that we won't end up waiting on delta_msr_wait again since
237 * any outstanding file descriptors should be pointing at
238 * hung_up_tty_fops now.
239 */
240 wake_up_interruptible(&state->delta_msr_wait);
241
242 /*
243 * Free the IRQ and disable the port.
244 */
245 uport->ops->shutdown(uport);
246
247 /*
248 * Ensure that the IRQ handler isn't running on another CPU.
249 */
250 synchronize_irq(uport->irq);
251 }
252
253 /*
254 * kill off our tasklet
255 */
256 tasklet_kill(&state->tlet);
257
258 /*
259 * Free the transmit buffer page.
260 */
261 if (state->xmit.buf) {
262 free_page((unsigned long)state->xmit.buf);
263 state->xmit.buf = NULL;
264 }
265 }
266
267 /**
268 * uart_update_timeout - update per-port FIFO timeout.
269 * @port: uart_port structure describing the port
270 * @cflag: termios cflag value
271 * @baud: speed of the port
272 *
273 * Set the port FIFO timeout value. The @cflag value should
274 * reflect the actual hardware settings.
275 */
276 void
277 uart_update_timeout(struct uart_port *port, unsigned int cflag,
278 unsigned int baud)
279 {
280 unsigned int bits;
281
282 /* byte size and parity */
283 switch (cflag & CSIZE) {
284 case CS5:
285 bits = 7;
286 break;
287 case CS6:
288 bits = 8;
289 break;
290 case CS7:
291 bits = 9;
292 break;
293 default:
294 bits = 10;
295 break; /* CS8 */
296 }
297
298 if (cflag & CSTOPB)
299 bits++;
300 if (cflag & PARENB)
301 bits++;
302
303 /*
304 * The total number of bits to be transmitted in the fifo.
305 */
306 bits = bits * port->fifosize;
307
308 /*
309 * Figure the timeout to send the above number of bits.
310 * Add .02 seconds of slop
311 */
312 port->timeout = (HZ * bits) / baud + HZ/50;
313 }
314
315 EXPORT_SYMBOL(uart_update_timeout);
316
317 /**
318 * uart_get_baud_rate - return baud rate for a particular port
319 * @port: uart_port structure describing the port in question.
320 * @termios: desired termios settings.
321 * @old: old termios (or NULL)
322 * @min: minimum acceptable baud rate
323 * @max: maximum acceptable baud rate
324 *
325 * Decode the termios structure into a numeric baud rate,
326 * taking account of the magic 38400 baud rate (with spd_*
327 * flags), and mapping the %B0 rate to 9600 baud.
328 *
329 * If the new baud rate is invalid, try the old termios setting.
330 * If it's still invalid, we try 9600 baud.
331 *
332 * Update the @termios structure to reflect the baud rate
333 * we're actually going to be using. Don't do this for the case
334 * where B0 is requested ("hang up").
335 */
336 unsigned int
337 uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
338 struct ktermios *old, unsigned int min, unsigned int max)
339 {
340 unsigned int try, baud, altbaud = 38400;
341 int hung_up = 0;
342 upf_t flags = port->flags & UPF_SPD_MASK;
343
344 if (flags == UPF_SPD_HI)
345 altbaud = 57600;
346 if (flags == UPF_SPD_VHI)
347 altbaud = 115200;
348 if (flags == UPF_SPD_SHI)
349 altbaud = 230400;
350 if (flags == UPF_SPD_WARP)
351 altbaud = 460800;
352
353 for (try = 0; try < 2; try++) {
354 baud = tty_termios_baud_rate(termios);
355
356 /*
357 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
358 * Die! Die! Die!
359 */
360 if (baud == 38400)
361 baud = altbaud;
362
363 /*
364 * Special case: B0 rate.
365 */
366 if (baud == 0) {
367 hung_up = 1;
368 baud = 9600;
369 }
370
371 if (baud >= min && baud <= max)
372 return baud;
373
374 /*
375 * Oops, the quotient was zero. Try again with
376 * the old baud rate if possible.
377 */
378 termios->c_cflag &= ~CBAUD;
379 if (old) {
380 baud = tty_termios_baud_rate(old);
381 if (!hung_up)
382 tty_termios_encode_baud_rate(termios,
383 baud, baud);
384 old = NULL;
385 continue;
386 }
387
388 /*
389 * As a last resort, if the quotient is zero,
390 * default to 9600 bps
391 */
392 if (!hung_up)
393 tty_termios_encode_baud_rate(termios, 9600, 9600);
394 }
395
396 return 0;
397 }
398
399 EXPORT_SYMBOL(uart_get_baud_rate);
400
401 /**
402 * uart_get_divisor - return uart clock divisor
403 * @port: uart_port structure describing the port.
404 * @baud: desired baud rate
405 *
406 * Calculate the uart clock divisor for the port.
407 */
408 unsigned int
409 uart_get_divisor(struct uart_port *port, unsigned int baud)
410 {
411 unsigned int quot;
412
413 /*
414 * Old custom speed handling.
415 */
416 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
417 quot = port->custom_divisor;
418 else
419 quot = (port->uartclk + (8 * baud)) / (16 * baud);
420
421 return quot;
422 }
423
424 EXPORT_SYMBOL(uart_get_divisor);
425
426 /* FIXME: Consistent locking policy */
427 static void
428 uart_change_speed(struct uart_state *state, struct ktermios *old_termios)
429 {
430 struct tty_port *port = &state->port;
431 struct tty_struct *tty = port->tty;
432 struct uart_port *uport = state->uart_port;
433 struct ktermios *termios;
434
435 /*
436 * If we have no tty, termios, or the port does not exist,
437 * then we can't set the parameters for this port.
438 */
439 if (!tty || !tty->termios || uport->type == PORT_UNKNOWN)
440 return;
441
442 termios = tty->termios;
443
444 /*
445 * Set flags based on termios cflag
446 */
447 if (termios->c_cflag & CRTSCTS)
448 set_bit(ASYNCB_CTS_FLOW, &port->flags);
449 else
450 clear_bit(ASYNCB_CTS_FLOW, &port->flags);
451
452 if (termios->c_cflag & CLOCAL)
453 clear_bit(ASYNCB_CHECK_CD, &port->flags);
454 else
455 set_bit(ASYNCB_CHECK_CD, &port->flags);
456
457 uport->ops->set_termios(uport, termios, old_termios);
458 }
459
460 static inline int
461 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
462 {
463 unsigned long flags;
464 int ret = 0;
465
466 if (!circ->buf)
467 return 0;
468
469 spin_lock_irqsave(&port->lock, flags);
470 if (uart_circ_chars_free(circ) != 0) {
471 circ->buf[circ->head] = c;
472 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
473 ret = 1;
474 }
475 spin_unlock_irqrestore(&port->lock, flags);
476 return ret;
477 }
478
479 static int uart_put_char(struct tty_struct *tty, unsigned char ch)
480 {
481 struct uart_state *state = tty->driver_data;
482
483 return __uart_put_char(state->uart_port, &state->xmit, ch);
484 }
485
486 static void uart_flush_chars(struct tty_struct *tty)
487 {
488 uart_start(tty);
489 }
490
491 static int
492 uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
493 {
494 struct uart_state *state = tty->driver_data;
495 struct uart_port *port;
496 struct circ_buf *circ;
497 unsigned long flags;
498 int c, ret = 0;
499
500 /*
501 * This means you called this function _after_ the port was
502 * closed. No cookie for you.
503 */
504 if (!state) {
505 WARN_ON(1);
506 return -EL3HLT;
507 }
508
509 port = state->uart_port;
510 circ = &state->xmit;
511
512 if (!circ->buf)
513 return 0;
514
515 spin_lock_irqsave(&port->lock, flags);
516 while (1) {
517 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
518 if (count < c)
519 c = count;
520 if (c <= 0)
521 break;
522 memcpy(circ->buf + circ->head, buf, c);
523 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
524 buf += c;
525 count -= c;
526 ret += c;
527 }
528 spin_unlock_irqrestore(&port->lock, flags);
529
530 uart_start(tty);
531 return ret;
532 }
533
534 static int uart_write_room(struct tty_struct *tty)
535 {
536 struct uart_state *state = tty->driver_data;
537 unsigned long flags;
538 int ret;
539
540 spin_lock_irqsave(&state->uart_port->lock, flags);
541 ret = uart_circ_chars_free(&state->xmit);
542 spin_unlock_irqrestore(&state->uart_port->lock, flags);
543 return ret;
544 }
545
546 static int uart_chars_in_buffer(struct tty_struct *tty)
547 {
548 struct uart_state *state = tty->driver_data;
549 unsigned long flags;
550 int ret;
551
552 spin_lock_irqsave(&state->uart_port->lock, flags);
553 ret = uart_circ_chars_pending(&state->xmit);
554 spin_unlock_irqrestore(&state->uart_port->lock, flags);
555 return ret;
556 }
557
558 static void uart_flush_buffer(struct tty_struct *tty)
559 {
560 struct uart_state *state = tty->driver_data;
561 struct uart_port *port;
562 unsigned long flags;
563
564 /*
565 * This means you called this function _after_ the port was
566 * closed. No cookie for you.
567 */
568 if (!state) {
569 WARN_ON(1);
570 return;
571 }
572
573 port = state->uart_port;
574 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
575
576 spin_lock_irqsave(&port->lock, flags);
577 uart_circ_clear(&state->xmit);
578 if (port->ops->flush_buffer)
579 port->ops->flush_buffer(port);
580 spin_unlock_irqrestore(&port->lock, flags);
581 tty_wakeup(tty);
582 }
583
584 /*
585 * This function is used to send a high-priority XON/XOFF character to
586 * the device
587 */
588 static void uart_send_xchar(struct tty_struct *tty, char ch)
589 {
590 struct uart_state *state = tty->driver_data;
591 struct uart_port *port = state->uart_port;
592 unsigned long flags;
593
594 if (port->ops->send_xchar)
595 port->ops->send_xchar(port, ch);
596 else {
597 port->x_char = ch;
598 if (ch) {
599 spin_lock_irqsave(&port->lock, flags);
600 port->ops->start_tx(port);
601 spin_unlock_irqrestore(&port->lock, flags);
602 }
603 }
604 }
605
606 static void uart_throttle(struct tty_struct *tty)
607 {
608 struct uart_state *state = tty->driver_data;
609
610 if (I_IXOFF(tty))
611 uart_send_xchar(tty, STOP_CHAR(tty));
612
613 if (tty->termios->c_cflag & CRTSCTS)
614 uart_clear_mctrl(state->uart_port, TIOCM_RTS);
615 }
616
617 static void uart_unthrottle(struct tty_struct *tty)
618 {
619 struct uart_state *state = tty->driver_data;
620 struct uart_port *port = state->uart_port;
621
622 if (I_IXOFF(tty)) {
623 if (port->x_char)
624 port->x_char = 0;
625 else
626 uart_send_xchar(tty, START_CHAR(tty));
627 }
628
629 if (tty->termios->c_cflag & CRTSCTS)
630 uart_set_mctrl(port, TIOCM_RTS);
631 }
632
633 static int uart_get_info(struct uart_state *state,
634 struct serial_struct __user *retinfo)
635 {
636 struct uart_port *uport = state->uart_port;
637 struct tty_port *port = &state->port;
638 struct serial_struct tmp;
639
640 memset(&tmp, 0, sizeof(tmp));
641
642 /* Ensure the state we copy is consistent and no hardware changes
643 occur as we go */
644 mutex_lock(&port->mutex);
645
646 tmp.type = uport->type;
647 tmp.line = uport->line;
648 tmp.port = uport->iobase;
649 if (HIGH_BITS_OFFSET)
650 tmp.port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
651 tmp.irq = uport->irq;
652 tmp.flags = uport->flags;
653 tmp.xmit_fifo_size = uport->fifosize;
654 tmp.baud_base = uport->uartclk / 16;
655 tmp.close_delay = port->close_delay / 10;
656 tmp.closing_wait = port->closing_wait == USF_CLOSING_WAIT_NONE ?
657 ASYNC_CLOSING_WAIT_NONE :
658 port->closing_wait / 10;
659 tmp.custom_divisor = uport->custom_divisor;
660 tmp.hub6 = uport->hub6;
661 tmp.io_type = uport->iotype;
662 tmp.iomem_reg_shift = uport->regshift;
663 tmp.iomem_base = (void *)(unsigned long)uport->mapbase;
664
665 mutex_unlock(&port->mutex);
666
667 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
668 return -EFAULT;
669 return 0;
670 }
671
672 static int uart_set_info(struct uart_state *state,
673 struct serial_struct __user *newinfo)
674 {
675 struct serial_struct new_serial;
676 struct uart_port *uport = state->uart_port;
677 struct tty_port *port = &state->port;
678 unsigned long new_port;
679 unsigned int change_irq, change_port, closing_wait;
680 unsigned int old_custom_divisor, close_delay;
681 upf_t old_flags, new_flags;
682 int retval = 0;
683
684 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
685 return -EFAULT;
686
687 new_port = new_serial.port;
688 if (HIGH_BITS_OFFSET)
689 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
690
691 new_serial.irq = irq_canonicalize(new_serial.irq);
692 close_delay = new_serial.close_delay * 10;
693 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
694 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
695
696 /*
697 * This semaphore protects port->count. It is also
698 * very useful to prevent opens. Also, take the
699 * port configuration semaphore to make sure that a
700 * module insertion/removal doesn't change anything
701 * under us.
702 */
703 mutex_lock(&port->mutex);
704
705 change_irq = !(uport->flags & UPF_FIXED_PORT)
706 && new_serial.irq != uport->irq;
707
708 /*
709 * Since changing the 'type' of the port changes its resource
710 * allocations, we should treat type changes the same as
711 * IO port changes.
712 */
713 change_port = !(uport->flags & UPF_FIXED_PORT)
714 && (new_port != uport->iobase ||
715 (unsigned long)new_serial.iomem_base != uport->mapbase ||
716 new_serial.hub6 != uport->hub6 ||
717 new_serial.io_type != uport->iotype ||
718 new_serial.iomem_reg_shift != uport->regshift ||
719 new_serial.type != uport->type);
720
721 old_flags = uport->flags;
722 new_flags = new_serial.flags;
723 old_custom_divisor = uport->custom_divisor;
724
725 if (!capable(CAP_SYS_ADMIN)) {
726 retval = -EPERM;
727 if (change_irq || change_port ||
728 (new_serial.baud_base != uport->uartclk / 16) ||
729 (close_delay != port->close_delay) ||
730 (closing_wait != port->closing_wait) ||
731 (new_serial.xmit_fifo_size &&
732 new_serial.xmit_fifo_size != uport->fifosize) ||
733 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
734 goto exit;
735 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
736 (new_flags & UPF_USR_MASK));
737 uport->custom_divisor = new_serial.custom_divisor;
738 goto check_and_exit;
739 }
740
741 /*
742 * Ask the low level driver to verify the settings.
743 */
744 if (uport->ops->verify_port)
745 retval = uport->ops->verify_port(uport, &new_serial);
746
747 if ((new_serial.irq >= nr_irqs) || (new_serial.irq < 0) ||
748 (new_serial.baud_base < 9600))
749 retval = -EINVAL;
750
751 if (retval)
752 goto exit;
753
754 if (change_port || change_irq) {
755 retval = -EBUSY;
756
757 /*
758 * Make sure that we are the sole user of this port.
759 */
760 if (uart_users(state) > 1)
761 goto exit;
762
763 /*
764 * We need to shutdown the serial port at the old
765 * port/type/irq combination.
766 */
767 uart_shutdown(state);
768 }
769
770 if (change_port) {
771 unsigned long old_iobase, old_mapbase;
772 unsigned int old_type, old_iotype, old_hub6, old_shift;
773
774 old_iobase = uport->iobase;
775 old_mapbase = uport->mapbase;
776 old_type = uport->type;
777 old_hub6 = uport->hub6;
778 old_iotype = uport->iotype;
779 old_shift = uport->regshift;
780
781 /*
782 * Free and release old regions
783 */
784 if (old_type != PORT_UNKNOWN)
785 uport->ops->release_port(uport);
786
787 uport->iobase = new_port;
788 uport->type = new_serial.type;
789 uport->hub6 = new_serial.hub6;
790 uport->iotype = new_serial.io_type;
791 uport->regshift = new_serial.iomem_reg_shift;
792 uport->mapbase = (unsigned long)new_serial.iomem_base;
793
794 /*
795 * Claim and map the new regions
796 */
797 if (uport->type != PORT_UNKNOWN) {
798 retval = uport->ops->request_port(uport);
799 } else {
800 /* Always success - Jean II */
801 retval = 0;
802 }
803
804 /*
805 * If we fail to request resources for the
806 * new port, try to restore the old settings.
807 */
808 if (retval && old_type != PORT_UNKNOWN) {
809 uport->iobase = old_iobase;
810 uport->type = old_type;
811 uport->hub6 = old_hub6;
812 uport->iotype = old_iotype;
813 uport->regshift = old_shift;
814 uport->mapbase = old_mapbase;
815 retval = uport->ops->request_port(uport);
816 /*
817 * If we failed to restore the old settings,
818 * we fail like this.
819 */
820 if (retval)
821 uport->type = PORT_UNKNOWN;
822
823 /*
824 * We failed anyway.
825 */
826 retval = -EBUSY;
827 /* Added to return the correct error -Ram Gupta */
828 goto exit;
829 }
830 }
831
832 if (change_irq)
833 uport->irq = new_serial.irq;
834 if (!(uport->flags & UPF_FIXED_PORT))
835 uport->uartclk = new_serial.baud_base * 16;
836 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
837 (new_flags & UPF_CHANGE_MASK);
838 uport->custom_divisor = new_serial.custom_divisor;
839 port->close_delay = close_delay;
840 port->closing_wait = closing_wait;
841 if (new_serial.xmit_fifo_size)
842 uport->fifosize = new_serial.xmit_fifo_size;
843 if (port->tty)
844 port->tty->low_latency =
845 (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
846
847 check_and_exit:
848 retval = 0;
849 if (uport->type == PORT_UNKNOWN)
850 goto exit;
851 if (port->flags & ASYNC_INITIALIZED) {
852 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
853 old_custom_divisor != uport->custom_divisor) {
854 /*
855 * If they're setting up a custom divisor or speed,
856 * instead of clearing it, then bitch about it. No
857 * need to rate-limit; it's CAP_SYS_ADMIN only.
858 */
859 if (uport->flags & UPF_SPD_MASK) {
860 char buf[64];
861 printk(KERN_NOTICE
862 "%s sets custom speed on %s. This "
863 "is deprecated.\n", current->comm,
864 tty_name(port->tty, buf));
865 }
866 uart_change_speed(state, NULL);
867 }
868 } else
869 retval = uart_startup(state, 1);
870 exit:
871 mutex_unlock(&port->mutex);
872 return retval;
873 }
874
875
876 /*
877 * uart_get_lsr_info - get line status register info.
878 * Note: uart_ioctl protects us against hangups.
879 */
880 static int uart_get_lsr_info(struct uart_state *state,
881 unsigned int __user *value)
882 {
883 struct uart_port *uport = state->uart_port;
884 struct tty_port *port = &state->port;
885 unsigned int result;
886
887 result = uport->ops->tx_empty(uport);
888
889 /*
890 * If we're about to load something into the transmit
891 * register, we'll pretend the transmitter isn't empty to
892 * avoid a race condition (depending on when the transmit
893 * interrupt happens).
894 */
895 if (uport->x_char ||
896 ((uart_circ_chars_pending(&state->xmit) > 0) &&
897 !port->tty->stopped && !port->tty->hw_stopped))
898 result &= ~TIOCSER_TEMT;
899
900 return put_user(result, value);
901 }
902
903 static int uart_tiocmget(struct tty_struct *tty, struct file *file)
904 {
905 struct uart_state *state = tty->driver_data;
906 struct tty_port *port = &state->port;
907 struct uart_port *uport = state->uart_port;
908 int result = -EIO;
909
910 mutex_lock(&port->mutex);
911 if ((!file || !tty_hung_up_p(file)) &&
912 !(tty->flags & (1 << TTY_IO_ERROR))) {
913 result = uport->mctrl;
914
915 spin_lock_irq(&uport->lock);
916 result |= uport->ops->get_mctrl(uport);
917 spin_unlock_irq(&uport->lock);
918 }
919 mutex_unlock(&port->mutex);
920
921 return result;
922 }
923
924 static int
925 uart_tiocmset(struct tty_struct *tty, struct file *file,
926 unsigned int set, unsigned int clear)
927 {
928 struct uart_state *state = tty->driver_data;
929 struct uart_port *uport = state->uart_port;
930 struct tty_port *port = &state->port;
931 int ret = -EIO;
932
933 mutex_lock(&port->mutex);
934 if ((!file || !tty_hung_up_p(file)) &&
935 !(tty->flags & (1 << TTY_IO_ERROR))) {
936 uart_update_mctrl(uport, set, clear);
937 ret = 0;
938 }
939 mutex_unlock(&port->mutex);
940 return ret;
941 }
942
943 static int uart_break_ctl(struct tty_struct *tty, int break_state)
944 {
945 struct uart_state *state = tty->driver_data;
946 struct tty_port *port = &state->port;
947 struct uart_port *uport = state->uart_port;
948
949 mutex_lock(&port->mutex);
950
951 if (uport->type != PORT_UNKNOWN)
952 uport->ops->break_ctl(uport, break_state);
953
954 mutex_unlock(&port->mutex);
955 return 0;
956 }
957
958 static int uart_do_autoconfig(struct uart_state *state)
959 {
960 struct uart_port *uport = state->uart_port;
961 struct tty_port *port = &state->port;
962 int flags, ret;
963
964 if (!capable(CAP_SYS_ADMIN))
965 return -EPERM;
966
967 /*
968 * Take the per-port semaphore. This prevents count from
969 * changing, and hence any extra opens of the port while
970 * we're auto-configuring.
971 */
972 if (mutex_lock_interruptible(&port->mutex))
973 return -ERESTARTSYS;
974
975 ret = -EBUSY;
976 if (uart_users(state) == 1) {
977 uart_shutdown(state);
978
979 /*
980 * If we already have a port type configured,
981 * we must release its resources.
982 */
983 if (uport->type != PORT_UNKNOWN)
984 uport->ops->release_port(uport);
985
986 flags = UART_CONFIG_TYPE;
987 if (uport->flags & UPF_AUTO_IRQ)
988 flags |= UART_CONFIG_IRQ;
989
990 /*
991 * This will claim the ports resources if
992 * a port is found.
993 */
994 uport->ops->config_port(uport, flags);
995
996 ret = uart_startup(state, 1);
997 }
998 mutex_unlock(&port->mutex);
999 return ret;
1000 }
1001
1002 /*
1003 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1004 * - mask passed in arg for lines of interest
1005 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1006 * Caller should use TIOCGICOUNT to see which one it was
1007 */
1008 static int
1009 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1010 {
1011 struct uart_port *uport = state->uart_port;
1012 DECLARE_WAITQUEUE(wait, current);
1013 struct uart_icount cprev, cnow;
1014 int ret;
1015
1016 /*
1017 * note the counters on entry
1018 */
1019 spin_lock_irq(&uport->lock);
1020 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1021
1022 /*
1023 * Force modem status interrupts on
1024 */
1025 uport->ops->enable_ms(uport);
1026 spin_unlock_irq(&uport->lock);
1027
1028 add_wait_queue(&state->delta_msr_wait, &wait);
1029 for (;;) {
1030 spin_lock_irq(&uport->lock);
1031 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1032 spin_unlock_irq(&uport->lock);
1033
1034 set_current_state(TASK_INTERRUPTIBLE);
1035
1036 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1037 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1038 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1039 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1040 ret = 0;
1041 break;
1042 }
1043
1044 schedule();
1045
1046 /* see if a signal did it */
1047 if (signal_pending(current)) {
1048 ret = -ERESTARTSYS;
1049 break;
1050 }
1051
1052 cprev = cnow;
1053 }
1054
1055 current->state = TASK_RUNNING;
1056 remove_wait_queue(&state->delta_msr_wait, &wait);
1057
1058 return ret;
1059 }
1060
1061 /*
1062 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1063 * Return: write counters to the user passed counter struct
1064 * NB: both 1->0 and 0->1 transitions are counted except for
1065 * RI where only 0->1 is counted.
1066 */
1067 static int uart_get_count(struct uart_state *state,
1068 struct serial_icounter_struct __user *icnt)
1069 {
1070 struct serial_icounter_struct icount;
1071 struct uart_icount cnow;
1072 struct uart_port *uport = state->uart_port;
1073
1074 spin_lock_irq(&uport->lock);
1075 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1076 spin_unlock_irq(&uport->lock);
1077
1078 icount.cts = cnow.cts;
1079 icount.dsr = cnow.dsr;
1080 icount.rng = cnow.rng;
1081 icount.dcd = cnow.dcd;
1082 icount.rx = cnow.rx;
1083 icount.tx = cnow.tx;
1084 icount.frame = cnow.frame;
1085 icount.overrun = cnow.overrun;
1086 icount.parity = cnow.parity;
1087 icount.brk = cnow.brk;
1088 icount.buf_overrun = cnow.buf_overrun;
1089
1090 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1091 }
1092
1093 /*
1094 * Called via sys_ioctl. We can use spin_lock_irq() here.
1095 */
1096 static int
1097 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1098 unsigned long arg)
1099 {
1100 struct uart_state *state = tty->driver_data;
1101 struct tty_port *port = &state->port;
1102 void __user *uarg = (void __user *)arg;
1103 int ret = -ENOIOCTLCMD;
1104
1105
1106 /*
1107 * These ioctls don't rely on the hardware to be present.
1108 */
1109 switch (cmd) {
1110 case TIOCGSERIAL:
1111 ret = uart_get_info(state, uarg);
1112 break;
1113
1114 case TIOCSSERIAL:
1115 ret = uart_set_info(state, uarg);
1116 break;
1117
1118 case TIOCSERCONFIG:
1119 ret = uart_do_autoconfig(state);
1120 break;
1121
1122 case TIOCSERGWILD: /* obsolete */
1123 case TIOCSERSWILD: /* obsolete */
1124 ret = 0;
1125 break;
1126 }
1127
1128 if (ret != -ENOIOCTLCMD)
1129 goto out;
1130
1131 if (tty->flags & (1 << TTY_IO_ERROR)) {
1132 ret = -EIO;
1133 goto out;
1134 }
1135
1136 /*
1137 * The following should only be used when hardware is present.
1138 */
1139 switch (cmd) {
1140 case TIOCMIWAIT:
1141 ret = uart_wait_modem_status(state, arg);
1142 break;
1143
1144 case TIOCGICOUNT:
1145 ret = uart_get_count(state, uarg);
1146 break;
1147 }
1148
1149 if (ret != -ENOIOCTLCMD)
1150 goto out;
1151
1152 mutex_lock(&port->mutex);
1153
1154 if (tty_hung_up_p(filp)) {
1155 ret = -EIO;
1156 goto out_up;
1157 }
1158
1159 /*
1160 * All these rely on hardware being present and need to be
1161 * protected against the tty being hung up.
1162 */
1163 switch (cmd) {
1164 case TIOCSERGETLSR: /* Get line status register */
1165 ret = uart_get_lsr_info(state, uarg);
1166 break;
1167
1168 default: {
1169 struct uart_port *uport = state->uart_port;
1170 if (uport->ops->ioctl)
1171 ret = uport->ops->ioctl(uport, cmd, arg);
1172 break;
1173 }
1174 }
1175 out_up:
1176 mutex_unlock(&port->mutex);
1177 out:
1178 return ret;
1179 }
1180
1181 static void uart_set_ldisc(struct tty_struct *tty)
1182 {
1183 struct uart_state *state = tty->driver_data;
1184 struct uart_port *uport = state->uart_port;
1185
1186 if (uport->ops->set_ldisc)
1187 uport->ops->set_ldisc(uport);
1188 }
1189
1190 static void uart_set_termios(struct tty_struct *tty,
1191 struct ktermios *old_termios)
1192 {
1193 struct uart_state *state = tty->driver_data;
1194 unsigned long flags;
1195 unsigned int cflag = tty->termios->c_cflag;
1196
1197
1198 /*
1199 * These are the bits that are used to setup various
1200 * flags in the low level driver. We can ignore the Bfoo
1201 * bits in c_cflag; c_[io]speed will always be set
1202 * appropriately by set_termios() in tty_ioctl.c
1203 */
1204 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1205 if ((cflag ^ old_termios->c_cflag) == 0 &&
1206 tty->termios->c_ospeed == old_termios->c_ospeed &&
1207 tty->termios->c_ispeed == old_termios->c_ispeed &&
1208 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
1209 return;
1210 }
1211
1212 uart_change_speed(state, old_termios);
1213
1214 /* Handle transition to B0 status */
1215 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1216 uart_clear_mctrl(state->uart_port, TIOCM_RTS | TIOCM_DTR);
1217
1218 /* Handle transition away from B0 status */
1219 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1220 unsigned int mask = TIOCM_DTR;
1221 if (!(cflag & CRTSCTS) ||
1222 !test_bit(TTY_THROTTLED, &tty->flags))
1223 mask |= TIOCM_RTS;
1224 uart_set_mctrl(state->uart_port, mask);
1225 }
1226
1227 /* Handle turning off CRTSCTS */
1228 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1229 spin_lock_irqsave(&state->uart_port->lock, flags);
1230 tty->hw_stopped = 0;
1231 __uart_start(tty);
1232 spin_unlock_irqrestore(&state->uart_port->lock, flags);
1233 }
1234
1235 /* Handle turning on CRTSCTS */
1236 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1237 spin_lock_irqsave(&state->uart_port->lock, flags);
1238 if (!(state->uart_port->ops->get_mctrl(state->uart_port) & TIOCM_CTS)) {
1239 tty->hw_stopped = 1;
1240 state->uart_port->ops->stop_tx(state->uart_port);
1241 }
1242 spin_unlock_irqrestore(&state->uart_port->lock, flags);
1243 }
1244 #if 0
1245 /*
1246 * No need to wake up processes in open wait, since they
1247 * sample the CLOCAL flag once, and don't recheck it.
1248 * XXX It's not clear whether the current behavior is correct
1249 * or not. Hence, this may change.....
1250 */
1251 if (!(old_termios->c_cflag & CLOCAL) &&
1252 (tty->termios->c_cflag & CLOCAL))
1253 wake_up_interruptible(&state->uart_port.open_wait);
1254 #endif
1255 }
1256
1257 /*
1258 * In 2.4.5, calls to this will be serialized via the BKL in
1259 * linux/drivers/char/tty_io.c:tty_release()
1260 * linux/drivers/char/tty_io.c:do_tty_handup()
1261 */
1262 static void uart_close(struct tty_struct *tty, struct file *filp)
1263 {
1264 struct uart_state *state = tty->driver_data;
1265 struct tty_port *port;
1266 struct uart_port *uport;
1267
1268 BUG_ON(!kernel_locked());
1269
1270 uport = state->uart_port;
1271 port = &state->port;
1272
1273 pr_debug("uart_close(%d) called\n", uport->line);
1274
1275 mutex_lock(&port->mutex);
1276
1277 if (tty_hung_up_p(filp))
1278 goto done;
1279
1280 if ((tty->count == 1) && (port->count != 1)) {
1281 /*
1282 * Uh, oh. tty->count is 1, which means that the tty
1283 * structure will be freed. port->count should always
1284 * be one in these conditions. If it's greater than
1285 * one, we've got real problems, since it means the
1286 * serial port won't be shutdown.
1287 */
1288 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1289 "port->count is %d\n", port->count);
1290 port->count = 1;
1291 }
1292 if (--port->count < 0) {
1293 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1294 tty->name, port->count);
1295 port->count = 0;
1296 }
1297 if (port->count)
1298 goto done;
1299
1300 /*
1301 * Now we wait for the transmit buffer to clear; and we notify
1302 * the line discipline to only process XON/XOFF characters by
1303 * setting tty->closing.
1304 */
1305 tty->closing = 1;
1306
1307 if (port->closing_wait != USF_CLOSING_WAIT_NONE)
1308 tty_wait_until_sent(tty, msecs_to_jiffies(port->closing_wait));
1309
1310 /*
1311 * At this point, we stop accepting input. To do this, we
1312 * disable the receive line status interrupts.
1313 */
1314 if (port->flags & ASYNC_INITIALIZED) {
1315 unsigned long flags;
1316 spin_lock_irqsave(&port->lock, flags);
1317 uport->ops->stop_rx(uport);
1318 spin_unlock_irqrestore(&port->lock, flags);
1319 /*
1320 * Before we drop DTR, make sure the UART transmitter
1321 * has completely drained; this is especially
1322 * important if there is a transmit FIFO!
1323 */
1324 uart_wait_until_sent(tty, uport->timeout);
1325 }
1326
1327 uart_shutdown(state);
1328 uart_flush_buffer(tty);
1329
1330 tty_ldisc_flush(tty);
1331
1332 tty->closing = 0;
1333 port->tty = NULL;
1334
1335 if (port->blocked_open) {
1336 if (port->close_delay)
1337 msleep_interruptible(port->close_delay);
1338 } else if (!uart_console(uport)) {
1339 uart_change_pm(state, 3);
1340 }
1341
1342 /*
1343 * Wake up anyone trying to open this port.
1344 */
1345 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1346 wake_up_interruptible(&port->open_wait);
1347
1348 done:
1349 mutex_unlock(&port->mutex);
1350 }
1351
1352 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1353 {
1354 struct uart_state *state = tty->driver_data;
1355 struct uart_port *port = state->uart_port;
1356 unsigned long char_time, expire;
1357
1358 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1359 return;
1360
1361 lock_kernel();
1362
1363 /*
1364 * Set the check interval to be 1/5 of the estimated time to
1365 * send a single character, and make it at least 1. The check
1366 * interval should also be less than the timeout.
1367 *
1368 * Note: we have to use pretty tight timings here to satisfy
1369 * the NIST-PCTS.
1370 */
1371 char_time = (port->timeout - HZ/50) / port->fifosize;
1372 char_time = char_time / 5;
1373 if (char_time == 0)
1374 char_time = 1;
1375 if (timeout && timeout < char_time)
1376 char_time = timeout;
1377
1378 /*
1379 * If the transmitter hasn't cleared in twice the approximate
1380 * amount of time to send the entire FIFO, it probably won't
1381 * ever clear. This assumes the UART isn't doing flow
1382 * control, which is currently the case. Hence, if it ever
1383 * takes longer than port->timeout, this is probably due to a
1384 * UART bug of some kind. So, we clamp the timeout parameter at
1385 * 2*port->timeout.
1386 */
1387 if (timeout == 0 || timeout > 2 * port->timeout)
1388 timeout = 2 * port->timeout;
1389
1390 expire = jiffies + timeout;
1391
1392 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1393 port->line, jiffies, expire);
1394
1395 /*
1396 * Check whether the transmitter is empty every 'char_time'.
1397 * 'timeout' / 'expire' give us the maximum amount of time
1398 * we wait.
1399 */
1400 while (!port->ops->tx_empty(port)) {
1401 msleep_interruptible(jiffies_to_msecs(char_time));
1402 if (signal_pending(current))
1403 break;
1404 if (time_after(jiffies, expire))
1405 break;
1406 }
1407 set_current_state(TASK_RUNNING); /* might not be needed */
1408 unlock_kernel();
1409 }
1410
1411 /*
1412 * This is called with the BKL held in
1413 * linux/drivers/char/tty_io.c:do_tty_hangup()
1414 * We're called from the eventd thread, so we can sleep for
1415 * a _short_ time only.
1416 */
1417 static void uart_hangup(struct tty_struct *tty)
1418 {
1419 struct uart_state *state = tty->driver_data;
1420 struct tty_port *port = &state->port;
1421
1422 BUG_ON(!kernel_locked());
1423 pr_debug("uart_hangup(%d)\n", state->uart_port->line);
1424
1425 mutex_lock(&port->mutex);
1426 if (port->flags & ASYNC_NORMAL_ACTIVE) {
1427 uart_flush_buffer(tty);
1428 uart_shutdown(state);
1429 port->count = 0;
1430 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1431 port->tty = NULL;
1432 wake_up_interruptible(&port->open_wait);
1433 wake_up_interruptible(&state->delta_msr_wait);
1434 }
1435 mutex_unlock(&port->mutex);
1436 }
1437
1438 /*
1439 * Copy across the serial console cflag setting into the termios settings
1440 * for the initial open of the port. This allows continuity between the
1441 * kernel settings, and the settings init adopts when it opens the port
1442 * for the first time.
1443 */
1444 static void uart_update_termios(struct uart_state *state)
1445 {
1446 struct tty_struct *tty = state->port.tty;
1447 struct uart_port *port = state->uart_port;
1448
1449 if (uart_console(port) && port->cons->cflag) {
1450 tty->termios->c_cflag = port->cons->cflag;
1451 port->cons->cflag = 0;
1452 }
1453
1454 /*
1455 * If the device failed to grab its irq resources,
1456 * or some other error occurred, don't try to talk
1457 * to the port hardware.
1458 */
1459 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1460 /*
1461 * Make termios settings take effect.
1462 */
1463 uart_change_speed(state, NULL);
1464
1465 /*
1466 * And finally enable the RTS and DTR signals.
1467 */
1468 if (tty->termios->c_cflag & CBAUD)
1469 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1470 }
1471 }
1472
1473 /*
1474 * Block the open until the port is ready. We must be called with
1475 * the per-port semaphore held.
1476 */
1477 static int
1478 uart_block_til_ready(struct file *filp, struct uart_state *state)
1479 {
1480 DECLARE_WAITQUEUE(wait, current);
1481 struct uart_port *uport = state->uart_port;
1482 struct tty_port *port = &state->port;
1483 unsigned int mctrl;
1484
1485 port->blocked_open++;
1486 port->count--;
1487
1488 add_wait_queue(&port->open_wait, &wait);
1489 while (1) {
1490 set_current_state(TASK_INTERRUPTIBLE);
1491
1492 /*
1493 * If we have been hung up, tell userspace/restart open.
1494 */
1495 if (tty_hung_up_p(filp) || port->tty == NULL)
1496 break;
1497
1498 /*
1499 * If the port has been closed, tell userspace/restart open.
1500 */
1501 if (!(port->flags & ASYNC_INITIALIZED))
1502 break;
1503
1504 /*
1505 * If non-blocking mode is set, or CLOCAL mode is set,
1506 * we don't want to wait for the modem status lines to
1507 * indicate that the port is ready.
1508 *
1509 * Also, if the port is not enabled/configured, we want
1510 * to allow the open to succeed here. Note that we will
1511 * have set TTY_IO_ERROR for a non-existant port.
1512 */
1513 if ((filp->f_flags & O_NONBLOCK) ||
1514 (port->tty->termios->c_cflag & CLOCAL) ||
1515 (port->tty->flags & (1 << TTY_IO_ERROR)))
1516 break;
1517
1518 /*
1519 * Set DTR to allow modem to know we're waiting. Do
1520 * not set RTS here - we want to make sure we catch
1521 * the data from the modem.
1522 */
1523 if (port->tty->termios->c_cflag & CBAUD)
1524 uart_set_mctrl(uport, TIOCM_DTR);
1525
1526 /*
1527 * and wait for the carrier to indicate that the
1528 * modem is ready for us.
1529 */
1530 spin_lock_irq(&uport->lock);
1531 uport->ops->enable_ms(uport);
1532 mctrl = uport->ops->get_mctrl(uport);
1533 spin_unlock_irq(&uport->lock);
1534 if (mctrl & TIOCM_CAR)
1535 break;
1536
1537 mutex_unlock(&port->mutex);
1538 schedule();
1539 mutex_lock(&port->mutex);
1540
1541 if (signal_pending(current))
1542 break;
1543 }
1544 set_current_state(TASK_RUNNING);
1545 remove_wait_queue(&port->open_wait, &wait);
1546
1547 port->count++;
1548 port->blocked_open--;
1549
1550 if (signal_pending(current))
1551 return -ERESTARTSYS;
1552
1553 if (!port->tty || tty_hung_up_p(filp))
1554 return -EAGAIN;
1555
1556 return 0;
1557 }
1558
1559 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1560 {
1561 struct uart_state *state;
1562 struct tty_port *port;
1563 int ret = 0;
1564
1565 state = drv->state + line;
1566 port = &state->port;
1567 if (mutex_lock_interruptible(&port->mutex)) {
1568 ret = -ERESTARTSYS;
1569 goto err;
1570 }
1571
1572 port->count++;
1573 if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
1574 ret = -ENXIO;
1575 goto err_unlock;
1576 }
1577 return state;
1578
1579 err_unlock:
1580 port->count--;
1581 mutex_unlock(&port->mutex);
1582 err:
1583 return ERR_PTR(ret);
1584 }
1585
1586 /*
1587 * calls to uart_open are serialised by the BKL in
1588 * fs/char_dev.c:chrdev_open()
1589 * Note that if this fails, then uart_close() _will_ be called.
1590 *
1591 * In time, we want to scrap the "opening nonpresent ports"
1592 * behaviour and implement an alternative way for setserial
1593 * to set base addresses/ports/types. This will allow us to
1594 * get rid of a certain amount of extra tests.
1595 */
1596 static int uart_open(struct tty_struct *tty, struct file *filp)
1597 {
1598 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1599 struct uart_state *state;
1600 struct tty_port *port;
1601 int retval, line = tty->index;
1602
1603 BUG_ON(!kernel_locked());
1604 pr_debug("uart_open(%d) called\n", line);
1605
1606 /*
1607 * tty->driver->num won't change, so we won't fail here with
1608 * tty->driver_data set to something non-NULL (and therefore
1609 * we won't get caught by uart_close()).
1610 */
1611 retval = -ENODEV;
1612 if (line >= tty->driver->num)
1613 goto fail;
1614
1615 /*
1616 * We take the semaphore inside uart_get to guarantee that we won't
1617 * be re-entered while allocating the state structure, or while we
1618 * request any IRQs that the driver may need. This also has the nice
1619 * side-effect that it delays the action of uart_hangup, so we can
1620 * guarantee that state->port.tty will always contain something
1621 * reasonable.
1622 */
1623 state = uart_get(drv, line);
1624 if (IS_ERR(state)) {
1625 retval = PTR_ERR(state);
1626 goto fail;
1627 }
1628 port = &state->port;
1629
1630 /*
1631 * Once we set tty->driver_data here, we are guaranteed that
1632 * uart_close() will decrement the driver module use count.
1633 * Any failures from here onwards should not touch the count.
1634 */
1635 tty->driver_data = state;
1636 state->uart_port->state = state;
1637 tty->low_latency = (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1638 tty->alt_speed = 0;
1639 port->tty = tty;
1640
1641 /*
1642 * If the port is in the middle of closing, bail out now.
1643 */
1644 if (tty_hung_up_p(filp)) {
1645 retval = -EAGAIN;
1646 port->count--;
1647 mutex_unlock(&port->mutex);
1648 goto fail;
1649 }
1650
1651 /*
1652 * Make sure the device is in D0 state.
1653 */
1654 if (port->count == 1)
1655 uart_change_pm(state, 0);
1656
1657 /*
1658 * Start up the serial port.
1659 */
1660 retval = uart_startup(state, 0);
1661
1662 /*
1663 * If we succeeded, wait until the port is ready.
1664 */
1665 if (retval == 0)
1666 retval = uart_block_til_ready(filp, state);
1667 mutex_unlock(&port->mutex);
1668
1669 /*
1670 * If this is the first open to succeed, adjust things to suit.
1671 */
1672 if (retval == 0 && !(port->flags & ASYNC_NORMAL_ACTIVE)) {
1673 set_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1674
1675 uart_update_termios(state);
1676 }
1677
1678 fail:
1679 return retval;
1680 }
1681
1682 static const char *uart_type(struct uart_port *port)
1683 {
1684 const char *str = NULL;
1685
1686 if (port->ops->type)
1687 str = port->ops->type(port);
1688
1689 if (!str)
1690 str = "unknown";
1691
1692 return str;
1693 }
1694
1695 #ifdef CONFIG_PROC_FS
1696
1697 static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1698 {
1699 struct uart_state *state = drv->state + i;
1700 struct tty_port *port = &state->port;
1701 int pm_state;
1702 struct uart_port *uport = state->uart_port;
1703 char stat_buf[32];
1704 unsigned int status;
1705 int mmio;
1706
1707 if (!uport)
1708 return;
1709
1710 mmio = uport->iotype >= UPIO_MEM;
1711 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1712 uport->line, uart_type(uport),
1713 mmio ? "mmio:0x" : "port:",
1714 mmio ? (unsigned long long)uport->mapbase
1715 : (unsigned long long)uport->iobase,
1716 uport->irq);
1717
1718 if (uport->type == PORT_UNKNOWN) {
1719 seq_putc(m, '\n');
1720 return;
1721 }
1722
1723 if (capable(CAP_SYS_ADMIN)) {
1724 mutex_lock(&port->mutex);
1725 pm_state = state->pm_state;
1726 if (pm_state)
1727 uart_change_pm(state, 0);
1728 spin_lock_irq(&uport->lock);
1729 status = uport->ops->get_mctrl(uport);
1730 spin_unlock_irq(&uport->lock);
1731 if (pm_state)
1732 uart_change_pm(state, pm_state);
1733 mutex_unlock(&port->mutex);
1734
1735 seq_printf(m, " tx:%d rx:%d",
1736 uport->icount.tx, uport->icount.rx);
1737 if (uport->icount.frame)
1738 seq_printf(m, " fe:%d",
1739 uport->icount.frame);
1740 if (uport->icount.parity)
1741 seq_printf(m, " pe:%d",
1742 uport->icount.parity);
1743 if (uport->icount.brk)
1744 seq_printf(m, " brk:%d",
1745 uport->icount.brk);
1746 if (uport->icount.overrun)
1747 seq_printf(m, " oe:%d",
1748 uport->icount.overrun);
1749
1750 #define INFOBIT(bit, str) \
1751 if (uport->mctrl & (bit)) \
1752 strncat(stat_buf, (str), sizeof(stat_buf) - \
1753 strlen(stat_buf) - 2)
1754 #define STATBIT(bit, str) \
1755 if (status & (bit)) \
1756 strncat(stat_buf, (str), sizeof(stat_buf) - \
1757 strlen(stat_buf) - 2)
1758
1759 stat_buf[0] = '\0';
1760 stat_buf[1] = '\0';
1761 INFOBIT(TIOCM_RTS, "|RTS");
1762 STATBIT(TIOCM_CTS, "|CTS");
1763 INFOBIT(TIOCM_DTR, "|DTR");
1764 STATBIT(TIOCM_DSR, "|DSR");
1765 STATBIT(TIOCM_CAR, "|CD");
1766 STATBIT(TIOCM_RNG, "|RI");
1767 if (stat_buf[0])
1768 stat_buf[0] = ' ';
1769
1770 seq_puts(m, stat_buf);
1771 }
1772 seq_putc(m, '\n');
1773 #undef STATBIT
1774 #undef INFOBIT
1775 }
1776
1777 static int uart_proc_show(struct seq_file *m, void *v)
1778 {
1779 struct tty_driver *ttydrv = m->private;
1780 struct uart_driver *drv = ttydrv->driver_state;
1781 int i;
1782
1783 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
1784 "", "", "");
1785 for (i = 0; i < drv->nr; i++)
1786 uart_line_info(m, drv, i);
1787 return 0;
1788 }
1789
1790 static int uart_proc_open(struct inode *inode, struct file *file)
1791 {
1792 return single_open(file, uart_proc_show, PDE(inode)->data);
1793 }
1794
1795 static const struct file_operations uart_proc_fops = {
1796 .owner = THIS_MODULE,
1797 .open = uart_proc_open,
1798 .read = seq_read,
1799 .llseek = seq_lseek,
1800 .release = single_release,
1801 };
1802 #endif
1803
1804 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1805 /*
1806 * uart_console_write - write a console message to a serial port
1807 * @port: the port to write the message
1808 * @s: array of characters
1809 * @count: number of characters in string to write
1810 * @write: function to write character to port
1811 */
1812 void uart_console_write(struct uart_port *port, const char *s,
1813 unsigned int count,
1814 void (*putchar)(struct uart_port *, int))
1815 {
1816 unsigned int i;
1817
1818 for (i = 0; i < count; i++, s++) {
1819 if (*s == '\n')
1820 putchar(port, '\r');
1821 putchar(port, *s);
1822 }
1823 }
1824 EXPORT_SYMBOL_GPL(uart_console_write);
1825
1826 /*
1827 * Check whether an invalid uart number has been specified, and
1828 * if so, search for the first available port that does have
1829 * console support.
1830 */
1831 struct uart_port * __init
1832 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1833 {
1834 int idx = co->index;
1835
1836 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1837 ports[idx].membase == NULL))
1838 for (idx = 0; idx < nr; idx++)
1839 if (ports[idx].iobase != 0 ||
1840 ports[idx].membase != NULL)
1841 break;
1842
1843 co->index = idx;
1844
1845 return ports + idx;
1846 }
1847
1848 /**
1849 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1850 * @options: pointer to option string
1851 * @baud: pointer to an 'int' variable for the baud rate.
1852 * @parity: pointer to an 'int' variable for the parity.
1853 * @bits: pointer to an 'int' variable for the number of data bits.
1854 * @flow: pointer to an 'int' variable for the flow control character.
1855 *
1856 * uart_parse_options decodes a string containing the serial console
1857 * options. The format of the string is <baud><parity><bits><flow>,
1858 * eg: 115200n8r
1859 */
1860 void
1861 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1862 {
1863 char *s = options;
1864
1865 *baud = simple_strtoul(s, NULL, 10);
1866 while (*s >= '0' && *s <= '9')
1867 s++;
1868 if (*s)
1869 *parity = *s++;
1870 if (*s)
1871 *bits = *s++ - '0';
1872 if (*s)
1873 *flow = *s;
1874 }
1875 EXPORT_SYMBOL_GPL(uart_parse_options);
1876
1877 struct baud_rates {
1878 unsigned int rate;
1879 unsigned int cflag;
1880 };
1881
1882 static const struct baud_rates baud_rates[] = {
1883 { 921600, B921600 },
1884 { 460800, B460800 },
1885 { 230400, B230400 },
1886 { 115200, B115200 },
1887 { 57600, B57600 },
1888 { 38400, B38400 },
1889 { 19200, B19200 },
1890 { 9600, B9600 },
1891 { 4800, B4800 },
1892 { 2400, B2400 },
1893 { 1200, B1200 },
1894 { 0, B38400 }
1895 };
1896
1897 /**
1898 * uart_set_options - setup the serial console parameters
1899 * @port: pointer to the serial ports uart_port structure
1900 * @co: console pointer
1901 * @baud: baud rate
1902 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1903 * @bits: number of data bits
1904 * @flow: flow control character - 'r' (rts)
1905 */
1906 int
1907 uart_set_options(struct uart_port *port, struct console *co,
1908 int baud, int parity, int bits, int flow)
1909 {
1910 struct ktermios termios;
1911 static struct ktermios dummy;
1912 int i;
1913
1914 /*
1915 * Ensure that the serial console lock is initialised
1916 * early.
1917 */
1918 spin_lock_init(&port->lock);
1919 lockdep_set_class(&port->lock, &port_lock_key);
1920
1921 memset(&termios, 0, sizeof(struct ktermios));
1922
1923 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1924
1925 /*
1926 * Construct a cflag setting.
1927 */
1928 for (i = 0; baud_rates[i].rate; i++)
1929 if (baud_rates[i].rate <= baud)
1930 break;
1931
1932 termios.c_cflag |= baud_rates[i].cflag;
1933
1934 if (bits == 7)
1935 termios.c_cflag |= CS7;
1936 else
1937 termios.c_cflag |= CS8;
1938
1939 switch (parity) {
1940 case 'o': case 'O':
1941 termios.c_cflag |= PARODD;
1942 /*fall through*/
1943 case 'e': case 'E':
1944 termios.c_cflag |= PARENB;
1945 break;
1946 }
1947
1948 if (flow == 'r')
1949 termios.c_cflag |= CRTSCTS;
1950
1951 /*
1952 * some uarts on other side don't support no flow control.
1953 * So we set * DTR in host uart to make them happy
1954 */
1955 port->mctrl |= TIOCM_DTR;
1956
1957 port->ops->set_termios(port, &termios, &dummy);
1958 /*
1959 * Allow the setting of the UART parameters with a NULL console
1960 * too:
1961 */
1962 if (co)
1963 co->cflag = termios.c_cflag;
1964
1965 return 0;
1966 }
1967 EXPORT_SYMBOL_GPL(uart_set_options);
1968 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1969
1970 static void uart_change_pm(struct uart_state *state, int pm_state)
1971 {
1972 struct uart_port *port = state->uart_port;
1973
1974 if (state->pm_state != pm_state) {
1975 if (port->ops->pm)
1976 port->ops->pm(port, pm_state, state->pm_state);
1977 state->pm_state = pm_state;
1978 }
1979 }
1980
1981 struct uart_match {
1982 struct uart_port *port;
1983 struct uart_driver *driver;
1984 };
1985
1986 static int serial_match_port(struct device *dev, void *data)
1987 {
1988 struct uart_match *match = data;
1989 struct tty_driver *tty_drv = match->driver->tty_driver;
1990 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1991 match->port->line;
1992
1993 return dev->devt == devt; /* Actually, only one tty per port */
1994 }
1995
1996 int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
1997 {
1998 struct uart_state *state = drv->state + uport->line;
1999 struct tty_port *port = &state->port;
2000 struct device *tty_dev;
2001 struct uart_match match = {uport, drv};
2002
2003 mutex_lock(&port->mutex);
2004
2005 if (!console_suspend_enabled && uart_console(uport)) {
2006 /* we're going to avoid suspending serial console */
2007 mutex_unlock(&port->mutex);
2008 return 0;
2009 }
2010
2011 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2012 if (device_may_wakeup(tty_dev)) {
2013 enable_irq_wake(uport->irq);
2014 put_device(tty_dev);
2015 mutex_unlock(&port->mutex);
2016 return 0;
2017 }
2018 uport->suspended = 1;
2019
2020 if (port->flags & ASYNC_INITIALIZED) {
2021 const struct uart_ops *ops = uport->ops;
2022 int tries;
2023
2024 set_bit(ASYNCB_SUSPENDED, &port->flags);
2025 clear_bit(ASYNCB_INITIALIZED, &port->flags);
2026
2027 spin_lock_irq(&uport->lock);
2028 ops->stop_tx(uport);
2029 ops->set_mctrl(uport, 0);
2030 ops->stop_rx(uport);
2031 spin_unlock_irq(&uport->lock);
2032
2033 /*
2034 * Wait for the transmitter to empty.
2035 */
2036 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
2037 msleep(10);
2038 if (!tries)
2039 printk(KERN_ERR "%s%s%s%d: Unable to drain "
2040 "transmitter\n",
2041 uport->dev ? dev_name(uport->dev) : "",
2042 uport->dev ? ": " : "",
2043 drv->dev_name,
2044 drv->tty_driver->name_base + uport->line);
2045
2046 ops->shutdown(uport);
2047 }
2048
2049 /*
2050 * Disable the console device before suspending.
2051 */
2052 if (uart_console(uport))
2053 console_stop(uport->cons);
2054
2055 uart_change_pm(state, 3);
2056
2057 mutex_unlock(&port->mutex);
2058
2059 return 0;
2060 }
2061
2062 int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2063 {
2064 struct uart_state *state = drv->state + uport->line;
2065 struct tty_port *port = &state->port;
2066 struct device *tty_dev;
2067 struct uart_match match = {uport, drv};
2068
2069 mutex_lock(&port->mutex);
2070
2071 if (!console_suspend_enabled && uart_console(uport)) {
2072 /* no need to resume serial console, it wasn't suspended */
2073 mutex_unlock(&port->mutex);
2074 return 0;
2075 }
2076
2077 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2078 if (!uport->suspended && device_may_wakeup(tty_dev)) {
2079 disable_irq_wake(uport->irq);
2080 mutex_unlock(&port->mutex);
2081 return 0;
2082 }
2083 uport->suspended = 0;
2084
2085 /*
2086 * Re-enable the console device after suspending.
2087 */
2088 if (uart_console(uport)) {
2089 struct ktermios termios;
2090
2091 /*
2092 * First try to use the console cflag setting.
2093 */
2094 memset(&termios, 0, sizeof(struct ktermios));
2095 termios.c_cflag = uport->cons->cflag;
2096
2097 /*
2098 * If that's unset, use the tty termios setting.
2099 */
2100 if (port->tty && termios.c_cflag == 0)
2101 termios = *port->tty->termios;
2102
2103 uart_change_pm(state, 0);
2104 uport->ops->set_termios(uport, &termios, NULL);
2105 console_start(uport->cons);
2106 }
2107
2108 if (port->flags & ASYNC_SUSPENDED) {
2109 const struct uart_ops *ops = uport->ops;
2110 int ret;
2111
2112 uart_change_pm(state, 0);
2113 spin_lock_irq(&uport->lock);
2114 ops->set_mctrl(uport, 0);
2115 spin_unlock_irq(&uport->lock);
2116 ret = ops->startup(uport);
2117 if (ret == 0) {
2118 uart_change_speed(state, NULL);
2119 spin_lock_irq(&uport->lock);
2120 ops->set_mctrl(uport, uport->mctrl);
2121 ops->start_tx(uport);
2122 spin_unlock_irq(&uport->lock);
2123 set_bit(ASYNCB_INITIALIZED, &port->flags);
2124 } else {
2125 /*
2126 * Failed to resume - maybe hardware went away?
2127 * Clear the "initialized" flag so we won't try
2128 * to call the low level drivers shutdown method.
2129 */
2130 uart_shutdown(state);
2131 }
2132
2133 clear_bit(ASYNCB_SUSPENDED, &port->flags);
2134 }
2135
2136 mutex_unlock(&port->mutex);
2137
2138 return 0;
2139 }
2140
2141 static inline void
2142 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2143 {
2144 char address[64];
2145
2146 switch (port->iotype) {
2147 case UPIO_PORT:
2148 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2149 break;
2150 case UPIO_HUB6:
2151 snprintf(address, sizeof(address),
2152 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2153 break;
2154 case UPIO_MEM:
2155 case UPIO_MEM32:
2156 case UPIO_AU:
2157 case UPIO_TSI:
2158 case UPIO_DWAPB:
2159 snprintf(address, sizeof(address),
2160 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2161 break;
2162 default:
2163 strlcpy(address, "*unknown*", sizeof(address));
2164 break;
2165 }
2166
2167 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2168 port->dev ? dev_name(port->dev) : "",
2169 port->dev ? ": " : "",
2170 drv->dev_name,
2171 drv->tty_driver->name_base + port->line,
2172 address, port->irq, uart_type(port));
2173 }
2174
2175 static void
2176 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2177 struct uart_port *port)
2178 {
2179 unsigned int flags;
2180
2181 /*
2182 * If there isn't a port here, don't do anything further.
2183 */
2184 if (!port->iobase && !port->mapbase && !port->membase)
2185 return;
2186
2187 /*
2188 * Now do the auto configuration stuff. Note that config_port
2189 * is expected to claim the resources and map the port for us.
2190 */
2191 flags = 0;
2192 if (port->flags & UPF_AUTO_IRQ)
2193 flags |= UART_CONFIG_IRQ;
2194 if (port->flags & UPF_BOOT_AUTOCONF) {
2195 if (!(port->flags & UPF_FIXED_TYPE)) {
2196 port->type = PORT_UNKNOWN;
2197 flags |= UART_CONFIG_TYPE;
2198 }
2199 port->ops->config_port(port, flags);
2200 }
2201
2202 if (port->type != PORT_UNKNOWN) {
2203 unsigned long flags;
2204
2205 uart_report_port(drv, port);
2206
2207 /* Power up port for set_mctrl() */
2208 uart_change_pm(state, 0);
2209
2210 /*
2211 * Ensure that the modem control lines are de-activated.
2212 * keep the DTR setting that is set in uart_set_options()
2213 * We probably don't need a spinlock around this, but
2214 */
2215 spin_lock_irqsave(&port->lock, flags);
2216 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2217 spin_unlock_irqrestore(&port->lock, flags);
2218
2219 /*
2220 * If this driver supports console, and it hasn't been
2221 * successfully registered yet, try to re-register it.
2222 * It may be that the port was not available.
2223 */
2224 if (port->cons && !(port->cons->flags & CON_ENABLED))
2225 register_console(port->cons);
2226
2227 /*
2228 * Power down all ports by default, except the
2229 * console if we have one.
2230 */
2231 if (!uart_console(port))
2232 uart_change_pm(state, 3);
2233 }
2234 }
2235
2236 #ifdef CONFIG_CONSOLE_POLL
2237
2238 static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2239 {
2240 struct uart_driver *drv = driver->driver_state;
2241 struct uart_state *state = drv->state + line;
2242 struct uart_port *port;
2243 int baud = 9600;
2244 int bits = 8;
2245 int parity = 'n';
2246 int flow = 'n';
2247
2248 if (!state || !state->uart_port)
2249 return -1;
2250
2251 port = state->uart_port;
2252 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2253 return -1;
2254
2255 if (options) {
2256 uart_parse_options(options, &baud, &parity, &bits, &flow);
2257 return uart_set_options(port, NULL, baud, parity, bits, flow);
2258 }
2259
2260 return 0;
2261 }
2262
2263 static int uart_poll_get_char(struct tty_driver *driver, int line)
2264 {
2265 struct uart_driver *drv = driver->driver_state;
2266 struct uart_state *state = drv->state + line;
2267 struct uart_port *port;
2268
2269 if (!state || !state->uart_port)
2270 return -1;
2271
2272 port = state->uart_port;
2273 return port->ops->poll_get_char(port);
2274 }
2275
2276 static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2277 {
2278 struct uart_driver *drv = driver->driver_state;
2279 struct uart_state *state = drv->state + line;
2280 struct uart_port *port;
2281
2282 if (!state || !state->uart_port)
2283 return;
2284
2285 port = state->uart_port;
2286 port->ops->poll_put_char(port, ch);
2287 }
2288 #endif
2289
2290 static const struct tty_operations uart_ops = {
2291 .open = uart_open,
2292 .close = uart_close,
2293 .write = uart_write,
2294 .put_char = uart_put_char,
2295 .flush_chars = uart_flush_chars,
2296 .write_room = uart_write_room,
2297 .chars_in_buffer= uart_chars_in_buffer,
2298 .flush_buffer = uart_flush_buffer,
2299 .ioctl = uart_ioctl,
2300 .throttle = uart_throttle,
2301 .unthrottle = uart_unthrottle,
2302 .send_xchar = uart_send_xchar,
2303 .set_termios = uart_set_termios,
2304 .set_ldisc = uart_set_ldisc,
2305 .stop = uart_stop,
2306 .start = uart_start,
2307 .hangup = uart_hangup,
2308 .break_ctl = uart_break_ctl,
2309 .wait_until_sent= uart_wait_until_sent,
2310 #ifdef CONFIG_PROC_FS
2311 .proc_fops = &uart_proc_fops,
2312 #endif
2313 .tiocmget = uart_tiocmget,
2314 .tiocmset = uart_tiocmset,
2315 #ifdef CONFIG_CONSOLE_POLL
2316 .poll_init = uart_poll_init,
2317 .poll_get_char = uart_poll_get_char,
2318 .poll_put_char = uart_poll_put_char,
2319 #endif
2320 };
2321
2322 /**
2323 * uart_register_driver - register a driver with the uart core layer
2324 * @drv: low level driver structure
2325 *
2326 * Register a uart driver with the core driver. We in turn register
2327 * with the tty layer, and initialise the core driver per-port state.
2328 *
2329 * We have a proc file in /proc/tty/driver which is named after the
2330 * normal driver.
2331 *
2332 * drv->port should be NULL, and the per-port structures should be
2333 * registered using uart_add_one_port after this call has succeeded.
2334 */
2335 int uart_register_driver(struct uart_driver *drv)
2336 {
2337 struct tty_driver *normal = NULL;
2338 int i, retval;
2339
2340 BUG_ON(drv->state);
2341
2342 /*
2343 * Maybe we should be using a slab cache for this, especially if
2344 * we have a large number of ports to handle.
2345 */
2346 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2347 retval = -ENOMEM;
2348 if (!drv->state)
2349 goto out;
2350
2351 normal = alloc_tty_driver(drv->nr);
2352 if (!normal)
2353 goto out;
2354
2355 drv->tty_driver = normal;
2356
2357 normal->owner = drv->owner;
2358 normal->driver_name = drv->driver_name;
2359 normal->name = drv->dev_name;
2360 normal->major = drv->major;
2361 normal->minor_start = drv->minor;
2362 normal->type = TTY_DRIVER_TYPE_SERIAL;
2363 normal->subtype = SERIAL_TYPE_NORMAL;
2364 normal->init_termios = tty_std_termios;
2365 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2366 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2367 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2368 normal->driver_state = drv;
2369 tty_set_operations(normal, &uart_ops);
2370
2371 /*
2372 * Initialise the UART state(s).
2373 */
2374 for (i = 0; i < drv->nr; i++) {
2375 struct uart_state *state = drv->state + i;
2376 struct tty_port *port = &state->port;
2377
2378 tty_port_init(port);
2379 port->close_delay = 500; /* .5 seconds */
2380 port->closing_wait = 30000; /* 30 seconds */
2381 init_waitqueue_head(&state->delta_msr_wait);
2382 tasklet_init(&state->tlet, uart_tasklet_action,
2383 (unsigned long)state);
2384 }
2385
2386 retval = tty_register_driver(normal);
2387 out:
2388 if (retval < 0) {
2389 put_tty_driver(normal);
2390 kfree(drv->state);
2391 }
2392 return retval;
2393 }
2394
2395 /**
2396 * uart_unregister_driver - remove a driver from the uart core layer
2397 * @drv: low level driver structure
2398 *
2399 * Remove all references to a driver from the core driver. The low
2400 * level driver must have removed all its ports via the
2401 * uart_remove_one_port() if it registered them with uart_add_one_port().
2402 * (ie, drv->port == NULL)
2403 */
2404 void uart_unregister_driver(struct uart_driver *drv)
2405 {
2406 struct tty_driver *p = drv->tty_driver;
2407 tty_unregister_driver(p);
2408 put_tty_driver(p);
2409 kfree(drv->state);
2410 drv->tty_driver = NULL;
2411 }
2412
2413 struct tty_driver *uart_console_device(struct console *co, int *index)
2414 {
2415 struct uart_driver *p = co->data;
2416 *index = co->index;
2417 return p->tty_driver;
2418 }
2419
2420 /**
2421 * uart_add_one_port - attach a driver-defined port structure
2422 * @drv: pointer to the uart low level driver structure for this port
2423 * @port: uart port structure to use for this port.
2424 *
2425 * This allows the driver to register its own uart_port structure
2426 * with the core driver. The main purpose is to allow the low
2427 * level uart drivers to expand uart_port, rather than having yet
2428 * more levels of structures.
2429 */
2430 int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
2431 {
2432 struct uart_state *state;
2433 struct tty_port *port;
2434 int ret = 0;
2435 struct device *tty_dev;
2436
2437 BUG_ON(in_interrupt());
2438
2439 if (uport->line >= drv->nr)
2440 return -EINVAL;
2441
2442 state = drv->state + uport->line;
2443 port = &state->port;
2444
2445 mutex_lock(&port_mutex);
2446 mutex_lock(&port->mutex);
2447 if (state->uart_port) {
2448 ret = -EINVAL;
2449 goto out;
2450 }
2451
2452 state->uart_port = uport;
2453 state->pm_state = -1;
2454
2455 uport->cons = drv->cons;
2456 uport->state = state;
2457
2458 /*
2459 * If this port is a console, then the spinlock is already
2460 * initialised.
2461 */
2462 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2463 spin_lock_init(&uport->lock);
2464 lockdep_set_class(&uport->lock, &port_lock_key);
2465 }
2466
2467 uart_configure_port(drv, state, uport);
2468
2469 /*
2470 * Register the port whether it's detected or not. This allows
2471 * setserial to be used to alter this ports parameters.
2472 */
2473 tty_dev = tty_register_device(drv->tty_driver, uport->line, uport->dev);
2474 if (likely(!IS_ERR(tty_dev))) {
2475 device_init_wakeup(tty_dev, 1);
2476 device_set_wakeup_enable(tty_dev, 0);
2477 } else
2478 printk(KERN_ERR "Cannot register tty device on line %d\n",
2479 uport->line);
2480
2481 /*
2482 * Ensure UPF_DEAD is not set.
2483 */
2484 uport->flags &= ~UPF_DEAD;
2485
2486 out:
2487 mutex_unlock(&port->mutex);
2488 mutex_unlock(&port_mutex);
2489
2490 return ret;
2491 }
2492
2493 /**
2494 * uart_remove_one_port - detach a driver defined port structure
2495 * @drv: pointer to the uart low level driver structure for this port
2496 * @port: uart port structure for this port
2497 *
2498 * This unhooks (and hangs up) the specified port structure from the
2499 * core driver. No further calls will be made to the low-level code
2500 * for this port.
2501 */
2502 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
2503 {
2504 struct uart_state *state = drv->state + uport->line;
2505 struct tty_port *port = &state->port;
2506
2507 BUG_ON(in_interrupt());
2508
2509 if (state->uart_port != uport)
2510 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2511 state->uart_port, uport);
2512
2513 mutex_lock(&port_mutex);
2514
2515 /*
2516 * Mark the port "dead" - this prevents any opens from
2517 * succeeding while we shut down the port.
2518 */
2519 mutex_lock(&port->mutex);
2520 uport->flags |= UPF_DEAD;
2521 mutex_unlock(&port->mutex);
2522
2523 /*
2524 * Remove the devices from the tty layer
2525 */
2526 tty_unregister_device(drv->tty_driver, uport->line);
2527
2528 if (port->tty)
2529 tty_vhangup(port->tty);
2530
2531 /*
2532 * Free the port IO and memory resources, if any.
2533 */
2534 if (uport->type != PORT_UNKNOWN)
2535 uport->ops->release_port(uport);
2536
2537 /*
2538 * Indicate that there isn't a port here anymore.
2539 */
2540 uport->type = PORT_UNKNOWN;
2541
2542 /*
2543 * Kill the tasklet, and free resources.
2544 */
2545 tasklet_kill(&state->tlet);
2546
2547 state->uart_port = NULL;
2548 mutex_unlock(&port_mutex);
2549
2550 return 0;
2551 }
2552
2553 /*
2554 * Are the two ports equivalent?
2555 */
2556 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2557 {
2558 if (port1->iotype != port2->iotype)
2559 return 0;
2560
2561 switch (port1->iotype) {
2562 case UPIO_PORT:
2563 return (port1->iobase == port2->iobase);
2564 case UPIO_HUB6:
2565 return (port1->iobase == port2->iobase) &&
2566 (port1->hub6 == port2->hub6);
2567 case UPIO_MEM:
2568 case UPIO_MEM32:
2569 case UPIO_AU:
2570 case UPIO_TSI:
2571 case UPIO_DWAPB:
2572 return (port1->mapbase == port2->mapbase);
2573 }
2574 return 0;
2575 }
2576 EXPORT_SYMBOL(uart_match_port);
2577
2578 EXPORT_SYMBOL(uart_write_wakeup);
2579 EXPORT_SYMBOL(uart_register_driver);
2580 EXPORT_SYMBOL(uart_unregister_driver);
2581 EXPORT_SYMBOL(uart_suspend_port);
2582 EXPORT_SYMBOL(uart_resume_port);
2583 EXPORT_SYMBOL(uart_add_one_port);
2584 EXPORT_SYMBOL(uart_remove_one_port);
2585
2586 MODULE_DESCRIPTION("Serial driver core");
2587 MODULE_LICENSE("GPL");