PageRenderTime 71ms CodeModel.GetById 18ms app.highlight 39ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/char/sx.c

https://bitbucket.org/cresqo/cm7-p500-kernel
C | 2894 lines | 2000 code | 388 blank | 506 comment | 329 complexity | 4687a44d3d9a40fa7059fcf6dd30472a MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0

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

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

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