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

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