root/drivers/char/istallion.c

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

DEFINITIONS

This source file includes following definitions.
  1. init_module
  2. cleanup_module
  3. stli_memalloc
  4. stli_open
  5. stli_close
  6. stli_initopen
  7. stli_rawopen
  8. stli_rawclose
  9. stli_cmdwait
  10. stli_setport
  11. stli_delay
  12. stli_waitcarrier
  13. stli_write
  14. stli_putchar
  15. stli_flushchars
  16. stli_writeroom
  17. stli_charsinbuffer
  18. stli_getserial
  19. stli_setserial
  20. stli_ioctl
  21. stli_settermios
  22. stli_throttle
  23. stli_unthrottle
  24. stli_stop
  25. stli_start
  26. stli_dohangup
  27. stli_hangup
  28. stli_flushbuffer
  29. stli_sendcmd
  30. stli_read
  31. stli_dodelaycmd
  32. stli_hostcmd
  33. stli_poll
  34. stli_mkasyport
  35. stli_mkasysigs
  36. stli_mktiocm
  37. stli_initports
  38. stli_ecpinit
  39. stli_ecpenable
  40. stli_ecpdisable
  41. stli_ecpgetmemptr
  42. stli_ecpreset
  43. stli_ecpintr
  44. stli_ecpeiinit
  45. stli_ecpeienable
  46. stli_ecpeidisable
  47. stli_ecpeigetmemptr
  48. stli_ecpeireset
  49. stli_ecpmcenable
  50. stli_ecpmcdisable
  51. stli_ecpmcgetmemptr
  52. stli_ecpmcreset
  53. stli_onbinit
  54. stli_onbenable
  55. stli_onbdisable
  56. stli_onbgetmemptr
  57. stli_onbreset
  58. stli_onbeinit
  59. stli_onbeenable
  60. stli_onbedisable
  61. stli_onbegetmemptr
  62. stli_onbereset
  63. stli_bbyinit
  64. stli_bbygetmemptr
  65. stli_bbyreset
  66. stli_stalinit
  67. stli_stalgetmemptr
  68. stli_stalreset
  69. stli_mapbrdmem
  70. stli_initecp
  71. stli_initonb
  72. stli_startbrd
  73. stli_brdinit
  74. stli_eisamemprobe
  75. stli_findeisabrds
  76. stli_initbrds
  77. stli_memread
  78. stli_memwrite
  79. stli_memioctl
  80. stli_init

   1 /*****************************************************************************/
   2 
   3 /*
   4  *      istallion.c  -- stallion intelligent 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/timer.h>
  32 #include <linux/wait.h>
  33 #include <linux/interrupt.h>
  34 #include <linux/termios.h>
  35 #include <linux/fcntl.h>
  36 #include <linux/tty_driver.h>
  37 #include <linux/tty.h>
  38 #include <linux/tty_flip.h>
  39 #include <linux/serial.h>
  40 #include <linux/cdk.h>
  41 #include <linux/string.h>
  42 #include <linux/malloc.h>
  43 #include <linux/ioport.h>
  44 #include <linux/delay.h>
  45 #include <asm/io.h>
  46 
  47 /*****************************************************************************/
  48 
  49 /*
  50  *      Define different board types. Not all of the following board types
  51  *      are supported by this driver. But I will use the standard "assigned"
  52  *      board numbers. Currently supported boards are abbreviated as:
  53  *      ECP = EasyConnection 8/64, ONB = ONboard, BBY = Brumby and
  54  *      STAL = Stallion.
  55  */
  56 #define BRD_UNKNOWN     0
  57 #define BRD_STALLION    1
  58 #define BRD_BRUMBY4     2
  59 #define BRD_ONBOARD2    3
  60 #define BRD_ONBOARD     4
  61 #define BRD_BRUMBY8     5
  62 #define BRD_BRUMBY16    6
  63 #define BRD_ONBOARDE    7
  64 #define BRD_ONBOARD32   9
  65 #define BRD_ONBOARD2_32 10
  66 #define BRD_ONBOARDRS   11
  67 #define BRD_EASYIO      20
  68 #define BRD_ECH         21
  69 #define BRD_ECHMC       22
  70 #define BRD_ECP         23
  71 #define BRD_ECPE        24
  72 #define BRD_ECPMC       25
  73 #define BRD_ECHPCI      26
  74 
  75 #define BRD_BRUMBY      BRD_BRUMBY4
  76 
  77 /*
  78  *      Define a configuration structure to hold the board configuration.
  79  *      Need to set this up in the code (for now) with the boards that are
  80  *      to be configured into the system. This is what needs to be modified
  81  *      when adding/removing/modifying boards. Each line entry in the
  82  *      stli_brdconf[] array is a board. Each line contains io/irq/memory
  83  *      ranges for that board (as well as what type of board it is).
  84  *      Some examples:
  85  *              { BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },
  86  *      This line will configure an EasyConnection 8/64 at io address 2a0,
  87  *      and shared memory address of cc000. Multiple EasyConnection 8/64
  88  *      boards can share the same shared memory address space. No interrupt
  89  *      is required for this board type.
  90  *      Another example:
  91  *              { BRD_ONBOARD, 0x240, 0, 0xd0000, 0, 0 },
  92  *      This line will configure an ONboard (ISA type) at io address 240,
  93  *      and shared memory address of d0000. Multiple ONboards can share
  94  *      the same shared memory address space. No interrupt required.
  95  *      Another example:
  96  *              { BRD_BRUMBY4, 0x360, 0, 0xc8000, 0, 0 },
  97  *      This line will configure a Brumby board (any number of ports!) at
  98  *      io address 360 and shared memory address of c8000. All Brumby boards
  99  *      configured into a system must have their own separate io and memory
 100  *      addresses. No interrupt is required.
 101  *      Another example:
 102  *              { BRD_STALLION, 0x330, 0, 0xd0000, 0, 0 },
 103  *      This line will configure an original Stallion board at io address 330
 104  *      and shared memory address d0000 (this would only be valid for a "V4.0"
 105  *      or Rev.O Stallion board). All Stallion boards configured into the
 106  *      system must have their own separate io and memory addresses. No
 107  *      interrupt is required.
 108  */
 109 
 110 typedef struct {
 111         int             brdtype;
 112         int             ioaddr1;
 113         int             ioaddr2;
 114         unsigned long   memaddr;
 115         int             irq;
 116         int             irqtype;
 117 } stlconf_t;
 118 
 119 static stlconf_t        stli_brdconf[] = {
 120         { BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },
 121 };
 122 
 123 static int      stli_nrbrds = sizeof(stli_brdconf) / sizeof(stlconf_t);
 124 
 125 /*
 126  *      Code support is offered for boards to use the above 1Mb memory
 127  *      ranges for those boards which support this (supported on the ONboard
 128  *      and ECP-EI hardware). The following switch should be enabled. The only
 129  *      catch is that the kernel functions required to do this are not
 130  *      normally exported symbols, so you will have to do some extra work
 131  *      for this to be used in the loadable module form of the driver.
 132  *      Unfortunately this doesn't work either if you linke the driver into
 133  *      the kernel, sincethe memory management code is not set up early
 134  *      enough (before our initialization routine is run).
 135  */
 136 #define STLI_HIMEMORY   0
 137 
 138 #if STLI_HIMEMORY
 139 #include <asm/page.h>
 140 #include <asm/pgtable.h>
 141 #endif
 142 
 143 /*
 144  *      There is some experimental EISA board detection code in this driver.
 145  *      By default it is disabled, but for those that want to try it out,
 146  *      then set the define below to be 1.
 147  */
 148 #define STLI_EISAPROBE  0
 149 
 150 /*****************************************************************************/
 151 
 152 /*
 153  *      Define some important driver characteristics. Device major numbers
 154  *      allocated as per Linux Device Registery.
 155  */
 156 #ifndef STL_SIOMEMMAJOR
 157 #define STL_SIOMEMMAJOR         28
 158 #endif
 159 #ifndef STL_SERIALMAJOR
 160 #define STL_SERIALMAJOR         24
 161 #endif
 162 #ifndef STL_CALLOUTMAJOR
 163 #define STL_CALLOUTMAJOR        25
 164 #endif
 165 
 166 #define STL_DRVTYPSERIAL        1
 167 #define STL_DRVTYPCALLOUT       2
 168 
 169 #define STL_MAXBRDS             4
 170 #define STL_MAXPANELS           4
 171 #define STL_MAXPORTS            64
 172 #define STL_MAXCHANS            (STL_MAXPORTS + 1)
 173 #define STL_MAXDEVS             (STL_MAXBRDS * STL_MAXPORTS)
 174 
 175 /*****************************************************************************/
 176 
 177 /*
 178  *      Define our local driver identity first. Set up stuff to deal with
 179  *      all the local structures required by a serial tty driver.
 180  */
 181 static char     *stli_drvname = "Stallion Intelligent Multiport Serial Driver";
 182 static char     *stli_drvversion = "1.0.2";
 183 static char     *stli_serialname = "ttyE";
 184 static char     *stli_calloutname = "cue";
 185 
 186 static struct tty_driver        stli_serial;
 187 static struct tty_driver        stli_callout;
 188 static struct tty_struct        *stli_ttys[STL_MAXDEVS];
 189 static struct termios           *stli_termios[STL_MAXDEVS];
 190 static struct termios           *stli_termioslocked[STL_MAXDEVS];
 191 static int                      stli_refcount;
 192 
 193 /*
 194  *      We will need to allocate a temporary write buffer for chars that
 195  *      come direct from user space. The problem is that a copy from user
 196  *      space might cause a page fault (typically on a system that is
 197  *      swapping!). All ports will share one buffer - since if the system
 198  *      is already swapping a shared buffer won't make things any worse.
 199  */
 200 static char                     *stli_tmpwritebuf = (char *) NULL;
 201 static struct semaphore         stli_tmpwritesem = MUTEX;
 202 
 203 #define STLI_TXBUFSIZE          4096
 204 
 205 /*
 206  *      Use a fast local buffer for cooked characters. Typically a whole
 207  *      bunch of cooked characters come in for a port, 1 at a time. So we
 208  *      save those up into a local buffer, then write out the whole lot
 209  *      with a large memcpy. Just use 1 buffer for all ports, since its
 210  *      use it is only need for short periods of time by each port.
 211  */
 212 static char                     *stli_txcookbuf = (char *) NULL;
 213 static int                      stli_txcooksize = 0;
 214 static int                      stli_txcookrealsize = 0;
 215 static struct tty_struct        *stli_txcooktty = (struct tty_struct *) NULL;
 216 
 217 /*
 218  *      Define a local default termios struct. All ports will be created
 219  *      with this termios initially. Basically all it defines is a raw port
 220  *      at 9600 baud, 8 data bits, no parity, 1 stop bit.
 221  */
 222 static struct termios           stli_deftermios = {
 223         0,
 224         0,
 225         (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
 226         0,
 227         0,
 228         INIT_C_CC
 229 };
 230 
 231 /*****************************************************************************/
 232 
 233 /*
 234  *      Define a set of structures to hold all the board/panel/port info
 235  *      for our ports. These will be dynamically allocated as required at
 236  *      driver initialization time.
 237  */
 238 
 239 /*
 240  *      Port and board structures to hold status info about each object.
 241  *      The board structure contains pointers to structures for each port
 242  *      connected to it. Panels are not distinguished here, since
 243  *      communication with the slave board will always be on a per port
 244  *      basis.
 245  */
 246 typedef struct {
 247         int                     portnr;
 248         int                     panelnr;
 249         int                     brdnr;
 250         unsigned long           state;
 251         int                     devnr;
 252         int                     flags;
 253         int                     baud_base;
 254         int                     custom_divisor;
 255         int                     close_delay;
 256         int                     closing_wait;
 257         int                     refcount;
 258         int                     openwaitcnt;
 259         int                     rc;
 260         int                     argsize;
 261         void                    *argp;
 262         long                    session;
 263         long                    pgrp;
 264         unsigned int            rxmarkmsk;
 265         struct tty_struct       *tty;
 266         struct wait_queue       *open_wait;
 267         struct wait_queue       *close_wait;
 268         struct wait_queue       *raw_wait;
 269         struct tq_struct        tqhangup;
 270         struct termios          normaltermios;
 271         struct termios          callouttermios;
 272         asysigs_t               asig;
 273         unsigned long           addr;
 274         unsigned long           rxoffset;
 275         unsigned long           txoffset;
 276         unsigned long           sigs;
 277         unsigned long           pflag;
 278         unsigned int            rxsize;
 279         unsigned int            txsize;
 280         unsigned char           reqbit;
 281         unsigned char           portidx;
 282         unsigned char           portbit;
 283 } stliport_t;
 284 
 285 /*
 286  *      Use a structure of function pointers to do board level operations.
 287  *      These include, enable/disable, paging shared memory, interrupting, etc.
 288  */
 289 typedef struct stlbrd {
 290         int             brdnr;
 291         int             brdtype;
 292         int             state;
 293         int             nrpanels;
 294         int             nrports;
 295         int             nrdevs;
 296         unsigned int    iobase;
 297         unsigned long   memaddr;
 298         void            *membase;
 299         int             memsize;
 300         int             pagesize;
 301         int             hostoffset;
 302         int             slaveoffset;
 303         int             bitsize;
 304         int             panels[STL_MAXPANELS];
 305         void            (*init)(struct stlbrd *brdp);
 306         void            (*enable)(struct stlbrd *brdp);
 307         void            (*reenable)(struct stlbrd *brdp);
 308         void            (*disable)(struct stlbrd *brdp);
 309         char            *(*getmemptr)(struct stlbrd *brdp, unsigned long offset, int line);
 310         void            (*intr)(struct stlbrd *brdp);
 311         void            (*reset)(struct stlbrd *brdp);
 312         stliport_t      *ports[STL_MAXPORTS];
 313 } stlibrd_t;
 314 
 315 static stlibrd_t        *stli_brds[STL_MAXBRDS];
 316 
 317 static int              stli_shared = 0;
 318 
 319 /*
 320  *      Per board state flags. Used with the state field of the board struct.
 321  *      Not really much here... All we need to do is keep track of whether
 322  *      the board has been detected, and whether it is actully running a slave
 323  *      or not.
 324  */
 325 #define BST_FOUND       0x1
 326 #define BST_STARTED     0x2
 327 
 328 /*
 329  *      Define the set of port state flags. These are marked for internal
 330  *      state purposes only, usually to do with the state of communications
 331  *      with the slave. Most of them need to be updated atomically, so always
 332  *      use the bit setting operations (unless protected by cli/sti).
 333  */
 334 #define ST_INITIALIZING 1
 335 #define ST_OPENING      2
 336 #define ST_CLOSING      3
 337 #define ST_CMDING       4
 338 #define ST_TXBUSY       5
 339 #define ST_RXING        6
 340 #define ST_DOFLUSHRX    7
 341 #define ST_DOFLUSHTX    8
 342 #define ST_DOSIGS       9
 343 #define ST_RXSTOP       10
 344 #define ST_GETSIGS      11
 345 
 346 /*
 347  *      Define an array of board names as printable strings. Handy for
 348  *      referencing boards when printing trace and stuff.
 349  */
 350 static char     *stli_brdnames[] = {
 351         "Unknown",
 352         "Stallion",
 353         "Brumby",
 354         "ONboard-MC",
 355         "ONboard",
 356         "Brumby",
 357         "Brumby",
 358         "ONboard-EI",
 359         (char *) NULL,
 360         "ONboard",
 361         "ONboard-MC",
 362         "ONboard-MC",
 363         (char *) NULL,
 364         (char *) NULL,
 365         (char *) NULL,
 366         (char *) NULL,
 367         (char *) NULL,
 368         (char *) NULL,
 369         (char *) NULL,
 370         (char *) NULL,
 371         "EasyIO",
 372         "EC8/32-AT",
 373         "EC8/32-MC",
 374         "EC8/64-AT",
 375         "EC8/64-EI",
 376         "EC8/64-MC",
 377         "EC8/32-PCI",
 378 };
 379 
 380 /*
 381  *      Set up a default memory address table for EISA board probing.
 382  *      The default addresses are all bellow 1Mbyte, which has to be the
 383  *      case anyway. They should be safe, since we only read values from
 384  *      them, and interrupts are disabled while we do it. If the higher
 385  *      memory support is compiled in then we also try probing around
 386  *      the 1Gb, 2Gb and 3Gb areas as well...
 387  */
 388 static unsigned long    stli_eisamemprobeaddrs[] = {
 389         0xc0000, 0xd0000, 0xe0000, 0xf0000,
 390         0x80000000, 0x80010000, 0x80020000, 0x80030000,
 391         0x40000000, 0x40010000, 0x40020000, 0x40030000,
 392         0xc0000000, 0xc0010000, 0xc0020000, 0xc0030000,
 393         0xff000000, 0xff010000, 0xff020000, 0xff030000,
 394 };
 395 
 396 #if STLI_HIMEMORY
 397 static int      stli_eisamempsize = sizeof(stli_eisamemprobeaddrs) / sizeof(unsigned long);
 398 #else
 399 static int      stli_eisamempsize = 4;
 400 #endif
 401 
 402 int             stli_eisaprobe = STLI_EISAPROBE;
 403 
 404 /*****************************************************************************/
 405 
 406 /*
 407  *      Hardware configuration info for ECP boards. These defines apply
 408  *      to the directly accessable io ports of the ECP. There is a set of
 409  *      defines for each ECP board type, ISA, EISA and MCA.
 410  */
 411 #define ECP_IOSIZE      4
 412 #define ECP_MEMSIZE     (128 * 1024)
 413 #define ECP_ATPAGESIZE  (4 * 1024)
 414 #define ECP_EIPAGESIZE  (64 * 1024)
 415 #define ECP_MCPAGESIZE  (4 * 1024)
 416 
 417 #define STL_EISAID      0x8c4e
 418 
 419 /*
 420  *      Important defines for the ISA class of ECP board.
 421  */
 422 #define ECP_ATIREG      0
 423 #define ECP_ATCONFR     1
 424 #define ECP_ATMEMAR     2
 425 #define ECP_ATMEMPR     3
 426 #define ECP_ATSTOP      0x1
 427 #define ECP_ATINTENAB   0x10
 428 #define ECP_ATENABLE    0x20
 429 #define ECP_ATDISABLE   0x00
 430 #define ECP_ATADDRMASK  0x3f000
 431 #define ECP_ATADDRSHFT  12
 432 
 433 /*
 434  *      Important defines for the EISA class of ECP board.
 435  */
 436 #define ECP_EIIREG      0
 437 #define ECP_EIMEMARL    1
 438 #define ECP_EICONFR     2
 439 #define ECP_EIMEMARH    3
 440 #define ECP_EIENABLE    0x1
 441 #define ECP_EIDISABLE   0x0
 442 #define ECP_EISTOP      0x4
 443 #define ECP_EIEDGE      0x00
 444 #define ECP_EILEVEL     0x80
 445 #define ECP_EIADDRMASKL 0x00ff0000
 446 #define ECP_EIADDRSHFTL 16
 447 #define ECP_EIADDRMASKH 0xff000000
 448 #define ECP_EIADDRSHFTH 24
 449 #define ECP_EIBRDENAB   0xc84
 450 
 451 #define ECP_EISAID      0x4
 452 
 453 /*
 454  *      Important defines for the Micro-channel class of ECP board.
 455  *      (It has a lot in common with the ISA boards.)
 456  */
 457 #define ECP_MCIREG      0
 458 #define ECP_MCCONFR     1
 459 #define ECP_MCSTOP      0x20
 460 #define ECP_MCENABLE    0x80
 461 #define ECP_MCDISABLE   0x00
 462 
 463 /*
 464  *      Hardware configuration info for ONboard and Brumby boards. These
 465  *      defines apply to the directly accessable io ports of these boards.
 466  */
 467 #define ONB_IOSIZE      16
 468 #define ONB_MEMSIZE     (64 * 1024)
 469 #define ONB_ATPAGESIZE  (64 * 1024)
 470 #define ONB_MCPAGESIZE  (64 * 1024)
 471 #define ONB_EIMEMSIZE   (128 * 1024)
 472 #define ONB_EIPAGESIZE  (64 * 1024)
 473 
 474 /*
 475  *      Important defines for the ISA class of ONboard board.
 476  */
 477 #define ONB_ATIREG      0
 478 #define ONB_ATMEMAR     1
 479 #define ONB_ATCONFR     2
 480 #define ONB_ATSTOP      0x4
 481 #define ONB_ATENABLE    0x01
 482 #define ONB_ATDISABLE   0x00
 483 #define ONB_ATADDRMASK  0xff0000
 484 #define ONB_ATADDRSHFT  16
 485 
 486 #if STLI_HIMEMORY
 487 #define ONB_HIMEMENAB   0x02
 488 #else
 489 #define ONB_HIMEMENAB   0
 490 #endif
 491 
 492 /*
 493  *      Important defines for the EISA class of ONboard board.
 494  */
 495 #define ONB_EIIREG      0
 496 #define ONB_EIMEMARL    1
 497 #define ONB_EICONFR     2
 498 #define ONB_EIMEMARH    3
 499 #define ONB_EIENABLE    0x1
 500 #define ONB_EIDISABLE   0x0
 501 #define ONB_EISTOP      0x4
 502 #define ONB_EIEDGE      0x00
 503 #define ONB_EILEVEL     0x80
 504 #define ONB_EIADDRMASKL 0x00ff0000
 505 #define ONB_EIADDRSHFTL 16
 506 #define ONB_EIADDRMASKH 0xff000000
 507 #define ONB_EIADDRSHFTH 24
 508 #define ONB_EIBRDENAB   0xc84
 509 
 510 #define ONB_EISAID      0x1
 511 
 512 /*
 513  *      Important defines for the Brumby boards. They are pretty simple,
 514  *      there is not much that is programmably configurable.
 515  */
 516 #define BBY_IOSIZE      16
 517 #define BBY_MEMSIZE     (64 * 1024)
 518 #define BBY_PAGESIZE    (16 * 1024)
 519 
 520 #define BBY_ATIREG      0
 521 #define BBY_ATCONFR     1
 522 #define BBY_ATSTOP      0x4
 523 
 524 /*
 525  *      Important defines for the Stallion boards. They are pretty simple,
 526  *      there is not much that is programmably configurable.
 527  */
 528 #define STAL_IOSIZE     16
 529 #define STAL_MEMSIZE    (64 * 1024)
 530 #define STAL_PAGESIZE   (64 * 1024)
 531 
 532 /*
 533  *      Define the set of status register values for EasyConnection panels.
 534  *      The signature will return with the status value for each panel. From
 535  *      this we can determine what is attached to the board - before we have
 536  *      actually down loaded any code to it.
 537  */
 538 #define ECH_PNLSTATUS   2
 539 #define ECH_PNL16PORT   0x20
 540 #define ECH_PNLIDMASK   0x07
 541 #define ECH_PNLINTRPEND 0x80
 542 
 543 /*
 544  *      Define some macros to do things to the board. Even those these boards
 545  *      are somewhat related there is often significantly different ways of
 546  *      doing some operation on it (like enable, paging, reset, etc). So each
 547  *      board class has a set of functions which do the commonly required
 548  *      operations. The macros below basically just call these functions,
 549  *      generally checking for a NULL function - which means that the board
 550  *      needs nothing done to it to achieve this operation!
 551  */
 552 #define EBRDINIT(brdp)                                          \
 553         if (brdp->init != NULL)                                 \
 554                 (* brdp->init)(brdp)
 555 
 556 #define EBRDENABLE(brdp)                                        \
 557         if (brdp->enable != NULL)                               \
 558                 (* brdp->enable)(brdp);
 559 
 560 #define EBRDDISABLE(brdp)                                       \
 561         if (brdp->disable != NULL)                              \
 562                 (* brdp->disable)(brdp);
 563 
 564 #define EBRDINTR(brdp)                                          \
 565         if (brdp->intr != NULL)                                 \
 566                 (* brdp->intr)(brdp);
 567 
 568 #define EBRDRESET(brdp)                                         \
 569         if (brdp->reset != NULL)                                \
 570                 (* brdp->reset)(brdp);
 571 
 572 #define EBRDGETMEMPTR(brdp,offset)                              \
 573         (* brdp->getmemptr)(brdp, offset, __LINE__)
 574 
 575 /*
 576  *      Define the maximal baud rate, and the default baud base for ports.
 577  */
 578 #define STL_MAXBAUD     230400
 579 #define STL_BAUDBASE    115200
 580 #define STL_CLOSEDELAY  50
 581 
 582 /*****************************************************************************/
 583 
 584 /*
 585  *      Define macros to extract a brd or port number from a minor number.
 586  */
 587 #define MKDEV2BRD(min)          (((min) & 0xc0) >> 6)
 588 #define MKDEV2PORT(min)         ((min) & 0x3f)
 589 
 590 /*
 591  *      Define a baud rate table that converts termios baud rate selector
 592  *      into the actual baud rate value. All baud rate calculations are based
 593  *      on the actual baud rate required.
 594  */
 595 static unsigned int     stli_baudrates[] = {
 596         0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
 597         9600, 19200, 38400, 57600, 115200, 230400
 598 };
 599 
 600 /*****************************************************************************/
 601 
 602 /*
 603  *      Define some handy local macros...
 604  */
 605 #define MIN(a,b)                (((a) <= (b)) ? (a) : (b))
 606 
 607 /*****************************************************************************/
 608 
 609 /*
 610  *      Prototype all functions in this driver!
 611  */
 612 
 613 #ifdef MODULE
 614 int             init_module(void);
 615 void            cleanup_module(void);
 616 #endif
 617 
 618 int             stli_init(void);
 619 static int      stli_open(struct tty_struct *tty, struct file *filp);
 620 static void     stli_close(struct tty_struct *tty, struct file *filp);
 621 static int      stli_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
 622 static void     stli_putchar(struct tty_struct *tty, unsigned char ch);
 623 static void     stli_flushchars(struct tty_struct *tty);
 624 static int      stli_writeroom(struct tty_struct *tty);
 625 static int      stli_charsinbuffer(struct tty_struct *tty);
 626 static int      stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
 627 static void     stli_settermios(struct tty_struct *tty, struct termios *old);
 628 static void     stli_throttle(struct tty_struct *tty);
 629 static void     stli_unthrottle(struct tty_struct *tty);
 630 static void     stli_stop(struct tty_struct *tty);
 631 static void     stli_start(struct tty_struct *tty);
 632 static void     stli_flushbuffer(struct tty_struct *tty);
 633 static void     stli_hangup(struct tty_struct *tty);
 634 
 635 static int      stli_initbrds(void);
 636 static int      stli_brdinit(stlibrd_t *brdp);
 637 static int      stli_initecp(stlibrd_t *brdp);
 638 static int      stli_initonb(stlibrd_t *brdp);
 639 static int      stli_eisamemprobe(stlibrd_t *brdp);
 640 static int      stli_findeisabrds(void);
 641 static int      stli_initports(stlibrd_t *brdp);
 642 static int      stli_startbrd(stlibrd_t *brdp);
 643 static int      stli_memread(struct inode *ip, struct file *fp, char *buf, int count);
 644 static int      stli_memwrite(struct inode *ip, struct file *fp, const char *buf, int count);
 645 static int      stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
 646 static void     stli_poll(unsigned long arg);
 647 static int      stli_hostcmd(stlibrd_t *brdp, int channr);
 648 static int      stli_initopen(stlibrd_t *brdp, stliport_t *portp);
 649 static int      stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
 650 static int      stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
 651 static int      stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp);
 652 static void     stli_dohangup(void *arg);
 653 static void     stli_delay(int len);
 654 static int      stli_setport(stliport_t *portp);
 655 static int      stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
 656 static void     stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
 657 static void     stli_dodelaycmd(stliport_t *portp, volatile cdkctrl_t *cp);
 658 static void     stli_mkasyport(stliport_t *portp, asyport_t *pp, struct termios *tiosp);
 659 static void     stli_mkasysigs(asysigs_t *sp, int dtr, int rts);
 660 static long     stli_mktiocm(unsigned long sigvalue);
 661 static void     stli_read(stlibrd_t *brdp, stliport_t *portp);
 662 static void     stli_getserial(stliport_t *portp, struct serial_struct *sp);
 663 static int      stli_setserial(stliport_t *portp, struct serial_struct *sp);
 664 static void     *stli_memalloc(int len);
 665 
 666 static void     stli_ecpinit(stlibrd_t *brdp);
 667 static void     stli_ecpenable(stlibrd_t *brdp);
 668 static void     stli_ecpdisable(stlibrd_t *brdp);
 669 static char     *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
 670 static void     stli_ecpreset(stlibrd_t *brdp);
 671 static void     stli_ecpintr(stlibrd_t *brdp);
 672 static void     stli_ecpeiinit(stlibrd_t *brdp);
 673 static void     stli_ecpeienable(stlibrd_t *brdp);
 674 static void     stli_ecpeidisable(stlibrd_t *brdp);
 675 static char     *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
 676 static void     stli_ecpeireset(stlibrd_t *brdp);
 677 static void     stli_ecpmcenable(stlibrd_t *brdp);
 678 static void     stli_ecpmcdisable(stlibrd_t *brdp);
 679 static char     *stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
 680 static void     stli_ecpmcreset(stlibrd_t *brdp);
 681 
 682 static void     stli_onbinit(stlibrd_t *brdp);
 683 static void     stli_onbenable(stlibrd_t *brdp);
 684 static void     stli_onbdisable(stlibrd_t *brdp);
 685 static char     *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
 686 static void     stli_onbreset(stlibrd_t *brdp);
 687 static void     stli_onbeinit(stlibrd_t *brdp);
 688 static void     stli_onbeenable(stlibrd_t *brdp);
 689 static void     stli_onbedisable(stlibrd_t *brdp);
 690 static char     *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
 691 static void     stli_onbereset(stlibrd_t *brdp);
 692 static void     stli_bbyinit(stlibrd_t *brdp);
 693 static char     *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
 694 static void     stli_bbyreset(stlibrd_t *brdp);
 695 static void     stli_stalinit(stlibrd_t *brdp);
 696 static char     *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
 697 static void     stli_stalreset(stlibrd_t *brdp);
 698 
 699 #if STLI_HIMEMORY
 700 static void *stli_mapbrdmem(unsigned long physaddr, unsigned int size);
 701 #endif
 702 
 703 /*****************************************************************************/
 704 
 705 /*
 706  *      Define the driver info for a user level shared memory device. This
 707  *      device will work sort of like the /dev/kmem device - except that it
 708  *      will give access to the shared memory on the Stallion intelligent
 709  *      board. This is also a very useful debugging tool.
 710  */
 711 static struct file_operations   stli_fsiomem = {
 712         NULL,
 713         stli_memread,
 714         stli_memwrite,
 715         NULL,
 716         NULL,
 717         stli_memioctl,
 718         NULL,
 719         NULL,
 720         NULL,
 721         NULL
 722 };
 723 
 724 /*****************************************************************************/
 725 
 726 /*
 727  *      Define a timer_list entry for our poll routine. The slave board
 728  *      is polled every so often to see if anything needs doing. This is
 729  *      much cheaper on host cpu than using interrupts. It turns out to
 730  *      not increase character latency by much either...
 731  */
 732 static struct timer_list        stli_timerlist = {
 733         NULL, NULL, 0, 0, stli_poll
 734 };
 735 
 736 static int      stli_timeron = 0;
 737 
 738 /*
 739  *      Define the calculation for the timeout routine.
 740  */
 741 #define STLI_TIMEOUT    (jiffies + 1)
 742 
 743 /*****************************************************************************/
 744 
 745 #ifdef MODULE
 746 
 747 /*
 748  *      Loadable module initialization stuff.
 749  */
 750 
 751 int init_module()
     /* [previous][next][first][last][top][bottom][index][help] */
 752 {
 753         unsigned long   flags;
 754 
 755 #if DEBUG
 756         printk("init_module()\n");
 757 #endif
 758 
 759         save_flags(flags);
 760         cli();
 761         stli_init();
 762         restore_flags(flags);
 763 
 764         return(0);
 765 }
 766 
 767 /*****************************************************************************/
 768 
 769 void cleanup_module()
     /* [previous][next][first][last][top][bottom][index][help] */
 770 {
 771         stlibrd_t       *brdp;
 772         stliport_t      *portp;
 773         unsigned long   flags;
 774         int             i, j;
 775 
 776 #if DEBUG
 777         printk("cleanup_module()\n");
 778 #endif
 779 
 780         printk("Unloading %s: version %s\n", stli_drvname, stli_drvversion);
 781 
 782         save_flags(flags);
 783         cli();
 784 
 785 /*
 786  *      Free up all allocated resources used by the ports. This includes
 787  *      memory and interrupts.
 788  */
 789         if (stli_timeron) {
 790                 stli_timeron = 0;
 791                 del_timer(&stli_timerlist);
 792         }
 793 
 794         i = tty_unregister_driver(&stli_serial);
 795         j = tty_unregister_driver(&stli_callout);
 796         if (i || j) {
 797                 printk("STALLION: failed to un-register tty driver, errno=%d,%d\n", -i, -j);
 798                 restore_flags(flags);
 799                 return;
 800         }
 801         if ((i = unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
 802                 printk("STALLION: failed to un-register serial memory device, errno=%d\n", -i);
 803 
 804         if (stli_tmpwritebuf != (char *) NULL)
 805                 kfree_s(stli_tmpwritebuf, STLI_TXBUFSIZE);
 806         if (stli_txcookbuf != (char *) NULL)
 807                 kfree_s(stli_txcookbuf, STLI_TXBUFSIZE);
 808 
 809         for (i = 0; (i < stli_nrbrds); i++) {
 810                 brdp = stli_brds[i];
 811                 if (brdp == (stlibrd_t *) NULL)
 812                         continue;
 813                 for (j = 0; (j < STL_MAXPORTS); j++) {
 814                         portp = brdp->ports[j];
 815                         if (portp != (stliport_t *) NULL) {
 816                                 if (portp->tty != (struct tty_struct *) NULL)
 817                                         tty_hangup(portp->tty);
 818                                 kfree_s(portp, sizeof(stliport_t));
 819                         }
 820                 }
 821 
 822 #if STLI_HIMEMORY
 823                 if (((unsigned long) brdp->membase) >= 0x100000)
 824                         vfree(brdp->membase);
 825 #endif
 826                 if ((brdp->brdtype == BRD_ECP) || (brdp->brdtype == BRD_ECPE) || (brdp->brdtype == BRD_ECPMC))
 827                         release_region(brdp->iobase, ECP_IOSIZE);
 828                 else
 829                         release_region(brdp->iobase, ONB_IOSIZE);
 830                 kfree_s(brdp, sizeof(stlibrd_t));
 831                 stli_brds[i] = (stlibrd_t *) NULL;
 832         }
 833 
 834         restore_flags(flags);
 835 }
 836 
 837 #endif
 838 
 839 /*****************************************************************************/
 840 
 841 /*
 842  *      Local driver kernel malloc routine.
 843  */
 844 
 845 static void *stli_memalloc(int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 846 {
 847         return((void *) kmalloc(len, GFP_KERNEL));
 848 }
 849 
 850 /*****************************************************************************/
 851 
 852 static int stli_open(struct tty_struct *tty, struct file *filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 853 {
 854         stlibrd_t       *brdp;
 855         stliport_t      *portp;
 856         unsigned int    minordev;
 857         int             brdnr, portnr, rc;
 858 
 859 #if DEBUG
 860         printk("stli_open(tty=%x,filp=%x): device=%x\n", (int) tty, (int) filp, tty->device);
 861 #endif
 862 
 863         minordev = MINOR(tty->device);
 864         brdnr = MKDEV2BRD(minordev);
 865         if (brdnr >= stli_nrbrds)
 866                 return(-ENODEV);
 867         brdp = stli_brds[brdnr];
 868         if (brdp == (stlibrd_t *) NULL)
 869                 return(-ENODEV);
 870         if ((brdp->state & BST_STARTED) == 0)
 871                 return(-ENODEV);
 872         portnr = MKDEV2PORT(minordev);
 873         if ((portnr < 0) || (portnr > brdp->nrports))
 874                 return(-ENODEV);
 875 
 876         portp = brdp->ports[portnr];
 877         if (portp == (stliport_t *) NULL)
 878                 return(-ENODEV);
 879         if (portp->devnr < 1)
 880                 return(-ENODEV);
 881 
 882 /*
 883  *      Check if this port is in the middle of closing. If so then wait
 884  *      until it is closed then return error status based on flag settings.
 885  *      The sleep here does not need interrupt protection since the wakeup
 886  *      for it is done with the same context.
 887  */
 888         if (portp->flags & ASYNC_CLOSING) {
 889                 interruptible_sleep_on(&portp->close_wait);
 890                 if (portp->flags & ASYNC_HUP_NOTIFY)
 891                         return(-EAGAIN);
 892                 return(-ERESTARTSYS);
 893         }
 894 
 895 /*
 896  *      On the first open of the device setup the port hardware, and
 897  *      initialize the per port data structure. Since initializing the port
 898  *      requires serval commands to the board we will need to wait for any
 899  *      other open that is already initializing the port.
 900  */
 901         portp->tty = tty;
 902         tty->driver_data = portp;
 903         portp->refcount++;
 904 
 905         while (test_bit(ST_INITIALIZING, &portp->state)) {
 906                 if (current->signal & ~current->blocked)
 907                         return(-ERESTARTSYS);
 908                 interruptible_sleep_on(&portp->raw_wait);
 909         }
 910 
 911         if ((portp->flags & ASYNC_INITIALIZED) == 0) {
 912                 set_bit(ST_INITIALIZING, &portp->state);
 913                 if ((rc = stli_initopen(brdp, portp)) >= 0) {
 914                         portp->flags |= ASYNC_INITIALIZED;
 915                         clear_bit(TTY_IO_ERROR, &tty->flags);
 916                 }
 917                 clear_bit(ST_INITIALIZING, &portp->state);
 918                 wake_up_interruptible(&portp->raw_wait);
 919                 if (rc < 0)
 920                         return(rc);
 921         }
 922 
 923 /*
 924  *      Check if this port is in the middle of closing. If so then wait
 925  *      until it is closed then return error status, based on flag settings.
 926  *      The sleep here does not need interrupt protection since the wakeup
 927  *      for it is done with the same context.
 928  */
 929         if (portp->flags & ASYNC_CLOSING) {
 930                 interruptible_sleep_on(&portp->close_wait);
 931                 if (portp->flags & ASYNC_HUP_NOTIFY)
 932                         return(-EAGAIN);
 933                 return(-ERESTARTSYS);
 934         }
 935 
 936 /*
 937  *      Based on type of open being done check if it can overlap with any
 938  *      previous opens still in effect. If we are a normal serial device
 939  *      then also we might have to wait for carrier.
 940  */
 941         if (tty->driver.subtype == STL_DRVTYPCALLOUT) {
 942                 if (portp->flags & ASYNC_NORMAL_ACTIVE)
 943                         return(-EBUSY);
 944                 if (portp->flags & ASYNC_CALLOUT_ACTIVE) {
 945                         if ((portp->flags & ASYNC_SESSION_LOCKOUT) &&
 946                                         (portp->session != current->session))
 947                                 return(-EBUSY);
 948                         if ((portp->flags & ASYNC_PGRP_LOCKOUT) &&
 949                                         (portp->pgrp != current->pgrp))
 950                                 return(-EBUSY);
 951                 }
 952                 portp->flags |= ASYNC_CALLOUT_ACTIVE;
 953         } else {
 954                 if (filp->f_flags & O_NONBLOCK) {
 955                         if (portp->flags & ASYNC_CALLOUT_ACTIVE)
 956                                 return(-EBUSY);
 957                 } else {
 958                         if ((rc = stli_waitcarrier(brdp, portp, filp)) != 0)
 959                                 return(rc);
 960                 }
 961                 portp->flags |= ASYNC_NORMAL_ACTIVE;
 962         }
 963 
 964         if ((portp->refcount == 1) && (portp->flags & ASYNC_SPLIT_TERMIOS)) {
 965                 if (tty->driver.subtype == STL_DRVTYPSERIAL)
 966                         *tty->termios = portp->normaltermios;
 967                 else
 968                         *tty->termios = portp->callouttermios;
 969                 stli_setport(portp);
 970         }
 971 
 972         portp->session = current->session;
 973         portp->pgrp = current->pgrp;
 974         return(0);
 975 }
 976 
 977 /*****************************************************************************/
 978 
 979 static void stli_close(struct tty_struct *tty, struct file *filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 980 {
 981         stlibrd_t       *brdp;
 982         stliport_t      *portp;
 983         unsigned long   flags;
 984 
 985 #if DEBUG
 986         printk("stli_close(tty=%x,filp=%x)\n", (int) tty, (int) filp);
 987 #endif
 988 
 989         portp = tty->driver_data;
 990         if (portp == (stliport_t *) NULL)
 991                 return;
 992 
 993         save_flags(flags);
 994         cli();
 995         if (tty_hung_up_p(filp)) {
 996                 restore_flags(flags);
 997                 return;
 998         }
 999         if (portp->refcount-- > 1) {
1000                 restore_flags(flags);
1001                 return;
1002         }
1003 
1004         portp->flags |= ASYNC_CLOSING;
1005 
1006         if (portp->flags & ASYNC_NORMAL_ACTIVE)
1007                 portp->normaltermios = *tty->termios;
1008         if (portp->flags & ASYNC_CALLOUT_ACTIVE)
1009                 portp->callouttermios = *tty->termios;
1010 
1011 /*
1012  *      May want to wait for data to drain before closing. The BUSY flag
1013  *      keeps track of whether we are still transmitting or not. It is
1014  *      updated by messages from the slave - indicating when all chars
1015  *      really have drained.
1016  */
1017         if (tty == stli_txcooktty)
1018                 stli_flushchars(tty);
1019         tty->closing = 1;
1020         if (test_bit(ST_TXBUSY, &portp->state)) {
1021                 if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1022                         tty_wait_until_sent(tty, portp->closing_wait);
1023         }
1024 
1025         portp->flags &= ~ASYNC_INITIALIZED;
1026         brdp = stli_brds[portp->brdnr];
1027         stli_rawclose(brdp, portp, 0, 0);
1028         if (tty->termios->c_cflag & HUPCL) {
1029                 stli_mkasysigs(&portp->asig, 0, 0);
1030                 if (test_bit(ST_CMDING, &portp->state))
1031                         set_bit(ST_DOSIGS, &portp->state);
1032                 else
1033                         stli_sendcmd(brdp, portp, A_SETSIGNALS, &portp->asig, sizeof(asysigs_t), 0);
1034         }
1035         clear_bit(ST_TXBUSY, &portp->state);
1036         clear_bit(ST_RXSTOP, &portp->state);
1037         set_bit(TTY_IO_ERROR, &tty->flags);
1038         if (tty->ldisc.flush_buffer)
1039                 (tty->ldisc.flush_buffer)(tty);
1040         set_bit(ST_DOFLUSHRX, &portp->state);
1041         stli_flushbuffer(tty);
1042 
1043         tty->closing = 0;
1044         tty->driver_data = (void *) NULL;
1045         portp->tty = (struct tty_struct *) NULL;
1046 
1047         if (portp->openwaitcnt) {
1048                 if (portp->close_delay)
1049                         stli_delay(portp->close_delay);
1050                 wake_up_interruptible(&portp->open_wait);
1051         }
1052 
1053         portp->flags &= ~(ASYNC_CALLOUT_ACTIVE | ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1054         wake_up_interruptible(&portp->close_wait);
1055         restore_flags(flags);
1056 }
1057 
1058 /*****************************************************************************/
1059 
1060 /*
1061  *      Carry out first open operations on a port. This involves a number of
1062  *      commands to be sent to the slave. We need to open the port, set the
1063  *      notification events, set the initial port settings, get and set the
1064  *      initial signal values. We sleep and wait in between each one. But
1065  *      this still all happens pretty quickly.
1066  */
1067 
1068 static int stli_initopen(stlibrd_t *brdp, stliport_t *portp)
     /* [previous][next][first][last][top][bottom][index][help] */
1069 {
1070         struct tty_struct       *tty;
1071         asynotify_t             nt;
1072         asyport_t               aport;
1073         int                     rc;
1074 
1075 #if DEBUG
1076         printk("stli_initopen(brdp=%x,portp=%x)\n", (int) brdp, (int) portp);
1077 #endif
1078 
1079         if ((rc = stli_rawopen(brdp, portp, 0, 1)) < 0)
1080                 return(rc);
1081 
1082         memset(&nt, 0, sizeof(asynotify_t));
1083         nt.data = (DT_TXLOW | DT_TXEMPTY | DT_RXBUSY | DT_RXBREAK);
1084         nt.signal = SG_DCD;
1085         if ((rc = stli_cmdwait(brdp, portp, A_SETNOTIFY, &nt, sizeof(asynotify_t), 0)) < 0)
1086                 return(rc);
1087 
1088         tty = portp->tty;
1089         if (tty == (struct tty_struct *) NULL)
1090                 return(-ENODEV);
1091         stli_mkasyport(portp, &aport, tty->termios);
1092         if ((rc = stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0)) < 0)
1093                 return(rc);
1094 
1095         set_bit(ST_GETSIGS, &portp->state);
1096         if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS, &portp->asig, sizeof(asysigs_t), 1)) < 0)
1097                 return(rc);
1098         if (clear_bit(ST_GETSIGS, &portp->state))
1099                 portp->sigs = stli_mktiocm(portp->asig.sigvalue);
1100         stli_mkasysigs(&portp->asig, 1, 1);
1101         if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig, sizeof(asysigs_t), 0)) < 0)
1102                 return(rc);
1103 
1104         return(0);
1105 }
1106 
1107 /*****************************************************************************/
1108 
1109 /*
1110  *      Send an open message to the slave. This will sleep waiting for the
1111  *      acknowledgement, so must have user context. We need to co-ordinate
1112  *      with close events here, since we don't want open and close events
1113  *      to overlap.
1114  */
1115 
1116 static int stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
     /* [previous][next][first][last][top][bottom][index][help] */
