root/drivers/char/cyclades.c

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

DEFINITIONS

This source file includes following definitions.
  1. serial_paranoia_check
  2. SP
  3. CP
  4. CP1
  5. CP2
  6. CP4
  7. CP8
  8. write_cy_cmd
  9. cy_stop
  10. cy_start
  11. cy_sched_event
  12. cy_probe
  13. cy_interrupt
  14. do_cyclades_bh
  15. do_softint
  16. grab_all_interrupts
  17. free_all_interrupts
  18. check_wild_interrupts
  19. get_auto_irq
  20. do_auto_irq
  21. startup
  22. start_xmit
  23. shutdown
  24. config_setup
  25. cy_put_char
  26. cy_flush_chars
  27. cy_write
  28. cy_write_room
  29. cy_chars_in_buffer
  30. cy_flush_buffer
  31. cy_throttle
  32. cy_unthrottle
  33. get_serial_info
  34. set_serial_info
  35. get_modem_info
  36. set_modem_info
  37. send_break
  38. get_mon_info
  39. set_threshold
  40. get_threshold
  41. set_default_threshold
  42. get_default_threshold
  43. set_timeout
  44. get_timeout
  45. set_default_timeout
  46. get_default_timeout
  47. cy_ioctl
  48. cy_set_termios
  49. cy_close
  50. cy_hangup
  51. block_til_ready
  52. cy_open
  53. show_version
  54. cy_init_card
  55. cy_init
  56. init_module
  57. cleanup_module
  58. cy_detect_isa
  59. cy_detect_pci
  60. show_status

   1 static char rcsid[] =
   2 "$Revision: 1.36.3.4 $$Date: 1995/11/13 20:45:10 $";
   3 /*
   4  *  linux/drivers/char/cyclades.c
   5  *
   6  * This file contains the driver for the Cyclades Cyclom-Y multiport
   7  * serial boards.
   8  *
   9  * Maintained by Marcio Saito (marcio@cyclades.com) and
  10  * Randolph Bentson (bentson@grieg.seaslug.org)
  11  *
  12  * For Technical support and installation problems, please send e-mail
  13  * to support@cyclades.com.
  14  *
  15  * Much of the design and some of the code came from serial.c
  16  * which was copyright (C) 1991, 1992  Linus Torvalds.  It was
  17  * extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92,
  18  * and then fixed as suggested by Michael K. Johnson 12/12/92.
  19  *
  20  * This version does not support shared irq's.
  21  *
  22  * This module exports the following rs232 io functions:
  23  *   int cy_init(void);
  24  *   int cy_open(struct tty_struct *tty, struct file *filp);
  25  *
  26  * $Log: cyclades.c,v $
  27  * Revision 1.36.3.4  1995/11/13  20:45:10  bentson
  28  * Changes by Corey Minyard <minyard@wf-rch.cirr.com> distributed
  29  * in 1.3.41 kernel to remove a possible race condition, extend
  30  * some error messages, and let the driver run as a loadable module
  31  * Change by Alan Wendt <alan@ez0.ezlink.com> to remove a
  32  * possible race condition.
  33  * Change by Marcio Saito <marcio@cyclades.com> to fix PCI addressing.
  34  *
  35  * Revision 1.36.3.3  1995/11/13  19:44:48  bentson
  36  * Changes by Linus Torvalds in 1.3.33 kernel distribution
  37  * required due to reordering of driver initialization.
  38  * Drivers are now initialized *after* memory management.
  39  *
  40  * Revision 1.36.3.2  1995/09/08  22:07:14  bentson
  41  * remove printk from ISR; fix typo
  42  *
  43  * Revision 1.36.3.1  1995/09/01  12:00:42  marcio
  44  * Minor fixes in the PCI board support. PCI function calls in
  45  * conditional compilation (CONFIG_PCI). Thanks to Jim Duncan
  46  * <duncan@okay.com>. "bad serial count" message removed.
  47  *
  48  * Revision 1.36.3  1995/08/22  09:19:42  marcio
  49  * Cyclom-Y/PCI support added. Changes in the cy_init routine and
  50  * board initialization. Changes in the boot messages. The driver
  51  * supports up to 4 boards and 64 ports by default.
  52  *
  53  * Revision 1.36.1.4  1995/03/29  06:14:14  bentson
  54  * disambiguate between Cyclom-16Y and Cyclom-32Ye;
  55  *
  56  * Revision 1.36.1.3  1995/03/23  22:15:35  bentson
  57  * add missing break in modem control block in ioctl switch statement
  58  * (discovered by Michael Edward Chastain <mec@jobe.shell.portal.com>);
  59  *
  60  * Revision 1.36.1.2  1995/03/22  19:16:22  bentson
  61  * make sure CTS flow control is set as soon as possible (thanks
  62  * to note from David Lambert <lambert@chesapeake.rps.slb.com>);
  63  *
  64  * Revision 1.36.1.1  1995/03/13  15:44:43  bentson
  65  * initialize defaults for receive threshold and stale data timeout;
  66  * cosmetic changes;
  67  *
  68  * Revision 1.36  1995/03/10  23:33:53  bentson
  69  * added support of chips 4-7 in 32 port Cyclom-Ye;
  70  * fix cy_interrupt pointer dereference problem
  71  * (Joe Portman <baron@aa.net>);
  72  * give better error response if open is attempted on non-existent port
  73  * (Zachariah Vaum <jchryslr@netcom.com>);
  74  * correct command timeout (Kenneth Lerman <lerman@@seltd.newnet.com>);
  75  * conditional compilation for -16Y on systems with fast, noisy bus;
  76  * comment out diagnostic print function;
  77  * cleaned up table of base addresses;
  78  * set receiver time-out period register to correct value,
  79  * set receive threshold to better default values,
  80  * set chip timer to more accurate 200 Hz ticking,
  81  * add code to monitor and modify receive parameters
  82  * (Rik Faith <faith@cs.unc.edu> Nick Simicich
  83  * <njs@scifi.emi.net>);
  84  *
  85  * Revision 1.35  1994/12/16  13:54:18  steffen
  86  * additional patch by Marcio Saito for board detection
  87  * Accidently left out in 1.34
  88  *
  89  * Revision 1.34  1994/12/10  12:37:12  steffen
  90  * This is the corrected version as suggested by Marcio Saito
  91  *
  92  * Revision 1.33  1994/12/01  22:41:18  bentson
  93  * add hooks to support more high speeds directly; add tytso
  94  * patch regarding CLOCAL wakeups
  95  *
  96  * Revision 1.32  1994/11/23  19:50:04  bentson
  97  * allow direct kernel control of higher signalling rates;
  98  * look for cards at additional locations
  99  *
 100  * Revision 1.31  1994/11/16  04:33:28  bentson
 101  * ANOTHER fix from Corey Minyard, minyard@wf-rch.cirr.com--
 102  * a problem in chars_in_buffer has been resolved by some
 103  * small changes;  this should yield smoother output
 104  *
 105  * Revision 1.30  1994/11/16  04:28:05  bentson
 106  * Fix from Corey Minyard, Internet: minyard@metronet.com,
 107  * UUCP: minyard@wf-rch.cirr.com, WORK: minyardbnr.ca, to
 108  * cy_hangup that appears to clear up much (all?) of the
 109  * DTR glitches; also he's added/cleaned-up diagnostic messages
 110  *
 111  * Revision 1.29  1994/11/16  04:16:07  bentson
 112  * add change proposed by Ralph Sims, ralphs@halcyon.com, to
 113  * operate higher speeds in same way as other serial ports;
 114  * add more serial ports (for up to two 16-port muxes).
 115  *
 116  * Revision 1.28  1994/11/04  00:13:16  root
 117  * turn off diagnostic messages
 118  *
 119  * Revision 1.27  1994/11/03  23:46:37  root
 120  * bunch of changes to bring driver into greater conformance
 121  * with the serial.c driver (looking for missed fixes)
 122  *
 123  * Revision 1.26  1994/11/03  22:40:36  root
 124  * automatic interrupt probing fixed.
 125  *
 126  * Revision 1.25  1994/11/03  20:17:02  root
 127  * start to implement auto-irq
 128  *
 129  * Revision 1.24  1994/11/03  18:01:55  root
 130  * still working on modem signals--trying not to drop DTR
 131  * during the getty/login processes
 132  *
 133  * Revision 1.23  1994/11/03  17:51:36  root
 134  * extend baud rate support; set receive threshold as function
 135  * of baud rate; fix some problems with RTS/CTS;
 136  *
 137  * Revision 1.22  1994/11/02  18:05:35  root
 138  * changed arguments to udelay to type long to get
 139  * delays to be of correct duration
 140  *
 141  * Revision 1.21  1994/11/02  17:37:30  root
 142  * employ udelay (after calibrating loops_per_second earlier
 143  * in init/main.c) instead of using home-grown delay routines
 144  *
 145  * Revision 1.20  1994/11/02  03:11:38  root
 146  * cy_chars_in_buffer forces a return value of 0 to let
 147  * login work (don't know why it does); some functions
 148  * that were returning EFAULT, now executes the code;
 149  * more work on deciding when to disable xmit interrupts;
 150  *
 151  * Revision 1.19  1994/11/01  20:10:14  root
 152  * define routine to start transmission interrupts (by enabling
 153  * transmit interrupts); directly enable/disable modem interrupts;
 154  *
 155  * Revision 1.18  1994/11/01  18:40:45  bentson
 156  * Don't always enable transmit interrupts in startup; interrupt on
 157  * TxMpty instead of TxRdy to help characters get out before shutdown;
 158  * restructure xmit interrupt to check for chars first and quit if
 159  * none are ready to go; modem status (MXVRx) is upright, _not_ inverted
 160  * (to my view);
 161  *
 162  * Revision 1.17  1994/10/30  04:39:45  bentson
 163  * rename serial_driver and callout_driver to cy_serial_driver and
 164  * cy_callout_driver to avoid linkage interference; initialize
 165  * info->type to PORT_CIRRUS; ruggedize paranoia test; elide ->port
 166  * from cyclades_port structure; add paranoia check to cy_close;
 167  *
 168  * Revision 1.16  1994/10/30  01:14:33  bentson
 169  * change major numbers; add some _early_ return statements;
 170  *
 171  * Revision 1.15  1994/10/29  06:43:15  bentson
 172  * final tidying up for clean compile;  enable some error reporting
 173  *
 174  * Revision 1.14  1994/10/28  20:30:22  Bentson
 175  * lots of changes to drag the driver towards the new tty_io
 176  * structures and operation.  not expected to work, but may
 177  * compile cleanly.
 178  *
 179  * Revision 1.13  1994/07/21  23:08:57  Bentson
 180  * add some diagnostic cruft; support 24 lines (for testing
 181  * both -8Y and -16Y cards; be more thorough in servicing all
 182  * chips during interrupt; add "volatile" a few places to
 183  * circumvent compiler optimizations; fix base & offset
 184  * computations in block_til_ready (was causing chip 0 to
 185  * stop operation)
 186  *
 187  * Revision 1.12  1994/07/19  16:42:11  Bentson
 188  * add some hackery for kernel version 1.1.8; expand
 189  * error messages; refine timing for delay loops and
 190  * declare loop params volatile
 191  *
 192  * Revision 1.11  1994/06/11  21:53:10  bentson
 193  * get use of save_car right in transmit interrupt service
 194  *
 195  * Revision 1.10.1.1  1994/06/11  21:31:18  bentson
 196  * add some diagnostic printing; try to fix save_car stuff
 197  *
 198  * Revision 1.10  1994/06/11  20:36:08  bentson
 199  * clean up compiler warnings
 200  *
 201  * Revision 1.9  1994/06/11  19:42:46  bentson
 202  * added a bunch of code to support modem signalling
 203  *
 204  * Revision 1.8  1994/06/11  17:57:07  bentson
 205  * recognize break & parity error
 206  *
 207  * Revision 1.7  1994/06/05  05:51:34  bentson
 208  * Reorder baud table to be monotonic; add cli to CP; discard
 209  * incoming characters and status if the line isn't open; start to
 210  * fold code into cy_throttle; start to port get_serial_info,
 211  * set_serial_info, get_modem_info, set_modem_info, and send_break
 212  * from serial.c; expand cy_ioctl; relocate and expand config_setup;
 213  * get flow control characters from tty struct; invalidate ports w/o
 214  * hardware;
 215  *
 216  * Revision 1.6  1994/05/31  18:42:21  bentson
 217  * add a loop-breaker in the interrupt service routine;
 218  * note when port is initialized so that it can be shut
 219  * down under the right conditions; receive works without
 220  * any obvious errors
 221  *
 222  * Revision 1.5  1994/05/30  00:55:02  bentson
 223  * transmit works without obvious errors
 224  *
 225  * Revision 1.4  1994/05/27  18:46:27  bentson
 226  * incorporated more code from lib_y.c; can now print short
 227  * strings under interrupt control to port zero; seems to
 228  * select ports/channels/lines correctly
 229  *
 230  * Revision 1.3  1994/05/25  22:12:44  bentson
 231  * shifting from multi-port on a card to proper multiplexor
 232  * data structures;  added skeletons of most routines
 233  *
 234  * Revision 1.2  1994/05/19  13:21:43  bentson
 235  * start to crib from other sources
 236  *
 237  */
 238 
 239 #include <linux/module.h>
 240 
 241 #include <linux/errno.h>
 242 #include <linux/signal.h>
 243 #include <linux/sched.h>
 244 #include <linux/timer.h>
 245 #include <linux/tty.h>
 246 #include <linux/serial.h>
 247 #include <linux/interrupt.h>
 248 #include <linux/string.h>
 249 #include <linux/fcntl.h>
 250 #include <linux/ptrace.h>
 251 #include <linux/cyclades.h>
 252 #include <linux/delay.h>
 253 #include <linux/major.h>
 254 #include <linux/mm.h>
 255 
 256 #include <asm/system.h>
 257 #include <asm/io.h>
 258 #include <asm/segment.h>
 259 #include <asm/bitops.h>
 260 
 261 #include <linux/config.h>
 262 #include <linux/types.h>
 263 #include <linux/kernel.h>
 264 #include <linux/bios32.h>
 265 #include <linux/pci.h>
 266 
 267 #define small_delay(x) for(j=0;j<x;j++)k++;
 268 
 269 
 270 #define SERIAL_PARANOIA_CHECK
 271 #undef  SERIAL_DEBUG_OPEN
 272 #undef  SERIAL_DEBUG_THROTTLE
 273 #undef  SERIAL_DEBUG_OTHER
 274 #undef  SERIAL_DEBUG_IO
 275 #undef  SERIAL_DEBUG_COUNT
 276 #undef  SERIAL_DEBUG_DTR
 277 #undef  CYCLOM_16Y_HACK
 278 #undef  CYCLOM_ENABLE_MONITORING
 279 
 280 #ifndef MIN
 281 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
 282 #endif
 283 
 284 #define WAKEUP_CHARS 256
 285 
 286 #define STD_COM_FLAGS (0)
 287 
 288 #define SERIAL_TYPE_NORMAL  1
 289 #define SERIAL_TYPE_CALLOUT 2
 290 
 291 
 292 DECLARE_TASK_QUEUE(tq_cyclades);
 293 
 294 struct tty_driver cy_serial_driver, cy_callout_driver;
 295 
 296 static volatile int cy_irq_triggered;
 297 static volatile int cy_triggered;
 298 static int cy_wild_int_mask;
 299 static unsigned char *intr_base_addr;
 300 
 301 
 302 /* This is the address lockup table. The driver will probe for Cyclom-Y/ISA
 303    boards at all addresses in here. If you want the driver to probe addresses
 304    in a different address, add it to this table.
 305    If the driver is probing some other board and causing problems, remove the
 306    address from this table.  */
 307 
 308 static unsigned char *cy_isa_addresses[] = {
 309         (unsigned char *) 0xD0000,
 310         (unsigned char *) 0xD2000,
 311         (unsigned char *) 0xD4000,
 312         (unsigned char *) 0xD6000,
 313         (unsigned char *) 0xD8000,
 314         (unsigned char *) 0xDA000,
 315         (unsigned char *) 0xDC000,
 316         (unsigned char *) 0xDE000,
 317 };
 318 #define NR_ISA_ADDRESSES        (sizeof(cy_isa_addresses)/sizeof(unsigned char *))
 319 
 320 /* This is the per-card data structure containing address, irq, number of
 321    channels, etc. This driver supports a maximum of NR_CARDS cards. If
 322    you need to install more boards, change this constant in the definition
 323    bellow. No other change is necesary to support more boards. */
 324 
 325 #define NR_CARDS        4
 326 
 327 static struct cyclades_card cy_card[NR_CARDS];
 328 
 329 /* This is the per-channel data structure containing pointers, flags
 330    and variables for the port. This driver supports a maximum of NR_PORTS.
 331    If the total number of ports is larger than NR_PORTS, change this
 332    constant in the definition bellow. No other change is necessary to
 333    support more boards/ports. */
 334 
 335 #define NR_PORTS        64
 336 
 337 static struct cyclades_port cy_port[NR_PORTS];
 338 
 339 /*  The Cyclom-Ye has placed the sequential chips in non-sequential
 340  *  address order.  This look-up table overcomes that problem.
 341  */
 342 static int cy_chip_offset [] =
 343     { 0x0000,
 344       0x0400,
 345       0x0800,
 346       0x0C00,
 347       0x0200,
 348       0x0600,
 349       0x0A00,
 350       0x0E00
 351     };
 352 
 353 /* PCI related definitions */
 354 
 355 static unsigned short   cy_pci_nboard = 0;
 356 static unsigned short   cy_isa_nboard = 0;
 357 static unsigned short   cy_nboard = 0;
 358 
 359 int                     cy_detect_isa(void);
 360 int                     cy_detect_pci(void);
 361 
 362 static int              cy_next_channel = 0;    /* next minor available */
 363 
 364 static int serial_refcount;
 365 
 366 static struct tty_struct *serial_table[NR_PORTS];
 367 static struct termios *serial_termios[NR_PORTS];
 368 static struct termios *serial_termios_locked[NR_PORTS];
 369 
 370 /* This is the per-irq data structure,
 371    it maps an irq to the corresponding card */
 372 
 373 struct cyclades_card    *IRQ_cards[16];
 374 
 375 
 376 /*
 377  * tmp_buf is used as a temporary buffer by serial_write.  We need to
 378  * lock it in case the memcpy_fromfs blocks while swapping in a page,
 379  * and some other program tries to do a serial write at the same time.
 380  * Since the lock will only come under contention when the system is
 381  * swapping and available memory is low, it makes sense to share one
 382  * buffer across all the serial ports, since it significantly saves
 383  * memory if large numbers of serial ports are open.
 384  */
 385 static unsigned char *tmp_buf = 0;
 386 static struct semaphore tmp_buf_sem = MUTEX;
 387 
 388 /*
 389  * This is used to look up the divisor speeds and the timeouts
 390  * We're normally limited to 15 distinct baud rates.  The extra
 391  * are accessed via settings in info->flags.
 392  *         0,     1,     2,     3,     4,     5,     6,     7,     8,     9,
 393  *        10,    11,    12,    13,    14,    15,    16,    17,    18,    19,
 394  *                                                  HI            VHI
 395  */
 396 static int baud_table[] = {
 397            0,    50,    75,   110,   134,   150,   200,   300,   600,  1200,
 398         1800,  2400,  4800,  9600, 19200, 38400, 57600, 76800,115200,150000,
 399         0};
 400 
 401 static char baud_co[] = {  /* 25 MHz clock option table */
 402         /* value =>    00    01   02    03    04 */
 403         /* divide by    8    32   128   512  2048 */
 404         0x00,  0x04,  0x04,  0x04,  0x04,  0x04,  0x03,  0x03,  0x03,  0x02,
 405         0x02,  0x02,  0x01,  0x01,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00};
 406 
 407 static char baud_bpr[] = {  /* 25 MHz baud rate period table */
 408         0x00,  0xf5,  0xa3,  0x6f,  0x5c,  0x51,  0xf5,  0xa3,  0x51,  0xa3,
 409         0x6d,  0x51,  0xa3,  0x51,  0xa3,  0x51,  0x36,  0x29,  0x1b,  0x15};
 410 
 411 static char baud_cor3[] = {  /* receive threshold */
 412         0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,
 413         0x0a,  0x0a,  0x0a,  0x09,  0x09,  0x08,  0x08,  0x08,  0x08,  0x07};
 414 
 415 
 416 
 417 static void shutdown(struct cyclades_port *);
 418 static int startup (struct cyclades_port *);
 419 static void cy_throttle(struct tty_struct *);
 420 static void cy_unthrottle(struct tty_struct *);
 421 static void config_setup(struct cyclades_port *);
 422 #ifdef CYCLOM_SHOW_STATUS
 423 static void show_status(int);
 424 #endif
 425 
 426 
 427 static inline int
 428 serial_paranoia_check(struct cyclades_port *info,
     /* [previous][next][first][last][top][bottom][index][help] */
 429                         kdev_t device, const char *routine)
 430 {
 431 #ifdef SERIAL_PARANOIA_CHECK
 432     static const char *badmagic =
 433         "Warning: bad magic number for serial struct (%s) in %s\n";
 434     static const char *badinfo =
 435         "Warning: null cyclades_port for (%s) in %s\n";
 436     static const char *badrange =
 437         "Warning: cyclades_port out of range for (%s) in %s\n";
 438 
 439     if (!info) {
 440         printk(badinfo, kdevname(device), routine);
 441         return 1;
 442     }
 443 
 444     if( (long)info < (long)(&cy_port[0])
 445     || (long)(&cy_port[NR_PORTS]) < (long)info ){
 446         printk(badrange, kdevname(device), routine);
 447         return 1;
 448     }
 449 
 450     if (info->magic != CYCLADES_MAGIC) {
 451         printk(badmagic, kdevname(device), routine);
 452         return 1;
 453     }
 454 #endif
 455         return 0;
 456 } /* serial_paranoia_check */
 457 
 458 /* The following diagnostic routines allow the driver to spew
 459    information on the screen, even (especially!) during interrupts.
 460  */
 461 void
 462 SP(char *data){
     /* [previous][next][first][last][top][bottom][index][help] */
 463   unsigned long flags;
 464     save_flags(flags); cli();
 465         console_print(data);
 466     restore_flags(flags);
 467 }
 468 void
 469 CP(char data){
     /* [previous][next][first][last][top][bottom][index][help] */
 470   unsigned long flags;
 471   char scrn[2];
 472     save_flags(flags); cli();
 473         scrn[0] = data;
 474         scrn[1] = '\0';
 475         console_print(scrn);
 476     restore_flags(flags);
 477 }/* CP */
 478 
 479 void CP1(int data) { (data<10)?  CP(data+'0'): CP(data+'A'-10); }/* CP1 */
     /* [previous][next][first][last][top][bottom][index][help] */
 480 void CP2(int data) { CP1((data>>4) & 0x0f); CP1( data & 0x0f); }/* CP2 */
     /* [previous][next][first][last][top][bottom][index][help] */
 481 void CP4(int data) { CP2((data>>8) & 0xff); CP2(data & 0xff); }/* CP4 */
     /* [previous][next][first][last][top][bottom][index][help] */
 482 void CP8(long data) { CP4((data>>16) & 0xffff); CP4(data & 0xffff); }/* CP8 */
     /* [previous][next][first][last][top][bottom][index][help] */
 483 
 484 /* This routine waits up to 1000 micro-seconds for the previous
 485    command to the Cirrus chip to complete and then issues the
 486    new command.  An error is returned if the previous command
 487    didn't finish within the time limit.
 488  */
 489 u_short
 490 write_cy_cmd(u_char *base_addr, u_char cmd, int index)
     /* [previous][next][first][last][top][bottom][index][help] */
 491 {
 492   unsigned long flags;
 493   volatile int  i;
 494 
 495     save_flags(flags); cli();
 496         /* Check to see that the previous command has completed */
 497         for(i = 0 ; i < 100 ; i++){
 498             if (base_addr[CyCCR<<index] == 0){
 499                 break;
 500             }
 501             udelay(10L);
 502         }
 503         /* if the CCR never cleared, the previous command
 504             didn't finish within the "reasonable time" */
 505         if ( i == 100 ) {
 506             restore_flags(flags);
 507             return (-1);
 508         }
 509 
 510         /* Issue the new command */
 511         base_addr[CyCCR<<index] = cmd;
 512     restore_flags(flags);
 513     return(0);
 514 } /* write_cy_cmd */
 515 
 516 
 517 /* cy_start and cy_stop provide software output flow control as a
 518    function of XON/XOFF, software CTS, and other such stuff. */
 519 
 520 static void
 521 cy_stop(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 522 {
 523   struct cyclades_card *cinfo;
 524   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
 525   unsigned char *base_addr;
 526   int chip,channel,index;
 527   unsigned long flags;
 528 
 529 #ifdef SERIAL_DEBUG_OTHER
 530     printk("cy_stop ttyC%d\n", info->line); /* */
 531 #endif
 532 
 533     if (serial_paranoia_check(info, tty->device, "cy_stop"))
 534         return;
 535         
 536     cinfo = &cy_card[info->card];
 537     index = cinfo->bus_index;
 538     channel = info->line - cinfo->first_line;
 539     chip = channel>>2;
 540     channel &= 0x03;
 541     base_addr = (unsigned char*)
 542                    (cy_card[info->card].base_addr + (cy_chip_offset[chip]<<index));
 543 
 544     save_flags(flags); cli();
 545         base_addr[CyCAR<<index] = (u_char)(channel & 0x0003); /* index channel */
 546         base_addr[CySRER<<index] &= ~CyTxMpty;
 547     restore_flags(flags);
 548 
 549     return;
 550 } /* cy_stop */
 551 
 552 static void
 553 cy_start(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 554 {
 555   struct cyclades_card *cinfo;
 556   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
 557   unsigned char *base_addr;
 558   int chip,channel,index;
 559   unsigned long flags;
 560 
 561 #ifdef SERIAL_DEBUG_OTHER
 562     printk("cy_start ttyC%d\n", info->line); /* */
 563 #endif
 564 
 565     if (serial_paranoia_check(info, tty->device, "cy_start"))
 566         return;
 567         
 568     cinfo = &cy_card[info->card];
 569     index = cinfo->bus_index;
 570     channel = info->line - cinfo->first_line;
 571     chip = channel>>2;
 572     channel &= 0x03;
 573     base_addr = (unsigned char*)
 574                    (cy_card[info->card].base_addr + (cy_chip_offset[chip]<<index));
 575 
 576     save_flags(flags); cli();
 577         base_addr[CyCAR<<index] = (u_char)(channel & 0x0003);
 578         base_addr[CySRER<<index] |= CyTxMpty;
 579     restore_flags(flags);
 580 
 581     return;
 582 } /* cy_start */
 583 
 584 
 585 /*
 586  * This routine is used by the interrupt handler to schedule
 587  * processing in the software interrupt portion of the driver
 588  * (also known as the "bottom half").  This can be called any
 589  * number of times for any channel without harm.
 590  */
 591 static inline void
 592 cy_sched_event(struct cyclades_port *info, int event)
     /* [previous][next][first][last][top][bottom][index][help] */
 593 {
 594     info->event |= 1 << event; /* remember what kind of event and who */
 595     queue_task_irq_off(&info->tqueue, &tq_cyclades); /* it belongs to */
 596     mark_bh(CYCLADES_BH);                       /* then trigger event */
 597 } /* cy_sched_event */
 598 
 599 
 600 static int probe_ready;
 601 
 602 /*
 603  * This interrupt routine is used
 604  * while we are probing for submarines.
 605  */
 606 static void
 607 cy_probe(int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 608 {
 609   int save_xir, save_car;
 610   int index = 0;        /* probing interrupts is only for ISA */
 611 
 612     if (!probe_ready)
 613         return;
 614 
 615     cy_irq_triggered = irq;
 616     cy_triggered |= 1 << irq;
 617 
 618         if(intr_base_addr[CySVRR<<index] != 0) {
 619             save_xir = (u_char) intr_base_addr[CyTIR<<index];
 620             save_car = intr_base_addr[CyCAR<<index];
 621             if ((save_xir & 0x3) != 0){
 622                 SP("channel ");
 623                 CP2(save_xir);
 624                 SP(" requesting unexpected interrupt\n");
 625             }
 626             intr_base_addr[CyCAR<<index] = (save_xir & 0x3);
 627             intr_base_addr[CySRER<<index] &= ~CyTxMpty;
 628             intr_base_addr[CyTIR<<index] = (save_xir & 0x3f);
 629             intr_base_addr[CyCAR<<index] = (save_car);
 630             *(intr_base_addr + (Cy_ClrIntr<<index)) = 0; /* Cy_ClrIntr is 0x1800 */
 631         }
 632     return;
 633 } /* cy_probe */
 634 
 635 /* The real interrupt service routine is called
 636    whenever the card wants its hand held--chars
 637    received, out buffer empty, modem change, etc.
 638  */
 639 static void
 640 cy_interrupt(int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 641 {
 642   struct tty_struct *tty;
 643   int status;
 644   struct cyclades_card *cinfo;
 645   struct cyclades_port *info;
 646   volatile unsigned char *base_addr, *card_base_addr;
 647   int chip;
 648   int save_xir, channel, save_car;
 649   char data;
 650   int char_count;
 651   int outch;
 652   int i,j,index;
 653   int too_many;
 654   int had_work;
 655   int mdm_change;
 656   int mdm_status;
 657 
 658     if((cinfo = IRQ_cards[irq]) == 0){
 659         return; /* spurious interrupt */
 660     }
 661 
 662     card_base_addr = (unsigned char *)cinfo->base_addr;
 663     index = cinfo->bus_index;
 664 
 665     /* This loop checks all chips in the card.  Make a note whenever
 666        _any_ chip had some work to do, as this is considered an
 667        indication that there will be more to do.  Only when no chip
 668        has any work does this outermost loop exit.
 669      */
 670     do{
 671         had_work = 0;
 672         for ( chip = 0 ; chip < cinfo->num_chips ; chip ++) {
 673             base_addr = (unsigned char *)
 674                            (cinfo->base_addr + (cy_chip_offset[chip]<<index));
 675             too_many = 0;
 676             while ( (status = base_addr[CySVRR<<index]) != 0x00) {
 677                 had_work++;
 678                 /* The purpose of the following test is to ensure that
 679                    no chip can monopolize the driver.  This forces the
 680                    chips to be checked in a round-robin fashion (after
 681                    draining each of a bunch (1000) of characters).
 682                  */
 683                 if(1000<too_many++){
 684                     break;
 685                 }
 686                 if (status & CySRReceive) {      /* reception interrupt */
 687                     /* determine the channel and change to that context */
 688                     save_xir = (u_char) base_addr[CyRIR<<index];
 689                     channel = (u_short ) (save_xir & CyIRChannel);
 690                     i = channel + chip * 4 + cinfo->first_line;
 691                     info = &cy_port[i];
 692                     info->last_active = jiffies;
 693                     save_car = base_addr[CyCAR<<index];
 694                     base_addr[CyCAR<<index] = save_xir;
 695 
 696                     /* if there is nowhere to put the data, discard it */
 697                     if(info->tty == 0){
 698                         j = (base_addr[CyRIVR<<index] & CyIVRMask);
 699                         if ( j == CyIVRRxEx ) { /* exception */
 700                             data = base_addr[CyRDSR<<index];
 701                         } else { /* normal character reception */
 702                             char_count = base_addr[CyRDCR<<index];
 703                             while(char_count--){
 704                                 data = base_addr[CyRDSR<<index];
 705                             }
 706                         }
 707                     }else{ /* there is an open port for this data */
 708                         tty = info->tty;
 709                         j = (base_addr[CyRIVR<<index] & CyIVRMask);
 710                         if ( j == CyIVRRxEx ) { /* exception */
 711                             data = base_addr[CyRDSR<<index];
 712                             if(data & info->ignore_status_mask){
 713                                 continue;
 714                             }
 715                             if (tty->flip.count < TTY_FLIPBUF_SIZE){
 716                                 tty->flip.count++;
 717                                 if (data & info->read_status_mask){
 718                                     if(data & CyBREAK){
 719                                         *tty->flip.flag_buf_ptr++ =
 720                                                                 TTY_BREAK;
 721                                         *tty->flip.char_buf_ptr++ =
 722                                                         base_addr[CyRDSR<<index];
 723                                         if (info->flags & ASYNC_SAK){
 724                                             do_SAK(tty);
 725                                         }
 726                                     }else if(data & CyFRAME){
 727                                         *tty->flip.flag_buf_ptr++ =
 728                                                                 TTY_FRAME;
 729                                         *tty->flip.char_buf_ptr++ =
 730                                                         base_addr[CyRDSR<<index];
 731                                     }else if(data & CyPARITY){
 732                                         *tty->flip.flag_buf_ptr++ =
 733                                                                 TTY_PARITY;
 734                                         *tty->flip.char_buf_ptr++ =
 735                                                         base_addr[CyRDSR<<index];
 736                                     }else if(data & CyOVERRUN){
 737                                         *tty->flip.flag_buf_ptr++ =
 738                                                                 TTY_OVERRUN;
 739                                         *tty->flip.char_buf_ptr++ = 0;
 740                                         /* If the flip buffer itself is
 741                                            overflowing, we still loose
 742                                            the next incoming character.
 743                                          */
 744                                         if(tty->flip.count < TTY_FLIPBUF_SIZE){
 745                                             tty->flip.count++;
 746                                             *tty->flip.flag_buf_ptr++ =
 747                                                                  TTY_NORMAL;
 748                                             *tty->flip.char_buf_ptr++ =
 749                                                         base_addr[CyRDSR<<index];
 750                                         }
 751                                     /* These two conditions may imply */
 752                                     /* a normal read should be done. */
 753                                     /* }else if(data & CyTIMEOUT){ */
 754                                     /* }else if(data & CySPECHAR){ */
 755                                     }else{
 756                                         *tty->flip.flag_buf_ptr++ = 0;
 757                                         *tty->flip.char_buf_ptr++ = 0;
 758                                     }
 759                                 }else{
 760                                     *tty->flip.flag_buf_ptr++ = 0;
 761                                     *tty->flip.char_buf_ptr++ = 0;
 762                                 }
 763                             }else{
 764                                 /* there was a software buffer overrun
 765                                     and nothing could be done about it!!! */
 766                             }
 767                         } else { /* normal character reception */
 768                             /* load # characters available from the chip */
 769                             char_count = base_addr[CyRDCR<<index];
 770 
 771 #ifdef CYCLOM_ENABLE_MONITORING
 772                             ++info->mon.int_count;
 773                             info->mon.char_count += char_count;
 774                             if (char_count > info->mon.char_max)
 775                                info->mon.char_max = char_count;
 776                             info->mon.char_last = char_count;
 777 #endif
 778                             while(char_count--){
 779                                 if (tty->flip.count >= TTY_FLIPBUF_SIZE){
 780                                         break;
 781                                 }
 782                                 tty->flip.count++;
 783                                 data = base_addr[CyRDSR<<index];
 784                                 *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
 785                                 *tty->flip.char_buf_ptr++ = data;
 786 #ifdef CYCLOM_16Y_HACK
 787                                 udelay(10L);
 788 #endif
 789                             }
 790                         }
 791                         queue_task_irq_off(&tty->flip.tqueue, &tq_timer);
 792                     }
 793                     /* end of service */
 794                     base_addr[CyRIR<<index] = (save_xir & 0x3f);
 795                     base_addr[CyCAR<<index] = (save_car);
 796                 }
 797 
 798 
 799                 if (status & CySRTransmit) {     /* transmission interrupt */
 800                     /* Since we only get here when the transmit buffer is empty,
 801                         we know we can always stuff a dozen characters. */
 802 
 803                     /* determine the channel and change to that context */
 804                     save_xir = (u_char) base_addr[CyTIR<<index];
 805                     channel = (u_short ) (save_xir & CyIRChannel);
 806                     i = channel + chip * 4 + cinfo->first_line;
 807                     save_car = base_addr[CyCAR<<index];
 808                     base_addr[CyCAR<<index] = save_xir;
 809 
 810                     /* validate the port number (as configured and open) */
 811                     if( (i < 0) || (NR_PORTS <= i) ){
 812                         base_addr[CySRER<<index] &= ~CyTxMpty;
 813                         goto txend;
 814                     }
 815                     info = &cy_port[i];
 816                     info->last_active = jiffies;
 817                     if(info->tty == 0){
 818                         base_addr[CySRER<<index] &= ~CyTxMpty;
 819                         goto txdone;
 820                     }
 821 
 822                     /* load the on-chip space available for outbound data */
 823                     char_count = info->xmit_fifo_size;
 824 
 825 
 826                     if(info->x_char) { /* send special char */
 827                         outch = info->x_char;
 828                         base_addr[CyTDR<<index] = outch;
 829                         char_count--;
 830                         info->x_char = 0;
 831                     }
 832 
 833                     if (info->x_break){
 834                         /*  The Cirrus chip requires the "Embedded Transmit
 835                             Commands" of start break, delay, and end break
 836                             sequences to be sent.  The duration of the
 837                             break is given in TICs, which runs at HZ
 838                             (typically 100) and the PPR runs at 200 Hz,
 839                             so the delay is duration * 200/HZ, and thus a
 840                             break can run from 1/100 sec to about 5/4 sec.
 841                          */
 842                         base_addr[CyTDR<<index] = 0; /* start break */
 843                         base_addr[CyTDR<<index] = 0x81;
 844                         base_addr[CyTDR<<index] = 0; /* delay a bit */
 845                         base_addr[CyTDR<<index] = 0x82;
 846                         base_addr[CyTDR<<index] = info->x_break*200/HZ;
 847                         base_addr[CyTDR<<index] = 0; /* terminate break */
 848                         base_addr[CyTDR<<index] = 0x83;
 849                         char_count -= 7;
 850                         info->x_break = 0;
 851                     }
 852 
 853                     while (char_count-- > 0){
 854                         if (!info->xmit_cnt){
 855                             base_addr[CySRER<<index] &= ~CyTxMpty;
 856                             goto txdone;
 857                         }
 858                         if (info->xmit_buf == 0){
 859                             base_addr[CySRER<<index] &= ~CyTxMpty;
 860                             goto txdone;
 861                         }
 862                         if (info->tty->stopped || info->tty->hw_stopped){
 863                             base_addr[CySRER<<index] &= ~CyTxMpty;
 864                             goto txdone;
 865                         }
 866                         /* Because the Embedded Transmit Commands have been
 867                            enabled, we must check to see if the escape
 868                            character, NULL, is being sent.  If it is, we
 869                            must ensure that there is room for it to be
 870                            doubled in the output stream.  Therefore we
 871                            no longer advance the pointer when the character
 872                            is fetched, but rather wait until after the check
 873                            for a NULL output character. (This is necessary
 874                            because there may not be room for the two chars
 875                            needed to send a NULL.
 876                          */
 877                         outch = info->xmit_buf[info->xmit_tail];
 878                         if( outch ){
 879                             info->xmit_cnt--;
 880                             info->xmit_tail = (info->xmit_tail + 1)
 881                                                       & (PAGE_SIZE - 1);
 882                             base_addr[CyTDR<<index] = outch;
 883                         }else{
 884                             if(char_count > 1){
 885                                 info->xmit_cnt--;
 886                                 info->xmit_tail = (info->xmit_tail + 1)
 887                                                           & (PAGE_SIZE - 1);
 888                                 base_addr[CyTDR<<index] = outch;
 889                                 base_addr[CyTDR<<index] = 0;
 890                                 char_count--;
 891                             }else{
 892                             }
 893                         }
 894                     }
 895 
 896         txdone:
 897                     if (info->xmit_cnt < WAKEUP_CHARS) {
 898                         cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
 899                     }
 900 
 901         txend:
 902                     /* end of service */
 903                     base_addr[CyTIR<<index] = (save_xir & 0x3f);
 904                     base_addr[CyCAR<<index] = (save_car);
 905                 }
 906 
 907                 if (status & CySRModem) {        /* modem interrupt */
 908 
 909                     /* determine the channel and change to that context */
 910                     save_xir = (u_char) base_addr[CyMIR<<index];
 911                     channel = (u_short ) (save_xir & CyIRChannel);
 912                     info = &cy_port[channel + chip * 4 + cinfo->first_line];
 913                     info->last_active = jiffies;
 914                     save_car = base_addr[CyCAR<<index];
 915                     base_addr[CyCAR<<index] = save_xir;
 916 
 917                     mdm_change = base_addr[CyMISR<<index];
 918                     mdm_status = base_addr[CyMSVR1<<index];
 919 
 920                     if(info->tty == 0){ /* nowhere to put the data, ignore it */
 921                         ;
 922                     }else{
 923                         if((mdm_change & CyDCD)
 924                         && (info->flags & ASYNC_CHECK_CD)){
 925                             if(mdm_status & CyDCD){
 926 /* CP('!'); */
 927                                 cy_sched_event(info, Cy_EVENT_OPEN_WAKEUP);
 928                             }else if(!((info->flags & ASYNC_CALLOUT_ACTIVE)
 929                                      &&(info->flags & ASYNC_CALLOUT_NOHUP))){
 930 /* CP('@'); */
 931                                 cy_sched_event(info, Cy_EVENT_HANGUP);
 932                             }
 933                         }
 934                         if((mdm_change & CyCTS)
 935                         && (info->flags & ASYNC_CTS_FLOW)){
 936                             if(info->tty->stopped){
 937                                 if(mdm_status & CyCTS){
 938                                     /* !!! cy_start isn't used because... */
 939                                     info->tty->stopped = 0;
 940                                     base_addr[CySRER<<index] |= CyTxMpty;
 941                                     cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
 942                                 }
 943                             }else{
 944                                 if(!(mdm_status & CyCTS)){
 945                                     /* !!! cy_stop isn't used because... */
 946                                     info->tty->stopped = 1;
 947                                     base_addr[CySRER<<index] &= ~CyTxMpty;
 948                                 }
 949                             }
 950                         }
 951                         if(mdm_status & CyDSR){
 952                         }
 953                         if(mdm_status & CyRI){
 954                         }
 955                     }
 956                     /* end of service */
 957                     base_addr[CyMIR<<index] = (save_xir & 0x3f);
 958                     base_addr[CyCAR<<index] = save_car;
 959                 }
 960             }          /* end while status != 0 */
 961         }            /* end loop for chips... */
 962     } while(had_work);
 963 
 964    /* clear interrupts */
 965    *(card_base_addr + (Cy_ClrIntr<<index)) = 0; /* Cy_ClrIntr is 0x1800 */
 966 
 967 } /* cy_interrupt */
 968 
 969 /*
 970  * This routine is used to handle the "bottom half" processing for the
 971  * serial driver, known also the "software interrupt" processing.
 972  * This processing is done at the kernel interrupt level, after the
 973  * cy_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
 974  * is where time-consuming activities which can not be done in the
 975  * interrupt driver proper are done; the interrupt driver schedules
 976  * them using cy_sched_event(), and they get done here.
 977  *
 978  * This is done through one level of indirection--the task queue.
 979  * When a hardware interrupt service routine wants service by the
 980  * driver's bottom half, it enqueues the appropriate tq_struct (one
 981  * per port) to the tq_cyclades work queue and sets a request flag
 982  * via mark_bh for processing that queue.  When the time is right,
 983  * do_cyclades_bh is called (because of the mark_bh) and it requests
 984  * that the work queue be processed.
 985  *
 986  * Although this may seem unwieldy, it gives the system a way to
 987  * pass an argument (in this case the pointer to the cyclades_port
 988  * structure) to the bottom half of the driver.  Previous kernels
 989  * had to poll every port to see if that port needed servicing.
 990  */
 991 static void
 992 do_cyclades_bh(void *unused)
     /* [previous][next][first][last][top][bottom][index][help] */
 993 {
 994     run_task_queue(&tq_cyclades);
 995 } /* do_cyclades_bh */
 996 
 997 static void
 998 do_softint(void *private_)
     /* [previous][next][first][last][top][bottom][index][help] */
 999 {
1000   struct cyclades_port *info = (struct cyclades_port *) private_;
1001   struct tty_struct    *tty;
1002 
1003     tty = info->tty;
1004     if (!tty)
1005         return;
1006 
1007     if (clear_bit(Cy_EVENT_HANGUP, &info->event)) {
1008         tty_hangup(info->tty);
1009         wake_up_interruptible(&info->open_wait);
1010         info->flags &= ~(ASYNC_NORMAL_ACTIVE|
1011                              ASYNC_CALLOUT_ACTIVE);
1012     }
1013     if (clear_bit(Cy_EVENT_OPEN_WAKEUP, &info->event)) {
1014         wake_up_interruptible(&info->open_wait);
1015     }
1016     if (clear_bit(Cy_EVENT_WRITE_WAKEUP, &info->event)) {
1017         if((tty->flags & (1<< TTY_DO_WRITE_WAKEUP))
1018         && tty->ldisc.write_wakeup){
1019             (tty->ldisc.write_wakeup)(tty);
1020         }
1021         wake_up_interruptible(&tty->write_wait);
1022     }
1023 } /* do_softint */
1024 
1025 
1026 /*
1027  * Grab all interrupts in preparation for doing an automatic irq
1028  * detection.  dontgrab is a mask of irq's _not_ to grab.  Returns a
1029  * mask of irq's which were grabbed and should therefore be freed
1030  * using free_all_interrupts().
1031  */
1032 static int
1033 grab_all_interrupts(int dontgrab)
     /* [previous][next][first][last][top][bottom][index][help] */
1034 {
1035   int irq_lines = 0;
1036   int i, mask;
1037     
1038     for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
1039         if (!(mask & dontgrab)
1040         && !request_irq(i, cy_probe, SA_INTERRUPT, "serial probe")) {
1041             irq_lines |= mask;
1042         }
1043     }
1044     return irq_lines;
1045 } /* grab_all_interrupts */
1046 
1047 /*
1048  * Release all interrupts grabbed by grab_all_interrupts
1049  */
1050 static void
1051 free_all_interrupts(int irq_lines)
     /* [previous][next][first][last][top][bottom][index][help] */
1052 {
1053   int i;
1054     
1055     for (i = 0; i < 16; i++) {
1056         if (irq_lines & (1 << i))
1057             free_irq(i);
1058     }
1059 } /* free_all_interrupts */
1060 
1061 /*
1062  * This routine returns a bitfield of "wild interrupts".  Basically,
1063  * any unclaimed interrupts which is flapping around.
1064  */
1065 static int
1066 check_wild_interrupts(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1067 {
1068   int   i, mask;
1069   int   wild_interrupts = 0;
1070   int   irq_lines;
1071   unsigned long timeout;
1072   unsigned long flags;
1073         
1074     /*Turn on interrupts (they may be off) */
1075     save_flags(flags); sti();
1076 
1077         irq_lines = grab_all_interrupts(0);
1078        
1079         /*
1080          * Delay for 0.1 seconds -- we use a busy loop since this may 
1081          * occur during the bootup sequence
1082          */
1083         timeout = jiffies+10;
1084         while (timeout >= jiffies)
1085             ;
1086         
1087         cy_triggered = 0;       /* Reset after letting things settle */
1088 
1089         timeout = jiffies+10;
1090         while (timeout >= jiffies)
1091                 ;
1092         
1093         for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
1094             if ((cy_triggered & (1 << i)) &&
1095                 (irq_lines & (1 << i))) {
1096                     wild_interrupts |= mask;
1097             }
1098         }
1099         free_all_interrupts(irq_lines);
1100     restore_flags(flags);
1101     return wild_interrupts;
1102 } /* check_wild_interrupts */
1103 
1104 /*
1105  * This routine is called by do_auto_irq(); it attempts to determine
1106  * which interrupt a serial port is configured to use.  It is not
1107  * fool-proof, but it works a large part of the time.
1108  */
1109 static int
1110 get_auto_irq(unsigned char *address)
     /* [previous][next][first][last][top][bottom][index][help] */
1111 {
1112   unsigned long timeout;
1113   unsigned char *base_addr;
1114   int           index;
1115 
1116     index = 0;  /* IRQ probing is only for ISA */
1117     base_addr = address;
1118     intr_base_addr = address;
1119         
1120     /*
1121      * Enable interrupts and see who answers
1122      */
1123     cy_irq_triggered = 0;
1124     cli();
1125         base_addr[CyCAR<<index] = 0;
1126         write_cy_cmd(base_addr,CyCHAN_CTL|CyENB_XMTR,index);
1127         base_addr[CySRER<<index] |= CyTxMpty;
1128         probe_ready = 1;
1129     sti();
1130     
1131     timeout = jiffies+2;
1132     while (timeout >= jiffies) {
1133         if (cy_irq_triggered)
1134             break;
1135     }
1136     probe_ready = 0;
1137     return(cy_irq_triggered);
1138 } /* get_auto_irq */
1139 
1140 /*
1141  * Calls get_auto_irq() multiple times, to make sure we don't get
1142  * faked out by random interrupts
1143  */
1144 static int
1145 do_auto_irq(unsigned char *address)
     /* [previous][next][first][last][top][bottom][index][help] */
1146 {
1147   int                   irq_lines = 0;
1148   int                   irq_try_1 = 0, irq_try_2 = 0;
1149   int                   retries;
1150   unsigned long flags;
1151 
1152     /* Turn on interrupts (they may be off) */
1153     save_flags(flags); sti();
1154 
1155         probe_ready = 0;
1156 
1157         cy_wild_int_mask = check_wild_interrupts();
1158 
1159         irq_lines = grab_all_interrupts(cy_wild_int_mask);
1160         
1161         for (retries = 0; retries < 5; retries++) {
1162             if (!irq_try_1)
1163                 irq_try_1 = get_auto_irq(address);
1164             if (!irq_try_2)
1165                 irq_try_2 = get_auto_irq(address);
1166             if (irq_try_1 && irq_try_2) {
1167                 if (irq_try_1 == irq_try_2)
1168                     break;
1169                 irq_try_1 = irq_try_2 = 0;
1170             }
1171         }
1172     restore_flags(flags);
1173     free_all_interrupts(irq_lines);
1174     return (irq_try_1 == irq_try_2) ? irq_try_1 : 0;
1175 } /* do_auto_irq */
1176 
1177 
1178 /* This is called whenever a port becomes active;
1179    interrupts are enabled and DTR & RTS are turned on.
1180  */
1181 static int
1182 startup(struct cyclades_port * info)
     /* [previous][next][first][last][top][bottom][index][help] */
1183 {
1184   unsigned long flags;
1185   unsigned char *base_addr;
1186   int card,chip,channel,index;
1187 
1188     if (info->flags & ASYNC_INITIALIZED){
1189         return 0;
1190     }
1191 
1192     if (!info->type){
1193         if (info->tty){
1194             set_bit(TTY_IO_ERROR, &info->tty->flags);
1195         }
1196         return 0;
1197     }
1198     if (!info->xmit_buf){
1199         info->xmit_buf = (unsigned char *) get_free_page (GFP_KERNEL);
1200         if (!info->xmit_buf){
1201             return -ENOMEM;
1202         }
1203     }
1204 
1205     config_setup(info);
1206 
1207     card = info->card;
1208     channel = (info->line) - (cy_card[card].first_line);
1209     chip = channel>>2;
1210     channel &= 0x03;
1211     index = cy_card[card].bus_index;
1212     base_addr = (unsigned char*)
1213                    (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1214 
1215 #ifdef SERIAL_DEBUG_OPEN
1216     printk("startup card %d, chip %d, channel %d, base_addr %lx",
1217          card, chip, channel, (long)base_addr);/**/
1218 #endif
1219 
1220     save_flags(flags); cli();
1221         base_addr[CyCAR<<index] = (u_char)channel;
1222 
1223         base_addr[CyRTPR<<index] = (info->default_timeout
1224                              ? info->default_timeout
1225                              : 0x02); /* 10ms rx timeout */
1226 
1227         write_cy_cmd(base_addr,CyCHAN_CTL|CyENB_RCVR|CyENB_XMTR,index);
1228 
1229         base_addr[CyCAR<<index] = (u_char)channel; /* !!! Is this needed? */
1230         base_addr[CyMSVR1<<index] = CyRTS;
1231 /* CP('S');CP('1'); */
1232         base_addr[CyMSVR2<<index] = CyDTR;
1233 
1234 #ifdef SERIAL_DEBUG_DTR
1235         printk("cyc: %d: raising DTR\n", __LINE__);
1236         printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
1237 #endif
1238 
1239         base_addr[CySRER<<index] |= CyRxData;
1240         info->flags |= ASYNC_INITIALIZED;
1241 
1242         if (info->tty){
1243             clear_bit(TTY_IO_ERROR, &info->tty->flags);
1244         }
1245         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1246 
1247     restore_flags(flags);
1248 
1249 #ifdef SERIAL_DEBUG_OPEN
1250     printk(" done\n");
1251 #endif
1252     return 0;
1253 } /* startup */
1254 
1255 void
1256 start_xmit( struct cyclades_port *info )
     /* [previous][next][first][last][top][bottom][index][help] */
1257 {
1258   unsigned long flags;
1259   unsigned char *base_addr;
1260   int card,chip,channel,index;
1261 
1262     card = info->card;
1263     channel = (info->line) - (cy_card[card].first_line);
1264     chip = channel>>2;
1265     channel &= 0x03;
1266     index = cy_card[card].bus_index;
1267     base_addr = (unsigned char*)
1268                    (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1269 
1270     save_flags(flags); cli();
1271         base_addr[CyCAR<<index] = channel;
1272         base_addr[CySRER<<index] |= CyTxMpty;
1273     restore_flags(flags);
1274 } /* start_xmit */
1275 
1276 /*
1277  * This routine shuts down a serial port; interrupts are disabled,
1278  * and DTR is dropped if the hangup on close termio flag is on.
1279  */
1280 static void
1281 shutdown(struct cyclades_port * info)
     /* [previous][next][first][last][top][bottom][index][help] */
1282 {
1283   unsigned long flags;
1284   unsigned char *base_addr;
1285   int card,chip,channel,index;
1286 
1287     if (!(info->flags & ASYNC_INITIALIZED)){
1288 /* CP('$'); */
1289         return;
1290     }
1291 
1292     card = info->card;
1293     channel = info->line - cy_card[card].first_line;
1294     chip = channel>>2;
1295     channel &= 0x03;
1296     index = cy_card[card].bus_index;
1297     base_addr = (unsigned char*)
1298                    (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1299 
1300 #ifdef SERIAL_DEBUG_OPEN
1301     printk("shutdown card %d, chip %d, channel %d, base_addr %lx\n",
1302             card, chip, channel, (long)base_addr);
1303 #endif
1304 
1305     /* !!! REALLY MUST WAIT FOR LAST CHARACTER TO BE
1306        SENT BEFORE DROPPING THE LINE !!!  (Perhaps
1307        set some flag that is read when XMTY happens.)
1308        Other choices are to delay some fixed interval
1309        or schedule some later processing.
1310      */
1311     save_flags(flags); cli();
1312         if (info->xmit_buf){
1313             unsigned long temp;
1314             temp = info->xmit_buf;
1315             info->xmit_buf = 0;
1316             free_page((unsigned long) temp);
1317         }
1318 
1319         base_addr[CyCAR<<index] = (u_char)channel;
1320         if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
1321             base_addr[CyMSVR1<<index] = ~CyRTS;
1322             base_addr[CyMSVR2<<index] = ~CyDTR;
1323 #ifdef SERIAL_DEBUG_DTR
1324             printk("cyc: %d: dropping DTR\n", __LINE__);
1325             printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
1326 #endif
1327         }
1328         write_cy_cmd(base_addr,CyCHAN_CTL|CyDIS_RCVR,index);
1329          /* it may be appropriate to clear _XMIT at
1330            some later date (after testing)!!! */
1331 
1332         if (info->tty){
1333             set_bit(TTY_IO_ERROR, &info->tty->flags);
1334         }
1335         info->flags &= ~ASYNC_INITIALIZED;
1336     restore_flags(flags);
1337 
1338 #ifdef SERIAL_DEBUG_OPEN
1339     printk(" done\n");
1340 #endif
1341     return;
1342 } /* shutdown */
1343 
1344 /*
1345  * This routine finds or computes the various line characteristics.
1346  */
1347 static void
1348 config_setup(struct cyclades_port * info)
     /* [previous][next][first][last][top][bottom][index][help] */
1349 {
1350   unsigned long flags;
1351   unsigned char *base_addr;
1352   int card,chip,channel,index;
1353   unsigned cflag;
1354   int   i;
1355 
1356     if (!info->tty || !info->tty->termios){
1357         return;
1358     }
1359     if (info->line == -1){
1360         return;
1361     }
1362     cflag = info->tty->termios->c_cflag;
1363 
1364     /* baud rate */
1365     i = cflag & CBAUD;
1366 #ifdef CBAUDEX
1367 /* Starting with kernel 1.1.65, there is direct support for
1368    higher baud rates.  The following code supports those
1369    changes.  The conditional aspect allows this driver to be
1370    used for earlier as well as later kernel versions.  (The
1371    mapping is slightly different from serial.c because there
1372    is still the possibility of supporting 75 kbit/sec with
1373    the Cyclades board.)
1374  */
1375     if (i & CBAUDEX) {
1376         if (i == B57600)
1377             i = 16;
1378         else if(i == B115200) 
1379             i = 18;
1380 #ifdef B78600
1381         else if(i == B78600) 
1382             i = 17;
1383 #endif
1384         else
1385             info->tty->termios->c_cflag &= ~CBAUDEX;
1386     }
1387 #endif
1388     if (i == 15) {
1389             if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1390                     i += 1;
1391             if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1392                     i += 3;
1393     }
1394     info->tbpr = baud_bpr[i]; /* Tx BPR */
1395     info->tco = baud_co[i]; /* Tx CO */
1396     info->rbpr = baud_bpr[i]; /* Rx BPR */
1397     info->rco = baud_co[i]; /* Rx CO */
1398     if (baud_table[i] == 134) {
1399         info->timeout = (info->xmit_fifo_size*HZ*30/269) + 2;
1400         /* get it right for 134.5 baud */
1401     } else if (baud_table[i]) {
1402         info->timeout = (info->xmit_fifo_size*HZ*15/baud_table[i]) + 2;
1403         /* this needs to be propagated into the card info */
1404     } else {
1405         info->timeout = 0;
1406     }
1407     /* By tradition (is it a standard?) a baud rate of zero
1408        implies the line should be/has been closed.  A bit
1409        later in this routine such a test is performed. */
1410 
1411     /* byte size and parity */
1412     info->cor5 = 0;
1413     info->cor4 = 0;
1414     info->cor3 = (info->default_threshold
1415                   ? info->default_threshold
1416                   : baud_cor3[i]); /* receive threshold */
1417     info->cor2 = CyETC;
1418     switch(cflag & CSIZE){
1419     case CS5:
1420         info->cor1 = Cy_5_BITS;
1421         break;
1422     case CS6:
1423         info->cor1 = Cy_6_BITS;
1424         break;
1425     case CS7:
1426         info->cor1 = Cy_7_BITS;
1427         break;
1428     case CS8:
1429         info->cor1 = Cy_8_BITS;
1430         break;
1431     }
1432     if(cflag & CSTOPB){
1433         info->cor1 |= Cy_2_STOP;
1434     }
1435     if (cflag & PARENB){
1436         if (cflag & PARODD){
1437             info->cor1 |= CyPARITY_O;
1438         }else{
1439             info->cor1 |= CyPARITY_E;
1440         }
1441     }else{
1442         info->cor1 |= CyPARITY_NONE;
1443     }
1444         
1445     /* CTS flow control flag */
1446     if (cflag & CRTSCTS){
1447         info->flags |= ASYNC_CTS_FLOW;
1448         info->cor2 |= CyCtsAE;
1449     }else{
1450         info->flags &= ~ASYNC_CTS_FLOW;
1451         info->cor2 &= ~CyCtsAE;
1452     }
1453     if (cflag & CLOCAL)
1454         info->flags &= ~ASYNC_CHECK_CD;
1455     else
1456         info->flags |= ASYNC_CHECK_CD;
1457 
1458      /***********************************************
1459         The hardware option, CyRtsAO, presents RTS when
1460         the chip has characters to send.  Since most modems
1461         use RTS as reverse (inbound) flow control, this
1462         option is not used.  If inbound flow control is
1463         necessary, DTR can be programmed to provide the
1464         appropriate signals for use with a non-standard
1465         cable.  Contact Marcio Saito for details.
1466      ***********************************************/
1467 
1468     card = info->card;
1469     channel = (info->line) - (cy_card[card].first_line);
1470     chip = channel>>2;
1471     channel &= 0x03;
1472     index = cy_card[card].bus_index;
1473     base_addr = (unsigned char*)
1474                    (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1475 
1476     save_flags(flags); cli();
1477         base_addr[CyCAR<<index] = (u_char)channel;
1478 
1479        /* tx and rx baud rate */
1480 
1481         base_addr[CyTCOR<<index] = info->tco;
1482         base_addr[CyTBPR<<index] = info->tbpr;
1483         base_addr[CyRCOR<<index] = info->rco;
1484         base_addr[CyRBPR<<index] = info->rbpr;
1485 
1486         /* set line characteristics  according configuration */
1487 
1488         base_addr[CySCHR1<<index] = START_CHAR(info->tty);
1489         base_addr[CySCHR2<<index] = STOP_CHAR(info->tty);
1490         base_addr[CyCOR1<<index] = info->cor1;
1491         base_addr[CyCOR2<<index] = info->cor2;
1492         base_addr[CyCOR3<<index] = info->cor3;
1493         base_addr[CyCOR4<<index] = info->cor4;
1494         base_addr[CyCOR5<<index] = info->cor5;
1495 
1496         write_cy_cmd(base_addr,CyCOR_CHANGE|CyCOR1ch|CyCOR2ch|CyCOR3ch,index);
1497 
1498         base_addr[CyCAR<<index] = (u_char)channel; /* !!! Is this needed? */
1499 
1500         base_addr[CyRTPR<<index] = (info->default_timeout
1501                              ? info->default_timeout
1502                              : 0x02); /* 10ms rx timeout */
1503 
1504         if (C_CLOCAL(info->tty)) {
1505             base_addr[CySRER<<index] |= 0; /* without modem intr */
1506                                     /* ignore 1->0 modem transitions */
1507             base_addr[CyMCOR1<<index] = 0x0;
1508                                     /* ignore 0->1 modem transitions */
1509             base_addr[CyMCOR2<<index] = 0x0;
1510         } else {
1511             base_addr[CySRER<<index] |= CyMdmCh; /* with modem intr */
1512                                     /* act on 1->0 modem transitions */
1513             base_addr[CyMCOR1<<index] = CyDSR|CyCTS|CyRI|CyDCD;
1514                                     /* act on 0->1 modem transitions */
1515             base_addr[CyMCOR2<<index] = CyDSR|CyCTS|CyRI|CyDCD;
1516         }
1517 
1518         if(i == 0){ /* baud rate is zero, turn off line */
1519             base_addr[CyMSVR2<<index] = ~CyDTR;
1520 #ifdef SERIAL_DEBUG_DTR
1521             printk("cyc: %d: dropping DTR\n", __LINE__);
1522             printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
1523 #endif
1524         }else{
1525             base_addr[CyMSVR2<<index] = CyDTR;
1526 #ifdef SERIAL_DEBUG_DTR
1527             printk("cyc: %d: raising DTR\n", __LINE__);
1528             printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
1529 #endif
1530         }
1531 
1532         if (info->tty){
1533             clear_bit(TTY_IO_ERROR, &info->tty->flags);
1534         }
1535 
1536     restore_flags(flags);
1537 
1538 } /* config_setup */
1539 
1540 
1541 static void
1542 cy_put_char(struct tty_struct *tty, unsigned char ch)
     /* [previous][next][first][last][top][bottom][index][help] */
1543 {
1544   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1545   unsigned long flags;
1546 
1547 #ifdef SERIAL_DEBUG_IO
1548     printk("cy_put_char ttyC%d\n", info->line);
1549 #endif
1550 
1551     if (serial_paranoia_check(info, tty->device, "cy_put_char"))
1552         return;
1553 
1554     if (!tty || !info->xmit_buf)
1555         return;
1556 
1557     save_flags(flags); cli();
1558         if (info->xmit_cnt >= PAGE_SIZE - 1) {
1559             restore_flags(flags);
1560             return;
1561         }
1562 
1563         info->xmit_buf[info->xmit_head++] = ch;
1564         info->xmit_head &= PAGE_SIZE - 1;
1565         info->xmit_cnt++;
1566     restore_flags(flags);
1567 } /* cy_put_char */
1568 
1569 
1570 static void
1571 cy_flush_chars(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1572 {
1573   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1574   unsigned long flags;
1575   unsigned char *base_addr;
1576   int card,chip,channel,index;
1577                                 
1578 #ifdef SERIAL_DEBUG_IO
1579     printk("cy_flush_chars ttyC%d\n", info->line); /* */
1580 #endif
1581 
1582     if (serial_paranoia_check(info, tty->device, "cy_flush_chars"))
1583         return;
1584 
1585     if (info->xmit_cnt <= 0 || tty->stopped
1586     || tty->hw_stopped || !info->xmit_buf)
1587         return;
1588 
1589     card = info->card;
1590     channel = info->line - cy_card[card].first_line;
1591     chip = channel>>2;
1592     channel &= 0x03;
1593     index = cy_card[card].bus_index;
1594     base_addr = (unsigned char*)
1595                    (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1596 
1597     save_flags(flags); cli();
1598         base_addr[CyCAR<<index] = channel;
1599         base_addr[CySRER<<index] |= CyTxMpty;
1600     restore_flags(flags);
1601 } /* cy_flush_chars */
1602 
1603 
1604 /* This routine gets called when tty_write has put something into
1605     the write_queue.  If the port is not already transmitting stuff,
1606     start it off by enabling interrupts.  The interrupt service
1607     routine will then ensure that the characters are sent.  If the
1608     port is already active, there is no need to kick it.
1609  */
1610 static int
1611 cy_write(struct tty_struct * tty, int from_user,
     /* [previous][next][first][last][top][bottom][index][help] */
1612            const unsigned char *buf, int count)
1613 {
1614   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1615   unsigned long flags;
1616   int c, total = 0;
1617 
1618 #ifdef SERIAL_DEBUG_IO
1619     printk("cy_write ttyC%d\n", info->line); /* */
1620 #endif
1621 
1622     if (serial_paranoia_check(info, tty->device, "cy_write")){
1623         return 0;
1624     }
1625         
1626     if (!tty || !info->xmit_buf || !tmp_buf){
1627         return 0;
1628     }
1629 
1630     while (1) {
1631         save_flags(flags); cli();               
1632         c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1633                            SERIAL_XMIT_SIZE - info->xmit_head));
1634         if (c <= 0){
1635             restore_flags(flags);
1636             break;
1637         }
1638 
1639         if (from_user) {
1640             down(&tmp_buf_sem);
1641             memcpy_fromfs(tmp_buf, buf, c);
1642             c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1643                        SERIAL_XMIT_SIZE - info->xmit_head));
1644             memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1645             up(&tmp_buf_sem);
1646         } else
1647             memcpy(info->xmit_buf + info->xmit_head, buf, c);
1648         info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1649         info->xmit_cnt += c;
1650         restore_flags(flags);
1651         buf += c;
1652         count -= c;
1653         total += c;
1654     }
1655 
1656 
1657     if (info->xmit_cnt
1658     && !tty->stopped
1659     && !tty->hw_stopped ) {
1660         start_xmit(info);
1661     }
1662     return total;
1663 } /* cy_write */
1664 
1665 
1666 static int
1667 cy_write_room(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1668 {
1669   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1670   int   ret;
1671                                 
1672 #ifdef SERIAL_DEBUG_IO
1673     printk("cy_write_room ttyC%d\n", info->line); /* */
1674 #endif
1675 
1676     if (serial_paranoia_check(info, tty->device, "cy_write_room"))
1677         return 0;
1678     ret = PAGE_SIZE - info->xmit_cnt - 1;
1679     if (ret < 0)
1680         ret = 0;
1681     return ret;
1682 } /* cy_write_room */
1683 
1684 
1685 static int
1686 cy_chars_in_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1687 {
1688   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1689                                 
1690 #ifdef SERIAL_DEBUG_IO
1691     printk("cy_chars_in_buffer ttyC%d %d\n", info->line, info->xmit_cnt); /* */
1692 #endif
1693 
1694     if (serial_paranoia_check(info, tty->device, "cy_chars_in_buffer"))
1695         return 0;
1696 
1697     return info->xmit_cnt;
1698 } /* cy_chars_in_buffer */
1699 
1700 
1701 static void
1702 cy_flush_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1703 {
1704   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1705   unsigned long flags;
1706                                 
1707 #ifdef SERIAL_DEBUG_IO
1708     printk("cy_flush_buffer ttyC%d\n", info->line); /* */
1709 #endif
1710 
1711     if (serial_paranoia_check(info, tty->device, "cy_flush_buffer"))
1712         return;
1713     save_flags(flags); cli();
1714         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1715     restore_flags(flags);
1716     wake_up_interruptible(&tty->write_wait);
1717     if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP))
1718     && tty->ldisc.write_wakeup)
1719         (tty->ldisc.write_wakeup)(tty);
1720 } /* cy_flush_buffer */
1721 
1722 
1723 /* This routine is called by the upper-layer tty layer to signal
1724    that incoming characters should be throttled or that the
1725    throttle should be released.
1726  */
1727 static void
1728 cy_throttle(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1729 {
1730   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1731   unsigned long flags;
1732   unsigned char *base_addr;
1733   int card,chip,channel,index;
1734 
1735 #ifdef SERIAL_DEBUG_THROTTLE
1736   char buf[64];
1737         
1738     printk("throttle %s: %d....\n", _tty_name(tty, buf),
1739            tty->ldisc.chars_in_buffer(tty));
1740     printk("cy_throttle ttyC%d\n", info->line);
1741 #endif
1742 
1743     if (serial_paranoia_check(info, tty->device, "cy_nthrottle")){
1744             return;
1745     }
1746 
1747     if (I_IXOFF(tty)) {
1748         info->x_char = STOP_CHAR(tty);
1749             /* Should use the "Send Special Character" feature!!! */
1750     }
1751 
1752     card = info->card;
1753     channel = info->line - cy_card[card].first_line;
1754     chip = channel>>2;
1755     channel &= 0x03;
1756     index = cy_card[card].bus_index;
1757     base_addr = (unsigned char*)
1758                    (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1759 
1760     save_flags(flags); cli();
1761         base_addr[CyCAR<<index] = (u_char)channel;
1762         base_addr[CyMSVR1<<index] = ~CyRTS;
1763     restore_flags(flags);
1764 
1765     return;
1766 } /* cy_throttle */
1767 
1768 
1769 static void
1770 cy_unthrottle(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1771 {
1772   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1773   unsigned long flags;
1774   unsigned char *base_addr;
1775   int card,chip,channel,index;
1776 
1777 #ifdef SERIAL_DEBUG_THROTTLE
1778   char buf[64];
1779         
1780     printk("throttle %s: %d....\n", _tty_name(tty, buf),
1781            tty->ldisc.chars_in_buffer(tty));
1782     printk("cy_unthrottle ttyC%d\n", info->line);
1783 #endif
1784 
1785     if (serial_paranoia_check(info, tty->device, "cy_nthrottle")){
1786             return;
1787     }
1788 
1789     if (I_IXOFF(tty)) {
1790         info->x_char = START_CHAR(tty);
1791         /* Should use the "Send Special Character" feature!!! */
1792     }
1793 
1794     card = info->card;
1795     channel = info->line - cy_card[card].first_line;
1796     chip = channel>>2;
1797     channel &= 0x03;
1798     index = cy_card[card].bus_index;
1799     base_addr = (unsigned char*)
1800                    (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1801 
1802     save_flags(flags); cli();
1803         base_addr[CyCAR<<index] = (u_char)channel;
1804         base_addr[CyMSVR1<<index] = CyRTS;
1805     restore_flags(flags);
1806 
1807     return;
1808 } /* cy_unthrottle */
1809 
1810 static int
1811 get_serial_info(struct cyclades_port * info,
     /* [previous][next][first][last][top][bottom][index][help] */
1812                            struct serial_struct * retinfo)
1813 {
1814   struct serial_struct tmp;
1815   struct cyclades_card *cinfo = &cy_card[info->card];
1816 
1817 /* CP('g'); */
1818     if (!retinfo)
1819             return -EFAULT;
1820     memset(&tmp, 0, sizeof(tmp));
1821     tmp.type = info->type;
1822     tmp.line = info->line;
1823     tmp.port = info->card * 0x100 + info->line - cinfo->first_line;
1824     tmp.irq = cinfo->irq;
1825     tmp.flags = info->flags;
1826     tmp.baud_base = 0;          /*!!!*/
1827     tmp.close_delay = info->close_delay;
1828     tmp.custom_divisor = 0;     /*!!!*/
1829     tmp.hub6 = 0;               /*!!!*/
1830     memcpy_tofs(retinfo,&tmp,sizeof(*retinfo));
1831     return 0;
1832 } /* get_serial_info */
1833 
1834 static int
1835 set_serial_info(struct cyclades_port * info,
     /* [previous][next][first][last][top][bottom][index][help] */
1836                            struct serial_struct * new_info)
1837 {
1838   struct serial_struct new_serial;
1839   struct cyclades_port old_info;
1840 
1841 /* CP('s'); */
1842     if (!new_info)
1843             return -EFAULT;
1844     memcpy_fromfs(&new_serial,new_info,sizeof(new_serial));
1845     old_info = *info;
1846 
1847     if (!suser()) {
1848             if ((new_serial.close_delay != info->close_delay) ||
1849                 ((new_serial.flags & ASYNC_FLAGS & ~ASYNC_USR_MASK) !=
1850                  (info->flags & ASYNC_FLAGS & ~ASYNC_USR_MASK)))
1851                     return -EPERM;
1852             info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1853                            (new_serial.flags & ASYNC_USR_MASK));
1854             goto check_and_exit;
1855     }
1856 
1857 
1858     /*
1859      * OK, past this point, all the error checking has been done.
1860      * At this point, we start making changes.....
1861      */
1862 
1863     info->flags = ((info->flags & ~ASYNC_FLAGS) |
1864                     (new_serial.flags & ASYNC_FLAGS));
1865     info->close_delay = new_serial.close_delay;
1866 
1867 
1868 check_and_exit:
1869     if (info->flags & ASYNC_INITIALIZED){
1870         config_setup(info);
1871         return 0;
1872     }else{
1873         return startup(info);
1874     }
1875 } /* set_serial_info */
1876 
1877 static int
1878 get_modem_info(struct cyclades_port * info, unsigned int *value)
     /* [previous][next][first][last][top][bottom][index][help] */
1879 {
1880   int card,chip,channel,index;
1881   unsigned char *base_addr;
1882   unsigned long flags;
1883   unsigned char status;
1884   unsigned int result;
1885 
1886     card = info->card;
1887     channel = (info->line) - (cy_card[card].first_line);
1888     chip = channel>>2;
1889     channel &= 0x03;
1890     index = cy_card[card].bus_index;
1891     base_addr = (unsigned char*)
1892                    (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1893 
1894     save_flags(flags); cli();
1895         base_addr[CyCAR<<index] = (u_char)channel;
1896         status = base_addr[CyMSVR1<<index];
1897         status |= base_addr[CyMSVR2<<index];
1898     restore_flags(flags);
1899 
1900     result =  ((status  & CyRTS) ? TIOCM_RTS : 0)
1901             | ((status  & CyDTR) ? TIOCM_DTR : 0)
1902             | ((status  & CyDCD) ? TIOCM_CAR : 0)
1903             | ((status  & CyRI) ? TIOCM_RNG : 0)
1904             | ((status  & CyDSR) ? TIOCM_DSR : 0)
1905             | ((status  & CyCTS) ? TIOCM_CTS : 0);
1906     put_fs_long(result,(unsigned long *) value);
1907     return 0;
1908 } /* get_modem_info */
1909 
1910 static int
1911 set_modem_info(struct cyclades_port * info, unsigned int cmd,
     /* [previous][next][first][last][top][bottom][index][help] */
1912                           unsigned int *value)
1913 {
1914   int card,chip,channel,index;
1915   unsigned char *base_addr;
1916   unsigned long flags;
1917   unsigned int arg = get_fs_long((unsigned long *) value);
1918 
1919     card = info->card;
1920     channel = (info->line) - (cy_card[card].first_line);
1921     chip = channel>>2;
1922     channel &= 0x03;
1923     index = cy_card[card].bus_index;
1924     base_addr = (unsigned char*)
1925                    (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1926 
1927     switch (cmd) {
1928     case TIOCMBIS:
1929         if (arg & TIOCM_RTS){
1930             save_flags(flags); cli();
1931                 base_addr[CyCAR<<index] = (u_char)channel;
1932                 base_addr[CyMSVR1<<index] = CyRTS;
1933             restore_flags(flags);
1934         }
1935         if (arg & TIOCM_DTR){
1936             save_flags(flags); cli();
1937             base_addr[CyCAR<<index] = (u_char)channel;
1938 /* CP('S');CP('2'); */
1939             base_addr[CyMSVR2<<index] = CyDTR;
1940 #ifdef SERIAL_DEBUG_DTR
1941             printk("cyc: %d: raising DTR\n", __LINE__);
1942             printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
1943 #endif
1944             restore_flags(flags);
1945         }
1946         break;
1947     case TIOCMBIC:
1948         if (arg & TIOCM_RTS){
1949             save_flags(flags); cli();
1950                 base_addr[CyCAR<<index] = (u_char)channel;
1951                 base_addr[CyMSVR1<<index] = ~CyRTS;
1952             restore_flags(flags);
1953         }
1954         if (arg & TIOCM_DTR){
1955             save_flags(flags); cli();
1956             base_addr[CyCAR<<index] = (u_char)channel;
1957 /* CP('C');CP('2'); */
1958             base_addr[CyMSVR2<<index] = ~CyDTR;
1959 #ifdef SERIAL_DEBUG_DTR
1960             printk("cyc: %d: dropping DTR\n", __LINE__);
1961             printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
1962 #endif
1963             restore_flags(flags);
1964         }
1965         break;
1966     case TIOCMSET:
1967         if (arg & TIOCM_RTS){
1968             save_flags(flags); cli();
1969                 base_addr[CyCAR<<index] = (u_char)channel;
1970                 base_addr[CyMSVR1<<index] = CyRTS;
1971             restore_flags(flags);
1972         }else{
1973             save_flags(flags); cli();
1974                 base_addr[CyCAR<<index] = (u_char)channel;
1975                 base_addr[CyMSVR1<<index] = ~CyRTS;
1976             restore_flags(flags);
1977         }
1978         if (arg & TIOCM_DTR){
1979             save_flags(flags); cli();
1980             base_addr[CyCAR<<index] = (u_char)channel;
1981 /* CP('S');CP('3'); */
1982             base_addr[CyMSVR2<<index] = CyDTR;
1983 #ifdef SERIAL_DEBUG_DTR
1984             printk("cyc: %d: raising DTR\n", __LINE__);
1985             printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
1986 #endif
1987             restore_flags(flags);
1988         }else{
1989             save_flags(flags); cli();
1990             base_addr[CyCAR<<index] = (u_char)channel;
1991 /* CP('C');CP('3'); */
1992             base_addr[CyMSVR2<<index] = ~CyDTR;
1993 #ifdef SERIAL_DEBUG_DTR
1994             printk("cyc: %d: dropping DTR\n", __LINE__);
1995             printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
1996 #endif
1997             restore_flags(flags);
1998         }
1999         break;
2000     default:
2001                 return -EINVAL;
2002         }
2003     return 0;
2004 } /* set_modem_info */
2005 
2006 static void
2007 send_break( struct cyclades_port * info, int duration)
     /* [previous][next][first][last][top][bottom][index][help] */
2008 { /* Let the transmit ISR take care of this (since it
2009      requires stuffing characters into the output stream).
2010    */
2011     info->x_break = duration;
2012     if (!info->xmit_cnt ) {
2013         start_xmit(info);
2014     }
2015 } /* send_break */
2016 
2017 static int
2018 get_mon_info(struct cyclades_port * info, struct cyclades_monitor * mon)
     /* [previous][next][first][last][top][bottom][index][help] */
2019 {
2020 
2021    memcpy_tofs(mon, &info->mon, sizeof(struct cyclades_monitor));
2022    info->mon.int_count  = 0;
2023    info->mon.char_count = 0;
2024    info->mon.char_max   = 0;
2025    info->mon.char_last  = 0;
2026    return 0;
2027 }
2028 
2029 static int
2030 set_threshold(struct cyclades_port * info, unsigned long value)
     /* [previous][next][first][last][top][bottom][index][help] */
2031 {
2032    unsigned char *base_addr;
2033    int card,channel,chip,index;
2034    
2035    card = info->card;
2036    channel = info->line - cy_card[card].first_line;
2037    chip = channel>>2;
2038    channel &= 0x03;
2039    index = cy_card[card].bus_index;
2040    base_addr = (unsigned char*)
2041                    (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
2042 
2043    info->cor3 &= ~CyREC_FIFO;
2044    info->cor3 |= value & CyREC_FIFO;
2045    base_addr[CyCOR3<<index] = info->cor3;
2046    write_cy_cmd(base_addr,CyCOR_CHANGE|CyCOR3ch,index);
2047    return 0;
2048 }
2049 
2050 static int
2051 get_threshold(struct cyclades_port * info, unsigned long *value)
     /* [previous][next][first][last][top][bottom][index][help] */
2052 {
2053    unsigned char *base_addr;
2054    int card,channel,chip,index;
2055    unsigned long tmp;
2056    
2057    card = info->card;
2058    channel = info->line - cy_card[card].first_line;
2059    chip = channel>>2;
2060    channel &= 0x03;
2061    index = cy_card[card].bus_index;
2062    base_addr = (unsigned char*)
2063                    (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
2064 
2065    tmp = base_addr[CyCOR3<<index] & CyREC_FIFO;
2066    put_fs_long(tmp,value);
2067    return 0;
2068 }
2069 
2070 static int
2071 set_default_threshold(struct cyclades_port * info, unsigned long value)
     /* [previous][next][first][last][top][bottom][index][help] */
2072 {
2073    info->default_threshold = value & 0x0f;
2074    return 0;
2075 }
2076 
2077 static int
2078 get_default_threshold(struct cyclades_port * info, unsigned long *value)
     /* [previous][next][first][last][top][bottom][index][help] */
2079 {
2080    put_fs_long(info->default_threshold,value);
2081    return 0;
2082 }
2083 
2084 static int
2085 set_timeout(struct cyclades_port * info, unsigned long value)
     /* [previous][next][first][last][top][bottom][index][help] */
2086 {
2087    unsigned char *base_addr;
2088    int card,channel,chip,index;
2089    
2090    card = info->card;
2091    channel = info->line - cy_card[card].first_line;
2092    chip = channel>>2;
2093    channel &= 0x03;
2094    index = cy_card[card].bus_index;
2095    base_addr = (unsigned char*)
2096                    (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
2097 
2098    base_addr[CyRTPR<<index] = value & 0xff;
2099    return 0;
2100 }
2101 
2102 static int
2103 get_timeout(struct cyclades_port * info, unsigned long *value)
     /* [previous][next][first][last][top][bottom][index][help] */
2104 {
2105    unsigned char *base_addr;
2106    int card,channel,chip,index;
2107    unsigned long tmp;
2108    
2109    card = info->card;
2110    channel = info->line - cy_card[card].first_line;
2111    chip = channel>>2;
2112    channel &= 0x03;
2113    index = cy_card[card].bus_index;
2114    base_addr = (unsigned char*)
2115                    (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
2116 
2117    tmp = base_addr[CyRTPR<<index];
2118    put_fs_long(tmp,value);
2119    return 0;
2120 }
2121 
2122 static int
2123 set_default_timeout(struct cyclades_port * info, unsigned long value)
     /* [previous][next][first][last][top][bottom][index][help] */
2124 {
2125    info->default_timeout = value & 0xff;
2126    return 0;
2127 }
2128 
2129 static int
2130 get_default_timeout(struct cyclades_port * info, unsigned long *value)
     /* [previous][next][first][last][top][bottom][index][help] */
2131 {
2132    put_fs_long(info->default_timeout,value);
2133    return 0;
2134 }
2135 
2136 static int
2137 cy_ioctl(struct tty_struct *tty, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
2138             unsigned int cmd, unsigned long arg)
2139 {
2140   int error;
2141   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
2142   int ret_val = 0;
2143 
2144 #ifdef SERIAL_DEBUG_OTHER
2145     printk("cy_ioctl ttyC%d, cmd = %x arg = %lx\n", info->line, cmd, arg); /* */
2146 #endif
2147 
2148     switch (cmd) {
2149         case CYGETMON:
2150             error = verify_area(VERIFY_WRITE, (void *) arg
2151                                 ,sizeof(struct cyclades_monitor));
2152             if (error){
2153                 ret_val = error;
2154                 break;
2155             }
2156             ret_val = get_mon_info(info, (struct cyclades_monitor *)arg);
2157             break;
2158         case CYGETTHRESH:
2159             error = verify_area(VERIFY_WRITE, (void *) arg
2160                                 ,sizeof(unsigned long));
2161             if (error){
2162                 ret_val = error;
2163                 break;
2164             }
2165             ret_val = get_threshold(info, (unsigned long *)arg);
2166             break;
2167         case CYSETTHRESH:
2168             ret_val = set_threshold(info, (unsigned long)arg);
2169             break;
2170         case CYGETDEFTHRESH:
2171             error = verify_area(VERIFY_WRITE, (void *) arg
2172                                 ,sizeof(unsigned long));
2173             if (error){
2174                 ret_val = error;
2175                 break;
2176             }
2177             ret_val = get_default_threshold(info, (unsigned long *)arg);
2178             break;
2179         case CYSETDEFTHRESH:
2180             ret_val = set_default_threshold(info, (unsigned long)arg);
2181             break;
2182         case CYGETTIMEOUT:
2183             error = verify_area(VERIFY_WRITE, (void *) arg
2184                                 ,sizeof(unsigned long));
2185             if (error){
2186                 ret_val = error;
2187                 break;
2188             }
2189             ret_val = get_timeout(info, (unsigned long *)arg);
2190             break;
2191         case CYSETTIMEOUT:
2192             ret_val = set_timeout(info, (unsigned long)arg);
2193             break;
2194         case CYGETDEFTIMEOUT:
2195             error = verify_area(VERIFY_WRITE, (void *) arg
2196                                 ,sizeof(unsigned long));
2197             if (error){
2198                 ret_val = error;
2199                 break;
2200             }
2201             ret_val = get_default_timeout(info, (unsigned long *)arg);
2202             break;
2203         case CYSETDEFTIMEOUT:
2204             ret_val = set_default_timeout(info, (unsigned long)arg);
2205             break;
2206         case TCSBRK:    /* SVID version: non-zero arg --> no break */
2207             ret_val = tty_check_change(tty);
2208             if (ret_val)
2209                 return ret_val;
2210             tty_wait_until_sent(tty,0);
2211             if (!arg)
2212                 send_break(info, HZ/4); /* 1/4 second */
2213             break;
2214         case TCSBRKP:   /* support for POSIX tcsendbreak() */
2215             ret_val = tty_check_change(tty);
2216             if (ret_val)
2217                 return ret_val;
2218             tty_wait_until_sent(tty,0);
2219             send_break(info, arg ? arg*(HZ/10) : HZ/4);
2220             break;
2221         case TIOCMBIS:
2222         case TIOCMBIC:
2223         case TIOCMSET:
2224             ret_val = set_modem_info(info, cmd, (unsigned int *) arg);
2225             break;
2226 
2227 /* The following commands are incompletely implemented!!! */
2228         case TIOCGSOFTCAR:
2229             error = verify_area(VERIFY_WRITE, (void *) arg
2230                                 ,sizeof(unsigned int *));
2231             if (error){
2232                 ret_val = error;
2233                 break;
2234             }
2235             put_fs_long(C_CLOCAL(tty) ? 1 : 0,
2236                         (unsigned long *) arg);
2237             break;
2238         case TIOCSSOFTCAR:
2239             arg = get_fs_long((unsigned long *) arg);
2240             tty->termios->c_cflag =
2241                     ((tty->termios->c_cflag & ~CLOCAL) |
2242                      (arg ? CLOCAL : 0));
2243             break;
2244         case TIOCMGET:
2245             error = verify_area(VERIFY_WRITE, (void *) arg
2246                                 ,sizeof(unsigned int *));
2247             if (error){
2248                 ret_val = error;
2249                 break;
2250             }
2251             ret_val = get_modem_info(info, (unsigned int *) arg);
2252             break;
2253         case TIOCGSERIAL:
2254             error = verify_area(VERIFY_WRITE, (void *) arg
2255                                 ,sizeof(struct serial_struct));
2256             if (error){
2257                 ret_val = error;
2258                 break;
2259             }
2260             ret_val = get_serial_info(info,
2261                                    (struct serial_struct *) arg);
2262             break;
2263         case TIOCSSERIAL:
2264             ret_val = set_serial_info(info,
2265                                    (struct serial_struct *) arg);
2266             break;
2267         default:
2268             ret_val = -ENOIOCTLCMD;
2269     }
2270 
2271 #ifdef SERIAL_DEBUG_OTHER
2272     printk("cy_ioctl done\n");
2273 #endif
2274 
2275     return ret_val;
2276 } /* cy_ioctl */
2277 
2278 
2279 
2280 
2281 static void
2282 cy_set_termios(struct tty_struct *tty, struct termios * old_termios)
     /* [previous][next][first][last][top][bottom][index][help] */
2283 {
2284   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
2285 
2286 #ifdef SERIAL_DEBUG_OTHER
2287     printk("cy_set_termios ttyC%d\n", info->line);
2288 #endif
2289 
2290     if (tty->termios->c_cflag == old_termios->c_cflag)
2291         return;
2292     config_setup(info);
2293 
2294     if ((old_termios->c_cflag & CRTSCTS) &&
2295         !(tty->termios->c_cflag & CRTSCTS)) {
2296             tty->stopped = 0;
2297             cy_start(tty);
2298     }
2299 #ifdef tytso_patch_94Nov25_1726
2300     if (!(old_termios->c_cflag & CLOCAL) &&
2301         (tty->termios->c_cflag & CLOCAL))
2302             wake_up_interruptible(&info->open_wait);
2303 #endif
2304 
2305     return;
2306 } /* cy_set_termios */
2307 
2308 
2309 static void
2310 cy_close(struct tty_struct * tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
2311 {
2312   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
2313   unsigned long flags;
2314 
2315 /* CP('C'); */
2316 #ifdef SERIAL_DEBUG_OTHER
2317     printk("cy_close ttyC%d\n", info->line);
2318 #endif
2319 
2320     if (!info
2321     || serial_paranoia_check(info, tty->device, "cy_close")){
2322         return;
2323     }
2324 #ifdef SERIAL_DEBUG_OPEN
2325     printk("cy_close ttyC%d, count = %d\n", info->line, info->count);
2326 #endif
2327 
2328     save_flags(flags); cli();
2329 
2330     /* If the TTY is being hung up, nothing to do */
2331     if (tty_hung_up_p(filp)) {
2332         restore_flags(flags);
2333         return;
2334     }
2335         
2336     if ((tty->count == 1) && (info->count != 1)) {
2337         /*
2338          * Uh, oh.  tty->count is 1, which means that the tty
2339          * structure will be freed.  Info->count should always
2340          * be one in these conditions.  If it's greater than
2341          * one, we've got real problems, since it means the
2342          * serial port won't be shutdown.
2343          */
2344         printk("cy_close: bad serial port count; tty->count is 1, "
2345            "info->count is %d\n", info->count);
2346         info->count = 1;
2347     }
2348 #ifdef SERIAL_DEBUG_COUNT
2349     printk("cyc: %d(%d): decrementing count to %d\n", __LINE__, current->pid, info->count - 1);
2350 #endif
2351     if (--info->count < 0) {
2352 #ifdef SERIAL_DEBUG_COUNT
2353     printk("cyc: %d: setting count to 0\n", __LINE__);
2354 #endif
2355         info->count = 0;
2356     }
2357     if (info->count)
2358     {
2359         MOD_DEC_USE_COUNT;
2360         restore_flags(flags);
2361         return;
2362     }
2363     info->flags |= ASYNC_CLOSING;
2364     /*
2365      * Save the termios structure, since this port may have
2366      * separate termios for callout and dialin.
2367      */
2368     if (info->flags & ASYNC_NORMAL_ACTIVE)
2369         info->normal_termios = *tty->termios;
2370     if (info->flags & ASYNC_CALLOUT_ACTIVE)
2371         info->callout_termios = *tty->termios;
2372     if (info->flags & ASYNC_INITIALIZED)
2373         tty_wait_until_sent(tty, 3000); /* 30 seconds timeout */
2374     shutdown(info);
2375     if (tty->driver.flush_buffer)
2376         tty->driver.flush_buffer(tty);
2377     if (tty->ldisc.flush_buffer)
2378         tty->ldisc.flush_buffer(tty);
2379     info->event = 0;
2380     info->tty = 0;
2381     if (info->blocked_open) {
2382         if (info->close_delay) {
2383             current->state = TASK_INTERRUPTIBLE;
2384             current->timeout = jiffies + info->close_delay;
2385             schedule();
2386         }
2387         wake_up_interruptible(&info->open_wait);
2388     }
2389     info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
2390                      ASYNC_CLOSING);
2391     wake_up_interruptible(&info->close_wait);
2392 
2393 #ifdef SERIAL_DEBUG_OTHER
2394     printk("cy_close done\n");
2395 #endif
2396 
2397     MOD_DEC_USE_COUNT;
2398     restore_flags(flags);
2399     return;
2400 } /* cy_close */
2401 
2402 /*
2403  * cy_hangup() --- called by tty_hangup() when a hangup is signaled.
2404  */
2405 void
2406 cy_hangup(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
2407 {
2408   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
2409         
2410 #ifdef SERIAL_DEBUG_OTHER
2411     printk("cy_hangup ttyC%d\n", info->line); /* */
2412 #endif
2413 
2414     if (serial_paranoia_check(info, tty->device, "cy_hangup"))
2415         return;
2416     
2417     shutdown(info);
2418     info->event = 0;
2419     info->count = 0;
2420 #ifdef SERIAL_DEBUG_COUNT
2421     printk("cyc: %d(%d): setting count to 0\n", __LINE__, current->pid);
2422 #endif
2423     info->tty = 0;
2424     info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2425     wake_up_interruptible(&info->open_wait);
2426 } /* cy_hangup */
2427 
2428 
2429 
2430 /*
2431  * ------------------------------------------------------------
2432  * cy_open() and friends
2433  * ------------------------------------------------------------
2434  */
2435 
2436 static int
2437 block_til_ready(struct tty_struct *tty, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
2438                            struct cyclades_port *info)
2439 {
2440   struct wait_queue wait = { current, NULL };
2441   struct cyclades_card *cinfo;
2442   unsigned long flags;
2443   int chip, channel,index;
2444   int retval;
2445   char *base_addr;
2446 
2447     /*
2448      * If the device is in the middle of being closed, then block
2449      * until it's done, and then try again.
2450      */
2451     if (info->flags & ASYNC_CLOSING) {
2452         interruptible_sleep_on(&info->close_wait);
2453         if (info->flags & ASYNC_HUP_NOTIFY){
2454             return -EAGAIN;
2455         }else{
2456             return -ERESTARTSYS;
2457         }
2458     }
2459 
2460     /*
2461      * If this is a callout device, then just make sure the normal
2462      * device isn't being used.
2463      */
2464     if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2465         if (info->flags & ASYNC_NORMAL_ACTIVE){
2466             return -EBUSY;
2467         }
2468         if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2469             (info->flags & ASYNC_SESSION_LOCKOUT) &&
2470             (info->session != current->session)){
2471             return -EBUSY;
2472         }
2473         if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2474             (info->flags & ASYNC_PGRP_LOCKOUT) &&
2475             (info->pgrp != current->pgrp)){
2476             return -EBUSY;
2477         }
2478         info->flags |= ASYNC_CALLOUT_ACTIVE;
2479         return 0;
2480     }
2481 
2482     /*
2483      * If non-blocking mode is set, then make the check up front
2484      * and then exit.
2485      */
2486     if (filp->f_flags & O_NONBLOCK) {
2487         if (info->flags & ASYNC_CALLOUT_ACTIVE){
2488             return -EBUSY;
2489         }
2490         info->flags |= ASYNC_NORMAL_ACTIVE;
2491         return 0;
2492     }
2493 
2494     /*
2495      * Block waiting for the carrier detect and the line to become
2496      * free (i.e., not in use by the callout).  While we are in
2497      * this loop, info->count is dropped by one, so that
2498      * cy_close() knows when to free things.  We restore it upon
2499      * exit, either normal or abnormal.
2500      */
2501     retval = 0;
2502     add_wait_queue(&info->open_wait, &wait);
2503 #ifdef SERIAL_DEBUG_OPEN
2504     printk("block_til_ready before block: ttyC%d, count = %d\n",
2505            info->line, info->count);/**/
2506 #endif
2507     info->count--;
2508 #ifdef SERIAL_DEBUG_COUNT
2509     printk("cyc: %d(%d): decrementing count to %d\n", __LINE__, current->pid, info->count);
2510 #endif
2511     info->blocked_open++;
2512 
2513     cinfo = &cy_card[info->card];
2514     channel = info->line - cinfo->first_line;
2515     chip = channel>>2;
2516     channel &= 0x03;
2517     index = cinfo->bus_index;
2518     base_addr = (char *) (cinfo->base_addr + (cy_chip_offset[chip]<<index));
2519 
2520     while (1) {
2521         save_flags(flags); cli();
2522             if (!(info->flags & ASYNC_CALLOUT_ACTIVE)){
2523                 base_addr[CyCAR<<index] = (u_char)channel;
2524                 base_addr[CyMSVR1<<index] = CyRTS;
2525 /* CP('S');CP('4'); */
2526                 base_addr[CyMSVR2<<index] = CyDTR;
2527 #ifdef SERIAL_DEBUG_DTR
2528                 printk("cyc: %d: raising DTR\n", __LINE__);
2529                 printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
2530 #endif
2531             }
2532         restore_flags(flags);
2533         current->state = TASK_INTERRUPTIBLE;
2534         if (tty_hung_up_p(filp)
2535         || !(info->flags & ASYNC_INITIALIZED) ){
2536             if (info->flags & ASYNC_HUP_NOTIFY) {
2537                 retval = -EAGAIN;
2538             }else{
2539                 retval = -ERESTARTSYS;
2540             }
2541             break;
2542         }
2543         save_flags(flags); cli();
2544             base_addr[CyCAR<<index] = (u_char)channel;
2545 /* CP('L');CP1(1 && C_CLOCAL(tty)); CP1(1 && (base_addr[CyMSVR1<<index] & CyDCD) ); */
2546             if (!(info->flags & ASYNC_CALLOUT_ACTIVE)
2547             && !(info->flags & ASYNC_CLOSING)
2548             && (C_CLOCAL(tty)
2549                 || (base_addr[CyMSVR1<<index] & CyDCD))) {
2550                     restore_flags(flags);
2551                     break;
2552             }
2553         restore_flags(flags);
2554         if (current->signal & ~current->blocked) {
2555             retval = -ERESTARTSYS;
2556             break;
2557         }
2558 #ifdef SERIAL_DEBUG_OPEN
2559         printk("block_til_ready blocking: ttyC%d, count = %d\n",
2560                info->line, info->count);/**/
2561 #endif
2562         schedule();
2563     }
2564     current->state = TASK_RUNNING;
2565     remove_wait_queue(&info->open_wait, &wait);
2566     if (!tty_hung_up_p(filp)){
2567         info->count++;
2568 #ifdef SERIAL_DEBUG_COUNT
2569     printk("cyc: %d(%d): incrementing count to %d\n", __LINE__, current->pid, info->count);
2570 #endif
2571     }
2572     info->blocked_open--;
2573 #ifdef SERIAL_DEBUG_OPEN
2574     printk("block_til_ready after blocking: ttyC%d, count = %d\n",
2575            info->line, info->count);/**/
2576 #endif
2577     if (retval)
2578             return retval;
2579     info->flags |= ASYNC_NORMAL_ACTIVE;
2580     return 0;
2581 } /* block_til_ready */
2582 
2583 /*
2584  * This routine is called whenever a serial port is opened.  It
2585  * performs the serial-specific initialization for the tty structure.
2586  */
2587 int
2588 cy_open(struct tty_struct *tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
2589 {
2590   struct cyclades_port  *info;
2591   int retval, line;
2592 
2593 /* CP('O'); */
2594     line = MINOR(tty->device) - tty->driver.minor_start;
2595     if ((line < 0) || (NR_PORTS <= line)){
2596         return -ENODEV;
2597     }
2598     info = &cy_port[line];
2599     if (info->line < 0){
2600         return -ENODEV;
2601     }
2602 #ifdef SERIAL_DEBUG_OTHER
2603     printk("cy_open ttyC%d\n", info->line); /* */
2604 #endif
2605     if (serial_paranoia_check(info, tty->device, "cy_open")){
2606         return -ENODEV;
2607     }
2608 #ifdef SERIAL_DEBUG_OPEN
2609     printk("cy_open ttyC%d, count = %d\n", info->line, info->count);/**/
2610 #endif
2611     info->count++;
2612 #ifdef SERIAL_DEBUG_COUNT
2613     printk("cyc: %d(%d): incrementing count to %d\n", __LINE__, current->pid, info->count);
2614 #endif
2615     tty->driver_data = info;
2616     info->tty = tty;
2617 
2618     if (!tmp_buf) {
2619         tmp_buf = (unsigned char *) get_free_page(GFP_KERNEL);
2620         if (!tmp_buf){
2621             return -ENOMEM;
2622         }
2623     }
2624 
2625     if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
2626         if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2627             *tty->termios = info->normal_termios;
2628         else 
2629             *tty->termios = info->callout_termios;
2630     }
2631     /*
2632      * Start up serial port
2633      */
2634     retval = startup(info);
2635     if (retval){
2636         return retval;
2637     }
2638 
2639     retval = block_til_ready(tty, filp, info);
2640     if (retval) {
2641 #ifdef SERIAL_DEBUG_OPEN
2642         printk("cy_open returning after block_til_ready with %d\n",
2643                retval);
2644 #endif
2645         return retval;
2646     }
2647 
2648     info->session = current->session;
2649     info->pgrp = current->pgrp;
2650 
2651 #ifdef SERIAL_DEBUG_OPEN
2652     printk("cy_open done\n");/**/
2653 #endif
2654     MOD_INC_USE_COUNT;
2655     return 0;
2656 } /* cy_open */
2657 
2658 
2659 
2660 /*
2661  * ---------------------------------------------------------------------
2662  * cy_init() and friends
2663  *
2664  * cy_init() is called at boot-time to initialize the serial driver.
2665  * ---------------------------------------------------------------------
2666  */
2667 
2668 /*
2669  * This routine prints out the appropriate serial driver version
2670  * number, and identifies which options were configured into this
2671  * driver.
2672  */
2673 static void
2674 show_version(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2675 {
2676     printk("Cyclom driver %s\n",rcsid);
2677 } /* show_version */
2678 
2679 /* initialize chips on card -- return number of valid
2680    chips (which is number of ports/4) */
2681 int
2682 cy_init_card(unsigned char *true_base_addr,int index)
     /* [previous][next][first][last][top][bottom][index][help] */
2683 {
2684   unsigned int chip_number;
2685   unsigned char* base_addr;
2686 
2687     true_base_addr[Cy_HwReset<<index] = 0; /* Cy_HwReset is 0x1400 */
2688     true_base_addr[Cy_ClrIntr<<index] = 0; /* Cy_ClrIntr is 0x1800 */
2689     udelay(500L);
2690 
2691     for(chip_number=0; chip_number<CyMaxChipsPerCard; chip_number++){
2692         base_addr = true_base_addr + (cy_chip_offset[chip_number]<<index);
2693         udelay(1000L);
2694         if(base_addr[CyCCR<<index] != 0x00){
2695             /*************
2696             printk(" chip #%d at %#6lx is never idle (CCR != 0)\n",
2697                chip_number, (unsigned long)base_addr);
2698             *************/
2699             return chip_number;
2700         }
2701 
2702         base_addr[CyGFRCR<<index] = 0;
2703         udelay(10L);
2704 
2705         /* The Cyclom-16Y does not decode address bit 9 and therefore
2706            cannot distinguish between references to chip 0 and a non-
2707            existent chip 4.  If the preceding clearing of the supposed
2708            chip 4 GFRCR register appears at chip 0, there is no chip 4
2709            and this must be a Cyclom-16Y, not a Cyclom-32Ye.
2710         */
2711         if (chip_number == 4
2712         && *(true_base_addr + (cy_chip_offset[0]<<index) + (CyGFRCR<<index)) == 0){
2713             return chip_number;
2714         }
2715 
2716         base_addr[CyCCR<<index] = CyCHIP_RESET;
2717         udelay(1000L);
2718 
2719         if(base_addr[CyGFRCR<<index] == 0x00){
2720             /*
2721             printk(" chip #%d at %#6lx is not responding (GFRCR stayed 0)\n",
2722                chip_number, (unsigned long)base_addr);
2723             */
2724             return chip_number;
2725         }
2726         if((0xf0 & base_addr[CyGFRCR<<index]) != 0x40){
2727             /*
2728             printk(" chip #%d at %#6lx is not valid (GFRCR == %#2x)\n",
2729                chip_number, (unsigned long)base_addr, base_addr[CyGFRCR<<index]);
2730             */
2731             return chip_number;
2732         }
2733         base_addr[CyGCR<<index] = CyCH0_SERIAL;
2734         base_addr[CyPPR<<index] = 244; /* better value than CyCLOCK_25_1MS * 5
2735                                                   to run clock at 200 Hz */
2736 
2737         /*
2738         printk(" chip #%d at %#6lx is rev 0x%2x\n",
2739                chip_number, (unsigned long)base_addr, base_addr[CyGFRCR<<index]);
2740         */
2741     }
2742 
2743     return chip_number;
2744 } /* cy_init_card */
2745 
2746 /* The serial driver boot-time initialization code!
2747     Hardware I/O ports are mapped to character special devices on a
2748     first found, first allocated manner.  That is, this code searches
2749     for Cyclom cards in the system.  As each is found, it is probed
2750     to discover how many chips (and thus how many ports) are present.
2751     These ports are mapped to the tty ports 32 and upward in monotonic
2752     fashion.  If an 8-port card is replaced with a 16-port card, the
2753     port mapping on a following card will shift.
2754 
2755     This approach is different from what is used in the other serial
2756     device driver because the Cyclom is more properly a multiplexer,
2757     not just an aggregation of serial ports on one card.
2758 
2759     If there are more cards with more ports than have been statically
2760     allocated above, a warning is printed and the extra ports are ignored.
2761  */
2762 int
2763 cy_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2764 {
2765   struct cyclades_port  *info;
2766   struct cyclades_card *cinfo;
2767   int                   board,port,i;
2768 
2769     show_version();
2770 
2771     /* Initialize the tty_driver structure */
2772     
2773     memset(&cy_serial_driver, 0, sizeof(struct tty_driver));
2774     cy_serial_driver.magic = TTY_DRIVER_MAGIC;
2775     cy_serial_driver.name = "ttyC";
2776     cy_serial_driver.major = CYCLADES_MAJOR;
2777     cy_serial_driver.minor_start = 32;
2778     cy_serial_driver.num = NR_PORTS;
2779     cy_serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2780     cy_serial_driver.subtype = SERIAL_TYPE_NORMAL;
2781     cy_serial_driver.init_termios = tty_std_termios;
2782     cy_serial_driver.init_termios.c_cflag =
2783             B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2784     cy_serial_driver.flags = TTY_DRIVER_REAL_RAW;
2785     cy_serial_driver.refcount = &serial_refcount;
2786     cy_serial_driver.table = serial_table;
2787     cy_serial_driver.termios = serial_termios;
2788     cy_serial_driver.termios_locked = serial_termios_locked;
2789     cy_serial_driver.open = cy_open;
2790     cy_serial_driver.close = cy_close;
2791     cy_serial_driver.write = cy_write;
2792     cy_serial_driver.put_char = cy_put_char;
2793     cy_serial_driver.flush_chars = cy_flush_chars;
2794     cy_serial_driver.write_room = cy_write_room;
2795     cy_serial_driver.chars_in_buffer = cy_chars_in_buffer;
2796     cy_serial_driver.flush_buffer = cy_flush_buffer;
2797     cy_serial_driver.ioctl = cy_ioctl;
2798     cy_serial_driver.throttle = cy_throttle;
2799     cy_serial_driver.unthrottle = cy_unthrottle;
2800     cy_serial_driver.set_termios = cy_set_termios;
2801     cy_serial_driver.stop = cy_stop;
2802     cy_serial_driver.start = cy_start;
2803     cy_serial_driver.hangup = cy_hangup;
2804 
2805     /*
2806      * The callout device is just like normal device except for
2807      * major number and the subtype code.
2808      */
2809     cy_callout_driver = cy_serial_driver;
2810     cy_callout_driver.name = "cub";
2811     cy_callout_driver.major = CYCLADESAUX_MAJOR;
2812     cy_callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2813 
2814     if (tty_register_driver(&cy_serial_driver))
2815             panic("Couldn't register Cyclom serial driver\n");
2816     if (tty_register_driver(&cy_callout_driver))
2817             panic("Couldn't register Cyclom callout driver\n");
2818 
2819     bh_base[CYCLADES_BH].routine = do_cyclades_bh;
2820     enable_bh(CYCLADES_BH);
2821 
2822     for (i = 0; i < 16; i++) {
2823             IRQ_cards[i] = 0;
2824     }
2825 
2826     for (i = 0; i < NR_CARDS; i++) {
2827             /* base_addr=0 indicates board not found */
2828             cy_card[i].base_addr = 0;
2829     }
2830 
2831     /* the code below is responsible to find the boards. Each different
2832        type of board has its own detection routine. If a board is found,
2833        the next cy_card structure available is set by the detection
2834        routine. These funcions are responsible for checking the availability
2835        of cy_card and cy_port data structures and updating the
2836        cy_next_channel. */
2837 
2838     /* look for isa boards */
2839     cy_isa_nboard = cy_detect_isa();
2840 
2841     /* look for pci boards */
2842     cy_pci_nboard = cy_detect_pci();
2843 
2844     cy_nboard = cy_isa_nboard + cy_pci_nboard;
2845 
2846     /* invalidate remaining cy_card structures */
2847     for (i = 0 ; i < NR_CARDS ; i++) {
2848         if (cy_card[i].base_addr == 0) {
2849                 cy_card[i].first_line = -1;
2850         }
2851     }
2852     /* invalidate remaining cy_port structures */
2853     for (i = cy_next_channel ; i < NR_PORTS ; i++) {
2854         cy_port[i].line = -1;
2855         cy_port[i].magic = -1;
2856     }
2857 
2858     /* initialize per-port data structures for each valid board found */
2859     for (board = 0 ; board < cy_nboard ; board++) {
2860             cinfo = &cy_card[board];
2861             for (port = cinfo->first_line ;
2862                  port < cinfo->first_line + 4*cinfo->num_chips ;
2863                  port++)
2864             {
2865                 info = &cy_port[port];
2866                 info->magic = CYCLADES_MAGIC;
2867                 info->type = PORT_CIRRUS;
2868                 info->card = board;
2869                 info->line = port;
2870                 info->flags = STD_COM_FLAGS;
2871                 info->tty = 0;
2872                 info->xmit_fifo_size = 12;
2873                 info->cor1 = CyPARITY_NONE|Cy_1_STOP|Cy_8_BITS;
2874                 info->cor2 = CyETC;
2875                 info->cor3 = 0x08; /* _very_ small receive threshold */
2876                 info->cor4 = 0;
2877                 info->cor5 = 0;
2878                 info->tbpr = baud_bpr[13]; /* Tx BPR */
2879                 info->tco = baud_co[13]; /* Tx CO */
2880                 info->rbpr = baud_bpr[13]; /* Rx BPR */
2881                 info->rco = baud_co[13]; /* Rx CO */
2882                 info->close_delay = 0;
2883                 info->x_char = 0;
2884                 info->event = 0;
2885                 info->count = 0;
2886 #ifdef SERIAL_DEBUG_COUNT
2887     printk("cyc: %d: setting count to 0\n", __LINE__);
2888 #endif
2889                 info->blocked_open = 0;
2890                 info->default_threshold = 0;
2891                 info->default_timeout = 0;
2892                 info->tqueue.routine = do_softint;
2893                 info->tqueue.data = info;
2894                 info->callout_termios =cy_callout_driver.init_termios;
2895                 info->normal_termios = cy_serial_driver.init_termios;
2896                 info->open_wait = 0;
2897                 info->close_wait = 0;
2898                 /* info->session */
2899                 /* info->pgrp */
2900                 info->read_status_mask = CyTIMEOUT| CySPECHAR| CyBREAK
2901                                        | CyPARITY| CyFRAME| CyOVERRUN;
2902                 /* info->timeout */
2903             }
2904     }
2905     return 0;
2906     
2907 } /* cy_init */
2908 
2909 #ifdef MODULE
2910 int
2911 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2912 {
2913    return(cy_init());
2914 }
2915 
2916 void
2917 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2918 {
2919     int i;
2920 
2921 
2922     if (tty_unregister_driver(&cy_callout_driver))
2923             printk("Couldn't unregister Cyclom callout driver\n");
2924     if (tty_unregister_driver(&cy_serial_driver))
2925             printk("Couldn't unregister Cyclom serial driver\n");
2926 
2927     for (i = 0; i < NR_CARDS; i++) {
2928         if (cy_card[i].base_addr != 0)
2929         {
2930             free_irq(cy_card[i].irq);
2931         }
2932     }
2933 }
2934 #endif
2935 
2936 /*
2937  * ---------------------------------------------------------------------
2938  * cy_detect_isa() - Probe for Cyclom-Y/ISA boards.
2939  * sets global variables and return the number of ISA boards found.
2940  * ---------------------------------------------------------------------
2941  */
2942 int
2943 cy_detect_isa()
     /* [previous][next][first][last][top][bottom][index][help] */
2944 {
2945   unsigned int          cy_isa_irq,nboard;
2946   unsigned char         *cy_isa_address;
2947   unsigned short        i,j,cy_isa_nchan;
2948 
2949         nboard = 0;
2950 
2951         /* scan the address table probing for Cyclom-Y/ISA boards */
2952         for (i = 0 ; i < NR_ISA_ADDRESSES ; i++) {
2953                 cy_isa_address = cy_isa_addresses[i];
2954                 if (cy_isa_address  == 0x0000) {
2955                         return(nboard);
2956                 }
2957 
2958                 /* probe for CD1400... */
2959                 cy_isa_nchan = 4 * cy_init_card(cy_isa_address,0);
2960                 if (cy_isa_nchan == 0) {
2961                         continue;
2962                 }
2963 
2964                 /* find out the board's irq by probing */
2965                 cy_isa_irq = do_auto_irq(cy_isa_address);
2966                 if (cy_isa_irq == 0) {
2967                         printk("Cyclom-Y/ISA found at 0x%x but the IRQ could not be detected.\n",
2968                                 (unsigned int) cy_isa_address);
2969                         continue;
2970                 }
2971 
2972                 if((cy_next_channel+cy_isa_nchan) > NR_PORTS) {
2973                         printk("Cyclom-Y/ISA found at 0x%x but no more channel structures are available.\n",
2974                                 (unsigned int) cy_isa_address);
2975                         return(nboard);
2976                 }
2977                 /* fill the next cy_card structure available */
2978                 for (j = 0 ; j < NR_CARDS ; j++) {
2979                         if (cy_card[j].base_addr == 0)  break;
2980                 }
2981                 if (j == NR_CARDS) {    /* no more cy_cards available */
2982                         printk("Cyclom-Y/ISA found at 0x%x but no more card structures are available.\n",
2983                                 (unsigned int) cy_isa_address);
2984                         return(nboard);
2985                 }
2986 
2987                 /* allocate IRQ */
2988                 if(request_irq(cy_isa_irq,cy_interrupt,SA_INTERRUPT,"cyclades"))
2989                 {
2990                         printk("Cyclom-Y/ISA found at 0x%x but could not allocate interrupt IRQ#%d.\n",
2991                                 (unsigned int) cy_isa_address,cy_isa_irq);
2992                         return(nboard);
2993                 }
2994 
2995                 /* set cy_card */
2996                 cy_card[j].base_addr = (int) cy_isa_address;
2997                 cy_card[j].irq = (int) cy_isa_irq;
2998                 cy_card[j].bus_index = 0;
2999                 cy_card[j].first_line = cy_next_channel;
3000                 cy_card[j].num_chips = cy_isa_nchan/4;
3001                 IRQ_cards[cy_isa_irq] = &cy_card[j];
3002                 nboard++;
3003                         
3004                 /* print message */
3005                 printk("Cyclom-Y/ISA #%d: 0x%x-0x%x, IRQ%d, %d channels starting from port %d.\n",
3006                         j+1,(unsigned int) cy_isa_address,
3007                         (unsigned int)(cy_isa_address + 0x1fff),
3008                         cy_isa_irq,cy_isa_nchan,cy_next_channel);
3009                 cy_next_channel += cy_isa_nchan;
3010         }
3011         return(nboard);
3012 
3013 }
3014 
3015 /*
3016  * ---------------------------------------------------------------------
3017  * cy_detect_pci() - Test PCI bus presence and Cyclom-Ye/PCI.
3018  * sets global variables and return the number of PCI boards found.
3019  * ---------------------------------------------------------------------
3020  */
3021 int
3022 cy_detect_pci()
     /* [previous][next][first][last][top][bottom][index][help] */
3023 {
3024   unsigned char         cyy_bus, cyy_dev_fn, cyy_rev_id;
3025   unsigned long         pci_intr_ctrl;
3026   unsigned char         cy_pci_irq;
3027   unsigned int          cy_pci_address, cy_pci_io;
3028   unsigned short        i,j,cy_pci_nchan;
3029 
3030 #ifndef CONFIG_PCI
3031         printk ("Kernel without PCI support.\n");
3032         return(0);
3033 #else
3034         if(pcibios_present() == 0) {    /* PCI bus not present */
3035                 return(0);
3036         }
3037         for (i = 0; i < NR_CARDS; i++) {
3038                 /* look for a Cyclom-Y card by vendor and device id */
3039                 if(pcibios_find_device (PCI_VENDOR_ID_CYCLADES,
3040                                         PCI_DEVICE_ID_CYCLOMY,i,
3041                                         &cyy_bus, &cyy_dev_fn) != 0)
3042                 {
3043                         break;
3044                 }
3045                 /* read PCI configuration area */
3046                 pcibios_read_config_byte(cyy_bus, cyy_dev_fn,
3047                                  PCI_INTERRUPT_LINE, &cy_pci_irq);
3048                 pcibios_read_config_dword(cyy_bus, cyy_dev_fn,
3049                                   PCI_BASE_ADDRESS_1, &cy_pci_io);
3050                 pcibios_read_config_dword(cyy_bus, cyy_dev_fn,
3051                                   PCI_BASE_ADDRESS_2, &cy_pci_address);
3052                 pcibios_read_config_byte(cyy_bus, cyy_dev_fn,
3053                                   PCI_REVISION_ID, &cyy_rev_id);
3054                 cy_pci_address &= 0xfffffff0;
3055                 cy_pci_io  &= 0xfffffffc;
3056                 cy_pci_nchan = 4 * cy_init_card((unsigned char *)
3057                                                 cy_pci_address,1);
3058                 if(cy_pci_nchan == 0) {
3059                         printk("Cyclom-Y PCI host card with no Serial-Modules at 0x%x.\n",
3060                                 (unsigned int) cy_pci_address);
3061                         continue;
3062                 }
3063                 if((cy_next_channel+cy_pci_nchan) > NR_PORTS) {
3064                         printk("Cyclom-Y/PCI found at 0x%x but no more channel structures are available.\n",
3065                                 (unsigned int) cy_pci_address);
3066                         return(i);
3067                 }
3068 #ifdef CY_PCI_DEBUG
3069                 printk("Cyclom-Ye/PCI #%d (bus=0x0%x, pci_id=0x%x, rev_id=%d).\n",
3070                         i+1,cyy_bus,cyy_dev_fn,cyy_rev_id);
3071                 printk("Cyclom-Ye/PCI: found at 0x%x, IRQ%d, ioaddr = 0x%lx.\n",
3072                         cy_pci_address,(int)cy_pci_irq,cy_pci_io);
3073 #endif
3074                 /* fill the next cy_card structure available */
3075                 for (j = 0 ; j < NR_CARDS ; j++) {
3076                         if (cy_card[j].base_addr == 0)  break;
3077                 }
3078                 if (j == NR_CARDS) {    /* no more cy_cards available */
3079                         printk("Cyclom-Y/PCI found at 0x%x but no more card structures are available.\n",
3080                                 (unsigned int) cy_pci_address);
3081                         return(i);
3082                 }
3083 
3084                 /* allocate IRQ */
3085                 if(request_irq(cy_pci_irq,cy_interrupt,SA_INTERRUPT,"cyclades"))
3086                 {
3087                         printk("Cyclom-Y/PCI found at 0x%x but could not allocate interrupt IRQ%d.\n",
3088                                 (unsigned int) cy_pci_address,cy_pci_irq);
3089                         return(i);
3090                 }
3091 
3092                 /* set cy_card */
3093                 cy_card[j].base_addr = (int) cy_pci_address;
3094                 cy_card[j].irq = (int) cy_pci_irq;
3095                 cy_card[j].bus_index = 1;
3096                 cy_card[j].first_line = cy_next_channel;
3097                 cy_card[j].num_chips = cy_pci_nchan/4;
3098                 IRQ_cards[cy_pci_irq] = &cy_card[j];
3099 
3100                 /* enable interrupts in the PCI interface */
3101                 outw(inw(cy_pci_io+0x68)|0x0900,cy_pci_io+0x68);
3102                 pci_intr_ctrl = (unsigned long)(inw(cy_pci_io+0x68) | inw(cy_pci_io+0x6a)<<16);
3103 
3104                 /* print message */
3105                 printk("Cyclom-Y/PCI #%d: 0x%x-0x%x, IRQ%d, %d channels starting from port %d.\n",
3106                         j+1,(unsigned int) cy_pci_address,
3107                         (unsigned int)(cy_pci_address + 0x3fff),
3108                         (int)cy_pci_irq,cy_pci_nchan,cy_next_channel);
3109 
3110                 cy_next_channel += cy_pci_nchan;
3111         }
3112         return(i);
3113 #endif /* ifndef CONFIG_PCI */
3114 }
3115 
3116 
3117 #ifdef CYCLOM_SHOW_STATUS
3118 static void
3119 show_status(int line_num)
     /* [previous][next][first][last][top][bottom][index][help] */
3120 {
3121   unsigned char *base_addr;
3122   int card,chip,channel,index;
3123   struct cyclades_port * info;
3124   unsigned long flags;
3125 
3126     info = &cy_port[line_num];
3127     card = info->card;
3128     index = cy_card[card].bus_index;
3129     channel = (info->line) - (cy_card[card].first_line);
3130     chip = channel>>2;
3131     channel &= 0x03;
3132     printk("  card %d, chip %d, channel %d\n", card, chip, channel);/**/
3133 
3134     printk(" cy_card\n");
3135     printk("  irq base_addr num_chips first_line = %d %lx %d %d\n",
3136            cy_card[card].irq, (long)cy_card[card].base_addr,
3137            cy_card[card].num_chips, cy_card[card].first_line);
3138 
3139     printk(" cy_port\n");
3140     printk("  card line flags = %d %d %x\n",
3141                  info->card, info->line, info->flags);
3142     printk("  *tty read_status_mask timeout xmit_fifo_size = %lx %x %x %x\n",
3143                  (long)info->tty, info->read_status_mask,
3144                  info->timeout, info->xmit_fifo_size);
3145     printk("  cor1,cor2,cor3,cor4,cor5 = %x %x %x %x %x\n",
3146              info->cor1, info->cor2, info->cor3, info->cor4, info->cor5);
3147     printk("  tbpr,tco,rbpr,rco = %d %d %d %d\n",
3148              info->tbpr, info->tco, info->rbpr, info->rco);
3149     printk("  close_delay event count = %d %d %d\n",
3150              info->close_delay, info->event, info->count);
3151     printk("  x_char blocked_open = %x %x\n",
3152              info->x_char, info->blocked_open);
3153     printk("  session pgrp open_wait = %lx %lx %lx\n",
3154              info->session, info->pgrp, (long)info->open_wait);
3155 
3156 
3157     save_flags(flags); cli();
3158 
3159         base_addr = (unsigned char*)
3160                        (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
3161 
3162 /* Global Registers */
3163 
3164         printk(" CyGFRCR %x\n", base_addr[CyGFRCR<<index]);
3165         printk(" CyCAR %x\n", base_addr[CyCAR<<index]);
3166         printk(" CyGCR %x\n", base_addr[CyGCR<<index]);
3167         printk(" CySVRR %x\n", base_addr[CySVRR<<index]);
3168         printk(" CyRICR %x\n", base_addr[CyRICR<<index]);
3169         printk(" CyTICR %x\n", base_addr[CyTICR<<index]);
3170         printk(" CyMICR %x\n", base_addr[CyMICR<<index]);
3171         printk(" CyRIR %x\n", base_addr[CyRIR<<index]);
3172         printk(" CyTIR %x\n", base_addr[CyTIR<<index]);
3173         printk(" CyMIR %x\n", base_addr[CyMIR<<index]);
3174         printk(" CyPPR %x\n", base_addr[CyPPR<<index]);
3175 
3176         base_addr[CyCAR<<index] = (u_char)channel;
3177 
3178 /* Virtual Registers */
3179 
3180         printk(" CyRIVR %x\n", base_addr[CyRIVR<<index]);
3181         printk(" CyTIVR %x\n", base_addr[CyTIVR<<index]);
3182         printk(" CyMIVR %x\n", base_addr[CyMIVR<<index]);
3183         printk(" CyMISR %x\n", base_addr[CyMISR<<index]);
3184 
3185 /* Channel Registers */
3186 
3187         printk(" CyCCR %x\n", base_addr[CyCCR<<index]);
3188         printk(" CySRER %x\n", base_addr[CySRER<<index]);
3189         printk(" CyCOR1 %x\n", base_addr[CyCOR1<<index]);
3190         printk(" CyCOR2 %x\n", base_addr[CyCOR2<<index]);
3191         printk(" CyCOR3 %x\n", base_addr[CyCOR3<<index]);
3192         printk(" CyCOR4 %x\n", base_addr[CyCOR4<<index]);
3193         printk(" CyCOR5 %x\n", base_addr[CyCOR5<<index]);
3194         printk(" CyCCSR %x\n", base_addr[CyCCSR<<index]);
3195         printk(" CyRDCR %x\n", base_addr[CyRDCR<<index]);
3196         printk(" CySCHR1 %x\n", base_addr[CySCHR1<<index]);
3197         printk(" CySCHR2 %x\n", base_addr[CySCHR2<<index]);
3198         printk(" CySCHR3 %x\n", base_addr[CySCHR3<<index]);
3199         printk(" CySCHR4 %x\n", base_addr[CySCHR4<<index]);
3200         printk(" CySCRL %x\n", base_addr[CySCRL<<index]);
3201         printk(" CySCRH %x\n", base_addr[CySCRH<<index]);
3202         printk(" CyLNC %x\n", base_addr[CyLNC<<index]);
3203         printk(" CyMCOR1 %x\n", base_addr[CyMCOR1<<index]);
3204         printk(" CyMCOR2 %x\n", base_addr[CyMCOR2<<index]);
3205         printk(" CyRTPR %x\n", base_addr[CyRTPR<<index]);
3206         printk(" CyMSVR1 %x\n", base_addr[CyMSVR1<<index]);
3207         printk(" CyMSVR2 %x\n", base_addr[CyMSVR2<<index]);
3208         printk(" CyRBPR %x\n", base_addr[CyRBPR<<index]);
3209         printk(" CyRCOR %x\n", base_addr[CyRCOR<<index]);
3210         printk(" CyTBPR %x\n", base_addr[CyTBPR<<index]);
3211         printk(" CyTCOR %x\n", base_addr[CyTCOR<<index]);
3212 
3213     restore_flags(flags);
3214 } /* show_status */
3215 #endif
3216 

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