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

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