1117 {
1118         volatile cdkhdr_t       *hdrp;
1119         volatile cdkctrl_t      *cp;
1120         volatile unsigned char  *bits;
1121         unsigned long           flags;
1122         int                     rc;
1123 
1124 #if DEBUG
1125         printk("stli_rawopen(brdp=%x,portp=%x,arg=%x,wait=%d)\n", (int) brdp, (int) portp, (int) arg, wait);
1126 #endif
1127 
1128 /*
1129  *      Send a message to the slave to open this port.
1130  */
1131         save_flags(flags);
1132         cli();
1133 
1134 /*
1135  *      Slave is already closing this port. This can happen if a hangup
1136  *      occurs on this port. So we must wait until it is complete. The
1137  *      order of opens and closes may not be preserved across shared
1138  *      memory, so we must wait until it is complete.
1139  */
1140         while (test_bit(ST_CLOSING, &portp->state)) {
1141                 if (current->signal & ~current->blocked) {
1142                         restore_flags(flags);
1143                         return(-ERESTARTSYS);
1144                 }
1145                 interruptible_sleep_on(&portp->raw_wait);
1146         }
1147 
1148 /*
1149  *      Everything is ready now, so write the open message into shared
1150  *      memory. Once the message is in set the service bits to say that
1151  *      this port wants service.
1152  */
1153         EBRDENABLE(brdp);
1154         cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1155         cp->openarg = arg;
1156         cp->open = 1;
1157         hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1158         hdrp->slavereq |= portp->reqbit;
1159         bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset + portp->portidx;
1160         *bits |= portp->portbit;
1161         EBRDDISABLE(brdp);
1162 
1163         if (wait == 0) {
1164                 restore_flags(flags);
1165                 return(0);
1166         }
1167 
1168 /*
1169  *      Slave is in action, so now we must wait for the open acknowledgment
1170  *      to come back.
1171  */
1172         rc = 0;
1173         set_bit(ST_OPENING, &portp->state);
1174         while (test_bit(ST_OPENING, &portp->state)) {
1175                 if (current->signal & ~current->blocked) {
1176                         rc = -ERESTARTSYS;
1177                         break;
1178                 }
1179                 interruptible_sleep_on(&portp->raw_wait);
1180         }
1181         restore_flags(flags);
1182 
1183         if ((rc == 0) && (portp->rc != 0))
1184                 rc = -EIO;
1185         return(rc);
1186 }
1187 
1188 /*****************************************************************************/
1189 
1190 /*
1191  *      Send a close message to the slave. Normally this will sleep waiting
1192  *      for the acknowledgement, but if wait parameter is 0 it will not. If
1193  *      wait is true then must have user context (to sleep).
1194  */
1195 
1196 static int stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
     /* [previous][next][first][last][top][bottom][index][help] */
