sx.c 75.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/* sx.c -- driver for the Specialix SX series cards. 
 *
 *  This driver will also support the older SI, and XIO cards.
 *
 *
 *   (C) 1998 - 2004  R.E.Wolff@BitWizard.nl
 *
 *  Simon Allen (simonallen@cix.compulink.co.uk) wrote a previous
 *  version of this driver. Some fragments may have been copied. (none
 *  yet :-)
 *
 * Specialix pays for the development and support of this driver.
 * Please DO contact support@specialix.co.uk if you require
 * support. But please read the documentation (sx.txt) first.
 *
 *
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License as
 *      published by the Free Software Foundation; either version 2 of
 *      the License, or (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be
 *      useful, but WITHOUT ANY WARRANTY; without even the implied
 *      warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 *      PURPOSE.  See the GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public
 *      License along with this program; if not, write to the Free
 *      Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
 *      USA.
 *
 * Revision history:
 * Revision 1.33  2000/03/09 10:00:00  pvdl,wolff
 * - Fixed module and port counting
 * - Fixed signal handling
 * - Fixed an Ooops
 * 
 * Revision 1.32  2000/03/07 09:00:00  wolff,pvdl
 * - Fixed some sx_dprintk typos
 * - added detection for an invalid board/module configuration
 *
 * Revision 1.31  2000/03/06 12:00:00  wolff,pvdl
 * - Added support for EISA
 *
 * Revision 1.30  2000/01/21 17:43:06  wolff
 * - Added support for SX+
 *
 * Revision 1.26  1999/08/05 15:22:14  wolff
 * - Port to 2.3.x
 * - Reformatted to Linus' liking.
 *
 * Revision 1.25  1999/07/30 14:24:08  wolff
 * Had accidentally left "gs_debug" set to "-1" instead of "off" (=0).
 *
 * Revision 1.24  1999/07/28 09:41:52  wolff
 * - I noticed the remark about use-count straying in sx.txt. I checked
 *   sx_open, and found a few places where that could happen. I hope it's
 *   fixed now.
 *
 * Revision 1.23  1999/07/28 08:56:06  wolff
 * - Fixed crash when sx_firmware run twice.
 * - Added sx_slowpoll as a module parameter (I guess nobody really wanted
 *   to change it from the default... )
 * - Fixed a stupid editing problem I introduced in 1.22.
 * - Fixed dropping characters on a termios change.
 *
 * Revision 1.22  1999/07/26 21:01:43  wolff
 * Russell Brown noticed that I had overlooked 4 out of six modem control
 * signals in sx_getsignals. Ooops.
 *
 * Revision 1.21  1999/07/23 09:11:33  wolff
 * I forgot to free dynamically allocated memory when the driver is unloaded.
 *
 * Revision 1.20  1999/07/20 06:25:26  wolff
 * The "closing wait" wasn't honoured. Thanks to James Griffiths for
 * reporting this.
 *
 * Revision 1.19  1999/07/11 08:59:59  wolff
 * Fixed an oops in close, when an open was pending. Changed the memtest
 * a bit. Should also test the board in word-mode, however my card fails the
 * memtest then. I still have to figure out what is wrong...
 *
 * Revision 1.18  1999/06/10 09:38:42  wolff
 * Changed the format of the firmware revision from %04x to %x.%02x .
 *
 * Revision 1.17  1999/06/04 09:44:35  wolff
 * fixed problem: reference to pci stuff when config_pci was off...
 * Thanks to Jorge Novo for noticing this.
 *
 * Revision 1.16  1999/06/02 08:30:15  wolff
 * added/removed the workaround for the DCD bug in the Firmware.
 * A bit more debugging code to locate that...
 *
 * Revision 1.15  1999/06/01 11:35:30  wolff
 * when DCD is left low (floating?), on TA's the firmware first tells us
 * that DCD is high, but after a short while suddenly comes to the
 * conclusion that it is low. All this would be fine, if it weren't that
 * Unix requires us to send a "hangup" signal in that case. This usually
 * all happens BEFORE the program has had a chance to ioctl the device
 * into clocal mode..
 *
 * Revision 1.14  1999/05/25 11:18:59  wolff
 * Added PCI-fix.
 * Added checks for return code of sx_sendcommand.
 * Don't issue "reconfig" if port isn't open yet. (bit us on TA modules...)
 *
 * Revision 1.13  1999/04/29 15:18:01  wolff
 * Fixed an "oops" that showed on SuSE 6.0 systems.
 * Activate DTR again after stty 0.
 *
 * Revision 1.12  1999/04/29 07:49:52  wolff
 * Improved "stty 0" handling a bit. (used to change baud to 9600 assuming
 *     the connection would be dropped anyway. That is not always the case,
 *     and confuses people).
 * Told the card to always monitor the modem signals.
 * Added support for dynamic  gs_debug adjustments.
 * Now tells the rest of the system the number of ports.
 *
 * Revision 1.11  1999/04/24 11:11:30  wolff
 * Fixed two stupid typos in the memory test.
 *
 * Revision 1.10  1999/04/24 10:53:39  wolff
 * Added some of Christian's suggestions.
 * Fixed an HW_COOK_IN bug (ISIG was not in I_OTHER. We used to trust the
 * card to send the signal to the process.....)
 *
 * Revision 1.9  1999/04/23 07:26:38  wolff
 * Included Christian Lademann's 2.0 compile-warning fixes and interrupt
 *    assignment redesign.
 * Cleanup of some other stuff.
 *
 * Revision 1.8  1999/04/16 13:05:30  wolff
 * fixed a DCD change unnoticed bug.
 *
 * Revision 1.7  1999/04/14 22:19:51  wolff
 * Fixed typo that showed up in 2.0.x builds (get_user instead of Get_user!)
 *
 * Revision 1.6  1999/04/13 18:40:20  wolff
 * changed misc-minor to 161, as assigned by HPA.
 *
 * Revision 1.5  1999/04/13 15:12:25  wolff
 * Fixed use-count leak when "hangup" occurred.
 * Added workaround for a stupid-PCIBIOS bug.
 *
 *
 * Revision 1.4  1999/04/01 22:47:40  wolff
 * Fixed < 1M linux-2.0 problem.
 * (vremap isn't compatible with ioremap in that case)
 *
 * Revision 1.3  1999/03/31 13:45:45  wolff
 * Firmware loading is now done through a separate IOCTL.
 *
 * Revision 1.2  1999/03/28 12:22:29  wolff
 * rcs cleanup
 *
 * Revision 1.1  1999/03/28 12:10:34  wolff
 * Readying for release on 2.0.x (sorry David, 1.01 becomes 1.1 for RCS). 
 *
 * Revision 0.12  1999/03/28 09:20:10  wolff
 * Fixed problem in 0.11, continueing cleanup.
 *
 * Revision 0.11  1999/03/28 08:46:44  wolff
 * cleanup. Not good.
 *
 * Revision 0.10  1999/03/28 08:09:43  wolff
 * Fixed loosing characters on close.
 *
 * Revision 0.9  1999/03/21 22:52:01  wolff
 * Ported back to 2.2.... (minor things)
 *
 * Revision 0.8  1999/03/21 22:40:33  wolff
 * Port to 2.0
 *
 * Revision 0.7  1999/03/21 19:06:34  wolff
 * Fixed hangup processing.
 *
 * Revision 0.6  1999/02/05 08:45:14  wolff
 * fixed real_raw problems. Inclusion into kernel imminent.
 *
 * Revision 0.5  1998/12/21 23:51:06  wolff
 * Snatched a nasty bug: sx_transmit_chars was getting re-entered, and it
 * shouldn't have. THATs why I want to have transmit interrupts even when
 * the buffer is empty.
 *
 * Revision 0.4  1998/12/17 09:34:46  wolff
 * PPP works. ioctl works. Basically works!
 *
 * Revision 0.3  1998/12/15 13:05:18  wolff
 * It works! Wow! Gotta start implementing IOCTL and stuff....
 *
 * Revision 0.2  1998/12/01 08:33:53  wolff
 * moved over to 2.1.130
 *
 * Revision 0.1  1998/11/03 21:23:51  wolff
 * Initial revision. Detects SX card.
 *
 * */

200
#define SX_VERSION	1.33
L
Linus Torvalds 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

#include <linux/module.h>
#include <linux/kdev_t.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <linux/errno.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/mm.h>
#include <linux/serial.h>
#include <linux/fcntl.h>
#include <linux/major.h>
#include <linux/delay.h>
J
Jiri Slaby 已提交
216
#include <linux/eisa.h>
L
Linus Torvalds 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
#include <linux/pci.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/bitops.h>

#include <asm/io.h>
#include <asm/uaccess.h>

/* The 3.0.0 version of sxboards/sxwindow.h  uses BYTE and WORD.... */
#define BYTE u8
#define WORD u16

/* .... but the 3.0.4 version uses _u8 and _u16. */
#define _u8 u8
#define _u16 u16

#include "sxboards.h"
#include "sxwindow.h"

#include <linux/generic_serial.h>
#include "sx.h"

/* I don't think that this driver can handle more than 256 ports on
   one machine. You'll have to increase the number of boards in sx.h
   if you want more than 4 boards.  */

#ifndef PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8
#define PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8 0x2000
#endif

/* Configurable options: 
   (Don't be too sure that it'll work if you toggle them) */

/* Am I paranoid or not ? ;-) */
#undef SX_PARANOIA_CHECK

/* 20 -> 2000 per second. The card should rate-limit interrupts at 100
   Hz, but it is user configurable. I don't recommend going above 1000
   Hz. The interrupt ratelimit might trigger if the interrupt is
   shared with a very active other device. */
#define IRQ_RATE_LIMIT 20

/* Sharing interrupts is possible now. If the other device wants more
   than 2000 interrupts per second, we'd gracefully decline further
   interrupts. That's not what we want. On the other hand, if the
   other device interrupts 2000 times a second, don't use the SX
   interrupt. Use polling. */
#undef IRQ_RATE_LIMIT

#if 0
/* Not implemented */
/* 
 * The following defines are mostly for testing purposes. But if you need
 * some nice reporting in your syslog, you can define them also.
 */
#define SX_REPORT_FIFO
#define SX_REPORT_OVERRUN
J
Jiri Slaby 已提交
275
#endif
L
Linus Torvalds 已提交
276 277

/* Function prototypes */
J
Jiri Slaby 已提交
278 279 280 281
static void sx_disable_tx_interrupts(void *ptr);
static void sx_enable_tx_interrupts(void *ptr);
static void sx_disable_rx_interrupts(void *ptr);
static void sx_enable_rx_interrupts(void *ptr);
282
static int sx_carrier_raised(struct tty_port *port);
J
Jiri Slaby 已提交
283 284 285 286 287 288
static void sx_shutdown_port(void *ptr);
static int sx_set_real_termios(void *ptr);
static void sx_close(void *ptr);
static int sx_chars_in_buffer(void *ptr);
static int sx_init_board(struct sx_board *board);
static int sx_init_portstructs(int nboards, int nports);
289 290
static long sx_fw_ioctl(struct file *filp, unsigned int cmd,
						unsigned long arg);
L
Linus Torvalds 已提交
291 292 293 294
static int sx_init_drivers(void);

static struct tty_driver *sx_driver;

J
Jiri Slaby 已提交
295
static DEFINE_MUTEX(sx_boards_lock);
L
Linus Torvalds 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
static struct sx_board boards[SX_NBOARDS];
static struct sx_port *sx_ports;
static int sx_initialized;
static int sx_nports;
static int sx_debug;

/* You can have the driver poll your card. 
    - Set sx_poll to 1 to poll every timer tick (10ms on Intel). 
      This is used when the card cannot use an interrupt for some reason.

    - set sx_slowpoll to 100 to do an extra poll once a second (on Intel). If 
      the driver misses an interrupt (report this if it DOES happen to you!)
      everything will continue to work.... 
 */
static int sx_poll = 1;
static int sx_slowpoll;

/* The card limits the number of interrupts per second. 
   At 115k2 "100" should be sufficient. 
   If you're using higher baudrates, you can increase this...
 */

static int sx_maxints = 100;

J
Jiri Slaby 已提交
320 321
#ifdef CONFIG_ISA

L
Linus Torvalds 已提交
322 323 324 325
/* These are the only open spaces in my computer. Yours may have more
   or less.... -- REW 
   duh: Card at 0xa0000 is possible on HP Netserver?? -- pvdl
*/
J
Jiri Slaby 已提交
326 327 328 329 330 331 332 333 334 335 336
static int sx_probe_addrs[] = {
	0xc0000, 0xd0000, 0xe0000,
	0xc8000, 0xd8000, 0xe8000
};
static int si_probe_addrs[] = {
	0xc0000, 0xd0000, 0xe0000,
	0xc8000, 0xd8000, 0xe8000, 0xa0000
};
static int si1_probe_addrs[] = {
	0xd0000
};
L
Linus Torvalds 已提交
337

338 339 340
#define NR_SX_ADDRS ARRAY_SIZE(sx_probe_addrs)
#define NR_SI_ADDRS ARRAY_SIZE(si_probe_addrs)
#define NR_SI1_ADDRS ARRAY_SIZE(si1_probe_addrs)
L
Linus Torvalds 已提交
341

J
Jiri Slaby 已提交
342 343 344
module_param_array(sx_probe_addrs, int, NULL, 0);
module_param_array(si_probe_addrs, int, NULL, 0);
#endif
L
Linus Torvalds 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

/* Set the mask to all-ones. This alas, only supports 32 interrupts. 
   Some architectures may need more. */
static int sx_irqmask = -1;

module_param(sx_poll, int, 0);
module_param(sx_slowpoll, int, 0);
module_param(sx_maxints, int, 0);
module_param(sx_debug, int, 0);
module_param(sx_irqmask, int, 0);

MODULE_LICENSE("GPL");

static struct real_driver sx_real_driver = {
	sx_disable_tx_interrupts,
	sx_enable_tx_interrupts,
	sx_disable_rx_interrupts,
	sx_enable_rx_interrupts,
J
Jiri Slaby 已提交
363 364
	sx_shutdown_port,
	sx_set_real_termios,
L
Linus Torvalds 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	sx_chars_in_buffer,
	sx_close,
};

/* 
   This driver can spew a whole lot of debugging output at you. If you
   need maximum performance, you should disable the DEBUG define. To
   aid in debugging in the field, I'm leaving the compile-time debug
   features enabled, and disable them "runtime". That allows me to
   instruct people with problems to enable debugging without requiring
   them to recompile... 
*/
#define DEBUG

#ifdef DEBUG
J
Jiri Slaby 已提交
380
#define sx_dprintk(f, str...)	if (sx_debug & f) printk (str)
L
Linus Torvalds 已提交
381
#else
J
Jiri Slaby 已提交
382
#define sx_dprintk(f, str...)	/* nothing */
L
Linus Torvalds 已提交
383 384
#endif

385 386
#define func_enter()	sx_dprintk(SX_DEBUG_FLOW, "sx: enter %s\n",__func__)
#define func_exit()	sx_dprintk(SX_DEBUG_FLOW, "sx: exit  %s\n",__func__)
L
Linus Torvalds 已提交
387

J
Jiri Slaby 已提交
388
#define func_enter2()	sx_dprintk(SX_DEBUG_FLOW, "sx: enter %s (port %d)\n", \
389
				__func__, port->line)
L
Linus Torvalds 已提交
390 391 392 393 394 395

/* 
 *  Firmware loader driver specific routines
 *
 */

396
static const struct file_operations sx_fw_fops = {
J
Jiri Slaby 已提交
397
	.owner = THIS_MODULE,
398
	.unlocked_ioctl = sx_fw_ioctl,
L
Linus Torvalds 已提交
399 400 401 402 403 404 405 406 407 408
};

static struct miscdevice sx_fw_device = {
	SXCTL_MISC_MINOR, "sxctl", &sx_fw_fops
};

#ifdef SX_PARANOIA_CHECK

/* This doesn't work. Who's paranoid around here? Not me! */

J
Jiri Slaby 已提交
409
static inline int sx_paranoia_check(struct sx_port const *port,
L
Linus Torvalds 已提交
410 411
				    char *name, const char *routine)
{
J
Jiri Slaby 已提交
412 413 414 415
	static const char *badmagic = KERN_ERR "sx: Warning: bad sx port magic "
			"number for device %s in %s\n";
	static const char *badinfo = KERN_ERR "sx: Warning: null sx port for "
			"device %s in %s\n";
L
Linus Torvalds 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

	if (!port) {
		printk(badinfo, name, routine);
		return 1;
	}
	if (port->magic != SX_MAGIC) {
		printk(badmagic, name, routine);
		return 1;
	}

	return 0;
}
#else
#define sx_paranoia_check(a,b,c) 0
#endif

/* The timeouts. First try 30 times as fast as possible. Then give
   the card some time to breathe between accesses. (Otherwise the
   processor on the card might not be able to access its OWN bus... */

#define TIMEOUT_1 30
#define TIMEOUT_2 1000000

