PageRenderTime 90ms CodeModel.GetById 3ms app.highlight 72ms RepoModel.GetById 1ms app.codeStats 1ms

/drivers/char/sx.c

https://bitbucket.org/evzijst/gittest
C | 2621 lines | 1653 code | 458 blank | 510 comment | 320 complexity | e1bc835ba89ca697969d28141e400705 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1
   2/* sx.c -- driver for the Specialix SX series cards. 
   3 *
   4 *  This driver will also support the older SI, and XIO cards.
   5 *
   6 *
   7 *   (C) 1998 - 2004  R.E.Wolff@BitWizard.nl
   8 *
   9 *  Simon Allen (simonallen@cix.compulink.co.uk) wrote a previous
  10 *  version of this driver. Some fragments may have been copied. (none
  11 *  yet :-)
  12 *
  13 * Specialix pays for the development and support of this driver.
  14 * Please DO contact support@specialix.co.uk if you require
  15 * support. But please read the documentation (sx.txt) first.
  16 *
  17 *
  18 *
  19 *      This program is free software; you can redistribute it and/or
  20 *      modify it under the terms of the GNU General Public License as
  21 *      published by the Free Software Foundation; either version 2 of
  22 *      the License, or (at your option) any later version.
  23 *
  24 *      This program is distributed in the hope that it will be
  25 *      useful, but WITHOUT ANY WARRANTY; without even the implied
  26 *      warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  27 *      PURPOSE.  See the GNU General Public License for more details.
  28 *
  29 *      You should have received a copy of the GNU General Public
  30 *      License along with this program; if not, write to the Free
  31 *      Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  32 *      USA.
  33 *
  34 * Revision history:
  35 * $Log: sx.c,v $
  36 * Revision 1.33  2000/03/09 10:00:00  pvdl,wolff
  37 * - Fixed module and port counting
  38 * - Fixed signal handling
  39 * - Fixed an Ooops
  40 * 
  41 * Revision 1.32  2000/03/07 09:00:00  wolff,pvdl
  42 * - Fixed some sx_dprintk typos
  43 * - added detection for an invalid board/module configuration
  44 *
  45 * Revision 1.31  2000/03/06 12:00:00  wolff,pvdl
  46 * - Added support for EISA
  47 *
  48 * Revision 1.30  2000/01/21 17:43:06  wolff
  49 * - Added support for SX+
  50 *
  51 * Revision 1.26  1999/08/05 15:22:14  wolff
  52 * - Port to 2.3.x
  53 * - Reformatted to Linus' liking.
  54 *
  55 * Revision 1.25  1999/07/30 14:24:08  wolff
  56 * Had accidentally left "gs_debug" set to "-1" instead of "off" (=0).
  57 *
  58 * Revision 1.24  1999/07/28 09:41:52  wolff
  59 * - I noticed the remark about use-count straying in sx.txt. I checked
  60 *   sx_open, and found a few places where that could happen. I hope it's
  61 *   fixed now.
  62 *
  63 * Revision 1.23  1999/07/28 08:56:06  wolff
  64 * - Fixed crash when sx_firmware run twice.
  65 * - Added sx_slowpoll as a module parameter (I guess nobody really wanted
  66 *   to change it from the default... )
  67 * - Fixed a stupid editing problem I introduced in 1.22.
  68 * - Fixed dropping characters on a termios change.
  69 *
  70 * Revision 1.22  1999/07/26 21:01:43  wolff
  71 * Russell Brown noticed that I had overlooked 4 out of six modem control
  72 * signals in sx_getsignals. Ooops.
  73 *
  74 * Revision 1.21  1999/07/23 09:11:33  wolff
  75 * I forgot to free dynamically allocated memory when the driver is unloaded.
  76 *
  77 * Revision 1.20  1999/07/20 06:25:26  wolff
  78 * The "closing wait" wasn't honoured. Thanks to James Griffiths for
  79 * reporting this.
  80 *
  81 * Revision 1.19  1999/07/11 08:59:59  wolff
  82 * Fixed an oops in close, when an open was pending. Changed the memtest
  83 * a bit. Should also test the board in word-mode, however my card fails the
  84 * memtest then. I still have to figure out what is wrong...
  85 *
  86 * Revision 1.18  1999/06/10 09:38:42  wolff
  87 * Changed the format of the firmware revision from %04x to %x.%02x .
  88 *
  89 * Revision 1.17  1999/06/04 09:44:35  wolff
  90 * fixed problem: reference to pci stuff when config_pci was off...
  91 * Thanks to Jorge Novo for noticing this.
  92 *
  93 * Revision 1.16  1999/06/02 08:30:15  wolff
  94 * added/removed the workaround for the DCD bug in the Firmware.
  95 * A bit more debugging code to locate that...
  96 *
  97 * Revision 1.15  1999/06/01 11:35:30  wolff
  98 * when DCD is left low (floating?), on TA's the firmware first tells us
  99 * that DCD is high, but after a short while suddenly comes to the
 100 * conclusion that it is low. All this would be fine, if it weren't that
 101 * Unix requires us to send a "hangup" signal in that case. This usually
 102 * all happens BEFORE the program has had a chance to ioctl the device
 103 * into clocal mode..
 104 *
 105 * Revision 1.14  1999/05/25 11:18:59  wolff
 106 * Added PCI-fix.
 107 * Added checks for return code of sx_sendcommand.
 108 * Don't issue "reconfig" if port isn't open yet. (bit us on TA modules...)
 109 *
 110 * Revision 1.13  1999/04/29 15:18:01  wolff
 111 * Fixed an "oops" that showed on SuSE 6.0 systems.
 112 * Activate DTR again after stty 0.
 113 *
 114 * Revision 1.12  1999/04/29 07:49:52  wolff
 115 * Improved "stty 0" handling a bit. (used to change baud to 9600 assuming
 116 *     the connection would be dropped anyway. That is not always the case,
 117 *     and confuses people).
 118 * Told the card to always monitor the modem signals.
 119 * Added support for dynamic  gs_debug adjustments.
 120 * Now tells the rest of the system the number of ports.
 121 *
 122 * Revision 1.11  1999/04/24 11:11:30  wolff
 123 * Fixed two stupid typos in the memory test.
 124 *
 125 * Revision 1.10  1999/04/24 10:53:39  wolff
 126 * Added some of Christian's suggestions.
 127 * Fixed an HW_COOK_IN bug (ISIG was not in I_OTHER. We used to trust the
 128 * card to send the signal to the process.....)
 129 *
 130 * Revision 1.9  1999/04/23 07:26:38  wolff
 131 * Included Christian Lademann's 2.0 compile-warning fixes and interrupt
 132 *    assignment redesign.
 133 * Cleanup of some other stuff.
 134 *
 135 * Revision 1.8  1999/04/16 13:05:30  wolff
 136 * fixed a DCD change unnoticed bug.
 137 *
 138 * Revision 1.7  1999/04/14 22:19:51  wolff
 139 * Fixed typo that showed up in 2.0.x builds (get_user instead of Get_user!)
 140 *
 141 * Revision 1.6  1999/04/13 18:40:20  wolff
 142 * changed misc-minor to 161, as assigned by HPA.
 143 *
 144 * Revision 1.5  1999/04/13 15:12:25  wolff
 145 * Fixed use-count leak when "hangup" occurred.
 146 * Added workaround for a stupid-PCIBIOS bug.
 147 *
 148 *
 149 * Revision 1.4  1999/04/01 22:47:40  wolff
 150 * Fixed < 1M linux-2.0 problem.
 151 * (vremap isn't compatible with ioremap in that case)
 152 *
 153 * Revision 1.3  1999/03/31 13:45:45  wolff
 154 * Firmware loading is now done through a separate IOCTL.
 155 *
 156 * Revision 1.2  1999/03/28 12:22:29  wolff
 157 * rcs cleanup
 158 *
 159 * Revision 1.1  1999/03/28 12:10:34  wolff
 160 * Readying for release on 2.0.x (sorry David, 1.01 becomes 1.1 for RCS). 
 161 *
 162 * Revision 0.12  1999/03/28 09:20:10  wolff
 163 * Fixed problem in 0.11, continueing cleanup.
 164 *
 165 * Revision 0.11  1999/03/28 08:46:44  wolff
 166 * cleanup. Not good.
 167 *
 168 * Revision 0.10  1999/03/28 08:09:43  wolff
 169 * Fixed loosing characters on close.
 170 *
 171 * Revision 0.9  1999/03/21 22:52:01  wolff
 172 * Ported back to 2.2.... (minor things)
 173 *
 174 * Revision 0.8  1999/03/21 22:40:33  wolff
 175 * Port to 2.0
 176 *
 177 * Revision 0.7  1999/03/21 19:06:34  wolff
 178 * Fixed hangup processing.
 179 *
 180 * Revision 0.6  1999/02/05 08:45:14  wolff
 181 * fixed real_raw problems. Inclusion into kernel imminent.
 182 *
 183 * Revision 0.5  1998/12/21 23:51:06  wolff
 184 * Snatched a nasty bug: sx_transmit_chars was getting re-entered, and it
 185 * shouldn't have. THATs why I want to have transmit interrupts even when
 186 * the buffer is empty.
 187 *
 188 * Revision 0.4  1998/12/17 09:34:46  wolff
 189 * PPP works. ioctl works. Basically works!
 190 *
 191 * Revision 0.3  1998/12/15 13:05:18  wolff
 192 * It works! Wow! Gotta start implementing IOCTL and stuff....
 193 *
 194 * Revision 0.2  1998/12/01 08:33:53  wolff
 195 * moved over to 2.1.130
 196 *
 197 * Revision 0.1  1998/11/03 21:23:51  wolff
 198 * Initial revision. Detects SX card.
 199 *
 200 * */
 201
 202
 203#define RCS_ID "$Id: sx.c,v 1.33 2000/03/08 10:01:02 wolff, pvdl Exp $"
 204#define RCS_REV "$Revision: 1.33 $"
 205
 206
 207#include <linux/module.h>
 208#include <linux/config.h> 
 209#include <linux/kdev_t.h>
 210#include <linux/kernel.h>
 211#include <linux/sched.h>
 212#include <linux/ioport.h>
 213#include <linux/interrupt.h>
 214#include <linux/errno.h>
 215#include <linux/tty.h>
 216#include <linux/tty_flip.h>
 217#include <linux/mm.h>
 218#include <linux/serial.h>
 219#include <linux/fcntl.h>
 220#include <linux/major.h>
 221#include <linux/delay.h>
 222#include <linux/pci.h>
 223#include <linux/slab.h>
 224#include <linux/init.h>
 225#include <linux/miscdevice.h>
 226#include <linux/bitops.h>
 227
 228#include <asm/io.h>
 229#include <asm/uaccess.h>
 230
 231/* The 3.0.0 version of sxboards/sxwindow.h  uses BYTE and WORD.... */
 232#define BYTE u8
 233#define WORD u16
 234
 235/* .... but the 3.0.4 version uses _u8 and _u16. */
 236#define _u8 u8
 237#define _u16 u16
 238
 239#include "sxboards.h"
 240#include "sxwindow.h"
 241
 242#include <linux/generic_serial.h>
 243#include "sx.h"
 244
 245
 246/* I don't think that this driver can handle more than 256 ports on
 247   one machine. You'll have to increase the number of boards in sx.h
 248   if you want more than 4 boards.  */
 249
 250#ifndef PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8
 251#define PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8 0x2000
 252#endif
 253
 254#ifdef CONFIG_PCI
 255static struct pci_device_id sx_pci_tbl[] = {
 256	{ PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8, PCI_ANY_ID, PCI_ANY_ID },
 257	{ 0 }
 258};
 259MODULE_DEVICE_TABLE(pci, sx_pci_tbl);
 260#endif /* CONFIG_PCI */
 261
 262/* Configurable options: 
 263   (Don't be too sure that it'll work if you toggle them) */
 264
 265/* Am I paranoid or not ? ;-) */
 266#undef SX_PARANOIA_CHECK
 267
 268
 269/* 20 -> 2000 per second. The card should rate-limit interrupts at 100
 270   Hz, but it is user configurable. I don't recommend going above 1000
 271   Hz. The interrupt ratelimit might trigger if the interrupt is
 272   shared with a very active other device. */
 273#define IRQ_RATE_LIMIT 20
 274
 275/* Sharing interrupts is possible now. If the other device wants more
 276   than 2000 interrupts per second, we'd gracefully decline further
 277   interrupts. That's not what we want. On the other hand, if the
 278   other device interrupts 2000 times a second, don't use the SX
 279   interrupt. Use polling. */
 280#undef IRQ_RATE_LIMIT
 281
 282
 283#if 0
 284/* Not implemented */
 285/* 
 286 * The following defines are mostly for testing purposes. But if you need
 287 * some nice reporting in your syslog, you can define them also.
 288 */
 289#define SX_REPORT_FIFO
 290#define SX_REPORT_OVERRUN
 291#endif 
 292
 293
 294/* Function prototypes */
 295static void sx_disable_tx_interrupts (void * ptr); 
 296static void sx_enable_tx_interrupts (void * ptr); 
 297static void sx_disable_rx_interrupts (void * ptr); 
 298static void sx_enable_rx_interrupts (void * ptr); 
 299static int  sx_get_CD (void * ptr); 
 300static void sx_shutdown_port (void * ptr);
 301static int  sx_set_real_termios (void  *ptr);
 302static void sx_close (void  *ptr);
 303static int sx_chars_in_buffer (void * ptr);
 304static int sx_init_board (struct sx_board *board);
 305static int sx_init_portstructs (int nboards, int nports);
 306static int sx_fw_ioctl (struct inode *inode, struct file *filp,
 307                        unsigned int cmd, unsigned long arg);
 308static int sx_init_drivers(void);
 309
 310
 311static struct tty_driver *sx_driver;
 312
 313static struct sx_board boards[SX_NBOARDS];
 314static struct sx_port *sx_ports;
 315static int sx_initialized;
 316static int sx_nports;
 317static int sx_debug;
 318
 319
 320/* You can have the driver poll your card. 
 321    - Set sx_poll to 1 to poll every timer tick (10ms on Intel). 
 322      This is used when the card cannot use an interrupt for some reason.
 323
 324    - set sx_slowpoll to 100 to do an extra poll once a second (on Intel). If 
 325      the driver misses an interrupt (report this if it DOES happen to you!)
 326      everything will continue to work.... 
 327 */
 328static int sx_poll = 1;
 329static int sx_slowpoll;
 330
 331/* The card limits the number of interrupts per second. 
 332   At 115k2 "100" should be sufficient. 
 333   If you're using higher baudrates, you can increase this...
 334 */
 335
 336static int sx_maxints = 100;
 337
 338/* These are the only open spaces in my computer. Yours may have more
 339   or less.... -- REW 
 340   duh: Card at 0xa0000 is possible on HP Netserver?? -- pvdl
 341*/
 342static int sx_probe_addrs[]= {0xc0000, 0xd0000, 0xe0000, 
 343                              0xc8000, 0xd8000, 0xe8000};
 344static int si_probe_addrs[]= {0xc0000, 0xd0000, 0xe0000, 
 345                              0xc8000, 0xd8000, 0xe8000, 0xa0000};
 346static int si1_probe_addrs[]= { 0xd0000};
 347
 348#define NR_SX_ADDRS (sizeof(sx_probe_addrs)/sizeof (int))
 349#define NR_SI_ADDRS (sizeof(si_probe_addrs)/sizeof (int))
 350#define NR_SI1_ADDRS (sizeof(si1_probe_addrs)/sizeof (int))
 351
 352
 353/* Set the mask to all-ones. This alas, only supports 32 interrupts. 
 354   Some architectures may need more. */
 355static int sx_irqmask = -1;
 356
 357module_param_array(sx_probe_addrs, int, NULL, 0);
 358module_param_array(si_probe_addrs, int, NULL, 0);
 359module_param(sx_poll, int, 0);
 360module_param(sx_slowpoll, int, 0);
 361module_param(sx_maxints, int, 0);
 362module_param(sx_debug, int, 0);
 363module_param(sx_irqmask, int, 0);
 364
 365MODULE_LICENSE("GPL");
 366
 367static struct real_driver sx_real_driver = {
 368	sx_disable_tx_interrupts,
 369	sx_enable_tx_interrupts,
 370	sx_disable_rx_interrupts,
 371	sx_enable_rx_interrupts,
 372	sx_get_CD,
 373	sx_shutdown_port, 
 374	sx_set_real_termios, 
 375	sx_chars_in_buffer,
 376	sx_close,
 377};
 378
 379
 380/* 
 381   This driver can spew a whole lot of debugging output at you. If you
 382   need maximum performance, you should disable the DEBUG define. To
 383   aid in debugging in the field, I'm leaving the compile-time debug
 384   features enabled, and disable them "runtime". That allows me to
 385   instruct people with problems to enable debugging without requiring
 386   them to recompile... 
 387*/
 388#define DEBUG
 389
 390
 391#ifdef DEBUG
 392#define sx_dprintk(f, str...) if (sx_debug & f) printk (str)
 393#else
 394#define sx_dprintk(f, str...) /* nothing */
 395#endif
 396
 397
 398
 399#define func_enter() sx_dprintk (SX_DEBUG_FLOW, "sx: enter %s\n",__FUNCTION__)
 400#define func_exit()  sx_dprintk (SX_DEBUG_FLOW, "sx: exit  %s\n", __FUNCTION__)
 401
 402#define func_enter2() sx_dprintk (SX_DEBUG_FLOW, "sx: enter %s (port %d)\n", \
 403					__FUNCTION__, port->line)
 404
 405
 406
 407
 408/* 
 409 *  Firmware loader driver specific routines
 410 *
 411 */
 412
 413static struct file_operations sx_fw_fops = {
 414	.owner		= THIS_MODULE,
 415	.ioctl		= sx_fw_ioctl,
 416};
 417
 418static struct miscdevice sx_fw_device = {
 419	SXCTL_MISC_MINOR, "sxctl", &sx_fw_fops
 420};
 421
 422
 423
 424
 425
 426#ifdef SX_PARANOIA_CHECK
 427
 428/* This doesn't work. Who's paranoid around here? Not me! */
 429
 430static inline int sx_paranoia_check(struct sx_port const * port,
 431				    char *name, const char *routine)
 432{
 433
 434	static const char *badmagic =
 435	  KERN_ERR "sx: Warning: bad sx port magic number for device %s in %s\n";
 436	static const char *badinfo =
 437	  KERN_ERR "sx: Warning: null sx port for device %s in %s\n";
 438 
 439	if (!port) {
 440		printk(badinfo, name, routine);
 441		return 1;
 442	}
 443	if (port->magic != SX_MAGIC) {
 444		printk(badmagic, name, routine);
 445		return 1;
 446	}
 447
 448	return 0;
 449}
 450#else
 451#define sx_paranoia_check(a,b,c) 0
 452#endif
 453
 454/* The timeouts. First try 30 times as fast as possible. Then give
 455   the card some time to breathe between accesses. (Otherwise the
 456   processor on the card might not be able to access its OWN bus... */
 457
 458#define TIMEOUT_1 30
 459#define TIMEOUT_2 1000000
 460
 461
 462#ifdef DEBUG
 463static void my_hd_io(void __iomem *p, int len)
 464{
 465	int i, j, ch;
 466	unsigned char __iomem *addr = p;
 467
 468	for (i=0;i<len;i+=16) {
 469		printk ("%p ", addr+i);
 470		for (j=0;j<16;j++) {
 471			printk ("%02x %s", readb(addr+j+i), (j==7)?" ":"");
 472		}
 473		for (j=0;j<16;j++) {
 474			ch = readb(addr+j+i);
 475			printk ("%c", (ch < 0x20)?'.':((ch > 0x7f)?'.':ch));
 476		}
 477		printk ("\n");
 478	}
 479}
 480static void my_hd(void *p, int len)
 481{
 482	int i, j, ch;
 483	unsigned char *addr = p;
 484
 485	for (i=0;i<len;i+=16) {
 486		printk ("%p ", addr+i);
 487		for (j=0;j<16;j++) {
 488			printk ("%02x %s", addr[j+i], (j==7)?" ":"");
 489		}
 490		for (j=0;j<16;j++) {
 491			ch = addr[j+i];
 492			printk ("%c", (ch < 0x20)?'.':((ch > 0x7f)?'.':ch));
 493		}
 494		printk ("\n");
 495	}
 496}
 497#endif
 498
 499
 500
 501/* This needs redoing for Alpha -- REW -- Done. */
 502
 503static inline void write_sx_byte (struct sx_board *board, int offset, u8 byte)
 504{
 505	writeb (byte, board->base+offset);
 506}
 507
 508static inline u8 read_sx_byte (struct sx_board *board, int offset)
 509{
 510	return readb (board->base+offset);
 511}
 512
 513
 514static inline void write_sx_word (struct sx_board *board, int offset, u16 word)
 515{
 516	writew (word, board->base+offset);
 517}
 518
 519static inline u16 read_sx_word (struct sx_board *board, int offset)
 520{
 521	return readw (board->base + offset);
 522}
 523
 524
 525static int sx_busy_wait_eq (struct sx_board *board, 
 526                     	    int offset, int mask, int correctval)
 527{
 528	int i;
 529
 530	func_enter ();
 531
 532	for (i=0; i < TIMEOUT_1 ;i++)
 533		if ((read_sx_byte (board, offset) & mask) == correctval) {
 534			func_exit ();
 535			return 1;
 536		}
 537
 538	for (i=0; i < TIMEOUT_2 ;i++) {
 539		if ((read_sx_byte (board, offset) & mask) == correctval) {
 540			func_exit ();
 541			return 1;
 542		}
 543		udelay (1);
 544	}
 545
 546	func_exit ();
 547	return 0;
 548}
 549
 550
 551static int sx_busy_wait_neq (struct sx_board *board, 
 552                      	     int offset, int mask, int badval)
 553{
 554	int i;
 555
 556	func_enter ();
 557
 558	for (i=0; i < TIMEOUT_1 ;i++)
 559		if ((read_sx_byte (board, offset) & mask) != badval) {
 560			func_exit ();
 561			return 1;
 562		}
 563
 564	for (i=0; i < TIMEOUT_2 ;i++) {
 565		if ((read_sx_byte (board, offset) & mask) != badval) {
 566			func_exit ();
 567			return 1;
 568		}
 569		udelay (1);
 570	}
 571
 572	func_exit ();
 573	return 0;
 574}
 575
 576
 577
 578/* 5.6.4 of 6210028 r2.3 */
 579static int sx_reset (struct sx_board *board)
 580{
 581	func_enter ();
 582
 583	if (IS_SX_BOARD (board)) {
 584
 585		write_sx_byte (board, SX_CONFIG, 0);
 586		write_sx_byte (board, SX_RESET, 1); /* Value doesn't matter */
 587
 588		if (!sx_busy_wait_eq (board, SX_RESET_STATUS, 1, 0)) {
 589			printk (KERN_INFO "sx: Card doesn't respond to reset....\n");
 590			return 0;
 591		}
 592	} else if (IS_EISA_BOARD(board)) {
 593		outb(board->irq<<4, board->eisa_base+0xc02);
 594	} else if (IS_SI1_BOARD(board)) {
 595	        write_sx_byte (board, SI1_ISA_RESET,   0); // value does not matter
 596	} else {
 597		/* Gory details of the SI/ISA board */
 598		write_sx_byte (board, SI2_ISA_RESET,    SI2_ISA_RESET_SET);
 599		write_sx_byte (board, SI2_ISA_IRQ11,    SI2_ISA_IRQ11_CLEAR);
 600		write_sx_byte (board, SI2_ISA_IRQ12,    SI2_ISA_IRQ12_CLEAR);
 601		write_sx_byte (board, SI2_ISA_IRQ15,    SI2_ISA_IRQ15_CLEAR);
 602		write_sx_byte (board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_CLEAR);
 603		write_sx_byte (board, SI2_ISA_IRQSET,   SI2_ISA_IRQSET_CLEAR);
 604	}
 605
 606	func_exit ();
 607	return 1;
 608}
 609
 610
 611/* This doesn't work on machines where "NULL" isn't 0 */
 612/* If you have one of those, someone will need to write 
 613   the equivalent of this, which will amount to about 3 lines. I don't
 614   want to complicate this right now. -- REW
 615   (See, I do write comments every now and then :-) */
 616#define OFFSETOF(strct, elem) ((long)&(((struct strct *)NULL)->elem))
 617
 618
 619#define CHAN_OFFSET(port,elem) (port->ch_base + OFFSETOF (_SXCHANNEL, elem))
 620#define MODU_OFFSET(board,addr,elem)    (addr + OFFSETOF (_SXMODULE, elem))
 621#define  BRD_OFFSET(board,elem)                (OFFSETOF (_SXCARD, elem))
 622
 623
 624#define sx_write_channel_byte(port, elem, val) \
 625   write_sx_byte (port->board, CHAN_OFFSET (port, elem), val)
 626
 627#define sx_read_channel_byte(port, elem) \
 628   read_sx_byte (port->board, CHAN_OFFSET (port, elem))
 629
 630#define sx_write_channel_word(port, elem, val) \
 631   write_sx_word (port->board, CHAN_OFFSET (port, elem), val)
 632
 633#define sx_read_channel_word(port, elem) \
 634   read_sx_word (port->board, CHAN_OFFSET (port, elem))
 635
 636
 637#define sx_write_module_byte(board, addr, elem, val) \
 638   write_sx_byte (board, MODU_OFFSET (board, addr, elem), val)
 639
 640#define sx_read_module_byte(board, addr, elem) \
 641   read_sx_byte (board, MODU_OFFSET (board, addr, elem))
 642
 643#define sx_write_module_word(board, addr, elem, val) \
 644   write_sx_word (board, MODU_OFFSET (board, addr, elem), val)
 645
 646#define sx_read_module_word(board, addr, elem) \
 647   read_sx_word (board, MODU_OFFSET (board, addr, elem))
 648
 649
 650#define sx_write_board_byte(board, elem, val) \
 651   write_sx_byte (board, BRD_OFFSET (board, elem), val)
 652
 653#define sx_read_board_byte(board, elem) \
 654   read_sx_byte (board, BRD_OFFSET (board, elem))
 655
 656#define sx_write_board_word(board, elem, val) \
 657   write_sx_word (board, BRD_OFFSET (board, elem), val)
 658
 659#define sx_read_board_word(board, elem) \
 660   read_sx_word (board, BRD_OFFSET (board, elem))
 661
 662
 663static int sx_start_board (struct sx_board *board)
 664{
 665	if (IS_SX_BOARD (board)) {
 666		write_sx_byte (board, SX_CONFIG, SX_CONF_BUSEN);
 667	} else if (IS_EISA_BOARD(board)) {
 668		write_sx_byte(board, SI2_EISA_OFF, SI2_EISA_VAL);
 669		outb((board->irq<<4)|4, board->eisa_base+0xc02);
 670	} else if (IS_SI1_BOARD(board)) {
 671		write_sx_byte (board, SI1_ISA_RESET_CLEAR, 0);
 672		write_sx_byte (board, SI1_ISA_INTCL, 0);
 673	} else {
 674		/* Don't bug me about the clear_set. 
 675		   I haven't the foggiest idea what it's about -- REW */
 676		write_sx_byte (board, SI2_ISA_RESET,    SI2_ISA_RESET_CLEAR);
 677		write_sx_byte (board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
 678	}
 679	return 1;
 680}
 681
 682#define SX_IRQ_REG_VAL(board) \
 683        ((board->flags & SX_ISA_BOARD)?(board->irq << 4):0)
 684
 685/* Note. The SX register is write-only. Therefore, we have to enable the
 686   bus too. This is a no-op, if you don't mess with this driver... */
 687static int sx_start_interrupts (struct sx_board *board)
 688{
 689
 690	/* Don't call this with board->irq == 0 */
 691
 692	if (IS_SX_BOARD(board)) {
 693		write_sx_byte (board, SX_CONFIG, SX_IRQ_REG_VAL (board) | 
 694		                                 SX_CONF_BUSEN | 
 695		                                 SX_CONF_HOSTIRQ);
 696	} else if (IS_EISA_BOARD(board)) {
 697		inb(board->eisa_base+0xc03);  
 698	} else if (IS_SI1_BOARD(board)) {
 699	       write_sx_byte (board, SI1_ISA_INTCL,0);
 700	       write_sx_byte (board, SI1_ISA_INTCL_CLEAR,0);
 701	} else {
 702		switch (board->irq) {
 703		case 11:write_sx_byte (board, SI2_ISA_IRQ11, SI2_ISA_IRQ11_SET);break;
 704		case 12:write_sx_byte (board, SI2_ISA_IRQ12, SI2_ISA_IRQ12_SET);break;
 705		case 15:write_sx_byte (board, SI2_ISA_IRQ15, SI2_ISA_IRQ15_SET);break;
 706		default:printk (KERN_INFO "sx: SI/XIO card doesn't support interrupt %d.\n", 
 707		                board->irq);
 708		return 0;
 709		}
 710		write_sx_byte (board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
 711	}
 712
 713	return 1;
 714}
 715
 716
 717static int sx_send_command (struct sx_port *port, 
 718                     	    int command, int mask, int newstat)
 719{
 720	func_enter2 ();
 721	write_sx_byte (port->board, CHAN_OFFSET (port, hi_hstat), command);
 722	func_exit ();
 723	return sx_busy_wait_eq (port->board, CHAN_OFFSET (port, hi_hstat), mask, newstat);
 724}
 725
 726
 727static char *mod_type_s (int module_type)
 728{
 729	switch (module_type) {
 730	case TA4:       return "TA4";
 731	case TA8:       return "TA8";
 732	case TA4_ASIC:  return "TA4_ASIC";
 733	case TA8_ASIC:  return "TA8_ASIC";
 734	case MTA_CD1400:return "MTA_CD1400";
 735	case SXDC:      return "SXDC";
 736	default:return "Unknown/invalid";
 737	}
 738}
 739
 740
 741static char *pan_type_s (int pan_type)
 742{
 743	switch (pan_type) {
 744	case MOD_RS232DB25:     return "MOD_RS232DB25";
 745	case MOD_RS232RJ45:     return "MOD_RS232RJ45";
 746	case MOD_RS422DB25:     return "MOD_RS422DB25";
 747	case MOD_PARALLEL:      return "MOD_PARALLEL";
 748	case MOD_2_RS232DB25:   return "MOD_2_RS232DB25";
 749	case MOD_2_RS232RJ45:   return "MOD_2_RS232RJ45";
 750	case MOD_2_RS422DB25:   return "MOD_2_RS422DB25";
 751	case MOD_RS232DB25MALE: return "MOD_RS232DB25MALE";
 752	case MOD_2_PARALLEL:    return "MOD_2_PARALLEL";
 753	case MOD_BLANK:         return "empty";
 754	default:return "invalid";
 755	}
 756}
 757
 758
 759static int mod_compat_type (int module_type)
 760{
 761	return module_type >> 4;
 762}
 763
 764static void sx_reconfigure_port(struct sx_port *port)
 765{
 766	if (sx_read_channel_byte (port, hi_hstat) == HS_IDLE_OPEN) {
 767		if (sx_send_command (port, HS_CONFIG, -1, HS_IDLE_OPEN) != 1) {
 768			printk (KERN_WARNING "sx: Sent reconfigure command, but card didn't react.\n");
 769		}
 770	} else {
 771		sx_dprintk (SX_DEBUG_TERMIOS, 
 772		            "sx: Not sending reconfigure: port isn't open (%02x).\n", 
 773		            sx_read_channel_byte (port, hi_hstat));
 774	}	
 775}
 776
 777static void sx_setsignals (struct sx_port *port, int dtr, int rts)
 778{
 779	int t;
 780	func_enter2 ();
 781
 782	t = sx_read_channel_byte (port, hi_op);
 783	if (dtr >= 0) t = dtr? (t | OP_DTR): (t & ~OP_DTR);
 784	if (rts >= 0) t = rts? (t | OP_RTS): (t & ~OP_RTS);
 785	sx_write_channel_byte (port, hi_op, t);
 786	sx_dprintk (SX_DEBUG_MODEMSIGNALS, "setsignals: %d/%d\n", dtr, rts);
 787
 788	func_exit ();
 789}
 790
 791
 792
 793static int sx_getsignals (struct sx_port *port)
 794{
 795	int i_stat,o_stat;
 796
 797	o_stat = sx_read_channel_byte (port, hi_op);
 798	i_stat = sx_read_channel_byte (port, hi_ip);
 799
 800	sx_dprintk (SX_DEBUG_MODEMSIGNALS, "getsignals: %d/%d  (%d/%d) %02x/%02x\n",
 801	            (o_stat & OP_DTR) != 0, (o_stat & OP_RTS) != 0,
 802	            port->c_dcd, sx_get_CD (port),
 803	            sx_read_channel_byte (port, hi_ip),
 804	            sx_read_channel_byte (port, hi_state));
 805
 806	return (((o_stat & OP_DTR)?TIOCM_DTR:0) |
 807	        ((o_stat & OP_RTS)?TIOCM_RTS:0) |
 808	        ((i_stat & IP_CTS)?TIOCM_CTS:0) |
 809	        ((i_stat & IP_DCD)?TIOCM_CAR:0) |
 810	        ((i_stat & IP_DSR)?TIOCM_DSR:0) |
 811	        ((i_stat & IP_RI)?TIOCM_RNG:0)
 812	        );
 813}
 814
 815
 816static void sx_set_baud (struct sx_port *port)
 817{
 818	int t;
 819
 820	if (port->board->ta_type == MOD_SXDC) {
 821		switch (port->gs.baud) {
 822		  /* Save some typing work... */
 823#define e(x) case x:t= BAUD_ ## x ; break
 824			e(50);e(75);e(110);e(150);e(200);e(300);e(600);
 825                        e(1200);e(1800);e(2000);e(2400);e(4800);e(7200);
 826                        e(9600);e(14400);e(19200);e(28800);e(38400);
 827                        e(56000);e(57600);e(64000);e(76800);e(115200);
 828			e(128000);e(150000);e(230400);e(256000);e(460800);
 829                        e(921600);
 830		case 134    :t = BAUD_134_5;   break;
 831		case 0      :t = -1;
 832								 break;
 833		default:
 834			/* Can I return "invalid"? */
 835			t = BAUD_9600;
 836			printk (KERN_INFO "sx: unsupported baud rate: %d.\n", port->gs.baud);
 837			break;
 838		}
 839#undef e
 840		if (t > 0) {
 841			/* The baud rate is not set to 0, so we're enabeling DTR... -- REW */
 842			sx_setsignals (port, 1, -1); 
 843			/* XXX This is not TA & MTA compatible */
 844			sx_write_channel_byte (port, hi_csr, 0xff);
 845
 846			sx_write_channel_byte (port, hi_txbaud, t);
 847			sx_write_channel_byte (port, hi_rxbaud, t);
 848		} else {
 849			sx_setsignals (port, 0, -1);
 850		}
 851	} else {
 852		switch (port->gs.baud) {
 853#define e(x) case x:t= CSR_ ## x ; break
 854			e(75);e(150);e(300);e(600);e(1200);e(2400);e(4800);
 855                        e(1800);e(9600);
 856			e(19200);e(57600);e(38400);
 857			/* TA supports 110, but not 115200, MTA supports 115200, but not 110 */
 858		case 110: 
 859			if (port->board->ta_type == MOD_TA) {
 860				t = CSR_110;
 861				break;
 862			} else {
 863				t = CSR_9600;
 864				printk (KERN_INFO "sx: Unsupported baud rate: %d.\n", port->gs.baud);
 865				break;
 866			}
 867		case 115200: 
 868			if (port->board->ta_type == MOD_TA) {
 869				t = CSR_9600;
 870				printk (KERN_INFO "sx: Unsupported baud rate: %d.\n", port->gs.baud);
 871				break;
 872			} else {
 873				t = CSR_110;
 874				break;
 875			}
 876		case 0      :t = -1;
 877								 break;
 878		default:
 879			t = CSR_9600;
 880			printk (KERN_INFO "sx: Unsupported baud rate: %d.\n", port->gs.baud);
 881			break;
 882		}
 883#undef e
 884		if (t >= 0) {
 885			sx_setsignals (port, 1, -1);
 886			sx_write_channel_byte (port, hi_csr, t * 0x11);
 887		} else {
 888			sx_setsignals (port, 0, -1);
 889		}
 890	}
 891}
 892
 893
 894/* Simon Allen's version of this routine was 225 lines long. 85 is a lot
 895   better. -- REW */
 896
 897static int sx_set_real_termios (void *ptr)
 898{
 899	struct sx_port *port = ptr;
 900
 901	func_enter2();
 902
 903	if (!port->gs.tty)
 904		return 0;
 905
 906	/* What is this doing here? -- REW
 907	   Ha! figured it out. It is to allow you to get DTR active again
 908	   if you've dropped it with stty 0. Moved to set_baud, where it
 909	   belongs (next to the drop dtr if baud == 0) -- REW */
 910	/* sx_setsignals (port, 1, -1); */
 911
 912	sx_set_baud (port);
 913
 914#define CFLAG port->gs.tty->termios->c_cflag
 915	sx_write_channel_byte (port, hi_mr1,
 916	                       (C_PARENB (port->gs.tty)? MR1_WITH:MR1_NONE) |
 917	                       (C_PARODD (port->gs.tty)? MR1_ODD:MR1_EVEN) |
 918	                       (C_CRTSCTS(port->gs.tty)? MR1_RTS_RXFLOW:0) |
 919	                       (((CFLAG & CSIZE)==CS8) ? MR1_8_BITS:0) |
 920	                       (((CFLAG & CSIZE)==CS7) ? MR1_7_BITS:0) |
 921	                       (((CFLAG & CSIZE)==CS6) ? MR1_6_BITS:0) |
 922	                       (((CFLAG & CSIZE)==CS5) ? MR1_5_BITS:0) );
 923
 924	sx_write_channel_byte (port, hi_mr2,
 925	                       (C_CRTSCTS(port->gs.tty)?MR2_CTS_TXFLOW:0) |
 926	                       (C_CSTOPB (port->gs.tty)?MR2_2_STOP:MR2_1_STOP));
 927
 928	switch (CFLAG & CSIZE) {
 929	case CS8:sx_write_channel_byte (port, hi_mask, 0xff);break;
 930	case CS7:sx_write_channel_byte (port, hi_mask, 0x7f);break;
 931	case CS6:sx_write_channel_byte (port, hi_mask, 0x3f);break;
 932	case CS5:sx_write_channel_byte (port, hi_mask, 0x1f);break;
 933	default:
 934		printk (KERN_INFO "sx: Invalid wordsize: %d\n", CFLAG & CSIZE);
 935		break;
 936	}
 937
 938	sx_write_channel_byte (port, hi_prtcl, 
 939	                       (I_IXON   (port->gs.tty)?SP_TXEN:0) |
 940	                       (I_IXOFF  (port->gs.tty)?SP_RXEN:0) |
 941	                       (I_IXANY  (port->gs.tty)?SP_TANY:0) |
 942	                       SP_DCEN);
 943
 944	sx_write_channel_byte (port, hi_break, 
 945	                       (I_IGNBRK(port->gs.tty)?BR_IGN:0 |
 946	                        I_BRKINT(port->gs.tty)?BR_INT:0));
 947
 948	sx_write_channel_byte (port, hi_txon,  START_CHAR (port->gs.tty));
 949	sx_write_channel_byte (port, hi_rxon,  START_CHAR (port->gs.tty));
 950	sx_write_channel_byte (port, hi_txoff, STOP_CHAR  (port->gs.tty));
 951	sx_write_channel_byte (port, hi_rxoff, STOP_CHAR  (port->gs.tty));
 952
 953	sx_reconfigure_port(port);
 954
 955	/* Tell line discipline whether we will do input cooking */
 956	if(I_OTHER(port->gs.tty)) {
 957		clear_bit(TTY_HW_COOK_IN, &port->gs.tty->flags);
 958	} else {
 959		set_bit(TTY_HW_COOK_IN, &port->gs.tty->flags);
 960	}
 961	sx_dprintk (SX_DEBUG_TERMIOS, "iflags: %x(%d) ", 
 962	            port->gs.tty->termios->c_iflag, 
 963	            I_OTHER(port->gs.tty));
 964
 965
 966/* Tell line discipline whether we will do output cooking.
 967 * If OPOST is set and no other output flags are set then we can do output
 968 * processing.  Even if only *one* other flag in the O_OTHER group is set
 969 * we do cooking in software.
 970 */
 971	if(O_OPOST(port->gs.tty) && !O_OTHER(port->gs.tty)) {
 972		set_bit(TTY_HW_COOK_OUT, &port->gs.tty->flags);
 973	} else {
 974		clear_bit(TTY_HW_COOK_OUT, &port->gs.tty->flags);
 975	}
 976	sx_dprintk (SX_DEBUG_TERMIOS, "oflags: %x(%d)\n", 
 977	            port->gs.tty->termios->c_oflag, 
 978	            O_OTHER(port->gs.tty));
 979	/* port->c_dcd = sx_get_CD (port); */
 980	func_exit ();
 981	return 0;
 982}
 983
 984
 985
 986/* ********************************************************************** *
 987 *                   the interrupt related routines                       *
 988 * ********************************************************************** */
 989
 990/* Note:
 991   Other drivers use the macro "MIN" to calculate how much to copy.
 992   This has the disadvantage that it will evaluate parts twice. That's
 993   expensive when it's IO (and the compiler cannot optimize those away!).
 994   Moreover, I'm not sure that you're race-free. 
 995
 996   I assign a value, and then only allow the value to decrease. This
 997   is always safe. This makes the code a few lines longer, and you
 998   know I'm dead against that, but I think it is required in this
 999   case.  */
1000
1001
1002static void sx_transmit_chars (struct sx_port *port)
1003{
1004	int c;
1005	int tx_ip;
1006	int txroom;
1007
1008	func_enter2 ();
1009	sx_dprintk (SX_DEBUG_TRANSMIT, "Port %p: transmit %d chars\n", 
1010	            port, port->gs.xmit_cnt);
1011
1012	if (test_and_set_bit (SX_PORT_TRANSMIT_LOCK, &port->locks)) {
1013		return;
1014	}
1015
1016	while (1) {
1017		c = port->gs.xmit_cnt;
1018
1019		sx_dprintk (SX_DEBUG_TRANSMIT, "Copying %d ", c);
1020		tx_ip  = sx_read_channel_byte (port, hi_txipos);
1021
1022		/* Took me 5 minutes to deduce this formula. 
1023		   Luckily it is literally in the manual in section 6.5.4.3.5 */
1024		txroom = (sx_read_channel_byte (port, hi_txopos) - tx_ip - 1) & 0xff;
1025
1026		/* Don't copy more bytes than there is room for in the buffer */
1027		if (c > txroom)
1028			c = txroom;
1029		sx_dprintk (SX_DEBUG_TRANSMIT, " %d(%d) ", c, txroom );
1030
1031		/* Don't copy past the end of the hardware transmit buffer */
1032		if (c > 0x100 - tx_ip) 
1033			c = 0x100 - tx_ip;
1034
1035		sx_dprintk (SX_DEBUG_TRANSMIT, " %d(%d) ", c, 0x100-tx_ip );
1036
1037		/* Don't copy pas the end of the source buffer */
1038		if (c > SERIAL_XMIT_SIZE - port->gs.xmit_tail) 
1039			c = SERIAL_XMIT_SIZE - port->gs.xmit_tail;
1040
1041		sx_dprintk (SX_DEBUG_TRANSMIT, " %d(%ld) \n", 
1042		            c, SERIAL_XMIT_SIZE- port->gs.xmit_tail);
1043
1044		/* If for one reason or another, we can't copy more data, we're done! */
1045		if (c == 0) break;
1046
1047
1048		memcpy_toio (port->board->base + CHAN_OFFSET(port,hi_txbuf) + tx_ip, 
1049		             port->gs.xmit_buf + port->gs.xmit_tail, c);
1050
1051		/* Update the pointer in the card */
1052		sx_write_channel_byte (port, hi_txipos, (tx_ip+c) & 0xff);
1053
1054		/* Update the kernel buffer end */
1055		port->gs.xmit_tail = (port->gs.xmit_tail + c) & (SERIAL_XMIT_SIZE-1);
1056
1057		/* This one last. (this is essential)
1058		   It would allow others to start putting more data into the buffer! */
1059		port->gs.xmit_cnt -= c;
1060	}
1061
1062	if (port->gs.xmit_cnt == 0) {
1063		sx_disable_tx_interrupts (port);
1064	}
1065
1066	if ((port->gs.xmit_cnt <= port->gs.wakeup_chars) && port->gs.tty) {
1067		tty_wakeup(port->gs.tty);
1068		sx_dprintk (SX_DEBUG_TRANSMIT, "Waking up.... ldisc (%d)....\n",
1069		            port->gs.wakeup_chars); 
1070	}
1071
1072	clear_bit (SX_PORT_TRANSMIT_LOCK, &port->locks);
1073	func_exit ();
1074}
1075
1076
1077/* Note the symmetry between receiving chars and transmitting them!
1078   Note: The kernel should have implemented both a receive buffer and
1079   a transmit buffer. */
1080
1081/* Inlined: Called only once. Remove the inline when you add another call */
1082static inline void sx_receive_chars (struct sx_port *port)
1083{
1084	int c;
1085	int rx_op;
1086	struct tty_struct *tty;
1087	int copied=0;
1088
1089	func_enter2 ();
1090	tty = port->gs.tty;
1091	while (1) {
1092		rx_op = sx_read_channel_byte (port, hi_rxopos);
1093		c = (sx_read_channel_byte (port, hi_rxipos) - rx_op) & 0xff;
1094
1095		sx_dprintk (SX_DEBUG_RECEIVE, "rxop=%d, c = %d.\n", rx_op, c); 
1096
1097		/* Don't copy more bytes than there is room for in the buffer */
1098		if (tty->flip.count + c > TTY_FLIPBUF_SIZE) 
1099			c = TTY_FLIPBUF_SIZE - tty->flip.count;
1100
1101		sx_dprintk (SX_DEBUG_RECEIVE, "c = %d.\n", c); 
1102
1103		/* Don't copy past the end of the hardware receive buffer */
1104		if (rx_op + c > 0x100) c = 0x100 - rx_op;
1105
1106		sx_dprintk (SX_DEBUG_RECEIVE, "c = %d.\n", c);
1107
1108		/* If for one reason or another, we can't copy more data, we're done! */
1109		if (c == 0) break;
1110
1111		sx_dprintk (SX_DEBUG_RECEIVE , "Copying over %d chars. First is %d at %lx\n", c, 
1112		            read_sx_byte (port->board, CHAN_OFFSET(port,hi_rxbuf) + rx_op),
1113		            CHAN_OFFSET(port, hi_rxbuf)); 
1114		memcpy_fromio (tty->flip.char_buf_ptr, 
1115		               port->board->base + CHAN_OFFSET(port,hi_rxbuf) + rx_op, c);
1116		memset(tty->flip.flag_buf_ptr, TTY_NORMAL, c);
1117
1118		/* Update the kernel buffer end */
1119		tty->flip.count += c;
1120		tty->flip.char_buf_ptr += c;
1121		tty->flip.flag_buf_ptr += c;
1122
1123		/* This one last. ( Not essential.)
1124		   It allows the card to start putting more data into the buffer! 
1125		   Update the pointer in the card */
1126		sx_write_channel_byte (port, hi_rxopos, (rx_op + c) & 0xff);
1127
1128		copied += c;
1129	}
1130	if (copied) {
1131		struct timeval tv;
1132
1133		do_gettimeofday (&tv);
1134		sx_dprintk (SX_DEBUG_RECEIVE, 
1135		            "pushing flipq port %d (%3d chars): %d.%06d  (%d/%d)\n", 
1136		            port->line, copied, 
1137		            (int) (tv.tv_sec % 60), (int)tv.tv_usec, tty->raw, tty->real_raw);
1138
1139		/* Tell the rest of the system the news. Great news. New characters! */
1140		tty_flip_buffer_push (tty);
1141		/*    tty_schedule_flip (tty); */
1142	}
1143
1144	func_exit ();
1145}
1146
1147/* Inlined: it is called only once. Remove the inline if you add another 
1148   call */
1149static inline void sx_check_modem_signals (struct sx_port *port)
1150{
1151	int hi_state;
1152	int c_dcd;
1153
1154	hi_state = sx_read_channel_byte (port, hi_state);
1155	sx_dprintk (SX_DEBUG_MODEMSIGNALS, "Checking modem signals (%d/%d)\n",
1156	            port->c_dcd, sx_get_CD (port));
1157
1158	if (hi_state & ST_BREAK) {
1159		hi_state &= ~ST_BREAK;
1160		sx_dprintk (SX_DEBUG_MODEMSIGNALS, "got a break.\n");
1161		sx_write_channel_byte (port, hi_state, hi_state);
1162		gs_got_break (&port->gs);
1163	}
1164	if (hi_state & ST_DCD) {
1165		hi_state &= ~ST_DCD;
1166		sx_dprintk (SX_DEBUG_MODEMSIGNALS, "got a DCD change.\n");
1167		sx_write_channel_byte (port, hi_state, hi_state);
1168		c_dcd = sx_get_CD (port);
1169		sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD is now %d\n", c_dcd);
1170		if (c_dcd != port->c_dcd) {
1171			port->c_dcd = c_dcd;
1172			if (sx_get_CD (port)) {
1173				/* DCD went UP */
1174				if ((sx_read_channel_byte(port, hi_hstat) != HS_IDLE_CLOSED) &&
1175						!(port->gs.tty->termios->c_cflag & CLOCAL) ) {
1176					/* Are we blocking in open?*/
1177					sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD active, unblocking open\n");
1178					wake_up_interruptible(&port->gs.open_wait);
1179				} else {
1180					sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD raised. Ignoring.\n");
1181				}
1182			} else {
1183				/* DCD went down! */
1184				if (!(port->gs.tty->termios->c_cflag & CLOCAL) ) {
1185					sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD dropped. hanging up....\n");
1186					tty_hangup (port->gs.tty);
1187				} else {
1188					sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD dropped. ignoring.\n");
1189				}
1190			}
1191		} else {
1192			sx_dprintk (SX_DEBUG_MODEMSIGNALS, "Hmmm. card told us DCD changed, but it didn't.\n");
1193		}
1194	}
1195}
1196
1197
1198/* This is what an interrupt routine should look like. 
1199 * Small, elegant, clear.
1200 */
1201
1202static irqreturn_t sx_interrupt (int irq, void *ptr, struct pt_regs *regs)
1203{
1204	struct sx_board *board = ptr;
1205	struct sx_port *port;
1206	int i;
1207
1208	func_enter ();
1209	sx_dprintk (SX_DEBUG_FLOW, "sx: enter sx_interrupt (%d/%d)\n", irq, board->irq); 
1210
1211	/* AAargh! The order in which to do these things is essential and
1212	   not trivial. 
1213
1214	   - Rate limit goes before "recursive". Otherwise a series of
1215	     recursive calls will hang the machine in the interrupt routine. 
1216
1217	   - hardware twiddling goes before "recursive". Otherwise when we
1218	     poll the card, and a recursive interrupt happens, we won't
1219	     ack the card, so it might keep on interrupting us. (especially
1220	     level sensitive interrupt systems like PCI).
1221
1222	   - Rate limit goes before hardware twiddling. Otherwise we won't
1223	     catch a card that has gone bonkers.
1224
1225	   - The "initialized" test goes after the hardware twiddling. Otherwise
1226	     the card will stick us in the interrupt routine again.
1227
1228	   - The initialized test goes before recursive. 
1229	*/
1230
1231
1232
1233#ifdef IRQ_RATE_LIMIT
1234	/* Aaargh! I'm ashamed. This costs more lines-of-code than the
1235	   actual interrupt routine!. (Well, used to when I wrote that comment) */
1236	{
1237		static int lastjif;
1238		static int nintr=0;
1239
1240		if (lastjif == jiffies) {
1241			if (++nintr > IRQ_RATE_LIMIT) {
1242				free_irq (board->irq, board);
1243				printk (KERN_ERR "sx: Too many interrupts. Turning off interrupt %d.\n", 
1244					      board->irq);
1245			}
1246		} else {
1247			lastjif = jiffies;
1248			nintr = 0;
1249		}
1250	}
1251#endif
1252
1253
1254	if (board->irq == irq) {
1255		/* Tell the card we've noticed the interrupt. */
1256
1257		sx_write_board_word (board, cc_int_pending, 0);
1258		if (IS_SX_BOARD (board)) {
1259			write_sx_byte (board, SX_RESET_IRQ, 1);
1260		} else if (IS_EISA_BOARD(board)) {
1261			inb(board->eisa_base+0xc03);
1262			write_sx_word(board, 8, 0); 
1263		} else {
1264			write_sx_byte (board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_CLEAR);
1265			write_sx_byte (board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
1266		}
1267	}
1268
1269	if (!sx_initialized)
1270		return IRQ_HANDLED;
1271	if (!(board->flags & SX_BOARD_INITIALIZED))
1272		return IRQ_HANDLED;
1273
1274	if (test_and_set_bit (SX_BOARD_INTR_LOCK, &board->locks)) {
1275		printk (KERN_ERR "Recursive interrupt! (%d)\n", board->irq);
1276		return IRQ_HANDLED;
1277	}
1278
1279	 for (i=0;i<board->nports;i++) {
1280		port = &board->ports[i];
1281		if (port->gs.flags & GS_ACTIVE) {
1282			if (sx_read_channel_byte (port, hi_state)) {
1283				sx_dprintk (SX_DEBUG_INTERRUPTS, 
1284				            "Port %d: modem signal change?... \n", i);
1285				sx_check_modem_signals (port); 
1286			}
1287			if (port->gs.xmit_cnt) {
1288				sx_transmit_chars (port);
1289			}
1290			if (!(port->gs.flags & SX_RX_THROTTLE)) {
1291				sx_receive_chars (port);
1292			}
1293		}
1294	}
1295
1296	clear_bit (SX_BOARD_INTR_LOCK, &board->locks);
1297
1298	sx_dprintk (SX_DEBUG_FLOW, "sx: exit sx_interrupt (%d/%d)\n", irq, board->irq); 
1299        func_exit ();
1300	return IRQ_HANDLED;
1301}
1302
1303
1304static void sx_pollfunc (unsigned long data)
1305{
1306	struct sx_board *board = (struct sx_board *) data;
1307
1308	func_enter ();
1309
1310	sx_interrupt (0, board, NULL);
1311
1312	init_timer(&board->timer);
1313
1314	board->timer.expires = jiffies + sx_poll;
1315	add_timer (&board->timer);
1316	func_exit ();
1317}
1318
1319
1320
1321/* ********************************************************************** *
1322 *                Here are the routines that actually                     *
1323 *              interface with the generic_serial driver                  *
1324 * ********************************************************************** */
1325
1326/* Ehhm. I don't know how to fiddle with interrupts on the SX card. --REW */
1327/* Hmm. Ok I figured it out. You don't.  */
1328
1329static void sx_disable_tx_interrupts (void * ptr) 
1330{
1331	struct sx_port *port = ptr; 
1332	func_enter2();
1333
1334	port->gs.flags &= ~GS_TX_INTEN;
1335
1336	func_exit();
1337}
1338
1339
1340static void sx_enable_tx_interrupts (void * ptr) 
1341{
1342	struct sx_port *port = ptr; 
1343	int data_in_buffer;
1344	func_enter2();
1345
1346	/* First transmit the characters that we're supposed to */
1347	sx_transmit_chars (port);
1348
1349	/* The sx card will never interrupt us if we don't fill the buffer
1350	   past 25%. So we keep considering interrupts off if that's the case. */
1351	data_in_buffer = (sx_read_channel_byte (port, hi_txipos) - 
1352	                  sx_read_channel_byte (port, hi_txopos)) & 0xff;
1353
1354	/* XXX Must be "HIGH_WATER" for SI card according to doc. */
1355	if (data_in_buffer < LOW_WATER) 
1356		port->gs.flags &= ~GS_TX_INTEN;
1357
1358	func_exit();
1359}
1360
1361
1362static void sx_disable_rx_interrupts (void * ptr) 
1363{
1364	/*  struct sx_port *port = ptr; */
1365	func_enter();
1366
1367	func_exit();
1368}
1369
1370static void sx_enable_rx_interrupts (void * ptr) 
1371{
1372	/*  struct sx_port *port = ptr; */
1373	func_enter();
1374
1375	func_exit();
1376}
1377
1378
1379/* Jeez. Isn't this simple? */
1380static int sx_get_CD (void * ptr) 
1381{
1382	struct sx_port *port = ptr;
1383	func_enter2();
1384
1385	func_exit();
1386	return ((sx_read_channel_byte (port, hi_ip) & IP_DCD) != 0);
1387}
1388
1389
1390/* Jeez. Isn't this simple? */
1391static int sx_chars_in_buffer (void * ptr) 
1392{
1393	struct sx_port *port = ptr;
1394	func_enter2();
1395
1396	func_exit();
1397	return ((sx_read_channel_byte (port, hi_txipos) - 
1398	         sx_read_channel_byte (port, hi_txopos)) & 0xff);
1399}
1400
1401
1402static void sx_shutdown_port (void * ptr) 
1403{
1404	struct sx_port *port = ptr; 
1405
1406	func_enter();
1407
1408	port->gs.flags &= ~ GS_ACTIVE;
1409	if (port->gs.tty && (port->gs.tty->termios->c_cflag & HUPCL)) {
1410		sx_setsignals (port, 0, 0);
1411		sx_reconfigure_port(port);
1412	}
1413
1414	func_exit();
1415}
1416
1417
1418
1419
1420
1421/* ********************************************************************** *
1422 *                Here are the routines that actually                     *
1423 *               interface with the rest of the system                    *
1424 * ********************************************************************** */
1425
1426static int sx_open  (struct tty_struct * tty, struct file * filp)
1427{
1428	struct sx_port *port;
1429	int retval, line;
1430	unsigned long flags;
1431
1432	func_enter();
1433
1434	if (!sx_initialized) {
1435		return -EIO;
1436	}
1437
1438	line = tty->index;
1439	sx_dprintk (SX_DEBUG_OPEN, "%d: opening line %d. tty=%p ctty=%p, np=%d)\n", 
1440	            current->pid, line, tty, current->signal->tty, sx_nports);
1441
1442	if ((line < 0) || (line >= SX_NPORTS) || (line >= sx_nports))
1443		return -ENODEV;
1444
1445	port = & sx_ports[line];
1446	port->c_dcd = 0; /* Make sure that the first interrupt doesn't detect a
1447	                    1 -> 0 transition. */
1448
1449
1450	sx_dprintk (SX_DEBUG_OPEN, "port = %p c_dcd = %d\n", port, port->c_dcd);
1451
1452	spin_lock_irqsave(&port->gs.driver_lock, flags);
1453
1454	tty->driver_data = port;
1455	port->gs.tty = tty;
1456	port->gs.count++;
1457	spin_unlock_irqrestore(&port->gs.driver_lock, flags);
1458
1459	sx_dprintk (SX_DEBUG_OPEN, "starting port\n");
1460
1461	/*
1462	 * Start up serial port
1463	 */
1464	retval = gs_init_port(&port->gs);
1465	sx_dprintk (SX_DEBUG_OPEN, "done gs_init\n");
1466	if (retval) {
1467		port->gs.count--;
1468		return retval;
1469	}
1470
1471	port->gs.flags |= GS_ACTIVE;
1472	if (port->gs.count <= 1)
1473		sx_setsignals (port, 1,1);
1474
1475#if 0
1476	if (sx_debug & SX_DEBUG_OPEN)
1477		my_hd (port, sizeof (*port));
1478#else
1479	if (sx_debug & SX_DEBUG_OPEN)
1480		my_hd_io (port->board->base + port->ch_base, sizeof (*port));
1481#endif
1482
1483	if (port->gs.count <= 1) {
1484		if (sx_send_command (port, HS_LOPEN, -1, HS_IDLE_OPEN) != 1) {
1485			printk (KERN_ERR "sx: Card didn't respond to LOPEN command.\n");
1486			spin_lock_irqsave(&port->gs.driver_lock, flags);
1487			port->gs.count--;
1488			spin_unlock_irqrestore(&port->gs.driver_lock, flags);
1489			return -EIO;
1490		}
1491	}
1492
1493	retval = gs_block_til_ready(port, filp);
1494	sx_dprintk (SX_DEBUG_OPEN, "Block til ready returned %d. Count=%d\n", 
1495	            retval, port->gs.count);
1496
1497	if (retval) {
1498		/* 
1499		 * Don't lower gs.count here because sx_close() will be called later
1500		 */ 
1501
1502		return retval;
1503	}
1504	/* tty->low_latency = 1; */
1505
1506	port->c_dcd = sx_get_CD (port);
1507	sx_dprintk (SX_DEBUG_OPEN, "at open: cd=%d\n", port->c_dcd);
1508
1509	func_exit();
1510	return 0;
1511
1512}
1513
1514
1515static void sx_close (void *ptr)
1516{
1517	struct sx_port *port = ptr; 
1518	/* Give the port 5 seconds to close down. */
1519	int to = 5 * HZ; 
1520
1521	func_enter ();
1522
1523	sx_setsignals (port, 0, 0);
1524	sx_reconfigure_port(port);	
1525	sx_send_command (port, HS_CLOSE, 0, 0);
1526
1527	while (to-- && (sx_read_channel_byte (port, hi_hstat) != HS_IDLE_CLOSED))
1528		if (msleep_interruptible(10))
1529			break;
1530	if (sx_read_channel_byte (port, hi_hstat) != HS_IDLE_CLOSED) {
1531		if (sx_send_command (port, HS_FORCE_CLOSED, -1, HS_IDLE_CLOSED) != 1) {
1532			printk (KERN_ERR 
1533			        "sx: sent the force_close command, but card didn't react\n");
1534		} else
1535			sx_dprintk (SX_DEBUG_CLOSE, "sent the force_close command.\n");
1536	}
1537
1538	sx_dprintk (SX_DEBUG_CLOSE, "waited %d jiffies for close. count=%d\n", 
1539	            5 * HZ - to - 1, port->gs.count);
1540
1541	if(port->gs.count) {
1542		sx_dprintk(SX_DEBUG_CLOSE, "WARNING port count:%d\n", port->gs.count);
1543		//printk ("%s SETTING port count to zero: %p count: %d\n", __FUNCTION__, port, port->gs.count);
1544		//port->gs.count = 0;
1545	}
1546
1547	func_exit ();
1548}
1549
1550
1551
1552/* This is relatively thorough. But then again it is only 20 lines. */
1553#define MARCHUP    for (i=min;i<max;i++) 
1554#define MARCHDOWN  for (i=max-1;i>=min;i--)
1555#define W0         write_sx_byte (board, i, 0x55)
1556#define W1         write_sx_byte (board, i, 0xaa)
1557#define R0         if (read_sx_byte (board, i) != 0x55) return 1
1558#define R1         if (read_sx_byte (board, i) != 0xaa) return 1
1559
1560/* This memtest takes a human-noticable time. You normally only do it
1561   once a boot, so I guess that it is worth it. */
1562static int do_memtest (struct sx_board *board, int min, int max)
1563{
1564	int i;
1565
1566	/* This is a marchb. Theoretically, marchb catches much more than
1567	   simpler tests. In practise, the longer test just catches more
1568	   intermittent errors. -- REW
1569	   (For the theory behind memory testing see: 
1570	   Testing Semiconductor Memories by A.J. van de Goor.) */
1571	MARCHUP	 {W0;}
1572	MARCHUP   {R0;W1;R1;W0;R0;W1;}
1573	MARCHUP   {R1;W0;W1;}
1574	MARCHDOWN {R1;W0;W1;W0;}
1575	MARCHDOWN {R0;W1;W0;}
1576
1577	return 0;
1578}
1579
1580
1581#undef MARCHUP
1582#undef MARCHDOWN
1583#undef W0
1584#undef W1
1585#undef R0
1586#undef R1
1587
1588#define MARCHUP    for (i=min;i<max;i+=2) 
1589#define MARCHDOWN  for (i=max-1;i>=min;i-=2)
1590#define W0         write_sx_word (board, i, 0x55aa)
1591#define W1         write_sx_word (board, i, 0xaa55)
1592#define R0         if (read_sx_word (board, i) != 0x55aa) return 1
1593#define R1         if (read_sx_word (board, i) != 0xaa55) return 1
1594
1595#if 0
1596/* This memtest takes a human-noticable time. You normally only do it
1597   once a boot, so I guess that it is worth it. */
1598static int do_memtest_w (struct sx_board *board, int min, int max)
1599{
1600	int i;
1601
1602	MARCHUP   {W0;}
1603	MARCHUP   {R0;W1;R1;W0;R0;W1;}
1604	MARCHUP   {R1;W0;W1;}
1605	MARCHDOWN {R1;W0;W1;W0;}
1606	MARCHDOWN {R0;W1;W0;}
1607
1608	return 0;
1609}
1610#endif
1611
1612
1613static int sx_fw_ioctl (struct inode *inode, struct file *filp,
1614                        unsigned int cmd, unsigned long arg)
1615{
1616	int rc = 0;
1617	int __user *descr = (int __user *)arg;
1618	int i;
1619	static struct sx_board *board = NULL;
1620	int nbytes, offset;
1621	unsigned long data;
1622	char *tmp;
1623
1624	func_enter();
1625
1626#if 0 
1627	/* Removed superuser check: Sysops can use the permissions on the device
1628	   file to restrict access. Recommendation: Root only. (root.root 600) */
1629	if (!capable(CAP_SYS_ADMIN)) {
1630		return -EPERM;
1631	}
1632#endif
1633
1634	sx_dprintk (SX_DEBUG_FIRMWARE, "IOCTL %x: %lx\n", cmd, arg);
1635
1636	if (!board) board = &boards[0];
1637	if (board->flags & SX_BOARD_PRESENT) {
1638		sx_dprintk (SX_DEBUG_FIRMWARE, "Board present! (%x)\n", 
1639		            board->flags);
1640	} else {
1641		sx_dprintk (SX_DEBUG_FIRMWARE, "Board not present! (%x) all:", 
1642		            board->flags);
1643		for (i=0;i< SX_NBOARDS;i++)
1644			sx_dprintk (SX_DEBUG_FIRMWARE, "<%x> ", boards[i].flags);
1645		sx_dprintk (SX_DEBUG_FIRMWARE, "\n");
1646		return -EIO;
1647	}
1648
1649	switch (cmd) {
1650	case SXIO_SET_BOARD:
1651		sx_dprintk (SX_DEBUG_FIRMWARE, "set board to %ld\n", arg);
1652		if (arg >= SX_NBOARDS) return -EIO;
1653		sx_dprintk (SX_DEBUG_FIRMWARE, "not out of range\n");
1654		if (!(boards[arg].flags	& SX_BOARD_PRESENT)) return -EIO;
1655		sx_dprintk (SX_DEBUG_FIRMWARE, ".. and present!\n");
1656		board = &boards[arg];
1657		break;
1658	case SXIO_GET_TYPE:
1659		rc = -ENOENT; /* If we manage to miss one, return error. */
1660		if (IS_SX_BOARD (board)) rc = SX_TYPE_SX;
1661		if (IS_CF_BOARD (board)) rc = SX_TYPE_CF;
1662		if (IS_SI_BOARD (board)) rc = SX_TYPE_SI;
1663		if (IS_SI1_BOARD (board)) rc = SX_TYPE_SI;
1664		if (IS_EISA_BOARD (board)) rc = SX_TYPE_SI;
1665		sx_dprintk (SX_DEBUG_FIRMWARE, "returning type= %d\n", rc);
1666		break;
1667	case SXIO_DO_RAMTEST:
1668		if (sx_initialized) /* Already initialized: better not ramtest the board.  */
1669			return -EPERM;
1670		if (IS_SX_BOARD (board)) {
1671			rc          = do_memtest   (board, 0, 0x7000);
1672			if (!rc) rc = do_memtest   (board, 0, 0x7000);
1673			/*if (!rc) rc = do_memtest_w (board, 0, 0x7000);*/
1674		} else {
1675			rc             = do_memtest   (board, 0, 0x7ff8);
1676			/* if (!rc) rc = do_memtest_w (board, 0, 0x7ff8); */
1677		}
1678		sx_dprintk (SX_DEBUG_FIRMWARE, "returning memtest result= %d\n", rc);
1679		break;
1680	case SXIO_DOWNLOAD:
1681		if (sx_initialized) /* Already initialized */
1682			return -EEXIST;
1683		if (!sx_reset (board)) 
1684			return -EIO;
1685		sx_dprintk (SX_DEBUG_INIT, "reset the board...\n");
1686
1687		tmp = kmalloc (SX_CHUNK_SIZE, GFP_USER);
1688		if (!tmp) return -ENOMEM;
1689		get_user (nbytes, descr++);
1690		get_user (offset, descr++); 
1691		get_user (data,	 descr++);
1692		while (nbytes && data) {
1693			for (i=0;i<nbytes;i += SX_CHUNK_SIZE) {
1694				if (copy_from_user(tmp, (char __user *)data+i, 
1695						   (i + SX_CHUNK_SIZE >
1696						    nbytes) ? nbytes - i :
1697						   	      SX_CHUNK_SIZE)) {
1698					kfree (tmp);
1699					return -EFAULT;
1700				}
1701				memcpy_toio(board->base2 + offset + i, tmp, 
1702				                (i+SX_CHUNK_SIZE>nbytes)?nbytes-i:SX_CHUNK_SIZE);
1703			}
1704
1705			get_user (nbytes, descr++);
1706			get_user (offset, descr++); 
1707			get_user (data,   descr++);
1708		}
1709		kfree (tmp);
1710		sx_nports += sx_init_board (board);
1711		rc = sx_nports;

Large files files are truncated, but you can click here to view the full file