1/* 2 * 3 * BRIEF MODULE DESCRIPTION 4 * Au1x00 serial port driver. 5 * 6 * Copyright 2001 MontaVista Software Inc. 7 * Author: MontaVista Software, Inc. 8 * ppopov@mvista.com or source@mvista.com 9 * 10 * Derived almost entirely from drivers/char/serial.c: 11 * 12 * Copyright (C) 1991, 1992 Linus Torvalds 13 * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 14 * 1998, 1999 Theodore Ts'o 15 * 16 * This program is free software; you can redistribute it and/or modify it 17 * under the terms of the GNU General Public License as published by the 18 * Free Software Foundation; either version 2 of the License, or (at your 19 * option) any later version. 20 * 21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 24 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 27 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 28 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * 32 * You should have received a copy of the GNU General Public License along 33 * with this program; if not, write to the Free Software Foundation, Inc., 34 * 675 Mass Ave, Cambridge, MA 02139, USA. 35 */ 36 37static char *serial_version = "1.01"; 38static char *serial_revdate = "2001-02-08"; 39 40 41#include <linux/config.h> 42#include <linux/version.h> 43 44#undef SERIAL_PARANOIA_CHECK 45#define CONFIG_SERIAL_NOPAUSE_IO 46#define SERIAL_DO_RESTART 47 48 49/* Set of debugging defines */ 50 51#undef SERIAL_DEBUG_INTR 52#undef SERIAL_DEBUG_OPEN 53#undef SERIAL_DEBUG_FLOW 54#undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT 55#undef SERIAL_DEBUG_PCI 56#undef SERIAL_DEBUG_AUTOCONF 57 58#ifdef MODULE 59#undef CONFIG_AU1X00_SERIAL_CONSOLE 60#endif 61 62#define CONFIG_SERIAL_RSA 63 64#define RS_STROBE_TIME (10*HZ) 65#define RS_ISR_PASS_LIMIT 256 66 67/* 68 * End of serial driver configuration section. 69 */ 70 71#include <linux/module.h> 72 73#include <linux/types.h> 74#ifdef LOCAL_HEADERS 75#include "serial_local.h" 76#else 77#include <linux/serial.h> 78#include <linux/serialP.h> 79#include <asm/au1000.h> 80#include <asm/serial.h> 81#define LOCAL_VERSTRING "" 82#endif 83 84#include <linux/errno.h> 85#include <linux/signal.h> 86#include <linux/sched.h> 87#include <linux/timer.h> 88#include <linux/interrupt.h> 89#include <linux/tty.h> 90#include <linux/tty_flip.h> 91#include <linux/major.h> 92#include <linux/string.h> 93#include <linux/fcntl.h> 94#include <linux/ptrace.h> 95#include <linux/ioport.h> 96#include <linux/mm.h> 97#include <linux/slab.h> 98#include <linux/init.h> 99#include <asm/uaccess.h> 100#include <linux/delay.h> 101#ifdef CONFIG_AU1X00_SERIAL_CONSOLE 102#include <linux/console.h> 103#endif 104#ifdef CONFIG_MAGIC_SYSRQ 105#include <linux/sysrq.h> 106#endif 107 108#include <asm/system.h> 109#include <asm/io.h> 110#include <asm/irq.h> 111#include <asm/bitops.h> 112 113#ifdef CONFIG_MAC_SERIAL 114#define SERIAL_DEV_OFFSET 2 115#else 116#define SERIAL_DEV_OFFSET 0 117#endif 118 119#ifdef SERIAL_INLINE 120#define _INLINE_ inline 121#else 122#define _INLINE_ 123#endif 124 125static char *serial_name = "Serial driver"; 126 127static DECLARE_TASK_QUEUE(tq_serial); 128 129static struct tty_driver serial_driver, callout_driver; 130static int serial_refcount; 131 132static struct timer_list serial_timer; 133 134extern unsigned long get_au1x00_uart_baud_base(void); 135 136/* serial subtype definitions */ 137#ifndef SERIAL_TYPE_NORMAL 138#define SERIAL_TYPE_NORMAL 1 139#define SERIAL_TYPE_CALLOUT 2 140#endif 141 142/* number of characters left in xmit buffer before we ask for more */ 143#define WAKEUP_CHARS 256 144 145/* 146 * IRQ_timeout - How long the timeout should be for each IRQ 147 * should be after the IRQ has been active. 148 */ 149 150static struct async_struct *IRQ_ports[NR_IRQS]; 151static int IRQ_timeout[NR_IRQS]; 152#ifdef CONFIG_AU1X00_SERIAL_CONSOLE 153static struct console sercons; 154static int lsr_break_flag; 155#endif 156#if defined(CONFIG_AU1X00_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 157static unsigned long break_pressed; /* break, really ... */ 158#endif 159 160static void autoconfig(struct serial_state * state); 161static void change_speed(struct async_struct *info, struct termios *old); 162static void rs_wait_until_sent(struct tty_struct *tty, int timeout); 163 164/* 165 * Here we define the default xmit fifo size used for each type of 166 * UART 167 */ 168static struct serial_uart_config uart_config[] = { 169 { "unknown", 1, 0 }, 170 { "8250", 1, 0 }, 171 { "16450", 1, 0 }, 172 { "16550", 1, 0 }, 173 { 0, 0} 174}; 175 176 177static struct serial_state rs_table[RS_TABLE_SIZE] = { 178 SERIAL_PORT_DFNS /* Defined in serial.h */ 179}; 180 181#define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state)) 182 183#ifndef PREPARE_FUNC 184#define PREPARE_FUNC(dev) (dev->prepare) 185#define ACTIVATE_FUNC(dev) (dev->activate) 186#define DEACTIVATE_FUNC(dev) (dev->deactivate) 187#endif 188 189#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8) 190 191static struct tty_struct *serial_table[NR_PORTS]; 192static struct termios *serial_termios[NR_PORTS]; 193static struct termios *serial_termios_locked[NR_PORTS]; 194 195 196#if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT) 197#define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \ 198 kdevname(tty->device), (info->flags), serial_refcount,info->count,tty->count,s) 199#else 200#define DBG_CNT(s) 201#endif 202 203/* 204 * tmp_buf is used as a temporary buffer by serial_write. We need to 205 * lock it in case the copy_from_user blocks while swapping in a page, 206 * and some other program tries to do a serial write at the same time. 207 * Since the lock will only come under contention when the system is 208 * swapping and available memory is low, it makes sense to share one 209 * buffer across all the serial ports, since it significantly saves 210 * memory if large numbers of serial ports are open. 211 */ 212static unsigned char *tmp_buf; 213#ifdef DECLARE_MUTEX 214static DECLARE_MUTEX(tmp_buf_sem); 215#else 216static struct semaphore tmp_buf_sem = MUTEX; 217#endif 218 219static spinlock_t serial_lock = SPIN_LOCK_UNLOCKED; 220 221static inline int serial_paranoia_check(struct async_struct *info, 222 kdev_t device, const char *routine) 223{ 224#ifdef SERIAL_PARANOIA_CHECK 225 static const char *badmagic = 226 "Warning: bad magic number for serial struct (%s) in %s\n"; 227 static const char *badinfo = 228 "Warning: null async_struct for (%s) in %s\n"; 229 230 if (!info) { 231 printk(badinfo, kdevname(device), routine); 232 return 1; 233 } 234 if (info->magic != SERIAL_MAGIC) { 235 printk(badmagic, kdevname(device), routine); 236 return 1; 237 } 238#endif 239 return 0; 240} 241 242static _INLINE_ unsigned int serial_in(struct async_struct *info, int offset) 243{ 244 return (au_readl(info->port+offset) & 0xffff); 245} 246 247static _INLINE_ void serial_out(struct async_struct *info, int offset, int value) 248{ 249 au_writel(value & 0xffff, info->port+offset); 250} 251 252 253/* 254 * We used to support using pause I/O for certain machines. We 255 * haven't supported this for a while, but just in case it's badly 256 * needed for certain old 386 machines, I've left these #define's 257 * in.... 258 */ 259#define serial_inp(info, offset) serial_in(info, offset) 260#define serial_outp(info, offset, value) serial_out(info, offset, value) 261 262 263/* 264 * ------------------------------------------------------------ 265 * rs_stop() and rs_start() 266 * 267 * This routines are called before setting or resetting tty->stopped. 268 * They enable or disable transmitter interrupts, as necessary. 269 * ------------------------------------------------------------ 270 */ 271static void rs_stop(struct tty_struct *tty) 272{ 273 struct async_struct *info = (struct async_struct *)tty->driver_data; 274 unsigned long flags; 275 276 if (serial_paranoia_check(info, tty->device, "rs_stop")) 277 return; 278 279 spin_lock_irqsave(&serial_lock, flags); 280 if (info->IER & UART_IER_THRI) { 281 info->IER &= ~UART_IER_THRI; 282 serial_out(info, UART_IER, info->IER); 283 } 284 spin_unlock_irqrestore(&serial_lock, flags); 285} 286 287static void rs_start(struct tty_struct *tty) 288{ 289 struct async_struct *info = (struct async_struct *)tty->driver_data; 290 unsigned long flags; 291 292 if (serial_paranoia_check(info, tty->device, "rs_start")) 293 return; 294 295 spin_lock_irqsave(&serial_lock, flags); 296 if (info->xmit.head != info->xmit.tail 297 && info->xmit.buf 298 && !(info->IER & UART_IER_THRI)) { 299 info->IER |= UART_IER_THRI; 300 serial_out(info, UART_IER, info->IER); 301 } 302 spin_unlock_irqrestore(&serial_lock, flags); 303} 304 305/* 306 * ---------------------------------------------------------------------- 307 * 308 * Here starts the interrupt handling routines. All of the following 309 * subroutines are declared as inline and are folded into 310 * rs_interrupt(). They were separated out for readability's sake. 311 * 312 * Note: rs_interrupt() is a "fast" interrupt, which means that it 313 * runs with interrupts turned off. People who may want to modify 314 * rs_interrupt() should try to keep the interrupt handler as fast as 315 * possible. After you are done making modifications, it is not a bad 316 * idea to do: 317 * 318 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c 319 * 320 * and look at the resulting assemble code in serial.s. 321 * 322 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93 323 * ----------------------------------------------------------------------- 324 */ 325 326/* 327 * This routine is used by the interrupt handler to schedule 328 * processing in the software interrupt portion of the driver. 329 */ 330static _INLINE_ void rs_sched_event(struct async_struct *info, 331 int event) 332{ 333 info->event |= 1 << event; 334 queue_task(&info->tqueue, &tq_serial); 335 mark_bh(SERIAL_BH); 336} 337 338static _INLINE_ void receive_chars(struct async_struct *info, 339 int *status, struct pt_regs * regs) 340{ 341 struct tty_struct *tty = info->tty; 342 unsigned char ch; 343 int ignored = 0; 344 struct async_icount *icount; 345 346 icount = &info->state->icount; 347 do { 348 ch = serial_inp(info, UART_RX); 349 if (tty->flip.count >= TTY_FLIPBUF_SIZE) 350 goto ignore_char; 351 *tty->flip.char_buf_ptr = ch; 352 icount->rx++; 353 354#ifdef SERIAL_DEBUG_INTR 355 printk("DR%02x:%02x...", ch, *status); 356#endif 357 *tty->flip.flag_buf_ptr = 0; 358 if (*status & (UART_LSR_BI | UART_LSR_PE | 359 UART_LSR_FE | UART_LSR_OE)) { 360 /* 361 * For statistics only 362 */ 363 if (*status & UART_LSR_BI) { 364 *status &= ~(UART_LSR_FE | UART_LSR_PE); 365 icount->brk++; 366 /* 367 * We do the SysRQ and SAK checking 368 * here because otherwise the break 369 * may get masked by ignore_status_mask 370 * or read_status_mask. 371 */ 372#if defined(CONFIG_AU1X00_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 373 if (info->line == sercons.index) { 374 if (!break_pressed) { 375 break_pressed = jiffies; 376 goto ignore_char; 377 } 378 break_pressed = 0; 379 } 380#endif 381 if (info->flags & ASYNC_SAK) 382 do_SAK(tty); 383 } else if (*status & UART_LSR_PE) 384 icount->parity++; 385 else if (*status & UART_LSR_FE) 386 icount->frame++; 387 if (*status & UART_LSR_OE) 388 icount->overrun++; 389 390 /* 391 * Now check to see if character should be 392 * ignored, and mask off conditions which 393 * should be ignored. 394 */ 395 if (*status & info->ignore_status_mask) { 396 if (++ignored > 100) 397 break; 398 goto ignore_char; 399 } 400 *status &= info->read_status_mask; 401 402#ifdef CONFIG_AU1X00_SERIAL_CONSOLE 403 if (info->line == sercons.index) { 404 /* Recover the break flag from console xmit */ 405 *status |= lsr_break_flag; 406 lsr_break_flag = 0; 407 } 408#endif 409 if (*status & (UART_LSR_BI)) { 410#ifdef SERIAL_DEBUG_INTR 411 printk("handling break...."); 412#endif 413 *tty->flip.flag_buf_ptr = TTY_BREAK; 414 } else if (*status & UART_LSR_PE) 415 *tty->flip.flag_buf_ptr = TTY_PARITY; 416 else if (*status & UART_LSR_FE) 417 *tty->flip.flag_buf_ptr = TTY_FRAME; 418 if (*status & UART_LSR_OE) { 419 /* 420 * Overrun is special, since it's 421 * reported immediately, and doesn't 422 * affect the current character 423 */ 424 tty->flip.count++; 425 tty->flip.flag_buf_ptr++; 426 tty->flip.char_buf_ptr++; 427 *tty->flip.flag_buf_ptr = TTY_OVERRUN; 428 if (tty->flip.count >= TTY_FLIPBUF_SIZE) 429 goto ignore_char; 430 } 431 } 432#if defined(CONFIG_AU1X00_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 433 if (break_pressed && info->line == sercons.index) { 434 if (ch != 0 && 435 time_before(jiffies, break_pressed + HZ*5)) { 436 handle_sysrq(ch, regs, NULL, NULL); 437 break_pressed = 0; 438 goto ignore_char; 439 } 440 break_pressed = 0; 441 } 442#endif 443 tty->flip.flag_buf_ptr++; 444 tty->flip.char_buf_ptr++; 445 tty->flip.count++; 446 ignore_char: 447 *status = serial_inp(info, UART_LSR); 448 } while (*status & UART_LSR_DR); 449 tty_flip_buffer_push(tty); 450} 451 452static _INLINE_ void transmit_chars(struct async_struct *info, int *intr_done) 453{ 454 int count; 455 456 if (info->x_char) { 457 serial_outp(info, UART_TX, info->x_char); 458 info->state->icount.tx++; 459 info->x_char = 0; 460 if (intr_done) 461 *intr_done = 0; 462 return; 463 } 464 if (info->xmit.head == info->xmit.tail 465 || info->tty->stopped 466 || info->tty->hw_stopped) { 467 info->IER &= ~UART_IER_THRI; 468 serial_out(info, UART_IER, info->IER); 469 return; 470 } 471 472 count = info->xmit_fifo_size; 473 do { 474 serial_out(info, UART_TX, info->xmit.buf[info->xmit.tail]); 475 info->xmit.tail = (info->xmit.tail + 1) & (SERIAL_XMIT_SIZE-1); 476 info->state->icount.tx++; 477 if (info->xmit.head == info->xmit.tail) 478 break; 479 } while (--count > 0); 480 481 if (CIRC_CNT(info->xmit.head, 482 info->xmit.tail, 483 SERIAL_XMIT_SIZE) < WAKEUP_CHARS) 484 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP); 485 486#ifdef SERIAL_DEBUG_INTR 487 printk("THRE..."); 488#endif 489 if (intr_done) 490 *intr_done = 0; 491 492 if (info->xmit.head == info->xmit.tail) { 493 info->IER &= ~UART_IER_THRI; 494 serial_out(info, UART_IER, info->IER); 495 } 496} 497 498static _INLINE_ void check_modem_status(struct async_struct *info) 499{ 500 int status; 501 struct async_icount *icount; 502 503 status = serial_in(info, UART_MSR); 504 505 if (status & UART_MSR_ANY_DELTA) { 506 icount = &info->state->icount; 507 /* update input line counters */ 508 if (status & UART_MSR_TERI) 509 icount->rng++; 510 if (status & UART_MSR_DDSR) 511 icount->dsr++; 512 if (status & UART_MSR_DDCD) { 513 icount->dcd++; 514#ifdef CONFIG_HARD_PPS 515 if ((info->flags & ASYNC_HARDPPS_CD) && 516 (status & UART_MSR_DCD)) 517 hardpps(); 518#endif 519 } 520 if (status & UART_MSR_DCTS) 521 icount->cts++; 522 wake_up_interruptible(&info->delta_msr_wait); 523 } 524 525 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) { 526#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR)) 527 printk("ttys%d CD now %s...", info->line, 528 (status & UART_MSR_DCD) ? "on" : "off"); 529#endif 530 if (status & UART_MSR_DCD) 531 wake_up_interruptible(&info->open_wait); 532 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) && 533 (info->flags & ASYNC_CALLOUT_NOHUP))) { 534#ifdef SERIAL_DEBUG_OPEN 535 printk("doing serial hangup..."); 536#endif 537 if (info->tty) 538 tty_hangup(info->tty); 539 } 540 } 541 if (info->flags & ASYNC_CTS_FLOW) { 542 if (info->tty->hw_stopped) { 543 if (status & UART_MSR_CTS) { 544#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW)) 545 printk("CTS tx start..."); 546#endif 547 info->tty->hw_stopped = 0; 548 info->IER |= UART_IER_THRI; 549 serial_out(info, UART_IER, info->IER); 550 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP); 551 return; 552 } 553 } else { 554 if (!(status & UART_MSR_CTS)) { 555#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW)) 556 printk("CTS tx stop..."); 557#endif 558 info->tty->hw_stopped = 1; 559 info->IER &= ~UART_IER_THRI; 560 serial_out(info, UART_IER, info->IER); 561 } 562 } 563 } 564} 565 566 567 568/* 569 * This is the serial driver's interrupt routine for a single port 570 */ 571static void rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs) 572{ 573 int status; 574 int pass_counter = 0; 575 struct async_struct * info; 576 577#ifdef SERIAL_DEBUG_INTR 578 printk("rs_interrupt_single(%d)...", irq); 579#endif 580 581 info = IRQ_ports[irq]; 582 if (!info || !info->tty) 583 return; 584 585 do { 586 status = serial_inp(info, UART_LSR); 587#ifdef SERIAL_DEBUG_INTR 588 printk("status = %x...", status); 589#endif 590 if (status & UART_LSR_DR) 591 receive_chars(info, &status, regs); 592 check_modem_status(info); 593 if (status & UART_LSR_THRE) 594 transmit_chars(info, 0); 595 if (pass_counter++ > RS_ISR_PASS_LIMIT) { 596 break; 597 } 598 } while (!(serial_in(info, UART_IIR) & UART_IIR_NO_INT)); 599 info->last_active = jiffies; 600#ifdef SERIAL_DEBUG_INTR 601 printk("end.\n"); 602#endif 603} 604 605 606/* 607 * ------------------------------------------------------------------- 608 * Here ends the serial interrupt routines. 609 * ------------------------------------------------------------------- 610 */ 611 612/* 613 * This routine is used to handle the "bottom half" processing for the 614 * serial driver, known also the "software interrupt" processing. 615 * This processing is done at the kernel interrupt level, after the 616 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This 617 * is where time-consuming activities which can not be done in the 618 * interrupt driver proper are done; the interrupt driver schedules 619 * them using rs_sched_event(), and they get done here. 620 */ 621static void do_serial_bh(void) 622{ 623 run_task_queue(&tq_serial); 624} 625 626static void do_softint(void *private_) 627{ 628 struct async_struct *info = (struct async_struct *) private_; 629 struct tty_struct *tty; 630 631 tty = info->tty; 632 if (!tty) 633 return; 634 635 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) { 636 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && 637 tty->ldisc.write_wakeup) 638 (tty->ldisc.write_wakeup)(tty); 639 wake_up_interruptible(&tty->write_wait); 640#ifdef SERIAL_HAVE_POLL_WAIT 641 wake_up_interruptible(&tty->poll_wait); 642#endif 643 } 644} 645 646/* 647 * This subroutine is called when the RS_TIMER goes off. It is used 648 * by the serial driver to handle ports that do not have an interrupt 649 * (irq=0). This doesn't work very well for 16450's, but gives barely 650 * passable results for a 16550A. (Although at the expense of much 651 * CPU overhead). 652 */ 653static void rs_timer(unsigned long dummy) 654{ 655 static unsigned long last_strobe; 656 struct async_struct *info; 657 unsigned int i; 658 unsigned long flags; 659 660 if ((jiffies - last_strobe) >= RS_STROBE_TIME) { 661 for (i=0; i < NR_IRQS; i++) { 662 info = IRQ_ports[i]; 663 if (!info) 664 continue; 665 spin_lock_irqsave(&serial_lock, flags); 666 rs_interrupt_single(i, NULL, NULL); 667 spin_unlock_irqrestore(&serial_lock, flags); 668 } 669 } 670 last_strobe = jiffies; 671 mod_timer(&serial_timer, jiffies + RS_STROBE_TIME); 672 673} 674 675/* 676 * --------------------------------------------------------------- 677 * Low level utility subroutines for the serial driver: routines to 678 * figure out the appropriate timeout for an interrupt chain, routines 679 * to initialize and startup a serial port, and routines to shutdown a 680 * serial port. Useful stuff like that. 681 * --------------------------------------------------------------- 682 */ 683 684/* 685 * This routine figures out the correct timeout for a particular IRQ. 686 * It uses the smallest timeout of all of the serial ports in a 687 * particular interrupt chain. Now only used for IRQ 0.... 688 */ 689static void figure_IRQ_timeout(int irq) 690{ 691 struct async_struct *info; 692 int timeout = 60*HZ; /* 60 seconds === a long time :-) */ 693 694 info = IRQ_ports[irq]; 695 if (!info) { 696 IRQ_timeout[irq] = 60*HZ; 697 return; 698 } 699 while (info) { 700 if (info->timeout < timeout) 701 timeout = info->timeout; 702 info = info->next_port; 703 } 704 if (!irq) 705 timeout = timeout / 2; 706 IRQ_timeout[irq] = (timeout > 3) ? timeout-2 : 1; 707} 708 709 710static int startup(struct async_struct * info) 711{ 712 unsigned long flags; 713 int retval=0; 714 void (*handler)(int, void *, struct pt_regs *); 715 struct serial_state *state= info->state; 716 unsigned long page; 717 718 page = get_zeroed_page(GFP_KERNEL); 719 if (!page) 720 return -ENOMEM; 721 722 spin_lock_irqsave(&serial_lock, flags); 723 724 if (info->flags & ASYNC_INITIALIZED) { 725 free_page(page); 726 goto errout; 727 } 728 729 if (!CONFIGURED_SERIAL_PORT(state) || !state->type) { 730 if (info->tty) 731 set_bit(TTY_IO_ERROR, &info->tty->flags); 732 free_page(page); 733 goto errout; 734 } 735 if (info->xmit.buf) 736 free_page(page); 737 else 738 info->xmit.buf = (unsigned char *) page; 739 740 741 if (au_readl(UART_MOD_CNTRL + state->port) != 0x3) { 742 au_writel(3, UART_MOD_CNTRL + state->port); 743 au_sync_delay(10); 744 } 745#ifdef SERIAL_DEBUG_OPEN 746 printk("starting up ttys%d (irq %d)...", info->line, state->irq); 747#endif 748 749 750 /* 751 * Clear the FIFO buffers and disable them 752 * (they will be reenabled in change_speed()) 753 */ 754 if (uart_config[state->type].flags & UART_CLEAR_FIFO) { 755 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO); 756 serial_outp(info, UART_FCR, (UART_FCR_ENABLE_FIFO | 757 UART_FCR_CLEAR_RCVR | 758 UART_FCR_CLEAR_XMIT)); 759 serial_outp(info, UART_FCR, 0); 760 } 761 762 /* 763 * Clear the interrupt registers. 764 */ 765 (void) serial_inp(info, UART_LSR); 766 (void) serial_inp(info, UART_RX); 767 (void) serial_inp(info, UART_IIR); 768 (void) serial_inp(info, UART_MSR); 769 770 /* 771 * At this point there's no way the LSR could still be 0xFF; 772 * if it is, then bail out, because there's likely no UART 773 * here. 774 */ 775 if (!(info->flags & ASYNC_BUGGY_UART) && 776 (serial_inp(info, UART_LSR) == 0xff)) { 777 printk("LSR safety check engaged!\n"); 778 if (capable(CAP_SYS_ADMIN)) { 779 if (info->tty) 780 set_bit(TTY_IO_ERROR, &info->tty->flags); 781 } else 782 retval = -ENODEV; 783 goto errout; 784 } 785 786 /* 787 * Allocate the IRQ if necessary 788 */ 789 if ((!IRQ_ports[state->irq] || !IRQ_ports[state->irq]->next_port)) { 790 if (IRQ_ports[state->irq]) { 791 retval = -EBUSY; 792 goto errout; 793 } else 794 handler = rs_interrupt_single; 795 796 retval = request_irq(state->irq, handler, SA_SHIRQ, 797 "serial", &IRQ_ports[state->irq]); 798 if (retval) { 799 if (capable(CAP_SYS_ADMIN)) { 800 if (info->tty) 801 set_bit(TTY_IO_ERROR, 802 &info->tty->flags); 803 retval = 0; 804 } 805 goto errout; 806 } 807 } 808 809 /* 810 * Insert serial port into IRQ chain. 811 */ 812 info->prev_port = 0; 813 info->next_port = IRQ_ports[state->irq]; 814 if (info->next_port) 815 info->next_port->prev_port = info; 816 IRQ_ports[state->irq] = info; 817 figure_IRQ_timeout(state->irq); 818 819 /* 820 * Now, initialize the UART 821 */ 822 serial_outp(info, UART_LCR, UART_LCR_WLEN8); 823 824 info->MCR = 0; 825 if (info->tty->termios->c_cflag & CBAUD) 826 info->MCR = UART_MCR_DTR | UART_MCR_RTS; 827 { 828 if (state->irq != 0) 829 info->MCR |= UART_MCR_OUT2; 830 } 831 info->MCR |= ALPHA_KLUDGE_MCR; /* Don't ask */ 832 serial_outp(info, UART_MCR, info->MCR); 833 834 /* 835 * Finally, enable interrupts 836 */ 837 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; 838 serial_outp(info, UART_IER, info->IER); /* enable interrupts */ 839 840 841 /* 842 * And clear the interrupt registers again for luck. 843 */ 844 (void)serial_inp(info, UART_LSR); 845 (void)serial_inp(info, UART_RX); 846 (void)serial_inp(info, UART_IIR); 847 (void)serial_inp(info, UART_MSR); 848 849 if (info->tty) 850 clear_bit(TTY_IO_ERROR, &info->tty->flags); 851 info->xmit.head = info->xmit.tail = 0; 852 853 /* 854 * Set up serial timers... 855 */ 856 mod_timer(&serial_timer, jiffies + 2*HZ/100); 857 858 /* 859 * Set up the tty->alt_speed kludge 860 */ 861 if (info->tty) { 862 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI) 863 info->tty->alt_speed = 57600; 864 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI) 865 info->tty->alt_speed = 115200; 866 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI) 867 info->tty->alt_speed = 230400; 868 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP) 869 info->tty->alt_speed = 460800; 870 } 871 872 /* 873 * and set the speed of the serial port 874 */ 875 change_speed(info, 0); 876 877 info->flags |= ASYNC_INITIALIZED; 878 spin_unlock_irqrestore(&serial_lock, flags); 879 return 0; 880 881errout: 882 spin_unlock_irqrestore(&serial_lock, flags); 883 return retval; 884} 885 886/* 887 * This routine will shutdown a serial port; interrupts are disabled, and 888 * DTR is dropped if the hangup on close termio flag is on. 889 */ 890static void shutdown(struct async_struct * info) 891{ 892 unsigned long flags; 893 struct serial_state *state; 894 int retval; 895 896 if (!(info->flags & ASYNC_INITIALIZED)) 897 return; 898 899 state = info->state; 900 901#ifdef SERIAL_DEBUG_OPEN 902 printk("Shutting down serial port %d (irq %d)....", info->line, 903 state->irq); 904#endif 905 906 spin_lock_irqsave(&serial_lock, flags); 907 908 /* 909 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq 910 * here so the queue might never be waken up 911 */ 912 wake_up_interruptible(&info->delta_msr_wait); 913 914 /* 915 * First unlink the serial port from the IRQ chain... 916 */ 917 if (info->next_port) 918 info->next_port->prev_port = info->prev_port; 919 if (info->prev_port) 920 info->prev_port->next_port = info->next_port; 921 else 922 IRQ_ports[state->irq] = info->next_port; 923 figure_IRQ_timeout(state->irq); 924 925 /* 926 * Free the IRQ, if necessary 927 */ 928// if (state->irq && (!IRQ_ports[state->irq] || 929 if ((!IRQ_ports[state->irq] || 930 !IRQ_ports[state->irq]->next_port)) { 931 if (IRQ_ports[state->irq]) { 932 free_irq(state->irq, &IRQ_ports[state->irq]); 933 retval = request_irq(state->irq, rs_interrupt_single, 934 SA_SHIRQ, "serial", 935 &IRQ_ports[state->irq]); 936 937 if (retval) 938 printk("serial shutdown: request_irq: error %d" 939 " Couldn't reacquire IRQ.\n", retval); 940 } else 941 free_irq(state->irq, &IRQ_ports[state->irq]); 942 } 943 944 if (info->xmit.buf) { 945 unsigned long pg = (unsigned long) info->xmit.buf; 946 info->xmit.buf = 0; 947 free_page(pg); 948 } 949 950 info->IER = 0; 951 serial_outp(info, UART_IER, 0x00); /* disable all intrs */ 952 info->MCR &= ~UART_MCR_OUT2; 953 info->MCR |= ALPHA_KLUDGE_MCR; /* Don't ask */ 954 955 /* disable break condition */ 956 serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC); 957 958 if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) 959 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS); 960 serial_outp(info, UART_MCR, info->MCR); 961 962 /* disable FIFO's */ 963 serial_outp(info, UART_FCR, (UART_FCR_ENABLE_FIFO | 964 UART_FCR_CLEAR_RCVR | 965 UART_FCR_CLEAR_XMIT)); 966 serial_outp(info, UART_FCR, 0); 967 968 (void)serial_in(info, UART_RX); /* read data port to reset things */ 969 970 if (info->tty) 971 set_bit(TTY_IO_ERROR, &info->tty->flags); 972 973 info->flags &= ~ASYNC_INITIALIZED; 974#ifndef CONFIG_REMOTE_DEBUG 975 au_writel(0, UART_MOD_CNTRL + state->port); 976 au_sync_delay(10); 977#endif 978 spin_unlock_irqrestore(&serial_lock, flags); 979} 980 981 982/* 983 * This routine is called to set the UART divisor registers to match 984 * the specified baud rate for a serial port. 985 */ 986static void change_speed(struct async_struct *info, 987 struct termios *old_termios) 988{ 989 int quot = 0, baud_base, baud; 990 unsigned cflag, cval, fcr = 0; 991 int bits; 992 unsigned long flags; 993 994 if (!info->tty || !info->tty->termios) 995 return; 996 cflag = info->tty->termios->c_cflag; 997 if (!CONFIGURED_SERIAL_PORT(info)) 998 return; 999 1000 /* byte size and parity */ 1001 switch (cflag & CSIZE) { 1002 case CS5: cval = 0x00; bits = 7; break; 1003 case CS6: cval = 0x01; bits = 8; break; 1004 case CS7: cval = 0x02; bits = 9; break; 1005 case CS8: cval = 0x03; bits = 10; break; 1006 /* Never happens, but GCC is too dumb to figure it out */ 1007 default: cval = 0x00; bits = 7; break; 1008 } 1009 if (cflag & CSTOPB) { 1010 cval |= 0x04; 1011 bits++; 1012 } 1013 if (cflag & PARENB) { 1014 cval |= UART_LCR_PARITY; 1015 bits++; 1016 } 1017 if (!(cflag & PARODD)) 1018 cval |= UART_LCR_EPAR; 1019#ifdef CMSPAR 1020 if (cflag & CMSPAR) 1021 cval |= UART_LCR_SPAR; 1022#endif 1023 1024 /* Determine divisor based on baud rate */ 1025 baud = tty_get_baud_rate(info->tty); 1026 if (!baud) { 1027 baud = 9600; /* B0 transition handled in rs_set_termios */ 1028 } 1029 baud_base = get_au1x00_uart_baud_base(); 1030 1031 //if (baud == 38400 && 1032 if (((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)) { 1033 quot = info->state->custom_divisor; 1034 } 1035 else { 1036 if (baud == 134) 1037 /* Special case since 134 is really 134.5 */ 1038 quot = (2*baud_base / 269); 1039 else if (baud) 1040 quot = baud_base / baud; 1041 } 1042 /* If the quotient is zero refuse the change */ 1043 if (!quot && old_termios) { 1044 info->tty->termios->c_cflag &= ~CBAUD; 1045 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD); 1046 baud = tty_get_baud_rate(info->tty); 1047 if (!baud) 1048 baud = 9600; 1049 if (baud == 38400 && 1050 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)) 1051 quot = info->state->custom_divisor; 1052 else { 1053 if (baud == 134) 1054 /* Special case since 134 is really 134.5 */ 1055 quot = (2*baud_base / 269); 1056 else if (baud) 1057 quot = baud_base / baud; 1058 } 1059 } 1060 /* As a last resort, if the quotient is zero, default to 9600 bps */ 1061 if (!quot) 1062 quot = baud_base / 9600; 1063 1064 info->quot = quot; 1065 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base); 1066 info->timeout += HZ/50; /* Add .02 seconds of slop */ 1067 1068 /* Set up FIFO's */ 1069 if (uart_config[info->state->type].flags & UART_USE_FIFO) { 1070 if ((info->state->baud_base / quot) < 2400) 1071 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIGGER_1; 1072 else 1073 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIGGER_8; 1074 } 1075 1076 /* CTS flow control flag and modem status interrupts */ 1077 info->IER &= ~UART_IER_MSI; 1078 if (info->flags & ASYNC_HARDPPS_CD) 1079 info->IER |= UART_IER_MSI; 1080 if (cflag & CRTSCTS) { 1081 info->flags |= ASYNC_CTS_FLOW; 1082 info->IER |= UART_IER_MSI; 1083 } else 1084 info->flags &= ~ASYNC_CTS_FLOW; 1085 if (cflag & CLOCAL) 1086 info->flags &= ~ASYNC_CHECK_CD; 1087 else { 1088 info->flags |= ASYNC_CHECK_CD; 1089 info->IER |= UART_IER_MSI; 1090 } 1091 serial_out(info, UART_IER, info->IER); 1092 1093 /* 1094 * Set up parity check flag 1095 */ 1096#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK)) 1097 1098 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR; 1099 if (I_INPCK(info->tty)) 1100 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE; 1101 if (I_BRKINT(info->tty) || I_PARMRK(info->tty)) 1102 info->read_status_mask |= UART_LSR_BI; 1103 1104 /* 1105 * Characters to ignore 1106 */ 1107 info->ignore_status_mask = 0; 1108 if (I_IGNPAR(info->tty)) 1109 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE; 1110 if (I_IGNBRK(info->tty)) { 1111 info->ignore_status_mask |= UART_LSR_BI; 1112 /* 1113 * If we're ignore parity and break indicators, ignore 1114 * overruns too. (For real raw support). 1115 */ 1116 if (I_IGNPAR(info->tty)) 1117 info->ignore_status_mask |= UART_LSR_OE; 1118 } 1119 /* 1120 * !!! ignore all characters if CREAD is not set 1121 */ 1122 if ((cflag & CREAD) == 0) 1123 info->ignore_status_mask |= UART_LSR_DR; 1124 spin_lock_irqsave(&serial_lock, flags); 1125 1126 serial_outp(info, UART_CLK, quot & 0xffff); 1127 serial_outp(info, UART_LCR, cval); 1128 info->LCR = cval; /* Save LCR */ 1129 spin_unlock_irqrestore(&serial_lock, flags); 1130} 1131 1132static void rs_put_char(struct tty_struct *tty, unsigned char ch) 1133{ 1134 struct async_struct *info = (struct async_struct *)tty->driver_data; 1135 unsigned long flags; 1136 1137 if (serial_paranoia_check(info, tty->device, "rs_put_char")) 1138 return; 1139 1140 if (!tty || !info->xmit.buf) 1141 return; 1142 1143 spin_lock_irqsave(&serial_lock, flags); 1144 if (CIRC_SPACE(info->xmit.head, 1145 info->xmit.tail, 1146 SERIAL_XMIT_SIZE) == 0) { 1147 spin_unlock_irqrestore(&serial_lock, flags); 1148 return; 1149 } 1150 1151 info->xmit.buf[info->xmit.head] = ch; 1152 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1); 1153 spin_unlock_irqrestore(&serial_lock, flags); 1154} 1155 1156static void rs_flush_chars(struct tty_struct *tty) 1157{ 1158 struct async_struct *info = (struct async_struct *)tty->driver_data; 1159 unsigned long flags; 1160 1161 if (serial_paranoia_check(info, tty->device, "rs_flush_chars")) 1162 return; 1163 1164 if (info->xmit.head == info->xmit.tail 1165 || tty->stopped 1166 || tty->hw_stopped 1167 || !info->xmit.buf) 1168 return; 1169 1170 spin_lock_irqsave(&serial_lock, flags); 1171 info->IER |= UART_IER_THRI; 1172 serial_out(info, UART_IER, info->IER); 1173 spin_unlock_irqrestore(&serial_lock, flags); 1174} 1175 1176static int rs_write(struct tty_struct * tty, int from_user, 1177 const unsigned char *buf, int count) 1178{ 1179 int c, ret = 0; 1180 struct async_struct *info = (struct async_struct *)tty->driver_data; 1181 unsigned long flags; 1182 1183 if (serial_paranoia_check(info, tty->device, "rs_write")) 1184 return 0; 1185 1186 if (!tty || !info->xmit.buf || !tmp_buf) 1187 return 0; 1188 1189 spin_lock_irqsave(&serial_lock, flags); 1190 if (from_user) { 1191 down(&tmp_buf_sem); 1192 while (1) { 1193 int c1; 1194 c = CIRC_SPACE_TO_END(info->xmit.head, 1195 info->xmit.tail, 1196 SERIAL_XMIT_SIZE); 1197 if (count < c) 1198 c = count; 1199 if (c <= 0) 1200 break; 1201 1202 c -= copy_from_user(tmp_buf, buf, c); 1203 if (!c) { 1204 if (!ret) 1205 ret = -EFAULT; 1206 break; 1207 } 1208 cli(); 1209 c1 = CIRC_SPACE_TO_END(info->xmit.head, 1210 info->xmit.tail, 1211 SERIAL_XMIT_SIZE); 1212 if (c1 < c) 1213 c = c1; 1214 memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c); 1215 info->xmit.head = ((info->xmit.head + c) & 1216 (SERIAL_XMIT_SIZE-1)); 1217 spin_unlock_irqrestore(&serial_lock, flags); 1218 buf += c; 1219 count -= c; 1220 ret += c; 1221 } 1222 up(&tmp_buf_sem); 1223 } else { 1224 cli(); 1225 while (1) { 1226 c = CIRC_SPACE_TO_END(info->xmit.head, 1227 info->xmit.tail, 1228 SERIAL_XMIT_SIZE); 1229 if (count < c) 1230 c = count; 1231 if (c <= 0) { 1232 break; 1233 } 1234 memcpy(info->xmit.buf + info->xmit.head, buf, c); 1235 info->xmit.head = ((info->xmit.head + c) & 1236 (SERIAL_XMIT_SIZE-1)); 1237 buf += c; 1238 count -= c; 1239 ret += c; 1240 } 1241 spin_unlock_irqrestore(&serial_lock, flags); 1242 } 1243 if (info->xmit.head != info->xmit.tail 1244 && !tty->stopped 1245 && !tty->hw_stopped 1246 && !(info->IER & UART_IER_THRI)) { 1247 info->IER |= UART_IER_THRI; 1248 serial_out(info, UART_IER, info->IER); 1249 } 1250 return ret; 1251} 1252 1253static int rs_write_room(struct tty_struct *tty) 1254{ 1255 struct async_struct *info = (struct async_struct *)tty->driver_data; 1256 1257 if (serial_paranoia_check(info, tty->device, "rs_write_room")) 1258 return 0; 1259 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE); 1260} 1261 1262static int rs_chars_in_buffer(struct tty_struct *tty) 1263{ 1264 struct async_struct *info = (struct async_struct *)tty->driver_data; 1265 1266 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer")) 1267 return 0; 1268 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE); 1269} 1270 1271static void rs_flush_buffer(struct tty_struct *tty) 1272{ 1273 struct async_struct *info = (struct async_struct *)tty->driver_data; 1274 unsigned long flags; 1275 1276 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer")) 1277 return; 1278 spin_lock_irqsave(&serial_lock, flags); 1279 info->xmit.head = info->xmit.tail = 0; 1280 spin_unlock_irqrestore(&serial_lock, flags); 1281 wake_up_interruptible(&tty->write_wait); 1282#ifdef SERIAL_HAVE_POLL_WAIT 1283 wake_up_interruptible(&tty->poll_wait); 1284#endif 1285 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && 1286 tty->ldisc.write_wakeup) 1287 (tty->ldisc.write_wakeup)(tty); 1288} 1289 1290/* 1291 * This function is used to send a high-priority XON/XOFF character to 1292 * the device 1293 */ 1294static void rs_send_xchar(struct tty_struct *tty, char ch) 1295{ 1296 struct async_struct *info = (struct async_struct *)tty->driver_data; 1297 1298 if (serial_paranoia_check(info, tty->device, "rs_send_char")) 1299 return; 1300 1301 info->x_char = ch; 1302 if (ch) { 1303 /* Make sure transmit interrupts are on */ 1304 info->IER |= UART_IER_THRI; 1305 serial_out(info, UART_IER, info->IER); 1306 } 1307} 1308 1309/* 1310 * ------------------------------------------------------------ 1311 * rs_throttle() 1312 * 1313 * This routine is called by the upper-layer tty layer to signal that 1314 * incoming characters should be throttled. 1315 * ------------------------------------------------------------ 1316 */ 1317static void rs_throttle(struct tty_struct * tty) 1318{ 1319 struct async_struct *info = (struct async_struct *)tty->driver_data; 1320 unsigned long flags; 1321#ifdef SERIAL_DEBUG_THROTTLE 1322 char buf[64]; 1323 1324 printk("throttle %s: %d....\n", tty_name(tty, buf), 1325 tty->ldisc.chars_in_buffer(tty)); 1326#endif 1327 1328 if (serial_paranoia_check(info, tty->device, "rs_throttle")) 1329 return; 1330 1331 if (I_IXOFF(tty)) 1332 rs_send_xchar(tty, STOP_CHAR(tty)); 1333 1334 if (tty->termios->c_cflag & CRTSCTS) 1335 info->MCR &= ~UART_MCR_RTS; 1336 1337 spin_lock_irqsave(&serial_lock, flags); 1338 serial_out(info, UART_MCR, info->MCR); 1339 spin_unlock_irqrestore(&serial_lock, flags); 1340} 1341 1342static void rs_unthrottle(struct tty_struct * tty) 1343{ 1344 struct async_struct *info = (struct async_struct *)tty->driver_data; 1345 unsigned long flags; 1346#ifdef SERIAL_DEBUG_THROTTLE 1347 char buf[64]; 1348 1349 printk("unthrottle %s: %d....\n", tty_name(tty, buf), 1350 tty->ldisc.chars_in_buffer(tty)); 1351#endif 1352 1353 if (serial_paranoia_check(info, tty->device, "rs_unthrottle")) 1354 return; 1355 1356 if (I_IXOFF(tty)) { 1357 if (info->x_char) 1358 info->x_char = 0; 1359 else 1360 rs_send_xchar(tty, START_CHAR(tty)); 1361 } 1362 if (tty->termios->c_cflag & CRTSCTS) 1363 info->MCR |= UART_MCR_RTS; 1364 spin_lock_irqsave(&serial_lock, flags); 1365 serial_out(info, UART_MCR, info->MCR); 1366 spin_unlock_irqrestore(&serial_lock, flags); 1367} 1368 1369/* 1370 * ------------------------------------------------------------ 1371 * rs_ioctl() and friends 1372 * ------------------------------------------------------------ 1373 */ 1374 1375static int get_serial_info(struct async_struct * info, 1376 struct serial_struct * retinfo) 1377{ 1378 struct serial_struct tmp; 1379 struct serial_state *state = info->state; 1380 1381 if (!retinfo) 1382 return -EFAULT; 1383 memset(&tmp, 0, sizeof(tmp)); 1384 tmp.type = state->type; 1385 tmp.line = state->line; 1386 tmp.port = state->port; 1387 if (HIGH_BITS_OFFSET) 1388 tmp.port_high = state->port >> HIGH_BITS_OFFSET; 1389 else 1390 tmp.port_high = 0; 1391 tmp.irq = state->irq; 1392 tmp.flags = state->flags; 1393 tmp.xmit_fifo_size = state->xmit_fifo_size; 1394 tmp.baud_base = state->baud_base; 1395 tmp.close_delay = state->close_delay; 1396 tmp.closing_wait = state->closing_wait; 1397 tmp.custom_divisor = state->custom_divisor; 1398 tmp.hub6 = state->hub6; 1399 tmp.io_type = state->io_type; 1400 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo))) 1401 return -EFAULT; 1402 return 0; 1403} 1404 1405static int set_serial_info(struct async_struct * info, 1406 struct serial_struct * new_info) 1407{ 1408 struct serial_struct new_serial; 1409 struct serial_state old_state, *state; 1410 unsigned int i,change_irq,change_port; 1411 int retval = 0; 1412 unsigned long new_port; 1413 1414 if (copy_from_user(&new_serial,new_info,sizeof(new_serial))) 1415 return -EFAULT; 1416 state = info->state; 1417 old_state = *state; 1418 1419 new_port = new_serial.port; 1420 if (HIGH_BITS_OFFSET) 1421 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET; 1422 1423 change_irq = new_serial.irq != state->irq; 1424 change_port = (new_port != ((int) state->port)) || 1425 (new_serial.hub6 != state->hub6); 1426 1427 if (!capable(CAP_SYS_ADMIN)) { 1428 if (change_irq || change_port || 1429 (new_serial.baud_base != state->baud_base) || 1430 (new_serial.type != state->type) || 1431 (new_serial.close_delay != state->close_delay) || 1432 (new_serial.xmit_fifo_size != state->xmit_fifo_size) || 1433 ((new_serial.flags & ~ASYNC_USR_MASK) != 1434 (state->flags & ~ASYNC_USR_MASK))) 1435 return -EPERM; 1436 state->flags = ((state->flags & ~ASYNC_USR_MASK) | 1437 (new_serial.flags & ASYNC_USR_MASK)); 1438 info->flags = ((info->flags & ~ASYNC_USR_MASK) | 1439 (new_serial.flags & ASYNC_USR_MASK)); 1440 state->custom_divisor = new_serial.custom_divisor; 1441 goto check_and_exit; 1442 } 1443 1444 new_serial.irq = irq_cannonicalize(new_serial.irq); 1445 1446 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) || 1447 (new_serial.baud_base < 9600)|| (new_serial.type < PORT_UNKNOWN) || 1448 (new_serial.type > PORT_MAX) || (new_serial.type == PORT_CIRRUS) || 1449 (new_serial.type == PORT_STARTECH)) { 1450 return -EINVAL; 1451 } 1452 1453 if ((new_serial.type != state->type) || 1454 (new_serial.xmit_fifo_size <= 0)) 1455 new_serial.xmit_fifo_size = 1456 uart_config[new_serial.type].dfl_xmit_fifo_size; 1457 1458 /* Make sure address is not already in use */ 1459 if (new_serial.type) { 1460 for (i = 0 ; i < NR_PORTS; i++) 1461 if ((state != &rs_table[i]) && 1462 (rs_table[i].port == new_port) && 1463 rs_table[i].type) 1464 return -EADDRINUSE; 1465 } 1466 1467 if ((change_port || change_irq) && (state->count > 1)) 1468 return -EBUSY; 1469 1470 /* 1471 * OK, past this point, all the error checking has been done. 1472 * At this point, we start making changes..... 1473 */ 1474 1475 state->baud_base = new_serial.baud_base; 1476 state->flags = ((state->flags & ~ASYNC_FLAGS) | 1477 (new_serial.flags & ASYNC_FLAGS)); 1478 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) | 1479 (info->flags & ASYNC_INTERNAL_FLAGS)); 1480 state->custom_divisor = new_serial.custom_divisor; 1481 state->close_delay = new_serial.close_delay * HZ/100; 1482 state->closing_wait = new_serial.closing_wait * HZ/100; 1483 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; 1484 info->xmit_fifo_size = state->xmit_fifo_size = 1485 new_serial.xmit_fifo_size; 1486 1487 if ((state->type != PORT_UNKNOWN) && state->port) { 1488 release_region(state->port,8); 1489 } 1490 state->type = new_serial.type; 1491 if (change_port || change_irq) { 1492 /* 1493 * We need to shutdown the serial port at the old 1494 * port/irq combination. 1495 */ 1496 shutdown(info); 1497 state->irq = new_serial.irq; 1498 info->port = state->port = new_port; 1499 info->hub6 = state->hub6 = new_serial.hub6; 1500 if (info->hub6) 1501 info->io_type = state->io_type = SERIAL_IO_HUB6; 1502 else if (info->io_type == SERIAL_IO_HUB6) 1503 info->io_type = state->io_type = SERIAL_IO_PORT; 1504 } 1505 if ((state->type != PORT_UNKNOWN) && state->port) { 1506 request_region(state->port,8,"serial(set)"); 1507 } 1508 1509 1510check_and_exit: 1511 if (!state->port || !state->type) 1512 return 0; 1513 if (info->flags & ASYNC_INITIALIZED) { 1514 if (((old_state.flags & ASYNC_SPD_MASK) != 1515 (state->flags & ASYNC_SPD_MASK)) || 1516 (old_state.custom_divisor != state->custom_divisor)) { 1517 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI) 1518 info->tty->alt_speed = 57600; 1519 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI) 1520 info->tty->alt_speed = 115200; 1521 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI) 1522 info->tty->alt_speed = 230400; 1523 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP) 1524 info->tty->alt_speed = 460800; 1525 change_speed(info, 0); 1526 } 1527 } else { 1528 retval = startup(info); 1529 } 1530 return retval; 1531} 1532 1533 1534/* 1535 * get_lsr_info - get line status register info 1536 * 1537 * Purpose: Let user call ioctl() to get info when the UART physically 1538 * is emptied. On bus types like RS485, the transmitter must 1539 * release the bus after transmitting. This must be done when 1540 * the transmit shift register is empty, not be done when the 1541 * transmit holding register is empty. This functionality 1542 * allows an RS485 driver to be written in user space. 1543 */ 1544static int get_lsr_info(struct async_struct * info, unsigned int *value) 1545{ 1546 unsigned char status; 1547 unsigned int result; 1548 unsigned long flags; 1549 1550 spin_lock_irqsave(&serial_lock, flags); 1551 status = serial_in(info, UART_LSR); 1552 spin_unlock_irqrestore(&serial_lock, flags); 1553 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0); 1554 1555 /* 1556 * If we're about to load something into the transmit 1557 * register, we'll pretend the transmitter isn't empty to 1558 * avoid a race condition (depending on when the transmit 1559 * interrupt happens). 1560 */ 1561 if (info->x_char || 1562 ((CIRC_CNT(info->xmit.head, info->xmit.tail, 1563 SERIAL_XMIT_SIZE) > 0) && 1564 !info->tty->stopped && !info->tty->hw_stopped)) 1565 result &= TIOCSER_TEMT; 1566 1567 if (copy_to_user(value, &result, sizeof(int))) 1568 return -EFAULT; 1569 return 0; 1570} 1571 1572 1573static int get_modem_info(struct async_struct * info, unsigned int *value) 1574{ 1575 unsigned char control, status; 1576 unsigned int result; 1577 unsigned long flags; 1578 1579 control = info->MCR; 1580 spin_lock_irqsave(&serial_lock, flags); 1581 status = serial_in(info, UART_MSR); 1582 spin_unlock_irqrestore(&serial_lock, flags); 1583 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) 1584 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) 1585#ifdef TIOCM_OUT1 1586 | ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0) 1587 | ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0) 1588#endif 1589 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) 1590 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0) 1591 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) 1592 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0); 1593 1594 if (copy_to_user(value, &result, sizeof(int))) 1595 return -EFAULT; 1596 return 0; 1597} 1598 1599static int set_modem_info(struct async_struct * info, unsigned int cmd, 1600 unsigned int *value) 1601{ 1602 unsigned int arg; 1603 unsigned long flags; 1604 1605 if (copy_from_user(&arg, value, sizeof(int))) 1606 return -EFAULT; 1607 1608 switch (cmd) { 1609 case TIOCMBIS: 1610 if (arg & TIOCM_RTS) 1611 info->MCR |= UART_MCR_RTS; 1612 if (arg & TIOCM_DTR) 1613 info->MCR |= UART_MCR_DTR; 1614#ifdef TIOCM_OUT1 1615 if (arg & TIOCM_OUT1) 1616 info->MCR |= UART_MCR_OUT1; 1617 if (arg & TIOCM_OUT2) 1618 info->MCR |= UART_MCR_OUT2; 1619#endif 1620 if (arg & TIOCM_LOOP) 1621 info->MCR |= UART_MCR_LOOP; 1622 break; 1623 case TIOCMBIC: 1624 if (arg & TIOCM_RTS) 1625 info->MCR &= ~UART_MCR_RTS; 1626 if (arg & TIOCM_DTR) 1627 info->MCR &= ~UART_MCR_DTR; 1628#ifdef TIOCM_OUT1 1629 if (arg & TIOCM_OUT1) 1630 info->MCR &= ~UART_MCR_OUT1; 1631 if (arg & TIOCM_OUT2) 1632 info->MCR &= ~UART_MCR_OUT2; 1633#endif 1634 if (arg & TIOCM_LOOP) 1635 info->MCR &= ~UART_MCR_LOOP; 1636 break; 1637 case TIOCMSET: 1638 info->MCR = ((info->MCR & ~(UART_MCR_RTS | 1639#ifdef TIOCM_OUT1 1640 UART_MCR_OUT1 | 1641 UART_MCR_OUT2 | 1642#endif 1643 UART_MCR_LOOP | 1644 UART_MCR_DTR)) 1645 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0) 1646#ifdef TIOCM_OUT1 1647 | ((arg & TIOCM_OUT1) ? UART_MCR_OUT1 : 0) 1648 | ((arg & TIOCM_OUT2) ? UART_MCR_OUT2 : 0) 1649#endif 1650 | ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0) 1651 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0)); 1652 break; 1653 default: 1654 return -EINVAL; 1655 } 1656 spin_lock_irqsave(&serial_lock, flags); 1657 info->MCR |= ALPHA_KLUDGE_MCR; /* Don't ask */ 1658 serial_out(info, UART_MCR, info->MCR); 1659 spin_unlock_irqrestore(&serial_lock, flags); 1660 return 0; 1661} 1662 1663static int do_autoconfig(struct async_struct * info) 1664{ 1665 int retval; 1666 1667 if (!capable(CAP_SYS_ADMIN)) 1668 return -EPERM; 1669 1670 if (info->state->count > 1) 1671 return -EBUSY; 1672 1673 shutdown(info); 1674 1675 autoconfig(info->state); 1676 retval = startup(info); 1677 if (retval) 1678 return retval; 1679 return 0; 1680} 1681 1682/* 1683 * rs_break() --- routine which turns the break handling on or off 1684 */ 1685static void rs_break(struct tty_struct *tty, int break_state) 1686{ 1687 struct async_struct * info = (struct async_struct *)tty->driver_data; 1688 unsigned long flags; 1689 1690 if (serial_paranoia_check(info, tty->device, "rs_break")) 1691 return; 1692 1693 if (!CONFIGURED_SERIAL_PORT(info)) 1694 return; 1695 spin_lock_irqsave(&serial_lock, flags); 1696 if (break_state == -1) 1697 info->LCR |= UART_LCR_SBC; 1698 else 1699 info->LCR &= ~UART_LCR_SBC; 1700 serial_out(info, UART_LCR, info->LCR); 1701 spin_unlock_irqrestore(&serial_lock, flags); 1702} 1703 1704 1705static int rs_ioctl(struct tty_struct *tty, struct file * file, 1706 unsigned int cmd, unsigned long arg) 1707{ 1708 struct async_struct * info = (struct async_struct *)tty->driver_data; 1709 struct async_icount cprev, cnow; /* kernel counter temps */ 1710 struct serial_icounter_struct icount; 1711 unsigned long flags; 1712 1713 if (serial_paranoia_check(info, tty->device, "rs_ioctl")) 1714 return -ENODEV; 1715 1716 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) && 1717 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) && 1718 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) { 1719 if (tty->flags & (1 << TTY_IO_ERROR)) 1720 return -EIO; 1721 } 1722 1723 switch (cmd) { 1724 case TIOCMGET: 1725 return get_modem_info(info, (unsigned int *) arg); 1726 case TIOCMBIS: 1727 case TIOCMBIC: 1728 case TIOCMSET: 1729 return set_modem_info(info, cmd, (unsigned int *) arg); 1730 case TIOCGSERIAL: 1731 return get_serial_info(info, 1732 (struct serial_struct *) arg); 1733 case TIOCSSERIAL: 1734 return set_serial_info(info, 1735 (struct serial_struct *) arg); 1736 case TIOCSERCONFIG: 1737 return do_autoconfig(info); 1738 1739 case TIOCSERGETLSR: /* Get line status register */ 1740 return get_lsr_info(info, (unsigned int *) arg); 1741 1742 case TIOCSERGSTRUCT: 1743 if (copy_to_user((struct async_struct *) arg, 1744 info, sizeof(struct async_struct))) 1745 return -EFAULT; 1746 return 0; 1747 1748 1749 /* 1750 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change 1751 * - mask passed in arg for lines of interest 1752 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking) 1753 * Caller should use TIOCGICOUNT to see which one it was 1754 */ 1755 case TIOCMIWAIT: 1756 spin_lock_irqsave(&serial_lock, flags); 1757 /* note the counters on entry */ 1758 cprev = info->state->icount; 1759 spin_unlock_irqrestore(&serial_lock, flags); 1760 /* Force modem status interrupts on */ 1761 info->IER |= UART_IER_MSI; 1762 serial_out(info, UART_IER, info->IER); 1763 while (1) { 1764 interruptible_sleep_on(&info->delta_msr_wait); 1765 /* see if a signal did it */ 1766 if (signal_pending(current)) 1767 return -ERESTARTSYS; 1768 spin_lock_irqsave(&serial_lock, flags); 1769 cnow = info->state->icount; /* atomic copy */ 1770 spin_unlock_irqrestore(&serial_lock, flags); 1771 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 1772 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) 1773 return -EIO; /* no change => error */ 1774 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 1775 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 1776 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || 1777 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) { 1778 return 0; 1779 } 1780 cprev = cnow; 1781 } 1782 /* NOTREACHED */ 1783 1784 /* 1785 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS) 1786 * Return: write counters to the user passed counter struct 1787 * NB: both 1->0 and 0->1 transitions are counted except for 1788 * RI where only 0->1 is counted. 1789 */ 1790 case TIOCGICOUNT: 1791 spin_lock_irqsave(&serial_lock, flags); 1792 cnow = info->state->icount; 1793 spin_unlock_irqrestore(&serial_lock, flags); 1794 icount.cts = cnow.cts; 1795 icount.dsr = cnow.dsr; 1796 icount.rng = cnow.rng; 1797 icount.dcd = cnow.dcd; 1798 icount.rx = cnow.rx; 1799 icount.tx = cnow.tx; 1800 icount.frame = cnow.frame; 1801 icount.overrun = cnow.overrun; 1802 icount.parity = cnow.parity; 1803 icount.brk = cnow.brk; 1804 icount.buf_overrun = cnow.buf_overrun; 1805 1806 if (copy_to_user((void *)arg, &icount, sizeof(icount))) 1807 return -EFAULT; 1808 return 0; 1809 case TIOCSERGWILD: 1810 case TIOCSERSWILD: 1811 /* "setserial -W" is called in Debian boot */ 1812 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n"); 1813 return 0; 1814 1815 default: 1816 return -ENOIOCTLCMD; 1817 } 1818 return 0; 1819} 1820 1821static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios) 1822{ 1823 struct async_struct *info = (struct async_struct *)tty->driver_data; 1824 unsigned long flags; 1825 unsigned int cflag = tty->termios->c_cflag; 1826 1827 if ( (cflag == old_termios->c_cflag) 1828 && ( RELEVANT_IFLAG(tty->termios->c_iflag) 1829 == RELEVANT_IFLAG(old_termios->c_iflag))) 1830 return; 1831 1832 change_speed(info, old_termios); 1833 1834 /* Handle transition to B0 status */ 1835 if ((old_termios->c_cflag & CBAUD) && 1836 !(cflag & CBAUD)) { 1837 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS); 1838 spin_lock_irqsave(&serial_lock, flags); 1839 serial_out(info, UART_MCR, info->MCR); 1840 spin_unlock_irqrestore(&serial_lock, flags); 1841 } 1842 1843 /* Handle transition away from B0 status */ 1844 if (!(old_termios->c_cflag & CBAUD) && 1845 (cflag & CBAUD)) { 1846 info->MCR |= UART_MCR_DTR; 1847 if (!(tty->termios->c_cflag & CRTSCTS) || 1848 !test_bit(TTY_THROTTLED, &tty->flags)) { 1849 info->MCR |= UART_MCR_RTS; 1850 } 1851 spin_lock_irqsave(&serial_lock, flags); 1852 serial_out(info, UART_MCR, info->MCR); 1853 spin_unlock_irqrestore(&serial_lock, flags); 1854 } 1855 1856 /* Handle turning off CRTSCTS */ 1857 if ((old_termios->c_cflag & CRTSCTS) && 1858 !(tty->termios->c_cflag & CRTSCTS)) { 1859 tty->hw_stopped = 0; 1860 rs_start(tty); 1861 } 1862} 1863 1864/* 1865 * ------------------------------------------------------------ 1866 * rs_close() 1867 * 1868 * This routine is called when the serial port gets closed. First, we 1869 * wait for the last remaining data to be sent. Then, we unlink its 1870 * async structure from the interrupt chain if necessary, and we free 1871 * that IRQ if nothing is left in the chain. 1872 * ------------------------------------------------------------ 1873 */ 1874static void rs_close(struct tty_struct *tty, struct file * filp) 1875{ 1876 struct async_struct * info = (struct async_struct *)tty->driver_data; 1877 struct serial_state *state; 1878 unsigned long flags; 1879 1880 if (!info || serial_paranoia_check(info, tty->device, "rs_close")) 1881 return; 1882 1883 state = info->state; 1884 1885 spin_lock_irqsave(&serial_lock, flags); 1886 1887 if (tty_hung_up_p(filp)) { 1888 DBG_CNT("before DEC-hung"); 1889 MOD_DEC_USE_COUNT; 1890 spin_unlock_irqrestore(&serial_lock, flags); 1891 return; 1892 } 1893 1894#ifdef SERIAL_DEBUG_OPEN 1895 printk("rs_close ttys%d, count = %d\n", info->line, state->count); 1896#endif 1897 if ((tty->count == 1) && (state->count != 1)) { 1898 /* 1899 * Uh, oh. tty->count is 1, which means that the tty 1900 * structure will be freed. state->count should always 1901 * be one in these conditions. If it's greater than 1902 * one, we've got real problems, since it means the 1903 * serial port won't be shutdown. 1904 */ 1905 printk("rs_close: bad serial port count; tty->count is 1, " 1906 "state->count is %d\n", state->count); 1907 state->count = 1; 1908 } 1909 if (--state->count < 0) { 1910 printk("rs_close: bad serial port count for ttys%d: %d\n", 1911 info->line, state->count); 1912 state->count = 0; 1913 } 1914 if (state->count) { 1915 DBG_CNT("before DEC-2"); 1916 MOD_DEC_USE_COUNT; 1917 spin_unlock_irqrestore(&serial_lock, flags); 1918 return; 1919 } 1920 info->flags |= ASYNC_CLOSING; 1921 spin_unlock_irqrestore(&serial_lock, flags); 1922 /* 1923 * Save the termios structure, since this port may have 1924 * separate termios for callout and dialin. 1925 */ 1926 if (info->flags & ASYNC_NORMAL_ACTIVE) 1927 info->state->normal_termios = *tty->termios; 1928 if (info->flags & ASYNC_CALLOUT_ACTIVE) 1929 info->state->callout_termios = *tty->termios; 1930 /* 1931 * Now we wait for the transmit buffer to clear; and we notify 1932 * the line discipline to only process XON/XOFF characters. 1933 */ 1934 tty->closing = 1; 1935 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) 1936 tty_wait_until_sent(tty, info->closing_wait); 1937 /* 1938 * At this point we stop accepting input. To do this, we 1939 * disable the receive line status interrupts, and tell the 1940 * interrupt driver to stop checking the data ready bit in the 1941 * line status register. 1942 */ 1943 info->IER &= ~UART_IER_RLSI; 1944 info->read_status_mask &= ~UART_LSR_DR; 1945 if (info->flags & ASYNC_INITIALIZED) { 1946 serial_out(info, UART_IER, info->IER); 1947 /* 1948 * Before we drop DTR, make sure the UART transmitter 1949 * has completely drained; this is especially 1950 * important if there is a transmit FIFO! 1951 */ 1952 rs_wait_until_sent(tty, info->timeout); 1953 } 1954 shutdown(info); 1955 if (tty->driver.flush_buffer) 1956 tty->driver.flush_buffer(tty); 1957 if (tty->ldisc.flush_buffer) 1958 tty->ldisc.flush_buffer(tty); 1959 tty->closing = 0; 1960 info->event = 0; 1961 info->tty = 0; 1962 if (info->blocked_open) { 1963 if (info->close_delay) { 1964 set_current_state(TASK_INTERRUPTIBLE); 1965 schedule_timeout(info->close_delay); 1966 } 1967 wake_up_interruptible(&info->open_wait); 1968 } 1969 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE| 1970 ASYNC_CLOSING); 1971 wake_up_interruptible(&info->close_wait); 1972 MOD_DEC_USE_COUNT; 1973} 1974 1975/* 1976 * rs_wait_until_sent() --- wait until the transmitter is empty 1977 */ 1978static void rs_wait_until_sent(struct tty_struct *tty, int timeout) 1979{ 1980 struct async_struct * info = (struct async_struct *)tty->driver_data; 1981 unsigned long orig_jiffies, char_time; 1982 int lsr; 1983 1984 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent")) 1985 return; 1986 1987 if (info->state->type == PORT_UNKNOWN) 1988 return; 1989 1990 if (info->xmit_fifo_size == 0) 1991 return; /* Just in case.... */ 1992 1993 orig_jiffies = jiffies; 1994 /* 1995 * Set the check interval to be 1/5 of the estimated time to 1996 * send a single character, and make it at least 1. The check 1997 * interval should also be less than the timeout. 1998 * 1999 * Note: we have to use pretty tight timings here to satisfy 2000 * the NIST-PCTS. 2001 */ 2002 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size; 2003 char_time = char_time / 5; 2004 if (char_time == 0) 2005 char_time = 1; 2006 if (timeout && timeout < char_time) 2007 char_time = timeout; 2008 /* 2009 * If the transmitter hasn't cleared in twice the approximate 2010 * amount of time to send the entire FIFO, it probably won't 2011 * ever clear. This assumes the UART isn't doing flow 2012 * control, which is currently the case. Hence, if it ever 2013 * takes longer than info->timeout, this is probably due to a 2014 * UART bug of some kind. So, we clamp the timeout parameter at 2015 * 2*info->timeout. 2016 */ 2017 if (!timeout || timeout > 2*info->timeout) 2018 timeout = 2*info->timeout; 2019#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT 2020 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time); 2021 printk("jiff=%lu...", jiffies); 2022#endif 2023 while (!((lsr = serial_inp(info, UART_LSR)) & UART_LSR_TEMT)) { 2024#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT 2025 printk("lsr = %d (jiff=%lu)...", lsr, jiffies); 2026#endif 2027 set_current_state(TASK_INTERRUPTIBLE); 2028 schedule_timeout(char_time); 2029 if (signal_pending(current)) 2030 break; 2031 if (timeout && time_after(jiffies, orig_jiffies + timeout)) 2032 break; 2033 } 2034 set_current_state(TASK_RUNNING); 2035#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT 2036 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies); 2037#endif 2038} 2039 2040/* 2041 * rs_hangup() --- called by tty_hangup() when a hangup is signaled. 2042 */ 2043static void rs_hangup(struct tty_struct *tty) 2044{ 2045 struct async_struct * info = (struct async_struct *)tty->driver_data; 2046 struct serial_state *state = info->state; 2047 2048 if (serial_paranoia_check(info, tty->device, "rs_hangup")) 2049 return; 2050 2051 state = info->state; 2052 2053 rs_flush_buffer(tty); 2054 if (info->flags & ASYNC_CLOSING) 2055 return; 2056 shutdown(info); 2057 info->event = 0; 2058 state->count = 0; 2059 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE); 2060 info->tty = 0; 2061 wake_up_interruptible(&info->open_wait); 2062} 2063 2064/* 2065 * ------------------------------------------------------------ 2066 * rs_open() and friends 2067 * ------------------------------------------------------------ 2068 */ 2069static int block_til_ready(struct tty_struct *tty, struct file * filp, 2070 struct async_struct *info) 2071{ 2072 DECLARE_WAITQUEUE(wait, current); 2073 struct serial_state *state = info->state; 2074 int retval; 2075 int do_clocal = 0, extra_count = 0; 2076 unsigned long flags; 2077 2078 /* 2079 * If the device is in the middle of being closed, then block 2080 * until it's done, and then try again. 2081 */ 2082 if (tty_hung_up_p(filp) || 2083 (info->flags & ASYNC_CLOSING)) { 2084 if (info->flags & ASYNC_CLOSING) 2085 interruptible_sleep_on(&info->close_wait); 2086#ifdef SERIAL_DO_RESTART 2087 return ((info->flags & ASYNC_HUP_NOTIFY) ? 2088 -EAGAIN : -ERESTARTSYS); 2089#else 2090 return -EAGAIN; 2091#endif 2092 } 2093 2094 /* 2095 * If this is a callout device, then just make sure the normal 2096 * device isn't being used. 2097 */ 2098 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) { 2099 if (info->flags & ASYNC_NORMAL_ACTIVE) 2100 return -EBUSY; 2101 if ((info->flags & ASYNC_CALLOUT_ACTIVE) && 2102 (info->flags & ASYNC_SESSION_LOCKOUT) && 2103 (info->session != current->session)) 2104 return -EBUSY; 2105 if ((info->flags & ASYNC_CALLOUT_ACTIVE) && 2106 (info->flags & ASYNC_PGRP_LOCKOUT) && 2107 (info->pgrp != current->pgrp)) 2108 return -EBUSY; 2109 info->flags |= ASYNC_CALLOUT_ACTIVE; 2110 return 0; 2111 } 2112 2113 /* 2114 * If non-blocking mode is set, or the port is not enabled, 2115 * then make the check up front and then exit. 2116 */ 2117 if ((filp->f_flags & O_NONBLOCK) || 2118 (tty->flags & (1 << TTY_IO_ERROR))) { 2119 if (info->flags & ASYNC_CALLOUT_ACTIVE) 2120 return -EBUSY; 2121 info->flags |= ASYNC_NORMAL_ACTIVE; 2122 return 0; 2123 } 2124 2125 if (info->flags & ASYNC_CALLOUT_ACTIVE) { 2126 if (state->normal_termios.c_cflag & CLOCAL) 2127 do_clocal = 1; 2128 } else { 2129 if (tty->termios->c_cflag & CLOCAL) 2130 do_clocal = 1; 2131 } 2132 2133 /* 2134 * Block waiting for the carrier detect and the line to become 2135 * free (i.e., not in use by the callout). While we are in 2136 * this loop, state->count is dropped by one, so that 2137 * rs_close() knows when to free things. We restore it upon 2138 * exit, either normal or abnormal. 2139 */ 2140 retval = 0; 2141 add_wait_queue(&info->open_wait, &wait); 2142#ifdef SERIAL_DEBUG_OPEN 2143 printk("block_til_ready before block: ttys%d, count = %d\n", 2144 state->line, state->count); 2145#endif 2146 spin_lock_irqsave(&serial_lock, flags); 2147 if (!tty_hung_up_p(filp)) { 2148 extra_count = 1; 2149 state->count--; 2150 } 2151 spin_unlock_irqrestore(&serial_lock, flags); 2152 info->blocked_open++; 2153 while (1) { 2154 spin_lock_irqsave(&serial_lock, flags); 2155 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) && 2156 (tty->termios->c_cflag & CBAUD)) 2157 serial_out(info, UART_MCR, 2158 serial_inp(info, UART_MCR) | 2159 (UART_MCR_DTR | UART_MCR_RTS)); 2160 spin_unlock_irqrestore(&serial_lock, flags); 2161 set_current_state(TASK_INTERRUPTIBLE); 2162 if (tty_hung_up_p(filp) || 2163 !(info->flags & ASYNC_INITIALIZED)) { 2164#ifdef SERIAL_DO_RESTART 2165 if (info->flags & ASYNC_HUP_NOTIFY) 2166 retval = -EAGAIN; 2167 else 2168 retval = -ERESTARTSYS; 2169#else 2170 retval = -EAGAIN; 2171#endif 2172 break; 2173 } 2174 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) && 2175 !(info->flags & ASYNC_CLOSING) && 2176 (do_clocal || (serial_in(info, UART_MSR) & 2177 UART_MSR_DCD))) 2178 break; 2179 if (signal_pending(current)) { 2180 retval = -ERESTARTSYS; 2181 break; 2182 } 2183#ifdef SERIAL_DEBUG_OPEN 2184 printk("block_til_ready blocking: ttys%d, count = %d\n", 2185 info->line, state->count); 2186#endif 2187 schedule(); 2188 } 2189 set_current_state(TASK_RUNNING); 2190 remove_wait_queue(&info->open_wait, &wait); 2191 if (extra_count) 2192 state->count++; 2193 info->blocked_open--; 2194#ifdef SERIAL_DEBUG_OPEN 2195 printk("block_til_ready after blocking: ttys%d, count = %d\n", 2196 info->line, state->count); 2197#endif 2198 if (retval) 2199 return retval; 2200 info->flags |= ASYNC_NORMAL_ACTIVE; 2201 return 0; 2202} 2203 2204static int get_async_struct(int line, struct async_struct **ret_info) 2205{ 2206 struct async_struct *info; 2207 struct serial_state *sstate; 2208 2209 sstate = rs_table + line; 2210 sstate->count++; 2211 if (sstate->info) { 2212 *ret_info = sstate->info; 2213 return 0; 2214 } 2215 info = kmalloc(sizeof(struct async_struct), GFP_KERNEL); 2216 if (!info) { 2217 sstate->count--; 2218 return -ENOMEM; 2219 } 2220 memset(info, 0, sizeof(struct async_struct)); 2221 init_waitqueue_head(&info->open_wait); 2222 init_waitqueue_head(&info->close_wait); 2223 init_waitqueue_head(&info->delta_msr_wait); 2224 info->magic = SERIAL_MAGIC; 2225 info->port = sstate->port; 2226 info->flags = sstate->flags; 2227 info->io_type = sstate->io_type; 2228 info->iomem_base = sstate->iomem_base; 2229 info->iomem_reg_shift = sstate->iomem_reg_shift; 2230 info->xmit_fifo_size = sstate->xmit_fifo_size; 2231 info->line = line; 2232 info->tqueue.routine = do_softint; 2233 info->tqueue.data = info; 2234 info->state = sstate; 2235 if (sstate->info) { 2236 kfree(info); 2237 *ret_info = sstate->info; 2238 return 0; 2239 } 2240 *ret_info = sstate->info = info; 2241 return 0; 2242} 2243 2244/* 2245 * This routine is called whenever a serial port is opened. It 2246 * enables interrupts for a serial port, linking in its async structure into 2247 * the IRQ chain. It also performs the serial-specific 2248 * initialization for the tty structure. 2249 */ 2250static int rs_open(struct tty_struct *tty, struct file * filp) 2251{ 2252 struct async_struct *info; 2253 int retval, line; 2254 unsigned long page; 2255 2256 MOD_INC_USE_COUNT; 2257 line = MINOR(tty->device) - tty->driver.minor_start; 2258 if ((line < 0) || (line >= NR_PORTS)) { 2259 MOD_DEC_USE_COUNT; 2260 return -ENODEV; 2261 } 2262 retval = get_async_struct(line, &info); 2263 if (retval) { 2264 MOD_DEC_USE_COUNT; 2265 return retval; 2266 } 2267 tty->driver_data = info; 2268 info->tty = tty; 2269 if (serial_paranoia_check(info, tty->device, "rs_open")) { 2270 MOD_DEC_USE_COUNT; 2271 return -ENODEV; 2272 } 2273 2274#ifdef SERIAL_DEBUG_OPEN 2275 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line, 2276 info->state->count); 2277#endif 2278 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; 2279 2280 if (!tmp_buf) { 2281 page = get_zeroed_page(GFP_KERNEL); 2282 if (!page) { 2283 MOD_DEC_USE_COUNT; 2284 return -ENOMEM; 2285 } 2286 if (tmp_buf) 2287 free_page(page); 2288 else 2289 tmp_buf = (unsigned char *) page; 2290 } 2291 2292 /* 2293 * If the port is the middle of closing, bail out now 2294 */ 2295 if (tty_hung_up_p(filp) || 2296 (info->flags & ASYNC_CLOSING)) { 2297 if (info->flags & ASYNC_CLOSING) 2298 interruptible_sleep_on(&info->close_wait); 2299 MOD_DEC_USE_COUNT; 2300#ifdef SERIAL_DO_RESTART 2301 return ((info->flags & ASYNC_HUP_NOTIFY) ? 2302 -EAGAIN : -ERESTARTSYS); 2303#else 2304 return -EAGAIN; 2305#endif 2306 } 2307 2308 /* 2309 * Start up serial port 2310 */ 2311 retval = startup(info); 2312 if (retval) { 2313 MOD_DEC_USE_COUNT; 2314 return retval; 2315 } 2316 2317 retval = block_til_ready(tty, filp, info); 2318 if (retval) { 2319#ifdef SERIAL_DEBUG_OPEN 2320 printk("rs_open returning after block_til_ready with %d\n", 2321 retval); 2322#endif 2323 MOD_DEC_USE_COUNT; 2324 return retval; 2325 } 2326 2327 if ((info->state->count == 1) && 2328 (info->flags & ASYNC_SPLIT_TERMIOS)) { 2329 if (tty->driver.subtype == SERIAL_TYPE_NORMAL) 2330 *tty->termios = info->state->normal_termios; 2331 else 2332 *tty->termios = info->state->callout_termios; 2333 change_speed(info, 0); 2334 } 2335#ifdef CONFIG_AU1X00_SERIAL_CONSOLE 2336 if (sercons.cflag && sercons.index == line) { 2337 tty->termios->c_cflag = sercons.cflag; 2338 sercons.cflag = 0; 2339 change_speed(info, 0); 2340 } 2341#endif 2342 info->session = current->session; 2343 info->pgrp = current->pgrp; 2344 2345#ifdef SERIAL_DEBUG_OPEN 2346 printk("rs_open ttys%d successful...", info->line); 2347#endif 2348 return 0; 2349} 2350 2351/* 2352 * /proc fs routines.... 2353 */ 2354 2355static inline int line_info(char *buf, struct serial_state *state) 2356{ 2357 struct async_struct *info = state->info, scr_info; 2358 char stat_buf[30], control, status; 2359 int ret; 2360 unsigned long flags; 2361 2362 ret = sprintf(buf, "%d: uart:%s port:%lX irq:%d", 2363 state->line, uart_config[state->type].name, 2364 state->port, state->irq); 2365 2366 if (!state->port || (state->type == PORT_UNKNOWN)) { 2367 ret += sprintf(buf+ret, "\n"); 2368 return ret; 2369 } 2370 2371 /* 2372 * Figure out the current RS-232 lines 2373 */ 2374 if (!info) { 2375 info = &scr_info; /* This is just for serial_{in,out} */ 2376 2377 info->magic = SERIAL_MAGIC; 2378 info->port = state->port; 2379 info->flags = state->flags; 2380 info->quot = 0; 2381 info->tty = 0; 2382 } 2383 spin_lock_irqsave(&serial_lock, flags); 2384 status = serial_in(info, UART_MSR); 2385 control = info != &scr_info ? info->MCR : serial_in(info, UART_MCR); 2386 spin_unlock_irqrestore(&serial_lock, flags); 2387 2388 stat_buf[0] = 0; 2389 stat_buf[1] = 0; 2390 if (control & UART_MCR_RTS) 2391 strcat(stat_buf, "|RTS"); 2392 if (status & UART_MSR_CTS) 2393 strcat(stat_buf, "|CTS"); 2394 if (control & UART_MCR_DTR) 2395 strcat(stat_buf, "|DTR"); 2396 if (status & UART_MSR_DSR) 2397 strcat(stat_buf, "|DSR"); 2398 if (status & UART_MSR_DCD) 2399 strcat(stat_buf, "|CD"); 2400 if (status & UART_MSR_RI) 2401 strcat(stat_buf, "|RI"); 2402 2403 if (info->quot) { 2404 ret += sprintf(buf+ret, " baud:%d", 2405 state->baud_base / info->quot); 2406 } 2407 2408 ret += sprintf(buf+ret, " tx:%d rx:%d", 2409 state->icount.tx, state->icount.rx); 2410 2411 if (state->icount.frame) 2412 ret += sprintf(buf+ret, " fe:%d", state->icount.frame); 2413 2414 if (state->icount.parity) 2415 ret += sprintf(buf+ret, " pe:%d", state->icount.parity); 2416 2417 if (state->icount.brk) 2418 ret += sprintf(buf+ret, " brk:%d", state->icount.brk); 2419 2420 if (state->icount.overrun) 2421 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun); 2422 2423 /* 2424 * Last thing is the RS-232 status lines 2425 */ 2426 ret += sprintf(buf+ret, " %s\n", stat_buf+1); 2427 return ret; 2428} 2429 2430int rs_read_proc(char *page, char **start, off_t off, int count, 2431 int *eof, void *data) 2432{ 2433 int i, len = 0, l; 2434 off_t begin = 0; 2435 2436 len += sprintf(page, "serinfo:1.0 driver:%s%s revision:%s\n", 2437 serial_version, LOCAL_VERSTRING, serial_revdate); 2438 for (i = 0; i < NR_PORTS && len < 4000; i++) { 2439 l = line_info(page + len, &rs_table[i]); 2440 len += l; 2441 if (len+begin > off+count) 2442 goto done; 2443 if (len+begin < off) { 2444 begin += len; 2445 len = 0; 2446 } 2447 } 2448 *eof = 1; 2449done: 2450 if (off >= len+begin) 2451 return 0; 2452 *start = page + (off-begin); 2453 return ((count < begin+len-off) ? count : begin+len-off); 2454} 2455 2456/* 2457 * --------------------------------------------------------------------- 2458 * rs_init() and friends 2459 * 2460 * rs_init() is called at boot-time to initialize the serial driver. 2461 * --------------------------------------------------------------------- 2462 */ 2463 2464/* 2465 * This routine prints out the appropriate serial driver version 2466 * number, and identifies which options were configured into this 2467 * driver. 2468 */ 2469static char serial_options[] __initdata = 2470 " no serial options enabled\n"; 2471#undef SERIAL_OPT 2472 2473static _INLINE_ void show_serial_version(void) 2474{ 2475 printk(KERN_INFO "%s version %s%s (%s) with%s", serial_name, 2476 serial_version, LOCAL_VERSTRING, serial_revdate, 2477 serial_options); 2478} 2479 2480 2481/* 2482 * This routine is called by rs_init() to initialize a specific serial 2483 * port. It determines what type of UART chip this serial port is 2484 * using: 8250, 16450, 16550, 16550A. The important question is 2485 * whether or not this UART is a 16550A or not, since this will 2486 * determine whether or not we can use its FIFO features or not. 2487 */ 2488static void autoconfig(struct serial_state * state) 2489{ 2490 struct async_struct *info, scr_info; 2491 unsigned long flags; 2492 2493 2494#ifdef SERIAL_DEBUG_AUTOCONF 2495 printk("Testing ttyS%d (0x%04lx, 0x%04x)...\n", state->line, 2496 state->port, (unsigned) state->iomem_base); 2497#endif 2498 2499 if (!CONFIGURED_SERIAL_PORT(state)) 2500 return; 2501 2502 if (au_readl(UART_MOD_CNTRL + state->port) != 0x3) { 2503 au_writel(3, UART_MOD_CNTRL + state->port); 2504 au_sync_delay(10); 2505 } 2506 2507 state->type = PORT_16550; 2508 info = &scr_info; /* This is just for serial_{in,out} */ 2509 2510 info->magic = SERIAL_MAGIC; 2511 info->state = state; 2512 info->port = state->port; 2513 info->flags = state->flags; 2514 info->io_type = state->io_type; 2515 info->iomem_base = state->iomem_base; 2516 info->iomem_reg_shift = state->iomem_reg_shift; 2517 2518 2519 spin_lock_irqsave(&serial_lock, flags); 2520 state->xmit_fifo_size = uart_config[state->type].dfl_xmit_fifo_size; 2521 2522 if (info->port) { 2523 request_region(info->port,8,"serial(auto)"); 2524 } 2525 2526 /* 2527 * Reset the UART. 2528 */ 2529 serial_outp(info, UART_FCR, (UART_FCR_ENABLE_FIFO | 2530 UART_FCR_CLEAR_RCVR | 2531 UART_FCR_CLEAR_XMIT)); 2532 serial_outp(info, UART_FCR, 0); 2533 (void)serial_in(info, UART_RX); 2534 serial_outp(info, UART_IER, 0); 2535 spin_unlock_irqrestore(&serial_lock, flags); 2536} 2537 2538int register_serial(struct serial_struct *req); 2539void unregister_serial(int line); 2540 2541EXPORT_SYMBOL(register_serial); 2542EXPORT_SYMBOL(unregister_serial); 2543 2544 2545/* 2546 * The serial driver boot-time initialization code! 2547 */ 2548static int __init rs_init(void) 2549{ 2550 int i; 2551 struct serial_state * state; 2552 2553 init_bh(SERIAL_BH, do_serial_bh); 2554 init_timer(&serial_timer); 2555 serial_timer.function = rs_timer; 2556 mod_timer(&serial_timer, jiffies + RS_STROBE_TIME); 2557 2558 for (i = 0; i < NR_IRQS; i++) { 2559 IRQ_ports[i] = 0; 2560 IRQ_timeout[i] = 0; 2561 } 2562#ifdef CONFIG_AU1X00_SERIAL_CONSOLE 2563 /* 2564 * The interrupt of the serial console port 2565 * can't be shared. 2566 */ 2567 if (sercons.flags & CON_CONSDEV) { 2568 for(i = 0; i < NR_PORTS; i++) 2569 if (i != sercons.index && 2570 rs_table[i].irq == rs_table[sercons.index].irq) 2571 rs_table[i].irq = 0; 2572 } 2573#endif 2574 show_serial_version(); 2575 2576 /* Initialize the tty_driver structure */ 2577 2578 memset(&serial_driver, 0, sizeof(struct tty_driver)); 2579 serial_driver.magic = TTY_DRIVER_MAGIC; 2580 serial_driver.driver_name = "serial"; 2581#if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS)) 2582 serial_driver.name = "tts/%d"; 2583#else 2584 serial_driver.name = "ttyS"; 2585#endif 2586 serial_driver.major = TTY_MAJOR; 2587 serial_driver.minor_start = 64 + SERIAL_DEV_OFFSET; 2588 serial_driver.num = NR_PORTS; 2589 serial_driver.type = TTY_DRIVER_TYPE_SERIAL; 2590 serial_driver.subtype = SERIAL_TYPE_NORMAL; 2591 serial_driver.init_termios = tty_std_termios; 2592 serial_driver.init_termios.c_cflag = 2593 B9600 | CS8 | CREAD | HUPCL | CLOCAL; 2594 serial_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS; 2595 serial_driver.refcount = &serial_refcount; 2596 serial_driver.table = serial_table; 2597 serial_driver.termios = serial_termios; 2598 serial_driver.termios_locked = serial_termios_locked; 2599 2600 serial_driver.open = rs_open; 2601 serial_driver.close = rs_close; 2602 serial_driver.write = rs_write; 2603 serial_driver.put_char = rs_put_char; 2604 serial_driver.flush_chars = rs_flush_chars; 2605 serial_driver.write_room = rs_write_room; 2606 serial_driver.chars_in_buffer = rs_chars_in_buffer; 2607 serial_driver.flush_buffer = rs_flush_buffer; 2608 serial_driver.ioctl = rs_ioctl; 2609 serial_driver.throttle = rs_throttle; 2610 serial_driver.unthrottle = rs_unthrottle; 2611 serial_driver.set_termios = rs_set_termios; 2612 serial_driver.stop = rs_stop; 2613 serial_driver.start = rs_start; 2614 serial_driver.hangup = rs_hangup; 2615 serial_driver.break_ctl = rs_break; 2616 serial_driver.send_xchar = rs_send_xchar; 2617 serial_driver.wait_until_sent = rs_wait_until_sent; 2618 serial_driver.read_proc = rs_read_proc; 2619 2620 /* 2621 * The callout device is just like normal device except for 2622 * major number and the subtype code. 2623 */ 2624 callout_driver = serial_driver; 2625#if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS)) 2626 callout_driver.name = "cua/%d"; 2627#else 2628 callout_driver.name = "cua"; 2629#endif 2630 callout_driver.major = TTYAUX_MAJOR; 2631 callout_driver.subtype = SERIAL_TYPE_CALLOUT; 2632 callout_driver.read_proc = 0; 2633 callout_driver.proc_entry = 0; 2634 2635 if (tty_register_driver(&serial_driver)) 2636 panic("Couldn't register serial driver"); 2637 if (tty_register_driver(&callout_driver)) 2638 panic("Couldn't register callout driver"); 2639 2640 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) { 2641 state->baud_base = get_au1x00_uart_baud_base(); 2642 state->magic = SSTATE_MAGIC; 2643 state->line = i; 2644 state->type = PORT_UNKNOWN; 2645 state->custom_divisor = 0; 2646 state->close_delay = 5*HZ/10; 2647 state->closing_wait = 30*HZ; 2648 state->callout_termios = callout_driver.init_termios; 2649 state->normal_termios = serial_driver.init_termios; 2650 state->icount.cts = state->icount.dsr = 2651 state->icount.rng = state->icount.dcd = 0; 2652 state->icount.rx = state->icount.tx = 0; 2653 state->icount.frame = state->icount.parity = 0; 2654 state->icount.overrun = state->icount.brk = 0; 2655 state->irq = irq_cannonicalize(state->irq); 2656 if (state->hub6) 2657 state->io_type = SERIAL_IO_HUB6; 2658 if (state->port && check_region(state->port,8)) { 2659 continue; 2660 } 2661 2662 if (state->flags & ASYNC_BOOT_AUTOCONF) { 2663 autoconfig(state); 2664 } 2665 } 2666 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) { 2667 if (state->type == PORT_UNKNOWN) { 2668 continue; 2669 } 2670 printk(KERN_INFO "ttyS%02d%s at 0x%04lx (irq = %d) is a %s\n", 2671 state->line + SERIAL_DEV_OFFSET, 2672 (state->flags & ASYNC_FOURPORT) ? " FourPort" : "", 2673 state->port, state->irq, 2674 uart_config[state->type].name); 2675 tty_register_devfs(&serial_driver, 0, 2676 serial_driver.minor_start + state->line); 2677 tty_register_devfs(&callout_driver, 0, 2678 callout_driver.minor_start + state->line); 2679 } 2680 return 0; 2681} 2682 2683/* 2684 * register_serial and unregister_serial allows for 16x50 serial ports to be 2685 * configured at run-time, to support PCMCIA modems. 2686 */ 2687 2688/** 2689 * register_serial - configure a 16x50 serial port at runtime 2690 * @req: request structure 2691 * 2692 * Configure the serial port specified by the request. If the 2693 * port exists and is in use an error is returned. If the port 2694 * is not currently in the table it is added. 2695 * 2696 * The port is then probed and if neccessary the IRQ is autodetected 2697 * If this fails an error is returned. 2698 * 2699 * On success the port is ready to use and the line number is returned. 2700 */ 2701 2702int register_serial(struct serial_struct *req) 2703{ 2704 int i; 2705 unsigned long flags; 2706 struct serial_state *state; 2707 struct async_struct *info; 2708 unsigned long port; 2709 2710 port = req->port; 2711 if (HIGH_BITS_OFFSET) 2712 port += (unsigned long) req->port_high << HIGH_BITS_OFFSET; 2713 2714 spin_lock_irqsave(&serial_lock, flags); 2715 for (i = 0; i < NR_PORTS; i++) { 2716 if ((rs_table[i].port == port) && 2717 (rs_table[i].iomem_base == req->iomem_base)) 2718 break; 2719 } 2720 if (i == NR_PORTS) { 2721 for (i = 0; i < NR_PORTS; i++) 2722 if ((rs_table[i].type == PORT_UNKNOWN) && 2723 (rs_table[i].count == 0)) 2724 break; 2725 } 2726 if (i == NR_PORTS) { 2727 spin_unlock_irqrestore(&serial_lock, flags); 2728 return -1; 2729 } 2730 state = &rs_table[i]; 2731 if (rs_table[i].count) { 2732 spin_unlock_irqrestore(&serial_lock, flags); 2733 printk("Couldn't configure serial #%d (port=%ld,irq=%d): " 2734 "device already open\n", i, port, req->irq); 2735 return -1; 2736 } 2737 state->irq = req->irq; 2738 state->port = port; 2739 state->flags = req->flags; 2740 state->io_type = req->io_type; 2741 state->iomem_base = req->iomem_base; 2742 state->iomem_reg_shift = req->iomem_reg_shift; 2743 if (req->baud_base) 2744 state->baud_base = req->baud_base; 2745 if ((info = state->info) != NULL) { 2746 info->port = port; 2747 info->flags = req->flags; 2748 info->io_type = req->io_type; 2749 info->iomem_base = req->iomem_base; 2750 info->iomem_reg_shift = req->iomem_reg_shift; 2751 } 2752 autoconfig(state); 2753 if (state->type == PORT_UNKNOWN) { 2754 spin_unlock_irqrestore(&serial_lock, flags); 2755 printk("register_serial(): autoconfig failed\n"); 2756 return -1; 2757 } 2758 spin_unlock_irqrestore(&serial_lock, flags); 2759 2760 printk(KERN_INFO "ttyS%02d at %s 0x%04lx (irq = %d) is a %s\n", 2761 state->line + SERIAL_DEV_OFFSET, 2762 state->iomem_base ? "iomem" : "port", 2763 state->iomem_base ? (unsigned long)state->iomem_base : 2764 state->port, state->irq, uart_config[state->type].name); 2765 tty_register_devfs(&serial_driver, 0, 2766 serial_driver.minor_start + state->line); 2767 tty_register_devfs(&callout_driver, 0, 2768 callout_driver.minor_start + state->line); 2769 return state->line + SERIAL_DEV_OFFSET; 2770} 2771 2772/** 2773 * unregister_serial - deconfigure a 16x50 serial port 2774 * @line: line to deconfigure 2775 * 2776 * The port specified is deconfigured and its resources are freed. Any 2777 * user of the port is disconnected as if carrier was dropped. Line is 2778 * the port number returned by register_serial(). 2779 */ 2780 2781void unregister_serial(int line) 2782{ 2783 unsigned long flags; 2784 struct serial_state *state = &rs_table[line]; 2785 2786 spin_lock_irqsave(&serial_lock, flags); 2787 if (state->info && state->info->tty) 2788 tty_hangup(state->info->tty); 2789 state->type = PORT_UNKNOWN; 2790 printk(KERN_INFO "tty%02d unloaded\n", state->line); 2791 /* These will be hidden, because they are devices that will no longer 2792 * be available to the system. (ie, PCMCIA modems, once ejected) 2793 */ 2794 tty_unregister_devfs(&serial_driver, 2795 serial_driver.minor_start + state->line); 2796 tty_unregister_devfs(&callout_driver, 2797 callout_driver.minor_start + state->line); 2798 spin_unlock_irqrestore(&serial_lock, flags); 2799} 2800 2801static void __exit rs_fini(void) 2802{ 2803 unsigned long flags; 2804 int e1, e2; 2805 int i; 2806 struct async_struct *info; 2807 2808 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */ 2809 del_timer_sync(&serial_timer); 2810 spin_lock_irqsave(&serial_lock, flags); 2811 remove_bh(SERIAL_BH); 2812 if ((e1 = tty_unregister_driver(&serial_driver))) 2813 printk("serial: failed to unregister serial driver (%d)\n", 2814 e1); 2815 if ((e2 = tty_unregister_driver(&callout_driver))) 2816 printk("serial: failed to unregister callout driver (%d)\n", 2817 e2); 2818 spin_unlock_irqrestore(&serial_lock, flags); 2819 2820 for (i = 0; i < NR_PORTS; i++) { 2821 if ((info = rs_table[i].info)) { 2822 rs_table[i].info = NULL; 2823 kfree(info); 2824 } 2825 if ((rs_table[i].type != PORT_UNKNOWN) && rs_table[i].port) { 2826 release_region(rs_table[i].port, 8); 2827 } 2828 } 2829 if (tmp_buf) { 2830 unsigned long pg = (unsigned long) tmp_buf; 2831 tmp_buf = NULL; 2832 free_page(pg); 2833 } 2834} 2835 2836module_init(rs_init); 2837module_exit(rs_fini); 2838MODULE_DESCRIPTION("Au1x00 serial driver"); 2839 2840 2841/* 2842 * ------------------------------------------------------------ 2843 * Serial console driver 2844 * ------------------------------------------------------------ 2845 */ 2846#ifdef CONFIG_AU1X00_SERIAL_CONSOLE 2847 2848#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE) 2849 2850static struct async_struct async_sercons; 2851 2852/* 2853 * Wait for transmitter & holding register to empty 2854 */ 2855static inline void wait_for_xmitr(struct async_struct *info) 2856{ 2857 unsigned int status, tmout = 0xffffff; 2858 2859 do { 2860 status = serial_in(info, UART_LSR); 2861 2862 if (status & UART_LSR_BI) 2863 lsr_break_flag = UART_LSR_BI; 2864 2865 if (--tmout == 0) 2866 break; 2867 } while((status & BOTH_EMPTY) != BOTH_EMPTY); 2868} 2869 2870 2871/* 2872 * Print a string to the serial port trying not to disturb 2873 * any possible real use of the port... 2874 * 2875 * The console_lock must be held when we get here. 2876 */ 2877static void serial_console_write(struct console *co, const char *s, 2878 unsigned count) 2879{ 2880 static struct async_struct *info = &async_sercons; 2881 int ier; 2882 unsigned i; 2883 2884 /* 2885 * First save the IER then disable the interrupts 2886 */ 2887 ier = serial_in(info, UART_IER); 2888 serial_out(info, UART_IER, 0x00); 2889 2890 /* 2891 * Now, do each character 2892 */ 2893 for (i = 0; i < count; i++, s++) { 2894 wait_for_xmitr(info); 2895 2896 /* 2897 * Send the character out. 2898 * If a LF, also do CR... 2899 */ 2900 serial_out(info, UART_TX, *s); 2901 if (*s == 10) { 2902 wait_for_xmitr(info); 2903 serial_out(info, UART_TX, 13); 2904 } 2905 } 2906 2907 /* 2908 * Finally, Wait for transmitter & holding register to empty 2909 * and restore the IER 2910 */ 2911 wait_for_xmitr(info); 2912 serial_out(info, UART_IER, ier); 2913} 2914 2915/* 2916 * Receive character from the serial port 2917 */ 2918static int serial_console_wait_key(struct console *co) 2919{ 2920 static struct async_struct *info; 2921 int ier, c; 2922 2923 info = &async_sercons; 2924 2925 /* 2926 * First save the IER then disable the interrupts so 2927 * that the real driver for the port does not get the 2928 * character. 2929 */ 2930 ier = serial_in(info, UART_IER); 2931 serial_out(info, UART_IER, 0x00); 2932 2933 while ((serial_in(info, UART_LSR) & UART_LSR_DR) == 0); 2934 c = serial_in(info, UART_RX); 2935 2936 /* 2937 * Restore the interrupts 2938 */ 2939 serial_out(info, UART_IER, ier); 2940 2941 return c; 2942} 2943 2944static kdev_t serial_console_device(struct console *c) 2945{ 2946 return MKDEV(TTY_MAJOR, 64 + c->index); 2947} 2948 2949/* 2950 * Setup initial baud/bits/parity. We do two things here: 2951 * - construct a cflag setting for the first rs_open() 2952 * - initialize the serial port 2953 * Return non-zero if we didn't find a serial port. 2954 */ 2955static int __init serial_console_setup(struct console *co, char *options) 2956{ 2957 static struct async_struct *info; 2958 struct serial_state *state; 2959 unsigned cval; 2960 int baud = 9600; 2961 int bits = 8; 2962 int parity = 'n'; 2963 int cflag = CREAD | HUPCL | CLOCAL; 2964 int quot = 0; 2965 char *s; 2966 2967 if (options) { 2968 baud = simple_strtoul(options, NULL, 10); 2969 s = options; 2970 while(*s >= '0' && *s <= '9') 2971 s++; 2972 if (*s) parity = *s++; 2973 if (*s) bits = *s - '0'; 2974 } 2975 2976 /* 2977 * Now construct a cflag setting. 2978 */ 2979 switch(baud) { 2980 case 1200: 2981 cflag |= B1200; 2982 break; 2983 case 2400: 2984 cflag |= B2400; 2985 break; 2986 case 4800: 2987 cflag |= B4800; 2988 break; 2989 case 19200: 2990 cflag |= B19200; 2991 break; 2992 case 38400: 2993 cflag |= B38400; 2994 break; 2995 case 57600: 2996 cflag |= B57600; 2997 break; 2998 case 115200: 2999 cflag |= B115200; 3000 break; 3001 case 9600: 3002 default: 3003 cflag |= B9600; 3004 break; 3005 } 3006 switch(bits) { 3007 case 7: 3008 cflag |= CS7; 3009 break; 3010 default: 3011 case 8: 3012 cflag |= CS8; 3013 break; 3014 } 3015 switch(parity) { 3016 case 'o': case 'O': 3017 cflag |= PARODD; 3018 break; 3019 case 'e': case 'E': 3020 cflag |= PARENB; 3021 break; 3022 } 3023 co->cflag = cflag; 3024 3025 /* 3026 * Divisor, bytesize and parity 3027 */ 3028 state = rs_table + co->index; 3029 info = &async_sercons; 3030 info->magic = SERIAL_MAGIC; 3031 info->state = state; 3032 info->port = state->port; 3033 info->flags = state->flags; 3034 info->io_type = state->io_type; 3035 info->iomem_base = state->iomem_base; 3036 info->iomem_reg_shift = state->iomem_reg_shift; 3037 state->baud_base = get_au1x00_uart_baud_base(); 3038 quot = state->baud_base / baud; 3039 3040 cval = cflag & (CSIZE | CSTOPB); 3041 cval >>= 4; 3042 if (cflag & PARENB) 3043 cval |= UART_LCR_PARITY; 3044 if (!(cflag & PARODD)) 3045 cval |= UART_LCR_EPAR; 3046 3047 /* 3048 * Disable UART interrupts, set DTR and RTS high 3049 * and set speed. 3050 */ 3051 serial_out(info, UART_CLK, quot & 0xffff); 3052 serial_out(info, UART_IER, 0); 3053 serial_out(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS); 3054 3055 /* 3056 * If we read 0xff from the LSR, there is no UART here. 3057 */ 3058 if (serial_in(info, UART_LSR) == 0xff) 3059 return -1; 3060 3061 return 0; 3062} 3063 3064static struct console sercons = { 3065 .name = "ttyS", 3066 .write = serial_console_write, 3067 .device = serial_console_device, 3068 .setup = serial_console_setup, 3069 .flags = CON_PRINTBUFFER, 3070 .index = -1, 3071}; 3072 3073/* 3074 * Register console. 3075 */ 3076void __init au1x00_serial_console_init(void) 3077{ 3078 register_console(&sercons); 3079} 3080#endif 3081 3082/* 3083 Local variables: 3084 compile-command: "gcc -D__KERNEL__ -I../../include -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -fno-strict-aliasing -pipe -fno-strength-reduce -march=i586 -DMODULE -DMODVERSIONS -include ../../include/linux/modversions.h -DEXPORT_SYMTAB -c serial.c" 3085 End: 3086*/ 3087