#ifdef DEBUG
static void my_hd_io(void __iomem *p, int len)
{
	int i, j, ch;
	unsigned char __iomem *addr = p;

J
Jiri Slaby 已提交
445 446 447 448 449
	for (i = 0; i < len; i += 16) {
		printk("%p ", addr + i);
		for (j = 0; j < 16; j++) {
			printk("%02x %s", readb(addr + j + i),
					(j == 7) ? " " : "");
L
Linus Torvalds 已提交
450
		}
J
Jiri Slaby 已提交
451 452 453 454
		for (j = 0; j < 16; j++) {
			ch = readb(addr + j + i);
			printk("%c", (ch < 0x20) ? '.' :
					((ch > 0x7f) ? '.' : ch));
L
Linus Torvalds 已提交
455
		}
J
Jiri Slaby 已提交
456
		printk("\n");
L
Linus Torvalds 已提交
457 458 459 460 461 462 463
	}
}
static void my_hd(void *p, int len)
{
	int i, j, ch;
	unsigned char *addr = p;

J
Jiri Slaby 已提交
464 465 466 467
	for (i = 0; i < len; i += 16) {
		printk("%p ", addr + i);
		for (j = 0; j < 16; j++) {
			printk("%02x %s", addr[j + i], (j == 7) ? " " : "");
L
Linus Torvalds 已提交
468
		}
J
Jiri Slaby 已提交
469 470 471 472
		for (j = 0; j < 16; j++) {
			ch = addr[j + i];
			printk("%c", (ch < 0x20) ? '.' :
					((ch > 0x7f) ? '.' : ch));
L
Linus Torvalds 已提交
473
		}
J
Jiri Slaby 已提交
474
		printk("\n");
L
Linus Torvalds 已提交
475 476 477 478 479 480
	}
}
#endif

/* This needs redoing for Alpha -- REW -- Done. */

J
Jiri Slaby 已提交
481
static inline void write_sx_byte(struct sx_board *board, int offset, u8 byte)
L
Linus Torvalds 已提交
482
{
J
Jiri Slaby 已提交
483
	writeb(byte, board->base + offset);
L
Linus Torvalds 已提交
484 485
}

J
Jiri Slaby 已提交
486
static inline u8 read_sx_byte(struct sx_board *board, int offset)
L
Linus Torvalds 已提交
487
{
J
Jiri Slaby 已提交
488
	return readb(board->base + offset);
L
Linus Torvalds 已提交
489 490
}

J
Jiri Slaby 已提交
491
static inline void write_sx_word(struct sx_board *board, int offset, u16 word)
L
Linus Torvalds 已提交
492
{
J
Jiri Slaby 已提交
493
	writew(word, board->base + offset);
L
Linus Torvalds 已提交
494 495
}

J
Jiri Slaby 已提交
496
static inline u16 read_sx_word(struct sx_board *board, int offset)
L
Linus Torvalds 已提交
497
{
J
Jiri Slaby 已提交
498
	return readw(board->base + offset);
L
Linus Torvalds 已提交
499 500
}

J
Jiri Slaby 已提交
501 502
static int sx_busy_wait_eq(struct sx_board *board,
		int offset, int mask, int correctval)
L
Linus Torvalds 已提交
503 504 505
{
	int i;

J
Jiri Slaby 已提交
506
	func_enter();
L
Linus Torvalds 已提交
507

J
Jiri Slaby 已提交
508 509 510
	for (i = 0; i < TIMEOUT_1; i++)
		if ((read_sx_byte(board, offset) & mask) == correctval) {
			func_exit();
L
Linus Torvalds 已提交
511 512 513
			return 1;
		}

J
Jiri Slaby 已提交
514 515 516
	for (i = 0; i < TIMEOUT_2; i++) {
		if ((read_sx_byte(board, offset) & mask) == correctval) {
			func_exit();
L
Linus Torvalds 已提交
517 518
			return 1;
		}
J
Jiri Slaby 已提交
519
		udelay(1);
L
Linus Torvalds 已提交
520 521
	}

J
Jiri Slaby 已提交
522
	func_exit();
L
Linus Torvalds 已提交
523 524 525
	return 0;
}

J
Jiri Slaby 已提交
526 527
static int sx_busy_wait_neq(struct sx_board *board,
		int offset, int mask, int badval)
L
Linus Torvalds 已提交
528 529 530
{
	int i;

J
Jiri Slaby 已提交
531
	func_enter();
L
Linus Torvalds 已提交
532

J
Jiri Slaby 已提交
533 534 535
	for (i = 0; i < TIMEOUT_1; i++)
		if ((read_sx_byte(board, offset) & mask) != badval) {
			func_exit();
L
Linus Torvalds 已提交
536 537 538
			return 1;
		}

J
Jiri Slaby 已提交
539 540 541
	for (i = 0; i < TIMEOUT_2; i++) {
		if ((read_sx_byte(board, offset) & mask) != badval) {
			func_exit();
L
Linus Torvalds 已提交
542 543
			return 1;
		}
J
Jiri Slaby 已提交
544
		udelay(1);
L
Linus Torvalds 已提交
545 546
	}

J
Jiri Slaby 已提交
547
	func_exit();
L
Linus Torvalds 已提交
548 549 550 551
	return 0;
}

/* 5.6.4 of 6210028 r2.3 */
J
Jiri Slaby 已提交
552
static int sx_reset(struct sx_board *board)
L
Linus Torvalds 已提交
553
{
J
Jiri Slaby 已提交
554
	func_enter();
L
Linus Torvalds 已提交
555

J
Jiri Slaby 已提交
556
	if (IS_SX_BOARD(board)) {
L
Linus Torvalds 已提交
557

J
Jiri Slaby 已提交
558 559
		write_sx_byte(board, SX_CONFIG, 0);
		write_sx_byte(board, SX_RESET, 1); /* Value doesn't matter */
L
Linus Torvalds 已提交
560

J
Jiri Slaby 已提交
561 562 563
		if (!sx_busy_wait_eq(board, SX_RESET_STATUS, 1, 0)) {
			printk(KERN_INFO "sx: Card doesn't respond to "
					"reset...\n");
L
Linus Torvalds 已提交
564 565 566
			return 0;
		}
	} else if (IS_EISA_BOARD(board)) {
J
Jiri Slaby 已提交
567
		outb(board->irq << 4, board->eisa_base + 0xc02);
L
Linus Torvalds 已提交
568
	} else if (IS_SI1_BOARD(board)) {
J
Jiri Slaby 已提交
569
		write_sx_byte(board, SI1_ISA_RESET, 0);	/*value doesn't matter*/
L
Linus Torvalds 已提交
570 571
	} else {
		/* Gory details of the SI/ISA board */
J
Jiri Slaby 已提交
572 573 574 575 576 577
		write_sx_byte(board, SI2_ISA_RESET, SI2_ISA_RESET_SET);
		write_sx_byte(board, SI2_ISA_IRQ11, SI2_ISA_IRQ11_CLEAR);
		write_sx_byte(board, SI2_ISA_IRQ12, SI2_ISA_IRQ12_CLEAR);
		write_sx_byte(board, SI2_ISA_IRQ15, SI2_ISA_IRQ15_CLEAR);
		write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_CLEAR);
		write_sx_byte(board, SI2_ISA_IRQSET, SI2_ISA_IRQSET_CLEAR);
L
Linus Torvalds 已提交
578 579
	}

J
Jiri Slaby 已提交
580
	func_exit();
L
Linus Torvalds 已提交
581 582 583 584 585 586 587 588
	return 1;
}

/* This doesn't work on machines where "NULL" isn't 0 */
/* If you have one of those, someone will need to write 
   the equivalent of this, which will amount to about 3 lines. I don't
   want to complicate this right now. -- REW
   (See, I do write comments every now and then :-) */
J
Jiri Slaby 已提交
589
#define OFFSETOF(strct, elem)	((long)&(((struct strct *)NULL)->elem))
L
Linus Torvalds 已提交
590

J
Jiri Slaby 已提交
591 592 593
#define CHAN_OFFSET(port,elem)	(port->ch_base + OFFSETOF (_SXCHANNEL, elem))
#define MODU_OFFSET(board,addr,elem)	(addr + OFFSETOF (_SXMODULE, elem))
#define  BRD_OFFSET(board,elem)	(OFFSETOF (_SXCARD, elem))
L
Linus Torvalds 已提交
594 595

#define sx_write_channel_byte(port, elem, val) \
J
Jiri Slaby 已提交
596
	write_sx_byte (port->board, CHAN_OFFSET (port, elem), val)
L
Linus Torvalds 已提交
597 598

#define sx_read_channel_byte(port, elem) \
J
Jiri Slaby 已提交
599
	read_sx_byte (port->board, CHAN_OFFSET (port, elem))
L
Linus Torvalds 已提交
600 601

#define sx_write_channel_word(port, elem, val) \
J
Jiri Slaby 已提交
602
	write_sx_word (port->board, CHAN_OFFSET (port, elem), val)
L
Linus Torvalds 已提交
603 604

#define sx_read_channel_word(port, elem) \
J
Jiri Slaby 已提交
605
	read_sx_word (port->board, CHAN_OFFSET (port, elem))
L
Linus Torvalds 已提交
606 607

#define sx_write_module_byte(board, addr, elem, val) \
J
Jiri Slaby 已提交
608
	write_sx_byte (board, MODU_OFFSET (board, addr, elem), val)
L
Linus Torvalds 已提交
609 610

#define sx_read_module_byte(board, addr, elem) \
J
Jiri Slaby 已提交
611
	read_sx_byte (board, MODU_OFFSET (board, addr, elem))
L
Linus Torvalds 已提交
612 613

#define sx_write_module_word(board, addr, elem, val) \
J
Jiri Slaby 已提交
614
	write_sx_word (board, MODU_OFFSET (board, addr, elem), val)
L
Linus Torvalds 已提交
615 616

#define sx_read_module_word(board, addr, elem) \
J
Jiri Slaby 已提交
617
	read_sx_word (board, MODU_OFFSET (board, addr, elem))
L
Linus Torvalds 已提交
618 619

#define sx_write_board_byte(board, elem, val) \
J
Jiri Slaby 已提交
620
	write_sx_byte (board, BRD_OFFSET (board, elem), val)
L
Linus Torvalds 已提交
621 622

#define sx_read_board_byte(board, elem) \
J
Jiri Slaby 已提交
623
	read_sx_byte (board, BRD_OFFSET (board, elem))
L
Linus Torvalds 已提交
624 625

#define sx_write_board_word(board, elem, val) \
J
Jiri Slaby 已提交
626
	write_sx_word (board, BRD_OFFSET (board, elem), val)
L
Linus Torvalds 已提交
627 628

#define sx_read_board_word(board, elem) \
J
Jiri Slaby 已提交
629
	read_sx_word (board, BRD_OFFSET (board, elem))
L
Linus Torvalds 已提交
630

J
Jiri Slaby 已提交
631
static int sx_start_board(struct sx_board *board)
L
Linus Torvalds 已提交
632
{
J
Jiri Slaby 已提交
633 634
	if (IS_SX_BOARD(board)) {
		write_sx_byte(board, SX_CONFIG, SX_CONF_BUSEN);
L
Linus Torvalds 已提交
635 636
	} else if (IS_EISA_BOARD(board)) {
		write_sx_byte(board, SI2_EISA_OFF, SI2_EISA_VAL);
J
Jiri Slaby 已提交
637
		outb((board->irq << 4) | 4, board->eisa_base + 0xc02);
L
Linus Torvalds 已提交
638
	} else if (IS_SI1_BOARD(board)) {
J
Jiri Slaby 已提交
639 640
		write_sx_byte(board, SI1_ISA_RESET_CLEAR, 0);
		write_sx_byte(board, SI1_ISA_INTCL, 0);
L
Linus Torvalds 已提交
641 642 643
	} else {
		/* Don't bug me about the clear_set. 
		   I haven't the foggiest idea what it's about -- REW */
J
Jiri Slaby 已提交
644 645
		write_sx_byte(board, SI2_ISA_RESET, SI2_ISA_RESET_CLEAR);
		write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
L
Linus Torvalds 已提交
646 647 648 649 650
	}
	return 1;
}

#define SX_IRQ_REG_VAL(board) \
J
Jiri Slaby 已提交
651
	((board->flags & SX_ISA_BOARD) ? (board->irq << 4) : 0)
L
Linus Torvalds 已提交
652 653 654

/* Note. The SX register is write-only. Therefore, we have to enable the
   bus too. This is a no-op, if you don't mess with this driver... */
J
Jiri Slaby 已提交
655
static int sx_start_interrupts(struct sx_board *board)
L
Linus Torvalds 已提交
656 657 658 659 660
{

	/* Don't call this with board->irq == 0 */

	if (IS_SX_BOARD(board)) {
J
Jiri Slaby 已提交
661 662
		write_sx_byte(board, SX_CONFIG, SX_IRQ_REG_VAL(board) |
				SX_CONF_BUSEN | SX_CONF_HOSTIRQ);
L
Linus Torvalds 已提交
663
	} else if (IS_EISA_BOARD(board)) {
J
Jiri Slaby 已提交
664
		inb(board->eisa_base + 0xc03);
L
Linus Torvalds 已提交
665
	} else if (IS_SI1_BOARD(board)) {
J
Jiri Slaby 已提交
666 667
		write_sx_byte(board, SI1_ISA_INTCL, 0);
		write_sx_byte(board, SI1_ISA_INTCL_CLEAR, 0);
L
Linus Torvalds 已提交
668 669
	} else {
		switch (board->irq) {
J
Jiri Slaby 已提交
670 671 672 673 674 675 676 677 678 679 680 681 682
		case 11:
			write_sx_byte(board, SI2_ISA_IRQ11, SI2_ISA_IRQ11_SET);
			break;
		case 12:
			write_sx_byte(board, SI2_ISA_IRQ12, SI2_ISA_IRQ12_SET);
			break;
		case 15:
			write_sx_byte(board, SI2_ISA_IRQ15, SI2_ISA_IRQ15_SET);
			break;
		default:
			printk(KERN_INFO "sx: SI/XIO card doesn't support "
					"interrupt %d.\n", board->irq);
			return 0;
L
Linus Torvalds 已提交
683
		}
J
Jiri Slaby 已提交
684
		write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
L
Linus Torvalds 已提交
685 686 687 688 689
	}

	return 1;
}

J
Jiri Slaby 已提交
690 691
static int sx_send_command(struct sx_port *port,
		int command, int mask, int newstat)
L
Linus Torvalds 已提交
692
{
J
Jiri Slaby 已提交
693 694 695 696 697
	func_enter2();
	write_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat), command);
	func_exit();
	return sx_busy_wait_eq(port->board, CHAN_OFFSET(port, hi_hstat), mask,
			newstat);
L
Linus Torvalds 已提交
698 699
}

J
Jiri Slaby 已提交
700
static char *mod_type_s(int module_type)
L
Linus Torvalds 已提交
701 702
{
	switch (module_type) {
J
Jiri Slaby 已提交
703 704 705 706 707 708 709 710 711 712 713 714 715 716
	case TA4:
		return "TA4";
	case TA8:
		return "TA8";
	case TA4_ASIC:
		return "TA4_ASIC";
	case TA8_ASIC:
		return "TA8_ASIC";
	case MTA_CD1400:
		return "MTA_CD1400";
	case SXDC:
		return "SXDC";
	default:
		return "Unknown/invalid";
L
Linus Torvalds 已提交
717 718 719
	}
}

J
Jiri Slaby 已提交
720
static char *pan_type_s(int pan_type)
L
Linus Torvalds 已提交
721 722
{
	switch (pan_type) {
J
Jiri Slaby 已提交
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
	case MOD_RS232DB25:
		return "MOD_RS232DB25";
	case MOD_RS232RJ45:
		return "MOD_RS232RJ45";
	case MOD_RS422DB25:
		return "MOD_RS422DB25";
	case MOD_PARALLEL:
		return "MOD_PARALLEL";
	case MOD_2_RS232DB25:
		return "MOD_2_RS232DB25";
	case MOD_2_RS232RJ45:
		return "MOD_2_RS232RJ45";
	case MOD_2_RS422DB25:
		return "MOD_2_RS422DB25";
	case MOD_RS232DB25MALE:
		return "MOD_RS232DB25MALE";
	case MOD_2_PARALLEL:
		return "MOD_2_PARALLEL";
	case MOD_BLANK:
		return "empty";
	default:
		return "invalid";
L
Linus Torvalds 已提交
745 746 747
	}
}

J
Jiri Slaby 已提交
748
static int mod_compat_type(int module_type)
L
Linus Torvalds 已提交
749 750 751 752 753 754
{
	return module_type >> 4;
}

static void sx_reconfigure_port(struct sx_port *port)
{
J
Jiri Slaby 已提交
755 756 757 758
	if (sx_read_channel_byte(port, hi_hstat) == HS_IDLE_OPEN) {
		if (sx_send_command(port, HS_CONFIG, -1, HS_IDLE_OPEN) != 1) {
			printk(KERN_WARNING "sx: Sent reconfigure command, but "
					"card didn't react.\n");
L
Linus Torvalds 已提交
759 760
		}
	} else {
J
Jiri Slaby 已提交
761 762 763 764
		sx_dprintk(SX_DEBUG_TERMIOS, "sx: Not sending reconfigure: "
				"port isn't open (%02x).\n",
				sx_read_channel_byte(port, hi_hstat));
	}
L
Linus Torvalds 已提交
765 766
}

J
Jiri Slaby 已提交
767
static void sx_setsignals(struct sx_port *port, int dtr, int rts)
L
Linus Torvalds 已提交
768 769
{
	int t;
J
Jiri Slaby 已提交
770
	func_enter2();
L
Linus Torvalds 已提交
771

J
Jiri Slaby 已提交
772 773 774 775 776 777 778
	t = sx_read_channel_byte(port, hi_op);
	if (dtr >= 0)
		t = dtr ? (t | OP_DTR) : (t & ~OP_DTR);
	if (rts >= 0)
		t = rts ? (t | OP_RTS) : (t & ~OP_RTS);
	sx_write_channel_byte(port, hi_op, t);
	sx_dprintk(SX_DEBUG_MODEMSIGNALS, "setsignals: %d/%d\n", dtr, rts);
L
Linus Torvalds 已提交
779

J
Jiri Slaby 已提交
780
	func_exit();
L
Linus Torvalds 已提交
781 782
}