1197 {
1198         volatile cdkhdr_t       *hdrp;
1199         volatile cdkctrl_t      *cp;
1200         volatile unsigned char  *bits;
1201         unsigned long           flags;
1202         int                     rc;
1203 
1204 #if DEBUG
1205         printk("stli_rawclose(brdp=%x,portp=%x,arg=%x,wait=%d)\n", (int) brdp, (int) portp, (int) arg, wait);
1206 #endif
1207 
1208         save_flags(flags);
1209         cli();
1210 
1211 /*
1212  *      Slave is already closing this port. This can happen if a hangup
1213  *      occurs on this port.
1214  */
1215         if (wait) {
1216                 while (test_bit(ST_CLOSING, &portp->state)) {
1217                         if (current->signal & ~current->blocked) {
1218                                 restore_flags(flags);
1219                                 return(-ERESTARTSYS);
1220                         }
1221                         interruptible_sleep_on(&portp->raw_wait);
1222                 }
1223         }
1224 
1225 /*
1226  *      Write the close command into shared memory.
1227  */
1228         EBRDENABLE(brdp);
1229         cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1230         cp->closearg = arg;
1231         cp->close = 1;
1232         hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1233         hdrp->slavereq |= portp->reqbit;
1234         bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset + portp->portidx;
1235         *bits |= portp->portbit;
1236         EBRDDISABLE(brdp);
1237 
1238         set_bit(ST_CLOSING, &portp->state);
1239         if (wait == 0) {
1240                 restore_flags(flags);
1241                 return(0);
1242         }
1243 
1244 /*
1245  *      Slave is in action, so now we must wait for the open acknowledgment
1246  *      to come back.
1247  */
1248         rc = 0;
1249         while (test_bit(ST_CLOSING, &portp->state)) {
1250                 if (current->signal & ~current->blocked) {
1251                         rc = -ERESTARTSYS;
1252                         break;
1253                 }
1254                 interruptible_sleep_on(&portp->raw_wait);
1255         }
1256         restore_flags(flags);
1257 
1258         if ((rc == 0) && (portp->rc != 0))
1259                 rc = -EIO;
1260         return(rc);
1261 }
1262 
1263 /*****************************************************************************/
1264 
1265 /*
1266  *      Send a command to the slave and wait for the response. This must
1267  *      have user context (it sleeps). This routine is generic in that it
1268  *      can send any type of command. Its purpose is to wait for that command
1269  *      to complete (as opposed to initiating the command then returning).
1270  */
1271 
1272 static int stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
     /* [previous][next][first][last][top][bottom][index][help] */
1273 {
1274         unsigned long   flags;
1275 
1276 #if DEBUG
1277         printk("stli_cmdwait(brdp=%x,portp=%x,cmd=%x,arg=%x,size=%d,copyback=%d)\n", (int) brdp, (int) portp, (int) cmd, (int) arg, size, copyback);
1278 #endif
1279 
1280         save_flags(flags);
1281         cli();
1282         while (test_bit(ST_CMDING, &portp->state)) {
1283                 if (current->signal & ~current->blocked) {
1284                         restore_flags(flags);
1285                         return(-ERESTARTSYS);
1286                 }
1287                 interruptible_sleep_on(&portp->raw_wait);
1288         }
1289 
1290         stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
1291 
1292         while (test_bit(ST_CMDING, &portp->state)) {
1293                 if (current->signal & ~current->blocked) {
1294                         restore_flags(flags);
1295                         return(-ERESTARTSYS);
1296                 }
1297                 interruptible_sleep_on(&portp->raw_wait);
1298         }
1299         restore_flags(flags);
1300 
1301         if (portp->rc != 0)
1302                 return(-EIO);
1303         return(0);
1304 }
1305 
1306 /*****************************************************************************/
1307 
1308 /*
1309  *      Send the termios settings for this port to the slave. This sleeps
1310  *      waiting for the command to complete - so must have user context.
1311  */
1312 
1313 static int stli_setport(stliport_t *portp)
     /* [previous][next][first][last][top][bottom][index][help] */
1314 {
1315         stlibrd_t       *brdp;
1316         asyport_t       aport;
1317 
1318 #if DEBUG
1319         printk("stli_setport(portp=%x)\n", (int) portp);
1320 #endif
1321 
1322         if (portp == (stliport_t *) NULL)
1323                 return(-ENODEV);
1324         if (portp->tty == (struct tty_struct *) NULL)
1325                 return(-ENODEV);
1326         if ((portp->brdnr < 0) && (portp->brdnr >= stli_nrbrds))
1327                 return(-ENODEV);
1328         brdp = stli_brds[portp->brdnr];
1329         if (brdp == (stlibrd_t *) NULL)
1330                 return(-ENODEV);
1331 
1332         stli_mkasyport(portp, &aport, portp->tty->termios);
1333         return(stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0));
1334 }
1335 
1336 /*****************************************************************************/
1337 
1338 /*
1339  *      Wait for a specified delay period, this is not a busy-loop. It will
1340  *      give up the processor while waiting. Unfortunately this has some
1341  *      rather intimate knowledge of the process management stuff.
1342  */
1343 
1344 static void stli_delay(int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1345 {
1346 #if DEBUG
1347         printk("stl_delay(len=%d)\n", len);
1348 #endif
1349         if (len > 0) {
1350                 current->state = TASK_INTERRUPTIBLE;
1351                 current->timeout = jiffies + len;
1352                 schedule();
1353         }
1354 }
1355 
1356 /*****************************************************************************/
1357 
1358 /*
1359  *      Possibly need to wait for carrier (DCD signal) to come high. Say
1360  *      maybe because if we are clocal then we don't need to wait...
1361  */
1362 
1363 static int stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1364 {
1365         unsigned long   flags;
1366         int             rc;
1367 
1368 #if DEBUG
1369         printk("stli_waitcarrier(brdp=%x,portp=%x,filp=%x)\n", (int) brdp, (int) portp, (int) filp);
1370 #endif
1371 
1372         rc = 0;
1373 
1374         save_flags(flags);
1375         cli();
1376         portp->openwaitcnt++;
1377         if (portp->refcount > 0)
1378                 portp->refcount--;
1379 
1380         for (;;) {
1381                 if ((portp->flags & ASYNC_CALLOUT_ACTIVE) == 0) {
1382                         stli_mkasysigs(&portp->asig, 1, 1);
1383                         if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig, sizeof(asysigs_t), 0)) < 0)
1384                                 break;
1385                 }
1386                 if (tty_hung_up_p(filp) || ((portp->flags & ASYNC_INITIALIZED) == 0)) {
1387                         if (portp->flags & ASYNC_HUP_NOTIFY)
1388                                 rc = -EBUSY;
1389                         else
1390                                 rc = -ERESTARTSYS;
1391                         break;
1392                 }
1393                 if (((portp->flags & ASYNC_CALLOUT_ACTIVE) == 0) &&
1394                                 ((portp->flags & ASYNC_CLOSING) == 0) &&
1395                                 ((portp->tty->termios->c_cflag & CLOCAL) ||
1396                                 (portp->sigs & TIOCM_CD))) {
1397                         break;
1398                 }
1399                 if (current->signal & ~current->blocked) {
1400                         rc = -ERESTARTSYS;
1401                         break;
1402                 }
1403                 interruptible_sleep_on(&portp->open_wait);
1404         }
1405 
1406         if (! tty_hung_up_p(filp))
1407                 portp->refcount++;
1408         portp->openwaitcnt--;
1409         restore_flags(flags);
1410 
1411         return(rc);
1412 }
1413 
1414 /*****************************************************************************/
1415 
1416 /*
1417  *      Write routine. Take the data and put it in the shared memory ring
1418  *      queue. If port is not already sending chars then need to mark the
1419  *      service bits for this port.
1420  */
1421 
1422 static int stli_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1423 {
1424         volatile cdkasy_t       *ap;
1425         volatile cdkhdr_t       *hdrp;
1426         volatile unsigned char  *bits;
1427         unsigned char           *shbuf, *chbuf;
1428         stliport_t              *portp;
1429         stlibrd_t               *brdp;
1430         unsigned int            len, stlen, head, tail, size;
1431         unsigned long           flags;
1432 
1433 #if DEBUG
1434         printk("stli_write(tty=%x,from_user=%d,buf=%x,count=%d)\n", (int) tty, from_user, (int) buf, count);
1435 #endif
1436 
1437         if ((tty == (struct tty_struct *) NULL) || (stli_tmpwritebuf == (char *) NULL))
1438                 return(0);
1439         if (tty == stli_txcooktty)
1440                 stli_flushchars(tty);
1441         portp = tty->driver_data;
1442         if (portp == (stliport_t *) NULL)
1443                 return(0);
1444         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1445                 return(0);
1446         brdp = stli_brds[portp->brdnr];
1447         if (brdp == (stlibrd_t *) NULL)
1448                 return(0);
1449         chbuf = (unsigned char *) buf;
1450 
1451 /*
1452  *      If copying direct from user space we need to be able to handle page
1453  *      faults while we are copying. To do this copy as much as we can now
1454  *      into a kernel buffer. From there we copy it into shared memory. The
1455  *      big problem is that we do not want shared memory enabled when we are
1456  *      sleeping (other boards may be serviced while asleep). Something else
1457  *      to note here is the reading of the tail twice. Since the boards
1458  *      shared memory can be on an 8-bit bus then we need to be very carefull
1459  *      reading 16 bit quantities - since both the board (slave) and host
1460  *      cound be writing and reading at the same time.
1461  */
1462         if (from_user) {
1463                 save_flags(flags);
1464                 cli();
1465                 EBRDENABLE(brdp);
1466                 ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1467                 head = (unsigned int) ap->txq.head;
1468                 tail = (unsigned int) ap->txq.tail;
1469                 if (tail != ((unsigned int) ap->txq.tail))
1470                         tail = (unsigned int) ap->txq.tail;
1471                 len = (head >= tail) ? (portp->txsize - (head - tail) - 1) : (tail - head - 1);
1472                 count = MIN(len, count);
1473                 EBRDDISABLE(brdp);
1474 
1475                 down(&stli_tmpwritesem);
1476                 memcpy_fromfs(stli_tmpwritebuf, chbuf, count);
1477                 up(&stli_tmpwritesem);
1478                 chbuf = &stli_tmpwritebuf[0];
1479                 restore_flags(flags);
1480         }
1481 
1482 /*
1483  *      All data is now local, shove as much as possible into shared memory.
1484  */
1485         save_flags(flags);
1486         cli();
1487         EBRDENABLE(brdp);
1488         ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1489         head = (unsigned int) ap->txq.head;
1490         tail = (unsigned int) ap->txq.tail;
1491         if (tail != ((unsigned int) ap->txq.tail))
1492                 tail = (unsigned int) ap->txq.tail;
1493         size = portp->txsize;
1494         if (head >= tail) {
1495                 len = size - (head - tail) - 1;
1496                 stlen = size - head;
1497         } else {
1498                 len = tail - head - 1;
1499                 stlen = len;
1500         }
1501 
1502         len = MIN(len, count);
1503         count = 0;
1504         shbuf = (char *) EBRDGETMEMPTR(brdp, portp->txoffset);
1505 
1506         while (len > 0) {
1507                 stlen = MIN(len, stlen);
1508                 memcpy((shbuf + head), chbuf, stlen);
1509                 chbuf += stlen;
1510                 len -= stlen;
1511                 count += stlen;
1512                 head += stlen;
1513                 if (head >= size) {
1514                         head = 0;
1515                         stlen = tail;
1516                 }
1517         }
1518 
1519         ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1520         ap->txq.head = head;
1521         if (test_bit(ST_TXBUSY, &portp->state)) {
1522                 if (ap->changed.data & DT_TXEMPTY)
1523                         ap->changed.data &= ~DT_TXEMPTY;
1524         }
1525         hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1526         hdrp->slavereq |= portp->reqbit;
1527         bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset + portp->portidx;
1528         *bits |= portp->portbit;
1529         set_bit(ST_TXBUSY, &portp->state);
1530 
1531         EBRDDISABLE(brdp);
1532         restore_flags(flags);
1533 
1534         return(count);
1535 }
1536 
1537 /*****************************************************************************/
1538 
1539 /*
1540  *      Output a single character. We put it into a temporary local buffer
1541  *      (for speed) then write out that buffer when the flushchars routine
1542  *      is called. There is a safety catch here so that if some other port
1543  *      writes chars before the current buffer has been, then we write them
1544  *      first them do the new ports.
1545  */
1546 
1547 static void stli_putchar(struct tty_struct *tty, unsigned char ch)
     /* [previous][next][first][last][top][bottom][index][help] */
