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