J
Jiri Slaby 已提交
783
static int sx_getsignals(struct sx_port *port)
L
Linus Torvalds 已提交
784
{
J
Jiri Slaby 已提交
785 786 787 788 789 790 791 792
	int i_stat, o_stat;

	o_stat = sx_read_channel_byte(port, hi_op);
	i_stat = sx_read_channel_byte(port, hi_ip);

	sx_dprintk(SX_DEBUG_MODEMSIGNALS, "getsignals: %d/%d  (%d/%d) "
			"%02x/%02x\n",
			(o_stat & OP_DTR) != 0, (o_stat & OP_RTS) != 0,
793
			port->c_dcd, tty_port_carrier_raised(&port->gs.port),
J
Jiri Slaby 已提交
794 795 796 797 798 799 800 801 802
			sx_read_channel_byte(port, hi_ip),
			sx_read_channel_byte(port, hi_state));

	return (((o_stat & OP_DTR) ? TIOCM_DTR : 0) |
		((o_stat & OP_RTS) ? TIOCM_RTS : 0) |
		((i_stat & IP_CTS) ? TIOCM_CTS : 0) |
		((i_stat & IP_DCD) ? TIOCM_CAR : 0) |
		((i_stat & IP_DSR) ? TIOCM_DSR : 0) |
		((i_stat & IP_RI) ? TIOCM_RNG : 0));
L
Linus Torvalds 已提交
803 804
}

J
Jiri Slaby 已提交
805
static void sx_set_baud(struct sx_port *port)
L
Linus Torvalds 已提交
806 807 808 809 810
{
	int t;

	if (port->board->ta_type == MOD_SXDC) {
		switch (port->gs.baud) {
J
Jiri Slaby 已提交
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
			/* Save some typing work... */
#define e(x) case x: t = BAUD_ ## x; break
			e(50);
			e(75);
			e(110);
			e(150);
			e(200);
			e(300);
			e(600);
			e(1200);
			e(1800);
			e(2000);
			e(2400);
			e(4800);
			e(7200);
			e(9600);
			e(14400);
			e(19200);
			e(28800);
			e(38400);
			e(56000);
			e(57600);
			e(64000);
			e(76800);
			e(115200);
			e(128000);
			e(150000);
			e(230400);
			e(256000);
			e(460800);
			e(921600);
		case 134:
			t = BAUD_134_5;
			break;
		case 0:
			t = -1;
			break;
L
Linus Torvalds 已提交
848 849 850
		default:
			/* Can I return "invalid"? */
			t = BAUD_9600;
J
Jiri Slaby 已提交
851 852
			printk(KERN_INFO "sx: unsupported baud rate: %d.\n",
					port->gs.baud);
L
Linus Torvalds 已提交
853 854 855 856
			break;
		}
#undef e
		if (t > 0) {
J
Jiri Slaby 已提交
857 858
/* The baud rate is not set to 0, so we're enabeling DTR... -- REW */
			sx_setsignals(port, 1, -1);
L
Linus Torvalds 已提交
859
			/* XXX This is not TA & MTA compatible */
J
Jiri Slaby 已提交
860
			sx_write_channel_byte(port, hi_csr, 0xff);
L
Linus Torvalds 已提交
861

J
Jiri Slaby 已提交
862 863
			sx_write_channel_byte(port, hi_txbaud, t);
			sx_write_channel_byte(port, hi_rxbaud, t);
L
Linus Torvalds 已提交
864
		} else {
J
Jiri Slaby 已提交
865
			sx_setsignals(port, 0, -1);
L
Linus Torvalds 已提交
866 867 868
		}
	} else {
		switch (port->gs.baud) {
J
Jiri Slaby 已提交
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
#define e(x) case x: t = CSR_ ## x; break
			e(75);
			e(150);
			e(300);
			e(600);
			e(1200);
			e(2400);
			e(4800);
			e(1800);
			e(9600);
			e(19200);
			e(57600);
			e(38400);
/* TA supports 110, but not 115200, MTA supports 115200, but not 110 */
		case 110:
L
Linus Torvalds 已提交
884 885 886 887 888
			if (port->board->ta_type == MOD_TA) {
				t = CSR_110;
				break;
			} else {
				t = CSR_9600;
J
Jiri Slaby 已提交
889 890
				printk(KERN_INFO "sx: Unsupported baud rate: "
						"%d.\n", port->gs.baud);
L
Linus Torvalds 已提交
891 892
				break;
			}
J
Jiri Slaby 已提交
893
		case 115200:
L
Linus Torvalds 已提交
894 895
			if (port->board->ta_type == MOD_TA) {
				t = CSR_9600;
J
Jiri Slaby 已提交
896 897
				printk(KERN_INFO "sx: Unsupported baud rate: "
						"%d.\n", port->gs.baud);
L
Linus Torvalds 已提交
898 899 900 901 902
				break;
			} else {
				t = CSR_110;
				break;
			}
J
Jiri Slaby 已提交
903 904 905
		case 0:
			t = -1;
			break;
L
Linus Torvalds 已提交
906 907
		default:
			t = CSR_9600;
J
Jiri Slaby 已提交
908 909
			printk(KERN_INFO "sx: Unsupported baud rate: %d.\n",
					port->gs.baud);
L
Linus Torvalds 已提交
910 911 912 913
			break;
		}
#undef e
		if (t >= 0) {
J
Jiri Slaby 已提交
914 915
			sx_setsignals(port, 1, -1);
			sx_write_channel_byte(port, hi_csr, t * 0x11);
L
Linus Torvalds 已提交
916
		} else {
J
Jiri Slaby 已提交
917
			sx_setsignals(port, 0, -1);
L
Linus Torvalds 已提交
918 919 920 921 922 923 924
		}
	}
}

/* Simon Allen's version of this routine was 225 lines long. 85 is a lot
   better. -- REW */

J
Jiri Slaby 已提交
925
static int sx_set_real_termios(void *ptr)
L
Linus Torvalds 已提交
926 927 928 929 930
{
	struct sx_port *port = ptr;

	func_enter2();

A
Alan Cox 已提交
931
	if (!port->gs.port.tty)
L
Linus Torvalds 已提交
932 933 934 935 936 937 938 939
		return 0;

	/* What is this doing here? -- REW
	   Ha! figured it out. It is to allow you to get DTR active again
	   if you've dropped it with stty 0. Moved to set_baud, where it
	   belongs (next to the drop dtr if baud == 0) -- REW */
	/* sx_setsignals (port, 1, -1); */

J
Jiri Slaby 已提交
940
	sx_set_baud(port);
L
Linus Torvalds 已提交
941

A
Alan Cox 已提交
942
#define CFLAG port->gs.port.tty->termios->c_cflag
J
Jiri Slaby 已提交
943
	sx_write_channel_byte(port, hi_mr1,
A
Alan Cox 已提交
944 945 946
			(C_PARENB(port->gs.port.tty) ? MR1_WITH : MR1_NONE) |
			(C_PARODD(port->gs.port.tty) ? MR1_ODD : MR1_EVEN) |
			(C_CRTSCTS(port->gs.port.tty) ? MR1_RTS_RXFLOW : 0) |
J
Jiri Slaby 已提交
947 948 949 950 951 952
			(((CFLAG & CSIZE) == CS8) ? MR1_8_BITS : 0) |
			(((CFLAG & CSIZE) == CS7) ? MR1_7_BITS : 0) |
			(((CFLAG & CSIZE) == CS6) ? MR1_6_BITS : 0) |
			(((CFLAG & CSIZE) == CS5) ? MR1_5_BITS : 0));

	sx_write_channel_byte(port, hi_mr2,
A
Alan Cox 已提交
953 954
			(C_CRTSCTS(port->gs.port.tty) ? MR2_CTS_TXFLOW : 0) |
			(C_CSTOPB(port->gs.port.tty) ? MR2_2_STOP :
J
Jiri Slaby 已提交
955
			MR2_1_STOP));
L
Linus Torvalds 已提交
956 957

	switch (CFLAG & CSIZE) {
J
Jiri Slaby 已提交
958 959 960 961 962 963 964 965 966 967 968 969
	case CS8:
		sx_write_channel_byte(port, hi_mask, 0xff);
		break;
	case CS7:
		sx_write_channel_byte(port, hi_mask, 0x7f);
		break;
	case CS6:
		sx_write_channel_byte(port, hi_mask, 0x3f);
		break;
	case CS5:
		sx_write_channel_byte(port, hi_mask, 0x1f);
		break;
L
Linus Torvalds 已提交
970
	default:
971 972
		printk(KERN_INFO "sx: Invalid wordsize: %u\n",
			(unsigned int)CFLAG & CSIZE);
L
Linus Torvalds 已提交
973 974 975
		break;
	}

J
Jiri Slaby 已提交
976
	sx_write_channel_byte(port, hi_prtcl,
A
Alan Cox 已提交
977 978 979
			(I_IXON(port->gs.port.tty) ? SP_TXEN : 0) |
			(I_IXOFF(port->gs.port.tty) ? SP_RXEN : 0) |
			(I_IXANY(port->gs.port.tty) ? SP_TANY : 0) | SP_DCEN);
L
Linus Torvalds 已提交
980

J
Jiri Slaby 已提交
981
	sx_write_channel_byte(port, hi_break,
A
Alan Cox 已提交
982 983
			(I_IGNBRK(port->gs.port.tty) ? BR_IGN : 0 |
			I_BRKINT(port->gs.port.tty) ? BR_INT : 0));
L
Linus Torvalds 已提交
984

A
Alan Cox 已提交
985 986 987 988
	sx_write_channel_byte(port, hi_txon, START_CHAR(port->gs.port.tty));
	sx_write_channel_byte(port, hi_rxon, START_CHAR(port->gs.port.tty));
	sx_write_channel_byte(port, hi_txoff, STOP_CHAR(port->gs.port.tty));
	sx_write_channel_byte(port, hi_rxoff, STOP_CHAR(port->gs.port.tty));
L
Linus Torvalds 已提交
989 990 991 992

	sx_reconfigure_port(port);

	/* Tell line discipline whether we will do input cooking */
A
Alan Cox 已提交
993 994
	if (I_OTHER(port->gs.port.tty)) {
		clear_bit(TTY_HW_COOK_IN, &port->gs.port.tty->flags);
L
Linus Torvalds 已提交
995
	} else {
A
Alan Cox 已提交
996
		set_bit(TTY_HW_COOK_IN, &port->gs.port.tty->flags);
L
Linus Torvalds 已提交
997
	}
J
Jiri Slaby 已提交
998
	sx_dprintk(SX_DEBUG_TERMIOS, "iflags: %x(%d) ",
A
Alan Cox 已提交
999 1000
			(unsigned int)port->gs.port.tty->termios->c_iflag,
			I_OTHER(port->gs.port.tty));
L
Linus Torvalds 已提交
1001 1002 1003 1004 1005 1006

/* Tell line discipline whether we will do output cooking.
 * If OPOST is set and no other output flags are set then we can do output
 * processing.  Even if only *one* other flag in the O_OTHER group is set
 * we do cooking in software.
 */
A
Alan Cox 已提交
1007 1008
	if (O_OPOST(port->gs.port.tty) && !O_OTHER(port->gs.port.tty)) {
		set_bit(TTY_HW_COOK_OUT, &port->gs.port.tty->flags);
L
Linus Torvalds 已提交
1009
	} else {
A
Alan Cox 已提交
1010
		clear_bit(TTY_HW_COOK_OUT, &port->gs.port.tty->flags);
L
Linus Torvalds 已提交
1011
	}
J
Jiri Slaby 已提交
1012
	sx_dprintk(SX_DEBUG_TERMIOS, "oflags: %x(%d)\n",
A
Alan Cox 已提交
1013 1014
			(unsigned int)port->gs.port.tty->termios->c_oflag,
			O_OTHER(port->gs.port.tty));
L
Linus Torvalds 已提交
1015
	/* port->c_dcd = sx_get_CD (port); */
J
Jiri Slaby 已提交
1016
	func_exit();
L
Linus Torvalds 已提交
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	return 0;
}

/* ********************************************************************** *
 *                   the interrupt related routines                       *
 * ********************************************************************** */

/* Note:
   Other drivers use the macro "MIN" to calculate how much to copy.
   This has the disadvantage that it will evaluate parts twice. That's
   expensive when it's IO (and the compiler cannot optimize those away!).
   Moreover, I'm not sure that you're race-free. 

   I assign a value, and then only allow the value to decrease. This
   is always safe. This makes the code a few lines longer, and you
   know I'm dead against that, but I think it is required in this
   case.  */

J
Jiri Slaby 已提交
1035
static void sx_transmit_chars(struct sx_port *port)
L
Linus Torvalds 已提交
1036 1037 1038 1039 1040
{
	int c;
	int tx_ip;
	int txroom;

J
Jiri Slaby 已提交
1041 1042 1043
	func_enter2();
	sx_dprintk(SX_DEBUG_TRANSMIT, "Port %p: transmit %d chars\n",
			port, port->gs.xmit_cnt);
L
Linus Torvalds 已提交
1044

J
Jiri Slaby 已提交
1045
	if (test_and_set_bit(SX_PORT_TRANSMIT_LOCK, &port->locks)) {
L
Linus Torvalds 已提交
1046 1047 1048 1049 1050 1051
		return;
	}

	while (1) {
		c = port->gs.xmit_cnt;

J
Jiri Slaby 已提交
1052 1053
		sx_dprintk(SX_DEBUG_TRANSMIT, "Copying %d ", c);
		tx_ip = sx_read_channel_byte(port, hi_txipos);
L
Linus Torvalds 已提交
1054 1055 1056

		/* Took me 5 minutes to deduce this formula. 
		   Luckily it is literally in the manual in section 6.5.4.3.5 */
J
Jiri Slaby 已提交
1057 1058
		txroom = (sx_read_channel_byte(port, hi_txopos) - tx_ip - 1) &
				0xff;
L
Linus Torvalds 已提交
1059 1060 1061 1062

		/* Don't copy more bytes than there is room for in the buffer */
		if (c > txroom)
			c = txroom;
J
Jiri Slaby 已提交
1063
		sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%d) ", c, txroom);
L
Linus Torvalds 已提交
1064 1065

		/* Don't copy past the end of the hardware transmit buffer */
J
Jiri Slaby 已提交
1066
		if (c > 0x100 - tx_ip)
L
Linus Torvalds 已提交
1067 1068
			c = 0x100 - tx_ip;

J
Jiri Slaby 已提交
1069
		sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%d) ", c, 0x100 - tx_ip);
L
Linus Torvalds 已提交
1070 1071

		/* Don't copy pas the end of the source buffer */
J
Jiri Slaby 已提交
1072
		if (c > SERIAL_XMIT_SIZE - port->gs.xmit_tail)
L
Linus Torvalds 已提交
1073 1074
			c = SERIAL_XMIT_SIZE - port->gs.xmit_tail;

J
Jiri Slaby 已提交
1075 1076
		sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%ld) \n",
				c, SERIAL_XMIT_SIZE - port->gs.xmit_tail);
L
Linus Torvalds 已提交
1077

J
Jiri Slaby 已提交
1078 1079 1080 1081
		/* If for one reason or another, we can't copy more data, we're
		   done! */
		if (c == 0)
			break;
L
Linus Torvalds 已提交
1082

J
Jiri Slaby 已提交
1083 1084
		memcpy_toio(port->board->base + CHAN_OFFSET(port, hi_txbuf) +
			tx_ip, port->gs.xmit_buf + port->gs.xmit_tail, c);
L
Linus Torvalds 已提交
1085 1086

		/* Update the pointer in the card */
J
Jiri Slaby 已提交
1087
		sx_write_channel_byte(port, hi_txipos, (tx_ip + c) & 0xff);
L
Linus Torvalds 已提交
1088 1089

		/* Update the kernel buffer end */
J
Jiri Slaby 已提交
1090 1091
		port->gs.xmit_tail = (port->gs.xmit_tail + c) &
				(SERIAL_XMIT_SIZE - 1);
L
Linus Torvalds 已提交
1092 1093

		/* This one last. (this is essential)
J
Jiri Slaby 已提交
1094 1095
		   It would allow others to start putting more data into the
		   buffer! */
L
Linus Torvalds 已提交
1096 1097 1098 1099
		port->gs.xmit_cnt -= c;
	}

	if (port->gs.xmit_cnt == 0) {
J
Jiri Slaby 已提交
1100
		sx_disable_tx_interrupts(port);
L
Linus Torvalds 已提交
1101 1102
	}

A
Alan Cox 已提交
1103 1104
	if ((port->gs.xmit_cnt <= port->gs.wakeup_chars) && port->gs.port.tty) {
		tty_wakeup(port->gs.port.tty);
J
Jiri Slaby 已提交
1105 1106
		sx_dprintk(SX_DEBUG_TRANSMIT, "Waking up.... ldisc (%d)....\n",
				port->gs.wakeup_chars);
L
Linus Torvalds 已提交
1107 1108
	}

J
Jiri Slaby 已提交
1109 1110
	clear_bit(SX_PORT_TRANSMIT_LOCK, &port->locks);
	func_exit();
L
Linus Torvalds 已提交
1111 1112 1113 1114 1115 1116 1117
}

/* Note the symmetry between receiving chars and transmitting them!
   Note: The kernel should have implemented both a receive buffer and
   a transmit buffer. */