1548 {
1549 #if DEBUG
1550         printk("stli_putchar(tty=%x,ch=%x)\n", (int) tty, (int) ch);
1551 #endif
1552 
1553         if (tty == (struct tty_struct *) NULL)
1554                 return;
1555         if (tty != stli_txcooktty) {
1556                 if (stli_txcooktty != (struct tty_struct *) NULL)
1557                         stli_flushchars(stli_txcooktty);
1558                 stli_txcooktty = tty;
1559         }
1560 
1561         stli_txcookbuf[stli_txcooksize++] = ch;
1562 }
1563 
1564 /*****************************************************************************/
1565 
1566 /*
1567  *      Transfer characters from the local TX cooking buffer to the board.
1568  *      We sort of ignore the tty that gets passed in here. We rely on the
1569  *      info stored with the TX cook buffer to tell us which port to flush
1570  *      the data on. In any case we clean out the TX cook buffer, for re-use
1571  *      by someone else.
1572  */
1573 
1574 static void stli_flushchars(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1575 {
1576         volatile cdkhdr_t       *hdrp;
1577         volatile unsigned char  *bits;
1578         volatile cdkasy_t       *ap;
1579         struct tty_struct       *cooktty;
1580         stliport_t              *portp;
1581         stlibrd_t               *brdp;
1582         unsigned int            len, stlen, head, tail, size, count, cooksize;
1583         unsigned char           *buf, *shbuf;
1584         unsigned long           flags;
1585 
1586 #if DEBUG
1587         printk("stli_flushchars(tty=%x)\n", (int) tty);
1588 #endif
1589 
1590         cooksize = stli_txcooksize;
1591         cooktty = stli_txcooktty;
1592         stli_txcooksize = 0;
1593         stli_txcookrealsize = 0;
1594         stli_txcooktty = (struct tty_struct *) NULL;
1595 
1596         if (tty == (struct tty_struct *) NULL)
1597                 return;
1598         if (cooktty == (struct tty_struct *) NULL)
1599                 return;
1600         if (tty != cooktty)
1601                 tty = cooktty;
1602         if (cooksize == 0)
1603                 return;
1604 
1605         portp = tty->driver_data;
1606         if (portp == (stliport_t *) NULL)
1607                 return;
1608         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1609                 return;
1610         brdp = stli_brds[portp->brdnr];
1611         if (brdp == (stlibrd_t *) NULL)
1612                 return;
1613 
1614         save_flags(flags);
1615         cli();
1616         EBRDENABLE(brdp);
1617 
1618         ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1619         head = (unsigned int) ap->txq.head;
1620         tail = (unsigned int) ap->txq.tail;
1621         if (tail != ((unsigned int) ap->txq.tail))
1622                 tail = (unsigned int) ap->txq.tail;
1623         size = portp->txsize;
1624         if (head >= tail) {
1625                 len = size - (head - tail) - 1;
1626                 stlen = size - head;
1627         } else {
1628                 len = tail - head - 1;
1629                 stlen = len;
1630         }
1631 
1632         len = MIN(len, cooksize);
1633         count = 0;
1634         shbuf = (char *) EBRDGETMEMPTR(brdp, portp->txoffset);
1635         buf = stli_txcookbuf;
1636 
1637         while (len > 0) {
1638                 stlen = MIN(len, stlen);
1639                 memcpy((shbuf + head), buf, stlen);
1640                 buf += stlen;
1641                 len -= stlen;
1642                 count += stlen;
1643                 head += stlen;
1644                 if (head >= size) {
1645                         head = 0;
1646                         stlen = tail;
1647                 }
1648         }
1649 
1650         ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1651         ap->txq.head = head;
1652 
1653         if (test_bit(ST_TXBUSY, &portp->state)) {
1654                 if (ap->changed.data & DT_TXEMPTY)
1655                         ap->changed.data &= ~DT_TXEMPTY;
1656         }
1657         hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1658         hdrp->slavereq |= portp->reqbit;
1659         bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset + portp->portidx;
1660         *bits |= portp->portbit;
1661         set_bit(ST_TXBUSY, &portp->state);
1662 
1663         EBRDDISABLE(brdp);
1664         restore_flags(flags);
1665 }
1666 
1667 /*****************************************************************************/
1668 
1669 static int stli_writeroom(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1670 {
1671         volatile cdkasyrq_t     *rp;
1672         stliport_t              *portp;
1673         stlibrd_t               *brdp;
1674         unsigned int            head, tail, len;
1675         unsigned long           flags;
1676 
1677 #if DEBUG
1678         printk("stli_writeroom(tty=%x)\n", (int) tty);
1679 #endif
1680 
1681         if (tty == (struct tty_struct *) NULL)
1682                 return(0);
1683         if (tty == stli_txcooktty) {
1684                 if (stli_txcookrealsize != 0) {
1685                         len = stli_txcookrealsize - stli_txcooksize;
1686                         return(len);
1687                 }
1688         }
1689 
1690         portp = tty->driver_data;
1691         if (portp == (stliport_t *) NULL)
1692                 return(0);
1693         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1694                 return(0);
1695         brdp = stli_brds[portp->brdnr];
1696         if (brdp == (stlibrd_t *) NULL)
1697                 return(0);
1698 
1699         save_flags(flags);
1700         cli();
1701         EBRDENABLE(brdp);
1702         rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1703         head = (unsigned int) rp->head;
1704         tail = (unsigned int) rp->tail;
1705         if (tail != ((unsigned int) rp->tail))
1706                 tail = (unsigned int) rp->tail;
1707         len = (head >= tail) ? (portp->txsize - (head - tail)) : (tail - head);
1708         len--;
1709         EBRDDISABLE(brdp);
1710         restore_flags(flags);
1711 
1712         if (tty == stli_txcooktty) {
1713                 stli_txcookrealsize = len;
1714                 len -= stli_txcooksize;
1715         }
1716         return(len);
1717 }
1718 
1719 /*****************************************************************************/
1720 
1721 /*
1722  *      Return the number of characters in the transmit buffer. Normally we
1723  *      will return the number of chars in the shared memory ring queue.
1724  *      We need to kludge around the case where the shared memory buffer is
1725  *      empty but not all characters have drained yet, for this case just
1726  *      return that there is 1 character in the buffer!
1727  */
1728 
1729 static int stli_charsinbuffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1730 {
1731         volatile cdkasyrq_t     *rp;
1732         stliport_t              *portp;
1733         stlibrd_t               *brdp;
1734         unsigned int            head, tail, len;
1735         unsigned long           flags;
1736 
1737 #if DEBUG
1738         printk("stli_charsinbuffer(tty=%x)\n", (int) tty);
1739 #endif
1740 
1741         if (tty == (struct tty_struct *) NULL)
1742                 return(0);
1743         if (tty == stli_txcooktty)
1744                 stli_flushchars(tty);
1745         portp = tty->driver_data;
1746         if (portp == (stliport_t *) NULL)
1747                 return(0);
1748         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1749                 return(0);
1750         brdp = stli_brds[portp->brdnr];
1751         if (brdp == (stlibrd_t *) NULL)
1752                 return(0);
1753 
1754         save_flags(flags);
1755         cli();
1756         EBRDENABLE(brdp);
1757         rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1758         head = (unsigned int) rp->head;
1759         tail = (unsigned int) rp->tail;
1760         if (tail != ((unsigned int) rp->tail))
1761                 tail = (unsigned int) rp->tail;
1762         len = (head >= tail) ? (head - tail) : (portp->txsize - (tail - head));
1763         if ((len == 0) && test_bit(ST_TXBUSY, &portp->state))
1764                 len = 1;
1765         EBRDDISABLE(brdp);
1766         restore_flags(flags);
1767 
1768         return(len);
1769 }
1770 
1771 /*****************************************************************************/
1772 
1773 /*
1774  *      Generate the serial struct info.
1775  */
1776 
1777 static void stli_getserial(stliport_t *portp, struct serial_struct *sp)
     /* [previous][next][first][last][top][bottom][index][help] */
1778 {
1779         struct serial_struct    sio;
1780         stlibrd_t               *brdp;
1781 
1782 #if DEBUG
1783         printk("stli_getserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1784 #endif
1785 
1786         memset(&sio, 0, sizeof(struct serial_struct));
1787         sio.type = PORT_UNKNOWN;
1788         sio.line = portp->portnr;
1789         sio.irq = 0;
1790         sio.flags = portp->flags;
1791         sio.baud_base = portp->baud_base;
1792         sio.close_delay = portp->close_delay;
1793         sio.closing_wait = portp->closing_wait;
1794         sio.custom_divisor = portp->custom_divisor;
1795         sio.xmit_fifo_size = 0;
1796         sio.hub6 = 0;
1797 
1798         brdp = stli_brds[portp->brdnr];
1799         if (brdp != (stlibrd_t *) NULL)
1800                 sio.port = brdp->iobase;
1801                 
1802         memcpy_tofs(sp, &sio, sizeof(struct serial_struct));
1803 }
1804 
1805 /*****************************************************************************/
1806 
1807 /*
1808  *      Set port according to the serial struct info.
1809  *      At this point we do not do any auto-configure stuff, so we will
1810  *      just quietly ignore any requests to change irq, etc.
1811  */
1812 
1813 static int stli_setserial(stliport_t *portp, struct serial_struct *sp)
     /* [previous][next][first][last][top][bottom][index][help] */
1814 {
1815         struct serial_struct    sio;
1816         int                     rc;
1817 
1818 #if DEBUG
1819         printk("stli_setserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1820 #endif
1821 
1822         memcpy_fromfs(&sio, sp, sizeof(struct serial_struct));
1823         if (!suser()) {
1824                 if ((sio.baud_base != portp->baud_base) ||
1825                                 (sio.close_delay != portp->close_delay) ||
1826                                 ((sio.flags & ~ASYNC_USR_MASK) != (portp->flags & ~ASYNC_USR_MASK)))
1827                         return(-EPERM);
1828         } 
1829 
1830         portp->flags = (portp->flags & ~ASYNC_USR_MASK) | (sio.flags & ASYNC_USR_MASK);
1831         portp->baud_base = sio.baud_base;
1832         portp->close_delay = sio.close_delay;
1833         portp->closing_wait = sio.closing_wait;
1834         portp->custom_divisor = sio.custom_divisor;
1835 
1836         if ((rc = stli_setport(portp)) < 0)
1837                 return(rc);
1838         return(0);
1839 }
1840 
1841 /*****************************************************************************/
1842 
1843 static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1844 {
1845         stliport_t      *portp;
1846         stlibrd_t       *brdp;
1847         unsigned long   val;
1848         int             rc;
1849 
1850 #if DEBUG
1851         printk("stli_ioctl(tty=%x,file=%x,cmd=%x,arg=%x)\n", (int) tty, (int) file, cmd, (int) arg);
1852 #endif
1853 
1854         if (tty == (struct tty_struct *) NULL)
1855                 return(-ENODEV);
1856         portp = tty->driver_data;
1857         if (portp == (stliport_t *) NULL)
1858                 return(-ENODEV);
1859         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1860                 return(0);
1861         brdp = stli_brds[portp->brdnr];
1862         if (brdp == (stlibrd_t *) NULL)
1863                 return(0);
1864 
1865         rc = 0;
1866 
1867         switch (cmd) {
1868         case TCSBRK:
1869                 if ((rc = tty_check_change(tty)) == 0) {
1870                         tty_wait_until_sent(tty, 0);
1871                         if (! arg) {
1872                                 val = 250;
1873                                 rc = stli_cmdwait(brdp, portp, A_BREAK, &val, sizeof(unsigned long), 0);
1874                         }
1875                 }
1876                 break;
1877         case TCSBRKP:
1878                 if ((rc = tty_check_change(tty)) == 0) {
1879                         tty_wait_until_sent(tty, 0);
1880                         val = (arg ? (arg * 100) : 250);
1881                         rc = stli_cmdwait(brdp, portp, A_BREAK, &val, sizeof(unsigned long), 0);
1882                 }
1883                 break;
1884         case TIOCGSOFTCAR:
1885                 if ((rc = verify_area(VERIFY_WRITE, (void *) arg, sizeof(long))) == 0)
1886                         put_fs_long(((tty->termios->c_cflag & CLOCAL) ? 1 : 0), (unsigned long *) arg);
1887                 break;
1888         case TIOCSSOFTCAR:
1889                 if ((rc = verify_area(VERIFY_READ, (void *) arg, sizeof(long))) == 0) {
1890                         arg = get_fs_long((unsigned long *) arg);
1891                         tty->termios->c_cflag = (tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0);
1892                 }
1893                 break;
1894         case TIOCMGET:
1895                 if ((rc = verify_area(VERIFY_WRITE, (void *) arg, sizeof(unsigned int))) == 0) {
1896                         if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS, &portp->asig, sizeof(asysigs_t), 1)) < 0)
1897                                 return(rc);
1898                         val = stli_mktiocm(portp->asig.sigvalue);
1899                         put_fs_long(val, (unsigned long *) arg);
1900                 }
1901                 break;
1902         case TIOCMBIS:
1903                 if ((rc = verify_area(VERIFY_READ, (void *) arg, sizeof(long))) == 0) {
1904                         arg = get_fs_long((unsigned long *) arg);
1905                         stli_mkasysigs(&portp->asig, ((arg & TIOCM_DTR) ? 1 : -1), ((arg & TIOCM_RTS) ? 1 : -1));
1906                         rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig, sizeof(asysigs_t), 0);
1907                 }
1908                 break;
1909         case TIOCMBIC:
1910                 if ((rc = verify_area(VERIFY_READ, (void *) arg, sizeof(long))) == 0) {
1911                         arg = get_fs_long((unsigned long *) arg);
1912                         stli_mkasysigs(&portp->asig, ((arg & TIOCM_DTR) ? 0 : -1), ((arg & TIOCM_RTS) ? 0 : -1));
1913                         rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig, sizeof(asysigs_t), 0);
1914                 }
1915                 break;
1916         case TIOCMSET:
1917                 if ((rc = verify_area(VERIFY_READ, (void *) arg, sizeof(long))) == 0) {
1918                         arg = get_fs_long((unsigned long *) arg);
1919                         stli_mkasysigs(&portp->asig, ((arg & TIOCM_DTR) ? 1 : 0), ((arg & TIOCM_RTS) ? 1 : 0));
1920                         rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig, sizeof(asysigs_t), 0);
1921                 }
1922                 break;
1923         case TIOCGSERIAL:
1924                 if ((rc = verify_area(VERIFY_WRITE, (void *) arg, sizeof(struct serial_struct))) == 0)
1925                         stli_getserial(portp, (struct serial_struct *) arg);
1926                 break;
1927         case TIOCSSERIAL:
1928                 if ((rc = verify_area(VERIFY_READ, (void *) arg, sizeof(struct serial_struct))) == 0)
1929                         rc = stli_setserial(portp, (struct serial_struct *) arg);
1930                 break;
1931         case STL_GETPFLAG:
1932                 if ((rc = verify_area(VERIFY_WRITE, (void *) arg, sizeof(unsigned long))) == 0)
1933                         put_fs_long(portp->pflag, (unsigned long *) arg);
1934                 break;
1935         case STL_SETPFLAG:
1936                 if ((rc = verify_area(VERIFY_READ, (void *) arg, sizeof(unsigned long))) == 0) {
1937                         portp->pflag = get_fs_long((unsigned long *) arg);
1938                         stli_setport(portp);
1939                 }
1940                 break;
1941         case TIOCSERCONFIG:
1942         case TIOCSERGWILD:
1943         case TIOCSERSWILD:
1944         case TIOCSERGETLSR:
1945         case TIOCSERGSTRUCT:
1946         case TIOCSERGETMULTI:
1947         case TIOCSERSETMULTI:
1948         default:
1949                 rc = -ENOIOCTLCMD;
1950                 break;
1951         }
1952 
1953         return(rc);
1954 }
1955 
1956 /*****************************************************************************/
1957 
1958 /*
1959  *      This routine assumes that we have user context and can sleep.
1960  *      Looks like it is true for the current ttys implementation..!!
1961  */
1962 
1963 static void stli_settermios(struct tty_struct *tty, struct termios *old)
     /* [previous][next][first][last][top][bottom][index][help] */
