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