/* Inlined: Called only once. Remove the inline when you add another call */
J
Jiri Slaby 已提交
1118
static inline void sx_receive_chars(struct sx_port *port)
L
Linus Torvalds 已提交
1119 1120 1121 1122
{
	int c;
	int rx_op;
	struct tty_struct *tty;
J
Jiri Slaby 已提交
1123
	int copied = 0;
A
Alan Cox 已提交
1124
	unsigned char *rp;
L
Linus Torvalds 已提交
1125

J
Jiri Slaby 已提交
1126
	func_enter2();
A
Alan Cox 已提交
1127
	tty = port->gs.port.tty;
L
Linus Torvalds 已提交
1128
	while (1) {
J
Jiri Slaby 已提交
1129 1130
		rx_op = sx_read_channel_byte(port, hi_rxopos);
		c = (sx_read_channel_byte(port, hi_rxipos) - rx_op) & 0xff;
L
Linus Torvalds 已提交
1131

J
Jiri Slaby 已提交
1132
		sx_dprintk(SX_DEBUG_RECEIVE, "rxop=%d, c = %d.\n", rx_op, c);
L
Linus Torvalds 已提交
1133

1134
		/* Don't copy past the end of the hardware receive buffer */
J
Jiri Slaby 已提交
1135 1136
		if (rx_op + c > 0x100)
			c = 0x100 - rx_op;
1137

J
Jiri Slaby 已提交
1138
		sx_dprintk(SX_DEBUG_RECEIVE, "c = %d.\n", c);
1139

L
Linus Torvalds 已提交
1140
		/* Don't copy more bytes than there is room for in the buffer */
A
Alan Cox 已提交
1141 1142

		c = tty_prepare_flip_string(tty, &rp, c);
L
Linus Torvalds 已提交
1143

J
Jiri Slaby 已提交
1144
		sx_dprintk(SX_DEBUG_RECEIVE, "c = %d.\n", c);
L
Linus Torvalds 已提交
1145 1146

		/* If for one reason or another, we can't copy more data, we're done! */
J
Jiri Slaby 已提交
1147 1148
		if (c == 0)
			break;
L
Linus Torvalds 已提交
1149

J
Jiri Slaby 已提交
1150 1151 1152 1153 1154 1155
		sx_dprintk(SX_DEBUG_RECEIVE, "Copying over %d chars. First is "
				"%d at %lx\n", c, read_sx_byte(port->board,
					CHAN_OFFSET(port, hi_rxbuf) + rx_op),
				CHAN_OFFSET(port, hi_rxbuf));
		memcpy_fromio(rp, port->board->base +
				CHAN_OFFSET(port, hi_rxbuf) + rx_op, c);
L
Linus Torvalds 已提交
1156 1157

		/* This one last. ( Not essential.)
J
Jiri Slaby 已提交
1158 1159
		   It allows the card to start putting more data into the
		   buffer!
L
Linus Torvalds 已提交
1160
		   Update the pointer in the card */
J
Jiri Slaby 已提交
1161
		sx_write_channel_byte(port, hi_rxopos, (rx_op + c) & 0xff);
L
Linus Torvalds 已提交
1162 1163 1164 1165 1166 1167

		copied += c;
	}
	if (copied) {
		struct timeval tv;

J
Jiri Slaby 已提交
1168 1169 1170 1171 1172
		do_gettimeofday(&tv);
		sx_dprintk(SX_DEBUG_RECEIVE, "pushing flipq port %d (%3d "
				"chars): %d.%06d  (%d/%d)\n", port->line,
				copied, (int)(tv.tv_sec % 60), (int)tv.tv_usec,
				tty->raw, tty->real_raw);
L
Linus Torvalds 已提交
1173

J
Jiri Slaby 已提交
1174 1175 1176
		/* Tell the rest of the system the news. Great news. New
		   characters! */
		tty_flip_buffer_push(tty);
L
Linus Torvalds 已提交
1177 1178 1179
		/*    tty_schedule_flip (tty); */
	}

J
Jiri Slaby 已提交
1180
	func_exit();
L
Linus Torvalds 已提交
1181 1182 1183 1184
}

/* Inlined: it is called only once. Remove the inline if you add another 
   call */
J
Jiri Slaby 已提交
1185
static inline void sx_check_modem_signals(struct sx_port *port)
L
Linus Torvalds 已提交
1186 1187 1188 1189
{
	int hi_state;
	int c_dcd;

J
Jiri Slaby 已提交
1190 1191
	hi_state = sx_read_channel_byte(port, hi_state);
	sx_dprintk(SX_DEBUG_MODEMSIGNALS, "Checking modem signals (%d/%d)\n",
1192
			port->c_dcd, tty_port_carrier_raised(&port->gs.port));
L
Linus Torvalds 已提交
1193 1194 1195

	if (hi_state & ST_BREAK) {
		hi_state &= ~ST_BREAK;
J
Jiri Slaby 已提交
1196 1197 1198
		sx_dprintk(SX_DEBUG_MODEMSIGNALS, "got a break.\n");
		sx_write_channel_byte(port, hi_state, hi_state);
		gs_got_break(&port->gs);
L
Linus Torvalds 已提交
1199 1200 1201
	}
	if (hi_state & ST_DCD) {
		hi_state &= ~ST_DCD;
J
Jiri Slaby 已提交
1202 1203
		sx_dprintk(SX_DEBUG_MODEMSIGNALS, "got a DCD change.\n");
		sx_write_channel_byte(port, hi_state, hi_state);
1204
		c_dcd = tty_port_carrier_raised(&port->gs.port);
J
Jiri Slaby 已提交
1205
		sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD is now %d\n", c_dcd);
L
Linus Torvalds 已提交
1206 1207
		if (c_dcd != port->c_dcd) {
			port->c_dcd = c_dcd;
1208
			if (tty_port_carrier_raised(&port->gs.port)) {
L
Linus Torvalds 已提交
1209
				/* DCD went UP */
J
Jiri Slaby 已提交
1210 1211
				if ((sx_read_channel_byte(port, hi_hstat) !=
						HS_IDLE_CLOSED) &&
A
Alan Cox 已提交
1212
						!(port->gs.port.tty->termios->
J
Jiri Slaby 已提交
1213 1214 1215 1216
							c_cflag & CLOCAL)) {
					/* Are we blocking in open? */
					sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
						"active, unblocking open\n");
A
Alan Cox 已提交
1217
					wake_up_interruptible(&port->gs.port.
J
Jiri Slaby 已提交
1218
							open_wait);
L
Linus Torvalds 已提交
1219
				} else {
J
Jiri Slaby 已提交
1220 1221
					sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
						"raised. Ignoring.\n");
L
Linus Torvalds 已提交
1222 1223 1224
				}
			} else {
				/* DCD went down! */
A
Alan Cox 已提交
1225
				if (!(port->gs.port.tty->termios->c_cflag & CLOCAL)){
J
Jiri Slaby 已提交
1226 1227
					sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
						"dropped. hanging up....\n");
A
Alan Cox 已提交
1228
					tty_hangup(port->gs.port.tty);
L
Linus Torvalds 已提交
1229
				} else {
J
Jiri Slaby 已提交
1230 1231
					sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
						"dropped. ignoring.\n");
L
Linus Torvalds 已提交
1232 1233 1234
				}
			}
		} else {
J
Jiri Slaby 已提交
1235 1236
			sx_dprintk(SX_DEBUG_MODEMSIGNALS, "Hmmm. card told us "
				"DCD changed, but it didn't.\n");
L
Linus Torvalds 已提交
1237 1238 1239 1240 1241 1242 1243 1244
		}
	}
}

/* This is what an interrupt routine should look like. 
 * Small, elegant, clear.
 */

J
Jiri Slaby 已提交
1245
static irqreturn_t sx_interrupt(int irq, void *ptr)
L
Linus Torvalds 已提交
1246 1247 1248 1249 1250
{
	struct sx_board *board = ptr;
	struct sx_port *port;
	int i;

J
Jiri Slaby 已提交
1251 1252 1253
	func_enter();
	sx_dprintk(SX_DEBUG_FLOW, "sx: enter sx_interrupt (%d/%d)\n", irq,
			board->irq);
L
Linus Torvalds 已提交
1254 1255 1256 1257 1258

	/* AAargh! The order in which to do these things is essential and
	   not trivial. 

	   - Rate limit goes before "recursive". Otherwise a series of
J
Jiri Slaby 已提交
1259
	   recursive calls will hang the machine in the interrupt routine.
L
Linus Torvalds 已提交
1260 1261

	   - hardware twiddling goes before "recursive". Otherwise when we
J
Jiri Slaby 已提交
1262 1263 1264
	   poll the card, and a recursive interrupt happens, we won't
	   ack the card, so it might keep on interrupting us. (especially
	   level sensitive interrupt systems like PCI).
L
Linus Torvalds 已提交
1265 1266

	   - Rate limit goes before hardware twiddling. Otherwise we won't
J
Jiri Slaby 已提交
1267
	   catch a card that has gone bonkers.
L
Linus Torvalds 已提交
1268 1269

	   - The "initialized" test goes after the hardware twiddling. Otherwise
J
Jiri Slaby 已提交
1270
	   the card will stick us in the interrupt routine again.
L
Linus Torvalds 已提交
1271 1272

	   - The initialized test goes before recursive. 
J
Jiri Slaby 已提交
1273
	 */
L
Linus Torvalds 已提交
1274 1275 1276

#ifdef IRQ_RATE_LIMIT
	/* Aaargh! I'm ashamed. This costs more lines-of-code than the
J
Jiri Slaby 已提交
1277 1278
	   actual interrupt routine!. (Well, used to when I wrote that
	   comment) */
L
Linus Torvalds 已提交
1279 1280
	{
		static int lastjif;
J
Jiri Slaby 已提交
1281
		static int nintr = 0;
L
Linus Torvalds 已提交
1282 1283 1284

		if (lastjif == jiffies) {
			if (++nintr > IRQ_RATE_LIMIT) {
J
Jiri Slaby 已提交
1285 1286 1287 1288
				free_irq(board->irq, board);
				printk(KERN_ERR "sx: Too many interrupts. "
						"Turning off interrupt %d.\n",
						board->irq);
L
Linus Torvalds 已提交
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
			}
		} else {
			lastjif = jiffies;
			nintr = 0;
		}
	}
#endif

	if (board->irq == irq) {
		/* Tell the card we've noticed the interrupt. */

J
Jiri Slaby 已提交
1300 1301 1302
		sx_write_board_word(board, cc_int_pending, 0);
		if (IS_SX_BOARD(board)) {
			write_sx_byte(board, SX_RESET_IRQ, 1);
L
Linus Torvalds 已提交
1303
		} else if (IS_EISA_BOARD(board)) {
J
Jiri Slaby 已提交
1304 1305
			inb(board->eisa_base + 0xc03);
			write_sx_word(board, 8, 0);
L
Linus Torvalds 已提交
1306
		} else {
J
Jiri Slaby 已提交
1307 1308 1309 1310
			write_sx_byte(board, SI2_ISA_INTCLEAR,
					SI2_ISA_INTCLEAR_CLEAR);
			write_sx_byte(board, SI2_ISA_INTCLEAR,
					SI2_ISA_INTCLEAR_SET);
L
Linus Torvalds 已提交
1311 1312 1313 1314 1315 1316 1317 1318
		}
	}

	if (!sx_initialized)
		return IRQ_HANDLED;
	if (!(board->flags & SX_BOARD_INITIALIZED))
		return IRQ_HANDLED;

J
Jiri Slaby 已提交
1319 1320
	if (test_and_set_bit(SX_BOARD_INTR_LOCK, &board->locks)) {
		printk(KERN_ERR "Recursive interrupt! (%d)\n", board->irq);
L
Linus Torvalds 已提交
1321 1322 1323
		return IRQ_HANDLED;
	}

J
Jiri Slaby 已提交
1324
	for (i = 0; i < board->nports; i++) {
L
Linus Torvalds 已提交
1325
		port = &board->ports[i];
A
Alan Cox 已提交
1326
		if (port->gs.port.flags & GS_ACTIVE) {
J
Jiri Slaby 已提交
1327 1328 1329 1330
			if (sx_read_channel_byte(port, hi_state)) {
				sx_dprintk(SX_DEBUG_INTERRUPTS, "Port %d: "
						"modem signal change?... \n",i);
				sx_check_modem_signals(port);
L
Linus Torvalds 已提交
1331 1332
			}
			if (port->gs.xmit_cnt) {
J
Jiri Slaby 已提交
1333
				sx_transmit_chars(port);
L
Linus Torvalds 已提交
1334
			}
A
Alan Cox 已提交
1335
			if (!(port->gs.port.flags & SX_RX_THROTTLE)) {
J
Jiri Slaby 已提交
1336
				sx_receive_chars(port);
L
Linus Torvalds 已提交
1337 1338 1339 1340
			}
		}
	}

J
Jiri Slaby 已提交
1341
	clear_bit(SX_BOARD_INTR_LOCK, &board->locks);
L
Linus Torvalds 已提交
1342

J
Jiri Slaby 已提交
1343 1344 1345
	sx_dprintk(SX_DEBUG_FLOW, "sx: exit sx_interrupt (%d/%d)\n", irq,
			board->irq);
	func_exit();
L
Linus Torvalds 已提交
1346 1347 1348
	return IRQ_HANDLED;
}

J
Jiri Slaby 已提交
1349
static void sx_pollfunc(unsigned long data)
L
Linus Torvalds 已提交
1350
{
J
Jiri Slaby 已提交
1351
	struct sx_board *board = (struct sx_board *)data;
L
Linus Torvalds 已提交
1352

J
Jiri Slaby 已提交
1353
	func_enter();
L
Linus Torvalds 已提交
1354

J
Jiri Slaby 已提交
1355
	sx_interrupt(0, board);
L
Linus Torvalds 已提交
1356

1357
	mod_timer(&board->timer, jiffies + sx_poll);
J
Jiri Slaby 已提交
1358
	func_exit();
L
Linus Torvalds 已提交
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
}

/* ********************************************************************** *
 *                Here are the routines that actually                     *
 *              interface with the generic_serial driver                  *
 * ********************************************************************** */

/* Ehhm. I don't know how to fiddle with interrupts on the SX card. --REW */
/* Hmm. Ok I figured it out. You don't.  */

J
Jiri Slaby 已提交
1369
static void sx_disable_tx_interrupts(void *ptr)
L
Linus Torvalds 已提交
1370
{
J
Jiri Slaby 已提交
1371
	struct sx_port *port = ptr;
L
Linus Torvalds 已提交
1372 1373
	func_enter2();

A
Alan Cox 已提交
1374
	port->gs.port.flags &= ~GS_TX_INTEN;
L
Linus Torvalds 已提交
1375 1376 1377 1378

	func_exit();
}

J
Jiri Slaby 已提交
1379
static void sx_enable_tx_interrupts(void *ptr)
L
Linus Torvalds 已提交
1380
{
J
Jiri Slaby 已提交
1381
	struct sx_port *port = ptr;
L
Linus Torvalds 已提交
1382 1383 1384 1385
	int data_in_buffer;
	func_enter2();

	/* First transmit the characters that we're supposed to */
J
Jiri Slaby 已提交
1386
	sx_transmit_chars(port);
L
Linus Torvalds 已提交
1387 1388 1389

	/* The sx card will never interrupt us if we don't fill the buffer
	   past 25%. So we keep considering interrupts off if that's the case. */
J
Jiri Slaby 已提交
1390 1391
	data_in_buffer = (sx_read_channel_byte(port, hi_txipos) -
			  sx_read_channel_byte(port, hi_txopos)) & 0xff;
L
Linus Torvalds 已提交
1392 1393

	/* XXX Must be "HIGH_WATER" for SI card according to doc. */
J
Jiri Slaby 已提交
1394
	if (data_in_buffer < LOW_WATER)
A
Alan Cox 已提交
1395
		port->gs.port.flags &= ~GS_TX_INTEN;
L
Linus Torvalds 已提交
1396 1397 1398 1399

	func_exit();
}

J
Jiri Slaby 已提交
1400
static void sx_disable_rx_interrupts(void *ptr)
L
Linus Torvalds 已提交
1401 1402 1403 1404 1405 1406 1407
{
	/*  struct sx_port *port = ptr; */
	func_enter();

	func_exit();
}

J
Jiri Slaby 已提交
1408
static void sx_enable_rx_interrupts(void *ptr)
L
Linus Torvalds 已提交
1409 1410 1411 1412 1413 1414 1415 1416
{
	/*  struct sx_port *port = ptr; */
	func_enter();

	func_exit();
}

/* Jeez. Isn't this simple? */
1417
static int sx_carrier_raised(struct tty_port *port)
L
Linus Torvalds 已提交
1418
{
1419 1420
	struct sx_port *sp = container_of(port, struct sx_port, gs.port);
	return ((sx_read_channel_byte(sp, hi_ip) & IP_DCD) != 0);
L
Linus Torvalds 已提交
1421 1422 1423
}

/* Jeez. Isn't this simple? */
J
Jiri Slaby 已提交
1424
static int sx_chars_in_buffer(void *ptr)
L
Linus Torvalds 已提交
1425 1426 1427 1428 1429
{
	struct sx_port *port = ptr;
	func_enter2();

	func_exit();
J
Jiri Slaby 已提交
1430 1431
	return ((sx_read_channel_byte(port, hi_txipos) -
		 sx_read_channel_byte(port, hi_txopos)) & 0xff);
L
Linus Torvalds 已提交
1432 1433
}

J
Jiri Slaby 已提交
1434
static void sx_shutdown_port(void *ptr)
L
Linus Torvalds 已提交
1435
{
J
Jiri Slaby 已提交
1436
	struct sx_port *port = ptr;
L
Linus Torvalds 已提交
1437 1438 1439

	func_enter();

A
Alan Cox 已提交
1440 1441
	port->gs.port.flags &= ~GS_ACTIVE;
	if (port->gs.port.tty && (port->gs.port.tty->termios->c_cflag & HUPCL)) {
J
Jiri Slaby 已提交
1442
		sx_setsignals(port, 0, 0);
L
Linus Torvalds 已提交
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
		sx_reconfigure_port(port);
	}

	func_exit();
}