1964 {
1965         stliport_t      *portp;
1966         stlibrd_t       *brdp;
1967         struct termios  *tiosp;
1968         asyport_t       aport;
1969 
1970 #if DEBUG
1971         printk("stli_settermios(tty=%x,old=%x)\n", (int) tty, (int) old);
1972 #endif
1973 
1974         if (tty == (struct tty_struct *) NULL)
1975                 return;
1976         portp = tty->driver_data;
1977         if (portp == (stliport_t *) NULL)
1978                 return;
1979         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1980                 return;
1981         brdp = stli_brds[portp->brdnr];
1982         if (brdp == (stlibrd_t *) NULL)
1983                 return;
1984 
1985         tiosp = tty->termios;
1986         if ((tiosp->c_cflag == old->c_cflag) && (tiosp->c_iflag == old->c_iflag))
1987                 return;
1988 
1989         stli_mkasyport(portp, &aport, tiosp);
1990         stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0);
1991         stli_mkasysigs(&portp->asig, ((tiosp->c_cflag & CBAUD) ? 1 : 0), -1);
1992         stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig, sizeof(asysigs_t), 0);
1993         if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0))
1994                 tty->hw_stopped = 0;
1995         if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
1996                 wake_up_interruptible(&portp->open_wait);
1997 }
1998 
1999 /*****************************************************************************/
2000 
2001 /*
2002  *      Attempt to flow control who ever is sending us data. We won't really
2003  *      do any flow control action here. We can't directly, and even if we
2004  *      wanted to we would have to send a command to the slave. The slave
2005  *      knows how to flow control, and will do so when its buffers reach its
2006  *      internal high water marks. So what we will do is set a local state
2007  *      bit that will stop us sending any RX data up from the poll routine
2008  *      (which is the place where RX data from the slave is handled).
2009  */
2010 
2011 static void stli_throttle(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
2012 {
2013         stliport_t      *portp;
2014 
2015 #if DEBUG
2016         printk("stli_throttle(tty=%x)\n", (int) tty);
2017 #endif
2018 
2019         if (tty == (struct tty_struct *) NULL)
2020                 return;
2021         portp = tty->driver_data;
2022         if (portp == (stliport_t *) NULL)
2023                 return;
2024 
2025         set_bit(ST_RXSTOP, &portp->state);
2026 }
2027 
2028 /*****************************************************************************/
2029 
2030 /*
2031  *      Unflow control the device sending us data... That means that all
2032  *      we have to do is clear the RXSTOP state bit. The next poll call
2033  *      will then be able to pass the RX data back up.
2034  */
2035 
2036 static void stli_unthrottle(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
2037 {
2038         stliport_t      *portp;
2039 
2040 #if DEBUG
2041         printk("stli_unthrottle(tty=%x)\n", (int) tty);
2042 #endif
2043 
2044         if (tty == (struct tty_struct *) NULL)
2045                 return;
2046         portp = tty->driver_data;
2047         if (portp == (stliport_t *) NULL)
2048                 return;
2049 
2050         clear_bit(ST_RXSTOP, &portp->state);
2051 }
2052 
2053 /*****************************************************************************/
2054 
2055 /*
2056  *      Stop the transmitter. Basically to do this we will just turn TX
2057  *      interrupts off.
2058  */
2059 
2060 static void stli_stop(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
2061 {
2062         stlibrd_t       *brdp;
2063         stliport_t      *portp;
2064         asyctrl_t       actrl;
2065 
2066 #if DEBUG
2067         printk("stli_stop(tty=%x)\n", (int) tty);
2068 #endif
2069 
2070         if (tty == (struct tty_struct *) NULL)
2071                 return;
2072         portp = tty->driver_data;
2073         if (portp == (stliport_t *) NULL)
2074                 return;
2075         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2076                 return;
2077         brdp = stli_brds[portp->brdnr];
2078         if (brdp == (stlibrd_t *) NULL)
2079                 return;
2080 
2081         memset(&actrl, 0, sizeof(asyctrl_t));
2082         actrl.txctrl = CT_STOPFLOW;
2083 #if 0
2084         stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t));
2085 #endif
2086 }
2087 
2088 /*****************************************************************************/
2089 
2090 /*
2091  *      Start the transmitter again. Just turn TX interrupts back on.
2092  */
2093 
2094 static void stli_start(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
2095 {
2096         stliport_t      *portp;
2097         stlibrd_t       *brdp;
2098         asyctrl_t       actrl;
2099 
2100 #if DEBUG
2101         printk("stli_start(tty=%x)\n", (int) tty);
2102 #endif
2103 
2104         if (tty == (struct tty_struct *) NULL)
2105                 return;
2106         portp = tty->driver_data;
2107         if (portp == (stliport_t *) NULL)
2108                 return;
2109         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2110                 return;
2111         brdp = stli_brds[portp->brdnr];
2112         if (brdp == (stlibrd_t *) NULL)
2113                 return;
2114 
2115         memset(&actrl, 0, sizeof(asyctrl_t));
2116         actrl.txctrl = CT_STARTFLOW;
2117 #if 0
2118         stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t));
2119 #endif
2120 }
2121 
2122 /*****************************************************************************/
2123 
2124 /*
2125  *      Scheduler called hang up routine. This is called from the scheduler,
2126  *      not direct from the driver "poll" routine. We can't call it there
2127  *      since the real local hangup code will enable/disable the board and
2128  *      other things that we can't do while handling the poll. Much easier
2129  *      to deal with it some time later (don't really care when, hangups
2130  *      aren't that time critical).
2131  */
2132 
2133 static void stli_dohangup(void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
2134 {
2135         stliport_t      *portp;
2136 
2137 #if DEBUG
2138         printk("stli_dohangup(portp=%x)\n", (int) arg);
2139 #endif
2140 
2141         portp = (stliport_t *) arg;
2142         if (portp == (stliport_t *) NULL)
2143                 return;
2144         if (portp->tty == (struct tty_struct *) NULL)
2145                 return;
2146         tty_hangup(portp->tty);
2147 }
2148 
2149 /*****************************************************************************/
2150 
2151 /*
2152  *      Hangup this port. This is pretty much like closing the port, only
2153  *      a little more brutal. No waiting for data to drain. Shutdown the
2154  *      port and maybe drop signals. This is rather tricky really. We want
2155  *      to close the port as well.
2156  */
2157 
2158 static void stli_hangup(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
2159 {
2160         stliport_t      *portp;
2161         stlibrd_t       *brdp;
2162         unsigned long   flags;
2163 
2164 #if DEBUG
2165         printk("stli_hangup(tty=%x)\n", (int) tty);
2166 #endif
2167 
2168         if (tty == (struct tty_struct *) NULL)
2169                 return;
2170         portp = tty->driver_data;
2171         if (portp == (stliport_t *) NULL)
2172                 return;
2173         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2174                 return;
2175         brdp = stli_brds[portp->brdnr];
2176         if (brdp == (stlibrd_t *) NULL)
2177                 return;
2178 
2179         portp->flags &= ~ASYNC_INITIALIZED;
2180 
2181         save_flags(flags);
2182         cli();
2183         if (! test_bit(ST_CLOSING, &portp->state))
2184                 stli_rawclose(brdp, portp, 0, 0);
2185         if (tty->termios->c_cflag & HUPCL) {
2186                 stli_mkasysigs(&portp->asig, 0, 0);
2187                 if (test_bit(ST_CMDING, &portp->state)) {
2188                         set_bit(ST_DOSIGS, &portp->state);
2189                         set_bit(ST_DOFLUSHTX, &portp->state);
2190                         set_bit(ST_DOFLUSHRX, &portp->state);
2191                 } else {
2192                         stli_sendcmd(brdp, portp, A_SETSIGNALSF, &portp->asig, sizeof(asysigs_t), 0);
2193                 }
2194         }
2195         restore_flags(flags);
2196 
2197         clear_bit(ST_TXBUSY, &portp->state);
2198         clear_bit(ST_RXSTOP, &portp->state);
2199         set_bit(TTY_IO_ERROR, &tty->flags);
2200         tty->driver_data = (void *) NULL;
2201         portp->tty = (struct tty_struct *) NULL;
2202         portp->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE);
2203         portp->refcount = 0;
2204         wake_up_interruptible(&portp->open_wait);
2205 }
2206 
2207 /*****************************************************************************/
2208 
2209 /*
2210  *      Flush characters from the lower buffer. We may not have user context
2211  *      so we cannot sleep waiting for it to complete. Also we need to check
2212  *      if there is chars for this port in the TX cook buffer, and flush them
2213  *      as well.
2214  */
2215 
2216 static void stli_flushbuffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
2217 {
2218         stliport_t      *portp;
2219         stlibrd_t       *brdp;
2220         unsigned long   ftype, flags;
2221 
2222 #if DEBUG
2223         printk("stli_flushbuffer(tty=%x)\n", (int) tty);
2224 #endif
2225 
2226         if (tty == (struct tty_struct *) NULL)
2227                 return;
2228         portp = tty->driver_data;
2229         if (portp == (stliport_t *) NULL)
2230                 return;
2231         if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2232                 return;
2233         brdp = stli_brds[portp->brdnr];
2234         if (brdp == (stlibrd_t *) NULL)
2235                 return;
2236 
2237         save_flags(flags);
2238         cli();
2239         if (tty == stli_txcooktty) {
2240                 stli_txcooktty = (struct tty_struct *) NULL;
2241                 stli_txcooksize = 0;
2242                 stli_txcookrealsize = 0;
2243         }
2244         if (test_bit(ST_CMDING, &portp->state)) {
2245                 set_bit(ST_DOFLUSHTX, &portp->state);
2246         } else {
2247                 ftype = FLUSHTX;
2248                 if (test_bit(ST_DOFLUSHRX, &portp->state)) {
2249                         ftype |= FLUSHRX;
2250                         clear_bit(ST_DOFLUSHRX, &portp->state);
2251                 }
2252                 stli_sendcmd(brdp, portp, A_FLUSH, &ftype, sizeof(unsigned long), 0);
2253         }
2254         restore_flags(flags);
2255 
2256         wake_up_interruptible(&tty->write_wait);
2257         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
2258                 (tty->ldisc.write_wakeup)(tty);
2259 }
2260 
2261 /*****************************************************************************/
2262 
2263 /*
2264  *      Generic send command routine. This will send a message to the slave,
2265  *      of the specified type with the specified argument. Must be very
2266  *      carefull of data that will be copied out from shared memory -
2267  *      containing command results. The command completion is all done from
2268  *      a poll routine that does not have user coontext. Therefore you cannot
2269  *      copy back directly into user space, or to the kernel stack of a
2270  *      process. This routine does not sleep, so can be called from anywhere.
2271  */
2272 
2273 static void stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
     /* [previous][next][first][last][top][bottom][index][help] */
2274 {
2275         volatile cdkhdr_t       *hdrp;
2276         volatile cdkctrl_t      *cp;
2277         volatile unsigned char  *bits;
2278         unsigned long           flags;
2279 
2280 #if DEBUG
2281         printk("stli_sendcmd(brdp=%x,portp=%x,cmd=%x,arg=%x,size=%d,copyback=%d)\n", (int) brdp, (int) portp, (int) cmd, (int) arg, size, copyback);
2282 #endif
2283 
2284         save_flags(flags);
2285         cli();
2286 
2287         if (test_bit(ST_CMDING, &portp->state)) {
2288                 printk("STALLION: command already busy, cmd=%x!\n", (int) cmd);
2289                 restore_flags(flags);
2290                 return;
2291         }
2292 
2293         EBRDENABLE(brdp);
2294         cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
2295         if (size > 0) {
2296                 memcpy((void *) &(cp->args[0]), arg, size);
2297                 if (copyback) {
2298                         portp->argp = arg;
2299                         portp->argsize = size;
2300                 }
2301         }
2302         cp->status = 0;
2303         cp->cmd = cmd;
2304         hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2305         hdrp->slavereq |= portp->reqbit;
2306         bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset + portp->portidx;
2307         *bits |= portp->portbit;
2308         set_bit(ST_CMDING, &portp->state);
2309         EBRDDISABLE(brdp);
2310         restore_flags(flags);
2311 }
2312 
2313 /*****************************************************************************/
2314 
2315 /*
2316  *      Read data from shared memory. This assumes that the shared memory
2317  *      is enabled and that interrupts are off. Basically we just empty out
2318  *      the shared memory buffer into the tty buffer. Must be carefull to
2319  *      handle the case where we fill up the tty buffer, but still have
2320  *      more chars to unload.
2321  */
2322 
2323 static inline void stli_read(stlibrd_t *brdp, stliport_t *portp)
     /* [previous][next][first][last][top][bottom][index][help] */
2324 {
2325         volatile cdkasyrq_t     *rp;
2326         volatile char           *shbuf;
2327         struct tty_struct       *tty;
2328         unsigned int            head, tail, size;
2329         unsigned int            len, stlen;
2330 
2331 #if DEBUG
2332         printk("stli_read(brdp=%x,portp=%d)\n", (int) brdp, (int) portp);
2333 #endif
2334 
2335         if (test_bit(ST_RXSTOP, &portp->state))
2336                 return;
2337         tty = portp->tty;
2338         if (tty == (struct tty_struct *) NULL)
2339                 return;
2340 
2341         rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2342         head = (unsigned int) rp->head;
2343         if (head != ((unsigned int) rp->head))
2344                 head = (unsigned int) rp->head;
2345         tail = (unsigned int) rp->tail;
2346         size = portp->rxsize;
2347         if (head >= tail) {
2348                 len = head - tail;
2349                 stlen = len;
2350         } else {
2351                 len = size - (tail - head);
2352                 stlen = size - tail;
2353         }
2354 
2355         len = MIN(len, (TTY_FLIPBUF_SIZE - tty->flip.count));
2356         shbuf = (volatile char *) EBRDGETMEMPTR(brdp, portp->rxoffset);
2357 
2358         while (len > 0) {
2359                 stlen = MIN(len, stlen);
2360                 memcpy(tty->flip.char_buf_ptr, (char *) (shbuf + tail), stlen);
2361                 memset(tty->flip.flag_buf_ptr, 0, stlen);
2362                 tty->flip.char_buf_ptr += stlen;
2363                 tty->flip.flag_buf_ptr += stlen;
2364                 tty->flip.count += stlen;
2365 
2366                 len -= stlen;
2367                 tail += stlen;
2368                 if (tail >= size) {
2369                         tail = 0;
2370                         stlen = head;
2371                 }
2372         }
2373         rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2374         rp->tail = tail;
2375 
2376         if (head != tail)
2377                 set_bit(ST_RXING, &portp->state);
2378 
2379         tty_schedule_flip(tty);
2380 }
2381 
2382 /*****************************************************************************/
2383 
2384 /*
2385  *      Set up and carry out any delayed commands. There is only a small set
2386  *      of slave commands that can be done "off-level". So it is not too
2387  *      difficult to deal with them here.
2388  */
2389 
2390 static inline void stli_dodelaycmd(stliport_t *portp, volatile cdkctrl_t *cp)
     /* [previous][next][first][last][top][bottom][index][help] */
2391 {
2392         int     cmd;
2393 
2394         if (test_bit(ST_DOSIGS, &portp->state)) {
2395                 if (test_bit(ST_DOFLUSHTX, &portp->state) && test_bit(ST_DOFLUSHRX, &portp->state))
2396                         cmd = A_SETSIGNALSF;
2397                 else if (test_bit(ST_DOFLUSHTX, &portp->state))
2398                         cmd = A_SETSIGNALSFTX;
2399                 else if (test_bit(ST_DOFLUSHRX, &portp->state))
2400                         cmd = A_SETSIGNALSFRX;
2401                 else
2402                         cmd = A_SETSIGNALS;
2403                 clear_bit(ST_DOFLUSHTX, &portp->state);
2404                 clear_bit(ST_DOFLUSHRX, &portp->state);
2405                 clear_bit(ST_DOSIGS, &portp->state);
2406                 memcpy((void *) &(cp->args[0]), (void *) &portp->asig, sizeof(asysigs_t));
2407                 cp->status = 0;
2408                 cp->cmd = cmd;
2409                 set_bit(ST_CMDING, &portp->state);
2410         } else if (test_bit(ST_DOFLUSHTX, &portp->state) || test_bit(ST_DOFLUSHRX, &portp->state)) {
2411                 cmd = ((test_bit(ST_DOFLUSHTX, &portp->state)) ? FLUSHTX : 0);
2412                 cmd |= ((test_bit(ST_DOFLUSHRX, &portp->state)) ? FLUSHRX : 0);
2413                 clear_bit(ST_DOFLUSHTX, &portp->state);
2414                 clear_bit(ST_DOFLUSHRX, &portp->state);
2415                 memcpy((void *) &(cp->args[0]), (void *) &cmd, sizeof(int));
2416                 cp->status = 0;
2417                 cp->cmd = A_FLUSH;
2418                 set_bit(ST_CMDING, &portp->state);
2419         }
2420 }
2421 
2422 /*****************************************************************************/
2423 
2424 /*
2425  *      Host command service checking. This handles commands or messages
2426  *      coming from the slave to the host. Must have board shared memory
2427  *      enabled and interrupts off when called. Notice that by servicing the
2428  *      read data last we don't need to change the shared memory pointer
2429  *      during processing (which is a slow IO operation).
2430  */
2431 
2432 static inline int stli_hostcmd(stlibrd_t *brdp, int channr)
     /* [previous][next][first][last][top][bottom][index][help] */
2433 {
2434         volatile cdkasy_t       *ap;
2435         volatile cdkctrl_t      *cp;
2436         struct tty_struct       *tty;
2437         asynotify_t             nt;
2438         stliport_t              *portp;
2439         unsigned long           oldsigs;
2440         int                     rc, donerx;
2441 
2442 #if DEBUG
2443         printk("stli_hostcmd(brdp=%x,channr=%d)\n", (int) brdp, channr);
2444 #endif
2445 
2446         portp = brdp->ports[(channr - 1)];
2447         ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
2448         cp = &ap->ctrl;
2449 
2450 /*
2451  *      Check if we are waiting for an open completion message.
2452  */
2453         if (test_bit(ST_OPENING, &portp->state)) {
2454                 rc = (int) cp->openarg;
2455                 if ((cp->open == 0) && (rc != 0)) {
2456                         if (rc > 0)
2457                                 rc--;
2458                         cp->openarg = 0;
2459                         portp->rc = rc;
2460                         clear_bit(ST_OPENING, &portp->state);
2461                         wake_up_interruptible(&portp->raw_wait);
2462                 }
2463         }
2464 
2465 /*
2466  *      Check if we are waiting for a close completion message.
2467  */
2468         if (test_bit(ST_CLOSING, &portp->state)) {
2469                 rc = (int) cp->closearg;
2470                 if ((cp->close == 0) && (rc != 0)) {
2471                         if (rc > 0)
2472                                 rc--;
2473                         cp->closearg = 0;
2474                         portp->rc = rc;
2475                         clear_bit(ST_CLOSING, &portp->state);
2476                         wake_up_interruptible(&portp->raw_wait);
2477                 }
2478         }
2479 
2480 /*
2481  *      Check if we are waiting for a command completion message. We may
2482  *      need to copy out the command results associated with this command.
2483  */
2484         if (test_bit(ST_CMDING, &portp->state)) {
2485                 rc = cp->status;
2486                 if ((cp->cmd == 0) && (rc != 0)) {
2487                         if (rc > 0)
2488                                 rc--;
2489                         if (portp->argp != (void *) NULL) {
2490                                 memcpy(portp->argp, (void *) &(cp->args[0]), portp->argsize);
2491                                 portp->argp = (void *) NULL;
2492                         }
2493                         cp->status = 0;
2494                         portp->rc = rc;
2495                         clear_bit(ST_CMDING, &portp->state);
2496                         stli_dodelaycmd(portp, cp);
2497                         wake_up_interruptible(&portp->raw_wait);
2498                 }
2499         }
2500 
2501 /*
2502  *      Check for any notification messages ready. This includes lots of
2503  *      different types of events - RX chars ready, RX break received,
2504  *      TX data low or empty in the slave, modem signals changed state.
2505  */
2506         donerx = 0;
2507 
2508         if (ap->notify) {
2509                 nt = ap->changed;
2510                 ap->notify = 0;
2511                 tty = portp->tty;
2512 
2513                 if (nt.signal & SG_DCD) {
2514                         oldsigs = portp->sigs;
2515                         portp->sigs = stli_mktiocm(nt.sigvalue);
2516                         clear_bit(ST_GETSIGS, &portp->state);
2517                         if ((portp->sigs & TIOCM_CD) && ((oldsigs & TIOCM_CD) == 0))
2518                                 wake_up_interruptible(&portp->open_wait);
2519                         if ((oldsigs & TIOCM_CD) && ((portp->sigs & TIOCM_CD) == 0)) {
2520                                 if (! ((portp->flags & ASYNC_CALLOUT_ACTIVE) &&
2521                                                 (portp->flags & ASYNC_CALLOUT_NOHUP))) {
2522                                         if (tty != (struct tty_struct *) NULL)
2523                                                 queue_task_irq_off(&portp->tqhangup, &tq_scheduler);
2524                                 }
2525                         }
2526                 }
2527 
2528                 if (nt.data & DT_TXEMPTY)
2529                         clear_bit(ST_TXBUSY, &portp->state);
2530                 if (nt.data & (DT_TXEMPTY | DT_TXLOW)) {
2531                         if (tty != (struct tty_struct *) NULL) {
2532                                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
2533                                         (tty->ldisc.write_wakeup)(tty);
2534                                 wake_up_interruptible(&tty->write_wait);
2535                         }
2536                 }
2537 
2538                 if ((nt.data & DT_RXBREAK) && (portp->rxmarkmsk & BRKINT)) {
2539                         if (tty != (struct tty_struct *) NULL) {
2540                                 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
2541                                         tty->flip.count++;
2542                                         *tty->flip.flag_buf_ptr++ = TTY_BREAK;
2543                                         *tty->flip.char_buf_ptr++ = 0;
2544 #ifndef MODULE
2545                                         if (portp->flags & ASYNC_SAK)
2546                                                 do_SAK(tty);
2547 #endif
2548                                         tty_schedule_flip(tty);
2549                                 }
2550                         }
2551                 }
2552 
2553                 if (nt.data & DT_RXBUSY) {
2554                         donerx++;
2555                         stli_read(brdp, portp);
2556                 }
2557         }
2558 
2559 /*
2560  *      It might seem odd that we are checking for more RX chars here.
2561  *      But, we need to handle the case where the tty buffer was previously
2562  *      filled, but we had more characters to pass up. The slave will not
2563  *      send any more RX notify messages until the RX buffer has been emptied.
2564  *      But it will leave the service bits on (since the buffer is not empty).
2565  *      So from here we can try to process more RX chars.
2566  */
2567         if ((!donerx) && test_bit(ST_RXING, &portp->state)) {
2568                 clear_bit(ST_RXING, &portp->state);
2569                 stli_read(brdp, portp);
2570         }
2571 
2572         return(0);
2573 }
2574 
2575 /*****************************************************************************/
2576 
2577 /*
2578  *      Driver poll routine. This routine polls the boards in use and passes
2579  *      messages back up to host when neccesary. This is actually very
2580  *      CPU efficient, since we will always have the kernel poll clock, it
2581  *      adds only a few cycles when idle (since board service can be
2582  *      determined very easily), but when loaded generates no interrupts
2583  *      (with their expensive associated context change).
2584  */
2585 
2586 static void stli_poll(unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
2587 {
2588         volatile cdkhdr_t       *hdrp;
2589         unsigned char           bits[(STL_MAXCHANS / 8) + 1];
2590         unsigned char           hostreq, slavereq;
2591         stliport_t              *portp;
2592         stlibrd_t               *brdp;
2593         int                     bitpos, bitat, bitsize;
2594         int                     brdnr, channr, nrdevs;
2595 
2596         stli_timerlist.expires = STLI_TIMEOUT;
2597         add_timer(&stli_timerlist);
2598 
2599 /*
2600  *      Check each board and do any servicing required.
2601  */
2602         for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
2603                 brdp = stli_brds[brdnr];
2604                 if (brdp == (stlibrd_t *) NULL)
2605                         continue;
2606                 if ((brdp->state & BST_STARTED) == 0)
2607                         continue;
2608 
2609                 EBRDENABLE(brdp);
2610                 hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2611                 hostreq = hdrp->hostreq;
2612                 slavereq = hdrp->slavereq;
2613                 bitsize = brdp->bitsize;
2614                 nrdevs = brdp->nrdevs;
2615 
2616 /*
2617  *              Check if slave wants any service. Basically we try to do as
2618  *              little work as possible here. There are 2 levels of service
2619  *              bits. So if there is nothing to do we bail early. We check
2620  *              8 service bits at a time in the inner loop, so we can bypass
2621  *              the lot if none of them want service.
2622  */
2623                 if (hostreq) {
2624                         memcpy(&bits[0], (((unsigned char *) hdrp) + brdp->hostoffset), bitsize);
2625 
2626                         for (bitpos = 0; (bitpos < bitsize); bitpos++) {
2627                                 if (bits[bitpos] == 0)
2628                                         continue;
2629                                 channr = bitpos * 8;
2630                                 for (bitat = 0x1; (channr < nrdevs); channr++, bitat <<= 1) {
2631                                         if (bits[bitpos] & bitat) {
2632                                                 stli_hostcmd(brdp, channr);
2633                                         }
2634                                 }
2635                         }
2636                 }
2637 
2638 /*
2639  *              Check if any of the out-standing host commands have completed.
2640  *              It is a bit unfortunate that we need to check stuff that we
2641  *              initiated!  This ain't pretty, but it needs to be fast.
2642  */
2643                 if (slavereq) {
2644                         slavereq = 0;
2645                         hostreq = 0;
2646                         memcpy(&bits[0], (((unsigned char *) hdrp) + brdp->slaveoffset), bitsize);
2647 
2648                         for (bitpos = 0; (bitpos < bitsize); bitpos++) {
2649                                 if (bits[bitpos] == 0)
2650                                         continue;
2651                                 channr = bitpos * 8;
2652                                 for (bitat = 0x1; (channr < nrdevs); channr++, bitat <<= 1) {
2653                                         if (bits[bitpos] & bitat) {
2654                                                 portp = brdp->ports[(channr - 1)];
2655                                                 if (test_bit(ST_OPENING, &portp->state) ||
2656                                                                 test_bit(ST_CLOSING, &portp->state) ||
2657                                                                 test_bit(ST_CMDING, &portp->state) ||
2658                                                                 test_bit(ST_TXBUSY, &portp->state)) {
2659                                                         slavereq |= portp->reqbit;
2660                                                 } else {
2661                                                         bits[bitpos] &= ~bitat;
2662                                                         hostreq++;
2663                                                 }
2664                                         }
2665                                 }
2666                         }
2667                         hdrp->slavereq = slavereq;
2668                         if (hostreq)
2669                                 memcpy((((unsigned char *) hdrp) + brdp->slaveoffset), &bits[0], bitsize);
2670                 }
2671 
2672                 EBRDDISABLE(brdp);
2673         }
2674 }
2675 
2676 /*****************************************************************************/
2677 
2678 /*
2679  *      Translate the termios settings into the port setting structure of
2680  *      the slave.
2681  */
2682 
2683 static void stli_mkasyport(stliport_t *portp, asyport_t *pp, struct termios *tiosp)
     /* [previous][next][first][last][top][bottom][index][help] */
