root/drivers/char/stallion.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. init_module
  2. cleanup_module
  3. stl_memalloc
  4. stl_open
  5. stl_waitcarrier
  6. stl_close
  7. stl_delay
  8. stl_write
  9. stl_putchar
  10. stl_flushchars
  11. stl_writeroom
  12. stl_charsinbuffer
  13. stl_getserial
  14. stl_setserial
  15. stl_ioctl
  16. stl_settermios
  17. stl_throttle
  18. stl_unthrottle
  19. stl_stop
  20. stl_start
  21. stl_hangup
  22. stl_flushbuffer
  23. stl_getreg
  24. stl_setreg
  25. stl_updatereg
  26. stl_txisr
  27. stl_rxisr
  28. stl_mdmisr
  29. stl_intr
  30. stl_offintr
  31. stl_ccrwait
  32. stl_setport
  33. stl_setsignals
  34. stl_getsignals
  35. stl_enablerxtx
  36. stl_startrxtx
  37. stl_disableintrs
  38. stl_sendbreak
  39. stl_mapirq
  40. stl_initports
  41. stl_initeio
  42. stl_initech
  43. stl_brdinit
  44. stl_findpcibrds
  45. stl_initbrds
  46. stl_init

   1 /*****************************************************************************/
   2 
   3 /*
   4  *      stallion.c  -- stallion multiport serial driver.
   5  *
   6  *      Copyright (C) 1994,1995  Greg Ungerer (gerg@stallion.oz.au).
   7  *
   8  *      This code is loosely based on the Linux serial driver, written by
   9  *      Linus Torvalds, Theodore T'so and others.
  10  *
  11  *      This program is free software; you can redistribute it and/or modify
  12  *      it under the terms of the GNU General Public License as published by
  13  *      the Free Software Foundation; either version 2 of the License, or
  14  *      (at your option) any later version.
  15  *
  16  *      This program is distributed in the hope that it will be useful,
  17  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  18  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19  *      GNU General Public License for more details.
  20  *
  21  *      You should have received a copy of the GNU General Public License
  22  *      along with this program; if not, write to the Free Software
  23  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24  */
  25 
  26 /*****************************************************************************/
  27 
  28 #include <linux/module.h>
  29 
  30 #include <linux/errno.h>
  31 #include <linux/sched.h>
  32 #include <linux/wait.h>
  33 #include <linux/interrupt.h>
  34 #include <linux/termios.h>
  35 #include <linux/fcntl.h>
  36 #include <linux/tty_driver.h>
  37 #include <linux/tty.h>
  38 #include <linux/tty_flip.h>
  39 #include <linux/serial.h>
  40 #include <linux/cd1400.h>
  41 #include <linux/string.h>
  42 #include <linux/malloc.h>
  43 #include <linux/ioport.h>
  44 #include <linux/config.h>       /* for CONFIG_PCI */
  45 
  46 #include <asm/system.h>
  47 #include <asm/io.h>
  48 #include <asm/segment.h>
  49 
  50 #ifdef CONFIG_PCI
  51 #include <linux/pci.h>
  52 #include <linux/bios32.h>
  53 #endif
  54 
  55 /*****************************************************************************/
  56 
  57 /*
  58  *      Define different board types. At the moment I have only declared
  59  *      those boards that this driver supports. But I will use the standard
  60  *      "assigned" board numbers. In the future this driver will support
  61  *      some of the other Stallion boards. Currently supported boards are
  62  *      abbreviated as EIO = EasyIO and ECH = EasyConnection 8/32.
  63  */
  64 #define BRD_EASYIO      20
  65 #define BRD_ECH         21
  66 #define BRD_ECHMC       22
  67 #define BRD_ECHPCI      26
  68 
  69 /*
  70  *      Define a configuration structure to hold the board configuration.
  71  *      Need to set this up in the code (for now) with the boards that are
  72  *      to be configured into the system. This is what needs to be modified
  73  *      when adding/removing/modifying boards. Each line entry in the
  74  *      stl_brdconf[] array is a board. Each line contains io/irq/memory
  75  *      ranges for that board (as well as what type of board it is).
  76  *      Some examples:
  77  *              { BRD_EASYIO, 0x2a0, 0, 0, 10, 0 }
  78  *      This line would configure an EasyIO board (4 or 8, no difference),
  79  *      at io addres 2a0 and irq 10.
  80  *      Another example:
  81  *              { BRD_ECH, 0x2a8, 0x280, 0, 12, 0 },
  82  *      This line will configure an EasyConnection 8/32 board at primary io
  83  *      addres 2a8, secondary io address 280 and irq 12.
  84  *      Enter as many lines into this array as you want (only the first 4
  85  *      will actually be used!). Any combination of EasyIO and EasyConnection
  86  *      boards can be specified. EasyConnection 8/32 boards can share their
  87  *      secondary io addresses between each other.
  88  *
  89  *      NOTE: there is no need to put any entries in this table for PCI
  90  *      boards. They will be found automatically by the driver - provided
  91  *      PCI BIOS32 support is compiled into the kernel.
  92  */
  93 
  94 typedef struct {
  95         int             brdtype;
  96         int             ioaddr1;
  97         int             ioaddr2;
  98         unsigned long   memaddr;
  99         int             irq;
 100         int             irqtype;
 101 } stlconf_t;
 102 
 103 static stlconf_t        stl_brdconf[] = {
 104         { BRD_EASYIO, 0x2a0, 0, 0, 10, 0 },
 105 };
 106 
 107 static int      stl_nrbrds = sizeof(stl_brdconf) / sizeof(stlconf_t);
 108 
 109 /*****************************************************************************/
 110 
 111 /*
 112  *      Define some important driver characteristics. Device major numbers
 113  *      allocated as per Linux Device Registery.
 114  */
 115 #ifndef STL_SERIALMAJOR
 116 #define STL_SERIALMAJOR         24
 117 #endif
 118 #ifndef STL_CALLOUTMAJOR
 119 #define STL_CALLOUTMAJOR        25
 120 #endif
 121 
 122 #define STL_DRVTYPSERIAL        1
 123 #define STL_DRVTYPCALLOUT       2
 124 
 125 #define STL_MAXBRDS             4
 126 #define STL_MAXPANELS           4
 127 #define STL_PORTSPERPANEL       16
 128 #define STL_MAXPORTS            64
 129 #define STL_MAXDEVS             (STL_MAXBRDS * STL_MAXPORTS)
 130 
 131 /*
 132  *      I haven't really decided (or measured) what TX buffer size gives
 133  *      a good balance between performance and memory usage. These seem
 134  *      to work pretty well...
 135  */
 136 #define STL_TXBUFLOW            256
 137 #define STL_TXBUFSIZE           2048
 138 
 139 /*****************************************************************************/
 140 
 141 /*
 142  *      Define our local driver identity first. Set up stuff to deal with
 143  *      all the local structures required by a serial tty driver.
 144  */
 145 static char     *stl_drvname = "Stallion Multiport Serial Driver";
 146 static char     *stl_drvversion = "1.0.0";
 147 static char     *stl_serialname = "ttyE";
 148 static char     *stl_calloutname = "cue";
 149 
 150 static struct tty_driver        stl_serial;
 151 static struct tty_driver        stl_callout;
 152 static struct tty_struct        *stl_ttys[STL_MAXDEVS];
 153 static struct termios           *stl_termios[STL_MAXDEVS];
 154 static struct termios           *stl_termioslocked[STL_MAXDEVS];
 155 static int                      stl_refcount = 0;
 156 
 157 /*
 158  *      We will need to allocate a temporary write buffer for chars that
 159  *      come direct from user space. The problem is that a copy from user
 160  *      space might cause a page fault (typically on a system that is
 161  *      swapping!). All ports will share one buffer - since if the system
 162  *      is already swapping a shared buffer won't make things any worse.
 163  */
 164 static char                     *stl_tmpwritebuf;
 165 static struct semaphore         stl_tmpwritesem = MUTEX;
 166 
 167 /*
 168  *      Define a local default termios struct. All ports will be created
 169  *      with this termios initially. Basically all it defines is a raw port
 170  *      at 9600, 8 data bits, 1 stop bit.
 171  */
 172 static struct termios           stl_deftermios = {
 173         0,
 174         0,
 175         (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
 176         0,
 177         0,
 178         INIT_C_CC
 179 };
 180 
 181 /*
 182  *      Keep track of what interrupts we have requested for us.
 183  *      We don't need to request an interrupt twice if it is being
 184  *      shared with another Stallion board.
 185  */
 186 static int      stl_gotintrs[STL_MAXBRDS];
 187 static int      stl_numintrs = 0;
 188 
 189 /*****************************************************************************/
 190 
 191 /*
 192  *      Define a set of structures to hold all the board/panel/port info
 193  *      for our ports. These will be dynamically allocated as required.
 194  */
 195 
 196 /*
 197  *      Define a ring queue structure for each port. This will hold the
 198  *      TX data waiting to be output. Characters are fed into this buffer
 199  *      from the line discipline (or even direct from user space!) and
 200  *      then fed into the UARTs during interrupts. Will use a clasic ring
 201  *      queue here for this. The good thing about this type of ring queue
 202  *      is that the head and tail pointers can be updated without interrupt
 203  *      protection - since "write" code only needs to change the head, and
 204  *      interrupt code only needs to change the tail.
 205  */
 206 typedef struct {
 207         char    *buf;
 208         char    *head;
 209         char    *tail;
 210 } stlrq_t;
 211 
 212 /*
 213  *      Port, panel and board structures to hold status info about each.
 214  *      The board structure contains pointers to structures for each panel
 215  *      connected to it, and in turn each panel structure contains pointers
 216  *      for each port structure for each port on that panel. Note that
 217  *      the port structure also contains the board and panel number that it
 218  *      is associated with, this makes it (fairly) easy to get back to the
 219  *      board/panel info for a port.
 220  */
 221 typedef struct {
 222         int                     portnr;
 223         int                     panelnr;
 224         int                     brdnr;
 225         int                     ioaddr;
 226         int                     uartaddr;
 227         int                     pagenr;
 228         int                     istate;
 229         int                     flags;
 230         int                     baud_base;
 231         int                     custom_divisor;
 232         int                     close_delay;
 233         int                     closing_wait;
 234         int                     refcount;
 235         int                     openwaitcnt;
 236         int                     brklen;
 237         long                    session;
 238         long                    pgrp;
 239         unsigned int            sigs;
 240         unsigned int            rxignoremsk;
 241         unsigned int            rxmarkmsk;
 242         struct tty_struct       *tty;
 243         struct wait_queue       *open_wait;
 244         struct wait_queue       *close_wait;
 245         struct termios          normaltermios;
 246         struct termios          callouttermios;
 247         struct tq_struct        tqueue;
 248         stlrq_t                 tx;
 249 } stlport_t;
 250 
 251 typedef struct {
 252         int             panelnr;
 253         int             brdnr;
 254         int             pagenr;
 255         int             nrports;
 256         int             iobase;
 257         unsigned int    ackmask;
 258         stlport_t       *ports[STL_PORTSPERPANEL];
 259 } stlpanel_t;
 260 
 261 typedef struct {
 262         int             brdnr;
 263         int             brdtype;
 264         int             state;
 265         int             nrpanels;
 266         int             nrports;
 267         int             irq;
 268         int             irqtype;
 269         unsigned int    ioaddr1;
 270         unsigned int    ioaddr2;
 271         unsigned int    iostatus;
 272         unsigned int    ioctrl;
 273         unsigned int    ioctrlval;
 274         stlpanel_t      *panels[STL_MAXPANELS];
 275 } stlbrd_t;
 276 
 277 static stlbrd_t         *stl_brds[STL_MAXBRDS];
 278 
 279 /*
 280  *      Per board state flags. Used with the state field of the board struct.
 281  *      Not really much here yet!
 282  */
 283 #define BRD_FOUND       0x1
 284 
 285 /*
 286  *      Define the port structure istate flags. These set of flags are
 287  *      modified at interrupt time - so setting and reseting them needs
 288  *      to be atomic. Use the bit clear/setting routines for this.
 289  */
 290 #define ASYI_TXBUSY     1
 291 #define ASYI_TXLOW      2
 292 #define ASYI_DCDCHANGE  3
 293 
 294 /*
 295  *      Define an array of board names as printable strings. Handy for
 296  *      referencing boards when printing trace and stuff.
 297  */
 298 static char     *stl_brdnames[] = {
 299         (char *) NULL,
 300         (char *) NULL,
 301         (char *) NULL,
 302         (char *) NULL,
 303         (char *) NULL,
 304         (char *) NULL,
 305         (char *) NULL,
 306         (char *) NULL,
 307         (char *) NULL,
 308         (char *) NULL,
 309         (char *) NULL,
 310         (char *) NULL,
 311         (char *) NULL,
 312         (char *) NULL,
 313         (char *) NULL,
 314         (char *) NULL,
 315         (char *) NULL,
 316         (char *) NULL,
 317         (char *) NULL,
 318         (char *) NULL,
 319         "EasyIO",
 320         "EC8/32-AT",
 321         "EC8/32-MC",
 322         (char *) NULL,
 323         (char *) NULL,
 324         (char *) NULL,
 325         "EC8/32-PCI",
 326 };
 327 
 328 /*****************************************************************************/
 329 
 330 /*
 331  *      Hardware ID bits for the EasyIO and ECH boards. These defines apply
 332  *      to the directly accessable io ports of these boards (not the cd1400
 333  *      uarts - they are in cd1400.h).
 334  */
 335 #define EIO_8PORTRS     0x04
 336 #define EIO_4PORTRS     0x05
 337 #define EIO_8PORTDI     0x00
 338 #define EIO_8PORTM      0x06
 339 #define EIO_IDBITMASK   0x07
 340 #define EIO_INTRPEND    0x08
 341 #define EIO_INTEDGE     0x00
 342 #define EIO_INTLEVEL    0x08
 343 
 344 #define ECH_ID          0xa0
 345 #define ECH_IDBITMASK   0xe0
 346 #define ECH_BRDENABLE   0x08
 347 #define ECH_BRDDISABLE  0x00
 348 #define ECH_INTENABLE   0x01
 349 #define ECH_INTDISABLE  0x00
 350 #define ECH_INTLEVEL    0x02
 351 #define ECH_INTEDGE     0x00
 352 #define ECH_INTRPEND    0x01
 353 #define ECH_BRDRESET    0x01
 354 
 355 #define ECHMC_INTENABLE 0x01
 356 #define ECHMC_BRDRESET  0x02
 357 
 358 #define ECH_PNLSTATUS   2
 359 #define ECH_PNL16PORT   0x20
 360 #define ECH_PNLIDMASK   0x07
 361 #define ECH_PNLINTRPEND 0x80
 362 #define ECH_ADDR2MASK   0x1e0
 363 
 364 /*
 365  *      Define the offsets within the register bank for all io registers.
 366  *      These io address offsets are common to both the EIO and ECH.
 367  */
 368 #define EREG_ADDR       0
 369 #define EREG_DATA       4
 370 #define EREG_RXACK      5
 371 #define EREG_TXACK      6
 372 #define EREG_MDACK      7
 373 
 374 #define EREG_BANKSIZE   8
 375 
 376 /*
 377  *      Define the vector mapping bits for the programmable interrupt board
 378  *      hardware. These bits encode the interrupt for the board to use - it
 379  *      is software selectable (except the EIO-8M).
 380  */
 381 static unsigned char    stl_vecmap[] = {
 382         0xff, 0xff, 0xff, 0x04, 0x06, 0x05, 0xff, 0x07,
 383         0xff, 0xff, 0x00, 0x02, 0x01, 0xff, 0xff, 0x03
 384 };
 385 
 386 /*
 387  *      Set up enable and disable macros for the ECH boards. They require
 388  *      the secondary io address space to be activated and deactivated.
 389  *      This way all ECH boards can share their secondary io region.
 390  *      If this is an ECH-PCI board then also need to set the page pointer
 391  *      to point to the correct page.
 392  */
 393 #define BRDENABLE(brdnr,pagenr)                                         \
 394         if (stl_brds[(brdnr)]->brdtype == BRD_ECH)                      \
 395                 outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDENABLE),    \
 396                         stl_brds[(brdnr)]->ioctrl);                     \
 397         else if (stl_brds[(brdnr)]->brdtype == BRD_ECHPCI)              \
 398                 outb((pagenr), stl_brds[(brdnr)]->ioctrl);
 399 
 400 #define BRDDISABLE(brdnr)                                               \
 401         if (stl_brds[(brdnr)]->brdtype == BRD_ECH)                      \
 402                 outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDDISABLE),   \
 403                         stl_brds[(brdnr)]->ioctrl);
 404 
 405 /*
 406  *      Define the cd1400 baud rate clocks. These are used when calculating
 407  *      what clock and divisor to use for the required baud rate. Also
 408  *      define the maximum baud rate allowed, and the default base baud.
 409  */
 410 static int      stl_cd1400clkdivs[] = {
 411         CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4
 412 };
 413 
 414 #define STL_MAXBAUD     230400
 415 #define STL_BAUDBASE    115200
 416 #define STL_CLOSEDELAY  50
 417 
 418 /*****************************************************************************/
 419 
 420 /*
 421  *      Define macros to extract a brd/port number from a minor number.
 422  */
 423 #define MKDEV2BRD(min)          (((min) & 0xc0) >> 6)
 424 #define MKDEV2PORT(min)         ((min) & 0x3f)
 425 
 426 /*
 427  *      Define a baud rate table that converts termios baud rate selector
 428  *      into the actual baud rate value. All baud rate calculates are based
 429  *      on the actual baud rate required.
 430  */
 431 static unsigned int     stl_baudrates[] = {
 432         0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
 433         9600, 19200, 38400, 57600, 115200, 230400
 434 };
 435 
 436 /*****************************************************************************/
 437 
 438 /*
 439  *      Define some handy local macros...
 440  */
 441 #ifndef MIN
 442 #define MIN(a,b)                (((a) <= (b)) ? (a) : (b))
 443 #endif
 444 
 445 /*****************************************************************************/
 446 
 447 /*
 448  *      Declare all those functions in this driver!
 449  */
 450 
 451 #ifdef MODULE
 452 int             init_module(void);
 453 void            cleanup_module(void);
 454 #endif
 455 static void     *stl_memalloc(int len);
 456 
 457 int             stl_init(void);
 458 static int      stl_open(struct tty_struct *tty, struct file *filp);
 459 static void     stl_close(struct tty_struct *tty, struct file *filp);
 460 static int      stl_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
 461 static void     stl_putchar(struct tty_struct *tty, unsigned char ch);
 462 static void     stl_flushchars(struct tty_struct *tty);
 463 static int      stl_writeroom(struct tty_struct *tty);
 464 static int      stl_charsinbuffer(struct tty_struct *tty);
 465 static int      stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
 466 static void     stl_settermios(struct tty_struct *tty, struct termios *old);
 467 static void     stl_throttle(struct tty_struct *tty);
 468 static void     stl_unthrottle(struct tty_struct *tty);
 469 static void     stl_stop(struct tty_struct *tty);
 470 static void     stl_start(struct tty_struct *tty);
 471 static void     stl_flushbuffer(struct tty_struct *tty);
 472 static void     stl_hangup(struct tty_struct *tty);
 473 
 474 static int      stl_initbrds(void);
 475 static int      stl_brdinit(stlbrd_t *brdp);
 476 static int      stl_initeio(stlbrd_t *brdp);
 477 static int      stl_initech(stlbrd_t *brdp);
 478 static int      stl_initports(stlbrd_t *brdp, stlpanel_t *panelp);
 479 static int      stl_mapirq(int irq);
 480 static void     stl_getserial(stlport_t *portp, struct serial_struct *sp);
 481 static int      stl_setserial(stlport_t *portp, struct serial_struct *sp);
 482 static void     stl_setreg(stlport_t *portp, int regnr, int value);
 483 static int      stl_getreg(stlport_t *portp, int regnr);
 484 static int      stl_updatereg(stlport_t *portp, int regnr, int value);
 485 static void     stl_setport(stlport_t *portp, struct termios *tiosp);
 486 static void     stl_getsignals(stlport_t *portp);
 487 static void     stl_setsignals(stlport_t *portp, int dtr, int rts);
 488 static void     stl_ccrwait(stlport_t *portp);
 489 static void     stl_enablerxtx(stlport_t *portp, int rx, int tx);
 490 static void     stl_startrxtx(stlport_t *portp, int rx, int tx);
 491 static void     stl_disableintrs(stlport_t *portp);
 492 static void     stl_sendbreak(stlport_t *portp, long len);
 493 static int      stl_waitcarrier(stlport_t *portp, struct file *filp);
 494 static void     stl_delay(int len);
 495 static void     stl_intr(int irq, struct pt_regs *regs);
 496 static void     stl_offintr(void *private);
 497 
 498 #ifdef  CONFIG_PCI
 499 static int      stl_findpcibrds(void);
 500 #endif
 501 
 502 /*****************************************************************************/
 503 
 504 #ifdef MODULE
 505 
 506 /*
 507  *      Use the kernel version number for modules.
 508  */
 509 int init_module()
     /* [previous][next][first][last][top][bottom][index][help] */
 510 {
 511         unsigned long   flags;
 512 
 513 #if DEBUG
 514         printk("init_module()\n");
 515 #endif
 516 
 517         save_flags(flags);
 518         cli();
 519         stl_init();
 520         restore_flags(flags);
 521 
 522         return(0);
 523 }
 524 
 525 /*****************************************************************************/
 526 
 527 void cleanup_module()
     /* [previous][next][first][last][top][bottom][index][help] */
 528 {
 529         stlbrd_t        *brdp;
 530         stlpanel_t      *panelp;
 531         stlport_t       *portp;
 532         unsigned long   flags;
 533         int             i, j, k;
 534 
 535 #if DEBUG
 536         printk("cleanup_module()\n");
 537 #endif
 538 
 539         printk("Unloading %s: version %s\n", stl_drvname, stl_drvversion);
 540 
 541         save_flags(flags);
 542         cli();
 543 
 544 /*
 545  *      Free up all allocated resources used by the ports. This includes
 546  *      memory and interrupts. As part of this process we will also do
 547  *      a hangup on every open port - to try and flush out any processes
 548  *      hanging onto ports.
 549  */
 550         i = tty_unregister_driver(&stl_serial);
 551         j = tty_unregister_driver(&stl_callout);
 552         if (i || j) {
 553                 printk("STALLION: failed to un-register tty driver, errno=%d,%d\n", -i, -j);
 554                 restore_flags(flags);
 555                 return;
 556         }
 557 
 558         if (stl_tmpwritebuf != (char *) NULL)
 559                 kfree_s(stl_tmpwritebuf, STL_TXBUFSIZE);
 560 
 561         for (i = 0; (i < stl_nrbrds); i++) {
 562                 brdp = stl_brds[i];
 563                 for (j = 0; (j < STL_MAXPANELS); j++) {
 564                         panelp = brdp->panels[j];
 565                         if (panelp != (stlpanel_t *) NULL) {
 566                                 for (k = 0; (k < STL_PORTSPERPANEL); k++) {
 567                                         portp = panelp->ports[k];
 568                                         if (portp != (stlport_t *) NULL) {
 569                                                 if (portp->tty != (struct tty_struct *) NULL)
 570                                                         stl_hangup(portp->tty);
 571                                                 if (portp->tx.buf != (char *) NULL)
 572                                                         kfree_s(portp->tx.buf, STL_TXBUFSIZE);
 573                                                 kfree_s(portp, sizeof(stlport_t));
 574                                         }
 575                                 }
 576                                 kfree_s(panelp, sizeof(stlpanel_t));
 577                         }
 578                         
 579                 }
 580 
 581                 if (brdp->brdtype == BRD_ECH) {
 582                         release_region(brdp->ioaddr1, 2);
 583                         release_region(brdp->ioaddr2, 32);
 584                 } else if (brdp->brdtype == BRD_ECHPCI) {
 585                         release_region(brdp->ioaddr1, 4);
 586                         release_region(brdp->ioaddr2, 8);
 587                 } else if (brdp->brdtype == BRD_ECHMC) {
 588                         release_region(brdp->ioaddr1, 64);
 589                 } else if (brdp->brdtype == BRD_EASYIO) {
 590                         release_region(brdp->ioaddr1, 8);
 591                 }
 592 
 593                 kfree_s(brdp, sizeof(stlbrd_t));
 594                 stl_brds[i] = (stlbrd_t *) NULL;
 595         }
 596 
 597         for (i = 0; (i < stl_numintrs); i++)
 598                 free_irq(stl_gotintrs[i]);
 599 
 600         restore_flags(flags);
 601 }
 602 
 603 #endif
 604 
 605 /*****************************************************************************/
 606 
 607 /*
 608  *      Local memory allocation routines. These are used so we can deal with
 609  *      memory allocation at init time and during run-time in a consistent
 610  *      way. Everbody just calls the stl_memalloc routine to allocate
 611  *      memory and it will do the right thing.
 612  */
 613 
 614 static void *stl_memalloc(int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 615 {
 616         return (void *) kmalloc(len, GFP_KERNEL);
 617 }
 618 
 619 /*****************************************************************************/
 620 
 621 static int stl_open(struct tty_struct *tty, struct file *filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 622 {
 623         stlport_t       *portp;
 624         stlbrd_t        *brdp;
 625         unsigned int    minordev;
 626         int             brdnr, panelnr, portnr, rc;
 627 
 628 #if DEBUG
 629         printk("stl_open(tty=%x,filp=%x): device=%x\n", (int) tty, (int) filp, tty->device);
 630 #endif
 631 
 632         minordev = MINOR(tty->device);
 633         brdnr = MKDEV2BRD(minordev);
 634         if (brdnr >= stl_nrbrds)
 635                 return(-ENODEV);
 636         brdp = stl_brds[brdnr];
 637         if (brdp == (stlbrd_t *) NULL)
 638                 return(-ENODEV);
 639         minordev = MKDEV2PORT(minordev);
 640         for (portnr = -1, panelnr = 0; (panelnr < STL_MAXPANELS); panelnr++) {
 641                 if (brdp->panels[panelnr] == (stlpanel_t *) NULL)
 642                         break;
 643                 if (minordev < brdp->panels[panelnr]->nrports) {
 644                         portnr = minordev;
 645                         break;
 646                 }
 647                 minordev -= brdp->panels[panelnr]->nrports;
 648         }
 649         if (portnr < 0)
 650                 return(-ENODEV);
 651 
 652         portp = brdp->panels[panelnr]->ports[portnr];
 653         if (portp == (stlport_t *) NULL)
 654                 return(-ENODEV);
 655 
 656 /*
 657  *      On the first open of the device setup the port hardware, and
 658  *      initialize the per port data structure.
 659  */
 660         portp->tty = tty;
 661         tty->driver_data = portp;
 662         portp->refcount++;
 663 
 664         if ((portp->flags & ASYNC_INITIALIZED) == 0) {
 665                 if (portp->tx.buf == (char *) NULL) {
 666                         portp->tx.buf = (char *) stl_memalloc(STL_TXBUFSIZE);
 667                         if (portp->tx.buf == (char *) NULL)
 668                                 return(-ENOMEM);
 669                         portp->tx.head = portp->tx.buf;
 670                         portp->tx.tail = portp->tx.buf;
 671                 }
 672                 stl_setport(portp, tty->termios);
 673                 stl_getsignals(portp);
 674                 stl_setsignals(portp, 1, 1);
 675                 stl_enablerxtx(portp, 1, 1);
 676                 stl_startrxtx(portp, 1, 0);
 677                 clear_bit(TTY_IO_ERROR, &tty->flags);
 678                 portp->flags |= ASYNC_INITIALIZED;
 679         }
 680 
 681 /*
 682  *      Check if this port is in the middle of closing. If so then wait
 683  *      until it is closed then return error status, based on flag settings.
 684  *      The sleep here does not need interrupt protection since the wakeup
 685  *      for it is done with the same context.
 686  */
 687         if (portp->flags & ASYNC_CLOSING) {
 688                 interruptible_sleep_on(&portp->close_wait);
 689                 if (portp->flags & ASYNC_HUP_NOTIFY)
 690                         return(-EAGAIN);
 691                 return(-ERESTARTSYS);
 692         }
 693 
 694 /*
 695  *      Based on type of open being done check if it can overlap with any
 696  *      previous opens still in effect. If we are a normal serial device
 697  *      then also we might have to wait for carrier.
 698  */
 699         if (tty->driver.subtype == STL_DRVTYPCALLOUT) {
 700                 if (portp->flags & ASYNC_NORMAL_ACTIVE)
 701                         return(-EBUSY);
 702                 if (portp->flags & ASYNC_CALLOUT_ACTIVE) {
 703                         if ((portp->flags & ASYNC_SESSION_LOCKOUT) &&
 704                                         (portp->session != current->session))
 705                                 return(-EBUSY);
 706                         if ((portp->flags & ASYNC_PGRP_LOCKOUT) &&
 707                                         (portp->pgrp != current->pgrp))
 708                                 return(-EBUSY);
 709                 }
 710                 portp->flags |= ASYNC_CALLOUT_ACTIVE;
 711         } else {
 712                 if (filp->f_flags & O_NONBLOCK) {
 713                         if (portp->flags & ASYNC_CALLOUT_ACTIVE)
 714                                 return(-EBUSY);
 715                 } else {
 716                         if ((rc = stl_waitcarrier(portp, filp)) != 0)
 717                                 return(rc);
 718                 }
 719                 portp->flags |= ASYNC_NORMAL_ACTIVE;
 720         }
 721 
 722         if ((portp->refcount == 1) && (portp->flags & ASYNC_SPLIT_TERMIOS)) {
 723                 if (tty->driver.subtype == STL_DRVTYPSERIAL)
 724                         *tty->termios = portp->normaltermios;
 725                 else
 726                         *tty->termios = portp->callouttermios;
 727                 stl_setport(portp, tty->termios);
 728         }
 729 
 730         portp->session = current->session;
 731         portp->pgrp = current->pgrp;
 732         return(0);
 733 }
 734 
 735 /*****************************************************************************/
 736 
 737 /*
 738  *      Possibly need to wait for carrier (DCD signal) to come high. Say
 739  *      maybe because if we are clocal then we don't need to wait...
 740  */
 741 
 742 static int stl_waitcarrier(stlport_t *portp, struct file *filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 743 {
 744         unsigned long   flags;
 745         int             rc;
 746 
 747 #if DEBUG
 748         printk("stl_waitcarrier(portp=%x,filp=%x)\n", (int) portp, (int) filp);
 749 #endif
 750 
 751         rc = 0;
 752 
 753         save_flags(flags);
 754         cli();
 755         portp->openwaitcnt++;
 756         if (portp->refcount > 0)
 757                 portp->refcount--;
 758 
 759         for (;;) {
 760                 if ((portp->flags & ASYNC_CALLOUT_ACTIVE) == 0)
 761                         stl_setsignals(portp, 1, 1);
 762                 if (tty_hung_up_p(filp) || ((portp->flags & ASYNC_INITIALIZED) == 0)) {
 763                         if (portp->flags & ASYNC_HUP_NOTIFY)
 764                                 rc = -EBUSY;
 765                         else
 766                                 rc = -ERESTARTSYS;
 767                         break;
 768                 }
 769                 if (((portp->flags & ASYNC_CALLOUT_ACTIVE) == 0) &&
 770                                 ((portp->flags & ASYNC_CLOSING) == 0) &&
 771                                 ((portp->tty->termios->c_cflag & CLOCAL) ||
 772                                 (portp->sigs & TIOCM_CD))) {
 773                         break;
 774                 }
 775                 if (current->signal & ~current->blocked) {
 776                         rc = -ERESTARTSYS;
 777                         break;
 778                 }
 779                 interruptible_sleep_on(&portp->open_wait);
 780         }
 781 
 782         if (! tty_hung_up_p(filp))
 783                 portp->refcount++;
 784         portp->openwaitcnt--;
 785         restore_flags(flags);
 786 
 787         return(rc);
 788 }
 789 
 790 /*****************************************************************************/
 791 
 792 static void stl_close(struct tty_struct *tty, struct file *filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 793 {
 794         stlport_t       *portp;
 795         unsigned long   flags;
 796 
 797 #if DEBUG
 798         printk("stl_close(tty=%x,filp=%x)\n", (int) tty, (int) filp);
 799 #endif
 800 
 801         portp = tty->driver_data;
 802         if (portp == (stlport_t *) NULL)
 803                 return;
 804 
 805         save_flags(flags);
 806         cli();
 807         if (tty_hung_up_p(filp)) {
 808                 restore_flags(flags);
 809                 return;
 810         }
 811         if (portp->refcount-- > 1) {
 812                 restore_flags(flags);
 813                 return;
 814         }
 815 
 816         portp->refcount = 0;
 817         portp->flags |= ASYNC_CLOSING;
 818 
 819         if (portp->flags & ASYNC_NORMAL_ACTIVE)
 820                 portp->normaltermios = *tty->termios;
 821         if (portp->flags & ASYNC_CALLOUT_ACTIVE)
 822                 portp->callouttermios = *tty->termios;
 823 
 824 /*
 825  *      May want to wait for any data to drain before closing. The BUSY
 826  *      flag keeps track of whether we are still sending or not - it allows
 827  *      for the FIFO in the cd1400.
 828  */
 829         tty->closing = 1;
 830         if (test_bit(ASYI_TXBUSY, &portp->istate)) {
 831                 if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
 832                         tty_wait_until_sent(tty, portp->closing_wait);
 833         }
 834 
 835         portp->flags &= ~ASYNC_INITIALIZED;
 836         stl_disableintrs(portp);
 837         if (tty->termios->c_cflag & HUPCL)
 838                 stl_setsignals(portp, 0, 0);
 839         stl_enablerxtx(portp, 0, 0);
 840         stl_flushbuffer(tty);
 841         portp->istate = 0;
 842         if (portp->tx.buf != (char *) NULL) {
 843                 kfree_s(portp->tx.buf, STL_TXBUFSIZE);
 844                 portp->tx.buf = (char *) NULL;
 845                 portp->tx.head = (char *) NULL;
 846                 portp->tx.tail = (char *) NULL;
 847         }
 848         set_bit(TTY_IO_ERROR, &tty->flags);
 849         if (tty->ldisc.flush_buffer)
 850                 (tty->ldisc.flush_buffer)(tty);
 851 
 852         tty->closing = 0;
 853         tty->driver_data = (void *) NULL;
 854         portp->tty = (struct tty_struct *) NULL;
 855 
 856         if (portp->openwaitcnt) {
 857                 if (portp->close_delay)
 858                         stl_delay(portp->close_delay);
 859                 wake_up_interruptible(&portp->open_wait);
 860         }
 861 
 862         portp->flags &= ~(ASYNC_CALLOUT_ACTIVE | ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
 863         wake_up_interruptible(&portp->close_wait);
 864         restore_flags(flags);
 865 }
 866 
 867 /*****************************************************************************/
 868 
 869 /*
 870  *      Wait for a specified delay period, this is not a busy-loop. It will
 871  *      give up the processor while waiting. Unfortunately this has some
 872  *      rather intimate knowledge of the process management stuff.
 873  */
 874 
 875 static void stl_delay(int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 876 {
 877 #if DEBUG
 878         printk("stl_delay(len=%d)\n", len);
 879 #endif
 880         if (len > 0) {
 881                 current->state = TASK_INTERRUPTIBLE;
 882                 current->timeout = jiffies + len;
 883                 schedule();
 884         }
 885 }
 886 
 887 /*****************************************************************************/
 888 
 889 /*
 890  *      Write routine. Take data and stuff it in to the TX ring queue.
 891  *      If transmit interrupts are not running then start them.
 892  */
 893 
 894 static int stl_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 895 {
 896         stlport_t       *portp;
 897         unsigned int    len, stlen;
 898         unsigned long   flags;
 899         unsigned char   *chbuf;
 900         char            *head, *tail;
 901 
 902 #if DEBUG
 903         printk("stl_write(tty=%x,from_user=%d,buf=%x,count=%d)\n", (int) tty, from_user, (int) buf, count);
 904 #endif
 905 
 906         if ((tty == (struct tty_struct *) NULL) || (stl_tmpwritebuf == (char *) NULL))
 907                 return(0);
 908         portp = tty->driver_data;
 909         if (portp == (stlport_t *) NULL)
 910                 return(0);
 911         if (portp->tx.buf == (char *) NULL)
 912                 return(0);
 913 
 914 /*
 915  *      If copying direct from user space we must cater for page faults,
 916  *      causing us to "sleep" here for a while. To handle this copy in all
 917  *      the data we need now, into a local buffer. Then when we got it all
 918  *      copy it into the TX buffer.
 919  */
 920         chbuf = (unsigned char *) buf;
 921         if (from_user) {
 922                 head = portp->tx.head;
 923                 tail = portp->tx.tail;
 924                 len = (head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) :
 925                         (tail - head - 1);
 926                 count = MIN(len, count);
 927                 
 928                 save_flags(flags);
 929                 cli();
 930                 down(&stl_tmpwritesem);
 931                 memcpy_fromfs(stl_tmpwritebuf, chbuf, count);
 932                 up(&stl_tmpwritesem);
 933                 restore_flags(flags);
 934                 chbuf = &stl_tmpwritebuf[0];
 935         }
 936 
 937         head = portp->tx.head;
 938         tail = portp->tx.tail;
 939         if (head >= tail) {
 940                 len = STL_TXBUFSIZE - (head - tail) - 1;
 941                 stlen = STL_TXBUFSIZE - (head - portp->tx.buf);
 942         } else {
 943                 len = tail - head - 1;
 944                 stlen = len;
 945         }
 946 
 947         len = MIN(len, count);
 948         count = 0;
 949         while (len > 0) {
 950                 stlen = MIN(len, stlen);
 951                 memcpy(head, chbuf, stlen);
 952                 len -= stlen;
 953                 chbuf += stlen;
 954                 count += stlen;
 955                 head += stlen;
 956                 if (head >= (portp->tx.buf + STL_TXBUFSIZE)) {
 957                         head = portp->tx.buf;
 958                         stlen = tail - head;
 959                 }
 960         }
 961         portp->tx.head = head;
 962 
 963         clear_bit(ASYI_TXLOW, &portp->istate);
 964         stl_startrxtx(portp, -1, 1);
 965 
 966         return(count);
 967 }
 968 
 969 /*****************************************************************************/
 970 
 971 static void stl_putchar(struct tty_struct *tty, unsigned char ch)
     /* [previous][next][first][last][top][bottom][index][help] */
 972 {
 973         stlport_t       *portp;
 974         unsigned int    len;
 975         char            *head, *tail;
 976 
 977 #if DEBUG
 978         printk("stl_putchar(tty=%x,ch=%x)\n", (int) tty, (int) ch);
 979 #endif
 980 
 981         if (tty == (struct tty_struct *) NULL)
 982                 return;
 983         portp = tty->driver_data;
 984         if (portp == (stlport_t *) NULL)
 985                 return;
 986         if (portp->tx.buf == (char *) NULL)
 987                 return;
 988 
 989         head = portp->tx.head;
 990         tail = portp->tx.tail;
 991 
 992         len = (head >= tail) ? (STL_TXBUFSIZE - (head - tail)) : (tail - head);
 993         len--;
 994 
 995         if (len > 0) {
 996                 *head++ = ch;
 997                 if (head >= (portp->tx.buf + STL_TXBUFSIZE))
 998                         head = portp->tx.buf;
 999         }       
1000         portp->tx.head = head;
1001 }
1002 
1003 /*****************************************************************************/
1004 
1005 /*
1006  *      If there are any characters in the buffer then make sure that TX
1007  *      interrupts are on and get'em out. Normally used after the putchar
1008  *      routine has been called.
1009  */
1010 
1011 static void stl_flushchars(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1012 {
1013         stlport_t       *portp;
1014 
1015 #if DEBUG
1016         printk("stl_flushchars(tty=%x)\n", (int) tty);
1017 #endif
1018 
1019         if (tty == (struct tty_struct *) NULL)
1020                 return;
1021         portp = tty->driver_data;
1022         if (portp == (stlport_t *) NULL)
1023                 return;
1024         if (portp->tx.buf == (char *) NULL)
1025                 return;
1026 
1027 #if 0
1028         if (tty->stopped || tty->hw_stopped || (portp->tx.head == portp->tx.tail))
1029                 return;
1030 #endif
1031         stl_startrxtx(portp, -1, 1);
1032 }
1033 
1034 /*****************************************************************************/
1035 
1036 static int stl_writeroom(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1037 {
1038         stlport_t       *portp;
1039         char            *head, *tail;
1040 
1041 #if DEBUG
1042         printk("stl_writeroom(tty=%x)\n", (int) tty);
1043 #endif
1044 
1045         if (tty == (struct tty_struct *) NULL)
1046                 return(0);
1047         portp = tty->driver_data;
1048         if (portp == (stlport_t *) NULL)
1049                 return(0);
1050         if (portp->tx.buf == (char *) NULL)
1051                 return(0);
1052 
1053         head = portp->tx.head;
1054         tail = portp->tx.tail;
1055         return((head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) : (tail - head - 1));
1056 }
1057 
1058 /*****************************************************************************/
1059 
1060 /*
1061  *      Return number of chars in the TX buffer. Normally we would just
1062  *      calculate the number of chars in the buffer and return that, but if
1063  *      the buffer is empty and TX interrupts are still on then we return
1064  *      that the buffer still has 1 char in it. This way whoever called us
1065  *      will not think that ALL chars have drained - since the UART still
1066  *      must have some chars in it (we are busy after all).
1067  */
1068 
1069 static int stl_charsinbuffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1070 {
1071         stlport_t       *portp;
1072         unsigned int    size;
1073         char            *head, *tail;
1074 
1075 #if DEBUG
1076         printk("stl_charsinbuffer(tty=%x)\n", (int) tty);
1077 #endif
1078 
1079         if (tty == (struct tty_struct *) NULL)
1080                 return(0);
1081         portp = tty->driver_data;
1082         if (portp == (stlport_t *) NULL)
1083                 return(0);
1084         if (portp->tx.buf == (char *) NULL)
1085                 return(0);
1086 
1087         head = portp->tx.head;
1088         tail = portp->tx.tail;
1089         size = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
1090         if ((size == 0) && test_bit(ASYI_TXBUSY, &portp->istate))
1091                 size = 1;
1092         return(size);
1093 }
1094 
1095 /*****************************************************************************/
1096 
1097 /*
1098  *      Generate the serial struct info.
1099  */
1100 
1101 static void stl_getserial(stlport_t *portp, struct serial_struct *sp)
     /* [previous][next][first][last][top][bottom][index][help] */
1102 {
1103         struct serial_struct    sio;
1104         stlbrd_t                *brdp;
1105 
1106 #if DEBUG
1107         printk("stl_getserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1108 #endif
1109 
1110         memset(&sio, 0, sizeof(struct serial_struct));
1111         sio.type = PORT_CIRRUS;
1112         sio.line = portp->portnr;
1113         sio.port = portp->ioaddr;
1114         sio.flags = portp->flags;
1115         sio.baud_base = portp->baud_base;
1116         sio.close_delay = portp->close_delay;
1117         sio.closing_wait = portp->closing_wait;
1118         sio.custom_divisor = portp->custom_divisor;
1119         sio.xmit_fifo_size = CD1400_TXFIFOSIZE;
1120         sio.hub6 = 0;
1121 
1122         brdp = stl_brds[portp->brdnr];
1123         if (brdp != (stlbrd_t *) NULL)
1124                 sio.irq = brdp->irq;
1125 
1126         memcpy_tofs(sp, &sio, sizeof(struct serial_struct));
1127 }
1128 
1129 /*****************************************************************************/
1130 
1131 /*
1132  *      Set port according to the serial struct info.
1133  *      At this point we do not do any auto-configure stuff, so we will
1134  *      just quietly ignore any requests to change irq, etc.
1135  */
1136 
1137 static int stl_setserial(stlport_t *portp, struct serial_struct *sp)
     /* [previous][next][first][last][top][bottom][index][help] */
1138 {
1139         struct serial_struct    sio;
1140 
1141 #if DEBUG
1142         printk("stl_setserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1143 #endif
1144 
1145         memcpy_fromfs(&sio, sp, sizeof(struct serial_struct));
1146         if (!suser()) {
1147                 if ((sio.baud_base != portp->baud_base) ||
1148                                 (sio.close_delay != portp->close_delay) ||
1149                                 ((sio.flags & ~ASYNC_USR_MASK) != (portp->flags & ~ASYNC_USR_MASK)))
1150                         return(-EPERM);
1151         } 
1152 
1153         portp->flags = (portp->flags & ~ASYNC_USR_MASK) | (sio.flags & ASYNC_USR_MASK);
1154         portp->baud_base = sio.baud_base;
1155         portp->close_delay = sio.close_delay;
1156         portp->closing_wait = sio.closing_wait;
1157         portp->custom_divisor = sio.custom_divisor;
1158         stl_setport(portp, portp->tty->termios);
1159         return(0);
1160 }
1161 
1162 /*****************************************************************************/
1163 
1164 static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1165 {
1166         stlport_t       *portp;
1167         int             rc;
1168 
1169 #if DEBUG
1170         printk("stl_ioctl(tty=%x,file=%x,cmd=%x,arg=%x)\n", (int) tty, (int) file, cmd, (int) arg);
1171 #endif
1172 
1173         if (tty == (struct tty_struct *) NULL)
1174                 return(-ENODEV);
1175         portp = tty->driver_data;
1176         if (portp == (stlport_t *) NULL)
1177                 return(-ENODEV);
1178 
1179         rc = 0;
1180 
1181         switch (cmd) {
1182         case TCSBRK:
1183                 if ((rc = tty_check_change(tty)) == 0) {
1184                         tty_wait_until_sent(tty, 0);
1185                         if (! arg)
1186                                 stl_sendbreak(portp, 250);
1187                 }
1188                 break;
1189         case TCSBRKP:
1190                 if ((rc = tty_check_change(tty)) == 0) {
1191                         tty_wait_until_sent(tty, 0);
1192                         stl_sendbreak(portp, (arg ? (arg * 100) : 250));
1193                 }
1194                 break;
1195         case TIOCGSOFTCAR:
1196                 if ((rc = verify_area(VERIFY_WRITE, (void *) arg, sizeof(long))) == 0)
1197                         put_fs_long(((tty->termios->c_cflag & CLOCAL) ? 1 : 0), (unsigned long *) arg);
1198                 break;
1199         case TIOCSSOFTCAR:
1200                 if ((rc = verify_area(VERIFY_READ, (void *) arg, sizeof(long))) == 0) {
1201                         arg = get_fs_long((unsigned long *) arg);
1202                         tty->termios->c_cflag = (tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0);
1203                 }
1204                 break;
1205         case TIOCMGET:
1206                 if ((rc = verify_area(VERIFY_WRITE, (void *) arg, sizeof(unsigned int))) == 0) {
1207                         stl_getsignals(portp);
1208                         put_fs_long(portp->sigs, (unsigned long *) arg);
1209                 }
1210                 break;
1211         case TIOCMBIS:
1212                 if ((rc = verify_area(VERIFY_READ, (void *) arg, sizeof(long))) == 0) {
1213                         arg = get_fs_long((unsigned long *) arg);
1214                         stl_setsignals(portp, ((arg & TIOCM_DTR) ? 1 : -1), ((arg & TIOCM_RTS) ? 1 : -1));
1215                 }
1216                 break;
1217         case TIOCMBIC:
1218                 if ((rc = verify_area(VERIFY_READ, (void *) arg, sizeof(long))) == 0) {
1219                         arg = get_fs_long((unsigned long *) arg);
1220                         stl_setsignals(portp, ((arg & TIOCM_DTR) ? 0 : -1), ((arg & TIOCM_RTS) ? 0 : -1));
1221                 }
1222                 break;
1223         case TIOCMSET:
1224                 if ((rc = verify_area(VERIFY_READ, (void *) arg, sizeof(long))) == 0) {
1225                         arg = get_fs_long((unsigned long *) arg);
1226                         stl_setsignals(portp, ((arg & TIOCM_DTR) ? 1 : 0), ((arg & TIOCM_RTS) ? 1 : 0));
1227                 }
1228                 break;
1229         case TIOCGSERIAL:
1230                 if ((rc = verify_area(VERIFY_WRITE, (void *) arg, sizeof(struct serial_struct))) == 0)
1231                         stl_getserial(portp, (struct serial_struct *) arg);
1232                 break;
1233         case TIOCSSERIAL:
1234                 if ((rc = verify_area(VERIFY_READ, (void *) arg, sizeof(struct serial_struct))) == 0)
1235                         rc = stl_setserial(portp, (struct serial_struct *) arg);
1236                 break;
1237         case TIOCSERCONFIG:
1238         case TIOCSERGWILD:
1239         case TIOCSERSWILD:
1240         case TIOCSERGETLSR:
1241         case TIOCSERGSTRUCT:
1242         case TIOCSERGETMULTI:
1243         case TIOCSERSETMULTI:
1244         default:
1245                 rc = -ENOIOCTLCMD;
1246                 break;
1247         }
1248 
1249         return(rc);
1250 }
1251 
1252 /*****************************************************************************/
1253 
1254 static void stl_settermios(struct tty_struct *tty, struct termios *old)
     /* [previous][next][first][last][top][bottom][index][help] */
1255 {
1256         stlport_t       *portp;
1257         struct termios  *tiosp;
1258 
1259 #if DEBUG
1260         printk("stl_settermios(tty=%x,old=%x)\n", (int) tty, (int) old);
1261 #endif
1262 
1263         if (tty == (struct tty_struct *) NULL)
1264                 return;
1265         portp = tty->driver_data;
1266         if (portp == (stlport_t *) NULL)
1267                 return;
1268 
1269         tiosp = tty->termios;
1270         if ((tiosp->c_cflag == old->c_cflag) && (tiosp->c_iflag == old->c_iflag))
1271                 return;
1272 
1273         stl_setport(portp, tiosp);
1274         stl_setsignals(portp, ((tiosp->c_cflag & (CBAUD & ~CBAUDEX)) ? 1 : 0), -1);
1275         if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0)) {
1276                 tty->hw_stopped = 0;
1277                 stl_start(tty);
1278         }
1279         if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
1280                 wake_up_interruptible(&portp->open_wait);
1281 }
1282 
1283 /*****************************************************************************/
1284 
1285 /*
1286  *      Attempt to flow control who ever is sending us data. Based on termios
1287  *      settings use software or/and hardware flow control.
1288  */
1289 
1290 static void stl_throttle(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1291 {
1292         stlport_t       *portp;
1293         unsigned long   flags;
1294 
1295 #if DEBUG
1296         printk("stl_throttle(tty=%x)\n", (int) tty);
1297 #endif
1298 
1299         if (tty == (struct tty_struct *) NULL)
1300                 return;
1301         portp = tty->driver_data;
1302         if (portp == (stlport_t *) NULL)
1303                 return;
1304 
1305         save_flags(flags);
1306         cli();
1307         BRDENABLE(portp->brdnr, portp->pagenr);
1308         stl_setreg(portp, CAR, (portp->portnr & 0x03));
1309         if (tty->termios->c_iflag & IXOFF) {
1310                 stl_ccrwait(portp);
1311                 stl_setreg(portp, CCR, CCR_SENDSCHR2);
1312                 stl_ccrwait(portp);
1313         }
1314         if (tty->termios->c_cflag & CRTSCTS) {
1315                 stl_setreg(portp, MCOR1, (stl_getreg(portp, MCOR1) & 0xf0));
1316                 stl_setreg(portp, MSVR2, 0);
1317         }
1318         BRDDISABLE(portp->brdnr);
1319         restore_flags(flags);
1320 }
1321 
1322 /*****************************************************************************/
1323 
1324 /*
1325  *      Unflow control the device sending us data...
1326  */
1327 
1328 static void stl_unthrottle(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1329 {
1330         stlport_t       *portp;
1331         unsigned long   flags;
1332 
1333 #if DEBUG
1334         printk("stl_unthrottle(tty=%x)\n", (int) tty);
1335 #endif
1336 
1337         if (tty == (struct tty_struct *) NULL)
1338                 return;
1339         portp = tty->driver_data;
1340         if (portp == (stlport_t *) NULL)
1341                 return;
1342 
1343         save_flags(flags);
1344         cli();
1345         BRDENABLE(portp->brdnr, portp->pagenr);
1346         stl_setreg(portp, CAR, (portp->portnr & 0x03));
1347         if (tty->termios->c_iflag & IXOFF) {
1348                 stl_ccrwait(portp);
1349                 stl_setreg(portp, CCR, CCR_SENDSCHR1);
1350                 stl_ccrwait(portp);
1351         }
1352 /*
1353  *      Question: should we return RTS to what it was before? It may have
1354  *      been set by an ioctl... Suppose not, since if you have hardware
1355  *      flow control set then it is pretty silly to go and set the RTS line
1356  *      by hand.
1357  */
1358         if (tty->termios->c_cflag & CRTSCTS) {
1359                 stl_setreg(portp, MCOR1, (stl_getreg(portp, MCOR1) | FIFO_RTSTHRESHOLD));
1360                 stl_setreg(portp, MSVR2, MSVR2_RTS);
1361         }
1362         BRDDISABLE(portp->brdnr);
1363         restore_flags(flags);
1364 }
1365 
1366 /*****************************************************************************/
1367 
1368 /*
1369  *      Stop the transmitter. Basically to do this we will just turn TX
1370  *      interrupts off.
1371  */
1372 
1373 static void stl_stop(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1374 {
1375         stlport_t       *portp;
1376 
1377 #if DEBUG
1378         printk("stl_stop(tty=%x)\n", (int) tty);
1379 #endif
1380 
1381         if (tty == (struct tty_struct *) NULL)
1382                 return;
1383         portp = tty->driver_data;
1384         if (portp == (stlport_t *) NULL)
1385                 return;
1386 
1387         stl_startrxtx(portp, -1, 0);
1388 }
1389 
1390 /*****************************************************************************/
1391 
1392 /*
1393  *      Start the transmitter again. Just turn TX interrupts back on.
1394  */
1395 
1396 static void stl_start(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1397 {
1398         stlport_t       *portp;
1399 
1400 #if DEBUG
1401         printk("stl_start(tty=%x)\n", (int) tty);
1402 #endif
1403 
1404         if (tty == (struct tty_struct *) NULL)
1405                 return;
1406         portp = tty->driver_data;
1407         if (portp == (stlport_t *) NULL)
1408                 return;
1409 
1410         stl_startrxtx(portp, -1, 1);
1411 }
1412 
1413 /*****************************************************************************/
1414 
1415 /*
1416  *      Hangup this port. This is pretty much like closing the port, only
1417  *      a little more brutal. No waiting for data to drain. Shutdown the
1418  *      port and maybe drop signals.
1419  */
1420 
1421 static void stl_hangup(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1422 {
1423         stlport_t       *portp;
1424 
1425 #if DEBUG
1426         printk("stl_hangup(tty=%x)\n", (int) tty);
1427 #endif
1428 
1429         if (tty == (struct tty_struct *) NULL)
1430                 return;
1431         portp = tty->driver_data;
1432         if (portp == (stlport_t *) NULL)
1433                 return;
1434 
1435         portp->flags &= ~ASYNC_INITIALIZED;
1436         stl_disableintrs(portp);
1437         if (tty->termios->c_cflag & HUPCL)
1438                 stl_setsignals(portp, 0, 0);
1439         stl_enablerxtx(portp, 0, 0);
1440         stl_flushbuffer(tty);
1441         portp->istate = 0;
1442         set_bit(TTY_IO_ERROR, &tty->flags);
1443         if (portp->tx.buf != (char *) NULL) {
1444                 kfree_s(portp->tx.buf, STL_TXBUFSIZE);
1445                 portp->tx.buf = (char *) NULL;
1446                 portp->tx.head = (char *) NULL;
1447                 portp->tx.tail = (char *) NULL;
1448         }
1449         tty->driver_data = (void *) NULL;
1450         portp->tty = (struct tty_struct *) NULL;
1451         portp->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE);
1452         portp->refcount = 0;
1453         wake_up_interruptible(&portp->open_wait);
1454 }
1455 
1456 /*****************************************************************************/
1457 
1458 static void stl_flushbuffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1459 {
1460         stlport_t       *portp;
1461         unsigned long   flags;
1462 
1463 #if DEBUG
1464         printk("stl_flushbuffer(tty=%x)\n", (int) tty);
1465 #endif
1466 
1467         if (tty == (struct tty_struct *) NULL)
1468                 return;
1469         portp = tty->driver_data;
1470         if (portp == (stlport_t *) NULL)
1471                 return;
1472 
1473         save_flags(flags);
1474         cli();
1475         BRDENABLE(portp->brdnr, portp->pagenr);
1476         stl_setreg(portp, CAR, (portp->portnr & 0x03));
1477         stl_ccrwait(portp);
1478         stl_setreg(portp, CCR, CCR_TXFLUSHFIFO);
1479         stl_ccrwait(portp);
1480         portp->tx.tail = portp->tx.head;
1481         BRDDISABLE(portp->brdnr);
1482         restore_flags(flags);
1483 
1484         wake_up_interruptible(&tty->write_wait);
1485         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1486                 (tty->ldisc.write_wakeup)(tty);
1487 }
1488 
1489 /*****************************************************************************/
1490 
1491 /*
1492  *      These functions get/set/update the registers of the cd1400 UARTs.
1493  *      Access to the cd1400 registers is via an address/data io port pair.
1494  *      (Maybe should make this inline...)
1495  */
1496 
1497 static int stl_getreg(stlport_t *portp, int regnr)
     /* [previous][next][first][last][top][bottom][index][help] */
1498 {
1499         outb((regnr + portp->uartaddr), portp->ioaddr);
1500         return(inb(portp->ioaddr + EREG_DATA));
1501 }
1502 
1503 static void stl_setreg(stlport_t *portp, int regnr, int value)
     /* [previous][next][first][last][top][bottom][index][help] */
1504 {
1505         outb((regnr + portp->uartaddr), portp->ioaddr);
1506         outb(value, portp->ioaddr + EREG_DATA);
1507 }
1508 
1509 static int stl_updatereg(stlport_t *portp, int regnr, int value)
     /* [previous][next][first][last][top][bottom][index][help] */
1510 {
1511         outb((regnr + portp->uartaddr), portp->ioaddr);
1512         if (inb(portp->ioaddr + EREG_DATA) != value) {
1513                 outb(value, portp->ioaddr + EREG_DATA);
1514                 return(1);
1515         }
1516         return(0);
1517 }
1518 
1519 /*****************************************************************************/
1520 
1521 /*
1522  *      Transmit interrupt handler. This has gotta be fast!  Handling TX
1523  *      chars is pretty simple, stuff as many as possible from the TX buffer
1524  *      into the cd1400 FIFO. Must also handle TX breaks here, since they
1525  *      are embedded as commands in the data stream. Oh no, had to use a goto!
1526  *      This could be optimized more, will do when I get time...
1527  *      In practice it is possible that interrupts are enabled but that the
1528  *      port has been hung up. Need to handle not having any TX buffer here,
1529  *      this is done by using the side effect that head and tail will also
1530  *      be NULL if the buffer has been freed.
1531  */
1532 
1533 static inline void stl_txisr(stlpanel_t *panelp, int ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
1534 {
1535         stlport_t       *portp;
1536         int             len, stlen;
1537         char            *head, *tail;
1538         unsigned char   ioack, srer;
1539 
1540 #if DEBUG
1541         printk("stl_txisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
1542 #endif
1543 
1544         ioack = inb(ioaddr + EREG_TXACK);
1545         if (((ioack & panelp->ackmask) != 0) || ((ioack & ACK_TYPMASK) != ACK_TYPTX)) {
1546                 printk("STALLION: bad TX interrupt ack value=%x\n", ioack);
1547                 return;
1548         }
1549         portp = panelp->ports[(ioack >> 3)];
1550 
1551 /*
1552  *      Unfortunately we need to handle breaks in the data stream, since
1553  *      this is the only way to generate them on the cd1400. Do it now if
1554  *      a break is to be sent.
1555  */
1556         if (portp->brklen != 0) {
1557                 if (portp->brklen > 0) {
1558                         outb((TDR + portp->uartaddr), ioaddr);
1559                         outb(ETC_CMD, (ioaddr + EREG_DATA));
1560                         outb(ETC_STARTBREAK, (ioaddr + EREG_DATA));
1561                         outb(ETC_CMD, (ioaddr + EREG_DATA));
1562                         outb(ETC_DELAY, (ioaddr + EREG_DATA));
1563                         outb(portp->brklen, (ioaddr + EREG_DATA));
1564                         outb(ETC_CMD, (ioaddr + EREG_DATA));
1565                         outb(ETC_STOPBREAK, (ioaddr + EREG_DATA));
1566                         portp->brklen = -1;
1567                         goto stl_txalldone;
1568                 } else {
1569                         outb((COR2 + portp->uartaddr), ioaddr);
1570                         outb((inb(ioaddr + EREG_DATA) & ~COR2_ETC), (ioaddr + EREG_DATA));
1571                         portp->brklen = 0;
1572                 }
1573         }
1574 
1575         head = portp->tx.head;
1576         tail = portp->tx.tail;
1577         len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
1578         if ((len == 0) || ((len < STL_TXBUFLOW) && (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
1579                 set_bit(ASYI_TXLOW, &portp->istate);
1580                 queue_task_irq_off(&portp->tqueue, &tq_scheduler);
1581         }
1582 
1583         if (len == 0) {
1584                 outb((SRER + portp->uartaddr), ioaddr);
1585                 srer = inb(ioaddr + EREG_DATA);
1586                 if (srer & SRER_TXDATA) {
1587                         srer = (srer & ~SRER_TXDATA) | SRER_TXEMPTY;
1588                 } else {
1589                         srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
1590                         clear_bit(ASYI_TXBUSY, &portp->istate);
1591                 }
1592                 outb(srer, (ioaddr + EREG_DATA));
1593         } else {
1594                 len = MIN(len, CD1400_TXFIFOSIZE);
1595                 stlen = MIN(len, ((portp->tx.buf + STL_TXBUFSIZE) - tail));
1596                 outb((TDR + portp->uartaddr), ioaddr);
1597                 outsb((ioaddr + EREG_DATA), tail, stlen);
1598                 len -= stlen;
1599                 tail += stlen;
1600                 if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
1601                         tail = portp->tx.buf;
1602                 if (len > 0) {
1603                         outsb((ioaddr + EREG_DATA), tail, len);
1604                         tail += len;
1605                 }
1606                 portp->tx.tail = tail;
1607         }
1608 
1609 stl_txalldone:
1610         outb((EOSRR + portp->uartaddr), ioaddr);
1611         outb(0, (ioaddr + EREG_DATA));
1612 }
1613 
1614 /*****************************************************************************/
1615 
1616 /*
1617  *      Receive character interrupt handler. Determine if we have good chars
1618  *      or bad chars and then process appropriately. Good chars are easy
1619  *      just shove the lot into the RX buffer and set all status byte to 0.
1620  *      If a bad RX char then process as required. This routine needs to be
1621  *      fast!  In practice it is possible that we get an interrupt on a port
1622  *      that is closed. This can happen on hangups - since they completely
1623  *      shutdown a port not in user context. Need to handle this case.
1624  */
1625 
1626 static inline void stl_rxisr(stlpanel_t *panelp, int ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
1627 {
1628         stlport_t               *portp;
1629         struct tty_struct       *tty;
1630         unsigned int            ioack, len, buflen;
1631         unsigned char           status;
1632         char                    ch;
1633         static char             unwanted[CD1400_RXFIFOSIZE];
1634 
1635 #if DEBUG
1636         printk("stl_rxisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
1637 #endif
1638 
1639         ioack = inb(ioaddr + EREG_RXACK);
1640         if ((ioack & panelp->ackmask) != 0) {
1641                 printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
1642                 return;
1643         }
1644         portp = panelp->ports[(ioack >> 3)];
1645         tty = portp->tty;
1646 
1647         if ((ioack & ACK_TYPMASK) == ACK_TYPRXGOOD) {
1648                 outb((RDCR + portp->uartaddr), ioaddr);
1649                 len = inb(ioaddr + EREG_DATA);
1650                 if ((tty == (struct tty_struct *) NULL) || (tty->flip.char_buf_ptr == (char *) NULL) ||
1651                                 ((buflen = TTY_FLIPBUF_SIZE - tty->flip.count) == 0)) {
1652                         outb((RDSR + portp->uartaddr), ioaddr);
1653                         insb((ioaddr + EREG_DATA), &unwanted[0], len);
1654                 } else {
1655                         len = MIN(len, buflen);
1656                         if (len > 0) {
1657                                 outb((RDSR + portp->uartaddr), ioaddr);
1658                                 insb((ioaddr + EREG_DATA), tty->flip.char_buf_ptr, len);
1659                                 memset(tty->flip.flag_buf_ptr, 0, len);
1660                                 tty->flip.flag_buf_ptr += len;
1661                                 tty->flip.char_buf_ptr += len;
1662                                 tty->flip.count += len;
1663                                 tty_schedule_flip(tty);
1664                         }
1665                 }
1666         } else if ((ioack & ACK_TYPMASK) == ACK_TYPRXBAD) {
1667                 outb((RDSR + portp->uartaddr), ioaddr);
1668                 status = inb(ioaddr + EREG_DATA);
1669                 ch = inb(ioaddr + EREG_DATA);
1670                 if ((tty != (struct tty_struct *) NULL) && ((portp->rxignoremsk & status) == 0)) {
1671                         if (portp->rxmarkmsk & status) {
1672                                 if (status & ST_BREAK) {
1673                                         status = TTY_BREAK;
1674 #ifndef MODULE
1675                                         if (portp->flags & ASYNC_SAK)
1676                                                 do_SAK(tty);
1677 #endif
1678                                 } else if (status & ST_PARITY) {
1679                                         status = TTY_PARITY;
1680                                 } else if (status & ST_FRAMING) {
1681                                         status = TTY_FRAME;
1682                                 } else if(status & ST_OVERRUN) {
1683                                         status = TTY_OVERRUN;
1684                                 } else {
1685                                         status = 0;
1686                                 }
1687                         } else {
1688                                 status = 0;
1689                         }
1690                         if (tty->flip.char_buf_ptr != (char *) NULL) {
1691                                 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
1692                                         *tty->flip.flag_buf_ptr++ = status;
1693                                         *tty->flip.char_buf_ptr++ = ch;
1694                                         tty->flip.count++;
1695                                 }
1696                                 tty_schedule_flip(tty);
1697                         }
1698                 }
1699         } else {
1700                 printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
1701                 return;
1702         }
1703 
1704         outb((EOSRR + portp->uartaddr), ioaddr);
1705         outb(0, (ioaddr + EREG_DATA));
1706 }
1707 
1708 /*****************************************************************************/
1709 
1710 /*
1711  *      Modem interrupt handler. The is called when the modem signal line
1712  *      (DCD) has changed state. Leave most of the work to the off-level
1713  *      processing routine.
1714  */
1715 
1716 static inline void stl_mdmisr(stlpanel_t *panelp, int ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
1717 {
1718         stlport_t       *portp;
1719         unsigned int    ioack;
1720         unsigned char   misr;
1721 
1722 #if DEBUG
1723         printk("stl_mdmisr(panelp=%x)\n", (int) panelp);
1724 #endif
1725 
1726         ioack = inb(ioaddr + EREG_MDACK);
1727         if (((ioack & panelp->ackmask) != 0) || ((ioack & ACK_TYPMASK) != ACK_TYPMDM)) {
1728                 printk("STALLION: bad MODEM interrupt ack value=%x\n", ioack);
1729                 return;
1730         }
1731         portp = panelp->ports[(ioack >> 3)];
1732 
1733         outb((MISR + portp->uartaddr), ioaddr);
1734         misr = inb(ioaddr + EREG_DATA);
1735         if (misr & MISR_DCD) {
1736                 set_bit(ASYI_DCDCHANGE, &portp->istate);
1737                 queue_task_irq_off(&portp->tqueue, &tq_scheduler);
1738         }
1739 
1740         outb((EOSRR + portp->uartaddr), ioaddr);
1741         outb(0, (ioaddr + EREG_DATA));
1742 }
1743 
1744 /*****************************************************************************/
1745 
1746 /*
1747  *      Interrupt handler for EIO and ECH boards. This code ain't all that
1748  *      pretty, but the idea is to make it as fast as possible. This code is
1749  *      well suited to be assemblerized :-)  We don't use the general purpose
1750  *      register access functions here, for speed we will go strait to the
1751  *      io region.
1752  */
1753 
1754 static void stl_intr(int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
1755 {
1756         stlbrd_t        *brdp;
1757         stlpanel_t      *panelp;
1758         unsigned char   svrtype;
1759         int             i, panelnr, iobase;
1760 
1761 #if DEBUG
1762         printk("stl_intr(irq=%d,regs=%x)\n", irq, (int) regs);
1763 #endif
1764 
1765         panelp = (stlpanel_t *) NULL;
1766         for (i = 0; (i < stl_nrbrds); ) {
1767                 if ((brdp = stl_brds[i]) == (stlbrd_t *) NULL) {
1768                         i++;
1769                         continue;
1770                 }
1771                 if (brdp->state == 0) {
1772                         i++;
1773                         continue;
1774                 }
1775 /*
1776  *              The following section of code handles the subtle differences
1777  *              between board types. It is sort of similar, but different
1778  *              enough to handle each separately.
1779  */
1780                 if (brdp->brdtype == BRD_EASYIO) {
1781                         if ((inb(brdp->iostatus) & EIO_INTRPEND) == 0) {
1782                                 i++;
1783                                 continue;
1784                         }
1785                         panelp = brdp->panels[0];
1786                         iobase = panelp->iobase;
1787                         outb(SVRR, iobase);
1788                         svrtype = inb(iobase + EREG_DATA);
1789                         if (brdp->nrports > 4) {
1790                                 outb((SVRR + 0x80), iobase);
1791                                 svrtype |= inb(iobase + EREG_DATA);
1792                         }
1793                 } else if (brdp->brdtype == BRD_ECH) {
1794                         if ((inb(brdp->iostatus) & ECH_INTRPEND) == 0) {
1795                                 i++;
1796                                 continue;
1797                         }
1798                         outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
1799                         for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
1800                                 panelp = brdp->panels[panelnr];
1801                                 iobase = panelp->iobase;
1802                                 if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1803                                         break;
1804                                 if (panelp->nrports > 8) {
1805                                         iobase += 0x8;
1806                                         if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1807                                                 break;
1808                                 }
1809                         }       
1810                         if (panelnr >= brdp->nrpanels) {
1811                                 i++;
1812                                 continue;
1813                         }
1814                         outb(SVRR, iobase);
1815                         svrtype = inb(iobase + EREG_DATA);
1816                         outb((SVRR + 0x80), iobase);
1817                         svrtype |= inb(iobase + EREG_DATA);
1818                 } else if (brdp->brdtype == BRD_ECHPCI) {
1819                         iobase = brdp->ioaddr2;
1820                         for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
1821                                 panelp = brdp->panels[panelnr];
1822                                 outb(panelp->pagenr, brdp->ioctrl);
1823                                 if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1824                                         break;
1825                                 if (panelp->nrports > 8) {
1826                                         outb((panelp->pagenr + 1), brdp->ioctrl);
1827                                         if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1828                                                 break;
1829                                 }
1830                         }       
1831                         if (panelnr >= brdp->nrpanels) {
1832                                 i++;
1833                                 continue;
1834                         }
1835                         outb(SVRR, iobase);
1836                         svrtype = inb(iobase + EREG_DATA);
1837                         outb((SVRR + 0x80), iobase);
1838                         svrtype |= inb(iobase + EREG_DATA);
1839                 } else if (brdp->brdtype == BRD_ECHMC) {
1840                         if ((inb(brdp->iostatus) & ECH_INTRPEND) == 0) {
1841                                 i++;
1842                                 continue;
1843                         }
1844                         for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
1845                                 panelp = brdp->panels[panelnr];
1846                                 iobase = panelp->iobase;
1847                                 if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1848                                         break;
1849                                 if (panelp->nrports > 8) {
1850                                         iobase += 0x8;
1851                                         if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1852                                                 break;
1853                                 }
1854                         }       
1855                         if (panelnr >= brdp->nrpanels) {
1856                                 i++;
1857                                 continue;
1858                         }
1859                         outb(SVRR, iobase);
1860                         svrtype = inb(iobase + EREG_DATA);
1861                         outb((SVRR + 0x80), iobase);
1862                         svrtype |= inb(iobase + EREG_DATA);
1863                 } else {
1864                         printk("STALLION: unknown board type=%x\n", brdp->brdtype);
1865                         i++;
1866                         continue;
1867                 }
1868 
1869 /*
1870  *              We have determined what type of service is required for a
1871  *              port. From here on in the service of a port is the same no
1872  *              matter what the board type...
1873  */
1874                 if (svrtype & SVRR_RX)
1875                         stl_rxisr(panelp, iobase);
1876                 if (svrtype & SVRR_TX)
1877                         stl_txisr(panelp, iobase);
1878                 if (svrtype & SVRR_MDM)
1879                         stl_mdmisr(panelp, iobase);
1880 
1881                 if (brdp->brdtype == BRD_ECH)
1882                         outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
1883         }
1884 }
1885 
1886 /*****************************************************************************/
1887 
1888 /*
1889  *      Service an off-level request for some channel.
1890  */
1891 
1892 static void stl_offintr(void *private)
     /* [previous][next][first][last][top][bottom][index][help] */
1893 {
1894         stlport_t               *portp;
1895         struct tty_struct       *tty;
1896         unsigned int            oldsigs;
1897 
1898         portp = private;
1899 #if DEBUG
1900         printk("stl_offintr(portp=%x)\n", (int) portp);
1901 #endif
1902 
1903         if (portp == (stlport_t *) NULL)
1904                 return;
1905         tty = portp->tty;
1906         if (tty == (struct tty_struct *) NULL)
1907                 return;
1908 
1909         if (test_bit(ASYI_TXLOW, &portp->istate)) {
1910                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1911                         (tty->ldisc.write_wakeup)(tty);
1912                 wake_up_interruptible(&tty->write_wait);
1913         }
1914         if (test_bit(ASYI_DCDCHANGE, &portp->istate)) {
1915                 clear_bit(ASYI_DCDCHANGE, &portp->istate);
1916                 oldsigs = portp->sigs;
1917                 stl_getsignals(portp);
1918                 if ((portp->sigs & TIOCM_CD) && ((oldsigs & TIOCM_CD) == 0))
1919                         wake_up_interruptible(&portp->open_wait);
1920                 if ((oldsigs & TIOCM_CD) && ((portp->sigs & TIOCM_CD) == 0)) {
1921                         if (! ((portp->flags & ASYNC_CALLOUT_ACTIVE) &&
1922                                         (portp->flags & ASYNC_CALLOUT_NOHUP))) {
1923                                 tty_hangup(tty);
1924                         }
1925                 }
1926         }
1927 }
1928 
1929 /*****************************************************************************/
1930 
1931 /*
1932  *      Wait for the command register to be ready. We will poll this,
1933  *      since it won't usually take too long to be ready.
1934  */
1935 
1936 static void stl_ccrwait(stlport_t *portp)
     /* [previous][next][first][last][top][bottom][index][help] */
1937 {
1938         int     i;
1939 
1940         for (i = 0; (i < CCR_MAXWAIT); i++) {
1941                 if (stl_getreg(portp, CCR) == 0) {
1942                         return;
1943                 }
1944         }
1945 
1946         printk("STALLION: cd1400 device not responding, port=%d panel=%d brd=%d\n", portp->portnr, portp->panelnr, portp->brdnr);
1947 }
1948 
1949 /*****************************************************************************/
1950 
1951 /*
1952  *      Set up the cd1400 registers for a port based on the termios port
1953  *      settings.
1954  */
1955 
1956 static void stl_setport(stlport_t *portp, struct termios *tiosp)
     /* [previous][next][first][last][top][bottom][index][help] */
1957 {
1958         stlbrd_t        *brdp;
1959         unsigned long   flags;
1960         unsigned int    clkdiv, baudrate;
1961         unsigned char   cor1, cor2, cor3;
1962         unsigned char   cor4, cor5, ccr;
1963         unsigned char   srer, sreron, sreroff;
1964         unsigned char   mcor1, mcor2, rtpr;
1965         unsigned char   clk, div;
1966 
1967         cor1 = 0;
1968         cor2 = 0;
1969         cor3 = 0;
1970         cor4 = 0;
1971         cor5 = 0;
1972         ccr = 0;
1973         rtpr = 0;
1974         clk = 0;
1975         div = 0;
1976         mcor1 = 0;
1977         mcor2 = 0;
1978         sreron = 0;
1979         sreroff = 0;
1980 
1981         brdp = stl_brds[portp->brdnr];
1982         if (brdp == (stlbrd_t *) NULL)
1983                 return;
1984 
1985 /*
1986  *      Set up the RX char ignore mask with those RX error types we
1987  *      can ignore. We can get the cd1400 to help us out a little here,
1988  *      it will ignore parity errors and breaks for us.
1989  */
1990         portp->rxignoremsk = 0;
1991         if (tiosp->c_iflag & IGNPAR) {
1992                 portp->rxignoremsk |= (ST_PARITY | ST_FRAMING | ST_OVERRUN);
1993                 cor1 |= COR1_PARIGNORE;
1994         }
1995         if (tiosp->c_iflag & IGNBRK) {
1996                 portp->rxignoremsk |= ST_BREAK;
1997                 cor4 |= COR4_IGNBRK;
1998         }
1999 
2000         portp->rxmarkmsk = ST_OVERRUN;
2001         if (tiosp->c_iflag & (INPCK | PARMRK))
2002                 portp->rxmarkmsk |= (ST_PARITY | ST_FRAMING);
2003         if (tiosp->c_iflag & BRKINT)
2004                 portp->rxmarkmsk |= ST_BREAK;
2005 
2006 /*
2007  *      Go through the char size, parity and stop bits and set all the
2008  *      option register appropriately.
2009  */
2010         switch (tiosp->c_cflag & CSIZE) {
2011         case CS5:
2012                 cor1 |= COR1_CHL5;
2013                 break;
2014         case CS6:
2015                 cor1 |= COR1_CHL6;
2016                 break;
2017         case CS7:
2018                 cor1 |= COR1_CHL7;
2019                 break;
2020         default:
2021                 cor1 |= COR1_CHL8;
2022                 break;
2023         }
2024 
2025         if (tiosp->c_cflag & CSTOPB)
2026                 cor1 |= COR1_STOP2;
2027         else
2028                 cor1 |= COR1_STOP1;
2029 
2030         if (tiosp->c_cflag & PARENB) {
2031                 if (tiosp->c_cflag & PARODD)
2032                         cor1 |= (COR1_PARENB | COR1_PARODD);
2033                 else
2034                         cor1 |= (COR1_PARENB | COR1_PAREVEN);
2035         } else {
2036                 cor1 |= COR1_PARNONE;
2037         }
2038 
2039 /*
2040  *      Set the RX FIFO threshold at 6 chars. This gives a bit of breathing
2041  *      space for hardware flow control and the like. This should be set to
2042  *      VMIN. Also here we will set the RX data timeout to 10ms - this should
2043  *      really be based on VTIME.
2044  */
2045         cor3 |= FIFO_RXTHRESHOLD;
2046         rtpr = 2;
2047 
2048 /*
2049  *      Calculate the baud rate timers. For now we will just assume that
2050  *      the input and output baud are the same. Could have used a baud
2051  *      table here, but this way we can generate virtually any baud rate
2052  *      we like!
2053  */
2054         baudrate = tiosp->c_cflag & CBAUD;
2055         if (baudrate & CBAUDEX) {
2056                 baudrate &= ~CBAUDEX;
2057                 if ((baudrate < 1) || (baudrate > 2))
2058                         tiosp->c_cflag &= ~CBAUDEX;
2059                 else
2060                         baudrate += 15;
2061         }
2062         baudrate = stl_baudrates[baudrate];
2063         if ((tiosp->c_cflag & CBAUD) == B38400) {
2064                 if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
2065                         baudrate = 57600;
2066                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
2067                         baudrate = 115200;
2068                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
2069                         baudrate = (portp->baud_base / portp->custom_divisor);
2070         }
2071         if (baudrate > STL_MAXBAUD)
2072                 baudrate = STL_MAXBAUD;
2073 
2074         if (baudrate > 0) {
2075                 for (clk = 0; (clk < CD1400_NUMCLKS); clk++) {
2076                         clkdiv = ((CD1400_CLKHZ / stl_cd1400clkdivs[clk]) / baudrate);
2077                         if (clkdiv < 0x100)
2078                                 break;
2079                 }
2080                 div = (unsigned char) clkdiv;
2081         }
2082 
2083 /*
2084  *      Check what form of modem signaling is required and set it up.
2085  */
2086         if ((tiosp->c_cflag & CLOCAL) == 0) {
2087                 mcor1 |= MCOR1_DCD;
2088                 mcor2 |= MCOR2_DCD;
2089                 sreron |= SRER_MODEM;
2090         }
2091 
2092 /*
2093  *      Setup cd1400 enhanced modes if we can. In particular we want to
2094  *      handle as much of the flow control as possbile automatically. As
2095  *      well as saving a few CPU cycles it will also greatly improve flow
2096  *      control reliablilty.
2097  */
2098         if (tiosp->c_iflag & IXON) {
2099                 cor2 |= COR2_TXIBE;
2100                 cor3 |= (COR3_FCT | COR3_SCD12);
2101                 if (tiosp->c_iflag & IXANY)
2102                         cor2 |= COR2_IXM;
2103         }
2104 
2105         if (tiosp->c_cflag & CRTSCTS) {
2106                 cor2 |= COR2_CTSAE;
2107                 mcor1 |= FIFO_RTSTHRESHOLD;
2108         }
2109 
2110 /*
2111  *      All register cd1400 register values calculated so go through and set
2112  *      them all up.
2113  */
2114 
2115 #if DEBUG
2116         printk("SETPORT: portnr=%d panelnr=%d brdnr=%d\n", portp->portnr, portp->panelnr, portp->brdnr);
2117         printk("    cor1=%x cor2=%x cor3=%x cor4=%x cor5=%x\n", cor1, cor2, cor3, cor4, cor5);
2118         printk("    mcor1=%x mcor2=%x rtpr=%x sreron=%x sreroff=%x\n", mcor1, mcor2, rtpr, sreron, sreroff);
2119         printk("    tcor=%x tbpr=%x rcor=%x rbpr=%x\n", clk, div, clk, div);
2120         printk("    schr1=%x schr2=%x schr3=%x schr4=%x\n", tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP], tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
2121 #endif
2122 
2123         save_flags(flags);
2124         cli();
2125         BRDENABLE(portp->brdnr, portp->pagenr);
2126         stl_setreg(portp, CAR, (portp->portnr & 0x3));
2127         srer = stl_getreg(portp, SRER);
2128         stl_setreg(portp, SRER, 0);
2129         if (stl_updatereg(portp, COR1, cor1))
2130                 ccr = 1;
2131         if (stl_updatereg(portp, COR2, cor2))
2132                 ccr = 1;
2133         if (stl_updatereg(portp, COR3, cor3))
2134                 ccr = 1;
2135         if (ccr) {
2136                 stl_ccrwait(portp);
2137                 stl_setreg(portp, CCR, CCR_CORCHANGE);
2138         }
2139         stl_setreg(portp, COR4, cor4);
2140         stl_setreg(portp, COR5, cor5);
2141         stl_setreg(portp, MCOR1, mcor1);
2142         stl_setreg(portp, MCOR2, mcor2);
2143         if (baudrate > 0) {
2144                 stl_setreg(portp, TCOR, clk);
2145                 stl_setreg(portp, TBPR, div);
2146                 stl_setreg(portp, RCOR, clk);
2147                 stl_setreg(portp, RBPR, div);
2148         }
2149         stl_setreg(portp, SCHR1, tiosp->c_cc[VSTART]);
2150         stl_setreg(portp, SCHR2, tiosp->c_cc[VSTOP]);
2151         stl_setreg(portp, SCHR3, tiosp->c_cc[VSTART]);
2152         stl_setreg(portp, SCHR4, tiosp->c_cc[VSTOP]);
2153         stl_setreg(portp, RTPR, rtpr);
2154         mcor1 = stl_getreg(portp, MSVR1);
2155         if (mcor1 & MSVR1_DCD)
2156                 portp->sigs |= TIOCM_CD;
2157         else
2158                 portp->sigs &= ~TIOCM_CD;
2159         stl_setreg(portp, SRER, ((srer & ~sreroff) | sreron));
2160         BRDDISABLE(portp->brdnr);
2161         restore_flags(flags);
2162 }
2163 
2164 /*****************************************************************************/
2165 
2166 /*
2167  *      Set the state of the DTR and RTS signals.
2168  */
2169 
2170 static void stl_setsignals(stlport_t *portp, int dtr, int rts)
     /* [previous][next][first][last][top][bottom][index][help] */
2171 {
2172         unsigned char   msvr1, msvr2;
2173         unsigned long   flags;
2174 
2175 #if DEBUG
2176         printk("stl_setsignals(portp=%x,dtr=%d,rts=%d)\n", (int) portp, dtr, rts);
2177 #endif
2178 
2179         msvr1 = 0;
2180         msvr2 = 0;
2181         if (dtr > 0)
2182                 msvr1 = MSVR1_DTR;
2183         if (rts > 0)
2184                 msvr2 = MSVR2_RTS;
2185 
2186         save_flags(flags);
2187         cli();
2188         BRDENABLE(portp->brdnr, portp->pagenr);
2189         stl_setreg(portp, CAR, (portp->portnr & 0x03));
2190         if (rts >= 0)
2191                 stl_setreg(portp, MSVR2, msvr2);
2192         if (dtr >= 0)
2193                 stl_setreg(portp, MSVR1, msvr1);
2194         BRDDISABLE(portp->brdnr);
2195         restore_flags(flags);
2196 }
2197 
2198 /*****************************************************************************/
2199 
2200 /*
2201  *      Get the state of the signals.
2202  */
2203 
2204 static void stl_getsignals(stlport_t *portp)
     /* [previous][next][first][last][top][bottom][index][help] */
2205 {
2206         unsigned char   msvr1, msvr2;
2207         unsigned long   flags;
2208 
2209 #if DEBUG
2210         printk("stl_getsignals(portp=%x)\n", (int) portp);
2211 #endif
2212 
2213         save_flags(flags);
2214         cli();
2215         BRDENABLE(portp->brdnr, portp->pagenr);
2216         stl_setreg(portp, CAR, (portp->portnr & 0x03));
2217         msvr1 = stl_getreg(portp, MSVR1);
2218         msvr2 = stl_getreg(portp, MSVR2);
2219         BRDDISABLE(portp->brdnr);
2220         portp->sigs = 0;
2221         portp->sigs |= (msvr1 & MSVR1_DCD) ? TIOCM_CD : 0;
2222         portp->sigs |= (msvr1 & MSVR1_CTS) ? TIOCM_CTS : 0;
2223         portp->sigs |= (msvr1 & MSVR1_RI) ? TIOCM_RI : 0;
2224         portp->sigs |= (msvr1 & MSVR1_DSR) ? TIOCM_DSR : 0;
2225         portp->sigs |= (msvr1 & MSVR1_DTR) ? TIOCM_DTR : 0;
2226         portp->sigs |= (msvr2 & MSVR2_RTS) ? TIOCM_RTS : 0;
2227         restore_flags(flags);
2228 }
2229 
2230 /*****************************************************************************/
2231 
2232 /*
2233  *      Enable/Disable the Transmitter and/or Reciever.
2234  */
2235 
2236 static void stl_enablerxtx(stlport_t *portp, int rx, int tx)
     /* [previous][next][first][last][top][bottom][index][help] */
2237 {
2238         unsigned char   ccr;
2239         unsigned long   flags;
2240 
2241 #if DEBUG
2242         printk("stl_enablerxtx(portp=%x,rx=%d,tx=%d)\n", (int) portp, rx, tx);
2243 #endif
2244         ccr = 0;
2245 
2246         if (tx == 0)
2247                 ccr |= CCR_TXDISABLE;
2248         else if (tx > 0)
2249                 ccr |= CCR_TXENABLE;
2250         if (rx == 0)
2251                 ccr |= CCR_RXDISABLE;
2252         else if (rx > 0)
2253                 ccr |= CCR_RXENABLE;
2254 
2255         save_flags(flags);
2256         cli();
2257         BRDENABLE(portp->brdnr, portp->pagenr);
2258         stl_setreg(portp, CAR, (portp->portnr & 0x03));
2259         stl_ccrwait(portp);
2260         stl_setreg(portp, CCR, ccr);
2261         stl_ccrwait(portp);
2262         BRDDISABLE(portp->brdnr);
2263         restore_flags(flags);
2264 }
2265 
2266 /*****************************************************************************/
2267 
2268 /*
2269  *      Start/stop the Transmitter and/or Reciever.
2270  */
2271 
2272 static void stl_startrxtx(stlport_t *portp, int rx, int tx)
     /* [previous][next][first][last][top][bottom][index][help] */
2273 {
2274         unsigned char   sreron, sreroff;
2275         unsigned long   flags;
2276 
2277 #if DEBUG
2278         printk("stl_startrxtx(portp=%x,rx=%d,tx=%d)\n", (int) portp, rx, tx);
2279 #endif
2280 
2281         sreron = 0;
2282         sreroff = 0;
2283         if (tx == 0)
2284                 sreroff |= (SRER_TXDATA | SRER_TXEMPTY);
2285         else if (tx == 1)
2286                 sreron |= SRER_TXDATA;
2287         else if (tx >= 2)
2288                 sreron |= SRER_TXEMPTY;
2289         if (rx == 0)
2290                 sreroff |= SRER_RXDATA;
2291         else if (rx > 0)
2292                 sreron |= SRER_RXDATA;
2293 
2294         save_flags(flags);
2295         cli();
2296         BRDENABLE(portp->brdnr, portp->pagenr);
2297         stl_setreg(portp, CAR, (portp->portnr & 0x03));
2298         stl_setreg(portp, SRER, ((stl_getreg(portp, SRER) & ~sreroff) | sreron));
2299         BRDDISABLE(portp->brdnr);
2300         if (tx > 0)
2301                 set_bit(ASYI_TXBUSY, &portp->istate);
2302         restore_flags(flags);
2303 }
2304 
2305 /*****************************************************************************/
2306 
2307 /*
2308  *      Disable all interrupts from this port.
2309  */
2310 
2311 static void stl_disableintrs(stlport_t *portp)
     /* [previous][next][first][last][top][bottom][index][help] */
2312 {
2313         unsigned long   flags;
2314 
2315 #if DEBUG
2316         printk("stl_disableintrs(portp=%x)\n", (int) portp);
2317 #endif
2318         save_flags(flags);
2319         cli();
2320         BRDENABLE(portp->brdnr, portp->pagenr);
2321         stl_setreg(portp, CAR, (portp->portnr & 0x03));
2322         stl_setreg(portp, SRER, 0);
2323         BRDDISABLE(portp->brdnr);
2324         restore_flags(flags);
2325 }
2326 
2327 /*****************************************************************************/
2328 
2329 static void stl_sendbreak(stlport_t *portp, long len)
     /* [previous][next][first][last][top][bottom][index][help] */
2330 {
2331         unsigned long   flags;
2332 
2333 #if DEBUG
2334         printk("stl_sendbreak(portp=%x,len=%d)\n", (int) portp, (int) len);
2335 #endif
2336 
2337         save_flags(flags);
2338         cli();
2339         BRDENABLE(portp->brdnr, portp->pagenr);
2340         stl_setreg(portp, CAR, (portp->portnr & 0x03));
2341         stl_setreg(portp, COR2, (stl_getreg(portp, COR2) | COR2_ETC));
2342         stl_setreg(portp, SRER, ((stl_getreg(portp, SRER) & ~SRER_TXDATA) | SRER_TXEMPTY));
2343         BRDDISABLE(portp->brdnr);
2344         len = len / 5;
2345         portp->brklen = (len > 255) ? 255 : len;
2346         restore_flags(flags);
2347 }
2348 
2349 /*****************************************************************************/
2350 
2351 /*
2352  *      Map in interrupt vector to this driver. Check that we don't
2353  *      already have this vector mapped, we might be sharing this
2354  *      interrupt accross multiple boards.
2355  */
2356 
2357 static int stl_mapirq(int irq)
     /* [previous][next][first][last][top][bottom][index][help] */
2358 {
2359         int     rc, i;
2360 
2361 #if DEBUG
2362         printk("stl_mapirq(irq=%d)\n", irq);
2363 #endif
2364 
2365         rc = 0;
2366         for (i = 0; (i < stl_numintrs); i++) {
2367                 if (stl_gotintrs[i] == irq)
2368                         break;
2369         }
2370         if (i >= stl_numintrs) {
2371                 if (request_irq(irq, stl_intr, SA_INTERRUPT, stl_drvname) != 0) {
2372                         printk("STALLION: failed to register interrupt routine for irq=%d\n", irq);
2373                         rc = -ENODEV;
2374                 } else {
2375                         stl_gotintrs[stl_numintrs++] = irq;
2376                 }
2377         }
2378         return(rc);
2379 }
2380 
2381 /*****************************************************************************/
2382 
2383 /*
2384  *      Try and find and initialize all the ports on a panel. We don't care
2385  *      what sort of board these ports are on - since the port io registers
2386  *      are almost identical when dealing with ports.
2387  */
2388 
2389 static int stl_initports(stlbrd_t *brdp, stlpanel_t *panelp)
     /* [previous][next][first][last][top][bottom][index][help] */
2390 {
2391         stlport_t       *portp;
2392         unsigned int    chipmask;
2393         unsigned int    gfrcr;
2394         int             nrchips, uartaddr, ioaddr;
2395         int             i, j;
2396 
2397 #if DEBUG
2398         printk("stl_initports(panelp=%x)\n", (int) panelp);
2399 #endif
2400 
2401         BRDENABLE(panelp->brdnr, panelp->pagenr);
2402 
2403 /*
2404  *      Check that each chip is present and started up OK.
2405  */
2406         chipmask = 0;
2407         nrchips = panelp->nrports / CD1400_PORTS;
2408         for (i = 0; (i < nrchips); i++) {
2409                 if (brdp->brdtype == BRD_ECHPCI) {
2410                         outb((panelp->pagenr + (i >> 1)), brdp->ioctrl);
2411                         ioaddr = panelp->iobase;
2412                 } else {
2413                         ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 1));
2414                 }
2415                 uartaddr = (i & 0x01) ? 0x080 : 0;
2416                 outb((GFRCR + uartaddr), ioaddr);
2417                 outb(0, (ioaddr + EREG_DATA));
2418                 outb((CCR + uartaddr), ioaddr);
2419                 outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
2420                 outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
2421                 outb((GFRCR + uartaddr), ioaddr);
2422                 for (j = 0; (j < CCR_MAXWAIT); j++) {
2423                         if ((gfrcr = inb(ioaddr + EREG_DATA)) != 0)
2424                                 break;
2425                 }
2426                 if ((j >= CCR_MAXWAIT) || (gfrcr < 0x40) || (gfrcr > 0x60)) {
2427                         printk("STALLION: cd1400 not responding, brd=%d panel=%d chip=%d\n", panelp->brdnr, panelp->panelnr, i);
2428                         continue;
2429                 }
2430                 chipmask |= (0x1 << i);
2431                 outb((PPR + uartaddr), ioaddr);
2432                 outb(PPR_SCALAR, (ioaddr + EREG_DATA));
2433         }
2434 
2435 /*
2436  *      All cd1400's are initialized (if found!). Now go through and setup
2437  *      each ports data structures. Also init the LIVR register of cd1400
2438  *      for each port.
2439  */
2440         ioaddr = panelp->iobase;
2441         for (i = 0; (i < panelp->nrports); i++) {
2442                 if (brdp->brdtype == BRD_ECHPCI) {
2443                         outb((panelp->pagenr + (i >> 3)), brdp->ioctrl);
2444                         ioaddr = panelp->iobase;
2445                 } else {
2446                         ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 3));
2447                 }
2448                 if ((chipmask & (0x1 << (i / 4))) == 0)
2449                         continue;
2450                 portp = (stlport_t *) stl_memalloc(sizeof(stlport_t));
2451                 if (portp == (stlport_t *) NULL) {
2452                         printk("STALLION: failed to allocate memory (size=%d)\n", sizeof(stlport_t));
2453                         break;
2454                 }
2455                 memset(portp, 0, sizeof(stlport_t));
2456                 portp->portnr = i;
2457                 portp->brdnr = panelp->brdnr;
2458                 portp->panelnr = panelp->panelnr;
2459                 portp->ioaddr = ioaddr;
2460                 portp->uartaddr = (i & 0x04) << 5;
2461                 portp->pagenr = panelp->pagenr + (i >> 3);
2462                 portp->baud_base = STL_BAUDBASE;
2463                 portp->close_delay = STL_CLOSEDELAY;
2464                 portp->closing_wait = 30 * HZ;
2465                 portp->normaltermios = stl_deftermios;
2466                 portp->callouttermios = stl_deftermios;
2467                 portp->tqueue.routine = stl_offintr;
2468                 portp->tqueue.data = portp;
2469                 stl_setreg(portp, CAR, (i & 0x03));
2470                 stl_setreg(portp, LIVR, (i << 3));
2471                 panelp->ports[i] = portp;
2472         }
2473 
2474         BRDDISABLE(panelp->brdnr);
2475         return(0);
2476 }
2477 
2478 /*****************************************************************************/
2479 
2480 /*
2481  *      Try to find and initialize an EasyIO board.
2482  */
2483 
2484 static int stl_initeio(stlbrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
2485 {
2486         stlpanel_t      *panelp;
2487         unsigned int    status;
2488         int             rc;
2489 
2490 #if DEBUG
2491         printk("stl_initeio(brdp=%x)\n", (int) brdp);
2492 #endif
2493 
2494         brdp->ioctrl = brdp->ioaddr1 + 1;
2495         brdp->iostatus = brdp->ioaddr1 + 2;
2496 
2497         status = inb(brdp->iostatus);
2498         switch (status & EIO_IDBITMASK) {
2499         case EIO_8PORTRS:
2500         case EIO_8PORTM:
2501         case EIO_8PORTDI:
2502                 brdp->nrports = 8;
2503                 break;
2504         case EIO_4PORTRS:
2505                 brdp->nrports = 4;
2506                 break;
2507         default:
2508                 return(-ENODEV);
2509         }
2510 
2511         request_region(brdp->ioaddr1, 8, "serial(EIO)");
2512 
2513 /*
2514  *      Check that the supplied IRQ is good and then use it to setup the
2515  *      programmable interrupt bits on EIO board. Also set the edge/level
2516  *      triggered interrupt bit.
2517  */
2518         if ((brdp->irq < 0) || (brdp->irq > 15) ||
2519                         (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2520                 printk("STALLION: invalid irq=%d for brd=%d\n", brdp->irq, brdp->brdnr);
2521                 return(-EINVAL);
2522         }
2523         outb((stl_vecmap[brdp->irq] | ((brdp->irqtype) ? EIO_INTLEVEL : EIO_INTEDGE)), brdp->ioctrl);
2524 
2525         panelp = (stlpanel_t *) stl_memalloc(sizeof(stlpanel_t));
2526         if (panelp == (stlpanel_t *) NULL) {
2527                 printk("STALLION: failed to allocate memory (size=%d)\n", sizeof(stlpanel_t));
2528                 return(-ENOMEM);
2529         }
2530         memset(panelp, 0, sizeof(stlpanel_t));
2531 
2532         panelp->brdnr = brdp->brdnr;
2533         panelp->panelnr = 0;
2534         panelp->nrports = brdp->nrports;
2535         panelp->iobase = brdp->ioaddr1;
2536         brdp->panels[0] = panelp;
2537         brdp->nrpanels = 1;
2538         brdp->state |= BRD_FOUND;
2539         rc = stl_mapirq(brdp->irq);
2540         return(rc);
2541 }
2542 
2543 /*****************************************************************************/
2544 
2545 /*
2546  *      Try to find an ECH board and initialize it. This code is capable of
2547  *      dealing with all types of ECH board.
2548  */
2549 
2550 static int stl_initech(stlbrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
2551 {
2552         stlpanel_t      *panelp;
2553         unsigned int    status, nxtid;
2554         int             panelnr, ioaddr, i;
2555 
2556 #if DEBUG
2557         printk("stl_initech(brdp=%x)\n", (int) brdp);
2558 #endif
2559 
2560 /*
2561  *      Set up the initial board register contents for boards. This varys a
2562  *      bit between the different board types. So we need to handle each
2563  *      separately. Also do a check that the supplied IRQ is good.
2564  */
2565         if (brdp->brdtype == BRD_ECH) {
2566                 brdp->ioctrl = brdp->ioaddr1 + 1;
2567                 brdp->iostatus = brdp->ioaddr1 + 1;
2568                 status = inb(brdp->iostatus);
2569                 if ((status & ECH_IDBITMASK) != ECH_ID)
2570                         return(-ENODEV);
2571 
2572                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2573                                 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2574                         printk("STALLION: invalid irq=%d for brd=%d\n", brdp->irq, brdp->brdnr);
2575                         return(-EINVAL);
2576                 }
2577                 status = ((brdp->ioaddr2 & ECH_ADDR2MASK) >> 1);
2578                 status |= (stl_vecmap[brdp->irq] << 1);
2579                 outb((status | ECH_BRDRESET), brdp->ioaddr1);
2580                 brdp->ioctrlval = ECH_INTENABLE | ((brdp->irqtype) ? ECH_INTLEVEL : ECH_INTEDGE);
2581                 outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
2582                 outb(status, brdp->ioaddr1);
2583 
2584                 request_region(brdp->ioaddr1, 2, "serial(EC8/32)");
2585                 request_region(brdp->ioaddr2, 32, "serial(EC8/32-secondary)");
2586         } else if (brdp->brdtype == BRD_ECHMC) {
2587                 brdp->ioctrl = brdp->ioaddr1 + 0x20;
2588                 brdp->iostatus = brdp->ioctrl;
2589                 status = inb(brdp->iostatus);
2590                 if ((status & ECH_IDBITMASK) != ECH_ID)
2591                         return(-ENODEV);
2592 
2593                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2594                                 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2595                         printk("STALLION: invalid irq=%d for brd=%d\n", brdp->irq, brdp->brdnr);
2596                         return(-EINVAL);
2597                 }
2598                 outb(ECHMC_BRDRESET, brdp->ioctrl);
2599                 outb(ECHMC_INTENABLE, brdp->ioctrl);
2600 
2601                 request_region(brdp->ioaddr1, 64, "serial(EC8/32-MC)");
2602         } else if (brdp->brdtype == BRD_ECHPCI) {
2603                 brdp->ioctrl = brdp->ioaddr1 + 2;
2604                 request_region(brdp->ioaddr1, 4, "serial(EC8/32-PCI)");
2605                 request_region(brdp->ioaddr2, 8, "serial(EC8/32-PCI-secondary)");
2606         }
2607 
2608 /*
2609  *      Scan through the secondary io address space looking for panels.
2610  *      As we find'em allocate and initialize panel structures for each.
2611  */
2612         ioaddr = brdp->ioaddr2;
2613         panelnr = 0;
2614         nxtid = 0;
2615 
2616         for (i = 0; (i < STL_MAXPANELS); i++) {
2617                 if (brdp->brdtype == BRD_ECHPCI) {
2618                         outb(nxtid, brdp->ioctrl);
2619                         ioaddr = brdp->ioaddr2;
2620                 }
2621                 status = inb(ioaddr + ECH_PNLSTATUS);
2622                 if ((status & ECH_PNLIDMASK) != nxtid)
2623                         break;
2624                 panelp = (stlpanel_t *) stl_memalloc(sizeof(stlpanel_t));
2625                 if (panelp == (stlpanel_t *) NULL) {
2626                         printk("STALLION: failed to allocate memory (size=%d)\n", sizeof(stlpanel_t));
2627                         break;
2628                 }
2629                 memset(panelp, 0, sizeof(stlpanel_t));
2630                 panelp->brdnr = brdp->brdnr;
2631                 panelp->panelnr = panelnr;
2632                 panelp->iobase = ioaddr;
2633                 panelp->pagenr = nxtid;
2634                 if (status & ECH_PNL16PORT) {
2635                         if ((brdp->nrports + 16) > 32)
2636                                 break;
2637                         panelp->nrports = 16;
2638                         panelp->ackmask = 0x80;
2639                         brdp->nrports += 16;
2640                         ioaddr += (EREG_BANKSIZE * 2);
2641                         nxtid += 2;
2642                 } else {
2643                         panelp->nrports = 8;
2644                         panelp->ackmask = 0xc0;
2645                         brdp->nrports += 8;
2646                         ioaddr += EREG_BANKSIZE;
2647                         nxtid++;
2648                 }
2649                 brdp->panels[panelnr++] = panelp;
2650                 brdp->nrpanels++;
2651                 if (ioaddr >= (brdp->ioaddr2 + 0x20))
2652                         break;
2653         }
2654 
2655         if (brdp->brdtype == BRD_ECH)
2656                 outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
2657 
2658         brdp->state |= BRD_FOUND;
2659         i = stl_mapirq(brdp->irq);
2660         return(i);
2661 }
2662 
2663 /*****************************************************************************/
2664 
2665 /*
2666  *      Initialize and configure the specified board.
2667  *      Scan through all the boards in the configuration and see what we
2668  *      can find. Handle EIO and the ECH boards a little differently here
2669  *      since the initial search and setup is too different.
2670  */
2671 
2672 static int stl_brdinit(stlbrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
2673 {
2674         int     i;
2675 
2676 #if DEBUG
2677         printk("stl_brdinit(brdp=%x)\n", (int) brdp);
2678 #endif
2679 
2680         switch (brdp->brdtype) {
2681         case BRD_EASYIO:
2682                 stl_initeio(brdp);
2683                 break;
2684         case BRD_ECH:
2685         case BRD_ECHMC:
2686         case BRD_ECHPCI:
2687                 stl_initech(brdp);
2688                 break;
2689         default:
2690                 printk("STALLION: unit=%d is unknown board type=%d\n", brdp->brdnr, brdp->brdtype);
2691                 return(ENODEV);
2692         }
2693 
2694         stl_brds[brdp->brdnr] = brdp;
2695         if ((brdp->state & BRD_FOUND) == 0) {
2696                 printk("STALLION: %s board not found, unit=%d io=%x irq=%d\n", stl_brdnames[brdp->brdtype], brdp->brdnr, brdp->ioaddr1, brdp->irq);
2697                 return(ENODEV);
2698         }
2699 
2700         for (i = 0; (i < STL_MAXPANELS); i++)
2701                 if (brdp->panels[i] != (stlpanel_t *) NULL)
2702                         stl_initports(brdp, brdp->panels[i]);
2703 
2704         printk("STALLION: %s found, unit=%d io=%x irq=%d nrpanels=%d nrports=%d\n", stl_brdnames[brdp->brdtype], brdp->brdnr, brdp->ioaddr1, brdp->irq, brdp->nrpanels, brdp->nrports);
2705         return(0);
2706 }
2707 
2708 /*****************************************************************************/
2709 
2710 /*
2711  *      Find any ECH-PCI boards that might be installed. Initialize each
2712  *      one as it is found.
2713  */
2714 
2715 #ifdef  CONFIG_PCI
2716 
2717 static int stl_findpcibrds()
     /* [previous][next][first][last][top][bottom][index][help] */
2718 {
2719         stlbrd_t        *brdp;
2720         unsigned char   busnr, devnr, irq;
2721         unsigned short  class;
2722         unsigned int    ioaddr;
2723         int             i, rc;
2724 
2725 #if DEBUG
2726         printk("stl_findpcibrds()\n");
2727 #endif
2728 
2729         if (pcibios_present()) {
2730                 for (i = 0; (i < STL_MAXBRDS); i++) {
2731                         if (pcibios_find_device(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87410, i, &busnr, &devnr))
2732                                 break;
2733 
2734 /*
2735  *                      Found a device on the PCI bus that has our vendor and
2736  *                      device ID. Need to check now that it is really us.
2737  */
2738                         if ((rc = pcibios_read_config_word(busnr, devnr, PCI_CLASS_DEVICE, &class))) {
2739                                 printk("STALLION: failed to read class type from PCI board, errno=%x\n", rc);
2740                                 continue;
2741                         }
2742                         if (class == PCI_CLASS_STORAGE_IDE)
2743                                 continue;
2744 
2745                         if (stl_nrbrds >= STL_MAXBRDS) {
2746                                 printk("STALLION: too many boards found, maximum supported %d\n", STL_MAXBRDS);
2747                                 break;
2748                         }
2749 
2750 /*
2751  *                      We have a Stallion board. Allocate a board structure
2752  *                      and initialize it. Read its IO and IRQ resources
2753  *                      from conf space.
2754  */
2755                         brdp = (stlbrd_t *) stl_memalloc(sizeof(stlbrd_t));
2756                         if (brdp == (stlbrd_t *) NULL) {
2757                                 printk("STALLION: failed to allocate memory (size=%d)\n", sizeof(stlbrd_t));
2758                                 return(-ENOMEM);
2759                         }
2760                         memset(brdp, 0, sizeof(stlbrd_t));
2761                         brdp->brdnr = stl_nrbrds++;
2762                         brdp->brdtype = BRD_ECHPCI;
2763 
2764                         if ((rc = pcibios_read_config_dword(busnr, devnr, PCI_BASE_ADDRESS_0, &ioaddr))) {
2765                                 printk("STALLION: failed to read BAR register from PCI board, errno=%x\n", rc);
2766                                 continue;
2767                         }
2768                         brdp->ioaddr2 = (ioaddr & PCI_BASE_ADDRESS_IO_MASK);
2769 
2770                         if ((rc = pcibios_read_config_dword(busnr, devnr, PCI_BASE_ADDRESS_1, &ioaddr))) {
2771                                 printk("STALLION: failed to read BAR register from PCI board, errno=%x\n", rc);
2772                                 continue;
2773                         }
2774                         brdp->ioaddr1 = (ioaddr & PCI_BASE_ADDRESS_IO_MASK);
2775 #if DEBUG
2776                         printk("%s(%d): BAR0=%x BAR1=%x\n", __FILE__, __LINE__, brdp->ioaddr2, brdp->ioaddr1);
2777 #endif
2778 
2779                         if ((rc = pcibios_read_config_byte(busnr, devnr, PCI_INTERRUPT_LINE, &irq))) {
2780                                 printk("STALLION: failed to read BAR register from PCI board, errno=%x\n", rc);
2781                                 continue;
2782                         }
2783                         brdp->irq = irq;
2784 
2785 #if 0
2786                         ioaddr = 0x0c000001;
2787                         if ((rc = pcibios_write_config_dword(busnr, devnr, 0x40, ioaddr))) {
2788                                 printk("STALLION: failed to write register on PCI board, errno=%x\n", rc);
2789                                 continue;
2790                         }
2791                         if ((rc = pcibios_write_config_dword(busnr, devnr, 0x48, ioaddr))) {
2792                                 printk("STALLION: failed to write register on PCI board, errno=%x\n", rc);
2793                                 continue;
2794                         }
2795 #endif
2796 
2797                         stl_brdinit(brdp);
2798                 }
2799         }
2800 
2801         return(0);
2802 }
2803 
2804 #endif
2805 
2806 /*****************************************************************************/
2807 
2808 /*
2809  *      Scan through all the boards in the configuration and see what we
2810  *      can find. Handle EIO and the ECH boards a little differently here
2811  *      since the initial search and setup is too different.
2812  */
2813 
2814 static int stl_initbrds()
     /* [previous][next][first][last][top][bottom][index][help] */
2815 {
2816         stlbrd_t        *brdp;
2817         stlconf_t       *confp;
2818         int             i;
2819 
2820 #if DEBUG
2821         printk("stl_initbrds()\n");
2822 #endif
2823 
2824         if (stl_nrbrds > STL_MAXBRDS) {
2825                 printk("STALLION: too many boards in configuration table, truncating to %d\n", STL_MAXBRDS);
2826                 stl_nrbrds = STL_MAXBRDS;
2827         }
2828 
2829 /*
2830  *      Firstly scan the list of static boards configured. Allocate
2831  *      resources and initialize the boards as found.
2832  */
2833         for (i = 0; (i < stl_nrbrds); i++) {
2834                 confp = &stl_brdconf[i];
2835                 brdp = (stlbrd_t *) stl_memalloc(sizeof(stlbrd_t));
2836                 if (brdp == (stlbrd_t *) NULL) {
2837                         printk("STALLION: failed to allocate memory (size=%d)\n", sizeof(stlbrd_t));
2838                         return(-ENOMEM);
2839                 }
2840                 memset(brdp, 0, sizeof(stlbrd_t));
2841 
2842                 brdp->brdnr = i;
2843                 brdp->brdtype = confp->brdtype;
2844                 brdp->ioaddr1 = confp->ioaddr1;
2845                 brdp->ioaddr2 = confp->ioaddr2;
2846                 brdp->irq = confp->irq;
2847                 brdp->irqtype = confp->irqtype;
2848                 stl_brdinit(brdp);
2849         }
2850 
2851 #ifdef CONFIG_PCI
2852 /*
2853  *      If the PCI BIOS support is compiled in then lets go looking for
2854  *      ECH-PCI boards.
2855  */
2856         stl_findpcibrds();
2857 #endif
2858 
2859         return(0);
2860 }
2861 
2862 /*****************************************************************************/
2863 
2864 int stl_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2865 {
2866         printk("%s: version %s\n", stl_drvname, stl_drvversion);
2867 
2868         stl_initbrds();
2869 
2870 /*
2871  *      Allocate a temporary write buffer.
2872  */
2873         stl_tmpwritebuf = (char *) stl_memalloc(STL_TXBUFSIZE);
2874         if (stl_tmpwritebuf == (char *) NULL)
2875                 printk("STALLION: failed to allocate memory (size=%d)\n", STL_TXBUFSIZE);
2876 
2877 /*
2878  *      Set up the tty driver structure and register us as a driver.
2879  *      Also setup the callout tty device.
2880  */
2881         memset(&stl_serial, 0, sizeof(struct tty_driver));
2882         stl_serial.magic = TTY_DRIVER_MAGIC;
2883         stl_serial.name = stl_serialname;
2884         stl_serial.major = STL_SERIALMAJOR;
2885         stl_serial.minor_start = 0;
2886         stl_serial.num = STL_MAXBRDS * STL_MAXPORTS;
2887         stl_serial.type = TTY_DRIVER_TYPE_SERIAL;
2888         stl_serial.subtype = STL_DRVTYPSERIAL;
2889         stl_serial.init_termios = stl_deftermios;
2890         stl_serial.flags = TTY_DRIVER_REAL_RAW;
2891         stl_serial.refcount = &stl_refcount;
2892         stl_serial.table = stl_ttys;
2893         stl_serial.termios = stl_termios;
2894         stl_serial.termios_locked = stl_termioslocked;
2895         
2896         stl_serial.open = stl_open;
2897         stl_serial.close = stl_close;
2898         stl_serial.write = stl_write;
2899         stl_serial.put_char = stl_putchar;
2900         stl_serial.flush_chars = stl_flushchars;
2901         stl_serial.write_room = stl_writeroom;
2902         stl_serial.chars_in_buffer = stl_charsinbuffer;
2903         stl_serial.ioctl = stl_ioctl;
2904         stl_serial.set_termios = stl_settermios;
2905         stl_serial.throttle = stl_throttle;
2906         stl_serial.unthrottle = stl_unthrottle;
2907         stl_serial.stop = stl_stop;
2908         stl_serial.start = stl_start;
2909         stl_serial.hangup = stl_hangup;
2910         stl_serial.flush_buffer = stl_flushbuffer;
2911 
2912         stl_callout = stl_serial;
2913         stl_callout.name = stl_calloutname;
2914         stl_callout.major = STL_CALLOUTMAJOR;
2915         stl_callout.subtype = STL_DRVTYPCALLOUT;
2916 
2917         if (tty_register_driver(&stl_serial))
2918                 printk("STALLION: failed to register serial driver\n");
2919         if (tty_register_driver(&stl_callout))
2920                 printk("STALLION: failed to register callout driver\n");
2921 
2922         return 0;
2923 }
2924 
2925 /*****************************************************************************/

/* [previous][next][first][last][top][bottom][index][help] */