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