2684 {
2685 #if DEBUG
2686         printk("stli_mkasyport(portp=%x,pp=%x,tiosp=%d)\n", (int) portp, (int) pp, (int) tiosp);
2687 #endif
2688 
2689         memset(pp, 0, sizeof(asyport_t));
2690 
2691 /*
2692  *      Start of by setting the baud, char size, parity and stop bit info.
2693  */
2694         pp->baudout = tiosp->c_cflag & CBAUD;
2695         if (pp->baudout & CBAUDEX) {
2696                 pp->baudout &= ~CBAUDEX;
2697                 if ((pp->baudout < 1) || (pp->baudout > 2))
2698                         tiosp->c_cflag &= ~CBAUDEX;
2699                 else
2700                         pp->baudout += 15;
2701         }
2702         pp->baudout = stli_baudrates[pp->baudout];
2703         if ((tiosp->c_cflag & CBAUD) == B38400) {
2704                 if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
2705                         pp->baudout = 57600;
2706                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
2707                         pp->baudout = 115200;
2708                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
2709                         pp->baudout = (portp->baud_base / portp->custom_divisor);
2710         }
2711         if (pp->baudout > STL_MAXBAUD)
2712                 pp->baudout = STL_MAXBAUD;
2713         pp->baudin = pp->baudout;
2714 
2715         switch (tiosp->c_cflag & CSIZE) {
2716         case CS5:
2717                 pp->csize = 5;
2718                 break;
2719         case CS6:
2720                 pp->csize = 6;
2721                 break;
2722         case CS7:
2723                 pp->csize = 7;
2724                 break;
2725         default:
2726                 pp->csize = 8;
2727                 break;
2728         }
2729 
2730         if (tiosp->c_cflag & CSTOPB)
2731                 pp->stopbs = PT_STOP2;
2732         else
2733                 pp->stopbs = PT_STOP1;
2734 
2735         if (tiosp->c_cflag & PARENB) {
2736                 if (tiosp->c_cflag & PARODD)
2737                         pp->parity = PT_ODDPARITY;
2738                 else
2739                         pp->parity = PT_EVENPARITY;
2740         } else {
2741                 pp->parity = PT_NOPARITY;
2742         }
2743 
2744 /*
2745  *      Set up any flow control options enabled.
2746  */
2747         if (tiosp->c_iflag & IXON) {
2748                 pp->flow |= F_IXON;
2749                 if (tiosp->c_iflag & IXANY)
2750                         pp->flow |= F_IXANY;
2751         }
2752         if (tiosp->c_cflag & CRTSCTS)
2753                 pp->flow |= (F_RTSFLOW | F_CTSFLOW);
2754 
2755         pp->startin = tiosp->c_cc[VSTART];
2756         pp->stopin = tiosp->c_cc[VSTOP];
2757         pp->startout = tiosp->c_cc[VSTART];
2758         pp->stopout = tiosp->c_cc[VSTOP];
2759 
2760 /*
2761  *      Set up the RX char marking mask with those RX error types we must
2762  *      catch. We can get the slave to help us out a little here, it will
2763  *      ignore parity errors and breaks for us, and mark parity errors in
2764  *      the data stream.
2765  */
2766         if (tiosp->c_iflag & IGNPAR)
2767                 pp->iflag |= FI_IGNRXERRS;
2768         if (tiosp->c_iflag & IGNBRK)
2769                 pp->iflag |= FI_IGNBREAK;
2770 
2771         portp->rxmarkmsk = 0;
2772         if (tiosp->c_iflag & (INPCK | PARMRK))
2773                 pp->iflag |= FI_1MARKRXERRS;
2774         if (tiosp->c_iflag & BRKINT)
2775                 portp->rxmarkmsk |= BRKINT;
2776 
2777 /*
2778  *      Transfer any persistent flags into the asyport structure.
2779  */
2780         pp->pflag = portp->pflag;
2781 }
2782 
2783 /*****************************************************************************/
2784 
2785 /*
2786  *      Construct a slave signals structure for setting the DTR and RTS
2787  *      signals as specified.
2788  */
2789 
2790 static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts)
     /* [previous][next][first][last][top][bottom][index][help] */
2791 {
2792 #if DEBUG
2793         printk("stli_mkasysigs(sp=%x,dtr=%d,rts=%d)\n", (int) sp, dtr, rts);
2794 #endif
2795 
2796         memset(sp, 0, sizeof(asysigs_t));
2797         if (dtr >= 0) {
2798                 sp->signal |= SG_DTR;
2799                 sp->sigvalue |= ((dtr > 0) ? SG_DTR : 0);
2800         }
2801         if (rts >= 0) {
2802                 sp->signal |= SG_RTS;
2803                 sp->sigvalue |= ((rts > 0) ? SG_RTS : 0);
2804         }
2805 }
2806 
2807 /*****************************************************************************/
2808 
2809 /*
2810  *      Convert the signals returned from the slave into a local TIOCM type
2811  *      signals value. We keep them localy in TIOCM format.
2812  */
2813 
2814 static long stli_mktiocm(unsigned long sigvalue)
     /* [previous][next][first][last][top][bottom][index][help] */
