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

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