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