/* ********************************************************************** *
 *                Here are the routines that actually                     *
 *               interface with the rest of the system                    *
 * ********************************************************************** */

J
Jiri Slaby 已提交
1454
static int sx_open(struct tty_struct *tty, struct file *filp)
L
Linus Torvalds 已提交
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
{
	struct sx_port *port;
	int retval, line;
	unsigned long flags;

	func_enter();

	if (!sx_initialized) {
		return -EIO;
	}

	line = tty->index;
J
Jiri Slaby 已提交
1467
	sx_dprintk(SX_DEBUG_OPEN, "%d: opening line %d. tty=%p ctty=%p, "
1468
			"np=%d)\n", task_pid_nr(current), line, tty,
J
Jiri Slaby 已提交
1469
			current->signal->tty, sx_nports);
L
Linus Torvalds 已提交
1470 1471 1472 1473

	if ((line < 0) || (line >= SX_NPORTS) || (line >= sx_nports))
		return -ENODEV;

J
Jiri Slaby 已提交
1474
	port = &sx_ports[line];
L
Linus Torvalds 已提交
1475
	port->c_dcd = 0; /* Make sure that the first interrupt doesn't detect a
J
Jiri Slaby 已提交
1476
			    1 -> 0 transition. */
L
Linus Torvalds 已提交
1477

J
Jiri Slaby 已提交
1478
	sx_dprintk(SX_DEBUG_OPEN, "port = %p c_dcd = %d\n", port, port->c_dcd);
L
Linus Torvalds 已提交
1479 1480 1481 1482

	spin_lock_irqsave(&port->gs.driver_lock, flags);

	tty->driver_data = port;
A
Alan Cox 已提交
1483 1484
	port->gs.port.tty = tty;
	port->gs.port.count++;
L
Linus Torvalds 已提交
1485 1486
	spin_unlock_irqrestore(&port->gs.driver_lock, flags);

J
Jiri Slaby 已提交
1487
	sx_dprintk(SX_DEBUG_OPEN, "starting port\n");
L
Linus Torvalds 已提交
1488 1489 1490 1491 1492

	/*
	 * Start up serial port
	 */
	retval = gs_init_port(&port->gs);
J
Jiri Slaby 已提交
1493
	sx_dprintk(SX_DEBUG_OPEN, "done gs_init\n");
L
Linus Torvalds 已提交
1494
	if (retval) {
A
Alan Cox 已提交
1495
		port->gs.port.count--;
L
Linus Torvalds 已提交
1496 1497 1498
		return retval;
	}

A
Alan Cox 已提交
1499 1500
	port->gs.port.flags |= GS_ACTIVE;
	if (port->gs.port.count <= 1)
J
Jiri Slaby 已提交
1501
		sx_setsignals(port, 1, 1);
L
Linus Torvalds 已提交
1502 1503 1504

#if 0
	if (sx_debug & SX_DEBUG_OPEN)
J
Jiri Slaby 已提交
1505
		my_hd(port, sizeof(*port));
L
Linus Torvalds 已提交
1506 1507
#else
	if (sx_debug & SX_DEBUG_OPEN)
J
Jiri Slaby 已提交
1508
		my_hd_io(port->board->base + port->ch_base, sizeof(*port));
L
Linus Torvalds 已提交
1509 1510
#endif

A
Alan Cox 已提交
1511
	if (port->gs.port.count <= 1) {
J
Jiri Slaby 已提交
1512 1513 1514
		if (sx_send_command(port, HS_LOPEN, -1, HS_IDLE_OPEN) != 1) {
			printk(KERN_ERR "sx: Card didn't respond to LOPEN "
					"command.\n");
L
Linus Torvalds 已提交
1515
			spin_lock_irqsave(&port->gs.driver_lock, flags);
A
Alan Cox 已提交
1516
			port->gs.port.count--;
L
Linus Torvalds 已提交
1517 1518 1519 1520 1521 1522
			spin_unlock_irqrestore(&port->gs.driver_lock, flags);
			return -EIO;
		}
	}

	retval = gs_block_til_ready(port, filp);
J
Jiri Slaby 已提交
1523
	sx_dprintk(SX_DEBUG_OPEN, "Block til ready returned %d. Count=%d\n",
A
Alan Cox 已提交
1524
			retval, port->gs.port.count);
L
Linus Torvalds 已提交
1525 1526

	if (retval) {
J
Jiri Slaby 已提交
1527
/*
A
Alan Cox 已提交
1528
 * Don't lower gs.port.count here because sx_close() will be called later
J
Jiri Slaby 已提交
1529
 */
L
Linus Torvalds 已提交
1530 1531 1532 1533 1534

		return retval;
	}
	/* tty->low_latency = 1; */

1535
	port->c_dcd = sx_carrier_raised(&port->gs.port);
J
Jiri Slaby 已提交
1536
	sx_dprintk(SX_DEBUG_OPEN, "at open: cd=%d\n", port->c_dcd);
L
Linus Torvalds 已提交
1537 1538 1539 1540 1541 1542

	func_exit();
	return 0;

}

J
Jiri Slaby 已提交
1543
static void sx_close(void *ptr)
L
Linus Torvalds 已提交
1544
{
J
Jiri Slaby 已提交
1545
	struct sx_port *port = ptr;
L
Linus Torvalds 已提交
1546
	/* Give the port 5 seconds to close down. */
J
Jiri Slaby 已提交
1547
	int to = 5 * HZ;
L
Linus Torvalds 已提交
1548

J
Jiri Slaby 已提交
1549
	func_enter();
L
Linus Torvalds 已提交
1550

J
Jiri Slaby 已提交
1551 1552 1553
	sx_setsignals(port, 0, 0);
	sx_reconfigure_port(port);
	sx_send_command(port, HS_CLOSE, 0, 0);
L
Linus Torvalds 已提交
1554

J
Jiri Slaby 已提交
1555
	while (to-- && (sx_read_channel_byte(port, hi_hstat) != HS_IDLE_CLOSED))
L
Linus Torvalds 已提交
1556 1557
		if (msleep_interruptible(10))
			break;
J
Jiri Slaby 已提交
1558 1559 1560 1561 1562
	if (sx_read_channel_byte(port, hi_hstat) != HS_IDLE_CLOSED) {
		if (sx_send_command(port, HS_FORCE_CLOSED, -1, HS_IDLE_CLOSED)
				!= 1) {
			printk(KERN_ERR "sx: sent the force_close command, but "
					"card didn't react\n");
L
Linus Torvalds 已提交
1563
		} else
J
Jiri Slaby 已提交
1564 1565
			sx_dprintk(SX_DEBUG_CLOSE, "sent the force_close "
					"command.\n");
L
Linus Torvalds 已提交
1566 1567
	}

J
Jiri Slaby 已提交
1568
	sx_dprintk(SX_DEBUG_CLOSE, "waited %d jiffies for close. count=%d\n",
A
Alan Cox 已提交
1569
			5 * HZ - to - 1, port->gs.port.count);
L
Linus Torvalds 已提交
1570

A
Alan Cox 已提交
1571
	if (port->gs.port.count) {
J
Jiri Slaby 已提交
1572
		sx_dprintk(SX_DEBUG_CLOSE, "WARNING port count:%d\n",
A
Alan Cox 已提交
1573
				port->gs.port.count);
J
Jiri Slaby 已提交
1574
		/*printk("%s SETTING port count to zero: %p count: %d\n",
A
Alan Cox 已提交
1575 1576
				__func__, port, port->gs.port.count);
		port->gs.port.count = 0;*/
L
Linus Torvalds 已提交
1577 1578
	}

J
Jiri Slaby 已提交
1579
	func_exit();
L
Linus Torvalds 已提交
1580 1581 1582
}

/* This is relatively thorough. But then again it is only 20 lines. */
J
Jiri Slaby 已提交
1583 1584 1585 1586 1587 1588
#define MARCHUP		for (i = min; i < max; i++)
#define MARCHDOWN	for (i = max - 1; i >= min; i--)
#define W0		write_sx_byte(board, i, 0x55)
#define W1		write_sx_byte(board, i, 0xaa)
#define R0		if (read_sx_byte(board, i) != 0x55) return 1
#define R1		if (read_sx_byte(board, i) != 0xaa) return 1
L
Linus Torvalds 已提交
1589 1590 1591

/* This memtest takes a human-noticable time. You normally only do it
   once a boot, so I guess that it is worth it. */
J
Jiri Slaby 已提交
1592
static int do_memtest(struct sx_board *board, int min, int max)
L
Linus Torvalds 已提交
1593 1594 1595 1596 1597 1598 1599 1600
{
	int i;

	/* This is a marchb. Theoretically, marchb catches much more than
	   simpler tests. In practise, the longer test just catches more
	   intermittent errors. -- REW
	   (For the theory behind memory testing see: 
	   Testing Semiconductor Memories by A.J. van de Goor.) */
J
Jiri Slaby 已提交
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
	MARCHUP {
		W0;
	}
	MARCHUP {
		R0;
		W1;
		R1;
		W0;
		R0;
		W1;
	}
	MARCHUP {
		R1;
		W0;
		W1;
	}
	MARCHDOWN {
		R1;
		W0;
		W1;
		W0;
	}
	MARCHDOWN {
		R0;
		W1;
		W0;
	}
L
Linus Torvalds 已提交
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638

	return 0;
}

#undef MARCHUP
#undef MARCHDOWN
#undef W0
#undef W1
#undef R0
#undef R1

J
Jiri Slaby 已提交
1639 1640 1641 1642 1643 1644
#define MARCHUP		for (i = min; i < max; i += 2)
#define MARCHDOWN	for (i = max - 1; i >= min; i -= 2)
#define W0		write_sx_word(board, i, 0x55aa)
#define W1		write_sx_word(board, i, 0xaa55)
#define R0		if (read_sx_word(board, i) != 0x55aa) return 1
#define R1		if (read_sx_word(board, i) != 0xaa55) return 1
L
Linus Torvalds 已提交
1645 1646 1647 1648

#if 0
/* This memtest takes a human-noticable time. You normally only do it
   once a boot, so I guess that it is worth it. */
J
Jiri Slaby 已提交
1649
static int do_memtest_w(struct sx_board *board, int min, int max)
L
Linus Torvalds 已提交
1650 1651 1652
{
	int i;

J
Jiri Slaby 已提交
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
	MARCHUP {
		W0;
	}
	MARCHUP {
		R0;
		W1;
		R1;
		W0;
		R0;
		W1;
	}
	MARCHUP {
		R1;
		W0;
		W1;
	}
	MARCHDOWN {
		R1;
		W0;
		W1;
		W0;
	}
	MARCHDOWN {
		R0;
		W1;
		W0;
	}
L
Linus Torvalds 已提交
1680 1681 1682 1683 1684

	return 0;
}
#endif

1685 1686
static long sx_fw_ioctl(struct file *filp, unsigned int cmd,
							unsigned long arg)
L
Linus Torvalds 已提交
1687
{
1688
	long rc = 0;
L
Linus Torvalds 已提交
1689 1690 1691 1692 1693 1694 1695 1696 1697
	int __user *descr = (int __user *)arg;
	int i;
	static struct sx_board *board = NULL;
	int nbytes, offset;
	unsigned long data;
	char *tmp;

	func_enter();

1698
	if (!capable(CAP_SYS_RAWIO))
L
Linus Torvalds 已提交
1699
		return -EPERM;
1700 1701

	lock_kernel();
L
Linus Torvalds 已提交
1702

J
Jiri Slaby 已提交
1703
	sx_dprintk(SX_DEBUG_FIRMWARE, "IOCTL %x: %lx\n", cmd, arg);
L
Linus Torvalds 已提交
1704

J
Jiri Slaby 已提交
1705 1706
	if (!board)
		board = &boards[0];
L
Linus Torvalds 已提交
1707
	if (board->flags & SX_BOARD_PRESENT) {
J
Jiri Slaby 已提交
1708 1709
		sx_dprintk(SX_DEBUG_FIRMWARE, "Board present! (%x)\n",
				board->flags);
L
Linus Torvalds 已提交
1710
	} else {
J
Jiri Slaby 已提交
1711 1712 1713 1714 1715
		sx_dprintk(SX_DEBUG_FIRMWARE, "Board not present! (%x) all:",
				board->flags);
		for (i = 0; i < SX_NBOARDS; i++)
			sx_dprintk(SX_DEBUG_FIRMWARE, "<%x> ", boards[i].flags);
		sx_dprintk(SX_DEBUG_FIRMWARE, "\n");
1716 1717
		rc = -EIO;
		goto out;
L
Linus Torvalds 已提交
1718 1719 1720 1721
	}

	switch (cmd) {
	case SXIO_SET_BOARD:
J
Jiri Slaby 已提交
1722
		sx_dprintk(SX_DEBUG_FIRMWARE, "set board to %ld\n", arg);
1723
		rc = -EIO;
J
Jiri Slaby 已提交
1724
		if (arg >= SX_NBOARDS)
1725
			break;
J
Jiri Slaby 已提交
1726 1727
		sx_dprintk(SX_DEBUG_FIRMWARE, "not out of range\n");
		if (!(boards[arg].flags & SX_BOARD_PRESENT))
1728
			break;
J
Jiri Slaby 已提交
1729
		sx_dprintk(SX_DEBUG_FIRMWARE, ".. and present!\n");
L
Linus Torvalds 已提交
1730
		board = &boards[arg];
1731 1732
		rc = 0;
		/* FIXME: And this does ... nothing?? */
L
Linus Torvalds 已提交
1733 1734
		break;
	case SXIO_GET_TYPE:
J
Jiri Slaby 已提交
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
		rc = -ENOENT;	/* If we manage to miss one, return error. */
		if (IS_SX_BOARD(board))
			rc = SX_TYPE_SX;
		if (IS_CF_BOARD(board))
			rc = SX_TYPE_CF;
		if (IS_SI_BOARD(board))
			rc = SX_TYPE_SI;
		if (IS_SI1_BOARD(board))
			rc = SX_TYPE_SI;
		if (IS_EISA_BOARD(board))
			rc = SX_TYPE_SI;
1746
		sx_dprintk(SX_DEBUG_FIRMWARE, "returning type= %ld\n", rc);
L
Linus Torvalds 已提交
1747 1748
		break;
	case SXIO_DO_RAMTEST:
J
Jiri Slaby 已提交
1749
		if (sx_initialized)	/* Already initialized: better not ramtest the board.  */
1750 1751
			rc = -EPERM;
			break;
J
Jiri Slaby 已提交
1752 1753 1754 1755 1756
		if (IS_SX_BOARD(board)) {
			rc = do_memtest(board, 0, 0x7000);
			if (!rc)
				rc = do_memtest(board, 0, 0x7000);
			/*if (!rc) rc = do_memtest_w (board, 0, 0x7000); */
L
Linus Torvalds 已提交
1757
		} else {
J
Jiri Slaby 已提交
1758
			rc = do_memtest(board, 0, 0x7ff8);
L
Linus Torvalds 已提交
1759 1760
			/* if (!rc) rc = do_memtest_w (board, 0, 0x7ff8); */
		}
1761 1762
		sx_dprintk(SX_DEBUG_FIRMWARE,
				"returning memtest result= %ld\n", rc);
L
Linus Torvalds 已提交
1763 1764
		break;
	case SXIO_DOWNLOAD:
1765 1766 1767 1768 1769 1770 1771 1772
		if (sx_initialized) {/* Already initialized */
			rc = -EEXIST;
			break;
		}
		if (!sx_reset(board)) {
			rc = -EIO;
			break;
		}
J
Jiri Slaby 已提交
1773 1774 1775
		sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");

		tmp = kmalloc(SX_CHUNK_SIZE, GFP_USER);
1776 1777 1778 1779 1780
		if (!tmp) {
			rc = -ENOMEM;
			break;
		}
		/* FIXME: check returns */
J
Jiri Slaby 已提交
1781 1782 1783
		get_user(nbytes, descr++);
		get_user(offset, descr++);
		get_user(data, descr++);
L
Linus Torvalds 已提交
1784
		while (nbytes && data) {
J
Jiri Slaby 已提交
1785 1786 1787 1788 1789
			for (i = 0; i < nbytes; i += SX_CHUNK_SIZE) {
				if (copy_from_user(tmp, (char __user *)data + i,
						(i + SX_CHUNK_SIZE > nbytes) ?
						nbytes - i : SX_CHUNK_SIZE)) {
					kfree(tmp);
1790 1791
					rc = -EFAULT;
					break;
L
Linus Torvalds 已提交
1792
				}
J
Jiri Slaby 已提交
1793 1794 1795
				memcpy_toio(board->base2 + offset + i, tmp,
						(i + SX_CHUNK_SIZE > nbytes) ?
						nbytes - i : SX_CHUNK_SIZE);
L
Linus Torvalds 已提交
1796 1797
			}

J
Jiri Slaby 已提交
1798 1799 1800
			get_user(nbytes, descr++);
			get_user(offset, descr++);
			get_user(data, descr++);
L
Linus Torvalds 已提交
1801
		}
J
Jiri Slaby 已提交
1802 1803
		kfree(tmp);
		sx_nports += sx_init_board(board);
L
Linus Torvalds 已提交
1804 1805 1806
		rc = sx_nports;
		break;
	case SXIO_INIT:
1807 1808 1809 1810
		if (sx_initialized) {	/* Already initialized */
			rc = -EEXIST;
			break;
		}
L
Linus Torvalds 已提交
1811
		/* This is not allowed until all boards are initialized... */
J
Jiri Slaby 已提交
1812 1813
		for (i = 0; i < SX_NBOARDS; i++) {
			if ((boards[i].flags & SX_BOARD_PRESENT) &&
1814 1815 1816 1817
				!(boards[i].flags & SX_BOARD_INITIALIZED)) {
				rc = -EIO;
				break;
			}
L
Linus Torvalds 已提交
1818
		}
J
Jiri Slaby 已提交
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828
		for (i = 0; i < SX_NBOARDS; i++)
			if (!(boards[i].flags & SX_BOARD_PRESENT))
				break;

		sx_dprintk(SX_DEBUG_FIRMWARE, "initing portstructs, %d boards, "
				"%d channels, first board: %d ports\n",
				i, sx_nports, boards[0].nports);
		rc = sx_init_portstructs(i, sx_nports);
		sx_init_drivers();
		if (rc >= 0)
L
Linus Torvalds 已提交
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
			sx_initialized++;
		break;
	case SXIO_SETDEBUG:
		sx_debug = arg;
		break;
	case SXIO_GETDEBUG:
		rc = sx_debug;
		break;
	case SXIO_GETGSDEBUG:
	case SXIO_SETGSDEBUG:
		rc = -EINVAL;
		break;
	case SXIO_GETNPORTS:
		rc = sx_nports;
		break;
	default:
1845
		rc = -ENOTTY;
L
Linus Torvalds 已提交
1846 1847
		break;
	}
1848
out:
1849
	unlock_kernel();
J
Jiri Slaby 已提交
1850
	func_exit();
L
Linus Torvalds 已提交
1851 1852 1853
	return rc;
}

