linux-old/drivers/char/esp.c
<<
>>
Prefs
   1/*
   2 *  esp.c - driver for Hayes ESP serial cards
   3 *
   4 *  --- Notices from serial.c, upon which this driver is based ---
   5 *
   6 *  Copyright (C) 1991, 1992  Linus Torvalds
   7 *
   8 *  Extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92.  Now
   9 *  much more extensible to support other serial cards based on the
  10 *  16450/16550A UART's.  Added support for the AST FourPort and the
  11 *  Accent Async board.  
  12 *
  13 *  set_serial_info fixed to set the flags, custom divisor, and uart
  14 *      type fields.  Fix suggested by Michael K. Johnson 12/12/92.
  15 *
  16 *  11/95: TIOCMIWAIT, TIOCGICOUNT by Angelo Haritsis <ah@doc.ic.ac.uk>
  17 *
  18 *  03/96: Modularised by Angelo Haritsis <ah@doc.ic.ac.uk>
  19 *
  20 *  rs_set_termios fixed to look also for changes of the input
  21 *      flags INPCK, BRKINT, PARMRK, IGNPAR and IGNBRK.
  22 *                                            Bernd Anhäupl 05/17/96.
  23 *
  24 * --- End of notices from serial.c ---
  25 *
  26 * Support for the ESP serial card by Andrew J. Robinson
  27 *     <arobinso@nyx.net> (Card detection routine taken from a patch
  28 *     by Dennis J. Boylan).  Patches to allow use with 2.1.x contributed
  29 *     by Chris Faylor.
  30 *
  31 * Most recent changes: (Andrew J. Robinson)
  32 *   Support for PIO mode.  This allows the driver to work properly with
  33 *     multiport cards.
  34 *
  35 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> -
  36 * several cleanups, use module_init/module_exit, etc
  37 *
  38 * This module exports the following rs232 io functions:
  39 *
  40 *      int espserial_init(void);
  41 */
  42
  43#include <linux/module.h>
  44#include <linux/errno.h>
  45#include <linux/signal.h>
  46#include <linux/sched.h>
  47#include <linux/interrupt.h>
  48#include <linux/tty.h>
  49#include <linux/tty_flip.h>
  50#include <linux/serial.h>
  51#include <linux/serialP.h>
  52#include <linux/serial_reg.h>
  53#include <linux/major.h>
  54#include <linux/string.h>
  55#include <linux/fcntl.h>
  56#include <linux/ptrace.h>
  57#include <linux/ioport.h>
  58#include <linux/mm.h>
  59#include <linux/init.h>
  60
  61#include <asm/system.h>
  62#include <asm/io.h>
  63#include <asm/segment.h>
  64#include <asm/bitops.h>
  65
  66#include <asm/dma.h>
  67#include <linux/slab.h>
  68#include <asm/uaccess.h>
  69
  70#include <linux/hayesesp.h>
  71
  72#define NR_PORTS 64     /* maximum number of ports */
  73#define NR_PRIMARY 8    /* maximum number of primary ports */
  74
  75/* The following variables can be set by giving module options */
  76static int irq[NR_PRIMARY];     /* IRQ for each base port */
  77static unsigned int divisor[NR_PRIMARY]; /* custom divisor for each port */
  78static unsigned int dma = ESP_DMA_CHANNEL; /* DMA channel */
  79static unsigned int rx_trigger = ESP_RX_TRIGGER;
  80static unsigned int tx_trigger = ESP_TX_TRIGGER;
  81static unsigned int flow_off = ESP_FLOW_OFF;
  82static unsigned int flow_on = ESP_FLOW_ON;
  83static unsigned int rx_timeout = ESP_RX_TMOUT;
  84static unsigned int pio_threshold = ESP_PIO_THRESHOLD;
  85
  86MODULE_LICENSE("GPL");
  87
  88MODULE_PARM(irq, "1-8i");
  89MODULE_PARM(divisor, "1-8i");
  90MODULE_PARM(dma, "i");
  91MODULE_PARM(rx_trigger, "i");
  92MODULE_PARM(tx_trigger, "i");
  93MODULE_PARM(flow_off, "i");
  94MODULE_PARM(flow_on, "i");
  95MODULE_PARM(rx_timeout, "i");
  96MODULE_PARM(pio_threshold, "i");
  97
  98/* END */
  99
 100static char *dma_buffer;
 101static int dma_bytes;
 102static struct esp_pio_buffer *free_pio_buf;
 103
 104#define DMA_BUFFER_SZ 1024
 105
 106#define WAKEUP_CHARS 1024
 107
 108static char serial_name[] __initdata = "ESP serial driver";
 109static char serial_version[] __initdata = "2.2";
 110
 111static DECLARE_TASK_QUEUE(tq_esp);
 112
 113static struct tty_driver esp_driver, esp_callout_driver;
 114static int serial_refcount;
 115
 116/* serial subtype definitions */
 117#define SERIAL_TYPE_NORMAL      1
 118#define SERIAL_TYPE_CALLOUT     2
 119
 120/*
 121 * Serial driver configuration section.  Here are the various options:
 122 *
 123 * SERIAL_PARANOIA_CHECK
 124 *              Check the magic number for the esp_structure where
 125 *              ever possible.
 126 */
 127
 128#undef SERIAL_PARANOIA_CHECK
 129#define SERIAL_DO_RESTART
 130
 131#undef SERIAL_DEBUG_INTR
 132#undef SERIAL_DEBUG_OPEN
 133#undef SERIAL_DEBUG_FLOW
 134
 135#define _INLINE_ inline
 136  
 137#if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
 138#define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
 139 kdevname(tty->device), (info->flags), serial_refcount,info->count,tty->count,s)
 140#else
 141#define DBG_CNT(s)
 142#endif
 143
 144static struct esp_struct *ports;
 145
 146static void change_speed(struct esp_struct *info);
 147static void rs_wait_until_sent(struct tty_struct *, int);
 148        
 149/*
 150 * The ESP card has a clock rate of 14.7456 MHz (that is, 2**ESPC_SCALE
 151 * times the normal 1.8432 Mhz clock of most serial boards).
 152 */
 153#define BASE_BAUD ((1843200 / 16) * (1 << ESPC_SCALE))
 154
 155/* Standard COM flags (except for COM4, because of the 8514 problem) */
 156#define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
 157
 158static struct tty_struct *serial_table[NR_PORTS];
 159static struct termios *serial_termios[NR_PORTS];
 160static struct termios *serial_termios_locked[NR_PORTS];
 161
 162#ifndef MIN
 163#define MIN(a,b)        ((a) < (b) ? (a) : (b))
 164#endif
 165
 166/*
 167 * tmp_buf is used as a temporary buffer by serial_write.  We need to
 168 * lock it in case the memcpy_fromfs blocks while swapping in a page,
 169 * and some other program tries to do a serial write at the same time.
 170 * Since the lock will only come under contention when the system is
 171 * swapping and available memory is low, it makes sense to share one
 172 * buffer across all the serial ports, since it significantly saves
 173 * memory if large numbers of serial ports are open.
 174 */
 175static unsigned char *tmp_buf;
 176static DECLARE_MUTEX(tmp_buf_sem);
 177
 178static inline int serial_paranoia_check(struct esp_struct *info,
 179                                        kdev_t device, const char *routine)
 180{
 181#ifdef SERIAL_PARANOIA_CHECK
 182        static const char badmagic[] = KERN_WARNING
 183                "Warning: bad magic number for serial struct (%s) in %s\n";
 184        static const char badinfo[] = KERN_WARNING
 185                "Warning: null esp_struct for (%s) in %s\n";
 186
 187        if (!info) {
 188                printk(badinfo, kdevname(device), routine);
 189                return 1;
 190        }
 191        if (info->magic != ESP_MAGIC) {
 192                printk(badmagic, kdevname(device), routine);
 193                return 1;
 194        }
 195#endif
 196        return 0;
 197}
 198
 199static inline unsigned int serial_in(struct esp_struct *info, int offset)
 200{
 201        return inb(info->port + offset);
 202}
 203
 204static inline void serial_out(struct esp_struct *info, int offset,
 205                              unsigned char value)
 206{
 207        outb(value, info->port+offset);
 208}
 209
 210/*
 211 * ------------------------------------------------------------
 212 * rs_stop() and rs_start()
 213 *
 214 * This routines are called before setting or resetting tty->stopped.
 215 * They enable or disable transmitter interrupts, as necessary.
 216 * ------------------------------------------------------------
 217 */
 218static void rs_stop(struct tty_struct *tty)
 219{
 220        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
 221        unsigned long flags;
 222
 223        if (serial_paranoia_check(info, tty->device, "rs_stop"))
 224                return;
 225        
 226        save_flags(flags); cli();
 227        if (info->IER & UART_IER_THRI) {
 228                info->IER &= ~UART_IER_THRI;
 229                serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
 230                serial_out(info, UART_ESI_CMD2, info->IER);
 231        }
 232
 233        restore_flags(flags);
 234}
 235
 236static void rs_start(struct tty_struct *tty)
 237{
 238        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
 239        unsigned long flags;
 240        
 241        if (serial_paranoia_check(info, tty->device, "rs_start"))
 242                return;
 243        
 244        save_flags(flags); cli();
 245        if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
 246                info->IER |= UART_IER_THRI;
 247                serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
 248                serial_out(info, UART_ESI_CMD2, info->IER);
 249        }
 250        restore_flags(flags);
 251}
 252
 253/*
 254 * ----------------------------------------------------------------------
 255 *
 256 * Here starts the interrupt handling routines.  All of the following
 257 * subroutines are declared as inline and are folded into
 258 * rs_interrupt().  They were separated out for readability's sake.
 259 *
 260 * Note: rs_interrupt() is a "fast" interrupt, which means that it
 261 * runs with interrupts turned off.  People who may want to modify
 262 * rs_interrupt() should try to keep the interrupt handler as fast as
 263 * possible.  After you are done making modifications, it is not a bad
 264 * idea to do:
 265 * 
 266 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
 267 *
 268 * and look at the resulting assemble code in serial.s.
 269 *
 270 *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
 271 * -----------------------------------------------------------------------
 272 */
 273
 274/*
 275 * This routine is used by the interrupt handler to schedule
 276 * processing in the software interrupt portion of the driver.
 277 */
 278static _INLINE_ void rs_sched_event(struct esp_struct *info,
 279                                  int event)
 280{
 281        info->event |= 1 << event;
 282        queue_task(&info->tqueue, &tq_esp);
 283        mark_bh(ESP_BH);
 284}
 285static _INLINE_ struct esp_pio_buffer *get_pio_buffer(void)
 286{
 287        struct esp_pio_buffer *buf;
 288
 289        if (free_pio_buf) {
 290                buf = free_pio_buf;
 291                free_pio_buf = buf->next;
 292        } else {
 293                buf = kmalloc(sizeof(struct esp_pio_buffer), GFP_ATOMIC);
 294        }
 295
 296        return buf;
 297}
 298
 299static _INLINE_ void release_pio_buffer(struct esp_pio_buffer *buf)
 300{
 301        buf->next = free_pio_buf;
 302        free_pio_buf = buf;
 303}
 304
 305static _INLINE_ void receive_chars_pio(struct esp_struct *info, int num_bytes)
 306{
 307        struct tty_struct *tty = info->tty;
 308        int i;
 309        struct esp_pio_buffer *pio_buf;
 310        struct esp_pio_buffer *err_buf;
 311        unsigned char status_mask;
 312
 313        pio_buf = get_pio_buffer();
 314
 315        if (!pio_buf)
 316                return;
 317
 318        err_buf = get_pio_buffer();
 319
 320        if (!err_buf) {
 321                release_pio_buffer(pio_buf);
 322                return;
 323        }
 324
 325        sti();
 326        
 327        status_mask = (info->read_status_mask >> 2) & 0x07;
 328                
 329        for (i = 0; i < num_bytes - 1; i += 2) {
 330                *((unsigned short *)(pio_buf->data + i)) =
 331                        inw(info->port + UART_ESI_RX);
 332                err_buf->data[i] = serial_in(info, UART_ESI_RWS);
 333                err_buf->data[i + 1] = (err_buf->data[i] >> 3) & status_mask;
 334                err_buf->data[i] &= status_mask;
 335        }
 336
 337        if (num_bytes & 0x0001) {
 338                pio_buf->data[num_bytes - 1] = serial_in(info, UART_ESI_RX);
 339                err_buf->data[num_bytes - 1] =
 340                        (serial_in(info, UART_ESI_RWS) >> 3) & status_mask;
 341        }
 342
 343        cli();
 344
 345        /* make sure everything is still ok since interrupts were enabled */
 346        tty = info->tty;
 347
 348        if (!tty) {
 349                release_pio_buffer(pio_buf);
 350                release_pio_buffer(err_buf);
 351                info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
 352                return;
 353        }
 354
 355        status_mask = (info->ignore_status_mask >> 2) & 0x07;
 356
 357        for (i = 0; i < num_bytes; i++) {
 358                if (!(err_buf->data[i] & status_mask)) {
 359                        *(tty->flip.char_buf_ptr++) = pio_buf->data[i];
 360
 361                        if (err_buf->data[i] & 0x04) {
 362                                *(tty->flip.flag_buf_ptr++) = TTY_BREAK;
 363
 364                                if (info->flags & ASYNC_SAK)
 365                                        do_SAK(tty);
 366                        }
 367                        else if (err_buf->data[i] & 0x02)
 368                                *(tty->flip.flag_buf_ptr++) = TTY_FRAME;
 369                        else if (err_buf->data[i] & 0x01)
 370                                *(tty->flip.flag_buf_ptr++) = TTY_PARITY;
 371                        else
 372                                *(tty->flip.flag_buf_ptr++) = 0;
 373                
 374                        tty->flip.count++;
 375                }
 376        }
 377
 378        queue_task(&tty->flip.tqueue, &tq_timer);
 379
 380        info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
 381        release_pio_buffer(pio_buf);
 382        release_pio_buffer(err_buf);
 383}
 384
 385static _INLINE_ void receive_chars_dma(struct esp_struct *info, int num_bytes)
 386{
 387        unsigned long flags;
 388        info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
 389        dma_bytes = num_bytes;
 390        info->stat_flags |= ESP_STAT_DMA_RX;
 391        
 392        flags=claim_dma_lock();
 393        disable_dma(dma);
 394        clear_dma_ff(dma);
 395        set_dma_mode(dma, DMA_MODE_READ);
 396        set_dma_addr(dma, virt_to_bus(dma_buffer));
 397        set_dma_count(dma, dma_bytes);
 398        enable_dma(dma);
 399        release_dma_lock(flags);
 400        
 401        serial_out(info, UART_ESI_CMD1, ESI_START_DMA_RX);
 402}
 403
 404static _INLINE_ void receive_chars_dma_done(struct esp_struct *info,
 405                                            int status)
 406{
 407        struct tty_struct *tty = info->tty;
 408        int num_bytes;
 409        unsigned long flags;
 410        
 411        
 412        flags=claim_dma_lock();
 413        disable_dma(dma);
 414        clear_dma_ff(dma);
 415
 416        info->stat_flags &= ~ESP_STAT_DMA_RX;
 417        num_bytes = dma_bytes - get_dma_residue(dma);
 418        release_dma_lock(flags);
 419        
 420        info->icount.rx += num_bytes;
 421
 422        memcpy(tty->flip.char_buf_ptr, dma_buffer, num_bytes);
 423        tty->flip.char_buf_ptr += num_bytes;
 424        tty->flip.count += num_bytes;
 425        memset(tty->flip.flag_buf_ptr, 0, num_bytes);
 426        tty->flip.flag_buf_ptr += num_bytes;
 427
 428        if (num_bytes > 0) {
 429                tty->flip.flag_buf_ptr--;
 430
 431                status &= (0x1c & info->read_status_mask);
 432
 433                if (status & info->ignore_status_mask) {
 434                        tty->flip.count--;
 435                        tty->flip.char_buf_ptr--;
 436                        tty->flip.flag_buf_ptr--;
 437                } else if (status & 0x10) {
 438                        *tty->flip.flag_buf_ptr = TTY_BREAK;
 439                        (info->icount.brk)++;
 440                        if (info->flags & ASYNC_SAK)
 441                                do_SAK(tty);
 442                } else if (status & 0x08) {
 443                        *tty->flip.flag_buf_ptr = TTY_FRAME;
 444                        (info->icount.frame)++;
 445                }
 446                else if (status & 0x04) {
 447                        *tty->flip.flag_buf_ptr = TTY_PARITY;
 448                        (info->icount.parity)++;
 449                }
 450
 451                tty->flip.flag_buf_ptr++;
 452                
 453                queue_task(&tty->flip.tqueue, &tq_timer);
 454        }
 455
 456        if (dma_bytes != num_bytes) {
 457                num_bytes = dma_bytes - num_bytes;
 458                dma_bytes = 0;
 459                receive_chars_dma(info, num_bytes);
 460        } else
 461                dma_bytes = 0;
 462}
 463
 464static _INLINE_ void transmit_chars_pio(struct esp_struct *info,
 465                                        int space_avail)
 466{
 467        int i;
 468        struct esp_pio_buffer *pio_buf;
 469
 470        pio_buf = get_pio_buffer();
 471
 472        if (!pio_buf)
 473                return;
 474
 475        while (space_avail && info->xmit_cnt) {
 476                if (info->xmit_tail + space_avail <= ESP_XMIT_SIZE) {
 477                        memcpy(pio_buf->data,
 478                               &(info->xmit_buf[info->xmit_tail]),
 479                               space_avail);
 480                } else {
 481                        i = ESP_XMIT_SIZE - info->xmit_tail;
 482                        memcpy(pio_buf->data,
 483                               &(info->xmit_buf[info->xmit_tail]), i);
 484                        memcpy(&(pio_buf->data[i]), info->xmit_buf,
 485                               space_avail - i);
 486                }
 487
 488                info->xmit_cnt -= space_avail;
 489                info->xmit_tail = (info->xmit_tail + space_avail) &
 490                        (ESP_XMIT_SIZE - 1);
 491
 492                sti();
 493
 494                for (i = 0; i < space_avail - 1; i += 2) {
 495                        outw(*((unsigned short *)(pio_buf->data + i)),
 496                             info->port + UART_ESI_TX);
 497                }
 498
 499                if (space_avail & 0x0001)
 500                        serial_out(info, UART_ESI_TX,
 501                                   pio_buf->data[space_avail - 1]);
 502
 503                cli();
 504
 505                if (info->xmit_cnt) {
 506                        serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
 507                        serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
 508                        space_avail = serial_in(info, UART_ESI_STAT1) << 8;
 509                        space_avail |= serial_in(info, UART_ESI_STAT2);
 510
 511                        if (space_avail > info->xmit_cnt)
 512                                space_avail = info->xmit_cnt;
 513                }
 514        }
 515
 516        if (info->xmit_cnt < WAKEUP_CHARS) {
 517                rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
 518
 519#ifdef SERIAL_DEBUG_INTR
 520                printk("THRE...");
 521#endif
 522
 523                if (info->xmit_cnt <= 0) {
 524                        info->IER &= ~UART_IER_THRI;
 525                        serial_out(info, UART_ESI_CMD1,
 526                                   ESI_SET_SRV_MASK);
 527                        serial_out(info, UART_ESI_CMD2, info->IER);
 528                }
 529        }
 530
 531        release_pio_buffer(pio_buf);
 532}
 533
 534static _INLINE_ void transmit_chars_dma(struct esp_struct *info, int num_bytes)
 535{
 536        unsigned long flags;
 537        
 538        dma_bytes = num_bytes;
 539
 540        if (info->xmit_tail + dma_bytes <= ESP_XMIT_SIZE) {
 541                memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
 542                       dma_bytes);
 543        } else {
 544                int i = ESP_XMIT_SIZE - info->xmit_tail;
 545                memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
 546                        i);
 547                memcpy(&(dma_buffer[i]), info->xmit_buf, dma_bytes - i);
 548        }
 549
 550        info->xmit_cnt -= dma_bytes;
 551        info->xmit_tail = (info->xmit_tail + dma_bytes) & (ESP_XMIT_SIZE - 1);
 552
 553        if (info->xmit_cnt < WAKEUP_CHARS) {
 554                rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
 555
 556#ifdef SERIAL_DEBUG_INTR
 557                printk("THRE...");
 558#endif
 559
 560                if (info->xmit_cnt <= 0) {
 561                        info->IER &= ~UART_IER_THRI;
 562                        serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
 563                        serial_out(info, UART_ESI_CMD2, info->IER);
 564                }
 565        }
 566
 567        info->stat_flags |= ESP_STAT_DMA_TX;
 568        
 569        flags=claim_dma_lock();
 570        disable_dma(dma);
 571        clear_dma_ff(dma);
 572        set_dma_mode(dma, DMA_MODE_WRITE);
 573        set_dma_addr(dma, virt_to_bus(dma_buffer));
 574        set_dma_count(dma, dma_bytes);
 575        enable_dma(dma);
 576        release_dma_lock(flags);
 577        
 578        serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
 579}
 580
 581static _INLINE_ void transmit_chars_dma_done(struct esp_struct *info)
 582{
 583        int num_bytes;
 584        unsigned long flags;
 585        
 586
 587        flags=claim_dma_lock();
 588        disable_dma(dma);
 589        clear_dma_ff(dma);
 590
 591        num_bytes = dma_bytes - get_dma_residue(dma);
 592        info->icount.tx += dma_bytes;
 593        release_dma_lock(flags);
 594
 595        if (dma_bytes != num_bytes) {
 596                dma_bytes -= num_bytes;
 597                memmove(dma_buffer, dma_buffer + num_bytes, dma_bytes);
 598                
 599                flags=claim_dma_lock();
 600                disable_dma(dma);
 601                clear_dma_ff(dma);
 602                set_dma_mode(dma, DMA_MODE_WRITE);
 603                set_dma_addr(dma, virt_to_bus(dma_buffer));
 604                set_dma_count(dma, dma_bytes);
 605                enable_dma(dma);
 606                release_dma_lock(flags);
 607                
 608                serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
 609        } else {
 610                dma_bytes = 0;
 611                info->stat_flags &= ~ESP_STAT_DMA_TX;
 612        }
 613}
 614
 615static _INLINE_ void check_modem_status(struct esp_struct *info)
 616{
 617        int     status;
 618        
 619        serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
 620        status = serial_in(info, UART_ESI_STAT2);
 621
 622        if (status & UART_MSR_ANY_DELTA) {
 623                /* update input line counters */
 624                if (status & UART_MSR_TERI)
 625                        info->icount.rng++;
 626                if (status & UART_MSR_DDSR)
 627                        info->icount.dsr++;
 628                if (status & UART_MSR_DDCD)
 629                        info->icount.dcd++;
 630                if (status & UART_MSR_DCTS)
 631                        info->icount.cts++;
 632                wake_up_interruptible(&info->delta_msr_wait);
 633        }
 634
 635        if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
 636#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
 637                printk("ttys%d CD now %s...", info->line,
 638                       (status & UART_MSR_DCD) ? "on" : "off");
 639#endif          
 640                if (status & UART_MSR_DCD)
 641                        wake_up_interruptible(&info->open_wait);
 642                else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
 643                           (info->flags & ASYNC_CALLOUT_NOHUP))) {
 644#ifdef SERIAL_DEBUG_OPEN
 645                        printk("scheduling hangup...");
 646#endif
 647                        MOD_INC_USE_COUNT;
 648                        if (schedule_task(&info->tqueue_hangup) == 0)
 649                                MOD_DEC_USE_COUNT;
 650                }
 651        }
 652}
 653
 654/*
 655 * This is the serial driver's interrupt routine
 656 */
 657static void rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs)
 658{
 659        struct esp_struct * info;
 660        unsigned err_status;
 661        unsigned int scratch;
 662
 663#ifdef SERIAL_DEBUG_INTR
 664        printk("rs_interrupt_single(%d)...", irq);
 665#endif
 666        info = (struct esp_struct *)dev_id;
 667        err_status = 0;
 668        scratch = serial_in(info, UART_ESI_SID);
 669
 670        cli();
 671        
 672        if (!info->tty) {
 673                sti();
 674                return;
 675        }
 676
 677        if (scratch & 0x04) { /* error */
 678                serial_out(info, UART_ESI_CMD1, ESI_GET_ERR_STAT);
 679                err_status = serial_in(info, UART_ESI_STAT1);
 680                serial_in(info, UART_ESI_STAT2);
 681
 682                if (err_status & 0x01)
 683                        info->stat_flags |= ESP_STAT_RX_TIMEOUT;
 684
 685                if (err_status & 0x20) /* UART status */
 686                        check_modem_status(info);
 687
 688                if (err_status & 0x80) /* Start break */
 689                        wake_up_interruptible(&info->break_wait);
 690        }
 691                
 692        if ((scratch & 0x88) || /* DMA completed or timed out */
 693            (err_status & 0x1c) /* receive error */) {
 694                if (info->stat_flags & ESP_STAT_DMA_RX)
 695                        receive_chars_dma_done(info, err_status);
 696                else if (info->stat_flags & ESP_STAT_DMA_TX)
 697                        transmit_chars_dma_done(info);
 698        }
 699
 700        if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
 701            ((scratch & 0x01) || (info->stat_flags & ESP_STAT_RX_TIMEOUT)) &&
 702            (info->IER & UART_IER_RDI)) {
 703                int num_bytes;
 704
 705                serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
 706                serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
 707                num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
 708                num_bytes |= serial_in(info, UART_ESI_STAT2);
 709
 710                if (num_bytes > (TTY_FLIPBUF_SIZE - info->tty->flip.count))
 711                  num_bytes = TTY_FLIPBUF_SIZE - info->tty->flip.count;
 712
 713                if (num_bytes) {
 714                        if (dma_bytes ||
 715                            (info->stat_flags & ESP_STAT_USE_PIO) ||
 716                            (num_bytes <= info->config.pio_threshold))
 717                                receive_chars_pio(info, num_bytes);
 718                        else
 719                                receive_chars_dma(info, num_bytes);
 720                }
 721        }
 722        
 723        if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
 724            (scratch & 0x02) && (info->IER & UART_IER_THRI)) {
 725                if ((info->xmit_cnt <= 0) || info->tty->stopped) {
 726                        info->IER &= ~UART_IER_THRI;
 727                        serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
 728                        serial_out(info, UART_ESI_CMD2, info->IER);
 729                } else {
 730                        int num_bytes;
 731
 732                        serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
 733                        serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
 734                        num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
 735                        num_bytes |= serial_in(info, UART_ESI_STAT2);
 736
 737                        if (num_bytes > info->xmit_cnt)
 738                                num_bytes = info->xmit_cnt;
 739
 740                        if (num_bytes) {
 741                                if (dma_bytes ||
 742                                    (info->stat_flags & ESP_STAT_USE_PIO) ||
 743                                    (num_bytes <= info->config.pio_threshold))
 744                                        transmit_chars_pio(info, num_bytes);
 745                                else
 746                                        transmit_chars_dma(info, num_bytes);
 747                        }
 748                }
 749        }
 750
 751        info->last_active = jiffies;
 752
 753#ifdef SERIAL_DEBUG_INTR
 754        printk("end.\n");
 755#endif
 756        sti();
 757}
 758
 759/*
 760 * -------------------------------------------------------------------
 761 * Here ends the serial interrupt routines.
 762 * -------------------------------------------------------------------
 763 */
 764
 765/*
 766 * This routine is used to handle the "bottom half" processing for the
 767 * serial driver, known also the "software interrupt" processing.
 768 * This processing is done at the kernel interrupt level, after the
 769 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
 770 * is where time-consuming activities which can not be done in the
 771 * interrupt driver proper are done; the interrupt driver schedules
 772 * them using rs_sched_event(), and they get done here.
 773 */
 774static void do_serial_bh(void)
 775{
 776        run_task_queue(&tq_esp);
 777}
 778
 779static void do_softint(void *private_)
 780{
 781        struct esp_struct       *info = (struct esp_struct *) private_;
 782        struct tty_struct       *tty;
 783        
 784        tty = info->tty;
 785        if (!tty)
 786                return;
 787
 788        if (test_and_clear_bit(ESP_EVENT_WRITE_WAKEUP, &info->event)) {
 789                tty_wakeup(tty);
 790        }
 791}
 792
 793/*
 794 * This routine is called from the scheduler tqueue when the interrupt
 795 * routine has signalled that a hangup has occurred.  The path of
 796 * hangup processing is:
 797 *
 798 *      serial interrupt routine -> (scheduler tqueue) ->
 799 *      do_serial_hangup() -> tty->hangup() -> esp_hangup()
 800 * 
 801 */
 802static void do_serial_hangup(void *private_)
 803{
 804        struct esp_struct       *info = (struct esp_struct *) private_;
 805        struct tty_struct       *tty;
 806        
 807        tty = info->tty;
 808        if (tty)
 809                tty_hangup(tty);
 810        MOD_DEC_USE_COUNT;
 811}
 812
 813/*
 814 * ---------------------------------------------------------------
 815 * Low level utility subroutines for the serial driver:  routines to
 816 * figure out the appropriate timeout for an interrupt chain, routines
 817 * to initialize and startup a serial port, and routines to shutdown a
 818 * serial port.  Useful stuff like that.
 819 * ---------------------------------------------------------------
 820 */
 821
 822static _INLINE_ void esp_basic_init(struct esp_struct * info)
 823{
 824        /* put ESPC in enhanced mode */
 825        serial_out(info, UART_ESI_CMD1, ESI_SET_MODE);
 826        
 827        if (info->stat_flags & ESP_STAT_NEVER_DMA)
 828                serial_out(info, UART_ESI_CMD2, 0x01);
 829        else
 830                serial_out(info, UART_ESI_CMD2, 0x31);
 831
 832        /* disable interrupts for now */
 833        serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
 834        serial_out(info, UART_ESI_CMD2, 0x00);
 835
 836        /* set interrupt and DMA channel */
 837        serial_out(info, UART_ESI_CMD1, ESI_SET_IRQ);
 838
 839        if (info->stat_flags & ESP_STAT_NEVER_DMA)
 840                serial_out(info, UART_ESI_CMD2, 0x01);
 841        else
 842                serial_out(info, UART_ESI_CMD2, (dma << 4) | 0x01);
 843
 844        serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
 845
 846        if (info->line % 8)     /* secondary port */
 847                serial_out(info, UART_ESI_CMD2, 0x0d);  /* shared */
 848        else if (info->irq == 9)
 849                serial_out(info, UART_ESI_CMD2, 0x02);
 850        else
 851                serial_out(info, UART_ESI_CMD2, info->irq);
 852
 853        /* set error status mask (check this) */
 854        serial_out(info, UART_ESI_CMD1, ESI_SET_ERR_MASK);
 855
 856        if (info->stat_flags & ESP_STAT_NEVER_DMA)
 857                serial_out(info, UART_ESI_CMD2, 0xa1);
 858        else
 859                serial_out(info, UART_ESI_CMD2, 0xbd);
 860
 861        serial_out(info, UART_ESI_CMD2, 0x00);
 862
 863        /* set DMA timeout */
 864        serial_out(info, UART_ESI_CMD1, ESI_SET_DMA_TMOUT);
 865        serial_out(info, UART_ESI_CMD2, 0xff);
 866
 867        /* set FIFO trigger levels */
 868        serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
 869        serial_out(info, UART_ESI_CMD2, info->config.rx_trigger >> 8);
 870        serial_out(info, UART_ESI_CMD2, info->config.rx_trigger);
 871        serial_out(info, UART_ESI_CMD2, info->config.tx_trigger >> 8);
 872        serial_out(info, UART_ESI_CMD2, info->config.tx_trigger);
 873
 874        /* Set clock scaling and wait states */
 875        serial_out(info, UART_ESI_CMD1, ESI_SET_PRESCALAR);
 876        serial_out(info, UART_ESI_CMD2, 0x04 | ESPC_SCALE);
 877
 878        /* set reinterrupt pacing */
 879        serial_out(info, UART_ESI_CMD1, ESI_SET_REINTR);
 880        serial_out(info, UART_ESI_CMD2, 0xff);
 881}
 882
 883static int startup(struct esp_struct * info)
 884{
 885        unsigned long flags;
 886        int     retval=0;
 887        unsigned int num_chars;
 888
 889        save_flags(flags); cli();
 890
 891        if (info->flags & ASYNC_INITIALIZED)
 892                goto out;
 893
 894        if (!info->xmit_buf) {
 895                info->xmit_buf = (unsigned char *)get_free_page(GFP_KERNEL);
 896                retval = -ENOMEM;
 897                if (!info->xmit_buf)
 898                        goto out;
 899        }
 900
 901#ifdef SERIAL_DEBUG_OPEN
 902        printk("starting up ttys%d (irq %d)...", info->line, info->irq);
 903#endif
 904
 905        /* Flush the RX buffer.  Using the ESI flush command may cause */
 906        /* wild interrupts, so read all the data instead. */
 907
 908        serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
 909        serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
 910        num_chars = serial_in(info, UART_ESI_STAT1) << 8;
 911        num_chars |= serial_in(info, UART_ESI_STAT2);
 912
 913        while (num_chars > 1) {
 914                inw(info->port + UART_ESI_RX);
 915                num_chars -= 2;
 916        }
 917
 918        if (num_chars)
 919                serial_in(info, UART_ESI_RX);
 920
 921        /* set receive character timeout */
 922        serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
 923        serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
 924
 925        /* clear all flags except the "never DMA" flag */
 926        info->stat_flags &= ESP_STAT_NEVER_DMA;
 927
 928        if (info->stat_flags & ESP_STAT_NEVER_DMA)
 929                info->stat_flags |= ESP_STAT_USE_PIO;
 930
 931        /*
 932         * Allocate the IRQ
 933         */
 934
 935        retval = request_irq(info->irq, rs_interrupt_single, SA_SHIRQ,
 936                             "esp serial", info);
 937
 938        if (retval) {
 939                if (capable(CAP_SYS_ADMIN)) {
 940                        if (info->tty)
 941                                set_bit(TTY_IO_ERROR,
 942                                        &info->tty->flags);
 943                        retval = 0;
 944                }
 945                goto out;
 946        }
 947
 948        if (!(info->stat_flags & ESP_STAT_USE_PIO) && !dma_buffer) {
 949                dma_buffer = (char *)__get_dma_pages(
 950                        GFP_KERNEL, get_order(DMA_BUFFER_SZ));
 951
 952                /* use PIO mode if DMA buf/chan cannot be allocated */
 953                if (!dma_buffer)
 954                        info->stat_flags |= ESP_STAT_USE_PIO;
 955                else if (request_dma(dma, "esp serial")) {
 956                        free_pages((unsigned long)dma_buffer,
 957                                   get_order(DMA_BUFFER_SZ));
 958                        dma_buffer = 0;
 959                        info->stat_flags |= ESP_STAT_USE_PIO;
 960                }
 961                        
 962        }
 963
 964        info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
 965        serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
 966        serial_out(info, UART_ESI_CMD2, UART_MCR);
 967        serial_out(info, UART_ESI_CMD2, info->MCR);
 968        
 969        /*
 970         * Finally, enable interrupts
 971         */
 972        /* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
 973        info->IER = UART_IER_RLSI | UART_IER_RDI | UART_IER_DMA_TMOUT |
 974                        UART_IER_DMA_TC;
 975        serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
 976        serial_out(info, UART_ESI_CMD2, info->IER);
 977        
 978        if (info->tty)
 979                clear_bit(TTY_IO_ERROR, &info->tty->flags);
 980        info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
 981
 982        /*
 983         * Set up the tty->alt_speed kludge
 984         */
 985        if (info->tty) {
 986                if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
 987                        info->tty->alt_speed = 57600;
 988                if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
 989                        info->tty->alt_speed = 115200;
 990                if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
 991                        info->tty->alt_speed = 230400;
 992                if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
 993                        info->tty->alt_speed = 460800;
 994        }
 995        
 996        /*
 997         * set the speed of the serial port
 998         */
 999        change_speed(info);
1000
1001        info->flags |= ASYNC_INITIALIZED;
1002        retval = 0;
1003out:    restore_flags(flags);
1004        return retval;
1005}
1006
1007/*
1008 * This routine will shutdown a serial port; interrupts are disabled, and
1009 * DTR is dropped if the hangup on close termio flag is on.
1010 */
1011static void shutdown(struct esp_struct * info)
1012{
1013        unsigned long   flags, f;
1014
1015        if (!(info->flags & ASYNC_INITIALIZED))
1016                return;
1017
1018#ifdef SERIAL_DEBUG_OPEN
1019        printk("Shutting down serial port %d (irq %d)....", info->line,
1020               info->irq);
1021#endif
1022        
1023        save_flags(flags); cli(); /* Disable interrupts */
1024
1025        /*
1026         * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1027         * here so the queue might never be waken up
1028         */
1029        wake_up_interruptible(&info->delta_msr_wait);
1030        wake_up_interruptible(&info->break_wait);
1031
1032        /* stop a DMA transfer on the port being closed */
1033           
1034        if (info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) {
1035                f=claim_dma_lock();
1036                disable_dma(dma);
1037                clear_dma_ff(dma);
1038                release_dma_lock(f);
1039                
1040                dma_bytes = 0;
1041        }
1042        
1043        /*
1044         * Free the IRQ
1045         */
1046        free_irq(info->irq, info);
1047
1048        if (dma_buffer) {
1049                struct esp_struct *current_port = ports;
1050
1051                while (current_port) {
1052                        if ((current_port != info) &&
1053                            (current_port->flags & ASYNC_INITIALIZED))
1054                                break;
1055
1056                        current_port = current_port->next_port;
1057                }
1058
1059                if (!current_port) {
1060                        free_dma(dma);
1061                        free_pages((unsigned long)dma_buffer,
1062                                   get_order(DMA_BUFFER_SZ));
1063                        dma_buffer = 0;
1064                }               
1065        }
1066
1067        if (info->xmit_buf) {
1068                free_page((unsigned long) info->xmit_buf);
1069                info->xmit_buf = 0;
1070        }
1071
1072        info->IER = 0;
1073        serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1074        serial_out(info, UART_ESI_CMD2, 0x00);
1075
1076        if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1077                info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1078
1079        info->MCR &= ~UART_MCR_OUT2;
1080        serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1081        serial_out(info, UART_ESI_CMD2, UART_MCR);
1082        serial_out(info, UART_ESI_CMD2, info->MCR);
1083
1084        if (info->tty)
1085                set_bit(TTY_IO_ERROR, &info->tty->flags);
1086        
1087        info->flags &= ~ASYNC_INITIALIZED;
1088        restore_flags(flags);
1089}
1090
1091/*
1092 * This routine is called to set the UART divisor registers to match
1093 * the specified baud rate for a serial port.
1094 */
1095static void change_speed(struct esp_struct *info)
1096{
1097        unsigned short port;
1098        int     quot = 0;
1099        unsigned cflag,cval;
1100        int     baud, bits;
1101        unsigned char flow1 = 0, flow2 = 0;
1102        unsigned long flags;
1103
1104        if (!info->tty || !info->tty->termios)
1105                return;
1106        cflag = info->tty->termios->c_cflag;
1107        port = info->port;
1108        
1109        /* byte size and parity */
1110        switch (cflag & CSIZE) {
1111              case CS5: cval = 0x00; bits = 7; break;
1112              case CS6: cval = 0x01; bits = 8; break;
1113              case CS7: cval = 0x02; bits = 9; break;
1114              case CS8: cval = 0x03; bits = 10; break;
1115              default:  cval = 0x00; bits = 7; break;
1116        }
1117        if (cflag & CSTOPB) {
1118                cval |= 0x04;
1119                bits++;
1120        }
1121        if (cflag & PARENB) {
1122                cval |= UART_LCR_PARITY;
1123                bits++;
1124        }
1125        if (!(cflag & PARODD))
1126                cval |= UART_LCR_EPAR;
1127#ifdef CMSPAR
1128        if (cflag & CMSPAR)
1129                cval |= UART_LCR_SPAR;
1130#endif
1131
1132        baud = tty_get_baud_rate(info->tty);
1133        if (baud == 38400 &&
1134            ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1135                quot = info->custom_divisor;
1136        else {
1137                if (baud == 134)
1138                        /* Special case since 134 is really 134.5 */
1139                        quot = (2*BASE_BAUD / 269);
1140                else if (baud)
1141                        quot = BASE_BAUD / baud;
1142        }
1143        /* If the quotient is ever zero, default to 9600 bps */
1144        if (!quot)
1145                quot = BASE_BAUD / 9600;
1146        
1147        info->timeout = ((1024 * HZ * bits * quot) / BASE_BAUD) + (HZ / 50);
1148
1149        /* CTS flow control flag and modem status interrupts */
1150        /* info->IER &= ~UART_IER_MSI; */
1151        if (cflag & CRTSCTS) {
1152                info->flags |= ASYNC_CTS_FLOW;
1153                /* info->IER |= UART_IER_MSI; */
1154                flow1 = 0x04;
1155                flow2 = 0x10;
1156        } else
1157                info->flags &= ~ASYNC_CTS_FLOW;
1158        if (cflag & CLOCAL)
1159                info->flags &= ~ASYNC_CHECK_CD;
1160        else {
1161                info->flags |= ASYNC_CHECK_CD;
1162                /* info->IER |= UART_IER_MSI; */
1163        }
1164
1165        /*
1166         * Set up parity check flag
1167         */
1168#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1169
1170        info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1171        if (I_INPCK(info->tty))
1172                info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1173        if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1174                info->read_status_mask |= UART_LSR_BI;
1175        
1176        info->ignore_status_mask = 0;
1177#if 0
1178        /* This should be safe, but for some broken bits of hardware... */
1179        if (I_IGNPAR(info->tty)) {
1180                info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1181                info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1182        }
1183#endif
1184        if (I_IGNBRK(info->tty)) {
1185                info->ignore_status_mask |= UART_LSR_BI;
1186                info->read_status_mask |= UART_LSR_BI;
1187                /*
1188                 * If we're ignore parity and break indicators, ignore 
1189                 * overruns too.  (For real raw support).
1190                 */
1191                if (I_IGNPAR(info->tty)) {
1192                        info->ignore_status_mask |= UART_LSR_OE | \
1193                                UART_LSR_PE | UART_LSR_FE;
1194                        info->read_status_mask |= UART_LSR_OE | \
1195                                UART_LSR_PE | UART_LSR_FE;
1196                }
1197        }
1198
1199        if (I_IXOFF(info->tty))
1200                flow1 |= 0x81;
1201
1202        save_flags(flags); cli();
1203        /* set baud */
1204        serial_out(info, UART_ESI_CMD1, ESI_SET_BAUD);
1205        serial_out(info, UART_ESI_CMD2, quot >> 8);
1206        serial_out(info, UART_ESI_CMD2, quot & 0xff);
1207
1208        /* set data bits, parity, etc. */
1209        serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1210        serial_out(info, UART_ESI_CMD2, UART_LCR);
1211        serial_out(info, UART_ESI_CMD2, cval);
1212
1213        /* Enable flow control */
1214        serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CNTL);
1215        serial_out(info, UART_ESI_CMD2, flow1);
1216        serial_out(info, UART_ESI_CMD2, flow2);
1217
1218        /* set flow control characters (XON/XOFF only) */
1219        if (I_IXOFF(info->tty)) {
1220                serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CHARS);
1221                serial_out(info, UART_ESI_CMD2, START_CHAR(info->tty));
1222                serial_out(info, UART_ESI_CMD2, STOP_CHAR(info->tty));
1223                serial_out(info, UART_ESI_CMD2, 0x10);
1224                serial_out(info, UART_ESI_CMD2, 0x21);
1225                switch (cflag & CSIZE) {
1226                        case CS5:
1227                                serial_out(info, UART_ESI_CMD2, 0x1f);
1228                                break;
1229                        case CS6:
1230                                serial_out(info, UART_ESI_CMD2, 0x3f);
1231                                break;
1232                        case CS7:
1233                        case CS8:
1234                                serial_out(info, UART_ESI_CMD2, 0x7f);
1235                                break;
1236                        default:
1237                                serial_out(info, UART_ESI_CMD2, 0xff);
1238                                break;
1239                }
1240        }
1241
1242        /* Set high/low water */
1243        serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1244        serial_out(info, UART_ESI_CMD2, info->config.flow_off >> 8);
1245        serial_out(info, UART_ESI_CMD2, info->config.flow_off);
1246        serial_out(info, UART_ESI_CMD2, info->config.flow_on >> 8);
1247        serial_out(info, UART_ESI_CMD2, info->config.flow_on);
1248
1249        restore_flags(flags);
1250}
1251
1252static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1253{
1254        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1255        unsigned long flags;
1256
1257        if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1258                return;
1259
1260        if (!tty || !info->xmit_buf)
1261                return;
1262
1263        save_flags(flags); cli();
1264        if (info->xmit_cnt >= ESP_XMIT_SIZE - 1) {
1265                restore_flags(flags);
1266                return;
1267        }
1268
1269        info->xmit_buf[info->xmit_head++] = ch;
1270        info->xmit_head &= ESP_XMIT_SIZE-1;
1271        info->xmit_cnt++;
1272        restore_flags(flags);
1273}
1274
1275static void rs_flush_chars(struct tty_struct *tty)
1276{
1277        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1278        unsigned long flags;
1279                                
1280        if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1281                return;
1282
1283        if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf)
1284                return;
1285
1286        save_flags(flags); cli();
1287        if (!(info->IER & UART_IER_THRI)) {
1288                info->IER |= UART_IER_THRI;
1289                serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1290                serial_out(info, UART_ESI_CMD2, info->IER);
1291        }
1292        restore_flags(flags);
1293}
1294
1295static int rs_write(struct tty_struct * tty, int from_user,
1296                    const unsigned char *buf, int count)
1297{
1298        int     c, t, ret = 0;
1299        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1300        unsigned long flags;
1301
1302        if (serial_paranoia_check(info, tty->device, "rs_write"))
1303                return 0;
1304
1305        if (!tty || !info->xmit_buf || !tmp_buf)
1306                return 0;
1307            
1308        if (from_user)
1309                down(&tmp_buf_sem);
1310
1311        while (1) {
1312                /* Thanks to R. Wolff for suggesting how to do this with */
1313                /* interrupts enabled */
1314
1315                c = count;
1316                t = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1317                
1318                if (t < c)
1319                        c = t;
1320
1321                t = ESP_XMIT_SIZE - info->xmit_head;
1322                
1323                if (t < c)
1324                        c = t;
1325
1326                if (c <= 0)
1327                        break;
1328
1329                if (from_user) {
1330                        c -= copy_from_user(tmp_buf, buf, c);
1331
1332                        if (!c) {
1333                                if (!ret)
1334                                        ret = -EFAULT;
1335                                break;
1336                        }
1337
1338                        memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1339                } else
1340                        memcpy(info->xmit_buf + info->xmit_head, buf, c);
1341
1342                info->xmit_head = (info->xmit_head + c) & (ESP_XMIT_SIZE-1);
1343                info->xmit_cnt += c;
1344                buf += c;
1345                count -= c;
1346                ret += c;
1347        }
1348
1349        if (from_user)
1350                up(&tmp_buf_sem);
1351        
1352        save_flags(flags); cli();
1353
1354        if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
1355                info->IER |= UART_IER_THRI;
1356                serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1357                serial_out(info, UART_ESI_CMD2, info->IER);
1358        }
1359
1360        restore_flags(flags);
1361        return ret;
1362}
1363
1364static int rs_write_room(struct tty_struct *tty)
1365{
1366        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1367        int     ret;
1368                                
1369        if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1370                return 0;
1371        ret = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1372        if (ret < 0)
1373                ret = 0;
1374        return ret;
1375}
1376
1377static int rs_chars_in_buffer(struct tty_struct *tty)
1378{
1379        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1380                                
1381        if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1382                return 0;
1383        return info->xmit_cnt;
1384}
1385
1386static void rs_flush_buffer(struct tty_struct *tty)
1387{
1388        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1389                                
1390        if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1391                return;
1392        cli();
1393        info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1394        sti();
1395        tty_wakeup(tty);
1396}
1397
1398/*
1399 * ------------------------------------------------------------
1400 * rs_throttle()
1401 * 
1402 * This routine is called by the upper-layer tty layer to signal that
1403 * incoming characters should be throttled.
1404 * ------------------------------------------------------------
1405 */
1406static void rs_throttle(struct tty_struct * tty)
1407{
1408        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1409#ifdef SERIAL_DEBUG_THROTTLE
1410        char    buf[64];
1411        
1412        printk("throttle %s: %d....\n", tty_name(tty, buf),
1413               tty->ldisc.chars_in_buffer(tty));
1414#endif
1415
1416        if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1417                return;
1418        
1419        cli();
1420        info->IER &= ~UART_IER_RDI;
1421        serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1422        serial_out(info, UART_ESI_CMD2, info->IER);
1423        serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1424        serial_out(info, UART_ESI_CMD2, 0x00);
1425        sti();
1426}
1427
1428static void rs_unthrottle(struct tty_struct * tty)
1429{
1430        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1431#ifdef SERIAL_DEBUG_THROTTLE
1432        char    buf[64];
1433        
1434        printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1435               tty->ldisc.chars_in_buffer(tty));
1436#endif
1437
1438        if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1439                return;
1440        
1441        cli();
1442        info->IER |= UART_IER_RDI;
1443        serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1444        serial_out(info, UART_ESI_CMD2, info->IER);
1445        serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1446        serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
1447        sti();
1448}
1449
1450/*
1451 * ------------------------------------------------------------
1452 * rs_ioctl() and friends
1453 * ------------------------------------------------------------
1454 */
1455
1456static int get_serial_info(struct esp_struct * info,
1457                           struct serial_struct * retinfo)
1458{
1459        struct serial_struct tmp;
1460  
1461        if (!retinfo)
1462                return -EFAULT;
1463        memset(&tmp, 0, sizeof(tmp));
1464        tmp.type = PORT_16550A;
1465        tmp.line = info->line;
1466        tmp.port = info->port;
1467        tmp.irq = info->irq;
1468        tmp.flags = info->flags;
1469        tmp.xmit_fifo_size = 1024;
1470        tmp.baud_base = BASE_BAUD;
1471        tmp.close_delay = info->close_delay;
1472        tmp.closing_wait = info->closing_wait;
1473        tmp.custom_divisor = info->custom_divisor;
1474        tmp.hub6 = 0;
1475        if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1476                return -EFAULT;
1477        return 0;
1478}
1479
1480static int get_esp_config(struct esp_struct * info,
1481                          struct hayes_esp_config * retinfo)
1482{
1483        struct hayes_esp_config tmp;
1484  
1485        if (!retinfo)
1486                return -EFAULT;
1487
1488        memset(&tmp, 0, sizeof(tmp));
1489        tmp.rx_timeout = info->config.rx_timeout;
1490        tmp.rx_trigger = info->config.rx_trigger;
1491        tmp.tx_trigger = info->config.tx_trigger;
1492        tmp.flow_off = info->config.flow_off;
1493        tmp.flow_on = info->config.flow_on;
1494        tmp.pio_threshold = info->config.pio_threshold;
1495        tmp.dma_channel = (info->stat_flags & ESP_STAT_NEVER_DMA ? 0 : dma);
1496
1497        return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
1498}
1499
1500static int set_serial_info(struct esp_struct * info,
1501                           struct serial_struct * new_info)
1502{
1503        struct serial_struct new_serial;
1504        struct esp_struct old_info;
1505        unsigned int change_irq;
1506        int retval = 0;
1507        struct esp_struct *current_async;
1508
1509        if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1510                return -EFAULT;
1511        old_info = *info;
1512
1513        if ((new_serial.type != PORT_16550A) ||
1514            (new_serial.hub6) ||
1515            (info->port != new_serial.port) ||
1516            (new_serial.baud_base != BASE_BAUD) ||
1517            (new_serial.irq > 15) ||
1518            (new_serial.irq < 2) ||
1519            (new_serial.irq == 6) ||
1520            (new_serial.irq == 8) ||
1521            (new_serial.irq == 13))
1522                return -EINVAL;
1523
1524        change_irq = new_serial.irq != info->irq;
1525
1526        if (change_irq && (info->line % 8))
1527                return -EINVAL;
1528
1529        if (!capable(CAP_SYS_ADMIN)) {
1530                if (change_irq || 
1531                    (new_serial.close_delay != info->close_delay) ||
1532                    ((new_serial.flags & ~ASYNC_USR_MASK) !=
1533                     (info->flags & ~ASYNC_USR_MASK)))
1534                        return -EPERM;
1535                info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1536                               (new_serial.flags & ASYNC_USR_MASK));
1537                info->custom_divisor = new_serial.custom_divisor;
1538        } else {
1539                if (new_serial.irq == 2)
1540                        new_serial.irq = 9;
1541
1542                if (change_irq) {
1543                        current_async = ports;
1544
1545                        while (current_async) {
1546                                if ((current_async->line >= info->line) &&
1547                                    (current_async->line < (info->line + 8))) {
1548                                        if (current_async == info) {
1549                                                if (current_async->count > 1)
1550                                                        return -EBUSY;
1551                                        } else if (current_async->count)
1552                                                return -EBUSY;
1553                                }
1554
1555                                current_async = current_async->next_port;
1556                        }
1557                }
1558
1559                /*
1560                 * OK, past this point, all the error checking has been done.
1561                 * At this point, we start making changes.....
1562                 */
1563
1564                info->flags = ((info->flags & ~ASYNC_FLAGS) |
1565                               (new_serial.flags & ASYNC_FLAGS));
1566                info->custom_divisor = new_serial.custom_divisor;
1567                info->close_delay = new_serial.close_delay * HZ/100;
1568                info->closing_wait = new_serial.closing_wait * HZ/100;
1569
1570                if (change_irq) {
1571                        /*
1572                         * We need to shutdown the serial port at the old
1573                         * port/irq combination.
1574                         */
1575                        shutdown(info);
1576
1577                        current_async = ports;
1578
1579                        while (current_async) {
1580                                if ((current_async->line >= info->line) &&
1581                                    (current_async->line < (info->line + 8)))
1582                                        current_async->irq = new_serial.irq;
1583
1584                                current_async = current_async->next_port;
1585                        }
1586
1587                        serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
1588                        if (info->irq == 9)
1589                                serial_out(info, UART_ESI_CMD2, 0x02);
1590                        else
1591                                serial_out(info, UART_ESI_CMD2, info->irq);
1592                }
1593        }
1594
1595        if (info->flags & ASYNC_INITIALIZED) {
1596                if (((old_info.flags & ASYNC_SPD_MASK) !=
1597                     (info->flags & ASYNC_SPD_MASK)) ||
1598                    (old_info.custom_divisor != info->custom_divisor)) {
1599                        if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1600                                info->tty->alt_speed = 57600;
1601                        if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1602                                info->tty->alt_speed = 115200;
1603                        if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1604                                info->tty->alt_speed = 230400;
1605                        if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1606                                info->tty->alt_speed = 460800;
1607                        change_speed(info);
1608                }
1609        } else
1610                retval = startup(info);
1611
1612        return retval;
1613}
1614
1615static int set_esp_config(struct esp_struct * info,
1616                          struct hayes_esp_config * new_info)
1617{
1618        struct hayes_esp_config new_config;
1619        unsigned int change_dma;
1620        int retval = 0;
1621        struct esp_struct *current_async;
1622
1623        /* Perhaps a non-sysadmin user should be able to do some of these */
1624        /* operations.  I haven't decided yet. */
1625
1626        if (!capable(CAP_SYS_ADMIN))
1627                return -EPERM;
1628
1629        if (copy_from_user(&new_config, new_info, sizeof(new_config)))
1630                return -EFAULT;
1631
1632        if ((new_config.flow_on >= new_config.flow_off) ||
1633            (new_config.rx_trigger < 1) ||
1634            (new_config.tx_trigger < 1) ||
1635            (new_config.flow_off < 1) ||
1636            (new_config.flow_on < 1) ||
1637            (new_config.rx_trigger > 1023) ||
1638            (new_config.tx_trigger > 1023) ||
1639            (new_config.flow_off > 1023) ||
1640            (new_config.flow_on > 1023) ||
1641            (new_config.pio_threshold < 0) ||
1642            (new_config.pio_threshold > 1024))
1643                return -EINVAL;
1644
1645        if ((new_config.dma_channel != 1) && (new_config.dma_channel != 3))
1646                new_config.dma_channel = 0;
1647
1648        if (info->stat_flags & ESP_STAT_NEVER_DMA)
1649                change_dma = new_config.dma_channel;
1650        else
1651                change_dma = (new_config.dma_channel != dma);
1652
1653        if (change_dma) {
1654                if (new_config.dma_channel) {
1655                        /* PIO mode to DMA mode transition OR */
1656                        /* change current DMA channel */
1657                        
1658                        current_async = ports;
1659
1660                        while (current_async) {
1661                                if (current_async == info) {
1662                                        if (current_async->count > 1)
1663                                                return -EBUSY;
1664                                } else if (current_async->count)
1665                                        return -EBUSY;
1666                                        
1667                                current_async =
1668                                        current_async->next_port;
1669                        }
1670
1671                        shutdown(info);
1672                        dma = new_config.dma_channel;
1673                        info->stat_flags &= ~ESP_STAT_NEVER_DMA;
1674                        
1675                        /* all ports must use the same DMA channel */
1676
1677                        current_async = ports;
1678
1679                        while (current_async) {
1680                                esp_basic_init(current_async);
1681                                current_async = current_async->next_port;
1682                        }
1683                } else {
1684                        /* DMA mode to PIO mode only */
1685                        
1686                        if (info->count > 1)
1687                                return -EBUSY;
1688
1689                        shutdown(info);
1690                        info->stat_flags |= ESP_STAT_NEVER_DMA;
1691                        esp_basic_init(info);
1692                }
1693        }
1694
1695        info->config.pio_threshold = new_config.pio_threshold;
1696
1697        if ((new_config.flow_off != info->config.flow_off) ||
1698            (new_config.flow_on != info->config.flow_on)) {
1699                unsigned long flags;
1700
1701                info->config.flow_off = new_config.flow_off;
1702                info->config.flow_on = new_config.flow_on;
1703                save_flags(flags); cli();
1704                serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1705                serial_out(info, UART_ESI_CMD2, new_config.flow_off >> 8);
1706                serial_out(info, UART_ESI_CMD2, new_config.flow_off);
1707                serial_out(info, UART_ESI_CMD2, new_config.flow_on >> 8);
1708                serial_out(info, UART_ESI_CMD2, new_config.flow_on);
1709                restore_flags(flags);
1710        }
1711
1712        if ((new_config.rx_trigger != info->config.rx_trigger) ||
1713            (new_config.tx_trigger != info->config.tx_trigger)) {
1714                unsigned long flags;
1715
1716                info->config.rx_trigger = new_config.rx_trigger;
1717                info->config.tx_trigger = new_config.tx_trigger;
1718                save_flags(flags); cli();
1719                serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
1720                serial_out(info, UART_ESI_CMD2,
1721                           new_config.rx_trigger >> 8);
1722                serial_out(info, UART_ESI_CMD2, new_config.rx_trigger);
1723                serial_out(info, UART_ESI_CMD2,
1724                           new_config.tx_trigger >> 8);
1725                serial_out(info, UART_ESI_CMD2, new_config.tx_trigger);
1726                restore_flags(flags);
1727        }
1728
1729        if (new_config.rx_timeout != info->config.rx_timeout) {
1730                unsigned long flags;
1731
1732                info->config.rx_timeout = new_config.rx_timeout;
1733                save_flags(flags); cli();
1734
1735                if (info->IER & UART_IER_RDI) {
1736                        serial_out(info, UART_ESI_CMD1,
1737                                   ESI_SET_RX_TIMEOUT);
1738                        serial_out(info, UART_ESI_CMD2,
1739                                   new_config.rx_timeout);
1740                }
1741
1742                restore_flags(flags);
1743        }
1744
1745        if (!(info->flags & ASYNC_INITIALIZED))
1746                retval = startup(info);
1747
1748        return retval;
1749}
1750
1751/*
1752 * get_lsr_info - get line status register info
1753 *
1754 * Purpose: Let user call ioctl() to get info when the UART physically
1755 *          is emptied.  On bus types like RS485, the transmitter must
1756 *          release the bus after transmitting. This must be done when
1757 *          the transmit shift register is empty, not be done when the
1758 *          transmit holding register is empty.  This functionality
1759 *          allows an RS485 driver to be written in user space. 
1760 */
1761static int get_lsr_info(struct esp_struct * info, unsigned int *value)
1762{
1763        unsigned char status;
1764        unsigned int result;
1765
1766        cli();
1767        serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1768        status = serial_in(info, UART_ESI_STAT1);
1769        sti();
1770        result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1771        return put_user(result,value);
1772}
1773
1774
1775static int get_modem_info(struct esp_struct * info, unsigned int *value)
1776{
1777        unsigned char control, status;
1778        unsigned int result;
1779
1780        control = info->MCR;
1781        cli();
1782        serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1783        status = serial_in(info, UART_ESI_STAT2);
1784        sti();
1785        result =  ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1786                | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1787                | ((status  & UART_MSR_DCD) ? TIOCM_CAR : 0)
1788                | ((status  & UART_MSR_RI) ? TIOCM_RNG : 0)
1789                | ((status  & UART_MSR_DSR) ? TIOCM_DSR : 0)
1790                | ((status  & UART_MSR_CTS) ? TIOCM_CTS : 0);
1791        return put_user(result,value);
1792}
1793
1794static int set_modem_info(struct esp_struct * info, unsigned int cmd,
1795                          unsigned int *value)
1796{
1797        unsigned int arg;
1798
1799        if (get_user(arg, value))
1800                return -EFAULT;
1801
1802        switch (cmd) {
1803        case TIOCMBIS: 
1804                if (arg & TIOCM_RTS)
1805                        info->MCR |= UART_MCR_RTS;
1806                if (arg & TIOCM_DTR)
1807                        info->MCR |= UART_MCR_DTR;
1808                break;
1809        case TIOCMBIC:
1810                if (arg & TIOCM_RTS)
1811                        info->MCR &= ~UART_MCR_RTS;
1812                if (arg & TIOCM_DTR)
1813                        info->MCR &= ~UART_MCR_DTR;
1814                break;
1815        case TIOCMSET:
1816                info->MCR = ((info->MCR & ~(UART_MCR_RTS | UART_MCR_DTR))
1817                             | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1818                             | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1819                break;
1820        default:
1821                return -EINVAL;
1822        }
1823        cli();
1824        serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1825        serial_out(info, UART_ESI_CMD2, UART_MCR);
1826        serial_out(info, UART_ESI_CMD2, info->MCR);
1827        sti();
1828        return 0;
1829}
1830
1831/*
1832 * rs_break() --- routine which turns the break handling on or off
1833 */
1834static void esp_break(struct tty_struct *tty, int break_state)
1835{
1836        struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1837        unsigned long flags;
1838        
1839        if (serial_paranoia_check(info, tty->device, "esp_break"))
1840                return;
1841
1842        save_flags(flags); cli();
1843        if (break_state == -1) {
1844                serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1845                serial_out(info, UART_ESI_CMD2, 0x01);
1846
1847                interruptible_sleep_on(&info->break_wait);
1848        } else {
1849                serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1850                serial_out(info, UART_ESI_CMD2, 0x00);
1851        }
1852        restore_flags(flags);
1853}
1854
1855static int rs_ioctl(struct tty_struct *tty, struct file * file,
1856                    unsigned int cmd, unsigned long arg)
1857{
1858        struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1859        struct async_icount cprev, cnow;        /* kernel counter temps */
1860        struct serial_icounter_struct *p_cuser; /* user space */
1861
1862        if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1863                return -ENODEV;
1864
1865        if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1866            (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1867            (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1868            (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT) &&
1869            (cmd != TIOCGHAYESESP) && (cmd != TIOCSHAYESESP)) {
1870                if (tty->flags & (1 << TTY_IO_ERROR))
1871                    return -EIO;
1872        }
1873        
1874        switch (cmd) {
1875                case TIOCMGET:
1876                        return get_modem_info(info, (unsigned int *) arg);
1877                case TIOCMBIS:
1878                case TIOCMBIC:
1879                case TIOCMSET:
1880                        return set_modem_info(info, cmd, (unsigned int *) arg);
1881                case TIOCGSERIAL:
1882                        return get_serial_info(info,
1883                                               (struct serial_struct *) arg);
1884                case TIOCSSERIAL:
1885                        return set_serial_info(info,
1886                                               (struct serial_struct *) arg);
1887                case TIOCSERCONFIG:
1888                        /* do not reconfigure after initial configuration */
1889                        return 0;
1890
1891                case TIOCSERGWILD:
1892                        return put_user(0L, (unsigned long *) arg);
1893
1894                case TIOCSERGETLSR: /* Get line status register */
1895                            return get_lsr_info(info, (unsigned int *) arg);
1896
1897                case TIOCSERSWILD:
1898                        if (!capable(CAP_SYS_ADMIN))
1899                                return -EPERM;
1900                        return 0;
1901
1902                /*
1903                 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1904                 * - mask passed in arg for lines of interest
1905                 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1906                 * Caller should use TIOCGICOUNT to see which one it was
1907                 */
1908                 case TIOCMIWAIT:
1909                        cli();
1910                        cprev = info->icount;   /* note the counters on entry */
1911                        sti();
1912                        while (1) {
1913                                interruptible_sleep_on(&info->delta_msr_wait);
1914                                /* see if a signal did it */
1915                                if (signal_pending(current))
1916                                        return -ERESTARTSYS;
1917                                cli();
1918                                cnow = info->icount;    /* atomic copy */
1919                                sti();
1920                                if (cnow.rng == cprev.rng &&
1921                                    cnow.dsr == cprev.dsr && 
1922                                    cnow.dcd == cprev.dcd &&
1923                                    cnow.cts == cprev.cts)
1924                                        return -EIO; /* no change => error */
1925                                if (((arg & TIOCM_RNG) &&
1926                                     (cnow.rng != cprev.rng)) ||
1927                                     ((arg & TIOCM_DSR) &&
1928                                      (cnow.dsr != cprev.dsr)) ||
1929                                     ((arg & TIOCM_CD) &&
1930                                      (cnow.dcd != cprev.dcd)) ||
1931                                     ((arg & TIOCM_CTS) &&
1932                                      (cnow.cts != cprev.cts)) ) {
1933                                        return 0;
1934                                }
1935                                cprev = cnow;
1936                        }
1937                        /* NOTREACHED */
1938
1939                /* 
1940                 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1941                 * Return: write counters to the user passed counter struct
1942                 * NB: both 1->0 and 0->1 transitions are counted except for
1943                 *     RI where only 0->1 is counted.
1944                 */
1945                case TIOCGICOUNT:
1946                        cli();
1947                        cnow = info->icount;
1948                        sti();
1949                        p_cuser = (struct serial_icounter_struct *) arg;
1950                        if (put_user(cnow.cts, &p_cuser->cts) ||
1951                            put_user(cnow.dsr, &p_cuser->dsr) ||
1952                            put_user(cnow.rng, &p_cuser->rng) ||
1953                            put_user(cnow.dcd, &p_cuser->dcd))
1954                                return -EFAULT;
1955
1956                        return 0;
1957        case TIOCGHAYESESP:
1958                return (get_esp_config(info, (struct hayes_esp_config *)arg));
1959        case TIOCSHAYESESP:
1960                return (set_esp_config(info, (struct hayes_esp_config *)arg));
1961
1962                default:
1963                        return -ENOIOCTLCMD;
1964                }
1965        return 0;
1966}
1967
1968static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1969{
1970        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1971
1972        if (   (tty->termios->c_cflag == old_termios->c_cflag)
1973            && (   RELEVANT_IFLAG(tty->termios->c_iflag) 
1974                == RELEVANT_IFLAG(old_termios->c_iflag)))
1975          return;
1976
1977        change_speed(info);
1978
1979        /* Handle transition to B0 status */
1980        if ((old_termios->c_cflag & CBAUD) &&
1981                !(tty->termios->c_cflag & CBAUD)) {
1982                info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1983                cli();
1984                serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1985                serial_out(info, UART_ESI_CMD2, UART_MCR);
1986                serial_out(info, UART_ESI_CMD2, info->MCR);
1987                sti();
1988        }
1989
1990        /* Handle transition away from B0 status */
1991        if (!(old_termios->c_cflag & CBAUD) &&
1992                (tty->termios->c_cflag & CBAUD)) {
1993                info->MCR |= (UART_MCR_DTR | UART_MCR_RTS);
1994                cli();
1995                serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1996                serial_out(info, UART_ESI_CMD2, UART_MCR);
1997                serial_out(info, UART_ESI_CMD2, info->MCR);
1998                sti();
1999        }
2000
2001        /* Handle turning of CRTSCTS */
2002        if ((old_termios->c_cflag & CRTSCTS) &&
2003            !(tty->termios->c_cflag & CRTSCTS)) {
2004                rs_start(tty);
2005        }
2006
2007#if 0
2008        /*
2009         * No need to wake up processes in open wait, since they
2010         * sample the CLOCAL flag once, and don't recheck it.
2011         * XXX  It's not clear whether the current behavior is correct
2012         * or not.  Hence, this may change.....
2013         */
2014        if (!(old_termios->c_cflag & CLOCAL) &&
2015            (tty->termios->c_cflag & CLOCAL))
2016                wake_up_interruptible(&info->open_wait);
2017#endif
2018}
2019
2020/*
2021 * ------------------------------------------------------------
2022 * rs_close()
2023 * 
2024 * This routine is called when the serial port gets closed.  First, we
2025 * wait for the last remaining data to be sent.  Then, we unlink its
2026 * async structure from the interrupt chain if necessary, and we free
2027 * that IRQ if nothing is left in the chain.
2028 * ------------------------------------------------------------
2029 */
2030static void rs_close(struct tty_struct *tty, struct file * filp)
2031{
2032        struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2033        unsigned long flags;
2034
2035        if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
2036                return;
2037        
2038        save_flags(flags); cli();
2039        
2040        if (tty_hung_up_p(filp)) {
2041                DBG_CNT("before DEC-hung");
2042                goto out;
2043        }
2044        
2045#ifdef SERIAL_DEBUG_OPEN
2046        printk("rs_close ttys%d, count = %d\n", info->line, info->count);
2047#endif
2048        if ((tty->count == 1) && (info->count != 1)) {
2049                /*
2050                 * Uh, oh.  tty->count is 1, which means that the tty
2051                 * structure will be freed.  Info->count should always
2052                 * be one in these conditions.  If it's greater than
2053                 * one, we've got real problems, since it means the
2054                 * serial port won't be shutdown.
2055                 */
2056                printk("rs_close: bad serial port count; tty->count is 1, "
2057                       "info->count is %d\n", info->count);
2058                info->count = 1;
2059        }
2060        if (--info->count < 0) {
2061                printk("rs_close: bad serial port count for ttys%d: %d\n",
2062                       info->line, info->count);
2063                info->count = 0;
2064        }
2065        if (info->count) {
2066                DBG_CNT("before DEC-2");
2067                goto out;
2068        }
2069        info->flags |= ASYNC_CLOSING;
2070        /*
2071         * Save the termios structure, since this port may have
2072         * separate termios for callout and dialin.
2073         */
2074        if (info->flags & ASYNC_NORMAL_ACTIVE)
2075                info->normal_termios = *tty->termios;
2076        if (info->flags & ASYNC_CALLOUT_ACTIVE)
2077                info->callout_termios = *tty->termios;
2078        /*
2079         * Now we wait for the transmit buffer to clear; and we notify 
2080         * the line discipline to only process XON/XOFF characters.
2081         */
2082        tty->closing = 1;
2083        if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2084                tty_wait_until_sent(tty, info->closing_wait);
2085        /*
2086         * At this point we stop accepting input.  To do this, we
2087         * disable the receive line status interrupts, and tell the
2088         * interrupt driver to stop checking the data ready bit in the
2089         * line status register.
2090         */
2091        /* info->IER &= ~UART_IER_RLSI; */
2092        info->IER &= ~UART_IER_RDI;
2093        info->read_status_mask &= ~UART_LSR_DR;
2094        if (info->flags & ASYNC_INITIALIZED) {
2095                serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
2096                serial_out(info, UART_ESI_CMD2, info->IER);
2097
2098                /* disable receive timeout */
2099                serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
2100                serial_out(info, UART_ESI_CMD2, 0x00);
2101
2102                /*
2103                 * Before we drop DTR, make sure the UART transmitter
2104                 * has completely drained; this is especially
2105                 * important if there is a transmit FIFO!
2106                 */
2107                rs_wait_until_sent(tty, info->timeout);
2108        }
2109        shutdown(info);
2110        if (tty->driver.flush_buffer)
2111                tty->driver.flush_buffer(tty);
2112        tty_ldisc_flush(tty);
2113        tty->closing = 0;
2114        info->event = 0;
2115        info->tty = 0;
2116
2117        if (info->blocked_open) {
2118                if (info->close_delay) {
2119                        set_current_state(TASK_INTERRUPTIBLE);
2120                        schedule_timeout(info->close_delay);
2121                }
2122                wake_up_interruptible(&info->open_wait);
2123        }
2124        info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
2125                         ASYNC_CLOSING);
2126        wake_up_interruptible(&info->close_wait);
2127out:    MOD_DEC_USE_COUNT;
2128        restore_flags(flags);
2129}
2130
2131static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2132{
2133        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
2134        unsigned long orig_jiffies, char_time;
2135        unsigned long flags;
2136
2137        if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
2138                return;
2139
2140        orig_jiffies = jiffies;
2141        char_time = ((info->timeout - HZ / 50) / 1024) / 5;
2142
2143        if (!char_time)
2144                char_time = 1;
2145
2146        save_flags(flags); cli();
2147        serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2148        serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2149
2150        while ((serial_in(info, UART_ESI_STAT1) != 0x03) ||
2151                (serial_in(info, UART_ESI_STAT2) != 0xff)) {
2152                set_current_state(TASK_INTERRUPTIBLE);
2153                schedule_timeout(char_time);
2154
2155                if (signal_pending(current))
2156                        break;
2157
2158                if (timeout && time_after(jiffies, orig_jiffies + timeout))
2159                        break;
2160
2161                serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2162                serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2163        }
2164        
2165        restore_flags(flags);
2166        set_current_state(TASK_RUNNING);
2167}
2168
2169/*
2170 * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2171 */
2172static void esp_hangup(struct tty_struct *tty)
2173{
2174        struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2175        
2176        if (serial_paranoia_check(info, tty->device, "esp_hangup"))
2177                return;
2178        
2179        rs_flush_buffer(tty);
2180        shutdown(info);
2181        info->event = 0;
2182        info->count = 0;
2183        info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2184        info->tty = 0;
2185        wake_up_interruptible(&info->open_wait);
2186}
2187
2188/*
2189 * ------------------------------------------------------------
2190 * esp_open() and friends
2191 * ------------------------------------------------------------
2192 */
2193static int block_til_ready(struct tty_struct *tty, struct file * filp,
2194                           struct esp_struct *info)
2195{
2196        DECLARE_WAITQUEUE(wait, current);
2197        int             retval;
2198        int             do_clocal = 0;
2199        unsigned long   flags;
2200
2201        /*
2202         * If the device is in the middle of being closed, then block
2203         * until it's done, and then try again.
2204         */
2205        if (tty_hung_up_p(filp) ||
2206            (info->flags & ASYNC_CLOSING)) {
2207                if (info->flags & ASYNC_CLOSING)
2208                        interruptible_sleep_on(&info->close_wait);
2209#ifdef SERIAL_DO_RESTART
2210                if (info->flags & ASYNC_HUP_NOTIFY)
2211                        return -EAGAIN;
2212                else
2213                        return -ERESTARTSYS;
2214#else
2215                return -EAGAIN;
2216#endif
2217        }
2218
2219        /*
2220         * If this is a callout device, then just make sure the normal
2221         * device isn't being used.
2222         */
2223        if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2224                if (info->flags & ASYNC_NORMAL_ACTIVE)
2225                        return -EBUSY;
2226                if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2227                    (info->flags & ASYNC_SESSION_LOCKOUT) &&
2228                    (info->session != current->session))
2229                    return -EBUSY;
2230                if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2231                    (info->flags & ASYNC_PGRP_LOCKOUT) &&
2232                    (info->pgrp != current->pgrp))
2233                    return -EBUSY;
2234                info->flags |= ASYNC_CALLOUT_ACTIVE;
2235                return 0;
2236        }
2237        
2238        /*
2239         * If non-blocking mode is set, or the port is not enabled,
2240         * then make the check up front and then exit.
2241         */
2242        if ((filp->f_flags & O_NONBLOCK) ||
2243            (tty->flags & (1 << TTY_IO_ERROR))) {
2244                if (info->flags & ASYNC_CALLOUT_ACTIVE)
2245                        return -EBUSY;
2246                info->flags |= ASYNC_NORMAL_ACTIVE;
2247                return 0;
2248        }
2249
2250        if (info->flags & ASYNC_CALLOUT_ACTIVE) {
2251                if (info->normal_termios.c_cflag & CLOCAL)
2252                        do_clocal = 1;
2253        } else {
2254                if (tty->termios->c_cflag & CLOCAL)
2255                        do_clocal = 1;
2256        }
2257        
2258        /*
2259         * Block waiting for the carrier detect and the line to become
2260         * free (i.e., not in use by the callout).  While we are in
2261         * this loop, info->count is dropped by one, so that
2262         * rs_close() knows when to free things.  We restore it upon
2263         * exit, either normal or abnormal.
2264         */
2265        retval = 0;
2266        add_wait_queue(&info->open_wait, &wait);
2267#ifdef SERIAL_DEBUG_OPEN
2268        printk("block_til_ready before block: ttys%d, count = %d\n",
2269               info->line, info->count);
2270#endif
2271        save_flags(flags);
2272        cli();
2273        if (!tty_hung_up_p(filp)) 
2274                info->count--;
2275        restore_flags(flags);
2276        info->blocked_open++;
2277        while (1) {
2278                save_flags(flags);
2279                cli();
2280                if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2281                        (tty->termios->c_cflag & CBAUD)) {
2282                        unsigned int scratch;
2283
2284                        serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
2285                        serial_out(info, UART_ESI_CMD2, UART_MCR);
2286                        scratch = serial_in(info, UART_ESI_STAT1);
2287                        serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2288                        serial_out(info, UART_ESI_CMD2, UART_MCR);
2289                        serial_out(info, UART_ESI_CMD2,
2290                                scratch | UART_MCR_DTR | UART_MCR_RTS);
2291                }
2292                restore_flags(flags);
2293                set_current_state(TASK_INTERRUPTIBLE);
2294                if (tty_hung_up_p(filp) ||
2295                    !(info->flags & ASYNC_INITIALIZED)) {
2296#ifdef SERIAL_DO_RESTART
2297                        if (info->flags & ASYNC_HUP_NOTIFY)
2298                                retval = -EAGAIN;
2299                        else
2300                                retval = -ERESTARTSYS;  
2301#else
2302                        retval = -EAGAIN;
2303#endif
2304                        break;
2305                }
2306
2307                serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
2308                if (serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD)
2309                        do_clocal = 1;
2310
2311                if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2312                    !(info->flags & ASYNC_CLOSING) &&
2313                    (do_clocal))
2314                        break;
2315                if (signal_pending(current)) {
2316                        retval = -ERESTARTSYS;
2317                        break;
2318                }
2319#ifdef SERIAL_DEBUG_OPEN
2320                printk("block_til_ready blocking: ttys%d, count = %d\n",
2321                       info->line, info->count);
2322#endif
2323                schedule();
2324        }
2325        set_current_state(TASK_RUNNING);
2326        remove_wait_queue(&info->open_wait, &wait);
2327        if (!tty_hung_up_p(filp))
2328                info->count++;
2329        info->blocked_open--;
2330#ifdef SERIAL_DEBUG_OPEN
2331        printk("block_til_ready after blocking: ttys%d, count = %d\n",
2332               info->line, info->count);
2333#endif
2334        if (retval)
2335                return retval;
2336        info->flags |= ASYNC_NORMAL_ACTIVE;
2337        return 0;
2338}       
2339
2340/*
2341 * This routine is called whenever a serial port is opened.  It
2342 * enables interrupts for a serial port, linking in its async structure into
2343 * the IRQ chain.   It also performs the serial-specific
2344 * initialization for the tty structure.
2345 */
2346static int esp_open(struct tty_struct *tty, struct file * filp)
2347{
2348        struct esp_struct       *info;
2349        int                     retval, line;
2350
2351        line = MINOR(tty->device) - tty->driver.minor_start;
2352        if ((line < 0) || (line >= NR_PORTS))
2353                return -ENODEV;
2354
2355        /* find the port in the chain */
2356
2357        info = ports;
2358
2359        while (info && (info->line != line))
2360                info = info->next_port;
2361
2362        if (!info) {
2363                serial_paranoia_check(info, tty->device, "esp_open");
2364                return -ENODEV;
2365        }
2366
2367#ifdef SERIAL_DEBUG_OPEN
2368        printk("esp_open %s%d, count = %d\n", tty->driver.name, info->line,
2369               info->count);
2370#endif
2371        MOD_INC_USE_COUNT;
2372        info->count++;
2373        tty->driver_data = info;
2374        info->tty = tty;
2375
2376        if (!tmp_buf) {
2377                tmp_buf = (unsigned char *) get_free_page(GFP_KERNEL);
2378                if (!tmp_buf)
2379                        return -ENOMEM;
2380        }
2381        
2382        /*
2383         * Start up serial port
2384         */
2385        retval = startup(info);
2386        if (retval)
2387                return retval;
2388
2389        retval = block_til_ready(tty, filp, info);
2390        if (retval) {
2391#ifdef SERIAL_DEBUG_OPEN
2392                printk("esp_open returning after block_til_ready with %d\n",
2393                       retval);
2394#endif
2395                return retval;
2396        }
2397
2398        if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
2399                if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2400                        *tty->termios = info->normal_termios;
2401                else 
2402                        *tty->termios = info->callout_termios;
2403                change_speed(info);
2404        }
2405
2406        info->session = current->session;
2407        info->pgrp = current->pgrp;
2408
2409#ifdef SERIAL_DEBUG_OPEN
2410        printk("esp_open ttys%d successful...", info->line);
2411#endif
2412        return 0;
2413}
2414
2415/*
2416 * ---------------------------------------------------------------------
2417 * espserial_init() and friends
2418 *
2419 * espserial_init() is called at boot-time to initialize the serial driver.
2420 * ---------------------------------------------------------------------
2421 */
2422
2423/*
2424 * This routine prints out the appropriate serial driver version
2425 * number, and identifies which options were configured into this
2426 * driver.
2427 */
2428 
2429static _INLINE_ void show_serial_version(void)
2430{
2431        printk(KERN_INFO "%s version %s (DMA %u)\n",
2432                serial_name, serial_version, dma);
2433}
2434
2435/*
2436 * This routine is called by espserial_init() to initialize a specific serial
2437 * port.
2438 */
2439static _INLINE_ int autoconfig(struct esp_struct * info, int *region_start)
2440{
2441        int port_detected = 0;
2442        unsigned long flags;
2443
2444        save_flags(flags); cli();
2445        
2446        /*
2447         * Check for ESP card
2448         */
2449
2450        if (!check_region(info->port, 8) && 
2451            serial_in(info, UART_ESI_BASE) == 0xf3) {
2452                serial_out(info, UART_ESI_CMD1, 0x00);
2453                serial_out(info, UART_ESI_CMD1, 0x01);
2454
2455                if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) {
2456                        port_detected = 1;
2457
2458                        if (!(info->irq)) {
2459                                serial_out(info, UART_ESI_CMD1, 0x02);
2460
2461                                if (serial_in(info, UART_ESI_STAT1) & 0x01)
2462                                        info->irq = 3;
2463                                else
2464                                        info->irq = 4;
2465                        }
2466
2467                        if (ports && (ports->port == (info->port - 8))) {
2468                                release_region(*region_start,
2469                                               info->port - *region_start);
2470                        } else
2471                                *region_start = info->port;
2472
2473                        request_region(*region_start,
2474                                       info->port - *region_start + 8,
2475                                       "esp serial");
2476
2477                        /* put card in enhanced mode */
2478                        /* this prevents access through */
2479                        /* the "old" IO ports */
2480                        esp_basic_init(info);
2481
2482                        /* clear out MCR */
2483                        serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2484                        serial_out(info, UART_ESI_CMD2, UART_MCR);
2485                        serial_out(info, UART_ESI_CMD2, 0x00);
2486                }
2487        }
2488
2489        restore_flags(flags);
2490        return (port_detected);
2491}
2492
2493/*
2494 * The serial driver boot-time initialization code!
2495 */
2496int __init espserial_init(void)
2497{
2498        int i, offset;
2499        int region_start;
2500        struct esp_struct * info;
2501        struct esp_struct *last_primary = 0;
2502        int esp[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380};
2503        
2504        init_bh(ESP_BH, do_serial_bh);
2505
2506        for (i = 0; i < NR_PRIMARY; i++) {
2507                if (irq[i] != 0) {
2508                        if ((irq[i] < 2) || (irq[i] > 15) || (irq[i] == 6) ||
2509                            (irq[i] == 8) || (irq[i] == 13))
2510                                irq[i] = 0;
2511                        else if (irq[i] == 2)
2512                                irq[i] = 9;
2513                }
2514        }
2515
2516        if ((dma != 1) && (dma != 3))
2517                dma = 0;
2518
2519        if ((rx_trigger < 1) || (rx_trigger > 1023))
2520                rx_trigger = 768;
2521
2522        if ((tx_trigger < 1) || (tx_trigger > 1023))
2523                tx_trigger = 768;
2524
2525        if ((flow_off < 1) || (flow_off > 1023))
2526                flow_off = 1016;
2527        
2528        if ((flow_on < 1) || (flow_on > 1023))
2529                flow_on = 944;
2530
2531        if ((rx_timeout < 0) || (rx_timeout > 255))
2532                rx_timeout = 128;
2533        
2534        if (flow_on >= flow_off)
2535                flow_on = flow_off - 1;
2536
2537        show_serial_version();
2538
2539        /* Initialize the tty_driver structure */
2540        
2541        memset(&esp_driver, 0, sizeof(struct tty_driver));
2542        esp_driver.magic = TTY_DRIVER_MAGIC;
2543        esp_driver.name = "ttyP";
2544        esp_driver.major = ESP_IN_MAJOR;
2545        esp_driver.minor_start = 0;
2546        esp_driver.num = NR_PORTS;
2547        esp_driver.type = TTY_DRIVER_TYPE_SERIAL;
2548        esp_driver.subtype = SERIAL_TYPE_NORMAL;
2549        esp_driver.init_termios = tty_std_termios;
2550        esp_driver.init_termios.c_cflag =
2551                B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2552        esp_driver.flags = TTY_DRIVER_REAL_RAW;
2553        esp_driver.refcount = &serial_refcount;
2554        esp_driver.table = serial_table;
2555        esp_driver.termios = serial_termios;
2556        esp_driver.termios_locked = serial_termios_locked;
2557
2558        esp_driver.open = esp_open;
2559        esp_driver.close = rs_close;
2560        esp_driver.write = rs_write;
2561        esp_driver.put_char = rs_put_char;
2562        esp_driver.flush_chars = rs_flush_chars;
2563        esp_driver.write_room = rs_write_room;
2564        esp_driver.chars_in_buffer = rs_chars_in_buffer;
2565        esp_driver.flush_buffer = rs_flush_buffer;
2566        esp_driver.ioctl = rs_ioctl;
2567        esp_driver.throttle = rs_throttle;
2568        esp_driver.unthrottle = rs_unthrottle;
2569        esp_driver.set_termios = rs_set_termios;
2570        esp_driver.stop = rs_stop;
2571        esp_driver.start = rs_start;
2572        esp_driver.hangup = esp_hangup;
2573        esp_driver.break_ctl = esp_break;
2574        esp_driver.wait_until_sent = rs_wait_until_sent;
2575
2576        /*
2577         * The callout device is just like normal device except for
2578         * major number and the subtype code.
2579         */
2580        esp_callout_driver = esp_driver;
2581        esp_callout_driver.name = "cup";
2582        esp_callout_driver.major = ESP_OUT_MAJOR;
2583        esp_callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2584
2585        if (tty_register_driver(&esp_driver))
2586        {
2587                printk(KERN_ERR "Couldn't register esp serial driver");
2588                return 1;
2589        }
2590
2591        if (tty_register_driver(&esp_callout_driver))
2592        {
2593                printk(KERN_ERR "Couldn't register esp callout driver");
2594                tty_unregister_driver(&esp_driver);
2595                return 1;
2596        }
2597        
2598        info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2599
2600        if (!info)
2601        {
2602                printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2603                tty_unregister_driver(&esp_driver);
2604                tty_unregister_driver(&esp_callout_driver);
2605                return 1;
2606        }
2607
2608        memset((void *)info, 0, sizeof(struct esp_struct));
2609        /* rx_trigger, tx_trigger are needed by autoconfig */
2610        info->config.rx_trigger = rx_trigger;
2611        info->config.tx_trigger = tx_trigger;
2612
2613        i = 0;
2614        offset = 0;
2615
2616        do {
2617                info->port = esp[i] + offset;
2618                info->irq = irq[i];
2619                info->line = (i * 8) + (offset / 8);
2620
2621                if (!autoconfig(info, &region_start)) {
2622                        i++;
2623                        offset = 0;
2624                        continue;
2625                }
2626
2627                info->custom_divisor = (divisor[i] >> (offset / 2)) & 0xf;
2628                info->flags = STD_COM_FLAGS;
2629                if (info->custom_divisor)
2630                        info->flags |= ASYNC_SPD_CUST;
2631                info->magic = ESP_MAGIC;
2632                info->close_delay = 5*HZ/10;
2633                info->closing_wait = 30*HZ;
2634                info->tqueue.routine = do_softint;
2635                info->tqueue.data = info;
2636                info->tqueue_hangup.routine = do_serial_hangup;
2637                info->tqueue_hangup.data = info;
2638                info->callout_termios = esp_callout_driver.init_termios;
2639                info->normal_termios = esp_driver.init_termios;
2640                info->config.rx_timeout = rx_timeout;
2641                info->config.flow_on = flow_on;
2642                info->config.flow_off = flow_off;
2643                info->config.pio_threshold = pio_threshold;
2644                info->next_port = ports;
2645                init_waitqueue_head(&info->open_wait);
2646                init_waitqueue_head(&info->close_wait);
2647                init_waitqueue_head(&info->delta_msr_wait);
2648                init_waitqueue_head(&info->break_wait);
2649                ports = info;
2650                printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
2651                        info->line, info->port, info->irq);
2652
2653                if (info->line % 8) {
2654                        printk("secondary port\n");
2655                        /* 8 port cards can't do DMA */
2656                        info->stat_flags |= ESP_STAT_NEVER_DMA;
2657
2658                        if (last_primary)
2659                                last_primary->stat_flags |= ESP_STAT_NEVER_DMA;
2660                } else {
2661                        printk("primary port\n");
2662                        last_primary = info;
2663                        irq[i] = info->irq;
2664                }
2665
2666                if (!dma)
2667                        info->stat_flags |= ESP_STAT_NEVER_DMA;
2668
2669                info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2670                if (!info)
2671                {
2672                        printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n"); 
2673
2674                        /* allow use of the already detected ports */
2675                        return 0;
2676                }
2677
2678                memset((void *)info, 0, sizeof(struct esp_struct));
2679                /* rx_trigger, tx_trigger are needed by autoconfig */
2680                info->config.rx_trigger = rx_trigger;
2681                info->config.tx_trigger = tx_trigger;
2682
2683                if (offset == 56) {
2684                        i++;
2685                        offset = 0;
2686                } else {
2687                        offset += 8;
2688                }
2689        } while (i < NR_PRIMARY);
2690
2691        /* free the last port memory allocation */
2692        kfree(info);
2693
2694        return 0;
2695}
2696
2697static void __exit espserial_exit(void) 
2698{
2699        unsigned long flags;
2700        int e1, e2;
2701        unsigned int region_start, region_end;
2702        struct esp_struct *temp_async;
2703        struct esp_pio_buffer *pio_buf;
2704
2705        /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2706        save_flags(flags);
2707        cli();
2708        remove_bh(ESP_BH);
2709        if ((e1 = tty_unregister_driver(&esp_driver)))
2710                printk("SERIAL: failed to unregister serial driver (%d)\n",
2711                       e1);
2712        if ((e2 = tty_unregister_driver(&esp_callout_driver)))
2713                printk("SERIAL: failed to unregister callout driver (%d)\n", 
2714                       e2);
2715        restore_flags(flags);
2716
2717        while (ports) {
2718                if (ports->port) {
2719                        region_start = region_end = ports->port;
2720                        temp_async = ports;
2721
2722                        while (temp_async) {
2723                                if ((region_start - temp_async->port) == 8) {
2724                                        region_start = temp_async->port;
2725                                        temp_async->port = 0;
2726                                        temp_async = ports;
2727                                } else if ((temp_async->port - region_end)
2728                                           == 8) {
2729                                        region_end = temp_async->port;
2730                                        temp_async->port = 0;
2731                                        temp_async = ports;
2732                                } else
2733                                        temp_async = temp_async->next_port;
2734                        }
2735                        
2736                        release_region(region_start,
2737                                       region_end - region_start + 8);
2738                }
2739
2740                temp_async = ports->next_port;
2741                kfree(ports);
2742                ports = temp_async;
2743        }
2744
2745        if (dma_buffer)
2746                free_pages((unsigned long)dma_buffer,
2747                        get_order(DMA_BUFFER_SZ));
2748
2749        if (tmp_buf)
2750                free_page((unsigned long)tmp_buf);
2751
2752        while (free_pio_buf) {
2753                pio_buf = free_pio_buf->next;
2754                kfree(free_pio_buf);
2755                free_pio_buf = pio_buf;
2756        }
2757}
2758
2759module_init(espserial_init);
2760module_exit(espserial_exit);
2761
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.