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

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