2815 {
2816         long    tiocm;
2817 
2818 #if DEBUG
2819         printk("stli_mktiocm(sigvalue=%x)\n", (int) sigvalue);
2820 #endif
2821 
2822         tiocm = 0;
2823         tiocm |= ((sigvalue & SG_DCD) ? TIOCM_CD : 0);
2824         tiocm |= ((sigvalue & SG_CTS) ? TIOCM_CTS : 0);
2825         tiocm |= ((sigvalue & SG_RI) ? TIOCM_RI : 0);
2826         tiocm |= ((sigvalue & SG_DSR) ? TIOCM_DSR : 0);
2827         tiocm |= ((sigvalue & SG_DTR) ? TIOCM_DTR : 0);
2828         tiocm |= ((sigvalue & SG_RTS) ? TIOCM_RTS : 0);
2829         return(tiocm);
2830 }
2831 
2832 /*****************************************************************************/
2833 
2834 /*
2835  *      All panels and ports actually attached have been worked out. All
2836  *      we need to do here is set up the appropriate per port data structures.
2837  */
2838 
2839 static int stli_initports(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
2840 {
2841         stliport_t      *portp;
2842         int             i, panelnr, panelport;
2843 
2844 #if DEBUG
2845         printk("stli_initports(brdp=%x)\n", (int) brdp);
2846 #endif
2847 
2848         for (i = 0, panelnr = 0, panelport = 0; (i < brdp->nrports); i++) {
2849                 portp = (stliport_t *) stli_memalloc(sizeof(stliport_t));
2850                 if (portp == (stliport_t *) NULL) {
2851                         printk("STALLION: failed to allocate port structure\n");
2852                         continue;
2853                 }
2854 
2855                 memset(portp, 0, sizeof(stliport_t));
2856                 portp->portnr = i;
2857                 portp->brdnr = brdp->brdnr;
2858                 portp->panelnr = panelnr;
2859                 portp->baud_base = STL_BAUDBASE;
2860                 portp->close_delay = STL_CLOSEDELAY;
2861                 portp->closing_wait = 30 * HZ;
2862                 portp->tqhangup.routine = stli_dohangup;
2863                 portp->tqhangup.data = portp;
2864                 portp->normaltermios = stli_deftermios;
2865                 portp->callouttermios = stli_deftermios;
2866                 panelport++;
2867                 if (panelport >= brdp->panels[panelnr]) {
2868                         panelport = 0;
2869                         panelnr++;
2870                 }
2871                 brdp->ports[i] = portp;
2872         }
2873 
2874         return(0);
2875 }
2876 
2877 /*****************************************************************************/
2878 
2879 /*
2880  *      All the following routines are board specific hardware operations.
2881  */
2882 
2883 static void stli_ecpinit(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
2884 {
2885         unsigned long   memconf;
2886 
2887 #if DEBUG
2888         printk("stli_ecpinit(brdp=%d)\n", (int) brdp);
2889 #endif
2890 
2891         outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
2892         udelay(10);
2893         outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
2894         udelay(100);
2895 
2896         memconf = (brdp->memaddr & ECP_ATADDRMASK) >> ECP_ATADDRSHFT;
2897         outb(memconf, (brdp->iobase + ECP_ATMEMAR));
2898 }
2899 
2900 /*****************************************************************************/
2901 
2902 static void stli_ecpenable(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
2903 {       
2904 #if DEBUG
2905         printk("stli_ecpenable(brdp=%x)\n", (int) brdp);
2906 #endif
2907         outb(ECP_ATENABLE, (brdp->iobase + ECP_ATCONFR));
2908 }
2909 
2910 /*****************************************************************************/
2911 
2912 static void stli_ecpdisable(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
2913 {       
2914 #if DEBUG
2915         printk("stli_ecpdisable(brdp=%x)\n", (int) brdp);
2916 #endif
2917         outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
2918 }
2919 
2920 /*****************************************************************************/
2921 
2922 static char *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
     /* [previous][next][first][last][top][bottom][index][help] */
2923 {       
2924         void            *ptr;
2925         unsigned char   val;
2926 
2927 #if DEBUG
2928         printk("stli_ecpgetmemptr(brdp=%x,offset=%x)\n", (int) brdp, (int) offset);
2929 #endif
2930 
2931         if (offset > brdp->memsize) {
2932                 printk("STALLION: shared memory pointer=%x out of range at line=%d(%d), brd=%d\n", (int) offset, line, __LINE__, brdp->brdnr);
2933                 ptr = 0;
2934                 val = 0;
2935         } else {
2936                 ptr = brdp->membase + (offset % ECP_ATPAGESIZE);
2937                 val = (unsigned char) (offset / ECP_ATPAGESIZE);
2938         }
2939         outb(val, (brdp->iobase + ECP_ATMEMPR));
2940         return(ptr);
2941 }
2942 
2943 /*****************************************************************************/
2944 
2945 static void stli_ecpreset(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
2946 {       
2947 #if DEBUG
2948         printk("stli_ecpreset(brdp=%x)\n", (int) brdp);
2949 #endif
2950 
2951         outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
2952         udelay(10);
2953         outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
2954         udelay(500);
2955 }
2956 
2957 /*****************************************************************************/
2958 
2959 static void stli_ecpintr(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
2960 {       
2961 #if DEBUG
2962         printk("stli_ecpintr(brdp=%x)\n", (int) brdp);
2963 #endif
2964         outb(0x1, brdp->iobase);
2965 }
2966 
2967 /*****************************************************************************/
2968 
2969 /*
2970  *      The following set of functions act on ECP EISA boards.
2971  */
2972 
2973 static void stli_ecpeiinit(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
2974 {
2975         unsigned long   memconf;
2976 
2977 #if DEBUG
2978         printk("stli_ecpeiinit(brdp=%x)\n", (int) brdp);
2979 #endif
2980 
2981         outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
2982         outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
2983         udelay(10);
2984         outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
2985         udelay(500);
2986 
2987         memconf = (brdp->memaddr & ECP_EIADDRMASKL) >> ECP_EIADDRSHFTL;
2988         outb(memconf, (brdp->iobase + ECP_EIMEMARL));
2989         memconf = (brdp->memaddr & ECP_EIADDRMASKH) >> ECP_EIADDRSHFTH;
2990         outb(memconf, (brdp->iobase + ECP_EIMEMARH));
2991 }
2992 
2993 /*****************************************************************************/
2994 
2995 static void stli_ecpeienable(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
2996 {       
2997         outb(ECP_EIENABLE, (brdp->iobase + ECP_EICONFR));
2998 }
2999 
3000 /*****************************************************************************/
3001 
3002 static void stli_ecpeidisable(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3003 {       
3004         outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3005 }
3006 
3007 /*****************************************************************************/
3008 
3009 static char *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
     /* [previous][next][first][last][top][bottom][index][help] */
3010 {       
3011         void            *ptr;
3012         unsigned char   val;
3013 
3014 #if DEBUG
3015         printk("stli_ecpeigetmemptr(brdp=%x,offset=%x,line=%d)\n", (int) brdp, (int) offset, line);
3016 #endif
3017 
3018         if (offset > brdp->memsize) {
3019                 printk("STALLION: shared memory pointer=%x out of range at line=%d(%d), brd=%d\n", (int) offset, line, __LINE__, brdp->brdnr);
3020                 ptr = 0;
3021                 val = 0;
3022         } else {
3023                 ptr = brdp->membase + (offset % ECP_EIPAGESIZE);
3024                 if (offset < ECP_EIPAGESIZE)
3025                         val = ECP_EIENABLE;
3026                 else
3027                         val = ECP_EIENABLE | 0x40;
3028         }
3029         outb(val, (brdp->iobase + ECP_EICONFR));
3030         return(ptr);
3031 }
3032 
3033 /*****************************************************************************/
3034 
3035 static void stli_ecpeireset(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3036 {       
3037         outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
3038         udelay(10);
3039         outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3040         udelay(500);
3041 }
3042 
3043 /*****************************************************************************/
3044 
3045 /*
3046  *      The following set of functions act on ECP MCA boards.
3047  */
3048 
3049 static void stli_ecpmcenable(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3050 {       
3051         outb(ECP_MCENABLE, (brdp->iobase + ECP_MCCONFR));
3052 }
3053 
3054 /*****************************************************************************/
3055 
3056 static void stli_ecpmcdisable(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3057 {       
3058         outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
3059 }
3060 
3061 /*****************************************************************************/
3062 
3063 static char *stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
     /* [previous][next][first][last][top][bottom][index][help] */
3064 {       
3065         void            *ptr;
3066         unsigned char   val;
3067 
3068         if (offset > brdp->memsize) {
3069                 printk("STALLION: shared memory pointer=%x out of range at line=%d(%d), brd=%d\n", (int) offset, line, __LINE__, brdp->brdnr);
3070                 ptr = 0;
3071                 val = 0;
3072         } else {
3073                 ptr = brdp->membase + (offset % ECP_MCPAGESIZE);
3074                 val = ((unsigned char) (offset / ECP_MCPAGESIZE)) | ECP_MCENABLE;
3075         }
3076         outb(val, (brdp->iobase + ECP_MCCONFR));
3077         return(ptr);
3078 }
3079 
3080 /*****************************************************************************/
3081 
3082 static void stli_ecpmcreset(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3083 {       
3084         outb(ECP_MCSTOP, (brdp->iobase + ECP_MCCONFR));
3085         udelay(10);
3086         outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
3087         udelay(500);
3088 }
3089 
3090 /*****************************************************************************/
3091 
3092 /*
3093  *      The following routines act on ONboards.
3094  */
3095 
3096 static void stli_onbinit(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3097 {
3098         unsigned long   memconf;
3099         int             i;
3100 
3101 #if DEBUG
3102         printk("stli_onbinit(brdp=%d)\n", (int) brdp);
3103 #endif
3104 
3105         outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
3106         udelay(10);
3107         outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
3108         for (i = 0; (i < 1000); i++)
3109                 udelay(1000);
3110 
3111         memconf = (brdp->memaddr & ONB_ATADDRMASK) >> ONB_ATADDRSHFT;
3112         outb(memconf, (brdp->iobase + ONB_ATMEMAR));
3113         outb(0x1, brdp->iobase);
3114         udelay(1000);
3115 }
3116 
3117 /*****************************************************************************/
3118 
3119 static void stli_onbenable(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3120 {       
3121 #if DEBUG
3122         printk("stli_onbenable(brdp=%x)\n", (int) brdp);
3123 #endif
3124         outb((ONB_ATENABLE | ONB_HIMEMENAB), (brdp->iobase + ONB_ATCONFR));
3125 }
3126 
3127 /*****************************************************************************/
3128 
3129 static void stli_onbdisable(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3130 {       
3131 #if DEBUG
3132         printk("stli_onbdisable(brdp=%x)\n", (int) brdp);
3133 #endif
3134         outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
3135 }
3136 
3137 /*****************************************************************************/
3138 
3139 static char *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
     /* [previous][next][first][last][top][bottom][index][help] */
3140 {       
3141         void    *ptr;
3142 
3143 #if DEBUG
3144         printk("stli_onbgetmemptr(brdp=%x,offset=%x)\n", (int) brdp, (int) offset);
3145 #endif
3146 
3147         if (offset > brdp->memsize) {
3148                 printk("STALLION: shared memory pointer=%x out of range at line=%d(%d), brd=%d\n", (int) offset, line, __LINE__, brdp->brdnr);
3149                 ptr = 0;
3150         } else {
3151                 ptr = brdp->membase + (offset % ONB_ATPAGESIZE);
3152         }
3153         return(ptr);
3154 }
3155 
3156 /*****************************************************************************/
3157 
3158 static void stli_onbreset(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3159 {       
3160         int     i;
3161 
3162 #if DEBUG
3163         printk("stli_onbreset(brdp=%x)\n", (int) brdp);
3164 #endif
3165 
3166         outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
3167         udelay(10);
3168         outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
3169         for (i = 0; (i < 1000); i++)
3170                 udelay(1000);
3171 }
3172 
3173 /*****************************************************************************/
3174 
3175 /*
3176  *      The following routines act on ONboard EISA.
3177  */
3178 
3179 static void stli_onbeinit(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3180 {
3181         unsigned long   memconf;
3182         int             i;
3183 
3184 #if DEBUG
3185         printk("stli_onbeinit(brdp=%d)\n", (int) brdp);
3186 #endif
3187 
3188         outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
3189         outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3190         udelay(10);
3191         outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3192         for (i = 0; (i < 1000); i++)
3193                 udelay(1000);
3194 
3195         memconf = (brdp->memaddr & ONB_EIADDRMASKL) >> ONB_EIADDRSHFTL;
3196         outb(memconf, (brdp->iobase + ONB_EIMEMARL));
3197         memconf = (brdp->memaddr & ONB_EIADDRMASKH) >> ONB_EIADDRSHFTH;
3198         outb(memconf, (brdp->iobase + ONB_EIMEMARH));
3199         outb(0x1, brdp->iobase);
3200         udelay(1000);
3201 }
3202 
3203 /*****************************************************************************/
3204 
3205 static void stli_onbeenable(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3206 {       
3207 #if DEBUG
3208         printk("stli_onbeenable(brdp=%x)\n", (int) brdp);
3209 #endif
3210         outb(ONB_EIENABLE, (brdp->iobase + ONB_EICONFR));
3211 }
3212 
3213 /*****************************************************************************/
3214 
3215 static void stli_onbedisable(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3216 {       
3217 #if DEBUG
3218         printk("stli_onbedisable(brdp=%x)\n", (int) brdp);
3219 #endif
3220         outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3221 }
3222 
3223 /*****************************************************************************/
3224 
3225 static char *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
     /* [previous][next][first][last][top][bottom][index][help] */
3226 {       
3227         void            *ptr;
3228         unsigned char   val;
3229 
3230 #if DEBUG
3231         printk("stli_onbegetmemptr(brdp=%x,offset=%x,line=%d)\n", (int) brdp, (int) offset, line);
3232 #endif
3233 
3234         if (offset > brdp->memsize) {
3235                 printk("STALLION: shared memory pointer=%x out of range at line=%d(%d), brd=%d\n", (int) offset, line, __LINE__, brdp->brdnr);
3236                 ptr = 0;
3237                 val = 0;
3238         } else {
3239                 ptr = brdp->membase + (offset % ONB_EIPAGESIZE);
3240                 if (offset < ONB_EIPAGESIZE)
3241                         val = ONB_EIENABLE;
3242                 else
3243                         val = ONB_EIENABLE | 0x40;
3244         }
3245         outb(val, (brdp->iobase + ONB_EICONFR));
3246         return(ptr);
3247 }
3248 
3249 /*****************************************************************************/
3250 
3251 static void stli_onbereset(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3252 {       
3253         int     i;
3254 
3255 #if DEBUG
3256         printk("stli_onbereset(brdp=%x)\n", (int) brdp);
3257 #endif
3258 
3259         outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3260         udelay(10);
3261         outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3262         for (i = 0; (i < 1000); i++)
3263                 udelay(1000);
3264 }
3265 
3266 /*****************************************************************************/
3267 
3268 /*
3269  *      The following routines act on Brumby boards.
3270  */
3271 
3272 static void stli_bbyinit(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3273 {
3274         int     i;
3275 
3276 #if DEBUG
3277         printk("stli_bbyinit(brdp=%d)\n", (int) brdp);
3278 #endif
3279 
3280         outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
3281         udelay(10);
3282         outb(0, (brdp->iobase + BBY_ATCONFR));
3283         for (i = 0; (i < 1000); i++)
3284                 udelay(1000);
3285         outb(0x1, brdp->iobase);
3286         udelay(1000);
3287 }
3288 
3289 /*****************************************************************************/
3290 
3291 static char *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
     /* [previous][next][first][last][top][bottom][index][help] */
3292 {       
3293         void            *ptr;
3294         unsigned char   val;
3295 
3296 #if DEBUG
3297         printk("stli_bbygetmemptr(brdp=%x,offset=%x)\n", (int) brdp, (int) offset);
3298 #endif
3299 
3300         if (offset > brdp->memsize) {
3301                 printk("STALLION: shared memory pointer=%x out of range at line=%d(%d), brd=%d\n", (int) offset, line, __LINE__, brdp->brdnr);
3302                 ptr = 0;
3303                 val = 0;
3304         } else {
3305                 ptr = brdp->membase + (offset % BBY_PAGESIZE);
3306                 val = (unsigned char) (offset / BBY_PAGESIZE);
3307         }
3308         outb(val, (brdp->iobase + BBY_ATCONFR));
3309         return(ptr);
3310 }
3311 
3312 /*****************************************************************************/
3313 
3314 static void stli_bbyreset(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3315 {       
3316         int     i;
3317 
3318 #if DEBUG
3319         printk("stli_bbyreset(brdp=%x)\n", (int) brdp);
3320 #endif
3321 
3322         outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
3323         udelay(10);
3324         outb(0, (brdp->iobase + BBY_ATCONFR));
3325         for (i = 0; (i < 1000); i++)
3326                 udelay(1000);
3327 }
3328 
3329 /*****************************************************************************/
3330 
3331 /*
3332  *      The following routines act on original old Stallion boards.
3333  */
3334 
3335 static void stli_stalinit(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3336 {
3337         int     i;
3338 
3339 #if DEBUG
3340         printk("stli_stalinit(brdp=%d)\n", (int) brdp);
3341 #endif
3342 
3343         outb(0x1, brdp->iobase);
3344         for (i = 0; (i < 1000); i++)
3345                 udelay(1000);
3346 }
3347 
3348 /*****************************************************************************/
3349 
3350 static char *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
     /* [previous][next][first][last][top][bottom][index][help] */
3351 {       
3352         void    *ptr;
3353 
3354 #if DEBUG
3355         printk("stli_stalgetmemptr(brdp=%x,offset=%x)\n", (int) brdp, (int) offset);
3356 #endif
3357 
3358         if (offset > brdp->memsize) {
3359                 printk("STALLION: shared memory pointer=%x out of range at line=%d(%d), brd=%d\n", (int) offset, line, __LINE__, brdp->brdnr);
3360                 ptr = 0;
3361         } else {
3362                 ptr = brdp->membase + (offset % STAL_PAGESIZE);
3363         }
3364         return(ptr);
3365 }
3366 
3367 /*****************************************************************************/
3368 
3369 static void stli_stalreset(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3370 {       
3371         volatile unsigned long  *vecp;
3372         int                     i;
3373 
3374 #if DEBUG
3375         printk("stli_stalreset(brdp=%x)\n", (int) brdp);
3376 #endif
3377 
3378         vecp = (volatile unsigned long *) (brdp->membase + 0x30);
3379         *vecp = 0xffff0000;
3380         outb(0, brdp->iobase);
3381         for (i = 0; (i < 1000); i++)
3382                 udelay(1000);
3383 }
3384 
3385 /*****************************************************************************/
3386 
3387 #if STLI_HIMEMORY
3388  
3389 #define PAGE_IOMEM      __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_PCD)
3390 
3391 /*
3392  *      To support shared memory addresses outside of the lower 1 Mb region
3393  *      we will need to pull some tricks with memory management to map the
3394  *      higher range into kernel virtual address space... Radical stuff...
3395  */
3396 
3397 static void *stli_mapbrdmem(unsigned long physaddr, unsigned int size)
     /* [previous][next][first][last][top][bottom][index][help] */
3398 {
3399         void    *virtaddr;
3400         int     rc;
3401 
3402 #if DEBUG
3403         printk("stli_mapbrdmem(physaddr=%x,size=%x)\n", (int) physaddr, size);
3404 #endif
3405 
3406         if ((virtaddr = vmalloc(size)) == (char *) NULL) {
3407                 printk("STALLION: failed to allocate virtual address space, size=%x\n", size);
3408                 return((void *) NULL);
3409         }
3410         if ((rc = remap_page_range((TASK_SIZE + ((unsigned long) virtaddr)), physaddr, size, PAGE_IOMEM))) {
3411                 printk("STALLION: failed to map phyiscal address=%x, errno=%d\n", (int) physaddr, rc);
3412                 return((void *) NULL);
3413         }
3414         return(virtaddr);
3415 }
3416 
3417 #endif
3418 
3419 /*****************************************************************************/
3420 
3421 /*
3422  *      Try to find an ECP board and initialize it. This handles only ECP
3423  *      board types.
3424  */
3425 
3426 static int stli_initecp(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3427 {
3428         cdkecpsig_t     sig;
3429         cdkecpsig_t     *sigsp;
3430         unsigned int    status, nxtid;
3431         int             panelnr;
3432 
3433 #if DEBUG
3434         printk("stli_initecp(brdp=%x)\n", (int) brdp);
3435 #endif
3436 
3437 /*
3438  *      Do a basic sanity check on the IO and memory addresses.
3439  */
3440         if ((brdp->iobase == 0) || (brdp->memaddr == 0))
3441                 return(-ENODEV);
3442 
3443 /*
3444  *      Based on the specific board type setup the common vars to access
3445  *      and enable shared memory. Set all board specific information now
3446  *      as well.
3447  */
3448         switch (brdp->brdtype) {
3449         case BRD_ECP:
3450                 brdp->membase = (void *) brdp->memaddr;
3451                 brdp->memsize = ECP_MEMSIZE;
3452                 brdp->pagesize = ECP_ATPAGESIZE;
3453                 brdp->init = stli_ecpinit;
3454                 brdp->enable = stli_ecpenable;
3455                 brdp->reenable = stli_ecpenable;
3456                 brdp->disable = stli_ecpdisable;
3457                 brdp->getmemptr = stli_ecpgetmemptr;
3458                 brdp->intr = stli_ecpintr;
3459                 brdp->reset = stli_ecpreset;
3460                 break;
3461 
3462         case BRD_ECPE:
3463                 brdp->membase = (void *) brdp->memaddr;
3464                 brdp->memsize = ECP_MEMSIZE;
3465                 brdp->pagesize = ECP_EIPAGESIZE;
3466                 brdp->init = stli_ecpeiinit;
3467                 brdp->enable = stli_ecpeienable;
3468                 brdp->reenable = stli_ecpeienable;
3469                 brdp->disable = stli_ecpeidisable;
3470                 brdp->getmemptr = stli_ecpeigetmemptr;
3471                 brdp->intr = stli_ecpintr;
3472                 brdp->reset = stli_ecpeireset;
3473                 break;
3474 
3475         case BRD_ECPMC:
3476                 brdp->membase = (void *) brdp->memaddr;
3477                 brdp->memsize = ECP_MEMSIZE;
3478                 brdp->pagesize = ECP_MCPAGESIZE;
3479                 brdp->init = NULL;
3480                 brdp->enable = stli_ecpmcenable;
3481                 brdp->reenable = stli_ecpmcenable;
3482                 brdp->disable = stli_ecpmcdisable;
3483                 brdp->getmemptr = stli_ecpmcgetmemptr;
3484                 brdp->intr = stli_ecpintr;
3485                 brdp->reset = stli_ecpmcreset;
3486                 break;
3487 
3488         default:
3489                 return(-EINVAL);
3490         }
3491 
3492 /*
3493  *      The per-board operations structure is all setup, so now lets go
3494  *      and get the board operational. Firstly initialize board configuration
3495  *      registers. Then if we are using the higher 1Mb support then set up
3496  *      the memory mapping info so we can get at the boards shared memory.
3497  */
3498         EBRDINIT(brdp);
3499 
3500 #if STLI_HIMEMORY
3501         if (brdp->memaddr > 0x100000) {
3502                 brdp->membase = stli_mapbrdmem(brdp->memaddr, brdp->memsize);
3503                 if (brdp->membase == (void *) NULL)
3504                         return(-ENOMEM);
3505         }
3506 #endif
3507 
3508 /*
3509  *      Now that all specific code is set up, enable the shared memory and
3510  *      look for the a signature area that will tell us exactly what board
3511  *      this is, and what it is connected to it.
3512  */
3513         EBRDENABLE(brdp);
3514         sigsp = (cdkecpsig_t *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
3515         memcpy(&sig, sigsp, sizeof(cdkecpsig_t));
3516         EBRDDISABLE(brdp);
3517 
3518 #if 0
3519         printk("%s(%d): sig-> magic=%x romver=%x panel=%x,%x,%x,%x,%x,%x,%x,%x\n",
3520                 __FILE__, __LINE__, (int) sig.magic, sig.romver, sig.panelid[0],
3521                 (int) sig.panelid[1], (int) sig.panelid[2], (int) sig.panelid[3],
3522                 (int) sig.panelid[4], (int) sig.panelid[5], (int) sig.panelid[6],
3523                 (int) sig.panelid[7]);
3524 #endif
3525 
3526         if (sig.magic != ECP_MAGIC)
3527                 return(-ENODEV);
3528 
3529 /*
3530  *      Scan through the signature looking at the panels connected to the
3531  *      board. Calculate the total number of ports as we go.
3532  */
3533         for (panelnr = 0, nxtid = 0; (panelnr < STL_MAXPANELS); panelnr++) {
3534                 status = sig.panelid[nxtid];
3535                 if ((status & ECH_PNLIDMASK) != nxtid)
3536                         break;
3537                 if (status & ECH_PNL16PORT) {
3538                         brdp->panels[panelnr] = 16;
3539                         brdp->nrports += 16;
3540                         nxtid += 2;
3541                 } else {
3542                         brdp->panels[panelnr] = 8;
3543                         brdp->nrports += 8;
3544                         nxtid++;
3545                 }
3546                 brdp->nrpanels++;
3547         }
3548 
3549         request_region(brdp->iobase, ECP_IOSIZE, "serial(ECP)");
3550         brdp->state |= BST_FOUND;
3551         return(0);
3552 }
3553 
3554 /*****************************************************************************/
3555 
3556 /*
3557  *      Try to find an ONboard, Brumby or Stallion board and initialize it.
3558  *      This handles only these board types.
3559  */
3560 
3561 static int stli_initonb(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3562 {
3563         cdkonbsig_t     sig;
3564         cdkonbsig_t     *sigsp;
3565         int             i;
3566 
3567 #if DEBUG
3568         printk("stli_initonb(brdp=%x)\n", (int) brdp);
3569 #endif
3570 
3571 /*
3572  *      Do a basic sanity check on the IO and memory addresses.
3573  */
3574         if ((brdp->iobase == 0) || (brdp->memaddr == 0))
3575                 return(-ENODEV);
3576 
3577 /*
3578  *      Based on the specific board type setup the common vars to access
3579  *      and enable shared memory. Set all board specific information now
3580  *      as well.
3581  */
3582         switch (brdp->brdtype) {
3583         case BRD_ONBOARD:
3584         case BRD_ONBOARD32:
3585         case BRD_ONBOARD2:
3586         case BRD_ONBOARD2_32:
3587         case BRD_ONBOARDRS:
3588                 brdp->membase = (void *) brdp->memaddr;
3589                 brdp->memsize = ONB_MEMSIZE;
3590                 brdp->pagesize = ONB_ATPAGESIZE;
3591                 brdp->init = stli_onbinit;
3592                 brdp->enable = stli_onbenable;
3593                 brdp->reenable = stli_onbenable;
3594                 brdp->disable = stli_onbdisable;
3595                 brdp->getmemptr = stli_onbgetmemptr;
3596                 brdp->intr = stli_ecpintr;
3597                 brdp->reset = stli_onbreset;
3598                 break;
3599 
3600         case BRD_ONBOARDE:
3601                 brdp->membase = (void *) brdp->memaddr;
3602                 brdp->memsize = ONB_EIMEMSIZE;
3603                 brdp->pagesize = ONB_EIPAGESIZE;
3604                 brdp->init = stli_onbeinit;
3605                 brdp->enable = stli_onbeenable;
3606                 brdp->reenable = stli_onbeenable;
3607                 brdp->disable = stli_onbedisable;
3608                 brdp->getmemptr = stli_onbegetmemptr;
3609                 brdp->intr = stli_ecpintr;
3610                 brdp->reset = stli_onbereset;
3611                 break;
3612 
3613         case BRD_BRUMBY4:
3614         case BRD_BRUMBY8:
3615         case BRD_BRUMBY16:
3616                 brdp->membase = (void *) brdp->memaddr;
3617                 brdp->memsize = BBY_MEMSIZE;
3618                 brdp->pagesize = BBY_PAGESIZE;
3619                 brdp->init = stli_bbyinit;
3620                 brdp->enable = NULL;
3621                 brdp->reenable = NULL;
3622                 brdp->disable = NULL;
3623                 brdp->getmemptr = stli_bbygetmemptr;
3624                 brdp->intr = stli_ecpintr;
3625                 brdp->reset = stli_bbyreset;
3626                 break;
3627 
3628         case BRD_STALLION:
3629                 brdp->membase = (void *) brdp->memaddr;
3630                 brdp->memsize = STAL_MEMSIZE;
3631                 brdp->pagesize = STAL_PAGESIZE;
3632                 brdp->init = stli_stalinit;
3633                 brdp->enable = NULL;
3634                 brdp->reenable = NULL;
3635                 brdp->disable = NULL;
3636                 brdp->getmemptr = stli_stalgetmemptr;
3637                 brdp->intr = stli_ecpintr;
3638                 brdp->reset = stli_stalreset;
3639                 break;
3640 
3641         default:
3642                 return(-EINVAL);
3643         }
3644 
3645 /*
3646  *      The per-board operations structure is all setup, so now lets go
3647  *      and get the board operational. Firstly initialize board configuration
3648  *      registers. Then if we are using the higher 1Mb support then set up
3649  *      the memory mapping info so we can get at the boards shared memory.
3650  */
3651         EBRDINIT(brdp);
3652 
3653 #if STLI_HIMEMORY
3654         if (brdp->memaddr > 0x100000) {
3655                 brdp->membase = stli_mapbrdmem(brdp->memaddr, brdp->memsize);
3656                 if (brdp->membase == (void *) NULL)
3657                         return(-ENOMEM);
3658         }
3659 #endif
3660 
3661 /*
3662  *      Now that all specific code is set up, enable the shared memory and
3663  *      look for the a signature area that will tell us exactly what board
3664  *      this is, and how many ports.
3665  */
3666         EBRDENABLE(brdp);
3667         sigsp = (cdkonbsig_t *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
3668         memcpy(&sig, sigsp, sizeof(cdkonbsig_t));
3669         EBRDDISABLE(brdp);
3670 
3671 #if 0
3672         printk("%s(%d): sig-> magic=%x:%x:%x:%x romver=%x amask=%x:%x:%x\n",
3673                 __FILE__, __LINE__, sig.magic0, sig.magic1, sig.magic2,
3674                 sig.magic3, sig.romver, sig.amask0, sig.amask1, sig.amask2);
3675 #endif
3676 
3677         if ((sig.magic0 != ONB_MAGIC0) || (sig.magic1 != ONB_MAGIC1) ||
3678                         (sig.magic2 != ONB_MAGIC2) || (sig.magic3 != ONB_MAGIC3))
3679                 return(-ENODEV);
3680 
3681 /*
3682  *      Scan through the signature alive mask and calculate how many ports
3683  *      there are on this board.
3684  */
3685         brdp->nrpanels = 1;
3686         if (sig.amask1) {
3687                 brdp->nrports = 32;
3688         } else {
3689                 for (i = 0; (i < 16); i++) {
3690                         if (((sig.amask0 << i) & 0x8000) == 0)
3691                                 break;
3692                 }
3693                 brdp->nrports = i;
3694         }
3695 
3696         request_region(brdp->iobase, ONB_IOSIZE, "serial(ONB/BBY)");
3697         brdp->state |= BST_FOUND;
3698         return(0);
3699 }
3700 
3701 /*****************************************************************************/
3702 
3703 /*
3704  *      Start up a running board. This routine is only called after the
3705  *      code has been down loaded to the board and is operational. It will
3706  *      read in the memory map, and get the show on the road...
3707  */
3708 
3709 static int stli_startbrd(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3710 {
3711         volatile cdkhdr_t       *hdrp;
3712         volatile cdkmem_t       *memp;
3713         volatile cdkasy_t       *ap;
3714         unsigned long           flags;
3715         stliport_t              *portp;
3716         int                     portnr, nrdevs, i, rc;
3717 
3718 #if DEBUG
3719         printk("stli_startbrd(brdp=%x)\n", (int) brdp);
3720 #endif
3721 
3722         rc = 0;
3723 
3724         save_flags(flags);
3725         cli();
3726         EBRDENABLE(brdp);
3727         hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
3728         nrdevs = hdrp->nrdevs;
3729 
3730 #if 0
3731         printk("%s(%d): CDK version %d.%d.%d --> nrdevs=%d memp=%x hostp=%x slavep=%x\n",
3732                  __FILE__, __LINE__, hdrp->ver_release, hdrp->ver_modification,
3733                  hdrp->ver_fix, nrdevs, (int) hdrp->memp, (int) hdrp->hostp,
3734                  (int) hdrp->slavep);
3735 #endif
3736 
3737         if (nrdevs < (brdp->nrports + 1)) {
3738                 printk("STALLION: slave failed to allocate memory for all devices, devices=%d\n", nrdevs);
3739                 brdp->nrports = nrdevs - 1;
3740         }
3741         brdp->nrdevs = nrdevs;
3742         brdp->hostoffset = hdrp->hostp - CDK_CDKADDR;
3743         brdp->slaveoffset = hdrp->slavep - CDK_CDKADDR;
3744         brdp->bitsize = (nrdevs + 7) / 8;
3745         memp = (volatile cdkmem_t *) hdrp->memp;
3746         if (((unsigned long) memp) > brdp->memsize) {
3747                 printk("STALLION: corrupted shared memory region?\n");
3748                 rc = -EIO;
3749                 goto stli_donestartup;
3750         }
3751         memp = (volatile cdkmem_t *) EBRDGETMEMPTR(brdp, (unsigned long) memp);
3752         if (memp->dtype != TYP_ASYNCTRL) {
3753                 printk("STALLION: no slave control device found\n");
3754                 goto stli_donestartup;
3755         }
3756         memp++;
3757 
3758 /*
3759  *      Cycle through memory allocation of each port. We are guaranteed to
3760  *      have all ports inside the first page of slave window, so no need to
3761  *      change pages while reading memory map.
3762  */
3763         for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++, memp++) {
3764                 if (memp->dtype != TYP_ASYNC)
3765                         break;
3766                 portp = brdp->ports[portnr];
3767                 if (portp == (stliport_t *) NULL)
3768                         break;
3769                 portp->devnr = i;
3770                 portp->addr = memp->offset;
3771                 portp->reqbit = (unsigned char) (0x1 << (i * 8 / nrdevs));
3772                 portp->portidx = (unsigned char) (i / 8);
3773                 portp->portbit = (unsigned char) (0x1 << (i % 8));
3774         }
3775 
3776 /*
3777  *      For each port setup a local copy of the RX and TX buffer offsets
3778  *      and sizes. We do this separate from the above, because we need to
3779  *      move the shared memory page...
3780  */
3781         for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++) {
3782                 portp = brdp->ports[portnr];
3783                 if (portp == (stliport_t *) NULL)
3784                         break;
3785                 if (portp->addr == 0)
3786                         break;
3787                 ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
3788                 if (ap != (volatile cdkasy_t *) NULL) {
3789                         portp->rxsize = ap->rxq.size;
3790                         portp->txsize = ap->txq.size;
3791                         portp->rxoffset = ap->rxq.offset;
3792                         portp->txoffset = ap->txq.offset;
3793                 }
3794         }
3795 
3796 stli_donestartup:
3797         EBRDDISABLE(brdp);
3798         restore_flags(flags);
3799 
3800         if (rc == 0)
3801                 brdp->state |= BST_STARTED;
3802 
3803         if (! stli_timeron) {
3804                 stli_timeron++;
3805                 stli_timerlist.expires = STLI_TIMEOUT;
3806                 add_timer(&stli_timerlist);
3807         }
3808 
3809         return(rc);
3810 }
3811 
3812 /*****************************************************************************/
3813 
3814 /*
3815  *      Probe and initialize the specified board.
3816  */
3817 
3818 static int stli_brdinit(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3819 {
3820 #if DEBUG
3821         printk("stli_brdinit(brdp=%x)\n", (int) brdp);
3822 #endif
3823 
3824         stli_brds[brdp->brdnr] = brdp;
3825 
3826         switch (brdp->brdtype) {
3827         case BRD_ECP:
3828         case BRD_ECPE:
3829         case BRD_ECPMC:
3830                 stli_initecp(brdp);
3831                 break;
3832         case BRD_ONBOARD:
3833         case BRD_ONBOARDE:
3834         case BRD_ONBOARD2:
3835         case BRD_ONBOARD32:
3836         case BRD_ONBOARD2_32:
3837         case BRD_ONBOARDRS:
3838         case BRD_BRUMBY4:
3839         case BRD_BRUMBY8:
3840         case BRD_BRUMBY16:
3841         case BRD_STALLION:
3842                 stli_initonb(brdp);
3843                 break;
3844         case BRD_EASYIO:
3845         case BRD_ECH:
3846         case BRD_ECHMC:
3847         case BRD_ECHPCI:
3848                 printk("STALLION: %s board type not supported in this driver\n", stli_brdnames[brdp->brdtype]);
3849                 return(ENODEV);
3850         default:
3851                 printk("STALLION: unit=%d is unknown board type=%d\n", brdp->brdnr, brdp->brdtype);
3852                 return(ENODEV);
3853         }
3854 
3855         if ((brdp->state & BST_FOUND) == 0) {
3856                 printk("STALLION: %s board not found, unit=%d io=%x mem=%x\n", stli_brdnames[brdp->brdtype], brdp->brdnr, brdp->iobase, (int) brdp->memaddr);
3857                 return(ENODEV);
3858         }
3859 
3860         stli_initports(brdp);
3861         printk("STALLION: %s found, unit=%d io=%x mem=%x nrpanels=%d nrports=%d\n", stli_brdnames[brdp->brdtype], brdp->brdnr, brdp->iobase, (int) brdp->memaddr, brdp->nrpanels, brdp->nrports);
3862         return(0);
3863 }
3864 
3865 /*****************************************************************************/
3866 
3867 /*
3868  *      Probe around trying to find where the EISA boards shared memory
3869  *      might be. This is a bit if hack, but it is the best we can do.
3870  */
3871 
3872 static int stli_eisamemprobe(stlibrd_t *brdp)
     /* [previous][next][first][last][top][bottom][index][help] */
3873 {
3874         cdkecpsig_t     ecpsig, *ecpsigp;
3875         cdkonbsig_t     onbsig, *onbsigp;
3876         int             i, foundit;
3877 
3878 #if DEBUG
3879         printk("stli_eisamemprobe(brdp=%x)\n", (int) brdp);
3880 #endif
3881 
3882 /*
3883  *      First up we reset the board, to get it into a known state. There
3884  *      is only 2 board types here we need to worry about. Don;t use the
3885  *      standard board init routine here, it programs up the shared
3886  *      memopry address, and we don't know it yet...
3887  */
3888         if (brdp->brdtype == BRD_ECPE) {
3889                 outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
3890                 outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
3891                 udelay(10);
3892                 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3893                 udelay(500);
3894                 stli_ecpeienable(brdp);
3895         } else if (brdp->brdtype == BRD_ONBOARDE) {
3896                 outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
3897                 outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3898                 udelay(10);
3899                 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3900                 for (i = 0; (i < 100); i++)
3901                         udelay(1000);
3902                 outb(0x1, brdp->iobase);
3903                 udelay(1000);
3904                 stli_onbeenable(brdp);
3905         } else {
3906                 return(-ENODEV);
3907         }
3908 
3909         foundit = 0;
3910         brdp->memsize = ECP_MEMSIZE;
3911 
3912 /*
3913  *      Board shared memory is enabled, so now we have a poke around and
3914  *      see if we can find it.
3915  */
3916         for (i = 0; (i < stli_eisamempsize); i++) {
3917                 brdp->memaddr = stli_eisamemprobeaddrs[i];
3918                 brdp->membase = (void *) brdp->memaddr;
3919 #if STLI_HIMEMORY
3920                 if (brdp->memaddr > 0x100000) {
3921                         brdp->membase = stli_mapbrdmem(brdp->memaddr, brdp->memsize);
3922                         if (brdp->membase == (void *) NULL)
3923                                 continue;
3924                 }
3925 #endif
3926                 if (brdp->brdtype == BRD_ECPE) {
3927                         ecpsigp = (cdkecpsig_t *) stli_ecpeigetmemptr(brdp, CDK_SIGADDR, __LINE__);
3928                         memcpy(&ecpsig, ecpsigp, sizeof(cdkecpsig_t));
3929                         if (ecpsig.magic == ECP_MAGIC)
3930                                 foundit = 1;
3931                 } else {
3932                         onbsigp = (cdkonbsig_t *) stli_onbegetmemptr(brdp, CDK_SIGADDR, __LINE__);
3933                         memcpy(&onbsig, onbsigp, sizeof(cdkonbsig_t));
3934                         if ((onbsig.magic0 == ONB_MAGIC0) && (onbsig.magic1 == ONB_MAGIC1) &&
3935                                         (onbsig.magic2 == ONB_MAGIC2) && (onbsig.magic3 == ONB_MAGIC3))
3936                                 foundit = 1;
3937                 }
3938 #if STLI_HIMEMORY
3939                 if (brdp->memaddr >= 0x100000)
3940                         vfree(brdp->membase);
3941 #endif
3942                 if (foundit)
3943                         break;
3944         }
3945 
3946 /*
3947  *      Regardless of whether we found the shared memory or not we must
3948  *      disable the region. After that return success or failure.
3949  */
3950         if (brdp->brdtype == BRD_ECPE)
3951                 stli_ecpeidisable(brdp);
3952         else
3953                 stli_onbedisable(brdp);
3954 
3955         if (! foundit) {
3956                 brdp->memaddr = 0;
3957                 brdp->membase = 0;
3958                 printk("STALLION: failed to probe shared memory region for %s in EISA slot=%d\n", stli_brdnames[brdp->brdtype], (brdp->iobase >> 12));
3959                 return(-ENODEV);
3960         }
3961         return(0);
3962 }
3963 
3964 /*****************************************************************************/
3965 
3966 /*
3967  *      Probe around and try to find any EISA boards in system. The biggest
3968  *      problem here is finding out what memory address is associated with
3969  *      an EISA board after it is found. The registers of the ECPE and
3970  *      ONboardE are not readable - so we can't read them from there. We
3971  *      don't have access to the EISA CMOS (or EISA BIOS) so we don't
3972  *      actually have any way to find out the real value. The best we can
3973  *      do is go probing around in the usual places hoping we can find it.
3974  */
3975 
3976 static int stli_findeisabrds()
     /* [previous][next][first][last][top][bottom][index][help] */
3977 {
3978         stlibrd_t       *brdp;
3979         unsigned int    iobase, eid;
3980         int             i;
3981 
3982 #if DEBUG
3983         printk("stli_findeisabrds()\n");
3984 #endif
3985 
3986 /*
3987  *      Firstly check if this is an EISA system. Do this by probing for
3988  *      the system board EISA ID. If this is not an EISA system then
3989  *      don't bother going any further!
3990  */
3991         outb(0xff, 0xc80);
3992         if (inb(0xc80) == 0xff)
3993                 return(0);
3994 
3995 /*
3996  *      Looks like an EISA system, so go searching for EISA boards.
3997  */
3998         for (iobase = 0x1000; (iobase <= 0xc000); iobase += 0x1000) {
3999                 outb(0xff, (iobase + 0xc80));
4000                 eid = inb(iobase + 0xc80);
4001                 eid |= inb(iobase + 0xc81) << 8;
4002                 if (eid != STL_EISAID)
4003                         continue;
4004 
4005 /*
4006  *              We have found a board. Need to check if this board was
4007  *              statically configured already (just in case!).
4008  */
4009                 for (i = 0; (i < STL_MAXBRDS); i++) {
4010                         brdp = stli_brds[i];
4011                         if (brdp == (stlibrd_t *) NULL)
4012                                 continue;
4013                         if (brdp->iobase == iobase)
4014                                 break;
4015                 }
4016                 if (i < STL_MAXBRDS)
4017                         continue;
4018 
4019 /*
4020  *              Check that we have room for this new board in our board
4021  *              info table.
4022  */
4023                 if (stli_nrbrds >= STL_MAXBRDS) {
4024                         printk("STALLION: no room for more probed boards, maximum supported %d\n", STL_MAXBRDS);
4025                         break;
4026                 }
4027 
4028 /*
4029  *              We have found a Stallion board and it is not configured already.
4030  *              Allocate a board structure and initialize it.
4031  */
4032                 brdp = (stlibrd_t *) stli_memalloc(sizeof(stlibrd_t));
4033                 if (brdp == (stlibrd_t *) NULL) {
4034                         printk("STALLION: failed to allocate memory (size=%d)\n", sizeof(stlibrd_t));
4035                         return(-ENOMEM);
4036                 }
4037                 memset(brdp, 0, sizeof(stlibrd_t));
4038 
4039                 brdp->brdnr = stli_nrbrds++;
4040                 eid = inb(iobase + 0xc82);
4041                 if (eid == ECP_EISAID)
4042                         brdp->brdtype = BRD_ECPE;
4043                 else if (eid == ONB_EISAID)
4044                         brdp->brdtype = BRD_ONBOARDE;
4045                 else
4046                         brdp->brdtype = BRD_UNKNOWN;
4047                 brdp->iobase = iobase;
4048                 outb(0x1, (iobase + 0xc84));
4049                 if (stli_eisamemprobe(brdp))
4050                         outb(0, (iobase + 0xc84));
4051                 stli_brdinit(brdp);
4052         }
4053 
4054         return(0);
4055 }
4056 
4057 /*****************************************************************************/
4058 
4059 /*
4060  *      Scan through all the boards in the configuration and see what we
4061  *      can find.
4062  */
4063 
4064 static int stli_initbrds()
     /* [previous][next][first][last][top][bottom][index][help] */
4065 {
4066         stlibrd_t       *brdp, *nxtbrdp;
4067         stlconf_t       *confp;
4068         int             i, j;
4069 
4070 #if DEBUG
4071         printk("stli_initbrds()\n");
4072 #endif
4073 
4074         if (stli_nrbrds > STL_MAXBRDS) {
4075                 printk("STALLION: too many boards in configuration table, truncating to %d\n", STL_MAXBRDS);
4076                 stli_nrbrds = STL_MAXBRDS;
4077         }
4078 
4079 /*
4080  *      Firstly scan the list of static boards configured. Allocate
4081  *      resources and initialize the boards as found.
4082  */
4083         for (i = 0; (i < stli_nrbrds); i++) {
4084                 confp = &stli_brdconf[i];
4085                 brdp = (stlibrd_t *) stli_memalloc(sizeof(stlibrd_t));
4086                 if (brdp == (stlibrd_t *) NULL) {
4087                         printk("STALLION: failed to allocate memory (size=%d)\n", sizeof(stlibrd_t));
4088                         return(-ENOMEM);
4089                 }
4090                 memset(brdp, 0, sizeof(stlibrd_t));
4091 
4092                 brdp->brdnr = i;
4093                 brdp->brdtype = confp->brdtype;
4094                 brdp->iobase = confp->ioaddr1;
4095                 brdp->memaddr = confp->memaddr;
4096                 stli_brdinit(brdp);
4097         }
4098 
4099 /*
4100  *      Now go probing for EISA boards if enabled.
4101  */
4102         if (stli_eisaprobe)
4103                 stli_findeisabrds();
4104 
4105 /*
4106  *      All found boards are initialized. Now for a little optimization, if
4107  *      no boards are sharing the "shared memory" regions then we can just
4108  *      leave them all enabled. This is in fact the usual case.
4109  */
4110         stli_shared = 0;
4111         if (stli_nrbrds > 1) {
4112                 for (i = 0; (i < stli_nrbrds); i++) {
4113                         brdp = stli_brds[i];
4114                         if (brdp == (stlibrd_t *) NULL)
4115                                 continue;
4116                         for (j = i + 1; (j < stli_nrbrds); j++) {
4117                                 nxtbrdp = stli_brds[j];
4118                                 if (nxtbrdp == (stlibrd_t *) NULL)
4119                                         continue;
4120                                 if ((brdp->membase >= nxtbrdp->membase) && (brdp->membase <= (nxtbrdp->membase + nxtbrdp->memsize - 1))) {
4121                                         stli_shared++;
4122                                         break;
4123                                 }
4124                         }
4125                 }
4126         }
4127 
4128         if (stli_shared == 0) {
4129                 for (i = 0; (i < stli_nrbrds); i++) {
4130                         brdp = stli_brds[i];
4131                         if (brdp == (stlibrd_t *) NULL)
4132                                 continue;
4133                         if (brdp->state & BST_FOUND) {
4134                                 EBRDENABLE(brdp);
4135                                 brdp->enable = NULL;
4136                                 brdp->disable = NULL;
4137                         }
4138                 }
4139         }
4140 
4141         return(0);
4142 }
4143 
4144 /*****************************************************************************/
4145 
4146 /*
4147  *      Code to handle an "staliomem" read operation. This device is the 
4148  *      contents of the board shared memory. It is used for down loading
4149  *      the slave image (and debugging :-)
4150  */
4151 
4152 static int stli_memread(struct inode *ip, struct file *fp, char *buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
4153 {
4154         unsigned long   flags;
4155         void            *memptr;
4156         stlibrd_t       *brdp;
4157         int             brdnr, size, n;
4158 
4159 #if DEBUG
4160         printk("stli_memread(ip=%x,fp=%x,buf=%x,count=%d)\n", (int) ip, (int) fp, (int) buf, count);
4161 #endif
4162 
4163         brdnr = MINOR(ip->i_rdev);
4164         if (brdnr >= stli_nrbrds)
4165                 return(-ENODEV);
4166         brdp = stli_brds[brdnr];
4167         if (brdp == (stlibrd_t *) NULL)
4168                 return(-ENODEV);
4169         if (brdp->state == 0)
4170                 return(-ENODEV);
4171         if (fp->f_pos >= brdp->memsize)
4172                 return(0);
4173 
4174         size = MIN(count, (brdp->memsize - fp->f_pos));
4175 
4176         save_flags(flags);
4177         cli();
4178         EBRDENABLE(brdp);
4179         while (size > 0) {
4180                 memptr = (void *) EBRDGETMEMPTR(brdp, fp->f_pos);
4181                 n = MIN(size, (brdp->pagesize - (((unsigned long) fp->f_pos) % brdp->pagesize)));
4182                 memcpy_tofs(buf, memptr, n);
4183                 fp->f_pos += n;
4184                 buf += n;
4185                 size -= n;
4186         }
4187         EBRDDISABLE(brdp);
4188         restore_flags(flags);
4189 
4190         return(count);
4191 }
4192 
4193 /*****************************************************************************/
4194 
4195 /*
4196  *      Code to handle an "staliomem" write operation. This device is the 
4197  *      contents of the board shared memory. It is used for down loading
4198  *      the slave image (and debugging :-)
4199  */
4200 
4201 static int stli_memwrite(struct inode *ip, struct file *fp, const char *buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
4202 {
4203         unsigned long   flags;
4204         void            *memptr;
4205         stlibrd_t       *brdp;
4206         char            *chbuf;
4207         int             brdnr, size, n;
4208 
4209 #if DEBUG
4210         printk("stli_memwrite(ip=%x,fp=%x,buf=%x,count=%x)\n", (int) ip, (int) fp, (int) buf, count);
4211 #endif
4212 
4213         brdnr = MINOR(ip->i_rdev);
4214         if (brdnr >= stli_nrbrds)
4215                 return(-ENODEV);
4216         brdp = stli_brds[brdnr];
4217         if (brdp == (stlibrd_t *) NULL)
4218                 return(-ENODEV);
4219         if (brdp->state == 0)
4220                 return(-ENODEV);
4221         if (fp->f_pos >= brdp->memsize)
4222                 return(0);
4223 
4224         chbuf = (char *) buf;
4225         size = MIN(count, (brdp->memsize - fp->f_pos));
4226 
4227         save_flags(flags);
4228         cli();
4229         EBRDENABLE(brdp);
4230         while (size > 0) {
4231                 memptr = (void *) EBRDGETMEMPTR(brdp, fp->f_pos);
4232                 n = MIN(size, (brdp->pagesize - (((unsigned long) fp->f_pos) % brdp->pagesize)));
4233                 memcpy_fromfs(memptr, chbuf, n);
4234                 fp->f_pos += n;
4235                 chbuf += n;
4236                 size -= n;
4237         }
4238         EBRDDISABLE(brdp);
4239         restore_flags(flags);
4240 
4241         return(count);
4242 }
4243 
4244 /*****************************************************************************/
4245 
4246 /*
4247  *      The "staliomem" device is also required to do some special operations on
4248  *      the board. We need to be able to send an interrupt to the board,
4249  *      reset it, and start/stop it.
4250  */
4251 
4252 static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
4253 {
4254         stlibrd_t       *brdp;
4255         int             brdnr, rc;
4256 
4257 #if DEBUG
4258         printk("stli_memioctl(ip=%x,fp=%x,cmd=%x,arg=%x)\n", (int) ip, (int) fp, cmd, (int) arg);
4259 #endif
4260 
4261         brdnr = MINOR(ip->i_rdev);
4262         if (brdnr >= stli_nrbrds)
4263                 return(-ENODEV);
4264         brdp = stli_brds[brdnr];
4265         if (brdp == (stlibrd_t *) NULL)
4266                 return(-ENODEV);
4267         if (brdp->state == 0)
4268                 return(-ENODEV);
4269 
4270         rc = 0;
4271 
4272         switch (cmd) {
4273         case STL_BINTR:
4274                 EBRDINTR(brdp);
4275                 break;
4276         case STL_BSTART:
4277                 rc = stli_startbrd(brdp);
4278                 break;
4279         case STL_BSTOP:
4280                 brdp->state &= ~BST_STARTED;
4281                 break;
4282         case STL_BRESET:
4283                 brdp->state &= ~BST_STARTED;
4284                 EBRDRESET(brdp);
4285                 if (stli_shared == 0) {
4286                         if (brdp->reenable != NULL)
4287                                 (* brdp->reenable)(brdp);
4288                 }
4289                 break;
4290         default:
4291                 rc = -ENOIOCTLCMD;
4292                 break;
4293         }
4294 
4295         return(rc);
4296 }
4297 
4298 /*****************************************************************************/
4299 
4300 int stli_init()
     /* [previous][next][first][last][top][bottom][index][help] */
4301 {
4302         printk("%s: version %s\n", stli_drvname, stli_drvversion);
4303 
4304         stli_initbrds();
4305 
4306 /*
4307  *      Allocate a temporary write buffer.
4308  */
4309         stli_tmpwritebuf = (char *) stli_memalloc(STLI_TXBUFSIZE);
4310         if (stli_tmpwritebuf == (char *) NULL)
4311                 printk("STALLION: failed to allocate memory (size=%d)\n", STLI_TXBUFSIZE);
4312         stli_txcookbuf = (char *) stli_memalloc(STLI_TXBUFSIZE);
4313         if (stli_txcookbuf == (char *) NULL)
4314                 printk("STALLION: failed to allocate memory (size=%d)\n", STLI_TXBUFSIZE);
4315 
4316 /*
4317  *      Set up a character driver for the shared memory region. We need this
4318  *      to down load the slave code image. Also it is a useful debugging tool.
4319  */
4320         if (register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stli_fsiomem))
4321                 printk("STALLION: failed to register serial memory device\n");
4322 
4323 /*
4324  *      Set up the tty driver structure and register us as a driver.
4325  *      Also setup the callout tty device.
4326  */
4327         memset(&stli_serial, 0, sizeof(struct tty_driver));
4328         stli_serial.magic = TTY_DRIVER_MAGIC;
4329         stli_serial.name = stli_serialname;
4330         stli_serial.major = STL_SERIALMAJOR;
4331         stli_serial.minor_start = 0;
4332         stli_serial.num = STL_MAXBRDS * STL_MAXPORTS;
4333         stli_serial.type = TTY_DRIVER_TYPE_SERIAL;
4334         stli_serial.subtype = STL_DRVTYPSERIAL;
4335         stli_serial.init_termios = stli_deftermios;
4336         stli_serial.flags = TTY_DRIVER_REAL_RAW;
4337         stli_serial.refcount = &stli_refcount;
4338         stli_serial.table = stli_ttys;
4339         stli_serial.termios = stli_termios;
4340         stli_serial.termios_locked = stli_termioslocked;
4341         
4342         stli_serial.open = stli_open;
4343         stli_serial.close = stli_close;
4344         stli_serial.write = stli_write;
4345         stli_serial.put_char = stli_putchar;
4346         stli_serial.flush_chars = stli_flushchars;
4347         stli_serial.write_room = stli_writeroom;
4348         stli_serial.chars_in_buffer = stli_charsinbuffer;
4349         stli_serial.ioctl = stli_ioctl;
4350         stli_serial.set_termios = stli_settermios;
4351         stli_serial.throttle = stli_throttle;
4352         stli_serial.unthrottle = stli_unthrottle;
4353         stli_serial.stop = stli_stop;
4354         stli_serial.start = stli_start;
4355         stli_serial.hangup = stli_hangup;
4356         stli_serial.flush_buffer = stli_flushbuffer;
4357 
4358         stli_callout = stli_serial;
4359         stli_callout.name = stli_calloutname;
4360         stli_callout.major = STL_CALLOUTMAJOR;
4361         stli_callout.subtype = STL_DRVTYPCALLOUT;
4362 
4363         if (tty_register_driver(&stli_serial))
4364                 printk("STALLION: failed to register serial driver\n");
4365         if (tty_register_driver(&stli_callout))
4366                 printk("STALLION: failed to register callout driver\n");
4367 
4368         return(0);
4369 }
4370 
4371 /*****************************************************************************/

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