A
Alan Cox 已提交
1854
static int sx_break(struct tty_struct *tty, int flag)
L
Linus Torvalds 已提交
1855 1856 1857 1858
{
	struct sx_port *port = tty->driver_data;
	int rv;

J
Jiri Slaby 已提交
1859
	func_enter();
A
Alan Cox 已提交
1860
	lock_kernel();
L
Linus Torvalds 已提交
1861

J
Jiri Slaby 已提交
1862 1863 1864 1865 1866 1867 1868
	if (flag)
		rv = sx_send_command(port, HS_START, -1, HS_IDLE_BREAK);
	else
		rv = sx_send_command(port, HS_STOP, -1, HS_IDLE_OPEN);
	if (rv != 1)
		printk(KERN_ERR "sx: couldn't send break (%x).\n",
			read_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat)));
A
Alan Cox 已提交
1869
	unlock_kernel();
J
Jiri Slaby 已提交
1870
	func_exit();
A
Alan Cox 已提交
1871
	return 0;
L
Linus Torvalds 已提交
1872 1873 1874 1875 1876 1877 1878 1879 1880
}

static int sx_tiocmget(struct tty_struct *tty, struct file *file)
{
	struct sx_port *port = tty->driver_data;
	return sx_getsignals(port);
}

static int sx_tiocmset(struct tty_struct *tty, struct file *file,
J
Jiri Slaby 已提交
1881
		unsigned int set, unsigned int clear)
L
Linus Torvalds 已提交
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
{
	struct sx_port *port = tty->driver_data;
	int rts = -1, dtr = -1;

	if (set & TIOCM_RTS)
		rts = 1;
	if (set & TIOCM_DTR)
		dtr = 1;
	if (clear & TIOCM_RTS)
		rts = 0;
	if (clear & TIOCM_DTR)
		dtr = 0;

	sx_setsignals(port, dtr, rts);
	sx_reconfigure_port(port);
	return 0;
}

J
Jiri Slaby 已提交
1900 1901
static int sx_ioctl(struct tty_struct *tty, struct file *filp,
		unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
1902 1903 1904 1905 1906 1907 1908 1909
{
	int rc;
	struct sx_port *port = tty->driver_data;
	void __user *argp = (void __user *)arg;

	/* func_enter2(); */

	rc = 0;
A
Alan Cox 已提交
1910
	lock_kernel();
L
Linus Torvalds 已提交
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
	switch (cmd) {
	case TIOCGSERIAL:
		rc = gs_getserial(&port->gs, argp);
		break;
	case TIOCSSERIAL:
		rc = gs_setserial(&port->gs, argp);
		break;
	default:
		rc = -ENOIOCTLCMD;
		break;
	}
A
Alan Cox 已提交
1922
	unlock_kernel();
L
Linus Torvalds 已提交
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943

	/* func_exit(); */
	return rc;
}

/* The throttle/unthrottle scheme for the Specialix card is different
 * from other drivers and deserves some explanation. 
 * The Specialix hardware takes care of XON/XOFF
 * and CTS/RTS flow control itself.  This means that all we have to
 * do when signalled by the upper tty layer to throttle/unthrottle is
 * to make a note of it here.  When we come to read characters from the
 * rx buffers on the card (sx_receive_chars()) we look to see if the
 * upper layer can accept more (as noted here in sx_rx_throt[]). 
 * If it can't we simply don't remove chars from the cards buffer. 
 * When the tty layer can accept chars, we again note that here and when
 * sx_receive_chars() is called it will remove them from the cards buffer.
 * The card will notice that a ports buffer has drained below some low
 * water mark and will unflow control the line itself, using whatever
 * flow control scheme is in use for that port. -- Simon Allen
 */

J
Jiri Slaby 已提交
1944
static void sx_throttle(struct tty_struct *tty)
L
Linus Torvalds 已提交
1945
{
A
Alan Cox 已提交
1946
	struct sx_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1947 1948 1949 1950 1951

	func_enter2();
	/* If the port is using any type of input flow
	 * control then throttle the port.
	 */
J
Jiri Slaby 已提交
1952
	if ((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty))) {
A
Alan Cox 已提交
1953
		port->gs.port.flags |= SX_RX_THROTTLE;
L
Linus Torvalds 已提交
1954 1955 1956 1957
	}
	func_exit();
}

J
Jiri Slaby 已提交
1958
static void sx_unthrottle(struct tty_struct *tty)
L
Linus Torvalds 已提交
1959
{
A
Alan Cox 已提交
1960
	struct sx_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1961 1962 1963 1964 1965 1966

	func_enter2();
	/* Always unthrottle even if flow control is not enabled on
	 * this port in case we disabled flow control while the port
	 * was throttled
	 */
A
Alan Cox 已提交
1967
	port->gs.port.flags &= ~SX_RX_THROTTLE;
L
Linus Torvalds 已提交
1968 1969 1970 1971 1972 1973 1974 1975
	func_exit();
	return;
}

/* ********************************************************************** *
 *                    Here are the initialization routines.               *
 * ********************************************************************** */

J
Jiri Slaby 已提交
1976
static int sx_init_board(struct sx_board *board)
L
Linus Torvalds 已提交
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
{
	int addr;
	int chans;
	int type;

	func_enter();

	/* This is preceded by downloading the download code. */

	board->flags |= SX_BOARD_INITIALIZED;

J
Jiri Slaby 已提交
1988
	if (read_sx_byte(board, 0))
L
Linus Torvalds 已提交
1989
		/* CF boards may need this. */
J
Jiri Slaby 已提交
1990
		write_sx_byte(board, 0, 0);
L
Linus Torvalds 已提交
1991 1992 1993

	/* This resets the processor again, to make sure it didn't do any
	   foolish things while we were downloading the image */
J
Jiri Slaby 已提交
1994
	if (!sx_reset(board))
L
Linus Torvalds 已提交
1995 1996
		return 0;

J
Jiri Slaby 已提交
1997 1998 1999 2000
	sx_start_board(board);
	udelay(10);
	if (!sx_busy_wait_neq(board, 0, 0xff, 0)) {
		printk(KERN_ERR "sx: Ooops. Board won't initialize.\n");
L
Linus Torvalds 已提交
2001 2002 2003 2004 2005
		return 0;
	}

	/* Ok. So now the processor on the card is running. It gathered
	   some info for us... */
J
Jiri Slaby 已提交
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
	sx_dprintk(SX_DEBUG_INIT, "The sxcard structure:\n");
	if (sx_debug & SX_DEBUG_INIT)
		my_hd_io(board->base, 0x10);
	sx_dprintk(SX_DEBUG_INIT, "the first sx_module structure:\n");
	if (sx_debug & SX_DEBUG_INIT)
		my_hd_io(board->base + 0x80, 0x30);

	sx_dprintk(SX_DEBUG_INIT, "init_status: %x, %dk memory, firmware "
			"V%x.%02x,\n",
			read_sx_byte(board, 0), read_sx_byte(board, 1),
			read_sx_byte(board, 5), read_sx_byte(board, 4));

	if (read_sx_byte(board, 0) == 0xff) {
		printk(KERN_INFO "sx: No modules found. Sorry.\n");
L
Linus Torvalds 已提交
2020 2021 2022 2023 2024 2025 2026
		board->nports = 0;
		return 0;
	}

	chans = 0;

	if (IS_SX_BOARD(board)) {
J
Jiri Slaby 已提交
2027
		sx_write_board_word(board, cc_int_count, sx_maxints);
L
Linus Torvalds 已提交
2028 2029
	} else {
		if (sx_maxints)
J
Jiri Slaby 已提交
2030 2031
			sx_write_board_word(board, cc_int_count,
					SI_PROCESSOR_CLOCK / 8 / sx_maxints);
L
Linus Torvalds 已提交
2032 2033 2034
	}

	/* grab the first module type... */
J
Jiri Slaby 已提交
2035 2036 2037
	/* board->ta_type = mod_compat_type (read_sx_byte (board, 0x80 + 0x08)); */
	board->ta_type = mod_compat_type(sx_read_module_byte(board, 0x80,
				mc_chip));
L
Linus Torvalds 已提交
2038 2039

	/* XXX byteorder */
J
Jiri Slaby 已提交
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
	for (addr = 0x80; addr != 0; addr = read_sx_word(board, addr) & 0x7fff){
		type = sx_read_module_byte(board, addr, mc_chip);
		sx_dprintk(SX_DEBUG_INIT, "Module at %x: %d channels\n",
				addr, read_sx_byte(board, addr + 2));

		chans += sx_read_module_byte(board, addr, mc_type);

		sx_dprintk(SX_DEBUG_INIT, "module is an %s, which has %s/%s "
				"panels\n",
				mod_type_s(type),
				pan_type_s(sx_read_module_byte(board, addr,
						mc_mods) & 0xf),
				pan_type_s(sx_read_module_byte(board, addr,
						mc_mods) >> 4));

		sx_dprintk(SX_DEBUG_INIT, "CD1400 versions: %x/%x, ASIC "
			"version: %x\n",
			sx_read_module_byte(board, addr, mc_rev1),
			sx_read_module_byte(board, addr, mc_rev2),
			sx_read_module_byte(board, addr, mc_mtaasic_rev));
L
Linus Torvalds 已提交
2060 2061 2062 2063

		/* The following combinations are illegal: It should theoretically
		   work, but timing problems make the bus HANG. */

J
Jiri Slaby 已提交
2064 2065 2066 2067 2068
		if (mod_compat_type(type) != board->ta_type) {
			printk(KERN_ERR "sx: This is an invalid "
				"configuration.\nDon't mix TA/MTA/SXDC on the "
				"same hostadapter.\n");
			chans = 0;
L
Linus Torvalds 已提交
2069 2070
			break;
		}
J
Jiri Slaby 已提交
2071 2072 2073 2074 2075 2076 2077
		if ((IS_EISA_BOARD(board) ||
				IS_SI_BOARD(board)) &&
				(mod_compat_type(type) == 4)) {
			printk(KERN_ERR	"sx: This is an invalid "
				"configuration.\nDon't use SXDCs on an SI/XIO "
				"adapter.\n");
			chans = 0;
L
Linus Torvalds 已提交
2078 2079
			break;
		}
J
Jiri Slaby 已提交
2080
#if 0				/* Problem fixed: firmware 3.05 */
L
Linus Torvalds 已提交
2081 2082 2083
		if (IS_SX_BOARD(board) && (type == TA8)) {
			/* There are some issues with the firmware and the DCD/RTS
			   lines. It might work if you tie them together or something.
J
Jiri Slaby 已提交
2084
			   It might also work if you get a newer sx_firmware.   Therefore
L
Linus Torvalds 已提交
2085
			   this is just a warning. */
J
Jiri Slaby 已提交
2086 2087 2088
			printk(KERN_WARNING
			       "sx: The SX host doesn't work too well "
			       "with the TA8 adapters.\nSpecialix is working on it.\n");
L
Linus Torvalds 已提交
2089 2090 2091 2092 2093
		}
#endif
	}

	if (chans) {
J
Jiri Slaby 已提交
2094
		if (board->irq > 0) {
L
Linus Torvalds 已提交
2095
			/* fixed irq, probably PCI */
J
Jiri Slaby 已提交
2096 2097 2098 2099 2100 2101
			if (sx_irqmask & (1 << board->irq)) {	/* may we use this irq? */
				if (request_irq(board->irq, sx_interrupt,
						IRQF_SHARED | IRQF_DISABLED,
						"sx", board)) {
					printk(KERN_ERR "sx: Cannot allocate "
						"irq %d.\n", board->irq);
L
Linus Torvalds 已提交
2102 2103 2104 2105
					board->irq = 0;
				}
			} else
				board->irq = 0;
J
Jiri Slaby 已提交
2106
		} else if (board->irq < 0 && sx_irqmask) {
L
Linus Torvalds 已提交
2107 2108
			/* auto-allocate irq */
			int irqnr;
J
Jiri Slaby 已提交
2109 2110 2111 2112 2113 2114 2115
			int irqmask = sx_irqmask & (IS_SX_BOARD(board) ?
					SX_ISA_IRQ_MASK : SI2_ISA_IRQ_MASK);
			for (irqnr = 15; irqnr > 0; irqnr--)
				if (irqmask & (1 << irqnr))
					if (!request_irq(irqnr, sx_interrupt,
						IRQF_SHARED | IRQF_DISABLED,
						"sx", board))
L
Linus Torvalds 已提交
2116
						break;
J
Jiri Slaby 已提交
2117
			if (!irqnr)
L
Linus Torvalds 已提交
2118 2119 2120 2121 2122 2123 2124
				printk(KERN_ERR "sx: Cannot allocate IRQ.\n");
			board->irq = irqnr;
		} else
			board->irq = 0;

		if (board->irq) {
			/* Found a valid interrupt, start up interrupts! */
J
Jiri Slaby 已提交
2125 2126 2127
			sx_dprintk(SX_DEBUG_INIT, "Using irq %d.\n",
					board->irq);
			sx_start_interrupts(board);
L
Linus Torvalds 已提交
2128 2129 2130 2131 2132
			board->poll = sx_slowpoll;
			board->flags |= SX_IRQ_ALLOCATED;
		} else {
			/* no irq: setup board for polled operation */
			board->poll = sx_poll;
J
Jiri Slaby 已提交
2133 2134
			sx_dprintk(SX_DEBUG_INIT, "Using poll-interval %d.\n",
					board->poll);
L
Linus Torvalds 已提交
2135 2136
		}

J
Jiri Slaby 已提交
2137 2138
		/* The timer should be initialized anyway: That way we can
		   safely del_timer it when the module is unloaded. */
2139
		setup_timer(&board->timer, sx_pollfunc, (unsigned long)board);
L
Linus Torvalds 已提交
2140

2141 2142
		if (board->poll)
			mod_timer(&board->timer, jiffies + board->poll);
L
Linus Torvalds 已提交
2143 2144 2145 2146 2147
	} else {
		board->irq = 0;
	}

	board->nports = chans;
J
Jiri Slaby 已提交
2148
	sx_dprintk(SX_DEBUG_INIT, "returning %d ports.", board->nports);
L
Linus Torvalds 已提交
2149 2150 2151 2152 2153

	func_exit();
	return chans;
}

2154
static void __devinit printheader(void)
L
Linus Torvalds 已提交
2155 2156 2157 2158
{
	static int header_printed;

	if (!header_printed) {
J
Jiri Slaby 已提交
2159 2160 2161
		printk(KERN_INFO "Specialix SX driver "
			"(C) 1998/1999 R.E.Wolff@BitWizard.nl\n");
		printk(KERN_INFO "sx: version " __stringify(SX_VERSION) "\n");
L
Linus Torvalds 已提交
2162 2163 2164 2165
		header_printed = 1;
	}
}

