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