linux/drivers/staging/dgap/dgap.c
<<
>>
Prefs
   1/*
   2 * Copyright 2003 Digi International (www.digi.com)
   3 *      Scott H Kilau <Scott_Kilau at digi dot com>
   4 *
   5 * This program is free software; you can redistribute it and/or modify
   6 * it under the terms of the GNU General Public License as published by
   7 * the Free Software Foundation; either version 2, or (at your option)
   8 * any later version.
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
  12 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13 * PURPOSE.  See the GNU General Public License for more details.
  14 *
  15 */
  16
  17/*
  18 *      In the original out of kernel Digi dgap driver, firmware
  19 *      loading was done via user land to driver handshaking.
  20 *
  21 *      For cards that support a concentrator (port expander),
  22 *      I believe the concentrator its self told the card which
  23 *      concentrator is actually attached and then that info
  24 *      was used to tell user land which concentrator firmware
  25 *      image was to be downloaded. I think even the BIOS or
  26 *      FEP images required could change with the connection
  27 *      of a particular concentrator.
  28 *
  29 *      Since I have no access to any of these cards or
  30 *      concentrators, I cannot put the correct concentrator
  31 *      firmware file names into the firmware_info structure
  32 *      as is now done for the BIOS and FEP images.
  33 *
  34 *      I think, but am not certain, that the cards supporting
  35 *      concentrators will function without them. So support
  36 *      of these cards has been left in this driver.
  37 *
  38 *      In order to fully support those cards, they would
  39 *      either have to be acquired for dissection or maybe
  40 *      Digi International could provide some assistance.
  41 */
  42#undef DIGI_CONCENTRATORS_SUPPORTED
  43
  44#define pr_fmt(fmt) "dgap: " fmt
  45
  46#include <linux/kernel.h>
  47#include <linux/module.h>
  48#include <linux/pci.h>
  49#include <linux/delay.h>        /* For udelay */
  50#include <linux/slab.h>
  51#include <linux/uaccess.h>
  52#include <linux/sched.h>
  53
  54#include <linux/interrupt.h>    /* For tasklet and interrupt structs/defines */
  55#include <linux/ctype.h>
  56#include <linux/tty.h>
  57#include <linux/tty_flip.h>
  58#include <linux/serial_reg.h>
  59#include <linux/io.h>           /* For read[bwl]/write[bwl] */
  60
  61#include <linux/string.h>
  62#include <linux/device.h>
  63#include <linux/kdev_t.h>
  64#include <linux/firmware.h>
  65
  66#include "dgap.h"
  67
  68/*
  69 * File operations permitted on Control/Management major.
  70 */
  71static const struct file_operations dgap_board_fops = {
  72        .owner  = THIS_MODULE,
  73};
  74
  75static uint dgap_numboards;
  76static struct board_t *dgap_board[MAXBOARDS];
  77static ulong dgap_poll_counter;
  78static int dgap_driver_state = DRIVER_INITIALIZED;
  79static int dgap_poll_tick = 20; /* Poll interval - 20 ms */
  80
  81static struct class *dgap_class;
  82
  83static uint dgap_count = 500;
  84
  85/*
  86 * Poller stuff
  87 */
  88static DEFINE_SPINLOCK(dgap_poll_lock); /* Poll scheduling lock */
  89static ulong dgap_poll_time;            /* Time of next poll */
  90static uint dgap_poll_stop;             /* Used to tell poller to stop */
  91static struct timer_list dgap_poll_timer;
  92
  93/*
  94     SUPPORTED PRODUCTS
  95
  96     Card Model               Number of Ports      Interface
  97     ----------------------------------------------------------------
  98     Acceleport Xem           4 - 64              (EIA232 & EIA422)
  99     Acceleport Xr            4 & 8               (EIA232)
 100     Acceleport Xr 920        4 & 8               (EIA232)
 101     Acceleport C/X           8 - 128             (EIA232)
 102     Acceleport EPC/X         8 - 224             (EIA232)
 103     Acceleport Xr/422        4 & 8               (EIA422)
 104     Acceleport 2r/920        2                   (EIA232)
 105     Acceleport 4r/920        4                   (EIA232)
 106     Acceleport 8r/920        8                   (EIA232)
 107
 108     IBM 8-Port Asynchronous PCI Adapter          (EIA232)
 109     IBM 128-Port Asynchronous PCI Adapter        (EIA232 & EIA422)
 110*/
 111
 112static struct pci_device_id dgap_pci_tbl[] = {
 113        { DIGI_VID, PCI_DEV_XEM_DID,      PCI_ANY_ID, PCI_ANY_ID, 0, 0,  0 },
 114        { DIGI_VID, PCI_DEV_CX_DID,       PCI_ANY_ID, PCI_ANY_ID, 0, 0,  1 },
 115        { DIGI_VID, PCI_DEV_CX_IBM_DID,   PCI_ANY_ID, PCI_ANY_ID, 0, 0,  2 },
 116        { DIGI_VID, PCI_DEV_EPCJ_DID,     PCI_ANY_ID, PCI_ANY_ID, 0, 0,  3 },
 117        { DIGI_VID, PCI_DEV_920_2_DID,    PCI_ANY_ID, PCI_ANY_ID, 0, 0,  4 },
 118        { DIGI_VID, PCI_DEV_920_4_DID,    PCI_ANY_ID, PCI_ANY_ID, 0, 0,  5 },
 119        { DIGI_VID, PCI_DEV_920_8_DID,    PCI_ANY_ID, PCI_ANY_ID, 0, 0,  6 },
 120        { DIGI_VID, PCI_DEV_XR_DID,       PCI_ANY_ID, PCI_ANY_ID, 0, 0,  7 },
 121        { DIGI_VID, PCI_DEV_XRJ_DID,      PCI_ANY_ID, PCI_ANY_ID, 0, 0,  8 },
 122        { DIGI_VID, PCI_DEV_XR_422_DID,   PCI_ANY_ID, PCI_ANY_ID, 0, 0,  9 },
 123        { DIGI_VID, PCI_DEV_XR_IBM_DID,   PCI_ANY_ID, PCI_ANY_ID, 0, 0, 10 },
 124        { DIGI_VID, PCI_DEV_XR_SAIP_DID,  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 11 },
 125        { DIGI_VID, PCI_DEV_XR_BULL_DID,  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 12 },
 126        { DIGI_VID, PCI_DEV_920_8_HP_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 13 },
 127        { DIGI_VID, PCI_DEV_XEM_HP_DID,   PCI_ANY_ID, PCI_ANY_ID, 0, 0, 14 },
 128        {0,}                                    /* 0 terminated list. */
 129};
 130MODULE_DEVICE_TABLE(pci, dgap_pci_tbl);
 131
 132/*
 133 * A generic list of Product names, PCI Vendor ID, and PCI Device ID.
 134 */
 135struct board_id {
 136        uint config_type;
 137        u8 *name;
 138        uint maxports;
 139        uint dpatype;
 140};
 141
 142static struct board_id dgap_ids[] = {
 143        { PPCM,        PCI_DEV_XEM_NAME,     64, (T_PCXM|T_PCLITE|T_PCIBUS) },
 144        { PCX,         PCI_DEV_CX_NAME,     128, (T_CX|T_PCIBUS)            },
 145        { PCX,         PCI_DEV_CX_IBM_NAME, 128, (T_CX|T_PCIBUS)            },
 146        { PEPC,        PCI_DEV_EPCJ_NAME,   224, (T_EPC|T_PCIBUS)           },
 147        { APORT2_920P, PCI_DEV_920_2_NAME,    2, (T_PCXR|T_PCLITE|T_PCIBUS) },
 148        { APORT4_920P, PCI_DEV_920_4_NAME,    4, (T_PCXR|T_PCLITE|T_PCIBUS) },
 149        { APORT8_920P, PCI_DEV_920_8_NAME,    8, (T_PCXR|T_PCLITE|T_PCIBUS) },
 150        { PAPORT8,     PCI_DEV_XR_NAME,       8, (T_PCXR|T_PCLITE|T_PCIBUS) },
 151        { PAPORT8,     PCI_DEV_XRJ_NAME,      8, (T_PCXR|T_PCLITE|T_PCIBUS) },
 152        { PAPORT8,     PCI_DEV_XR_422_NAME,   8, (T_PCXR|T_PCLITE|T_PCIBUS) },
 153        { PAPORT8,     PCI_DEV_XR_IBM_NAME,   8, (T_PCXR|T_PCLITE|T_PCIBUS) },
 154        { PAPORT8,     PCI_DEV_XR_SAIP_NAME,  8, (T_PCXR|T_PCLITE|T_PCIBUS) },
 155        { PAPORT8,     PCI_DEV_XR_BULL_NAME,  8, (T_PCXR|T_PCLITE|T_PCIBUS) },
 156        { APORT8_920P, PCI_DEV_920_8_HP_NAME, 8, (T_PCXR|T_PCLITE|T_PCIBUS) },
 157        { PPCM,        PCI_DEV_XEM_HP_NAME,  64, (T_PCXM|T_PCLITE|T_PCIBUS) },
 158        {0,}                                            /* 0 terminated list. */
 159};
 160
 161struct firmware_info {
 162        u8 *conf_name;  /* dgap.conf */
 163        u8 *bios_name;  /* BIOS filename */
 164        u8 *fep_name;   /* FEP  filename */
 165        u8 *con_name;   /* Concentrator filename  FIXME*/
 166        int num;        /* sequence number */
 167};
 168
 169/*
 170 * Firmware - BIOS, FEP, and CONC filenames
 171 */
 172static struct firmware_info fw_info[] = {
 173        { "dgap/dgap.conf", "dgap/sxbios.bin",  "dgap/sxfep.bin",  NULL, 0 },
 174        { "dgap/dgap.conf", "dgap/cxpbios.bin", "dgap/cxpfep.bin", NULL, 1 },
 175        { "dgap/dgap.conf", "dgap/cxpbios.bin", "dgap/cxpfep.bin", NULL, 2 },
 176        { "dgap/dgap.conf", "dgap/pcibios.bin", "dgap/pcifep.bin", NULL, 3 },
 177        { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  NULL, 4 },
 178        { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  NULL, 5 },
 179        { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  NULL, 6 },
 180        { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  NULL, 7 },
 181        { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  NULL, 8 },
 182        { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  NULL, 9 },
 183        { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  NULL, 10 },
 184        { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  NULL, 11 },
 185        { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  NULL, 12 },
 186        { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  NULL, 13 },
 187        { "dgap/dgap.conf", "dgap/sxbios.bin",  "dgap/sxfep.bin",  NULL, 14 },
 188        {NULL,}
 189};
 190
 191/*
 192 * Default transparent print information.
 193 */
 194static struct digi_t dgap_digi_init = {
 195        .digi_flags =   DIGI_COOK,      /* Flags                        */
 196        .digi_maxcps =  100,            /* Max CPS                      */
 197        .digi_maxchar = 50,             /* Max chars in print queue     */
 198        .digi_bufsize = 100,            /* Printer buffer size          */
 199        .digi_onlen =   4,              /* size of printer on string    */
 200        .digi_offlen =  4,              /* size of printer off string   */
 201        .digi_onstr =   "\033[5i",      /* ANSI printer on string ]     */
 202        .digi_offstr =  "\033[4i",      /* ANSI printer off string ]    */
 203        .digi_term =    "ansi"          /* default terminal type        */
 204};
 205
 206/*
 207 * Define a local default termios struct. All ports will be created
 208 * with this termios initially.
 209 *
 210 * This defines a raw port at 9600 baud, 8 data bits, no parity,
 211 * 1 stop bit.
 212 */
 213
 214static struct ktermios dgap_default_termios = {
 215        .c_iflag =      (DEFAULT_IFLAGS),       /* iflags */
 216        .c_oflag =      (DEFAULT_OFLAGS),       /* oflags */
 217        .c_cflag =      (DEFAULT_CFLAGS),       /* cflags */
 218        .c_lflag =      (DEFAULT_LFLAGS),       /* lflags */
 219        .c_cc =         INIT_C_CC,
 220        .c_line =       0,
 221};
 222
 223/*
 224 * Our needed internal static variables from dgap_parse.c
 225 */
 226static struct cnode dgap_head;
 227#define MAXCWORD 200
 228static char dgap_cword[MAXCWORD];
 229
 230struct toklist {
 231        int token;
 232        char *string;
 233};
 234
 235static struct toklist dgap_brdtype[] = {
 236        { PCX,          "Digi_AccelePort_C/X_PCI" },
 237        { PEPC,         "Digi_AccelePort_EPC/X_PCI" },
 238        { PPCM,         "Digi_AccelePort_Xem_PCI" },
 239        { APORT2_920P,  "Digi_AccelePort_2r_920_PCI" },
 240        { APORT4_920P,  "Digi_AccelePort_4r_920_PCI" },
 241        { APORT8_920P,  "Digi_AccelePort_8r_920_PCI" },
 242        { PAPORT4,      "Digi_AccelePort_4r_PCI(EIA-232/RS-422)" },
 243        { PAPORT8,      "Digi_AccelePort_8r_PCI(EIA-232/RS-422)" },
 244        { 0, NULL }
 245};
 246
 247static struct toklist dgap_tlist[] = {
 248        { BEGIN,        "config_begin" },
 249        { END,          "config_end" },
 250        { BOARD,        "board" },
 251        { IO,           "io" },
 252        { PCIINFO,      "pciinfo" },
 253        { LINE,         "line" },
 254        { CONC,         "conc" },
 255        { CONC,         "concentrator" },
 256        { CX,           "cx" },
 257        { CX,           "ccon" },
 258        { EPC,          "epccon" },
 259        { EPC,          "epc" },
 260        { MOD,          "module" },
 261        { ID,           "id" },
 262        { STARTO,       "start" },
 263        { SPEED,        "speed" },
 264        { CABLE,        "cable" },
 265        { CONNECT,      "connect" },
 266        { METHOD,       "method" },
 267        { STATUS,       "status" },
 268        { CUSTOM,       "Custom" },
 269        { BASIC,        "Basic" },
 270        { MEM,          "mem" },
 271        { MEM,          "memory" },
 272        { PORTS,        "ports" },
 273        { MODEM,        "modem" },
 274        { NPORTS,       "nports" },
 275        { TTYN,         "ttyname" },
 276        { CU,           "cuname" },
 277        { PRINT,        "prname" },
 278        { CMAJOR,       "major"  },
 279        { ALTPIN,       "altpin" },
 280        { USEINTR,      "useintr" },
 281        { TTSIZ,        "ttysize" },
 282        { CHSIZ,        "chsize" },
 283        { BSSIZ,        "boardsize" },
 284        { UNTSIZ,       "schedsize" },
 285        { F2SIZ,        "f2200size" },
 286        { VPSIZ,        "vpixsize" },
 287        { 0,            NULL }
 288};
 289
 290
 291/*
 292 * dgap_sindex: much like index(), but it looks for a match of any character in
 293 * the group, and returns that position.  If the first character is a ^, then
 294 * this will match the first occurrence not in that group.
 295 */
 296static char *dgap_sindex(char *string, char *group)
 297{
 298        char *ptr;
 299
 300        if (!string || !group)
 301                return NULL;
 302
 303        if (*group == '^') {
 304                group++;
 305                for (; *string; string++) {
 306                        for (ptr = group; *ptr; ptr++) {
 307                                if (*ptr == *string)
 308                                        break;
 309                        }
 310                        if (*ptr == '\0')
 311                                return string;
 312                }
 313        } else {
 314                for (; *string; string++) {
 315                        for (ptr = group; *ptr; ptr++) {
 316                                if (*ptr == *string)
 317                                        return string;
 318                        }
 319                }
 320        }
 321
 322        return NULL;
 323}
 324
 325/*
 326 * get a word from the input stream, also keep track of current line number.
 327 * words are separated by whitespace.
 328 */
 329static char *dgap_getword(char **in)
 330{
 331        char *ret_ptr = *in;
 332
 333        char *ptr = dgap_sindex(*in, " \t\n");
 334
 335        /* If no word found, return null */
 336        if (!ptr)
 337                return NULL;
 338
 339        /* Mark new location for our buffer */
 340        *ptr = '\0';
 341        *in = ptr + 1;
 342
 343        /* Eat any extra spaces/tabs/newlines that might be present */
 344        while (*in && **in && ((**in == ' ') ||
 345                               (**in == '\t') ||
 346                               (**in == '\n'))) {
 347                **in = '\0';
 348                *in = *in + 1;
 349        }
 350
 351        return ret_ptr;
 352}
 353
 354
 355/*
 356 * Get a token from the input file; return 0 if end of file is reached
 357 */
 358static int dgap_gettok(char **in)
 359{
 360        char *w;
 361        struct toklist *t;
 362
 363        if (strstr(dgap_cword, "board")) {
 364                w = dgap_getword(in);
 365                snprintf(dgap_cword, MAXCWORD, "%s", w);
 366                for (t = dgap_brdtype; t->token != 0; t++) {
 367                        if (!strcmp(w, t->string))
 368                                return t->token;
 369                }
 370        } else {
 371                while ((w = dgap_getword(in))) {
 372                        snprintf(dgap_cword, MAXCWORD, "%s", w);
 373                        for (t = dgap_tlist; t->token != 0; t++) {
 374                                if (!strcmp(w, t->string))
 375                                        return t->token;
 376                        }
 377                }
 378        }
 379
 380        return 0;
 381}
 382
 383/*
 384 * dgap_checknode: see if all the necessary info has been supplied for a node
 385 * before creating the next node.
 386 */
 387static int dgap_checknode(struct cnode *p)
 388{
 389        switch (p->type) {
 390        case LNODE:
 391                if (p->u.line.v_speed == 0) {
 392                        pr_err("line speed not specified");
 393                        return 1;
 394                }
 395                return 0;
 396
 397        case CNODE:
 398                if (p->u.conc.v_speed == 0) {
 399                        pr_err("concentrator line speed not specified");
 400                        return 1;
 401                }
 402                if (p->u.conc.v_nport == 0) {
 403                        pr_err("number of ports on concentrator not specified");
 404                        return 1;
 405                }
 406                if (p->u.conc.v_id == 0) {
 407                        pr_err("concentrator id letter not specified");
 408                        return 1;
 409                }
 410                return 0;
 411
 412        case MNODE:
 413                if (p->u.module.v_nport == 0) {
 414                        pr_err("number of ports on EBI module not specified");
 415                        return 1;
 416                }
 417                if (p->u.module.v_id == 0) {
 418                        pr_err("EBI module id letter not specified");
 419                        return 1;
 420                }
 421                return 0;
 422        }
 423        return 0;
 424}
 425
 426/*
 427 * Given a board pointer, returns whether we should use interrupts or not.
 428 */
 429static uint dgap_config_get_useintr(struct board_t *bd)
 430{
 431        struct cnode *p;
 432
 433        if (!bd)
 434                return 0;
 435
 436        for (p = bd->bd_config; p; p = p->next) {
 437                if (p->type == INTRNODE) {
 438                        /*
 439                         * check for pcxr types.
 440                         */
 441                        return p->u.useintr;
 442                }
 443        }
 444
 445        /* If not found, then don't turn on interrupts. */
 446        return 0;
 447}
 448
 449/*
 450 * Given a board pointer, returns whether we turn on altpin or not.
 451 */
 452static uint dgap_config_get_altpin(struct board_t *bd)
 453{
 454        struct cnode *p;
 455
 456        if (!bd)
 457                return 0;
 458
 459        for (p = bd->bd_config; p; p = p->next) {
 460                if (p->type == ANODE) {
 461                        /*
 462                         * check for pcxr types.
 463                         */
 464                        return p->u.altpin;
 465                }
 466        }
 467
 468        /* If not found, then don't turn on interrupts. */
 469        return 0;
 470}
 471
 472/*
 473 * Given a specific type of board, if found, detached link and
 474 * returns the first occurrence in the list.
 475 */
 476static struct cnode *dgap_find_config(int type, int bus, int slot)
 477{
 478        struct cnode *p, *prev, *prev2, *found;
 479
 480        p = &dgap_head;
 481
 482        while (p->next) {
 483                prev = p;
 484                p = p->next;
 485
 486                if (p->type != BNODE)
 487                        continue;
 488
 489                if (p->u.board.type != type)
 490                        continue;
 491
 492                if (p->u.board.v_pcibus &&
 493                    p->u.board.pcibus != bus)
 494                        continue;
 495
 496                if (p->u.board.v_pcislot &&
 497                    p->u.board.pcislot != slot)
 498                        continue;
 499
 500                found = p;
 501                /*
 502                 * Keep walking thru the list till we
 503                 * find the next board.
 504                 */
 505                while (p->next) {
 506                        prev2 = p;
 507                        p = p->next;
 508
 509                        if (p->type != BNODE)
 510                                continue;
 511
 512                        /*
 513                         * Mark the end of our 1 board
 514                         * chain of configs.
 515                         */
 516                        prev2->next = NULL;
 517
 518                        /*
 519                         * Link the "next" board to the
 520                         * previous board, effectively
 521                         * "unlinking" our board from
 522                         * the main config.
 523                         */
 524                        prev->next = p;
 525
 526                        return found;
 527                }
 528                /*
 529                 * It must be the last board in the list.
 530                 */
 531                prev->next = NULL;
 532                return found;
 533        }
 534        return NULL;
 535}
 536
 537/*
 538 * Given a board pointer, walks the config link, counting up
 539 * all ports user specified should be on the board.
 540 * (This does NOT mean they are all actually present right now tho)
 541 */
 542static uint dgap_config_get_num_prts(struct board_t *bd)
 543{
 544        int count = 0;
 545        struct cnode *p;
 546
 547        if (!bd)
 548                return 0;
 549
 550        for (p = bd->bd_config; p; p = p->next) {
 551
 552                switch (p->type) {
 553                case BNODE:
 554                        /*
 555                         * check for pcxr types.
 556                         */
 557                        if (p->u.board.type > EPCFE)
 558                                count += p->u.board.nport;
 559                        break;
 560                case CNODE:
 561                        count += p->u.conc.nport;
 562                        break;
 563                case MNODE:
 564                        count += p->u.module.nport;
 565                        break;
 566                }
 567        }
 568        return count;
 569}
 570
 571static char *dgap_create_config_string(struct board_t *bd, char *string)
 572{
 573        char *ptr = string;
 574        struct cnode *p;
 575        struct cnode *q;
 576        int speed;
 577
 578        if (!bd) {
 579                *ptr = 0xff;
 580                return string;
 581        }
 582
 583        for (p = bd->bd_config; p; p = p->next) {
 584
 585                switch (p->type) {
 586                case LNODE:
 587                        *ptr = '\0';
 588                        ptr++;
 589                        *ptr = p->u.line.speed;
 590                        ptr++;
 591                        break;
 592                case CNODE:
 593                        /*
 594                         * Because the EPC/con concentrators can have EM modules
 595                         * hanging off of them, we have to walk ahead in the
 596                         * list and keep adding the number of ports on each EM
 597                         * to the config. UGH!
 598                         */
 599                        speed = p->u.conc.speed;
 600                        q = p->next;
 601                        if (q && (q->type == MNODE)) {
 602                                *ptr = (p->u.conc.nport + 0x80);
 603                                ptr++;
 604                                p = q;
 605                                while (q->next && (q->next->type) == MNODE) {
 606                                        *ptr = (q->u.module.nport + 0x80);
 607                                        ptr++;
 608                                        p = q;
 609                                        q = q->next;
 610                                }
 611                                *ptr = q->u.module.nport;
 612                                ptr++;
 613                        } else {
 614                                *ptr = p->u.conc.nport;
 615                                ptr++;
 616                        }
 617
 618                        *ptr = speed;
 619                        ptr++;
 620                        break;
 621                }
 622        }
 623
 624        *ptr = 0xff;
 625        return string;
 626}
 627
 628/*
 629 * Parse a configuration file read into memory as a string.
 630 */
 631static int dgap_parsefile(char **in)
 632{
 633        struct cnode *p, *brd, *line, *conc;
 634        int rc;
 635        char *s;
 636        int linecnt = 0;
 637
 638        p = &dgap_head;
 639        brd = line = conc = NULL;
 640
 641        /* perhaps we are adding to an existing list? */
 642        while (p->next)
 643                p = p->next;
 644
 645        /* file must start with a BEGIN */
 646        while ((rc = dgap_gettok(in)) != BEGIN) {
 647                if (rc == 0) {
 648                        pr_err("unexpected EOF");
 649                        return -1;
 650                }
 651        }
 652
 653        for (; ;) {
 654                int board_type = 0;
 655                int conc_type = 0;
 656                int module_type = 0;
 657
 658                rc = dgap_gettok(in);
 659                if (rc == 0) {
 660                        pr_err("unexpected EOF");
 661                        return -1;
 662                }
 663
 664                switch (rc) {
 665                case BEGIN:     /* should only be 1 begin */
 666                        pr_err("unexpected config_begin\n");
 667                        return -1;
 668
 669                case END:
 670                        return 0;
 671
 672                case BOARD:     /* board info */
 673                        if (dgap_checknode(p))
 674                                return -1;
 675
 676                        p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
 677                        if (!p->next)
 678                                return -1;
 679
 680                        p = p->next;
 681
 682                        p->type = BNODE;
 683                        p->u.board.status = kstrdup("No", GFP_KERNEL);
 684                        line = conc = NULL;
 685                        brd = p;
 686                        linecnt = -1;
 687
 688                        board_type = dgap_gettok(in);
 689                        if (board_type == 0) {
 690                                pr_err("board !!type not specified");
 691                                return -1;
 692                        }
 693
 694                        p->u.board.type = board_type;
 695
 696                        break;
 697
 698                case IO:        /* i/o port */
 699                        if (p->type != BNODE) {
 700                                pr_err("IO port only valid for boards");
 701                                return -1;
 702                        }
 703                        s = dgap_getword(in);
 704                        if (!s) {
 705                                pr_err("unexpected end of file");
 706                                return -1;
 707                        }
 708                        p->u.board.portstr = kstrdup(s, GFP_KERNEL);
 709                        if (kstrtol(s, 0, &p->u.board.port)) {
 710                                pr_err("bad number for IO port");
 711                                return -1;
 712                        }
 713                        p->u.board.v_port = 1;
 714                        break;
 715
 716                case MEM:       /* memory address */
 717                        if (p->type != BNODE) {
 718                                pr_err("memory address only valid for boards");
 719                                return -1;
 720                        }
 721                        s = dgap_getword(in);
 722                        if (!s) {
 723                                pr_err("unexpected end of file");
 724                                return -1;
 725                        }
 726                        p->u.board.addrstr = kstrdup(s, GFP_KERNEL);
 727                        if (kstrtoul(s, 0, &p->u.board.addr)) {
 728                                pr_err("bad number for memory address");
 729                                return -1;
 730                        }
 731                        p->u.board.v_addr = 1;
 732                        break;
 733
 734                case PCIINFO:   /* pci information */
 735                        if (p->type != BNODE) {
 736                                pr_err("memory address only valid for boards");
 737                                return -1;
 738                        }
 739                        s = dgap_getword(in);
 740                        if (!s) {
 741                                pr_err("unexpected end of file");
 742                                return -1;
 743                        }
 744                        p->u.board.pcibusstr = kstrdup(s, GFP_KERNEL);
 745                        if (kstrtoul(s, 0, &p->u.board.pcibus)) {
 746                                pr_err("bad number for pci bus");
 747                                return -1;
 748                        }
 749                        p->u.board.v_pcibus = 1;
 750                        s = dgap_getword(in);
 751                        if (!s) {
 752                                pr_err("unexpected end of file");
 753                                return -1;
 754                        }
 755                        p->u.board.pcislotstr = kstrdup(s, GFP_KERNEL);
 756                        if (kstrtoul(s, 0, &p->u.board.pcislot)) {
 757                                pr_err("bad number for pci slot");
 758                                return -1;
 759                        }
 760                        p->u.board.v_pcislot = 1;
 761                        break;
 762
 763                case METHOD:
 764                        if (p->type != BNODE) {
 765                                pr_err("install method only valid for boards");
 766                                return -1;
 767                        }
 768                        s = dgap_getword(in);
 769                        if (!s) {
 770                                pr_err("unexpected end of file");
 771                                return -1;
 772                        }
 773                        p->u.board.method = kstrdup(s, GFP_KERNEL);
 774                        p->u.board.v_method = 1;
 775                        break;
 776
 777                case STATUS:
 778                        if (p->type != BNODE) {
 779                                pr_err("config status only valid for boards");
 780                                return -1;
 781                        }
 782                        s = dgap_getword(in);
 783                        if (!s) {
 784                                pr_err("unexpected end of file");
 785                                return -1;
 786                        }
 787                        p->u.board.status = kstrdup(s, GFP_KERNEL);
 788                        break;
 789
 790                case NPORTS:    /* number of ports */
 791                        if (p->type == BNODE) {
 792                                s = dgap_getword(in);
 793                                if (!s) {
 794                                        pr_err("unexpected end of file");
 795                                        return -1;
 796                                }
 797                                if (kstrtol(s, 0, &p->u.board.nport)) {
 798                                        pr_err("bad number for number of ports");
 799                                        return -1;
 800                                }
 801                                p->u.board.v_nport = 1;
 802                        } else if (p->type == CNODE) {
 803                                s = dgap_getword(in);
 804                                if (!s) {
 805                                        pr_err("unexpected end of file");
 806                                        return -1;
 807                                }
 808                                if (kstrtol(s, 0, &p->u.conc.nport)) {
 809                                        pr_err("bad number for number of ports");
 810                                        return -1;
 811                                }
 812                                p->u.conc.v_nport = 1;
 813                        } else if (p->type == MNODE) {
 814                                s = dgap_getword(in);
 815                                if (!s) {
 816                                        pr_err("unexpected end of file");
 817                                        return -1;
 818                                }
 819                                if (kstrtol(s, 0, &p->u.module.nport)) {
 820                                        pr_err("bad number for number of ports");
 821                                        return -1;
 822                                }
 823                                p->u.module.v_nport = 1;
 824                        } else {
 825                                pr_err("nports only valid for concentrators or modules");
 826                                return -1;
 827                        }
 828                        break;
 829
 830                case ID:        /* letter ID used in tty name */
 831                        s = dgap_getword(in);
 832                        if (!s) {
 833                                pr_err("unexpected end of file");
 834                                return -1;
 835                        }
 836
 837                        p->u.board.status = kstrdup(s, GFP_KERNEL);
 838
 839                        if (p->type == CNODE) {
 840                                p->u.conc.id = kstrdup(s, GFP_KERNEL);
 841                                p->u.conc.v_id = 1;
 842                        } else if (p->type == MNODE) {
 843                                p->u.module.id = kstrdup(s, GFP_KERNEL);
 844                                p->u.module.v_id = 1;
 845                        } else {
 846                                pr_err("id only valid for concentrators or modules");
 847                                return -1;
 848                        }
 849                        break;
 850
 851                case STARTO:    /* start offset of ID */
 852                        if (p->type == BNODE) {
 853                                s = dgap_getword(in);
 854                                if (!s) {
 855                                        pr_err("unexpected end of file");
 856                                        return -1;
 857                                }
 858                                if (kstrtol(s, 0, &p->u.board.start)) {
 859                                        pr_err("bad number for start of tty count");
 860                                        return -1;
 861                                }
 862                                p->u.board.v_start = 1;
 863                        } else if (p->type == CNODE) {
 864                                s = dgap_getword(in);
 865                                if (!s) {
 866                                        pr_err("unexpected end of file");
 867                                        return -1;
 868                                }
 869                                if (kstrtol(s, 0, &p->u.conc.start)) {
 870                                        pr_err("bad number for start of tty count");
 871                                        return -1;
 872                                }
 873                                p->u.conc.v_start = 1;
 874                        } else if (p->type == MNODE) {
 875                                s = dgap_getword(in);
 876                                if (!s) {
 877                                        pr_err("unexpected end of file");
 878                                        return -1;
 879                                }
 880                                if (kstrtol(s, 0, &p->u.module.start)) {
 881                                        pr_err("bad number for start of tty count");
 882                                        return -1;
 883                                }
 884                                p->u.module.v_start = 1;
 885                        } else {
 886                                pr_err("start only valid for concentrators or modules");
 887                                return -1;
 888                        }
 889                        break;
 890
 891                case TTYN:      /* tty name prefix */
 892                        if (dgap_checknode(p))
 893                                return -1;
 894
 895                        p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
 896                        if (!p->next)
 897                                return -1;
 898
 899                        p = p->next;
 900                        p->type = TNODE;
 901
 902                        s = dgap_getword(in);
 903                        if (!s) {
 904                                pr_err("unexpeced end of file");
 905                                return -1;
 906                        }
 907                        p->u.ttyname = kstrdup(s, GFP_KERNEL);
 908                        if (!p->u.ttyname)
 909                                return -1;
 910
 911                        break;
 912
 913                case CU:        /* cu name prefix */
 914                        if (dgap_checknode(p))
 915                                return -1;
 916
 917                        p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
 918                        if (!p->next)
 919                                return -1;
 920
 921                        p = p->next;
 922                        p->type = CUNODE;
 923
 924                        s = dgap_getword(in);
 925                        if (!s) {
 926                                pr_err("unexpeced end of file");
 927                                return -1;
 928                        }
 929                        p->u.cuname = kstrdup(s, GFP_KERNEL);
 930                        if (!p->u.cuname)
 931                                return -1;
 932
 933                        break;
 934
 935                case LINE:      /* line information */
 936                        if (dgap_checknode(p))
 937                                return -1;
 938                        if (!brd) {
 939                                pr_err("must specify board before line info");
 940                                return -1;
 941                        }
 942                        switch (brd->u.board.type) {
 943                        case PPCM:
 944                                pr_err("line not valid for PC/em");
 945                                return -1;
 946                        }
 947
 948                        p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
 949                        if (!p->next)
 950                                return -1;
 951
 952                        p = p->next;
 953                        p->type = LNODE;
 954                        conc = NULL;
 955                        line = p;
 956                        linecnt++;
 957                        break;
 958
 959                case CONC:      /* concentrator information */
 960                        if (dgap_checknode(p))
 961                                return -1;
 962                        if (!line) {
 963                                pr_err("must specify line info before concentrator");
 964                                return -1;
 965                        }
 966
 967                        p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
 968                        if (!p->next)
 969                                return -1;
 970
 971                        p = p->next;
 972                        p->type = CNODE;
 973                        conc = p;
 974
 975                        if (linecnt)
 976                                brd->u.board.conc2++;
 977                        else
 978                                brd->u.board.conc1++;
 979
 980                        conc_type = dgap_gettok(in);
 981                        if (conc_type == 0 || conc_type != CX ||
 982                            conc_type != EPC) {
 983                                pr_err("failed to set a type of concentratros");
 984                                return -1;
 985                        }
 986
 987                        p->u.conc.type = conc_type;
 988
 989                        break;
 990
 991                case MOD:       /* EBI module */
 992                        if (dgap_checknode(p))
 993                                return -1;
 994                        if (!brd) {
 995                                pr_err("must specify board info before EBI modules");
 996                                return -1;
 997                        }
 998                        switch (brd->u.board.type) {
 999                        case PPCM:
1000                                linecnt = 0;
1001                                break;
1002                        default:
1003                                if (!conc) {
1004                                        pr_err("must specify concentrator info before EBI module");
1005                                        return -1;
1006                                }
1007                        }
1008
1009                        p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1010                        if (!p->next)
1011                                return -1;
1012
1013                        p = p->next;
1014                        p->type = MNODE;
1015
1016                        if (linecnt)
1017                                brd->u.board.module2++;
1018                        else
1019                                brd->u.board.module1++;
1020
1021                        module_type = dgap_gettok(in);
1022                        if (module_type == 0 || module_type != PORTS ||
1023                            module_type != MODEM) {
1024                                pr_err("failed to set a type of module");
1025                                return -1;
1026                        }
1027
1028                        p->u.module.type = module_type;
1029
1030                        break;
1031
1032                case CABLE:
1033                        if (p->type == LNODE) {
1034                                s = dgap_getword(in);
1035                                if (!s) {
1036                                        pr_err("unexpected end of file");
1037                                        return -1;
1038                                }
1039                                p->u.line.cable = kstrdup(s, GFP_KERNEL);
1040                                p->u.line.v_cable = 1;
1041                        }
1042                        break;
1043
1044                case SPEED:     /* sync line speed indication */
1045                        if (p->type == LNODE) {
1046                                s = dgap_getword(in);
1047                                if (!s) {
1048                                        pr_err("unexpected end of file");
1049                                        return -1;
1050                                }
1051                                if (kstrtol(s, 0, &p->u.line.speed)) {
1052                                        pr_err("bad number for line speed");
1053                                        return -1;
1054                                }
1055                                p->u.line.v_speed = 1;
1056                        } else if (p->type == CNODE) {
1057                                s = dgap_getword(in);
1058                                if (!s) {
1059                                        pr_err("unexpected end of file");
1060                                        return -1;
1061                                }
1062                                if (kstrtol(s, 0, &p->u.conc.speed)) {
1063                                        pr_err("bad number for line speed");
1064                                        return -1;
1065                                }
1066                                p->u.conc.v_speed = 1;
1067                        } else {
1068                                pr_err("speed valid only for lines or concentrators.");
1069                                return -1;
1070                        }
1071                        break;
1072
1073                case CONNECT:
1074                        if (p->type == CNODE) {
1075                                s = dgap_getword(in);
1076                                if (!s) {
1077                                        pr_err("unexpected end of file");
1078                                        return -1;
1079                                }
1080                                p->u.conc.connect = kstrdup(s, GFP_KERNEL);
1081                                p->u.conc.v_connect = 1;
1082                        }
1083                        break;
1084                case PRINT:     /* transparent print name prefix */
1085                        if (dgap_checknode(p))
1086                                return -1;
1087
1088                        p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1089                        if (!p->next)
1090                                return -1;
1091
1092                        p = p->next;
1093                        p->type = PNODE;
1094
1095                        s = dgap_getword(in);
1096                        if (!s) {
1097                                pr_err("unexpeced end of file");
1098                                return -1;
1099                        }
1100                        p->u.printname = kstrdup(s, GFP_KERNEL);
1101                        if (!p->u.printname)
1102                                return -1;
1103
1104                        break;
1105
1106                case CMAJOR:    /* major number */
1107                        if (dgap_checknode(p))
1108                                return -1;
1109
1110                        p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1111                        if (!p->next)
1112                                return -1;
1113
1114                        p = p->next;
1115                        p->type = JNODE;
1116
1117                        s = dgap_getword(in);
1118                        if (!s) {
1119                                pr_err("unexpected end of file");
1120                                return -1;
1121                        }
1122                        if (kstrtol(s, 0, &p->u.majornumber)) {
1123                                pr_err("bad number for major number");
1124                                return -1;
1125                        }
1126                        break;
1127
1128                case ALTPIN:    /* altpin setting */
1129                        if (dgap_checknode(p))
1130                                return -1;
1131
1132                        p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1133                        if (!p->next)
1134                                return -1;
1135
1136                        p = p->next;
1137                        p->type = ANODE;
1138
1139                        s = dgap_getword(in);
1140                        if (!s) {
1141                                pr_err("unexpected end of file");
1142                                return -1;
1143                        }
1144                        if (kstrtol(s, 0, &p->u.altpin)) {
1145                                pr_err("bad number for altpin");
1146                                return -1;
1147                        }
1148                        break;
1149
1150                case USEINTR:           /* enable interrupt setting */
1151                        if (dgap_checknode(p))
1152                                return -1;
1153
1154                        p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1155                        if (!p->next)
1156                                return -1;
1157
1158                        p = p->next;
1159                        p->type = INTRNODE;
1160                        s = dgap_getword(in);
1161                        if (!s) {
1162                                pr_err("unexpected end of file");
1163                                return -1;
1164                        }
1165                        if (kstrtol(s, 0, &p->u.useintr)) {
1166                                pr_err("bad number for useintr");
1167                                return -1;
1168                        }
1169                        break;
1170
1171                case TTSIZ:     /* size of tty structure */
1172                        if (dgap_checknode(p))
1173                                return -1;
1174
1175                        p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1176                        if (!p->next)
1177                                return -1;
1178
1179                        p = p->next;
1180                        p->type = TSNODE;
1181
1182                        s = dgap_getword(in);
1183                        if (!s) {
1184                                pr_err("unexpected end of file");
1185                                return -1;
1186                        }
1187                        if (kstrtol(s, 0, &p->u.ttysize)) {
1188                                pr_err("bad number for ttysize");
1189                                return -1;
1190                        }
1191                        break;
1192
1193                case CHSIZ:     /* channel structure size */
1194                        if (dgap_checknode(p))
1195                                return -1;
1196
1197                        p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1198                        if (!p->next)
1199                                return -1;
1200
1201                        p = p->next;
1202                        p->type = CSNODE;
1203
1204                        s = dgap_getword(in);
1205                        if (!s) {
1206                                pr_err("unexpected end of file");
1207                                return -1;
1208                        }
1209                        if (kstrtol(s, 0, &p->u.chsize)) {
1210                                pr_err("bad number for chsize");
1211                                return -1;
1212                        }
1213                        break;
1214
1215                case BSSIZ:     /* board structure size */
1216                        if (dgap_checknode(p))
1217                                return -1;
1218
1219                        p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1220                        if (!p->next)
1221                                return -1;
1222
1223                        p = p->next;
1224                        p->type = BSNODE;
1225
1226                        s = dgap_getword(in);
1227                        if (!s) {
1228                                pr_err("unexpected end of file");
1229                                return -1;
1230                        }
1231                        if (kstrtol(s, 0, &p->u.bssize)) {
1232                                pr_err("bad number for bssize");
1233                                return -1;
1234                        }
1235                        break;
1236
1237                case UNTSIZ:    /* sched structure size */
1238                        if (dgap_checknode(p))
1239                                return -1;
1240
1241                        p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1242                        if (!p->next)
1243                                return -1;
1244
1245                        p = p->next;
1246                        p->type = USNODE;
1247
1248                        s = dgap_getword(in);
1249                        if (!s) {
1250                                pr_err("unexpected end of file");
1251                                return -1;
1252                        }
1253                        if (kstrtol(s, 0, &p->u.unsize)) {
1254                                pr_err("bad number for schedsize");
1255                                return -1;
1256                        }
1257                        break;
1258
1259                case F2SIZ:     /* f2200 structure size */
1260                        if (dgap_checknode(p))
1261                                return -1;
1262
1263                        p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1264                        if (!p->next)
1265                                return -1;
1266
1267                        p = p->next;
1268                        p->type = FSNODE;
1269
1270                        s = dgap_getword(in);
1271                        if (!s) {
1272                                pr_err("unexpected end of file");
1273                                return -1;
1274                        }
1275                        if (kstrtol(s, 0, &p->u.f2size)) {
1276                                pr_err("bad number for f2200size");
1277                                return -1;
1278                        }
1279                        break;
1280
1281                case VPSIZ:     /* vpix structure size */
1282                        if (dgap_checknode(p))
1283                                return -1;
1284
1285                        p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1286                        if (!p->next)
1287                                return -1;
1288
1289                        p = p->next;
1290                        p->type = VSNODE;
1291
1292                        s = dgap_getword(in);
1293                        if (!s) {
1294                                pr_err("unexpected end of file");
1295                                return -1;
1296                        }
1297                        if (kstrtol(s, 0, &p->u.vpixsize)) {
1298                                pr_err("bad number for vpixsize");
1299                                return -1;
1300                        }
1301                        break;
1302                }
1303        }
1304}
1305
1306static void dgap_cleanup_nodes(void)
1307{
1308        struct cnode *p;
1309
1310        p = &dgap_head;
1311
1312        while (p) {
1313                struct cnode *tmp = p->next;
1314
1315                if (p->type == NULLNODE) {
1316                        p = tmp;
1317                        continue;
1318                }
1319
1320                switch (p->type) {
1321                case BNODE:
1322                        kfree(p->u.board.portstr);
1323                        kfree(p->u.board.addrstr);
1324                        kfree(p->u.board.pcibusstr);
1325                        kfree(p->u.board.pcislotstr);
1326                        kfree(p->u.board.method);
1327                        break;
1328                case CNODE:
1329                        kfree(p->u.conc.id);
1330                        kfree(p->u.conc.connect);
1331                        break;
1332                case MNODE:
1333                        kfree(p->u.module.id);
1334                        break;
1335                case TNODE:
1336                        kfree(p->u.ttyname);
1337                        break;
1338                case CUNODE:
1339                        kfree(p->u.cuname);
1340                        break;
1341                case LNODE:
1342                        kfree(p->u.line.cable);
1343                        break;
1344                case PNODE:
1345                        kfree(p->u.printname);
1346                        break;
1347                }
1348
1349                kfree(p->u.board.status);
1350                kfree(p);
1351                p = tmp;
1352        }
1353}
1354
1355/*
1356 * Retrives the current custom baud rate from FEP memory,
1357 * and returns it back to the user.
1358 * Returns 0 on error.
1359 */
1360static uint dgap_get_custom_baud(struct channel_t *ch)
1361{
1362        u8 __iomem *vaddr;
1363        ulong offset;
1364        uint value;
1365
1366        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1367                return 0;
1368
1369        if (!ch->ch_bd || ch->ch_bd->magic != DGAP_BOARD_MAGIC)
1370                return 0;
1371
1372        if (!(ch->ch_bd->bd_flags & BD_FEP5PLUS))
1373                return 0;
1374
1375        vaddr = ch->ch_bd->re_map_membase;
1376
1377        if (!vaddr)
1378                return 0;
1379
1380        /*
1381         * Go get from fep mem, what the fep
1382         * believes the custom baud rate is.
1383         */
1384        offset = (ioread16(vaddr + ECS_SEG) << 4) + (ch->ch_portnum * 0x28)
1385               + LINE_SPEED;
1386
1387        value = readw(vaddr + offset);
1388        return value;
1389}
1390
1391/*
1392 * Remap PCI memory.
1393 */
1394static int dgap_remap(struct board_t *brd)
1395{
1396        if (!brd || brd->magic != DGAP_BOARD_MAGIC)
1397                return -EIO;
1398
1399        if (!request_mem_region(brd->membase, 0x200000, "dgap"))
1400                return -ENOMEM;
1401
1402        if (!request_mem_region(brd->membase + PCI_IO_OFFSET, 0x200000,
1403                                        "dgap")) {
1404                release_mem_region(brd->membase, 0x200000);
1405                return -ENOMEM;
1406        }
1407
1408        brd->re_map_membase = ioremap(brd->membase, 0x200000);
1409        if (!brd->re_map_membase) {
1410                release_mem_region(brd->membase, 0x200000);
1411                release_mem_region(brd->membase + PCI_IO_OFFSET, 0x200000);
1412                return -ENOMEM;
1413        }
1414
1415        brd->re_map_port = ioremap((brd->membase + PCI_IO_OFFSET), 0x200000);
1416        if (!brd->re_map_port) {
1417                release_mem_region(brd->membase, 0x200000);
1418                release_mem_region(brd->membase + PCI_IO_OFFSET, 0x200000);
1419                iounmap(brd->re_map_membase);
1420                return -ENOMEM;
1421        }
1422
1423        return 0;
1424}
1425
1426static void dgap_unmap(struct board_t *brd)
1427{
1428        iounmap(brd->re_map_port);
1429        iounmap(brd->re_map_membase);
1430        release_mem_region(brd->membase + PCI_IO_OFFSET, 0x200000);
1431        release_mem_region(brd->membase, 0x200000);
1432}
1433
1434/*
1435 * dgap_parity_scan()
1436 *
1437 * Convert the FEP5 way of reporting parity errors and breaks into
1438 * the Linux line discipline way.
1439 */
1440static void dgap_parity_scan(struct channel_t *ch, unsigned char *cbuf,
1441                                unsigned char *fbuf, int *len)
1442{
1443        int l = *len;
1444        int count = 0;
1445        unsigned char *in, *cout, *fout;
1446        unsigned char c;
1447
1448        in = cbuf;
1449        cout = cbuf;
1450        fout = fbuf;
1451
1452        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1453                return;
1454
1455        while (l--) {
1456                c = *in++;
1457                switch (ch->pscan_state) {
1458                default:
1459                        /* reset to sanity and fall through */
1460                        ch->pscan_state = 0;
1461
1462                case 0:
1463                        /* No FF seen yet */
1464                        if (c == (unsigned char) '\377')
1465                                /* delete this character from stream */
1466                                ch->pscan_state = 1;
1467                        else {
1468                                *cout++ = c;
1469                                *fout++ = TTY_NORMAL;
1470                                count += 1;
1471                        }
1472                        break;
1473
1474                case 1:
1475                        /* first FF seen */
1476                        if (c == (unsigned char) '\377') {
1477                                /* doubled ff, transform to single ff */
1478                                *cout++ = c;
1479                                *fout++ = TTY_NORMAL;
1480                                count += 1;
1481                                ch->pscan_state = 0;
1482                        } else {
1483                                /* save value examination in next state */
1484                                ch->pscan_savechar = c;
1485                                ch->pscan_state = 2;
1486                        }
1487                        break;
1488
1489                case 2:
1490                        /* third character of ff sequence */
1491
1492                        *cout++ = c;
1493
1494                        if (ch->pscan_savechar == 0x0) {
1495
1496                                if (c == 0x0) {
1497                                        ch->ch_err_break++;
1498                                        *fout++ = TTY_BREAK;
1499                                } else {
1500                                        ch->ch_err_parity++;
1501                                        *fout++ = TTY_PARITY;
1502                                }
1503                        }
1504
1505                        count += 1;
1506                        ch->pscan_state = 0;
1507                }
1508        }
1509        *len = count;
1510}
1511
1512/*=======================================================================
1513 *
1514 *      dgap_input - Process received data.
1515 *
1516 *              ch      - Pointer to channel structure.
1517 *
1518 *=======================================================================*/
1519
1520static void dgap_input(struct channel_t *ch)
1521{
1522        struct board_t *bd;
1523        struct bs_t __iomem *bs;
1524        struct tty_struct *tp;
1525        struct tty_ldisc *ld;
1526        uint rmask;
1527        uint head;
1528        uint tail;
1529        int data_len;
1530        ulong lock_flags;
1531        ulong lock_flags2;
1532        int flip_len;
1533        int len;
1534        int n;
1535        u8 *buf;
1536        u8 tmpchar;
1537        int s;
1538
1539        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1540                return;
1541
1542        tp = ch->ch_tun.un_tty;
1543
1544        bs  = ch->ch_bs;
1545        if (!bs)
1546                return;
1547
1548        bd = ch->ch_bd;
1549        if (!bd || bd->magic != DGAP_BOARD_MAGIC)
1550                return;
1551
1552        spin_lock_irqsave(&bd->bd_lock, lock_flags);
1553        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
1554
1555        /*
1556         *      Figure the number of characters in the buffer.
1557         *      Exit immediately if none.
1558         */
1559
1560        rmask = ch->ch_rsize - 1;
1561
1562        head = readw(&(bs->rx_head));
1563        head &= rmask;
1564        tail = readw(&(bs->rx_tail));
1565        tail &= rmask;
1566
1567        data_len = (head - tail) & rmask;
1568
1569        if (data_len == 0) {
1570                writeb(1, &(bs->idata));
1571                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1572                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1573                return;
1574        }
1575
1576        /*
1577         * If the device is not open, or CREAD is off, flush
1578         * input data and return immediately.
1579         */
1580        if ((bd->state != BOARD_READY) || !tp  ||
1581            (tp->magic != TTY_MAGIC) ||
1582            !(ch->ch_tun.un_flags & UN_ISOPEN) ||
1583            !(tp->termios.c_cflag & CREAD) ||
1584            (ch->ch_tun.un_flags & UN_CLOSING)) {
1585
1586                writew(head, &(bs->rx_tail));
1587                writeb(1, &(bs->idata));
1588                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1589                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1590                return;
1591        }
1592
1593        /*
1594         * If we are throttled, simply don't read any data.
1595         */
1596        if (ch->ch_flags & CH_RXBLOCK) {
1597                writeb(1, &(bs->idata));
1598                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1599                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1600                return;
1601        }
1602
1603        /*
1604         *      Ignore oruns.
1605         */
1606        tmpchar = readb(&(bs->orun));
1607        if (tmpchar) {
1608                ch->ch_err_overrun++;
1609                writeb(0, &(bs->orun));
1610        }
1611
1612        /* Decide how much data we can send into the tty layer */
1613        flip_len = TTY_FLIPBUF_SIZE;
1614
1615        /* Chop down the length, if needed */
1616        len = min(data_len, flip_len);
1617        len = min(len, (N_TTY_BUF_SIZE - 1));
1618
1619        ld = tty_ldisc_ref(tp);
1620
1621#ifdef TTY_DONT_FLIP
1622        /*
1623         * If the DONT_FLIP flag is on, don't flush our buffer, and act
1624         * like the ld doesn't have any space to put the data right now.
1625         */
1626        if (test_bit(TTY_DONT_FLIP, &tp->flags))
1627                len = 0;
1628#endif
1629
1630        /*
1631         * If we were unable to get a reference to the ld,
1632         * don't flush our buffer, and act like the ld doesn't
1633         * have any space to put the data right now.
1634         */
1635        if (!ld) {
1636                len = 0;
1637        } else {
1638                /*
1639                 * If ld doesn't have a pointer to a receive_buf function,
1640                 * flush the data, then act like the ld doesn't have any
1641                 * space to put the data right now.
1642                 */
1643                if (!ld->ops->receive_buf) {
1644                        writew(head, &(bs->rx_tail));
1645                        len = 0;
1646                }
1647        }
1648
1649        if (len <= 0) {
1650                writeb(1, &(bs->idata));
1651                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1652                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1653                if (ld)
1654                        tty_ldisc_deref(ld);
1655                return;
1656        }
1657
1658        buf = ch->ch_bd->flipbuf;
1659        n = len;
1660
1661        /*
1662         * n now contains the most amount of data we can copy,
1663         * bounded either by our buffer size or the amount
1664         * of data the card actually has pending...
1665         */
1666        while (n) {
1667
1668                s = ((head >= tail) ? head : ch->ch_rsize) - tail;
1669                s = min(s, n);
1670
1671                if (s <= 0)
1672                        break;
1673
1674                memcpy_fromio(buf, ch->ch_raddr + tail, s);
1675
1676                tail += s;
1677                buf += s;
1678
1679                n -= s;
1680                /* Flip queue if needed */
1681                tail &= rmask;
1682        }
1683
1684        writew(tail, &(bs->rx_tail));
1685        writeb(1, &(bs->idata));
1686        ch->ch_rxcount += len;
1687
1688        /*
1689         * If we are completely raw, we don't need to go through a lot
1690         * of the tty layers that exist.
1691         * In this case, we take the shortest and fastest route we
1692         * can to relay the data to the user.
1693         *
1694         * On the other hand, if we are not raw, we need to go through
1695         * the tty layer, which has its API more well defined.
1696         */
1697        if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
1698                dgap_parity_scan(ch, ch->ch_bd->flipbuf,
1699                                 ch->ch_bd->flipflagbuf, &len);
1700
1701                len = tty_buffer_request_room(tp->port, len);
1702                tty_insert_flip_string_flags(tp->port, ch->ch_bd->flipbuf,
1703                        ch->ch_bd->flipflagbuf, len);
1704        } else {
1705                len = tty_buffer_request_room(tp->port, len);
1706                tty_insert_flip_string(tp->port, ch->ch_bd->flipbuf, len);
1707        }
1708
1709        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1710        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1711
1712        /* Tell the tty layer its okay to "eat" the data now */
1713        tty_flip_buffer_push(tp->port);
1714
1715        if (ld)
1716                tty_ldisc_deref(ld);
1717
1718}
1719
1720static void dgap_write_wakeup(struct board_t *bd, struct channel_t *ch,
1721                              struct un_t *un, u32 mask,
1722                              unsigned long *irq_flags1,
1723                              unsigned long *irq_flags2)
1724{
1725        if (!(un->un_flags & mask))
1726                return;
1727
1728        un->un_flags &= ~mask;
1729
1730        if (!(un->un_flags & UN_ISOPEN))
1731                return;
1732
1733        if ((un->un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1734            un->un_tty->ldisc->ops->write_wakeup) {
1735                spin_unlock_irqrestore(&ch->ch_lock, *irq_flags2);
1736                spin_unlock_irqrestore(&bd->bd_lock, *irq_flags1);
1737
1738                (un->un_tty->ldisc->ops->write_wakeup)(un->un_tty);
1739
1740                spin_lock_irqsave(&bd->bd_lock, *irq_flags1);
1741                spin_lock_irqsave(&ch->ch_lock, *irq_flags2);
1742        }
1743        wake_up_interruptible(&un->un_tty->write_wait);
1744        wake_up_interruptible(&un->un_flags_wait);
1745}
1746
1747/************************************************************************
1748 * Determines when CARRIER changes state and takes appropriate
1749 * action.
1750 ************************************************************************/
1751static void dgap_carrier(struct channel_t *ch)
1752{
1753        struct board_t *bd;
1754
1755        int virt_carrier = 0;
1756        int phys_carrier = 0;
1757
1758        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1759                return;
1760
1761        bd = ch->ch_bd;
1762
1763        if (!bd || bd->magic != DGAP_BOARD_MAGIC)
1764                return;
1765
1766        /* Make sure altpin is always set correctly */
1767        if (ch->ch_digi.digi_flags & DIGI_ALTPIN) {
1768                ch->ch_dsr      = DM_CD;
1769                ch->ch_cd       = DM_DSR;
1770        } else {
1771                ch->ch_dsr      = DM_DSR;
1772                ch->ch_cd       = DM_CD;
1773        }
1774
1775        if (ch->ch_mistat & D_CD(ch))
1776                phys_carrier = 1;
1777
1778        if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
1779                virt_carrier = 1;
1780
1781        if (ch->ch_c_cflag & CLOCAL)
1782                virt_carrier = 1;
1783
1784        /*
1785         * Test for a VIRTUAL carrier transition to HIGH.
1786         */
1787        if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
1788
1789                /*
1790                 * When carrier rises, wake any threads waiting
1791                 * for carrier in the open routine.
1792                 */
1793
1794                if (waitqueue_active(&(ch->ch_flags_wait)))
1795                        wake_up_interruptible(&ch->ch_flags_wait);
1796        }
1797
1798        /*
1799         * Test for a PHYSICAL carrier transition to HIGH.
1800         */
1801        if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
1802
1803                /*
1804                 * When carrier rises, wake any threads waiting
1805                 * for carrier in the open routine.
1806                 */
1807
1808                if (waitqueue_active(&(ch->ch_flags_wait)))
1809                        wake_up_interruptible(&ch->ch_flags_wait);
1810        }
1811
1812        /*
1813         *  Test for a PHYSICAL transition to low, so long as we aren't
1814         *  currently ignoring physical transitions (which is what "virtual
1815         *  carrier" indicates).
1816         *
1817         *  The transition of the virtual carrier to low really doesn't
1818         *  matter... it really only means "ignore carrier state", not
1819         *  "make pretend that carrier is there".
1820         */
1821        if ((virt_carrier == 0) &&
1822            ((ch->ch_flags & CH_CD) != 0) &&
1823            (phys_carrier == 0)) {
1824
1825                /*
1826                 *   When carrier drops:
1827                 *
1828                 *   Drop carrier on all open units.
1829                 *
1830                 *   Flush queues, waking up any task waiting in the
1831                 *   line discipline.
1832                 *
1833                 *   Send a hangup to the control terminal.
1834                 *
1835                 *   Enable all select calls.
1836                 */
1837                if (waitqueue_active(&(ch->ch_flags_wait)))
1838                        wake_up_interruptible(&ch->ch_flags_wait);
1839
1840                if (ch->ch_tun.un_open_count > 0)
1841                        tty_hangup(ch->ch_tun.un_tty);
1842
1843                if (ch->ch_pun.un_open_count > 0)
1844                        tty_hangup(ch->ch_pun.un_tty);
1845        }
1846
1847        /*
1848         *  Make sure that our cached values reflect the current reality.
1849         */
1850        if (virt_carrier == 1)
1851                ch->ch_flags |= CH_FCAR;
1852        else
1853                ch->ch_flags &= ~CH_FCAR;
1854
1855        if (phys_carrier == 1)
1856                ch->ch_flags |= CH_CD;
1857        else
1858                ch->ch_flags &= ~CH_CD;
1859}
1860
1861/*=======================================================================
1862 *
1863 *      dgap_event - FEP to host event processing routine.
1864 *
1865 *              bd     - Board of current event.
1866 *
1867 *=======================================================================*/
1868static int dgap_event(struct board_t *bd)
1869{
1870        struct channel_t *ch;
1871        ulong lock_flags;
1872        ulong lock_flags2;
1873        struct bs_t __iomem *bs;
1874        u8 __iomem *event;
1875        u8 __iomem *vaddr;
1876        struct ev_t __iomem *eaddr;
1877        uint head;
1878        uint tail;
1879        int port;
1880        int reason;
1881        int modem;
1882        int b1;
1883
1884        if (!bd || bd->magic != DGAP_BOARD_MAGIC)
1885                return -EIO;
1886
1887        spin_lock_irqsave(&bd->bd_lock, lock_flags);
1888
1889        vaddr = bd->re_map_membase;
1890
1891        if (!vaddr) {
1892                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1893                return -EIO;
1894        }
1895
1896        eaddr = (struct ev_t __iomem *) (vaddr + EVBUF);
1897
1898        /* Get our head and tail */
1899        head = readw(&(eaddr->ev_head));
1900        tail = readw(&(eaddr->ev_tail));
1901
1902        /*
1903         * Forget it if pointers out of range.
1904         */
1905
1906        if (head >= EVMAX - EVSTART || tail >= EVMAX - EVSTART ||
1907            (head | tail) & 03) {
1908                /* Let go of board lock */
1909                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1910                return -EIO;
1911        }
1912
1913        /*
1914         * Loop to process all the events in the buffer.
1915         */
1916        while (tail != head) {
1917
1918                /*
1919                 * Get interrupt information.
1920                 */
1921
1922                event = bd->re_map_membase + tail + EVSTART;
1923
1924                port   = ioread8(event);
1925                reason = ioread8(event + 1);
1926                modem  = ioread8(event + 2);
1927                b1     = ioread8(event + 3);
1928
1929                /*
1930                 * Make sure the interrupt is valid.
1931                 */
1932                if (port >= bd->nasync)
1933                        goto next;
1934
1935                if (!(reason & (IFMODEM | IFBREAK | IFTLW | IFTEM | IFDATA)))
1936                        goto next;
1937
1938                ch = bd->channels[port];
1939
1940                if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1941                        goto next;
1942
1943                /*
1944                 * If we have made it here, the event was valid.
1945                 * Lock down the channel.
1946                 */
1947                spin_lock_irqsave(&ch->ch_lock, lock_flags2);
1948
1949                bs = ch->ch_bs;
1950
1951                if (!bs) {
1952                        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1953                        goto next;
1954                }
1955
1956                /*
1957                 * Process received data.
1958                 */
1959                if (reason & IFDATA) {
1960
1961                        /*
1962                         * ALL LOCKS *MUST* BE DROPPED BEFORE CALLING INPUT!
1963                         * input could send some data to ld, which in turn
1964                         * could do a callback to one of our other functions.
1965                         */
1966                        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1967                        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1968
1969                        dgap_input(ch);
1970
1971                        spin_lock_irqsave(&bd->bd_lock, lock_flags);
1972                        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
1973
1974                        if (ch->ch_flags & CH_RACTIVE)
1975                                ch->ch_flags |= CH_RENABLE;
1976                        else
1977                                writeb(1, &(bs->idata));
1978
1979                        if (ch->ch_flags & CH_RWAIT) {
1980                                ch->ch_flags &= ~CH_RWAIT;
1981
1982                                wake_up_interruptible
1983                                        (&ch->ch_tun.un_flags_wait);
1984                        }
1985                }
1986
1987                /*
1988                 * Process Modem change signals.
1989                 */
1990                if (reason & IFMODEM) {
1991                        ch->ch_mistat = modem;
1992                        dgap_carrier(ch);
1993                }
1994
1995                /*
1996                 * Process break.
1997                 */
1998                if (reason & IFBREAK) {
1999
2000                        if (ch->ch_tun.un_tty) {
2001                                /* A break has been indicated */
2002                                ch->ch_err_break++;
2003                                tty_buffer_request_room
2004                                        (ch->ch_tun.un_tty->port, 1);
2005                                tty_insert_flip_char(ch->ch_tun.un_tty->port,
2006                                                     0, TTY_BREAK);
2007                                tty_flip_buffer_push(ch->ch_tun.un_tty->port);
2008                        }
2009                }
2010
2011                /*
2012                 * Process Transmit low.
2013                 */
2014                if (reason & IFTLW) {
2015                        dgap_write_wakeup(bd, ch, &ch->ch_tun, UN_LOW,
2016                                          &lock_flags, &lock_flags2);
2017                        dgap_write_wakeup(bd, ch, &ch->ch_pun, UN_LOW,
2018                                          &lock_flags, &lock_flags2);
2019                        if (ch->ch_flags & CH_WLOW) {
2020                                ch->ch_flags &= ~CH_WLOW;
2021                                wake_up_interruptible(&ch->ch_flags_wait);
2022                        }
2023                }
2024
2025                /*
2026                 * Process Transmit empty.
2027                 */
2028                if (reason & IFTEM) {
2029                        dgap_write_wakeup(bd, ch, &ch->ch_tun, UN_EMPTY,
2030                                          &lock_flags, &lock_flags2);
2031                        dgap_write_wakeup(bd, ch, &ch->ch_pun, UN_EMPTY,
2032                                          &lock_flags, &lock_flags2);
2033                        if (ch->ch_flags & CH_WEMPTY) {
2034                                ch->ch_flags &= ~CH_WEMPTY;
2035                                wake_up_interruptible(&ch->ch_flags_wait);
2036                        }
2037                }
2038
2039                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
2040
2041next:
2042                tail = (tail + 4) & (EVMAX - EVSTART - 4);
2043        }
2044
2045        writew(tail, &(eaddr->ev_tail));
2046        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2047
2048        return 0;
2049}
2050
2051/*
2052 * Our board poller function.
2053 */
2054static void dgap_poll_tasklet(unsigned long data)
2055{
2056        struct board_t *bd = (struct board_t *) data;
2057        ulong lock_flags;
2058        char __iomem *vaddr;
2059        u16 head, tail;
2060
2061        if (!bd || (bd->magic != DGAP_BOARD_MAGIC))
2062                return;
2063
2064        if (bd->inhibit_poller)
2065                return;
2066
2067        spin_lock_irqsave(&bd->bd_lock, lock_flags);
2068
2069        vaddr = bd->re_map_membase;
2070
2071        /*
2072         * If board is ready, parse deeper to see if there is anything to do.
2073         */
2074        if (bd->state == BOARD_READY) {
2075
2076                struct ev_t __iomem *eaddr;
2077
2078                if (!bd->re_map_membase) {
2079                        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2080                        return;
2081                }
2082                if (!bd->re_map_port) {
2083                        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2084                        return;
2085                }
2086
2087                if (!bd->nasync)
2088                        goto out;
2089
2090                eaddr = (struct ev_t __iomem *) (vaddr + EVBUF);
2091
2092                /* Get our head and tail */
2093                head = readw(&(eaddr->ev_head));
2094                tail = readw(&(eaddr->ev_tail));
2095
2096                /*
2097                 * If there is an event pending. Go service it.
2098                 */
2099                if (head != tail) {
2100                        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2101                        dgap_event(bd);
2102                        spin_lock_irqsave(&bd->bd_lock, lock_flags);
2103                }
2104
2105out:
2106                /*
2107                 * If board is doing interrupts, ACK the interrupt.
2108                 */
2109                if (bd && bd->intr_running)
2110                        readb(bd->re_map_port + 2);
2111
2112                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2113                return;
2114        }
2115
2116        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2117}
2118
2119/*
2120 * dgap_found_board()
2121 *
2122 * A board has been found, init it.
2123 */
2124static struct board_t *dgap_found_board(struct pci_dev *pdev, int id,
2125                                        int boardnum)
2126{
2127        struct board_t *brd;
2128        unsigned int pci_irq;
2129        int i;
2130        int ret;
2131
2132        /* get the board structure and prep it */
2133        brd = kzalloc(sizeof(struct board_t), GFP_KERNEL);
2134        if (!brd)
2135                return ERR_PTR(-ENOMEM);
2136
2137        /* store the info for the board we've found */
2138        brd->magic = DGAP_BOARD_MAGIC;
2139        brd->boardnum = boardnum;
2140        brd->vendor = dgap_pci_tbl[id].vendor;
2141        brd->device = dgap_pci_tbl[id].device;
2142        brd->pdev = pdev;
2143        brd->pci_bus = pdev->bus->number;
2144        brd->pci_slot = PCI_SLOT(pdev->devfn);
2145        brd->name = dgap_ids[id].name;
2146        brd->maxports = dgap_ids[id].maxports;
2147        brd->type = dgap_ids[id].config_type;
2148        brd->dpatype = dgap_ids[id].dpatype;
2149        brd->dpastatus = BD_NOFEP;
2150        init_waitqueue_head(&brd->state_wait);
2151
2152        spin_lock_init(&brd->bd_lock);
2153
2154        brd->inhibit_poller     = FALSE;
2155        brd->wait_for_bios      = 0;
2156        brd->wait_for_fep       = 0;
2157
2158        for (i = 0; i < MAXPORTS; i++)
2159                brd->channels[i] = NULL;
2160
2161        /* store which card & revision we have */
2162        pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &brd->subvendor);
2163        pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &brd->subdevice);
2164        pci_read_config_byte(pdev, PCI_REVISION_ID, &brd->rev);
2165
2166        pci_irq = pdev->irq;
2167        brd->irq = pci_irq;
2168
2169        /* get the PCI Base Address Registers */
2170
2171        /* Xr Jupiter and EPC use BAR 2 */
2172        if (brd->device == PCI_DEV_XRJ_DID || brd->device == PCI_DEV_EPCJ_DID) {
2173                brd->membase     = pci_resource_start(pdev, 2);
2174                brd->membase_end = pci_resource_end(pdev, 2);
2175        }
2176        /* Everyone else uses BAR 0 */
2177        else {
2178                brd->membase     = pci_resource_start(pdev, 0);
2179                brd->membase_end = pci_resource_end(pdev, 0);
2180        }
2181
2182        if (!brd->membase) {
2183                ret = -ENODEV;
2184                goto free_brd;
2185        }
2186
2187        if (brd->membase & 1)
2188                brd->membase &= ~3;
2189        else
2190                brd->membase &= ~15;
2191
2192        /*
2193         * On the PCI boards, there is no IO space allocated
2194         * The I/O registers will be in the first 3 bytes of the
2195         * upper 2MB of the 4MB memory space.  The board memory
2196         * will be mapped into the low 2MB of the 4MB memory space
2197         */
2198        brd->port = brd->membase + PCI_IO_OFFSET;
2199        brd->port_end = brd->port + PCI_IO_SIZE;
2200
2201        /*
2202         * Special initialization for non-PLX boards
2203         */
2204        if (brd->device != PCI_DEV_XRJ_DID && brd->device != PCI_DEV_EPCJ_DID) {
2205                unsigned short cmd;
2206
2207                pci_write_config_byte(pdev, 0x40, 0);
2208                pci_write_config_byte(pdev, 0x46, 0);
2209
2210                /* Limit burst length to 2 doubleword transactions */
2211                pci_write_config_byte(pdev, 0x42, 1);
2212
2213                /*
2214                 * Enable IO and mem if not already done.
2215                 * This was needed for support on Itanium.
2216                 */
2217                pci_read_config_word(pdev, PCI_COMMAND, &cmd);
2218                cmd |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
2219                pci_write_config_word(pdev, PCI_COMMAND, cmd);
2220        }
2221
2222        /* init our poll helper tasklet */
2223        tasklet_init(&brd->helper_tasklet, dgap_poll_tasklet,
2224                        (unsigned long) brd);
2225
2226        ret = dgap_remap(brd);
2227        if (ret)
2228                goto free_brd;
2229
2230        pr_info("dgap: board %d: %s (rev %d), irq %ld\n",
2231                boardnum, brd->name, brd->rev, brd->irq);
2232
2233        return brd;
2234
2235free_brd:
2236        kfree(brd);
2237
2238        return ERR_PTR(ret);
2239}
2240
2241/*
2242 * dgap_intr()
2243 *
2244 * Driver interrupt handler.
2245 */
2246static irqreturn_t dgap_intr(int irq, void *voidbrd)
2247{
2248        struct board_t *brd = voidbrd;
2249
2250        if (!brd)
2251                return IRQ_NONE;
2252
2253        /*
2254         * Check to make sure its for us.
2255         */
2256        if (brd->magic != DGAP_BOARD_MAGIC)
2257                return IRQ_NONE;
2258
2259        brd->intr_count++;
2260
2261        /*
2262         * Schedule tasklet to run at a better time.
2263         */
2264        tasklet_schedule(&brd->helper_tasklet);
2265        return IRQ_HANDLED;
2266}
2267
2268/*****************************************************************************
2269*
2270* Function:
2271*
2272*    dgap_poll_handler
2273*
2274* Author:
2275*
2276*    Scott H Kilau
2277*
2278* Parameters:
2279*
2280*    dummy -- ignored
2281*
2282* Return Values:
2283*
2284*    none
2285*
2286* Description:
2287*
2288*    As each timer expires, it determines (a) whether the "transmit"
2289*    waiter needs to be woken up, and (b) whether the poller needs to
2290*    be rescheduled.
2291*
2292******************************************************************************/
2293
2294static void dgap_poll_handler(ulong dummy)
2295{
2296        unsigned int i;
2297        struct board_t *brd;
2298        unsigned long lock_flags;
2299        ulong new_time;
2300
2301        dgap_poll_counter++;
2302
2303        /*
2304         * Do not start the board state machine until
2305         * driver tells us its up and running, and has
2306         * everything it needs.
2307         */
2308        if (dgap_driver_state != DRIVER_READY)
2309                goto schedule_poller;
2310
2311        /*
2312         * If we have just 1 board, or the system is not SMP,
2313         * then use the typical old style poller.
2314         * Otherwise, use our new tasklet based poller, which should
2315         * speed things up for multiple boards.
2316         */
2317        if ((dgap_numboards == 1) || (num_online_cpus() <= 1)) {
2318                for (i = 0; i < dgap_numboards; i++) {
2319
2320                        brd = dgap_board[i];
2321
2322                        if (brd->state == BOARD_FAILED)
2323                                continue;
2324                        if (!brd->intr_running)
2325                                /* Call the real board poller directly */
2326                                dgap_poll_tasklet((unsigned long) brd);
2327                }
2328        } else {
2329                /*
2330                 * Go thru each board, kicking off a
2331                 * tasklet for each if needed
2332                 */
2333                for (i = 0; i < dgap_numboards; i++) {
2334                        brd = dgap_board[i];
2335
2336                        /*
2337                         * Attempt to grab the board lock.
2338                         *
2339                         * If we can't get it, no big deal, the next poll
2340                         * will get it. Basically, I just really don't want
2341                         * to spin in here, because I want to kick off my
2342                         * tasklets as fast as I can, and then get out the
2343                         * poller.
2344                         */
2345                        if (!spin_trylock(&brd->bd_lock))
2346                                continue;
2347
2348                        /*
2349                         * If board is in a failed state, don't bother
2350                         *  scheduling a tasklet
2351                         */
2352                        if (brd->state == BOARD_FAILED) {
2353                                spin_unlock(&brd->bd_lock);
2354                                continue;
2355                        }
2356
2357                        /* Schedule a poll helper task */
2358                        if (!brd->intr_running)
2359                                tasklet_schedule(&brd->helper_tasklet);
2360
2361                        /*
2362                         * Can't do DGAP_UNLOCK here, as we don't have
2363                         * lock_flags because we did a trylock above.
2364                         */
2365                        spin_unlock(&brd->bd_lock);
2366                }
2367        }
2368
2369schedule_poller:
2370
2371        /*
2372         * Schedule ourself back at the nominal wakeup interval.
2373         */
2374        spin_lock_irqsave(&dgap_poll_lock, lock_flags);
2375        dgap_poll_time +=  dgap_jiffies_from_ms(dgap_poll_tick);
2376
2377        new_time = dgap_poll_time - jiffies;
2378
2379        if ((ulong) new_time >= 2 * dgap_poll_tick) {
2380                dgap_poll_time =
2381                        jiffies +  dgap_jiffies_from_ms(dgap_poll_tick);
2382        }
2383
2384        dgap_poll_timer.function = dgap_poll_handler;
2385        dgap_poll_timer.data = 0;
2386        dgap_poll_timer.expires = dgap_poll_time;
2387        spin_unlock_irqrestore(&dgap_poll_lock, lock_flags);
2388
2389        if (!dgap_poll_stop)
2390                add_timer(&dgap_poll_timer);
2391}
2392
2393/*=======================================================================
2394 *
2395 *      dgap_cmdb - Sends a 2 byte command to the FEP.
2396 *
2397 *              ch      - Pointer to channel structure.
2398 *              cmd     - Command to be sent.
2399 *              byte1   - Integer containing first byte to be sent.
2400 *              byte2   - Integer containing second byte to be sent.
2401 *              ncmds   - Wait until ncmds or fewer cmds are left
2402 *                        in the cmd buffer before returning.
2403 *
2404 *=======================================================================*/
2405static void dgap_cmdb(struct channel_t *ch, u8 cmd, u8 byte1,
2406                        u8 byte2, uint ncmds)
2407{
2408        char __iomem *vaddr;
2409        struct __iomem cm_t *cm_addr;
2410        uint count;
2411        uint n;
2412        u16 head;
2413        u16 tail;
2414
2415        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2416                return;
2417
2418        /*
2419         * Check if board is still alive.
2420         */
2421        if (ch->ch_bd->state == BOARD_FAILED)
2422                return;
2423
2424        /*
2425         * Make sure the pointers are in range before
2426         * writing to the FEP memory.
2427         */
2428        vaddr = ch->ch_bd->re_map_membase;
2429
2430        if (!vaddr)
2431                return;
2432
2433        cm_addr = (struct cm_t __iomem *) (vaddr + CMDBUF);
2434        head = readw(&(cm_addr->cm_head));
2435
2436        /*
2437         * Forget it if pointers out of range.
2438         */
2439        if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
2440                ch->ch_bd->state = BOARD_FAILED;
2441                return;
2442        }
2443
2444        /*
2445         * Put the data in the circular command buffer.
2446         */
2447        writeb(cmd, (vaddr + head + CMDSTART + 0));
2448        writeb((u8) ch->ch_portnum, (vaddr + head + CMDSTART + 1));
2449        writeb(byte1, (vaddr + head + CMDSTART + 2));
2450        writeb(byte2, (vaddr + head + CMDSTART + 3));
2451
2452        head = (head + 4) & (CMDMAX - CMDSTART - 4);
2453
2454        writew(head, &(cm_addr->cm_head));
2455
2456        /*
2457         * Wait if necessary before updating the head
2458         * pointer to limit the number of outstanding
2459         * commands to the FEP.   If the time spent waiting
2460         * is outlandish, declare the FEP dead.
2461         */
2462        for (count = dgap_count ;;) {
2463
2464                head = readw(&(cm_addr->cm_head));
2465                tail = readw(&(cm_addr->cm_tail));
2466
2467                n = (head - tail) & (CMDMAX - CMDSTART - 4);
2468
2469                if (n <= ncmds * sizeof(struct cm_t))
2470                        break;
2471
2472                if (--count == 0) {
2473                        ch->ch_bd->state = BOARD_FAILED;
2474                        return;
2475                }
2476                udelay(10);
2477        }
2478}
2479
2480/*=======================================================================
2481 *
2482 *      dgap_cmdw - Sends a 1 word command to the FEP.
2483 *
2484 *              ch      - Pointer to channel structure.
2485 *              cmd     - Command to be sent.
2486 *              word    - Integer containing word to be sent.
2487 *              ncmds   - Wait until ncmds or fewer cmds are left
2488 *                        in the cmd buffer before returning.
2489 *
2490 *=======================================================================*/
2491static void dgap_cmdw(struct channel_t *ch, u8 cmd, u16 word, uint ncmds)
2492{
2493        char __iomem *vaddr;
2494        struct __iomem cm_t *cm_addr;
2495        uint count;
2496        uint n;
2497        u16 head;
2498        u16 tail;
2499
2500        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2501                return;
2502
2503        /*
2504         * Check if board is still alive.
2505         */
2506        if (ch->ch_bd->state == BOARD_FAILED)
2507                return;
2508
2509        /*
2510         * Make sure the pointers are in range before
2511         * writing to the FEP memory.
2512         */
2513        vaddr = ch->ch_bd->re_map_membase;
2514        if (!vaddr)
2515                return;
2516
2517        cm_addr = (struct cm_t __iomem *) (vaddr + CMDBUF);
2518        head = readw(&(cm_addr->cm_head));
2519
2520        /*
2521         * Forget it if pointers out of range.
2522         */
2523        if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
2524                ch->ch_bd->state = BOARD_FAILED;
2525                return;
2526        }
2527
2528        /*
2529         * Put the data in the circular command buffer.
2530         */
2531        writeb(cmd, (vaddr + head + CMDSTART + 0));
2532        writeb((u8) ch->ch_portnum, (vaddr + head + CMDSTART + 1));
2533        writew((u16) word, (vaddr + head + CMDSTART + 2));
2534
2535        head = (head + 4) & (CMDMAX - CMDSTART - 4);
2536
2537        writew(head, &(cm_addr->cm_head));
2538
2539        /*
2540         * Wait if necessary before updating the head
2541         * pointer to limit the number of outstanding
2542         * commands to the FEP.   If the time spent waiting
2543         * is outlandish, declare the FEP dead.
2544         */
2545        for (count = dgap_count ;;) {
2546
2547                head = readw(&(cm_addr->cm_head));
2548                tail = readw(&(cm_addr->cm_tail));
2549
2550                n = (head - tail) & (CMDMAX - CMDSTART - 4);
2551
2552                if (n <= ncmds * sizeof(struct cm_t))
2553                        break;
2554
2555                if (--count == 0) {
2556                        ch->ch_bd->state = BOARD_FAILED;
2557                        return;
2558                }
2559                udelay(10);
2560        }
2561}
2562
2563/*=======================================================================
2564 *
2565 *      dgap_cmdw_ext - Sends a extended word command to the FEP.
2566 *
2567 *              ch      - Pointer to channel structure.
2568 *              cmd     - Command to be sent.
2569 *              word    - Integer containing word to be sent.
2570 *              ncmds   - Wait until ncmds or fewer cmds are left
2571 *                        in the cmd buffer before returning.
2572 *
2573 *=======================================================================*/
2574static void dgap_cmdw_ext(struct channel_t *ch, u16 cmd, u16 word, uint ncmds)
2575{
2576        char __iomem *vaddr;
2577        struct __iomem cm_t *cm_addr;
2578        uint count;
2579        uint n;
2580        u16 head;
2581        u16 tail;
2582
2583        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2584                return;
2585
2586        /*
2587         * Check if board is still alive.
2588         */
2589        if (ch->ch_bd->state == BOARD_FAILED)
2590                return;
2591
2592        /*
2593         * Make sure the pointers are in range before
2594         * writing to the FEP memory.
2595         */
2596        vaddr = ch->ch_bd->re_map_membase;
2597        if (!vaddr)
2598                return;
2599
2600        cm_addr = (struct cm_t __iomem *) (vaddr + CMDBUF);
2601        head = readw(&(cm_addr->cm_head));
2602
2603        /*
2604         * Forget it if pointers out of range.
2605         */
2606        if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
2607                ch->ch_bd->state = BOARD_FAILED;
2608                return;
2609        }
2610
2611        /*
2612         * Put the data in the circular command buffer.
2613         */
2614
2615        /* Write an FF to tell the FEP that we want an extended command */
2616        writeb((u8) 0xff, (vaddr + head + CMDSTART + 0));
2617
2618        writeb((u8) ch->ch_portnum, (vaddr + head + CMDSTART + 1));
2619        writew((u16) cmd, (vaddr + head + CMDSTART + 2));
2620
2621        /*
2622         * If the second part of the command won't fit,
2623         * put it at the beginning of the circular buffer.
2624         */
2625        if (((head + 4) >= ((CMDMAX - CMDSTART)) || (head & 03)))
2626                writew((u16) word, (vaddr + CMDSTART));
2627        else
2628                writew((u16) word, (vaddr + head + CMDSTART + 4));
2629
2630        head = (head + 8) & (CMDMAX - CMDSTART - 4);
2631
2632        writew(head, &(cm_addr->cm_head));
2633
2634        /*
2635         * Wait if necessary before updating the head
2636         * pointer to limit the number of outstanding
2637         * commands to the FEP.   If the time spent waiting
2638         * is outlandish, declare the FEP dead.
2639         */
2640        for (count = dgap_count ;;) {
2641
2642                head = readw(&(cm_addr->cm_head));
2643                tail = readw(&(cm_addr->cm_tail));
2644
2645                n = (head - tail) & (CMDMAX - CMDSTART - 4);
2646
2647                if (n <= ncmds * sizeof(struct cm_t))
2648                        break;
2649
2650                if (--count == 0) {
2651                        ch->ch_bd->state = BOARD_FAILED;
2652                        return;
2653                }
2654                udelay(10);
2655        }
2656}
2657
2658/*=======================================================================
2659 *
2660 *      dgap_wmove - Write data to FEP buffer.
2661 *
2662 *              ch      - Pointer to channel structure.
2663 *              buf     - Poiter to characters to be moved.
2664 *              cnt     - Number of characters to move.
2665 *
2666 *=======================================================================*/
2667static void dgap_wmove(struct channel_t *ch, char *buf, uint cnt)
2668{
2669        int n;
2670        char __iomem *taddr;
2671        struct bs_t __iomem *bs;
2672        u16 head;
2673
2674        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2675                return;
2676
2677        /*
2678         * Check parameters.
2679         */
2680        bs   = ch->ch_bs;
2681        head = readw(&(bs->tx_head));
2682
2683        /*
2684         * If pointers are out of range, just return.
2685         */
2686        if ((cnt > ch->ch_tsize) ||
2687            (unsigned)(head - ch->ch_tstart) >= ch->ch_tsize)
2688                return;
2689
2690        /*
2691         * If the write wraps over the top of the circular buffer,
2692         * move the portion up to the wrap point, and reset the
2693         * pointers to the bottom.
2694         */
2695        n = ch->ch_tstart + ch->ch_tsize - head;
2696
2697        if (cnt >= n) {
2698                cnt -= n;
2699                taddr = ch->ch_taddr + head;
2700                memcpy_toio(taddr, buf, n);
2701                head = ch->ch_tstart;
2702                buf += n;
2703        }
2704
2705        /*
2706         * Move rest of data.
2707         */
2708        taddr = ch->ch_taddr + head;
2709        n = cnt;
2710        memcpy_toio(taddr, buf, n);
2711        head += cnt;
2712
2713        writew(head, &(bs->tx_head));
2714}
2715
2716/*
2717 * Calls the firmware to reset this channel.
2718 */
2719static void dgap_firmware_reset_port(struct channel_t *ch)
2720{
2721        dgap_cmdb(ch, CHRESET, 0, 0, 0);
2722
2723        /*
2724         * Now that the channel is reset, we need to make sure
2725         * all the current settings get reapplied to the port
2726         * in the firmware.
2727         *
2728         * So we will set the driver's cache of firmware
2729         * settings all to 0, and then call param.
2730         */
2731        ch->ch_fepiflag = 0;
2732        ch->ch_fepcflag = 0;
2733        ch->ch_fepoflag = 0;
2734        ch->ch_fepstartc = 0;
2735        ch->ch_fepstopc = 0;
2736        ch->ch_fepastartc = 0;
2737        ch->ch_fepastopc = 0;
2738        ch->ch_mostat = 0;
2739        ch->ch_hflow = 0;
2740}
2741
2742/*=======================================================================
2743 *
2744 *      dgap_param - Set Digi parameters.
2745 *
2746 *              struct tty_struct *     - TTY for port.
2747 *
2748 *=======================================================================*/
2749static int dgap_param(struct channel_t *ch, struct board_t *bd, u32 un_type)
2750{
2751        u16 head;
2752        u16 cflag;
2753        u16 iflag;
2754        u8 mval;
2755        u8 hflow;
2756
2757        /*
2758         * If baud rate is zero, flush queues, and set mval to drop DTR.
2759         */
2760        if ((ch->ch_c_cflag & (CBAUD)) == 0) {
2761
2762                /* flush rx */
2763                head = readw(&(ch->ch_bs->rx_head));
2764                writew(head, &(ch->ch_bs->rx_tail));
2765
2766                /* flush tx */
2767                head = readw(&(ch->ch_bs->tx_head));
2768                writew(head, &(ch->ch_bs->tx_tail));
2769
2770                ch->ch_flags |= (CH_BAUD0);
2771
2772                /* Drop RTS and DTR */
2773                ch->ch_mval &= ~(D_RTS(ch)|D_DTR(ch));
2774                mval = D_DTR(ch) | D_RTS(ch);
2775                ch->ch_baud_info = 0;
2776
2777        } else if (ch->ch_custom_speed && (bd->bd_flags & BD_FEP5PLUS)) {
2778                /*
2779                 * Tell the fep to do the command
2780                 */
2781
2782                dgap_cmdw_ext(ch, 0xff01, ch->ch_custom_speed, 0);
2783
2784                /*
2785                 * Now go get from fep mem, what the fep
2786                 * believes the custom baud rate is.
2787                 */
2788                ch->ch_custom_speed = dgap_get_custom_baud(ch);
2789                ch->ch_baud_info = ch->ch_custom_speed;
2790
2791                /* Handle transition from B0 */
2792                if (ch->ch_flags & CH_BAUD0) {
2793                        ch->ch_flags &= ~(CH_BAUD0);
2794                        ch->ch_mval |= (D_RTS(ch)|D_DTR(ch));
2795                }
2796                mval = D_DTR(ch) | D_RTS(ch);
2797
2798        } else {
2799                /*
2800                 * Set baud rate, character size, and parity.
2801                 */
2802
2803
2804                int iindex = 0;
2805                int jindex = 0;
2806                int baud = 0;
2807
2808                ulong bauds[4][16] = {
2809                        { /* slowbaud */
2810                                0,      50,     75,     110,
2811                                134,    150,    200,    300,
2812                                600,    1200,   1800,   2400,
2813                                4800,   9600,   19200,  38400 },
2814                        { /* slowbaud & CBAUDEX */
2815                                0,      57600,  115200, 230400,
2816                                460800, 150,    200,    921600,
2817                                600,    1200,   1800,   2400,
2818                                4800,   9600,   19200,  38400 },
2819                        { /* fastbaud */
2820                                0,      57600,  76800,  115200,
2821                                14400,  57600,  230400, 76800,
2822                                115200, 230400, 28800,  460800,
2823                                921600, 9600,   19200,  38400 },
2824                        { /* fastbaud & CBAUDEX */
2825                                0,      57600,  115200, 230400,
2826                                460800, 150,    200,    921600,
2827                                600,    1200,   1800,   2400,
2828                                4800,   9600,   19200,  38400 }
2829                };
2830
2831                /*
2832                 * Only use the TXPrint baud rate if the
2833                 * terminal unit is NOT open
2834                 */
2835                if (!(ch->ch_tun.un_flags & UN_ISOPEN) &&
2836                    un_type == DGAP_PRINT)
2837                        baud = C_BAUD(ch->ch_pun.un_tty) & 0xff;
2838                else
2839                        baud = C_BAUD(ch->ch_tun.un_tty) & 0xff;
2840
2841                if (ch->ch_c_cflag & CBAUDEX)
2842                        iindex = 1;
2843
2844                if (ch->ch_digi.digi_flags & DIGI_FAST)
2845                        iindex += 2;
2846
2847                jindex = baud;
2848
2849                if ((iindex >= 0) && (iindex < 4) &&
2850                    (jindex >= 0) && (jindex < 16))
2851                        baud = bauds[iindex][jindex];
2852                else
2853                        baud = 0;
2854
2855                if (baud == 0)
2856                        baud = 9600;
2857
2858                ch->ch_baud_info = baud;
2859
2860                /*
2861                 * CBAUD has bit position 0x1000 set these days to
2862                 * indicate Linux baud rate remap.
2863                 * We use a different bit assignment for high speed.
2864                 * Clear this bit out while grabbing the parts of
2865                 * "cflag" we want.
2866                 */
2867                cflag = ch->ch_c_cflag & ((CBAUD ^ CBAUDEX) | PARODD | PARENB |
2868                                                   CSTOPB | CSIZE);
2869
2870                /*
2871                 * HUPCL bit is used by FEP to indicate fast baud
2872                 * table is to be used.
2873                 */
2874                if ((ch->ch_digi.digi_flags & DIGI_FAST) ||
2875                    (ch->ch_c_cflag & CBAUDEX))
2876                        cflag |= HUPCL;
2877
2878                if ((ch->ch_c_cflag & CBAUDEX) &&
2879                    !(ch->ch_digi.digi_flags & DIGI_FAST)) {
2880                        /*
2881                         * The below code is trying to guarantee that only
2882                         * baud rates 115200, 230400, 460800, 921600 are
2883                         * remapped. We use exclusive or  because the various
2884                         * baud rates share common bit positions and therefore
2885                         * can't be tested for easily.
2886                         */
2887                        tcflag_t tcflag = (ch->ch_c_cflag & CBAUD) | CBAUDEX;
2888                        int baudpart = 0;
2889
2890                        /*
2891                         * Map high speed requests to index
2892                         * into FEP's baud table
2893                         */
2894                        switch (tcflag) {
2895                        case B57600:
2896                                baudpart = 1;
2897                                break;
2898#ifdef B76800
2899                        case B76800:
2900                                baudpart = 2;
2901                                break;
2902#endif
2903                        case B115200:
2904                                baudpart = 3;
2905                                break;
2906                        case B230400:
2907                                baudpart = 9;
2908                                break;
2909                        case B460800:
2910                                baudpart = 11;
2911                                break;
2912#ifdef B921600
2913                        case B921600:
2914                                baudpart = 12;
2915                                break;
2916#endif
2917                        default:
2918                                baudpart = 0;
2919                        }
2920
2921                        if (baudpart)
2922                                cflag = (cflag & ~(CBAUD | CBAUDEX)) | baudpart;
2923                }
2924
2925                cflag &= 0xffff;
2926
2927                if (cflag != ch->ch_fepcflag) {
2928                        ch->ch_fepcflag = (u16) (cflag & 0xffff);
2929
2930                        /*
2931                         * Okay to have channel and board
2932                         * locks held calling this
2933                         */
2934                        dgap_cmdw(ch, SCFLAG, (u16) cflag, 0);
2935                }
2936
2937                /* Handle transition from B0 */
2938                if (ch->ch_flags & CH_BAUD0) {
2939                        ch->ch_flags &= ~(CH_BAUD0);
2940                        ch->ch_mval |= (D_RTS(ch)|D_DTR(ch));
2941                }
2942                mval = D_DTR(ch) | D_RTS(ch);
2943        }
2944
2945        /*
2946         * Get input flags.
2947         */
2948        iflag = ch->ch_c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK |
2949                                  INPCK | ISTRIP | IXON | IXANY | IXOFF);
2950
2951        if ((ch->ch_startc == _POSIX_VDISABLE) ||
2952            (ch->ch_stopc == _POSIX_VDISABLE)) {
2953                iflag &= ~(IXON | IXOFF);
2954                ch->ch_c_iflag &= ~(IXON | IXOFF);
2955        }
2956
2957        /*
2958         * Only the IBM Xr card can switch between
2959         * 232 and 422 modes on the fly
2960         */
2961        if (bd->device == PCI_DEV_XR_IBM_DID) {
2962                if (ch->ch_digi.digi_flags & DIGI_422)
2963                        dgap_cmdb(ch, SCOMMODE, MODE_422, 0, 0);
2964                else
2965                        dgap_cmdb(ch, SCOMMODE, MODE_232, 0, 0);
2966        }
2967
2968        if (ch->ch_digi.digi_flags & DIGI_ALTPIN)
2969                iflag |= IALTPIN;
2970
2971        if (iflag != ch->ch_fepiflag) {
2972                ch->ch_fepiflag = iflag;
2973
2974                /* Okay to have channel and board locks held calling this */
2975                dgap_cmdw(ch, SIFLAG, (u16) ch->ch_fepiflag, 0);
2976        }
2977
2978        /*
2979         * Select hardware handshaking.
2980         */
2981        hflow = 0;
2982
2983        if (ch->ch_c_cflag & CRTSCTS)
2984                hflow |= (D_RTS(ch) | D_CTS(ch));
2985        if (ch->ch_digi.digi_flags & RTSPACE)
2986                hflow |= D_RTS(ch);
2987        if (ch->ch_digi.digi_flags & DTRPACE)
2988                hflow |= D_DTR(ch);
2989        if (ch->ch_digi.digi_flags & CTSPACE)
2990                hflow |= D_CTS(ch);
2991        if (ch->ch_digi.digi_flags & DSRPACE)
2992                hflow |= D_DSR(ch);
2993        if (ch->ch_digi.digi_flags & DCDPACE)
2994                hflow |= D_CD(ch);
2995
2996        if (hflow != ch->ch_hflow) {
2997                ch->ch_hflow = hflow;
2998
2999                /* Okay to have channel and board locks held calling this */
3000                dgap_cmdb(ch, SHFLOW, (u8) hflow, 0xff, 0);
3001        }
3002
3003        /*
3004         * Set RTS and/or DTR Toggle if needed,
3005         * but only if product is FEP5+ based.
3006         */
3007        if (bd->bd_flags & BD_FEP5PLUS) {
3008                u16 hflow2 = 0;
3009
3010                if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE)
3011                        hflow2 |= (D_RTS(ch));
3012                if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE)
3013                        hflow2 |= (D_DTR(ch));
3014
3015                dgap_cmdw_ext(ch, 0xff03, hflow2, 0);
3016        }
3017
3018        /*
3019         * Set modem control lines.
3020         */
3021
3022        mval ^= ch->ch_mforce & (mval ^ ch->ch_mval);
3023
3024        if (ch->ch_mostat ^ mval) {
3025                ch->ch_mostat = mval;
3026
3027                /* Okay to have channel and board locks held calling this */
3028                dgap_cmdb(ch, SMODEM, (u8) mval, D_RTS(ch)|D_DTR(ch), 0);
3029        }
3030
3031        /*
3032         * Read modem signals, and then call carrier function.
3033         */
3034        ch->ch_mistat = readb(&(ch->ch_bs->m_stat));
3035        dgap_carrier(ch);
3036
3037        /*
3038         * Set the start and stop characters.
3039         */
3040        if (ch->ch_startc != ch->ch_fepstartc ||
3041            ch->ch_stopc != ch->ch_fepstopc) {
3042                ch->ch_fepstartc = ch->ch_startc;
3043                ch->ch_fepstopc =  ch->ch_stopc;
3044
3045                /* Okay to have channel and board locks held calling this */
3046                dgap_cmdb(ch, SFLOWC, ch->ch_fepstartc, ch->ch_fepstopc, 0);
3047        }
3048
3049        /*
3050         * Set the Auxiliary start and stop characters.
3051         */
3052        if (ch->ch_astartc != ch->ch_fepastartc ||
3053            ch->ch_astopc != ch->ch_fepastopc) {
3054                ch->ch_fepastartc = ch->ch_astartc;
3055                ch->ch_fepastopc = ch->ch_astopc;
3056
3057                /* Okay to have channel and board locks held calling this */
3058                dgap_cmdb(ch, SAFLOWC, ch->ch_fepastartc, ch->ch_fepastopc, 0);
3059        }
3060
3061        return 0;
3062}
3063
3064/*
3065 * dgap_block_til_ready()
3066 *
3067 * Wait for DCD, if needed.
3068 */
3069static int dgap_block_til_ready(struct tty_struct *tty, struct file *file,
3070                                struct channel_t *ch)
3071{
3072        int retval = 0;
3073        struct un_t *un;
3074        ulong lock_flags;
3075        uint old_flags;
3076        int sleep_on_un_flags;
3077
3078        if (!tty || tty->magic != TTY_MAGIC || !file || !ch ||
3079                ch->magic != DGAP_CHANNEL_MAGIC)
3080                return -EIO;
3081
3082        un = tty->driver_data;
3083        if (!un || un->magic != DGAP_UNIT_MAGIC)
3084                return -EIO;
3085
3086        spin_lock_irqsave(&ch->ch_lock, lock_flags);
3087
3088        ch->ch_wopen++;
3089
3090        /* Loop forever */
3091        while (1) {
3092
3093                sleep_on_un_flags = 0;
3094
3095                /*
3096                 * If board has failed somehow during our sleep,
3097                 * bail with error.
3098                 */
3099                if (ch->ch_bd->state == BOARD_FAILED) {
3100                        retval = -EIO;
3101                        break;
3102                }
3103
3104                /* If tty was hung up, break out of loop and set error. */
3105                if (tty_hung_up_p(file)) {
3106                        retval = -EAGAIN;
3107                        break;
3108                }
3109
3110                /*
3111                 * If either unit is in the middle of the fragile part of close,
3112                 * we just cannot touch the channel safely.
3113                 * Go back to sleep, knowing that when the channel can be
3114                 * touched safely, the close routine will signal the
3115                 * ch_wait_flags to wake us back up.
3116                 */
3117                if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) &
3118                      UN_CLOSING)) {
3119
3120                        /*
3121                         * Our conditions to leave cleanly and happily:
3122                         * 1) NONBLOCKING on the tty is set.
3123                         * 2) CLOCAL is set.
3124                         * 3) DCD (fake or real) is active.
3125                         */
3126
3127                        if (file->f_flags & O_NONBLOCK)
3128                                break;
3129
3130                        if (tty->flags & (1 << TTY_IO_ERROR))
3131                                break;
3132
3133                        if (ch->ch_flags & CH_CD)
3134                                break;
3135
3136                        if (ch->ch_flags & CH_FCAR)
3137                                break;
3138                } else {
3139                        sleep_on_un_flags = 1;
3140                }
3141
3142                /*
3143                 * If there is a signal pending, the user probably
3144                 * interrupted (ctrl-c) us.
3145                 * Leave loop with error set.
3146                 */
3147                if (signal_pending(current)) {
3148                        retval = -ERESTARTSYS;
3149                        break;
3150                }
3151
3152                /*
3153                 * Store the flags before we let go of channel lock
3154                 */
3155                if (sleep_on_un_flags)
3156                        old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
3157                else
3158                        old_flags = ch->ch_flags;
3159
3160                /*
3161                 * Let go of channel lock before calling schedule.
3162                 * Our poller will get any FEP events and wake us up when DCD
3163                 * eventually goes active.
3164                 */
3165
3166                spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3167
3168                /*
3169                 * Wait for something in the flags to change
3170                 * from the current value.
3171                 */
3172                if (sleep_on_un_flags) {
3173                        retval = wait_event_interruptible(un->un_flags_wait,
3174                                (old_flags != (ch->ch_tun.un_flags |
3175                                               ch->ch_pun.un_flags)));
3176                } else {
3177                        retval = wait_event_interruptible(ch->ch_flags_wait,
3178                                (old_flags != ch->ch_flags));
3179                }
3180
3181                /*
3182                 * We got woken up for some reason.
3183                 * Before looping around, grab our channel lock.
3184                 */
3185                spin_lock_irqsave(&ch->ch_lock, lock_flags);
3186        }
3187
3188        ch->ch_wopen--;
3189
3190        spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3191
3192        return retval;
3193}
3194
3195/*
3196 * dgap_tty_flush_buffer()
3197 *
3198 * Flush Tx buffer (make in == out)
3199 */
3200static void dgap_tty_flush_buffer(struct tty_struct *tty)
3201{
3202        struct board_t *bd;
3203        struct channel_t *ch;
3204        struct un_t *un;
3205        ulong lock_flags;
3206        ulong lock_flags2;
3207        u16 head;
3208
3209        if (!tty || tty->magic != TTY_MAGIC)
3210                return;
3211
3212        un = tty->driver_data;
3213        if (!un || un->magic != DGAP_UNIT_MAGIC)
3214                return;
3215
3216        ch = un->un_ch;
3217        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3218                return;
3219
3220        bd = ch->ch_bd;
3221        if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3222                return;
3223
3224        spin_lock_irqsave(&bd->bd_lock, lock_flags);
3225        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3226
3227        ch->ch_flags &= ~CH_STOP;
3228        head = readw(&(ch->ch_bs->tx_head));
3229        dgap_cmdw(ch, FLUSHTX, (u16) head, 0);
3230        dgap_cmdw(ch, RESUMETX, 0, 0);
3231        if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
3232                ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
3233                wake_up_interruptible(&ch->ch_tun.un_flags_wait);
3234        }
3235        if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
3236                ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
3237                wake_up_interruptible(&ch->ch_pun.un_flags_wait);
3238        }
3239
3240        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3241        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3242        if (waitqueue_active(&tty->write_wait))
3243                wake_up_interruptible(&tty->write_wait);
3244        tty_wakeup(tty);
3245}
3246
3247/*
3248 * dgap_tty_hangup()
3249 *
3250 * Hangup the port.  Like a close, but don't wait for output to drain.
3251 */
3252static void dgap_tty_hangup(struct tty_struct *tty)
3253{
3254        struct board_t *bd;
3255        struct channel_t *ch;
3256        struct un_t *un;
3257
3258        if (!tty || tty->magic != TTY_MAGIC)
3259                return;
3260
3261        un = tty->driver_data;
3262        if (!un || un->magic != DGAP_UNIT_MAGIC)
3263                return;
3264
3265        ch = un->un_ch;
3266        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3267                return;
3268
3269        bd = ch->ch_bd;
3270        if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3271                return;
3272
3273        /* flush the transmit queues */
3274        dgap_tty_flush_buffer(tty);
3275}
3276
3277/*
3278 * dgap_tty_chars_in_buffer()
3279 *
3280 * Return number of characters that have not been transmitted yet.
3281 *
3282 * This routine is used by the line discipline to determine if there
3283 * is data waiting to be transmitted/drained/flushed or not.
3284 */
3285static int dgap_tty_chars_in_buffer(struct tty_struct *tty)
3286{
3287        struct board_t *bd;
3288        struct channel_t *ch;
3289        struct un_t *un;
3290        struct bs_t __iomem *bs;
3291        u8 tbusy;
3292        uint chars;
3293        u16 thead, ttail, tmask, chead, ctail;
3294        ulong lock_flags = 0;
3295        ulong lock_flags2 = 0;
3296
3297        if (!tty)
3298                return 0;
3299
3300        un = tty->driver_data;
3301        if (!un || un->magic != DGAP_UNIT_MAGIC)
3302                return 0;
3303
3304        ch = un->un_ch;
3305        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3306                return 0;
3307
3308        bd = ch->ch_bd;
3309        if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3310                return 0;
3311
3312        bs = ch->ch_bs;
3313        if (!bs)
3314                return 0;
3315
3316        spin_lock_irqsave(&bd->bd_lock, lock_flags);
3317        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3318
3319        tmask = (ch->ch_tsize - 1);
3320
3321        /* Get Transmit queue pointers */
3322        thead = readw(&(bs->tx_head)) & tmask;
3323        ttail = readw(&(bs->tx_tail)) & tmask;
3324
3325        /* Get tbusy flag */
3326        tbusy = readb(&(bs->tbusy));
3327
3328        /* Get Command queue pointers */
3329        chead = readw(&(ch->ch_cm->cm_head));
3330        ctail = readw(&(ch->ch_cm->cm_tail));
3331
3332        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3333        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3334
3335        /*
3336         * The only way we know for sure if there is no pending
3337         * data left to be transferred, is if:
3338         * 1) Transmit head and tail are equal (empty).
3339         * 2) Command queue head and tail are equal (empty).
3340         * 3) The "TBUSY" flag is 0. (Transmitter not busy).
3341         */
3342
3343        if ((ttail == thead) && (tbusy == 0) && (chead == ctail)) {
3344                chars = 0;
3345        } else {
3346                if (thead >= ttail)
3347                        chars = thead - ttail;
3348                else
3349                        chars = thead - ttail + ch->ch_tsize;
3350                /*
3351                 * Fudge factor here.
3352                 * If chars is zero, we know that the command queue had
3353                 * something in it or tbusy was set.  Because we cannot
3354                 * be sure if there is still some data to be transmitted,
3355                 * lets lie, and tell ld we have 1 byte left.
3356                 */
3357                if (chars == 0) {
3358                        /*
3359                         * If TBUSY is still set, and our tx buffers are empty,
3360                         * force the firmware to send me another wakeup after
3361                         * TBUSY has been cleared.
3362                         */
3363                        if (tbusy != 0) {
3364                                spin_lock_irqsave(&ch->ch_lock, lock_flags);
3365                                un->un_flags |= UN_EMPTY;
3366                                writeb(1, &(bs->iempty));
3367                                spin_unlock_irqrestore(&ch->ch_lock,
3368                                                       lock_flags);
3369                        }
3370                        chars = 1;
3371                }
3372        }
3373
3374        return chars;
3375}
3376
3377static int dgap_wait_for_drain(struct tty_struct *tty)
3378{
3379        struct channel_t *ch;
3380        struct un_t *un;
3381        struct bs_t __iomem *bs;
3382        int ret = 0;
3383        uint count = 1;
3384        ulong lock_flags = 0;
3385
3386        if (!tty || tty->magic != TTY_MAGIC)
3387                return -EIO;
3388
3389        un = tty->driver_data;
3390        if (!un || un->magic != DGAP_UNIT_MAGIC)
3391                return -EIO;
3392
3393        ch = un->un_ch;
3394        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3395                return -EIO;
3396
3397        bs = ch->ch_bs;
3398        if (!bs)
3399                return -EIO;
3400
3401        /* Loop until data is drained */
3402        while (count != 0) {
3403
3404                count = dgap_tty_chars_in_buffer(tty);
3405
3406                if (count == 0)
3407                        break;
3408
3409                /* Set flag waiting for drain */
3410                spin_lock_irqsave(&ch->ch_lock, lock_flags);
3411                un->un_flags |= UN_EMPTY;
3412                writeb(1, &(bs->iempty));
3413                spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3414
3415                /* Go to sleep till we get woken up */
3416                ret = wait_event_interruptible(un->un_flags_wait,
3417                                        ((un->un_flags & UN_EMPTY) == 0));
3418                /* If ret is non-zero, user ctrl-c'ed us */
3419                if (ret)
3420                        break;
3421        }
3422
3423        spin_lock_irqsave(&ch->ch_lock, lock_flags);
3424        un->un_flags &= ~(UN_EMPTY);
3425        spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3426
3427        return ret;
3428}
3429
3430/*
3431 * dgap_maxcps_room
3432 *
3433 * Reduces bytes_available to the max number of characters
3434 * that can be sent currently given the maxcps value, and
3435 * returns the new bytes_available.  This only affects printer
3436 * output.
3437 */
3438static int dgap_maxcps_room(struct channel_t *ch, struct un_t *un,
3439                            int bytes_available)
3440{
3441        /*
3442         * If its not the Transparent print device, return
3443         * the full data amount.
3444         */
3445        if (un->un_type != DGAP_PRINT)
3446                return bytes_available;
3447
3448        if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
3449                int cps_limit = 0;
3450                unsigned long current_time = jiffies;
3451                unsigned long buffer_time = current_time +
3452                        (HZ * ch->ch_digi.digi_bufsize) /
3453                        ch->ch_digi.digi_maxcps;
3454
3455                if (ch->ch_cpstime < current_time) {
3456                        /* buffer is empty */
3457                        ch->ch_cpstime = current_time;   /* reset ch_cpstime */
3458                        cps_limit = ch->ch_digi.digi_bufsize;
3459                } else if (ch->ch_cpstime < buffer_time) {
3460                        /* still room in the buffer */
3461                        cps_limit = ((buffer_time - ch->ch_cpstime) *
3462                                     ch->ch_digi.digi_maxcps) / HZ;
3463                } else {
3464                        /* no room in the buffer */
3465                        cps_limit = 0;
3466                }
3467
3468                bytes_available = min(cps_limit, bytes_available);
3469        }
3470
3471        return bytes_available;
3472}
3473
3474static inline void dgap_set_firmware_event(struct un_t *un, unsigned int event)
3475{
3476        struct channel_t *ch;
3477        struct bs_t __iomem *bs;
3478
3479        if (!un || un->magic != DGAP_UNIT_MAGIC)
3480                return;
3481        ch = un->un_ch;
3482        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3483                return;
3484        bs = ch->ch_bs;
3485        if (!bs)
3486                return;
3487
3488        if ((event & UN_LOW) != 0) {
3489                if ((un->un_flags & UN_LOW) == 0) {
3490                        un->un_flags |= UN_LOW;
3491                        writeb(1, &(bs->ilow));
3492                }
3493        }
3494        if ((event & UN_LOW) != 0) {
3495                if ((un->un_flags & UN_EMPTY) == 0) {
3496                        un->un_flags |= UN_EMPTY;
3497                        writeb(1, &(bs->iempty));
3498                }
3499        }
3500}
3501
3502/*
3503 * dgap_tty_write_room()
3504 *
3505 * Return space available in Tx buffer
3506 */
3507static int dgap_tty_write_room(struct tty_struct *tty)
3508{
3509        struct channel_t *ch;
3510        struct un_t *un;
3511        struct bs_t __iomem *bs;
3512        u16 head, tail, tmask;
3513        int ret;
3514        ulong lock_flags = 0;
3515
3516        if (!tty)
3517                return 0;
3518
3519        un = tty->driver_data;
3520        if (!un || un->magic != DGAP_UNIT_MAGIC)
3521                return 0;
3522
3523        ch = un->un_ch;
3524        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3525                return 0;
3526
3527        bs = ch->ch_bs;
3528        if (!bs)
3529                return 0;
3530
3531        spin_lock_irqsave(&ch->ch_lock, lock_flags);
3532
3533        tmask = ch->ch_tsize - 1;
3534        head = readw(&(bs->tx_head)) & tmask;
3535        tail = readw(&(bs->tx_tail)) & tmask;
3536
3537        ret = tail - head - 1;
3538        if (ret < 0)
3539                ret += ch->ch_tsize;
3540
3541        /* Limit printer to maxcps */
3542        ret = dgap_maxcps_room(ch, un, ret);
3543
3544        /*
3545         * If we are printer device, leave space for
3546         * possibly both the on and off strings.
3547         */
3548        if (un->un_type == DGAP_PRINT) {
3549                if (!(ch->ch_flags & CH_PRON))
3550                        ret -= ch->ch_digi.digi_onlen;
3551                ret -= ch->ch_digi.digi_offlen;
3552        } else {
3553                if (ch->ch_flags & CH_PRON)
3554                        ret -= ch->ch_digi.digi_offlen;
3555        }
3556
3557        if (ret < 0)
3558                ret = 0;
3559
3560        /*
3561         * Schedule FEP to wake us up if needed.
3562         *
3563         * TODO:  This might be overkill...
3564         * Do we really need to schedule callbacks from the FEP
3565         * in every case?  Can we get smarter based on ret?
3566         */
3567        dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
3568        spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3569
3570        return ret;
3571}
3572
3573/*
3574 * dgap_tty_write()
3575 *
3576 * Take data from the user or kernel and send it out to the FEP.
3577 * In here exists all the Transparent Print magic as well.
3578 */
3579static int dgap_tty_write(struct tty_struct *tty, const unsigned char *buf,
3580                                int count)
3581{
3582        struct channel_t *ch;
3583        struct un_t *un;
3584        struct bs_t __iomem *bs;
3585        char __iomem *vaddr;
3586        u16 head, tail, tmask, remain;
3587        int bufcount, n;
3588        ulong lock_flags;
3589
3590        if (!tty)
3591                return 0;
3592
3593        un = tty->driver_data;
3594        if (!un || un->magic != DGAP_UNIT_MAGIC)
3595                return 0;
3596
3597        ch = un->un_ch;
3598        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3599                return 0;
3600
3601        bs = ch->ch_bs;
3602        if (!bs)
3603                return 0;
3604
3605        if (!count)
3606                return 0;
3607
3608        spin_lock_irqsave(&ch->ch_lock, lock_flags);
3609
3610        /* Get our space available for the channel from the board */
3611        tmask = ch->ch_tsize - 1;
3612        head = readw(&(bs->tx_head)) & tmask;
3613        tail = readw(&(bs->tx_tail)) & tmask;
3614
3615        bufcount = tail - head - 1;
3616        if (bufcount < 0)
3617                bufcount += ch->ch_tsize;
3618
3619        /*
3620         * Limit printer output to maxcps overall, with bursts allowed
3621         * up to bufsize characters.
3622         */
3623        bufcount = dgap_maxcps_room(ch, un, bufcount);
3624
3625        /*
3626         * Take minimum of what the user wants to send, and the
3627         * space available in the FEP buffer.
3628         */
3629        count = min(count, bufcount);
3630
3631        /*
3632         * Bail if no space left.
3633         */
3634        if (count <= 0) {
3635                dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
3636                spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3637                return 0;
3638        }
3639
3640        /*
3641         * Output the printer ON string, if we are in terminal mode, but
3642         * need to be in printer mode.
3643         */
3644        if ((un->un_type == DGAP_PRINT) && !(ch->ch_flags & CH_PRON)) {
3645                dgap_wmove(ch, ch->ch_digi.digi_onstr,
3646                    (int) ch->ch_digi.digi_onlen);
3647                head = readw(&(bs->tx_head)) & tmask;
3648                ch->ch_flags |= CH_PRON;
3649        }
3650
3651        /*
3652         * On the other hand, output the printer OFF string, if we are
3653         * currently in printer mode, but need to output to the terminal.
3654         */
3655        if ((un->un_type != DGAP_PRINT) && (ch->ch_flags & CH_PRON)) {
3656                dgap_wmove(ch, ch->ch_digi.digi_offstr,
3657                        (int) ch->ch_digi.digi_offlen);
3658                head = readw(&(bs->tx_head)) & tmask;
3659                ch->ch_flags &= ~CH_PRON;
3660        }
3661
3662        n = count;
3663
3664        /*
3665         * If the write wraps over the top of the circular buffer,
3666         * move the portion up to the wrap point, and reset the
3667         * pointers to the bottom.
3668         */
3669        remain = ch->ch_tstart + ch->ch_tsize - head;
3670
3671        if (n >= remain) {
3672                n -= remain;
3673                vaddr = ch->ch_taddr + head;
3674
3675                memcpy_toio(vaddr, (u8 *) buf, remain);
3676
3677                head = ch->ch_tstart;
3678                buf += remain;
3679        }
3680
3681        if (n > 0) {
3682
3683                /*
3684                 * Move rest of data.
3685                 */
3686                vaddr = ch->ch_taddr + head;
3687                remain = n;
3688
3689                memcpy_toio(vaddr, (u8 *) buf, remain);
3690                head += remain;
3691
3692        }
3693
3694        if (count) {
3695                ch->ch_txcount += count;
3696                head &= tmask;
3697                writew(head, &(bs->tx_head));
3698        }
3699
3700        dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
3701
3702        /*
3703         * If this is the print device, and the
3704         * printer is still on, we need to turn it
3705         * off before going idle.  If the buffer is
3706         * non-empty, wait until it goes empty.
3707         * Otherwise turn it off right now.
3708         */
3709        if ((un->un_type == DGAP_PRINT) && (ch->ch_flags & CH_PRON)) {
3710                tail = readw(&(bs->tx_tail)) & tmask;
3711
3712                if (tail != head) {
3713                        un->un_flags |= UN_EMPTY;
3714                        writeb(1, &(bs->iempty));
3715                } else {
3716                        dgap_wmove(ch, ch->ch_digi.digi_offstr,
3717                                (int) ch->ch_digi.digi_offlen);
3718                        head = readw(&(bs->tx_head)) & tmask;
3719                        ch->ch_flags &= ~CH_PRON;
3720                }
3721        }
3722
3723        /* Update printer buffer empty time. */
3724        if ((un->un_type == DGAP_PRINT) && (ch->ch_digi.digi_maxcps > 0)
3725            && (ch->ch_digi.digi_bufsize > 0)) {
3726                ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
3727        }
3728
3729        spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3730
3731        return count;
3732}
3733
3734/*
3735 * dgap_tty_put_char()
3736 *
3737 * Put a character into ch->ch_buf
3738 *
3739 *      - used by the line discipline for OPOST processing
3740 */
3741static int dgap_tty_put_char(struct tty_struct *tty, unsigned char c)
3742{
3743        /*
3744         * Simply call tty_write.
3745         */
3746        dgap_tty_write(tty, &c, 1);
3747        return 1;
3748}
3749
3750/*
3751 * Return modem signals to ld.
3752 */
3753static int dgap_tty_tiocmget(struct tty_struct *tty)
3754{
3755        struct channel_t *ch;
3756        struct un_t *un;
3757        int result;
3758        u8 mstat;
3759        ulong lock_flags;
3760
3761        if (!tty || tty->magic != TTY_MAGIC)
3762                return -EIO;
3763
3764        un = tty->driver_data;
3765        if (!un || un->magic != DGAP_UNIT_MAGIC)
3766                return -EIO;
3767
3768        ch = un->un_ch;
3769        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3770                return -EIO;
3771
3772        spin_lock_irqsave(&ch->ch_lock, lock_flags);
3773
3774        mstat = readb(&(ch->ch_bs->m_stat));
3775        /* Append any outbound signals that might be pending... */
3776        mstat |= ch->ch_mostat;
3777
3778        spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3779
3780        result = 0;
3781
3782        if (mstat & D_DTR(ch))
3783                result |= TIOCM_DTR;
3784        if (mstat & D_RTS(ch))
3785                result |= TIOCM_RTS;
3786        if (mstat & D_CTS(ch))
3787                result |= TIOCM_CTS;
3788        if (mstat & D_DSR(ch))
3789                result |= TIOCM_DSR;
3790        if (mstat & D_RI(ch))
3791                result |= TIOCM_RI;
3792        if (mstat & D_CD(ch))
3793                result |= TIOCM_CD;
3794
3795        return result;
3796}
3797
3798/*
3799 * dgap_tty_tiocmset()
3800 *
3801 * Set modem signals, called by ld.
3802 */
3803static int dgap_tty_tiocmset(struct tty_struct *tty,
3804                unsigned int set, unsigned int clear)
3805{
3806        struct board_t *bd;
3807        struct channel_t *ch;
3808        struct un_t *un;
3809        ulong lock_flags;
3810        ulong lock_flags2;
3811
3812        if (!tty || tty->magic != TTY_MAGIC)
3813                return -EIO;
3814
3815        un = tty->driver_data;
3816        if (!un || un->magic != DGAP_UNIT_MAGIC)
3817                return -EIO;
3818
3819        ch = un->un_ch;
3820        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3821                return -EIO;
3822
3823        bd = ch->ch_bd;
3824        if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3825                return -EIO;
3826
3827        spin_lock_irqsave(&bd->bd_lock, lock_flags);
3828        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3829
3830        if (set & TIOCM_RTS) {
3831                ch->ch_mforce |= D_RTS(ch);
3832                ch->ch_mval   |= D_RTS(ch);
3833        }
3834
3835        if (set & TIOCM_DTR) {
3836                ch->ch_mforce |= D_DTR(ch);
3837                ch->ch_mval   |= D_DTR(ch);
3838        }
3839
3840        if (clear & TIOCM_RTS) {
3841                ch->ch_mforce |= D_RTS(ch);
3842                ch->ch_mval   &= ~(D_RTS(ch));
3843        }
3844
3845        if (clear & TIOCM_DTR) {
3846                ch->ch_mforce |= D_DTR(ch);
3847                ch->ch_mval   &= ~(D_DTR(ch));
3848        }
3849
3850        dgap_param(ch, bd, un->un_type);
3851
3852        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3853        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3854
3855        return 0;
3856}
3857
3858/*
3859 * dgap_tty_send_break()
3860 *
3861 * Send a Break, called by ld.
3862 */
3863static int dgap_tty_send_break(struct tty_struct *tty, int msec)
3864{
3865        struct board_t *bd;
3866        struct channel_t *ch;
3867        struct un_t *un;
3868        ulong lock_flags;
3869        ulong lock_flags2;
3870
3871        if (!tty || tty->magic != TTY_MAGIC)
3872                return -EIO;
3873
3874        un = tty->driver_data;
3875        if (!un || un->magic != DGAP_UNIT_MAGIC)
3876                return -EIO;
3877
3878        ch = un->un_ch;
3879        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3880                return -EIO;
3881
3882        bd = ch->ch_bd;
3883        if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3884                return -EIO;
3885
3886        switch (msec) {
3887        case -1:
3888                msec = 0xFFFF;
3889                break;
3890        case 0:
3891                msec = 1;
3892                break;
3893        default:
3894                msec /= 10;
3895                break;
3896        }
3897
3898        spin_lock_irqsave(&bd->bd_lock, lock_flags);
3899        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3900#if 0
3901        dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
3902#endif
3903        dgap_cmdw(ch, SBREAK, (u16) msec, 0);
3904
3905        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3906        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3907
3908        return 0;
3909}
3910
3911/*
3912 * dgap_tty_wait_until_sent()
3913 *
3914 * wait until data has been transmitted, called by ld.
3915 */
3916static void dgap_tty_wait_until_sent(struct tty_struct *tty, int timeout)
3917{
3918        dgap_wait_for_drain(tty);
3919}
3920
3921/*
3922 * dgap_send_xchar()
3923 *
3924 * send a high priority character, called by ld.
3925 */
3926static void dgap_tty_send_xchar(struct tty_struct *tty, char c)
3927{
3928        struct board_t *bd;
3929        struct channel_t *ch;
3930        struct un_t *un;
3931        ulong lock_flags;
3932        ulong lock_flags2;
3933
3934        if (!tty || tty->magic != TTY_MAGIC)
3935                return;
3936
3937        un = tty->driver_data;
3938        if (!un || un->magic != DGAP_UNIT_MAGIC)
3939                return;
3940
3941        ch = un->un_ch;
3942        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3943                return;
3944
3945        bd = ch->ch_bd;
3946        if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3947                return;
3948
3949        spin_lock_irqsave(&bd->bd_lock, lock_flags);
3950        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3951
3952        /*
3953         * This is technically what we should do.
3954         * However, the NIST tests specifically want
3955         * to see each XON or XOFF character that it
3956         * sends, so lets just send each character
3957         * by hand...
3958         */
3959#if 0
3960        if (c == STOP_CHAR(tty))
3961                dgap_cmdw(ch, RPAUSE, 0, 0);
3962        else if (c == START_CHAR(tty))
3963                dgap_cmdw(ch, RRESUME, 0, 0);
3964        else
3965                dgap_wmove(ch, &c, 1);
3966#else
3967        dgap_wmove(ch, &c, 1);
3968#endif
3969
3970        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3971        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3972}
3973
3974/*
3975 * Return modem signals to ld.
3976 */
3977static int dgap_get_modem_info(struct channel_t *ch, unsigned int __user *value)
3978{
3979        int result;
3980        u8 mstat;
3981        ulong lock_flags;
3982        int rc;
3983
3984        spin_lock_irqsave(&ch->ch_lock, lock_flags);
3985
3986        mstat = readb(&(ch->ch_bs->m_stat));
3987        /* Append any outbound signals that might be pending... */
3988        mstat |= ch->ch_mostat;
3989
3990        spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3991
3992        result = 0;
3993
3994        if (mstat & D_DTR(ch))
3995                result |= TIOCM_DTR;
3996        if (mstat & D_RTS(ch))
3997                result |= TIOCM_RTS;
3998        if (mstat & D_CTS(ch))
3999                result |= TIOCM_CTS;
4000        if (mstat & D_DSR(ch))
4001                result |= TIOCM_DSR;
4002        if (mstat & D_RI(ch))
4003                result |= TIOCM_RI;
4004        if (mstat & D_CD(ch))
4005                result |= TIOCM_CD;
4006
4007        rc = put_user(result, value);
4008
4009        return rc;
4010}
4011
4012/*
4013 * dgap_set_modem_info()
4014 *
4015 * Set modem signals, called by ld.
4016 */
4017static int dgap_set_modem_info(struct channel_t *ch, struct board_t *bd,
4018                               struct un_t *un, unsigned int command,
4019                               unsigned int __user *value)
4020{
4021        int ret;
4022        unsigned int arg;
4023        ulong lock_flags;
4024        ulong lock_flags2;
4025
4026        ret = get_user(arg, value);
4027        if (ret)
4028                return ret;
4029
4030        switch (command) {
4031        case TIOCMBIS:
4032                if (arg & TIOCM_RTS) {
4033                        ch->ch_mforce |= D_RTS(ch);
4034                        ch->ch_mval   |= D_RTS(ch);
4035                }
4036
4037                if (arg & TIOCM_DTR) {
4038                        ch->ch_mforce |= D_DTR(ch);
4039                        ch->ch_mval   |= D_DTR(ch);
4040                }
4041
4042                break;
4043
4044        case TIOCMBIC:
4045                if (arg & TIOCM_RTS) {
4046                        ch->ch_mforce |= D_RTS(ch);
4047                        ch->ch_mval   &= ~(D_RTS(ch));
4048                }
4049
4050                if (arg & TIOCM_DTR) {
4051                        ch->ch_mforce |= D_DTR(ch);
4052                        ch->ch_mval   &= ~(D_DTR(ch));
4053                }
4054
4055                break;
4056
4057        case TIOCMSET:
4058                ch->ch_mforce = D_DTR(ch)|D_RTS(ch);
4059
4060                if (arg & TIOCM_RTS)
4061                        ch->ch_mval |= D_RTS(ch);
4062                else
4063                        ch->ch_mval &= ~(D_RTS(ch));
4064
4065                if (arg & TIOCM_DTR)
4066                        ch->ch_mval |= (D_DTR(ch));
4067                else
4068                        ch->ch_mval &= ~(D_DTR(ch));
4069
4070                break;
4071
4072        default:
4073                return -EINVAL;
4074        }
4075
4076        spin_lock_irqsave(&bd->bd_lock, lock_flags);
4077        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4078
4079        dgap_param(ch, bd, un->un_type);
4080
4081        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4082        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4083
4084        return 0;
4085}
4086
4087/*
4088 * dgap_tty_digigeta()
4089 *
4090 * Ioctl to get the information for ditty.
4091 *
4092 *
4093 *
4094 */
4095static int dgap_tty_digigeta(struct channel_t *ch,
4096                             struct digi_t __user *retinfo)
4097{
4098        struct digi_t tmp;
4099        ulong lock_flags;
4100
4101        if (!retinfo)
4102                return -EFAULT;
4103
4104        memset(&tmp, 0, sizeof(tmp));
4105
4106        spin_lock_irqsave(&ch->ch_lock, lock_flags);
4107        memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
4108        spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4109
4110        if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
4111                return -EFAULT;
4112
4113        return 0;
4114}
4115
4116/*
4117 * dgap_tty_digiseta()
4118 *
4119 * Ioctl to set the information for ditty.
4120 *
4121 *
4122 *
4123 */
4124static int dgap_tty_digiseta(struct channel_t *ch, struct board_t *bd,
4125                             struct un_t *un, struct digi_t __user *new_info)
4126{
4127        struct digi_t new_digi;
4128        ulong lock_flags = 0;
4129        unsigned long lock_flags2;
4130
4131        if (copy_from_user(&new_digi, new_info, sizeof(struct digi_t)))
4132                return -EFAULT;
4133
4134        spin_lock_irqsave(&bd->bd_lock, lock_flags);
4135        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4136
4137        memcpy(&ch->ch_digi, &new_digi, sizeof(struct digi_t));
4138
4139        if (ch->ch_digi.digi_maxcps < 1)
4140                ch->ch_digi.digi_maxcps = 1;
4141
4142        if (ch->ch_digi.digi_maxcps > 10000)
4143                ch->ch_digi.digi_maxcps = 10000;
4144
4145        if (ch->ch_digi.digi_bufsize < 10)
4146                ch->ch_digi.digi_bufsize = 10;
4147
4148        if (ch->ch_digi.digi_maxchar < 1)
4149                ch->ch_digi.digi_maxchar = 1;
4150
4151        if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
4152                ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
4153
4154        if (ch->ch_digi.digi_onlen > DIGI_PLEN)
4155                ch->ch_digi.digi_onlen = DIGI_PLEN;
4156
4157        if (ch->ch_digi.digi_offlen > DIGI_PLEN)
4158                ch->ch_digi.digi_offlen = DIGI_PLEN;
4159
4160        dgap_param(ch, bd, un->un_type);
4161
4162        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4163        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4164
4165        return 0;
4166}
4167
4168/*
4169 * dgap_tty_digigetedelay()
4170 *
4171 * Ioctl to get the current edelay setting.
4172 *
4173 *
4174 *
4175 */
4176static int dgap_tty_digigetedelay(struct tty_struct *tty, int __user *retinfo)
4177{
4178        struct channel_t *ch;
4179        struct un_t *un;
4180        int tmp;
4181        ulong lock_flags;
4182
4183        if (!retinfo)
4184                return -EFAULT;
4185
4186        if (!tty || tty->magic != TTY_MAGIC)
4187                return -EFAULT;
4188
4189        un = tty->driver_data;
4190        if (!un || un->magic != DGAP_UNIT_MAGIC)
4191                return -EFAULT;
4192
4193        ch = un->un_ch;
4194        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4195                return -EFAULT;
4196
4197        memset(&tmp, 0, sizeof(tmp));
4198
4199        spin_lock_irqsave(&ch->ch_lock, lock_flags);
4200        tmp = readw(&(ch->ch_bs->edelay));
4201        spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4202
4203        if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
4204                return -EFAULT;
4205
4206        return 0;
4207}
4208
4209/*
4210 * dgap_tty_digisetedelay()
4211 *
4212 * Ioctl to set the EDELAY setting
4213 *
4214 */
4215static int dgap_tty_digisetedelay(struct channel_t *ch, struct board_t *bd,
4216                                  struct un_t *un, int __user *new_info)
4217{
4218        int new_digi;
4219        ulong lock_flags;
4220        ulong lock_flags2;
4221
4222        if (copy_from_user(&new_digi, new_info, sizeof(int)))
4223                return -EFAULT;
4224
4225        spin_lock_irqsave(&bd->bd_lock, lock_flags);
4226        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4227
4228        writew((u16) new_digi, &(ch->ch_bs->edelay));
4229
4230        dgap_param(ch, bd, un->un_type);
4231
4232        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4233        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4234
4235        return 0;
4236}
4237
4238/*
4239 * dgap_tty_digigetcustombaud()
4240 *
4241 * Ioctl to get the current custom baud rate setting.
4242 */
4243static int dgap_tty_digigetcustombaud(struct channel_t *ch, struct un_t *un,
4244                                      int __user *retinfo)
4245{
4246        int tmp;
4247        ulong lock_flags;
4248
4249        if (!retinfo)
4250                return -EFAULT;
4251
4252        memset(&tmp, 0, sizeof(tmp));
4253
4254        spin_lock_irqsave(&ch->ch_lock, lock_flags);
4255        tmp = dgap_get_custom_baud(ch);
4256        spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4257
4258        if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
4259                return -EFAULT;
4260
4261        return 0;
4262}
4263
4264/*
4265 * dgap_tty_digisetcustombaud()
4266 *
4267 * Ioctl to set the custom baud rate setting
4268 */
4269static int dgap_tty_digisetcustombaud(struct channel_t *ch, struct board_t *bd,
4270                                      struct un_t *un, int __user *new_info)
4271{
4272        uint new_rate;
4273        ulong lock_flags;
4274        ulong lock_flags2;
4275
4276        if (copy_from_user(&new_rate, new_info, sizeof(unsigned int)))
4277                return -EFAULT;
4278
4279        if (bd->bd_flags & BD_FEP5PLUS) {
4280
4281                spin_lock_irqsave(&bd->bd_lock, lock_flags);
4282                spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4283
4284                ch->ch_custom_speed = new_rate;
4285
4286                dgap_param(ch, bd, un->un_type);
4287
4288                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4289                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4290        }
4291
4292        return 0;
4293}
4294
4295/*
4296 * dgap_set_termios()
4297 */
4298static void dgap_tty_set_termios(struct tty_struct *tty,
4299                                struct ktermios *old_termios)
4300{
4301        struct board_t *bd;
4302        struct channel_t *ch;
4303        struct un_t *un;
4304        unsigned long lock_flags;
4305        unsigned long lock_flags2;
4306
4307        if (!tty || tty->magic != TTY_MAGIC)
4308                return;
4309
4310        un = tty->driver_data;
4311        if (!un || un->magic != DGAP_UNIT_MAGIC)
4312                return;
4313
4314        ch = un->un_ch;
4315        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4316                return;
4317
4318        bd = ch->ch_bd;
4319        if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4320                return;
4321
4322        spin_lock_irqsave(&bd->bd_lock, lock_flags);
4323        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4324
4325        ch->ch_c_cflag   = tty->termios.c_cflag;
4326        ch->ch_c_iflag   = tty->termios.c_iflag;
4327        ch->ch_c_oflag   = tty->termios.c_oflag;
4328        ch->ch_c_lflag   = tty->termios.c_lflag;
4329        ch->ch_startc    = tty->termios.c_cc[VSTART];
4330        ch->ch_stopc     = tty->termios.c_cc[VSTOP];
4331
4332        dgap_carrier(ch);
4333        dgap_param(ch, bd, un->un_type);
4334
4335        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4336        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4337}
4338
4339static void dgap_tty_throttle(struct tty_struct *tty)
4340{
4341        struct board_t *bd;
4342        struct channel_t *ch;
4343        struct un_t *un;
4344        ulong lock_flags;
4345        ulong lock_flags2;
4346
4347        if (!tty || tty->magic != TTY_MAGIC)
4348                return;
4349
4350        un = tty->driver_data;
4351        if (!un || un->magic != DGAP_UNIT_MAGIC)
4352                return;
4353
4354        ch = un->un_ch;
4355        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4356                return;
4357
4358        bd = ch->ch_bd;
4359        if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4360                return;
4361
4362        spin_lock_irqsave(&bd->bd_lock, lock_flags);
4363        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4364
4365        ch->ch_flags |= (CH_RXBLOCK);
4366#if 1
4367        dgap_cmdw(ch, RPAUSE, 0, 0);
4368#endif
4369
4370        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4371        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4372
4373}
4374
4375static void dgap_tty_unthrottle(struct tty_struct *tty)
4376{
4377        struct board_t *bd;
4378        struct channel_t *ch;
4379        struct un_t *un;
4380        ulong lock_flags;
4381        ulong lock_flags2;
4382
4383        if (!tty || tty->magic != TTY_MAGIC)
4384                return;
4385
4386        un = tty->driver_data;
4387        if (!un || un->magic != DGAP_UNIT_MAGIC)
4388                return;
4389
4390        ch = un->un_ch;
4391        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4392                return;
4393
4394        bd = ch->ch_bd;
4395        if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4396                return;
4397
4398        spin_lock_irqsave(&bd->bd_lock, lock_flags);
4399        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4400
4401        ch->ch_flags &= ~(CH_RXBLOCK);
4402
4403#if 1
4404        dgap_cmdw(ch, RRESUME, 0, 0);
4405#endif
4406
4407        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4408        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4409}
4410
4411static struct board_t *find_board_by_major(unsigned int major)
4412{
4413        unsigned int i;
4414
4415        for (i = 0; i < MAXBOARDS; i++) {
4416                struct board_t *brd = dgap_board[i];
4417
4418                if (!brd)
4419                        return NULL;
4420                if (major == brd->serial_driver->major ||
4421                    major == brd->print_driver->major)
4422                        return brd;
4423        }
4424
4425        return NULL;
4426}
4427
4428/************************************************************************
4429 *
4430 * TTY Entry points and helper functions
4431 *
4432 ************************************************************************/
4433
4434/*
4435 * dgap_tty_open()
4436 *
4437 */
4438static int dgap_tty_open(struct tty_struct *tty, struct file *file)
4439{
4440        struct board_t *brd;
4441        struct channel_t *ch;
4442        struct un_t *un;
4443        struct bs_t __iomem *bs;
4444        uint major;
4445        uint minor;
4446        int rc;
4447        ulong lock_flags;
4448        ulong lock_flags2;
4449        u16 head;
4450
4451        major = MAJOR(tty_devnum(tty));
4452        minor = MINOR(tty_devnum(tty));
4453
4454        brd = find_board_by_major(major);
4455        if (!brd)
4456                return -EIO;
4457
4458        /*
4459         * If board is not yet up to a state of READY, go to
4460         * sleep waiting for it to happen or they cancel the open.
4461         */
4462        rc = wait_event_interruptible(brd->state_wait,
4463                (brd->state & BOARD_READY));
4464
4465        if (rc)
4466                return rc;
4467
4468        spin_lock_irqsave(&brd->bd_lock, lock_flags);
4469
4470        /* The wait above should guarantee this cannot happen */
4471        if (brd->state != BOARD_READY) {
4472                spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4473                return -EIO;
4474        }
4475
4476        /* If opened device is greater than our number of ports, bail. */
4477        if (MINOR(tty_devnum(tty)) > brd->nasync) {
4478                spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4479                return -EIO;
4480        }
4481
4482        ch = brd->channels[minor];
4483        if (!ch) {
4484                spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4485                return -EIO;
4486        }
4487
4488        /* Grab channel lock */
4489        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4490
4491        /* Figure out our type */
4492        if (major == brd->serial_driver->major) {
4493                un = &brd->channels[minor]->ch_tun;
4494                un->un_type = DGAP_SERIAL;
4495        } else if (major == brd->print_driver->major) {
4496                un = &brd->channels[minor]->ch_pun;
4497                un->un_type = DGAP_PRINT;
4498        } else {
4499                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4500                spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4501                return -EIO;
4502        }
4503
4504        /* Store our unit into driver_data, so we always have it available. */
4505        tty->driver_data = un;
4506
4507        /*
4508         * Error if channel info pointer is NULL.
4509         */
4510        bs = ch->ch_bs;
4511        if (!bs) {
4512                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4513                spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4514                return -EIO;
4515        }
4516
4517        /*
4518         * Initialize tty's
4519         */
4520        if (!(un->un_flags & UN_ISOPEN)) {
4521                /* Store important variables. */
4522                un->un_tty     = tty;
4523
4524                /* Maybe do something here to the TTY struct as well? */
4525        }
4526
4527        /*
4528         * Initialize if neither terminal or printer is open.
4529         */
4530        if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
4531
4532                ch->ch_mforce = 0;
4533                ch->ch_mval = 0;
4534
4535                /*
4536                 * Flush input queue.
4537                 */
4538                head = readw(&(bs->rx_head));
4539                writew(head, &(bs->rx_tail));
4540
4541                ch->ch_flags = 0;
4542                ch->pscan_state = 0;
4543                ch->pscan_savechar = 0;
4544
4545                ch->ch_c_cflag   = tty->termios.c_cflag;
4546                ch->ch_c_iflag   = tty->termios.c_iflag;
4547                ch->ch_c_oflag   = tty->termios.c_oflag;
4548                ch->ch_c_lflag   = tty->termios.c_lflag;
4549                ch->ch_startc = tty->termios.c_cc[VSTART];
4550                ch->ch_stopc  = tty->termios.c_cc[VSTOP];
4551
4552                /* TODO: flush our TTY struct here? */
4553        }
4554
4555        dgap_carrier(ch);
4556        /*
4557         * Run param in case we changed anything
4558         */
4559        dgap_param(ch, brd, un->un_type);
4560
4561        /*
4562         * follow protocol for opening port
4563         */
4564
4565        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4566        spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4567
4568        rc = dgap_block_til_ready(tty, file, ch);
4569
4570        if (!un->un_tty)
4571                return -ENODEV;
4572
4573        /* No going back now, increment our unit and channel counters */
4574        spin_lock_irqsave(&ch->ch_lock, lock_flags);
4575        ch->ch_open_count++;
4576        un->un_open_count++;
4577        un->un_flags |= (UN_ISOPEN);
4578        spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4579
4580        return rc;
4581}
4582
4583/*
4584 * dgap_tty_close()
4585 *
4586 */
4587static void dgap_tty_close(struct tty_struct *tty, struct file *file)
4588{
4589        struct ktermios *ts;
4590        struct board_t *bd;
4591        struct channel_t *ch;
4592        struct un_t *un;
4593        ulong lock_flags;
4594
4595        if (!tty || tty->magic != TTY_MAGIC)
4596                return;
4597
4598        un = tty->driver_data;
4599        if (!un || un->magic != DGAP_UNIT_MAGIC)
4600                return;
4601
4602        ch = un->un_ch;
4603        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4604                return;
4605
4606        bd = ch->ch_bd;
4607        if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4608                return;
4609
4610        ts = &tty->termios;
4611
4612        spin_lock_irqsave(&ch->ch_lock, lock_flags);
4613
4614        /*
4615         * Determine if this is the last close or not - and if we agree about
4616         * which type of close it is with the Line Discipline
4617         */
4618        if ((tty->count == 1) && (un->un_open_count != 1)) {
4619                /*
4620                 * Uh, oh.  tty->count is 1, which means that the tty
4621                 * structure will be freed.  un_open_count should always
4622                 * be one in these conditions.  If it's greater than
4623                 * one, we've got real problems, since it means the
4624                 * serial port won't be shutdown.
4625                 */
4626                un->un_open_count = 1;
4627        }
4628
4629        if (--un->un_open_count < 0)
4630                un->un_open_count = 0;
4631
4632        ch->ch_open_count--;
4633
4634        if (ch->ch_open_count && un->un_open_count) {
4635                spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4636                return;
4637        }
4638
4639        /* OK, its the last close on the unit */
4640
4641        un->un_flags |= UN_CLOSING;
4642
4643        tty->closing = 1;
4644
4645        /*
4646         * Only officially close channel if count is 0 and
4647         * DIGI_PRINTER bit is not set.
4648         */
4649        if ((ch->ch_open_count == 0) &&
4650            !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
4651
4652                ch->ch_flags &= ~(CH_RXBLOCK);
4653
4654                spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4655
4656                /* wait for output to drain */
4657                /* This will also return if we take an interrupt */
4658
4659                dgap_wait_for_drain(tty);
4660
4661                dgap_tty_flush_buffer(tty);
4662                tty_ldisc_flush(tty);
4663
4664                spin_lock_irqsave(&ch->ch_lock, lock_flags);
4665
4666                tty->closing = 0;
4667
4668                /*
4669                 * If we have HUPCL set, lower DTR and RTS
4670                 */
4671                if (ch->ch_c_cflag & HUPCL) {
4672                        ch->ch_mostat &= ~(D_RTS(ch)|D_DTR(ch));
4673                        dgap_cmdb(ch, SMODEM, 0, D_DTR(ch)|D_RTS(ch), 0);
4674
4675                        /*
4676                         * Go to sleep to ensure RTS/DTR
4677                         * have been dropped for modems to see it.
4678                         */
4679                        spin_unlock_irqrestore(&ch->ch_lock,
4680                                        lock_flags);
4681
4682                        /* .25 second delay for dropping RTS/DTR */
4683                        schedule_timeout_interruptible(msecs_to_jiffies(250));
4684
4685                        spin_lock_irqsave(&ch->ch_lock, lock_flags);
4686                }
4687
4688                ch->pscan_state = 0;
4689                ch->pscan_savechar = 0;
4690                ch->ch_baud_info = 0;
4691
4692        }
4693
4694        /*
4695         * turn off print device when closing print device.
4696         */
4697        if ((un->un_type == DGAP_PRINT)  && (ch->ch_flags & CH_PRON)) {
4698                dgap_wmove(ch, ch->ch_digi.digi_offstr,
4699                        (int) ch->ch_digi.digi_offlen);
4700                ch->ch_flags &= ~CH_PRON;
4701        }
4702
4703        un->un_tty = NULL;
4704        un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
4705        tty->driver_data = NULL;
4706
4707        wake_up_interruptible(&ch->ch_flags_wait);
4708        wake_up_interruptible(&un->un_flags_wait);
4709
4710        spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4711}
4712
4713static void dgap_tty_start(struct tty_struct *tty)
4714{
4715        struct board_t *bd;
4716        struct channel_t *ch;
4717        struct un_t *un;
4718        ulong lock_flags;
4719        ulong lock_flags2;
4720
4721        if (!tty || tty->magic != TTY_MAGIC)
4722                return;
4723
4724        un = tty->driver_data;
4725        if (!un || un->magic != DGAP_UNIT_MAGIC)
4726                return;
4727
4728        ch = un->un_ch;
4729        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4730                return;
4731
4732        bd = ch->ch_bd;
4733        if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4734                return;
4735
4736        spin_lock_irqsave(&bd->bd_lock, lock_flags);
4737        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4738
4739        dgap_cmdw(ch, RESUMETX, 0, 0);
4740
4741        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4742        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4743}
4744
4745static void dgap_tty_stop(struct tty_struct *tty)
4746{
4747        struct board_t *bd;
4748        struct channel_t *ch;
4749        struct un_t *un;
4750        ulong lock_flags;
4751        ulong lock_flags2;
4752
4753        if (!tty || tty->magic != TTY_MAGIC)
4754                return;
4755
4756        un = tty->driver_data;
4757        if (!un || un->magic != DGAP_UNIT_MAGIC)
4758                return;
4759
4760        ch = un->un_ch;
4761        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4762                return;
4763
4764        bd = ch->ch_bd;
4765        if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4766                return;
4767
4768        spin_lock_irqsave(&bd->bd_lock, lock_flags);
4769        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4770
4771        dgap_cmdw(ch, PAUSETX, 0, 0);
4772
4773        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4774        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4775}
4776
4777/*
4778 * dgap_tty_flush_chars()
4779 *
4780 * Flush the cook buffer
4781 *
4782 * Note to self, and any other poor souls who venture here:
4783 *
4784 * flush in this case DOES NOT mean dispose of the data.
4785 * instead, it means "stop buffering and send it if you
4786 * haven't already."  Just guess how I figured that out...   SRW 2-Jun-98
4787 *
4788 * It is also always called in interrupt context - JAR 8-Sept-99
4789 */
4790static void dgap_tty_flush_chars(struct tty_struct *tty)
4791{
4792        struct board_t *bd;
4793        struct channel_t *ch;
4794        struct un_t *un;
4795        ulong lock_flags;
4796        ulong lock_flags2;
4797
4798        if (!tty || tty->magic != TTY_MAGIC)
4799                return;
4800
4801        un = tty->driver_data;
4802        if (!un || un->magic != DGAP_UNIT_MAGIC)
4803                return;
4804
4805        ch = un->un_ch;
4806        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4807                return;
4808
4809        bd = ch->ch_bd;
4810        if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4811                return;
4812
4813        spin_lock_irqsave(&bd->bd_lock, lock_flags);
4814        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4815
4816        /* TODO: Do something here */
4817
4818        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4819        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4820}
4821
4822/*****************************************************************************
4823 *
4824 * The IOCTL function and all of its helpers
4825 *
4826 *****************************************************************************/
4827
4828/*
4829 * dgap_tty_ioctl()
4830 *
4831 * The usual assortment of ioctl's
4832 */
4833static int dgap_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
4834                unsigned long arg)
4835{
4836        struct board_t *bd;
4837        struct channel_t *ch;
4838        struct un_t *un;
4839        int rc;
4840        u16 head;
4841        ulong lock_flags = 0;
4842        ulong lock_flags2 = 0;
4843        void __user *uarg = (void __user *) arg;
4844
4845        if (!tty || tty->magic != TTY_MAGIC)
4846                return -ENODEV;
4847
4848        un = tty->driver_data;
4849        if (!un || un->magic != DGAP_UNIT_MAGIC)
4850                return -ENODEV;
4851
4852        ch = un->un_ch;
4853        if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4854                return -ENODEV;
4855
4856        bd = ch->ch_bd;
4857        if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4858                return -ENODEV;
4859
4860        spin_lock_irqsave(&bd->bd_lock, lock_flags);
4861        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4862
4863        if (un->un_open_count <= 0) {
4864                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4865                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4866                return -EIO;
4867        }
4868
4869        switch (cmd) {
4870
4871        /* Here are all the standard ioctl's that we MUST implement */
4872
4873        case TCSBRK:
4874                /*
4875                 * TCSBRK is SVID version: non-zero arg --> no break
4876                 * this behaviour is exploited by tcdrain().
4877                 *
4878                 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
4879                 * between 0.25 and 0.5 seconds so we'll ask for something
4880                 * in the middle: 0.375 seconds.
4881                 */
4882                rc = tty_check_change(tty);
4883                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4884                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4885                if (rc)
4886                        return rc;
4887
4888                rc = dgap_wait_for_drain(tty);
4889
4890                if (rc)
4891                        return -EINTR;
4892
4893                spin_lock_irqsave(&bd->bd_lock, lock_flags);
4894                spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4895
4896                if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
4897                        dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
4898
4899                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4900                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4901
4902                return 0;
4903
4904        case TCSBRKP:
4905                /* support for POSIX tcsendbreak()
4906
4907                 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
4908                 * between 0.25 and 0.5 seconds so we'll ask for something
4909                 * in the middle: 0.375 seconds.
4910                 */
4911                rc = tty_check_change(tty);
4912                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4913                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4914                if (rc)
4915                        return rc;
4916
4917                rc = dgap_wait_for_drain(tty);
4918                if (rc)
4919                        return -EINTR;
4920
4921                spin_lock_irqsave(&bd->bd_lock, lock_flags);
4922                spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4923
4924                dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
4925
4926                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4927                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4928
4929                return 0;
4930
4931        case TIOCSBRK:
4932                /*
4933                 * FEP5 doesn't support turning on a break unconditionally.
4934                 * The FEP5 device will stop sending a break automatically
4935                 * after the specified time value that was sent when turning on
4936                 * the break.
4937                 */
4938                rc = tty_check_change(tty);
4939                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4940                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4941                if (rc)
4942                        return rc;
4943
4944                rc = dgap_wait_for_drain(tty);
4945                if (rc)
4946                        return -EINTR;
4947
4948                spin_lock_irqsave(&bd->bd_lock, lock_flags);
4949                spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4950
4951                dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
4952
4953                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4954                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4955
4956                return 0;
4957
4958        case TIOCCBRK:
4959                /*
4960                 * FEP5 doesn't support turning off a break unconditionally.
4961                 * The FEP5 device will stop sending a break automatically
4962                 * after the specified time value that was sent when turning on
4963                 * the break.
4964                 */
4965                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4966                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4967                return 0;
4968
4969        case TIOCGSOFTCAR:
4970
4971                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4972                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4973
4974                rc = put_user(C_CLOCAL(tty) ? 1 : 0,
4975                                (unsigned long __user *) arg);
4976                return rc;
4977
4978        case TIOCSSOFTCAR:
4979                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4980                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4981
4982                rc = get_user(arg, (unsigned long __user *) arg);
4983                if (rc)
4984                        return rc;
4985
4986                spin_lock_irqsave(&bd->bd_lock, lock_flags);
4987                spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4988                tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) |
4989                                                (arg ? CLOCAL : 0));
4990                dgap_param(ch, bd, un->un_type);
4991                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4992                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4993
4994                return 0;
4995
4996        case TIOCMGET:
4997                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4998                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4999                return dgap_get_modem_info(ch, uarg);
5000
5001        case TIOCMBIS:
5002        case TIOCMBIC:
5003        case TIOCMSET:
5004                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5005                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5006                return dgap_set_modem_info(ch, bd, un, cmd, uarg);
5007
5008                /*
5009                 * Here are any additional ioctl's that we want to implement
5010                 */
5011
5012        case TCFLSH:
5013                /*
5014                 * The linux tty driver doesn't have a flush
5015                 * input routine for the driver, assuming all backed
5016                 * up data is in the line disc. buffers.  However,
5017                 * we all know that's not the case.  Here, we
5018                 * act on the ioctl, but then lie and say we didn't
5019                 * so the line discipline will process the flush
5020                 * also.
5021                 */
5022                rc = tty_check_change(tty);
5023                if (rc) {
5024                        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5025                        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5026                        return rc;
5027                }
5028
5029                if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
5030                        if (!(un->un_type == DGAP_PRINT)) {
5031                                head = readw(&(ch->ch_bs->rx_head));
5032                                writew(head, &(ch->ch_bs->rx_tail));
5033                                writeb(0, &(ch->ch_bs->orun));
5034                        }
5035                }
5036
5037                if ((arg != TCOFLUSH) && (arg != TCIOFLUSH)) {
5038                        /* pretend we didn't recognize this IOCTL */
5039                        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5040                        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5041
5042                        return -ENOIOCTLCMD;
5043                }
5044
5045                ch->ch_flags &= ~CH_STOP;
5046                head = readw(&(ch->ch_bs->tx_head));
5047                dgap_cmdw(ch, FLUSHTX, (u16) head, 0);
5048                dgap_cmdw(ch, RESUMETX, 0, 0);
5049                if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
5050                        ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
5051                        wake_up_interruptible(&ch->ch_tun.un_flags_wait);
5052                }
5053                if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
5054                        ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
5055                        wake_up_interruptible(&ch->ch_pun.un_flags_wait);
5056                }
5057                if (waitqueue_active(&tty->write_wait))
5058                        wake_up_interruptible(&tty->write_wait);
5059
5060                /* Can't hold any locks when calling tty_wakeup! */
5061                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5062                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5063                tty_wakeup(tty);
5064
5065                /* pretend we didn't recognize this IOCTL */
5066                return -ENOIOCTLCMD;
5067
5068        case TCSETSF:
5069        case TCSETSW:
5070                /*
5071                 * The linux tty driver doesn't have a flush
5072                 * input routine for the driver, assuming all backed
5073                 * up data is in the line disc. buffers.  However,
5074                 * we all know that's not the case.  Here, we
5075                 * act on the ioctl, but then lie and say we didn't
5076                 * so the line discipline will process the flush
5077                 * also.
5078                 */
5079                if (cmd == TCSETSF) {
5080                        /* flush rx */
5081                        ch->ch_flags &= ~CH_STOP;
5082                        head = readw(&(ch->ch_bs->rx_head));
5083                        writew(head, &(ch->ch_bs->rx_tail));
5084                }
5085
5086                /* now wait for all the output to drain */
5087                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5088                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5089                rc = dgap_wait_for_drain(tty);
5090                if (rc)
5091                        return -EINTR;
5092
5093                /* pretend we didn't recognize this */
5094                return -ENOIOCTLCMD;
5095
5096        case TCSETAW:
5097
5098                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5099                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5100                rc = dgap_wait_for_drain(tty);
5101                if (rc)
5102                        return -EINTR;
5103
5104                /* pretend we didn't recognize this */
5105                return -ENOIOCTLCMD;
5106
5107        case TCXONC:
5108                /*
5109                 * The Linux Line Discipline (LD) would do this for us if we
5110                 * let it, but we have the special firmware options to do this
5111                 * the "right way" regardless of hardware or software flow
5112                 * control so we'll do it outselves instead of letting the LD
5113                 * do it.
5114                 */
5115                rc = tty_check_change(tty);
5116                if (rc) {
5117                        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5118                        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5119                        return rc;
5120                }
5121
5122                switch (arg) {
5123
5124                case TCOON:
5125                        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5126                        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5127                        dgap_tty_start(tty);
5128                        return 0;
5129                case TCOOFF:
5130                        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5131                        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5132                        dgap_tty_stop(tty);
5133                        return 0;
5134                case TCION:
5135                        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5136                        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5137                        /* Make the ld do it */
5138                        return -ENOIOCTLCMD;
5139                case TCIOFF:
5140                        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5141                        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5142                        /* Make the ld do it */
5143                        return -ENOIOCTLCMD;
5144                default:
5145                        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5146                        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5147                        return -EINVAL;
5148                }
5149
5150        case DIGI_GETA:
5151                /* get information for ditty */
5152                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5153                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5154                return dgap_tty_digigeta(ch, uarg);
5155
5156        case DIGI_SETAW:
5157        case DIGI_SETAF:
5158
5159                /* set information for ditty */
5160                if (cmd == (DIGI_SETAW)) {
5161
5162                        spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5163                        spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5164                        rc = dgap_wait_for_drain(tty);
5165                        if (rc)
5166                                return -EINTR;
5167                        spin_lock_irqsave(&bd->bd_lock, lock_flags);
5168                        spin_lock_irqsave(&ch->ch_lock, lock_flags2);
5169                } else
5170                        tty_ldisc_flush(tty);
5171                /* fall thru */
5172
5173        case DIGI_SETA:
5174                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5175                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5176                return dgap_tty_digiseta(ch, bd, un, uarg);
5177
5178        case DIGI_GEDELAY:
5179                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5180                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5181                return dgap_tty_digigetedelay(tty, uarg);
5182
5183        case DIGI_SEDELAY:
5184                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5185                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5186                return dgap_tty_digisetedelay(ch, bd, un, uarg);
5187
5188        case DIGI_GETCUSTOMBAUD:
5189                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5190                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5191                return dgap_tty_digigetcustombaud(ch, un, uarg);
5192
5193        case DIGI_SETCUSTOMBAUD:
5194                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5195                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5196                return dgap_tty_digisetcustombaud(ch, bd, un, uarg);
5197
5198        case DIGI_RESET_PORT:
5199                dgap_firmware_reset_port(ch);
5200                dgap_param(ch, bd, un->un_type);
5201                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5202                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5203                return 0;
5204
5205        default:
5206                spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5207                spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5208
5209                return -ENOIOCTLCMD;
5210        }
5211}
5212
5213static const struct tty_operations dgap_tty_ops = {
5214        .open = dgap_tty_open,
5215        .close = dgap_tty_close,
5216        .write = dgap_tty_write,
5217        .write_room = dgap_tty_write_room,
5218        .flush_buffer = dgap_tty_flush_buffer,
5219        .chars_in_buffer = dgap_tty_chars_in_buffer,
5220        .flush_chars = dgap_tty_flush_chars,
5221        .ioctl = dgap_tty_ioctl,
5222        .set_termios = dgap_tty_set_termios,
5223        .stop = dgap_tty_stop,
5224        .start = dgap_tty_start,
5225        .throttle = dgap_tty_throttle,
5226        .unthrottle = dgap_tty_unthrottle,
5227        .hangup = dgap_tty_hangup,
5228        .put_char = dgap_tty_put_char,
5229        .tiocmget = dgap_tty_tiocmget,
5230        .tiocmset = dgap_tty_tiocmset,
5231        .break_ctl = dgap_tty_send_break,
5232        .wait_until_sent = dgap_tty_wait_until_sent,
5233        .send_xchar = dgap_tty_send_xchar
5234};
5235
5236/************************************************************************
5237 *
5238 * TTY Initialization/Cleanup Functions
5239 *
5240 ************************************************************************/
5241
5242/*
5243 * dgap_tty_register()
5244 *
5245 * Init the tty subsystem for this board.
5246 */
5247static int dgap_tty_register(struct board_t *brd)
5248{
5249        int rc;
5250
5251        brd->serial_driver = tty_alloc_driver(MAXPORTS,
5252                                              TTY_DRIVER_REAL_RAW |
5253                                              TTY_DRIVER_DYNAMIC_DEV |
5254                                              TTY_DRIVER_HARDWARE_BREAK);
5255        if (IS_ERR(brd->serial_driver))
5256                return PTR_ERR(brd->serial_driver);
5257
5258        snprintf(brd->serial_name, MAXTTYNAMELEN, "tty_dgap_%d_",
5259                 brd->boardnum);
5260        brd->serial_driver->name = brd->serial_name;
5261        brd->serial_driver->name_base = 0;
5262        brd->serial_driver->major = 0;
5263        brd->serial_driver->minor_start = 0;
5264        brd->serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
5265        brd->serial_driver->subtype = SERIAL_TYPE_NORMAL;
5266        brd->serial_driver->init_termios = dgap_default_termios;
5267        brd->serial_driver->driver_name = DRVSTR;
5268
5269        /*
5270         * Entry points for driver.  Called by the kernel from
5271         * tty_io.c and n_tty.c.
5272         */
5273        tty_set_operations(brd->serial_driver, &dgap_tty_ops);
5274
5275        /*
5276         * If we're doing transparent print, we have to do all of the above
5277         * again, separately so we don't get the LD confused about what major
5278         * we are when we get into the dgap_tty_open() routine.
5279         */
5280        brd->print_driver = tty_alloc_driver(MAXPORTS,
5281                                             TTY_DRIVER_REAL_RAW |
5282                                             TTY_DRIVER_DYNAMIC_DEV |
5283                                             TTY_DRIVER_HARDWARE_BREAK);
5284        if (IS_ERR(brd->print_driver)) {
5285                rc = PTR_ERR(brd->print_driver);
5286                goto free_serial_drv;
5287        }
5288
5289        snprintf(brd->print_name, MAXTTYNAMELEN, "pr_dgap_%d_",
5290                 brd->boardnum);
5291        brd->print_driver->name = brd->print_name;
5292        brd->print_driver->name_base = 0;
5293        brd->print_driver->major = 0;
5294        brd->print_driver->minor_start = 0;
5295        brd->print_driver->type = TTY_DRIVER_TYPE_SERIAL;
5296        brd->print_driver->subtype = SERIAL_TYPE_NORMAL;
5297        brd->print_driver->init_termios = dgap_default_termios;
5298        brd->print_driver->driver_name = DRVSTR;
5299
5300        /*
5301         * Entry points for driver.  Called by the kernel from
5302         * tty_io.c and n_tty.c.
5303         */
5304        tty_set_operations(brd->print_driver, &dgap_tty_ops);
5305
5306        /* Register tty devices */
5307        rc = tty_register_driver(brd->serial_driver);
5308        if (rc < 0)
5309                goto free_print_drv;
5310
5311        /* Register Transparent Print devices */
5312        rc = tty_register_driver(brd->