J
Jiri Slaby 已提交
2166
static int __devinit probe_sx(struct sx_board *board)
L
Linus Torvalds 已提交
2167 2168 2169 2170 2171 2172 2173
{
	struct vpd_prom vpdp;
	char *p;
	int i;

	func_enter();

J
Jiri Slaby 已提交
2174 2175 2176
	if (!IS_CF_BOARD(board)) {
		sx_dprintk(SX_DEBUG_PROBE, "Going to verify vpd prom at %p.\n",
				board->base + SX_VPD_ROM);
L
Linus Torvalds 已提交
2177 2178 2179 2180

		if (sx_debug & SX_DEBUG_PROBE)
			my_hd_io(board->base + SX_VPD_ROM, 0x40);

J
Jiri Slaby 已提交
2181 2182 2183
		p = (char *)&vpdp;
		for (i = 0; i < sizeof(struct vpd_prom); i++)
			*p++ = read_sx_byte(board, SX_VPD_ROM + i * 2);
L
Linus Torvalds 已提交
2184 2185

		if (sx_debug & SX_DEBUG_PROBE)
J
Jiri Slaby 已提交
2186
			my_hd(&vpdp, 0x20);
L
Linus Torvalds 已提交
2187

J
Jiri Slaby 已提交
2188
		sx_dprintk(SX_DEBUG_PROBE, "checking identifier...\n");
L
Linus Torvalds 已提交
2189

J
Jiri Slaby 已提交
2190 2191 2192
		if (strncmp(vpdp.identifier, SX_VPD_IDENT_STRING, 16) != 0) {
			sx_dprintk(SX_DEBUG_PROBE, "Got non-SX identifier: "
					"'%s'\n", vpdp.identifier);
L
Linus Torvalds 已提交
2193 2194 2195 2196
			return 0;
		}
	}

J
Jiri Slaby 已提交
2197
	printheader();
L
Linus Torvalds 已提交
2198

J
Jiri Slaby 已提交
2199 2200 2201 2202 2203 2204 2205
	if (!IS_CF_BOARD(board)) {
		printk(KERN_DEBUG "sx: Found an SX board at %lx\n",
			board->hw_base);
		printk(KERN_DEBUG "sx: hw_rev: %d, assembly level: %d, "
				"uniq ID:%08x, ",
				vpdp.hwrev, vpdp.hwass, vpdp.uniqid);
		printk("Manufactured: %d/%d\n", 1970 + vpdp.myear, vpdp.mweek);
L
Linus Torvalds 已提交
2206

J
Jiri Slaby 已提交
2207 2208 2209 2210 2211 2212 2213
		if ((((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) !=
				SX_PCI_UNIQUEID1) && (((vpdp.uniqid >> 24) &
				SX_UNIQUEID_MASK) != SX_ISA_UNIQUEID1)) {
			/* This might be a bit harsh. This was the primary
			   reason the SX/ISA card didn't work at first... */
			printk(KERN_ERR "sx: Hmm. Not an SX/PCI or SX/ISA "
					"card. Sorry: giving up.\n");
L
Linus Torvalds 已提交
2214 2215 2216
			return (0);
		}

J
Jiri Slaby 已提交
2217 2218
		if (((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) ==
				SX_ISA_UNIQUEID1) {
L
Linus Torvalds 已提交
2219
			if (((unsigned long)board->hw_base) & 0x8000) {
J
Jiri Slaby 已提交
2220 2221 2222 2223 2224
				printk(KERN_WARNING "sx: Warning: There may be "
					"hardware problems with the card at "
					"%lx.\n", board->hw_base);
				printk(KERN_WARNING "sx: Read sx.txt for more "
						"info.\n");
L
Linus Torvalds 已提交
2225 2226 2227 2228 2229 2230 2231
			}
		}
	}

	board->nports = -1;

	/* This resets the processor, and keeps it off the bus. */
J
Jiri Slaby 已提交
2232
	if (!sx_reset(board))
L
Linus Torvalds 已提交
2233
		return 0;
J
Jiri Slaby 已提交
2234
	sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
L
Linus Torvalds 已提交
2235 2236 2237 2238 2239

	func_exit();
	return 1;
}

J
Jiri Slaby 已提交
2240
#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
L
Linus Torvalds 已提交
2241 2242 2243 2244 2245 2246 2247

/* Specialix probes for this card at 32k increments from 640k to 16M.
   I consider machines with less than 16M unlikely nowadays, so I'm
   not probing above 1Mb. Also, 0xa0000, 0xb0000, are taken by the VGA
   card. 0xe0000 and 0xf0000 are taken by the BIOS. That only leaves 
   0xc0000, 0xc8000, 0xd0000 and 0xd8000 . */

J
Jiri Slaby 已提交
2248
static int __devinit probe_si(struct sx_board *board)
L
Linus Torvalds 已提交
2249 2250 2251 2252
{
	int i;

	func_enter();
J
Jiri Slaby 已提交
2253 2254
	sx_dprintk(SX_DEBUG_PROBE, "Going to verify SI signature hw %lx at "
		"%p.\n", board->hw_base, board->base + SI2_ISA_ID_BASE);
L
Linus Torvalds 已提交
2255 2256 2257 2258 2259

	if (sx_debug & SX_DEBUG_PROBE)
		my_hd_io(board->base + SI2_ISA_ID_BASE, 0x8);

	if (!IS_EISA_BOARD(board)) {
J
Jiri Slaby 已提交
2260 2261 2262 2263
		if (IS_SI1_BOARD(board)) {
			for (i = 0; i < 8; i++) {
				write_sx_byte(board, SI2_ISA_ID_BASE + 7 - i,i);
			}
L
Linus Torvalds 已提交
2264
		}
J
Jiri Slaby 已提交
2265 2266 2267 2268
		for (i = 0; i < 8; i++) {
			if ((read_sx_byte(board, SI2_ISA_ID_BASE + 7 - i) & 7)
					!= i) {
				func_exit();
L
Linus Torvalds 已提交
2269 2270 2271 2272 2273 2274 2275 2276 2277
				return 0;
			}
		}
	}

	/* Now we're pretty much convinced that there is an SI board here, 
	   but to prevent trouble, we'd better double check that we don't
	   have an SI1 board when we're probing for an SI2 board.... */

J
Jiri Slaby 已提交
2278 2279
	write_sx_byte(board, SI2_ISA_ID_BASE, 0x10);
	if (IS_SI1_BOARD(board)) {
L
Linus Torvalds 已提交
2280 2281
		/* This should be an SI1 board, which has this
		   location writable... */
J
Jiri Slaby 已提交
2282 2283 2284
		if (read_sx_byte(board, SI2_ISA_ID_BASE) != 0x10) {
			func_exit();
			return 0;
M
Marc Zyngier 已提交
2285
		}
L
Linus Torvalds 已提交
2286 2287 2288
	} else {
		/* This should be an SI2 board, which has the bottom
		   3 bits non-writable... */
J
Jiri Slaby 已提交
2289 2290 2291
		if (read_sx_byte(board, SI2_ISA_ID_BASE) == 0x10) {
			func_exit();
			return 0;
M
Marc Zyngier 已提交
2292
		}
L
Linus Torvalds 已提交
2293 2294 2295 2296 2297 2298
	}

	/* Now we're pretty much convinced that there is an SI board here, 
	   but to prevent trouble, we'd better double check that we don't
	   have an SI1 board when we're probing for an SI2 board.... */

J
Jiri Slaby 已提交
2299 2300
	write_sx_byte(board, SI2_ISA_ID_BASE, 0x10);
	if (IS_SI1_BOARD(board)) {
L
Linus Torvalds 已提交
2301 2302
		/* This should be an SI1 board, which has this
		   location writable... */
J
Jiri Slaby 已提交
2303
		if (read_sx_byte(board, SI2_ISA_ID_BASE) != 0x10) {
L
Linus Torvalds 已提交
2304
			func_exit();
J
Jiri Slaby 已提交
2305
			return 0;
M
Marc Zyngier 已提交
2306
		}
L
Linus Torvalds 已提交
2307 2308 2309
	} else {
		/* This should be an SI2 board, which has the bottom
		   3 bits non-writable... */
J
Jiri Slaby 已提交
2310 2311 2312
		if (read_sx_byte(board, SI2_ISA_ID_BASE) == 0x10) {
			func_exit();
			return 0;
M
Marc Zyngier 已提交
2313
		}
L
Linus Torvalds 已提交
2314 2315
	}

J
Jiri Slaby 已提交
2316
	printheader();
L
Linus Torvalds 已提交
2317

J
Jiri Slaby 已提交
2318
	printk(KERN_DEBUG "sx: Found an SI board at %lx\n", board->hw_base);
L
Linus Torvalds 已提交
2319
	/* Compared to the SX boards, it is a complete guess as to what
J
Jiri Slaby 已提交
2320
	   this card is up to... */
L
Linus Torvalds 已提交
2321 2322 2323 2324

	board->nports = -1;

	/* This resets the processor, and keeps it off the bus. */
J
Jiri Slaby 已提交
2325
	if (!sx_reset(board))
L
Linus Torvalds 已提交
2326
		return 0;
J
Jiri Slaby 已提交
2327
	sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
L
Linus Torvalds 已提交
2328 2329 2330 2331

	func_exit();
	return 1;
}
J
Jiri Slaby 已提交
2332
#endif
L
Linus Torvalds 已提交
2333

J
Jeff Dike 已提交
2334
static const struct tty_operations sx_ops = {
L
Linus Torvalds 已提交
2335
	.break_ctl = sx_break,
J
Jiri Slaby 已提交
2336
	.open = sx_open,
L
Linus Torvalds 已提交
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
	.close = gs_close,
	.write = gs_write,
	.put_char = gs_put_char,
	.flush_chars = gs_flush_chars,
	.write_room = gs_write_room,
	.chars_in_buffer = gs_chars_in_buffer,
	.flush_buffer = gs_flush_buffer,
	.ioctl = sx_ioctl,
	.throttle = sx_throttle,
	.unthrottle = sx_unthrottle,
	.set_termios = gs_set_termios,
	.stop = gs_stop,
	.start = gs_start,
	.hangup = gs_hangup,
	.tiocmget = sx_tiocmget,
	.tiocmset = sx_tiocmset,
};

2355 2356 2357 2358
static const struct tty_port_operations sx_port_ops = {
	.carrier_raised = sx_carrier_raised,
};

L
Linus Torvalds 已提交
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374
static int sx_init_drivers(void)
{
	int error;

	func_enter();

	sx_driver = alloc_tty_driver(sx_nports);
	if (!sx_driver)
		return 1;
	sx_driver->owner = THIS_MODULE;
	sx_driver->driver_name = "specialix_sx";
	sx_driver->name = "ttyX";
	sx_driver->major = SX_NORMAL_MAJOR;
	sx_driver->type = TTY_DRIVER_TYPE_SERIAL;
	sx_driver->subtype = SERIAL_TYPE_NORMAL;
	sx_driver->init_termios = tty_std_termios;
J
Jiri Slaby 已提交
2375
	sx_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
A
Alan Cox 已提交
2376 2377
	sx_driver->init_termios.c_ispeed = 9600;
	sx_driver->init_termios.c_ospeed = 9600;
L
Linus Torvalds 已提交
2378 2379 2380 2381 2382 2383
	sx_driver->flags = TTY_DRIVER_REAL_RAW;
	tty_set_operations(sx_driver, &sx_ops);

	if ((error = tty_register_driver(sx_driver))) {
		put_tty_driver(sx_driver);
		printk(KERN_ERR "sx: Couldn't register sx driver, error = %d\n",
J
Jiri Slaby 已提交
2384
			error);
L
Linus Torvalds 已提交
2385 2386 2387 2388 2389 2390
		return 1;
	}
	func_exit();
	return 0;
}

J
Jiri Slaby 已提交
2391
static int sx_init_portstructs(int nboards, int nports)
L
Linus Torvalds 已提交
2392 2393 2394 2395 2396 2397 2398 2399 2400 2401
{
	struct sx_board *board;
	struct sx_port *port;
	int i, j;
	int addr, chans;
	int portno;

	func_enter();

	/* Many drivers statically allocate the maximum number of ports
J
Jiri Slaby 已提交
2402 2403
	   There is no reason not to allocate them dynamically.
	   Is there? -- REW */
J
Jiri Slaby 已提交
2404
	sx_ports = kcalloc(nports, sizeof(struct sx_port), GFP_KERNEL);
L
Linus Torvalds 已提交
2405 2406 2407 2408 2409 2410 2411
	if (!sx_ports)
		return -ENOMEM;

	port = sx_ports;
	for (i = 0; i < nboards; i++) {
		board = &boards[i];
		board->ports = port;
J
Jiri Slaby 已提交
2412 2413
		for (j = 0; j < boards[i].nports; j++) {
			sx_dprintk(SX_DEBUG_INIT, "initing port %d\n", j);
A
Alan Cox 已提交
2414
			tty_port_init(&port->gs.port);
2415
			port->gs.port.ops = &sx_port_ops;
L
Linus Torvalds 已提交
2416
			port->gs.magic = SX_MAGIC;
J
Jiri Slaby 已提交
2417
			port->gs.close_delay = HZ / 2;
L
Linus Torvalds 已提交
2418 2419 2420 2421
			port->gs.closing_wait = 30 * HZ;
			port->board = board;
			port->gs.rd = &sx_real_driver;
#ifdef NEW_WRITE_LOCKING
2422
			port->gs.port_write_mutex = MUTEX;
L
Linus Torvalds 已提交
2423
#endif
I
Ingo Molnar 已提交
2424
			spin_lock_init(&port->gs.driver_lock);
L
Linus Torvalds 已提交
2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437
			/*
			 * Initializing wait queue
			 */
			port++;
		}
	}

	port = sx_ports;
	portno = 0;
	for (i = 0; i < nboards; i++) {
		board = &boards[i];
		board->port_base = portno;
		/* Possibly the configuration was rejected. */
J
Jiri Slaby 已提交
2438 2439 2440 2441
		sx_dprintk(SX_DEBUG_PROBE, "Board has %d channels\n",
				board->nports);
		if (board->nports <= 0)
			continue;
L
Linus Torvalds 已提交
2442
		/* XXX byteorder ?? */
J
Jiri Slaby 已提交
2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454
		for (addr = 0x80; addr != 0;
				addr = read_sx_word(board, addr) & 0x7fff) {
			chans = sx_read_module_byte(board, addr, mc_type);
			sx_dprintk(SX_DEBUG_PROBE, "Module at %x: %d "
					"channels\n", addr, chans);
			sx_dprintk(SX_DEBUG_PROBE, "Port at");
			for (j = 0; j < chans; j++) {
				/* The "sx-way" is the way it SHOULD be done.
				   That way in the future, the firmware may for
				   example pack the structures a bit more
				   efficient. Neil tells me it isn't going to
				   happen anytime soon though. */
L
Linus Torvalds 已提交
2455
				if (IS_SX_BOARD(board))
J
Jiri Slaby 已提交
2456 2457 2458
					port->ch_base = sx_read_module_word(
							board, addr + j * 2,
							mc_chan_pointer);
L
Linus Torvalds 已提交
2459
				else
J
Jiri Slaby 已提交
2460
					port->ch_base = addr + 0x100 + 0x300 *j;
L
Linus Torvalds 已提交
2461

J
Jiri Slaby 已提交
2462 2463
				sx_dprintk(SX_DEBUG_PROBE, " %x",
						port->ch_base);
L
Linus Torvalds 已提交
2464 2465 2466
				port->line = portno++;
				port++;
			}
J
Jiri Slaby 已提交
2467
			sx_dprintk(SX_DEBUG_PROBE, "\n");
L
Linus Torvalds 已提交
2468 2469 2470 2471 2472 2473 2474 2475 2476
		}
		/* This has to be done earlier. */
		/* board->flags |= SX_BOARD_INITIALIZED; */
	}

	func_exit();
	return 0;
}

J
Jiri Slaby 已提交
2477 2478 2479 2480
static unsigned int sx_find_free_board(void)
{
	unsigned int i;

J
Jiri Slaby 已提交
2481
	for (i = 0; i < SX_NBOARDS; i++)
J
Jiri Slaby 已提交
2482 2483 2484 2485 2486 2487
		if (!(boards[i].flags & SX_BOARD_PRESENT))
			break;

	return i;
}

L
Linus Torvalds 已提交
2488 2489 2490 2491 2492 2493 2494 2495
static void __exit sx_release_drivers(void)
{
	func_enter();
	tty_unregister_driver(sx_driver);
	put_tty_driver(sx_driver);
	func_exit();
}

J
Jiri Slaby 已提交
2496 2497
static void __devexit sx_remove_card(struct sx_board *board,
		struct pci_dev *pdev)
J
Jiri Slaby 已提交
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
{
	if (board->flags & SX_BOARD_INITIALIZED) {
		/* The board should stop messing with us. (actually I mean the
		   interrupt) */
		sx_reset(board);
		if ((board->irq) && (board->flags & SX_IRQ_ALLOCATED))
			free_irq(board->irq, board);

		/* It is safe/allowed to del_timer a non-active timer */
		del_timer(&board->timer);
J
Jiri Slaby 已提交
2508
		if (pdev) {
J
Jiri Slaby 已提交
2509
#ifdef CONFIG_PCI
J
Jiri Slaby 已提交
2510
			iounmap(board->base2);
J
Jiri Slaby 已提交
2511
			pci_release_region(pdev, IS_CF_BOARD(board) ? 3 : 2);
J
Jiri Slaby 已提交
2512
#endif
J
Jiri Slaby 已提交
2513
		} else {
J
Jiri Slaby 已提交
2514
			iounmap(board->base);
J
Jiri Slaby 已提交
2515 2516
			release_region(board->hw_base, board->hw_len);
		}
J
Jiri Slaby 已提交
2517

J
Jiri Slaby 已提交
2518
		board->flags &= ~(SX_BOARD_INITIALIZED | SX_BOARD_PRESENT);
J
Jiri Slaby 已提交
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531
	}
}

#ifdef CONFIG_EISA

static int __devinit sx_eisa_probe(struct device *dev)
{
	struct eisa_device *edev = to_eisa_device(dev);
	struct sx_board *board;
	unsigned long eisa_slot = edev->base_addr;
	unsigned int i;
	int retval = -EIO;

J
Jiri Slaby 已提交
2532
	mutex_lock(&sx_boards_lock);
J
Jiri Slaby 已提交
2533
	i = sx_find_free_board();
J
Jiri Slaby 已提交
2534 2535
	if (i == SX_NBOARDS) {
		mutex_unlock(&sx_boards_lock);
J
Jiri Slaby 已提交
2536
		goto err;
J
Jiri Slaby 已提交
2537 2538 2539 2540
	}
	board = &boards[i];
	board->flags |= SX_BOARD_PRESENT;
	mutex_unlock(&sx_boards_lock);
J
Jiri Slaby 已提交
2541 2542

	dev_info(dev, "XIO : Signature found in EISA slot %lu, "
J
Jiri Slaby 已提交
2543 2544 2545 2546
		 "Product %d Rev %d (REPORT THIS TO LKLM)\n",
		 eisa_slot >> 12,
		 inb(eisa_slot + EISA_VENDOR_ID_OFFSET + 2),
		 inb(eisa_slot + EISA_VENDOR_ID_OFFSET + 3));
J
Jiri Slaby 已提交
2547 2548 2549 2550 2551 2552

	board->eisa_base = eisa_slot;
	board->flags &= ~SX_BOARD_TYPE;
	board->flags |= SI_EISA_BOARD;

	board->hw_base = ((inb(eisa_slot + 0xc01) << 8) +
J
Jiri Slaby 已提交
2553
			  inb(eisa_slot + 0xc00)) << 16;
J
Jiri Slaby 已提交
2554 2555 2556 2557 2558
	board->hw_len = SI2_EISA_WINDOW_LEN;
	if (!request_region(board->hw_base, board->hw_len, "sx")) {
		dev_err(dev, "can't request region\n");
		goto err_flag;
	}
J
Jiri Slaby 已提交
2559
	board->base2 =
2560
	board->base = ioremap_nocache(board->hw_base, SI2_EISA_WINDOW_LEN);
J
Jiri Slaby 已提交
2561 2562 2563 2564
	if (!board->base) {
		dev_err(dev, "can't remap memory\n");
		goto err_reg;
	}
J
Jiri Slaby 已提交
2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578

	sx_dprintk(SX_DEBUG_PROBE, "IO hw_base address: %lx\n", board->hw_base);
	sx_dprintk(SX_DEBUG_PROBE, "base: %p\n", board->base);
	board->irq = inb(eisa_slot + 0xc02) >> 4;
	sx_dprintk(SX_DEBUG_PROBE, "IRQ: %d\n", board->irq);

	if (!probe_si(board))
		goto err_unmap;

	dev_set_drvdata(dev, board);

	return 0;
err_unmap:
	iounmap(board->base);
J
Jiri Slaby 已提交
2579 2580 2581
err_reg:
	release_region(board->hw_base, board->hw_len);
err_flag:
J
Jiri Slaby 已提交
2582
	board->flags &= ~SX_BOARD_PRESENT;
J
Jiri Slaby 已提交
2583 2584 2585 2586 2587 2588 2589 2590
err:
	return retval;
}

static int __devexit sx_eisa_remove(struct device *dev)
{
	struct sx_board *board = dev_get_drvdata(dev);

J
Jiri Slaby 已提交
2591
	sx_remove_card(board, NULL);
J
Jiri Slaby 已提交
2592 2593 2594 2595 2596 2597 2598 2599

	return 0;
}

static struct eisa_device_id sx_eisa_tbl[] = {
	{ "SLX" },
	{ "" }
};
J
Jiri Slaby 已提交
2600

J
Jiri Slaby 已提交
2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613
MODULE_DEVICE_TABLE(eisa, sx_eisa_tbl);

static struct eisa_driver sx_eisadriver = {
	.id_table = sx_eisa_tbl,
	.driver = {
		.name = "sx",
		.probe = sx_eisa_probe,
		.remove = __devexit_p(sx_eisa_remove),
	}
};

#endif

J
Jiri Slaby 已提交
2614
#ifdef CONFIG_PCI
L
Linus Torvalds 已提交
2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625
 /******************************************************** 
 * Setting bit 17 in the CNTRL register of the PLX 9050  * 
 * chip forces a retry on writes while a read is pending.*
 * This is to prevent the card locking up on Intel Xeon  *
 * multiprocessor systems with the NX chipset.    -- NV  *
 ********************************************************/

/* Newer cards are produced with this bit set from the configuration
   EEprom.  As the bit is read/write for the CPU, we can fix it here,
   if we detect that it isn't set correctly. -- REW */

2626
static void __devinit fix_sx_pci(struct pci_dev *pdev, struct sx_board *board)
L
Linus Torvalds 已提交
2627 2628 2629 2630 2631
{
	unsigned int hwbase;
	void __iomem *rebase;
	unsigned int t;

J
Jiri Slaby 已提交
2632 2633
#define CNTRL_REG_OFFSET	0x50
#define CNTRL_REG_GOODVALUE	0x18260000
L
Linus Torvalds 已提交
2634 2635 2636

	pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &hwbase);
	hwbase &= PCI_BASE_ADDRESS_MEM_MASK;
2637
	rebase = ioremap_nocache(hwbase, 0x80);
J
Jiri Slaby 已提交
2638
	t = readl(rebase + CNTRL_REG_OFFSET);
L
Linus Torvalds 已提交
2639
	if (t != CNTRL_REG_GOODVALUE) {
J
Jiri Slaby 已提交
2640 2641 2642
		printk(KERN_DEBUG "sx: performing cntrl reg fix: %08x -> "
			"%08x\n", t, CNTRL_REG_GOODVALUE);
		writel(CNTRL_REG_GOODVALUE, rebase + CNTRL_REG_OFFSET);
L
Linus Torvalds 已提交
2643 2644 2645
	}
	iounmap(rebase);
}
J
Jiri Slaby 已提交
2646
#endif
L
Linus Torvalds 已提交
2647

2648
static int __devinit sx_pci_probe(struct pci_dev *pdev,
J
Jiri Slaby 已提交
2649
				  const struct pci_device_id *ent)
2650
{
J
Jiri Slaby 已提交
2651
#ifdef CONFIG_PCI
2652
	struct sx_board *board;
J
Jiri Slaby 已提交
2653
	unsigned int i, reg;
2654 2655
	int retval = -EIO;

J
Jiri Slaby 已提交
2656
	mutex_lock(&sx_boards_lock);
J
Jiri Slaby 已提交
2657
	i = sx_find_free_board();
J
Jiri Slaby 已提交
2658 2659
	if (i == SX_NBOARDS) {
		mutex_unlock(&sx_boards_lock);
2660
		goto err;
J
Jiri Slaby 已提交
2661 2662 2663 2664
	}
	board = &boards[i];
	board->flags |= SX_BOARD_PRESENT;
	mutex_unlock(&sx_boards_lock);
2665 2666 2667

	retval = pci_enable_device(pdev);
	if (retval)
J
Jiri Slaby 已提交
2668
		goto err_flag;
2669 2670 2671

	board->flags &= ~SX_BOARD_TYPE;
	board->flags |= (pdev->subsystem_vendor == 0x200) ? SX_PCI_BOARD :
J
Jiri Slaby 已提交
2672
		SX_CFPCI_BOARD;
2673 2674

	/* CF boards use base address 3.... */
J
Jiri Slaby 已提交
2675
	reg = IS_CF_BOARD(board) ? 3 : 2;
J
Jiri Slaby 已提交
2676 2677 2678 2679 2680
	retval = pci_request_region(pdev, reg, "sx");
	if (retval) {
		dev_err(&pdev->dev, "can't request region\n");
		goto err_flag;
	}
J
Jiri Slaby 已提交
2681
	board->hw_base = pci_resource_start(pdev, reg);
2682
	board->base2 =
J
Jiri Slaby 已提交
2683
	board->base = ioremap_nocache(board->hw_base, WINDOW_LEN(board));
2684 2685
	if (!board->base) {
		dev_err(&pdev->dev, "ioremap failed\n");
J
Jiri Slaby 已提交
2686
		goto err_reg;
2687 2688 2689
	}

	/* Most of the stuff on the CF board is offset by 0x18000 ....  */
J
Jiri Slaby 已提交
2690
	if (IS_CF_BOARD(board))
2691 2692 2693 2694 2695
		board->base += 0x18000;

	board->irq = pdev->irq;

	dev_info(&pdev->dev, "Got a specialix card: %p(%d) %x.\n", board->base,
J
Jiri Slaby 已提交
2696
		 board->irq, board->flags);
2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708

	if (!probe_sx(board)) {
		retval = -EIO;
		goto err_unmap;
	}

	fix_sx_pci(pdev, board);

	pci_set_drvdata(pdev, board);

	return 0;
err_unmap:
J
Jiri Slaby 已提交
2709
	iounmap(board->base2);
J
Jiri Slaby 已提交
2710 2711
err_reg:
	pci_release_region(pdev, reg);
J
Jiri Slaby 已提交
2712 2713
err_flag:
	board->flags &= ~SX_BOARD_PRESENT;
2714 2715
err:
	return retval;
J
Jiri Slaby 已提交
2716 2717 2718
#else
	return -ENODEV;
#endif
2719 2720 2721 2722 2723 2724
}

static void __devexit sx_pci_remove(struct pci_dev *pdev)
{
	struct sx_board *board = pci_get_drvdata(pdev);

J
Jiri Slaby 已提交
2725
	sx_remove_card(board, pdev);
2726 2727 2728 2729 2730 2731
}

/* Specialix has a whole bunch of cards with 0x2000 as the device ID. They say
   its because the standard requires it. So check for SUBVENDOR_ID. */
static struct pci_device_id sx_pci_tbl[] = {
	{ PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8,
J
Jiri Slaby 已提交
2732
		.subvendor = PCI_ANY_ID, .subdevice = 0x0200 },
2733
	{ PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8,
J
Jiri Slaby 已提交
2734
		.subvendor = PCI_ANY_ID, .subdevice = 0x0300 },
2735 2736
	{ 0 }
};
J
Jiri Slaby 已提交
2737

2738 2739 2740 2741 2742 2743 2744 2745
MODULE_DEVICE_TABLE(pci, sx_pci_tbl);

static struct pci_driver sx_pcidriver = {
	.name = "sx",
	.id_table = sx_pci_tbl,
	.probe = sx_pci_probe,
	.remove = __devexit_p(sx_pci_remove)
};
L
Linus Torvalds 已提交
2746

J
Jiri Slaby 已提交
2747
static int __init sx_init(void)
L
Linus Torvalds 已提交
2748
{
J
Jiri Slaby 已提交
2749 2750 2751
#ifdef CONFIG_EISA
	int retval1;
#endif
J
Jiri Slaby 已提交
2752
#ifdef CONFIG_ISA
L
Linus Torvalds 已提交
2753
	struct sx_board *board;
J
Jiri Slaby 已提交
2754 2755 2756 2757
	unsigned int i;
#endif
	unsigned int found = 0;
	int retval;
L
Linus Torvalds 已提交
2758 2759

	func_enter();
J
Jiri Slaby 已提交
2760 2761 2762 2763 2764 2765
	sx_dprintk(SX_DEBUG_INIT, "Initing sx module... (sx_debug=%d)\n",
			sx_debug);
	if (abs((long)(&sx_debug) - sx_debug) < 0x10000) {
		printk(KERN_WARNING "sx: sx_debug is an address, instead of a "
				"value. Assuming -1.\n(%p)\n", &sx_debug);
		sx_debug = -1;
L
Linus Torvalds 已提交
2766 2767 2768
	}

	if (misc_register(&sx_fw_device) < 0) {
J
Jiri Slaby 已提交
2769 2770
		printk(KERN_ERR "SX: Unable to register firmware loader "
				"driver.\n");
L
Linus Torvalds 已提交
2771 2772
		return -EIO;
	}
J
Jiri Slaby 已提交
2773
#ifdef CONFIG_ISA
J
Jiri Slaby 已提交
2774
	for (i = 0; i < NR_SX_ADDRS; i++) {
L
Linus Torvalds 已提交
2775 2776
		board = &boards[found];
		board->hw_base = sx_probe_addrs[i];
J
Jiri Slaby 已提交
2777 2778 2779
		board->hw_len = SX_WINDOW_LEN;
		if (!request_region(board->hw_base, board->hw_len, "sx"))
			continue;
L
Linus Torvalds 已提交
2780
		board->base2 =
2781
		board->base = ioremap_nocache(board->hw_base, board->hw_len);
J
Jiri Slaby 已提交
2782 2783
		if (!board->base)
			goto err_sx_reg;
L
Linus Torvalds 已提交
2784
		board->flags &= ~SX_BOARD_TYPE;
J
Jiri Slaby 已提交
2785 2786
		board->flags |= SX_ISA_BOARD;
		board->irq = sx_irqmask ? -1 : 0;
L
Linus Torvalds 已提交
2787

J
Jiri Slaby 已提交
2788
		if (probe_sx(board)) {
J
Jiri Slaby 已提交
2789
			board->flags |= SX_BOARD_PRESENT;
L
Linus Torvalds 已提交
2790 2791 2792
			found++;
		} else {
			iounmap(board->base);
J
Jiri Slaby 已提交
2793 2794
err_sx_reg:
			release_region(board->hw_base, board->hw_len);
L
Linus Torvalds 已提交
2795 2796 2797
		}
	}

J
Jiri Slaby 已提交
2798
	for (i = 0; i < NR_SI_ADDRS; i++) {
L
Linus Torvalds 已提交
2799 2800
		board = &boards[found];
		board->hw_base = si_probe_addrs[i];
J
Jiri Slaby 已提交
2801 2802 2803
		board->hw_len = SI2_ISA_WINDOW_LEN;
		if (!request_region(board->hw_base, board->hw_len, "sx"))
			continue;
L
Linus Torvalds 已提交
2804
		board->base2 =
2805
		board->base = ioremap_nocache(board->hw_base, board->hw_len);
J
Jiri Slaby 已提交
2806 2807
		if (!board->base)
			goto err_si_reg;
L
Linus Torvalds 已提交
2808
		board->flags &= ~SX_BOARD_TYPE;
J
Jiri Slaby 已提交
2809 2810
		board->flags |= SI_ISA_BOARD;
		board->irq = sx_irqmask ? -1 : 0;
L
Linus Torvalds 已提交
2811

J
Jiri Slaby 已提交
2812
		if (probe_si(board)) {
J
Jiri Slaby 已提交
2813
			board->flags |= SX_BOARD_PRESENT;
L
Linus Torvalds 已提交
2814 2815
			found++;
		} else {
J
Jiri Slaby 已提交
2816
			iounmap(board->base);
J
Jiri Slaby 已提交
2817 2818
err_si_reg:
			release_region(board->hw_base, board->hw_len);
L
Linus Torvalds 已提交
2819 2820
		}
	}
J
Jiri Slaby 已提交
2821
	for (i = 0; i < NR_SI1_ADDRS; i++) {
L
Linus Torvalds 已提交
2822 2823
		board = &boards[found];
		board->hw_base = si1_probe_addrs[i];
J
Jiri Slaby 已提交
2824 2825 2826
		board->hw_len = SI1_ISA_WINDOW_LEN;
		if (!request_region(board->hw_base, board->hw_len, "sx"))
			continue;
L
Linus Torvalds 已提交
2827
		board->base2 =
2828
		board->base = ioremap_nocache(board->hw_base, board->hw_len);
J
Jiri Slaby 已提交
2829 2830
		if (!board->base)
			goto err_si1_reg;
L
Linus Torvalds 已提交
2831
		board->flags &= ~SX_BOARD_TYPE;
J
Jiri Slaby 已提交
2832 2833
		board->flags |= SI1_ISA_BOARD;
		board->irq = sx_irqmask ? -1 : 0;
L
Linus Torvalds 已提交
2834

J
Jiri Slaby 已提交
2835
		if (probe_si(board)) {
J
Jiri Slaby 已提交
2836
			board->flags |= SX_BOARD_PRESENT;
L
Linus Torvalds 已提交
2837 2838
			found++;
		} else {
J
Jiri Slaby 已提交
2839
			iounmap(board->base);
J
Jiri Slaby 已提交
2840 2841
err_si1_reg:
			release_region(board->hw_base, board->hw_len);
L
Linus Torvalds 已提交
2842 2843
		}
	}
J
Jiri Slaby 已提交
2844
#endif
J
Jiri Slaby 已提交
2845 2846 2847
#ifdef CONFIG_EISA
	retval1 = eisa_driver_register(&sx_eisadriver);
#endif
2848 2849
	retval = pci_register_driver(&sx_pcidriver);

L
Linus Torvalds 已提交
2850
	if (found) {
J
Jiri Slaby 已提交
2851
		printk(KERN_INFO "sx: total of %d boards detected.\n", found);
2852 2853
		retval = 0;
	} else if (retval) {
J
Jiri Slaby 已提交
2854
#ifdef CONFIG_EISA
2855
		retval = retval1;
J
Jiri Slaby 已提交
2856 2857
		if (retval1)
#endif
J
Jiri Slaby 已提交
2858
			misc_deregister(&sx_fw_device);
L
Linus Torvalds 已提交
2859 2860 2861
	}

	func_exit();
2862
	return retval;
L
Linus Torvalds 已提交
2863 2864
}

J
Jiri Slaby 已提交
2865
static void __exit sx_exit(void)
L
Linus Torvalds 已提交
2866
{
J
Jiri Slaby 已提交
2867
	int i;
L
Linus Torvalds 已提交
2868 2869

	func_enter();
J
Jiri Slaby 已提交
2870 2871 2872
#ifdef CONFIG_EISA
	eisa_driver_unregister(&sx_eisadriver);
#endif
2873
	pci_unregister_driver(&sx_pcidriver);
L
Linus Torvalds 已提交
2874

2875
	for (i = 0; i < SX_NBOARDS; i++)
J
Jiri Slaby 已提交
2876
		sx_remove_card(&boards[i], NULL);
2877

L
Linus Torvalds 已提交
2878
	if (misc_deregister(&sx_fw_device) < 0) {
J
Jiri Slaby 已提交
2879 2880
		printk(KERN_INFO "sx: couldn't deregister firmware loader "
				"device\n");
L
Linus Torvalds 已提交
2881
	}
J
Jiri Slaby 已提交
2882 2883
	sx_dprintk(SX_DEBUG_CLEANUP, "Cleaning up drivers (%d)\n",
			sx_initialized);
L
Linus Torvalds 已提交
2884
	if (sx_initialized)
J
Jiri Slaby 已提交
2885
		sx_release_drivers();
L
Linus Torvalds 已提交
2886

J
Jiri Slaby 已提交
2887
	kfree(sx_ports);
L
Linus Torvalds 已提交
2888 2889 2890 2891 2892
	func_exit();
}

module_init(sx_init);
module_exit(sx_exit);