1
2
3#define SX_VERSION	1.33
4
5#include <linux/module.h>
6#include <linux/kdev_t.h>
7#include <linux/kernel.h>
8#include <linux/sched.h>
9#include <linux/ioport.h>
10#include <linux/interrupt.h>
11#include <linux/errno.h>
12#include <linux/tty.h>
13#include <linux/tty_flip.h>
14#include <linux/mm.h>
15#include <linux/serial.h>
16#include <linux/fcntl.h>
17#include <linux/major.h>
18#include <linux/delay.h>
19#include <linux/eisa.h>
20#include <linux/pci.h>
21#include <linux/slab.h>
22#include <linux/init.h>
23#include <linux/miscdevice.h>
24#include <linux/bitops.h>
25
26#include <asm/io.h>
27#include <asm/uaccess.h>
28
29/* The 3.0.0 version of sxboards/sxwindow.h  uses BYTE and WORD.... */
30#define BYTE u8
31#define WORD u16
32
33/* .... but the 3.0.4 version uses _u8 and _u16. */
34#define _u8 u8
35#define _u16 u16
36
37#include "sxboards.h"
38#include "sxwindow.h"
39
40#include <linux/generic_serial.h>
41#include "sx.h"
42
43/* I don't think that this driver can handle more than 256 ports on
44   one machine. You'll have to increase the number of boards in sx.h
45   if you want more than 4 boards.  */
46
47#ifndef PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8
48#define PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8 0x2000
49#endif
50
51/* Configurable options:
52   (Don't be too sure that it'll work if you toggle them) */
53
54/* Am I paranoid or not ? ;-) */
55#undef SX_PARANOIA_CHECK
56
57/* 20 -> 2000 per second. The card should rate-limit interrupts at 100
58   Hz, but it is user configurable. I don't recommend going above 1000
59   Hz. The interrupt ratelimit might trigger if the interrupt is
60   shared with a very active other device. */
61#define IRQ_RATE_LIMIT 20
62
63/* Sharing interrupts is possible now. If the other device wants more
64   than 2000 interrupts per second, we'd gracefully decline further
65   interrupts. That's not what we want. On the other hand, if the
66   other device interrupts 2000 times a second, don't use the SX
67   interrupt. Use polling. */
68#undef IRQ_RATE_LIMIT
69
70
71/* Function prototypes */
72static void sx_disable_tx_interrupts(void *ptr);
73static void sx_enable_tx_interrupts(void *ptr);
74static void sx_disable_rx_interrupts(void *ptr);
75static void sx_enable_rx_interrupts(void *ptr);
76static int sx_get_CD(void *ptr);
77static void sx_shutdown_port(void *ptr);
78static int sx_set_real_termios(void *ptr);
79static void sx_close(void *ptr);
80static int sx_chars_in_buffer(void *ptr);
81static int sx_init_board(struct sx_board *board);
82static int sx_init_portstructs(int nboards, int nports);
83static int sx_fw_ioctl(struct inode *inode, struct file *filp,
84		unsigned int cmd, unsigned long arg);
85static int sx_init_drivers(void);
86
87static struct tty_driver *sx_driver;
88
89static DEFINE_MUTEX(sx_boards_lock);
90static struct sx_board boards[SX_NBOARDS];
91static struct sx_port *sx_ports;
92static int sx_initialized;
93static int sx_nports;
94static int sx_debug;
95
96/* You can have the driver poll your card.
97    - Set sx_poll to 1 to poll every timer tick (10ms on Intel).
98      This is used when the card cannot use an interrupt for some reason.
99
100    - set sx_slowpoll to 100 to do an extra poll once a second (on Intel). If
101      the driver misses an interrupt (report this if it DOES happen to you!)
102      everything will continue to work....
103 */
104static int sx_poll = 1;
105static int sx_slowpoll;
106
107/* The card limits the number of interrupts per second.
108   At 115k2 "100" should be sufficient.
109   If you're using higher baudrates, you can increase this...
110 */
111
112static int sx_maxints = 100;
113
114#ifdef CONFIG_ISA
115
116/* These are the only open spaces in my computer. Yours may have more
117   or less.... -- REW
118   duh: Card at 0xa0000 is possible on HP Netserver?? -- pvdl
119*/
120static int sx_probe_addrs[] = {
121	0xc0000, 0xd0000, 0xe0000,
122	0xc8000, 0xd8000, 0xe8000
123};
124static int si_probe_addrs[] = {
125	0xc0000, 0xd0000, 0xe0000,
126	0xc8000, 0xd8000, 0xe8000, 0xa0000
127};
128static int si1_probe_addrs[] = {
129	0xd0000
130};
131
132#define NR_SX_ADDRS ARRAY_SIZE(sx_probe_addrs)
133#define NR_SI_ADDRS ARRAY_SIZE(si_probe_addrs)
134#define NR_SI1_ADDRS ARRAY_SIZE(si1_probe_addrs)
135
136module_param_array(sx_probe_addrs, int, NULL, 0);
137module_param_array(si_probe_addrs, int, NULL, 0);
138#endif
139
140/* Set the mask to all-ones. This alas, only supports 32 interrupts.
141   Some architectures may need more. */
142static int sx_irqmask = -1;
143
144module_param(sx_poll, int, 0);
145module_param(sx_slowpoll, int, 0);
146module_param(sx_maxints, int, 0);
147module_param(sx_debug, int, 0);
148module_param(sx_irqmask, int, 0);
149
150MODULE_LICENSE("GPL");
151
152static struct real_driver sx_real_driver = {
153	sx_disable_tx_interrupts,
154	sx_enable_tx_interrupts,
155	sx_disable_rx_interrupts,
156	sx_enable_rx_interrupts,
157	sx_get_CD,
158	sx_shutdown_port,
159	sx_set_real_termios,
160	sx_chars_in_buffer,
161	sx_close,
162};
163
164/*
165   This driver can spew a whole lot of debugging output at you. If you
166   need maximum performance, you should disable the DEBUG define. To
167   aid in debugging in the field, I'm leaving the compile-time debug
168   features enabled, and disable them "runtime". That allows me to
169   instruct people with problems to enable debugging without requiring
170   them to recompile...
171*/
172#define DEBUG
173
174#ifdef DEBUG
175#define sx_dprintk(f, str...)	if (sx_debug & f) printk (str)
176#else
177#define sx_dprintk(f, str...)	/* nothing */
178#endif
179
180#define func_enter()	sx_dprintk(SX_DEBUG_FLOW, "sx: enter %s\n",__FUNCTION__)
181#define func_exit()	sx_dprintk(SX_DEBUG_FLOW, "sx: exit  %s\n",__FUNCTION__)
182
183#define func_enter2()	sx_dprintk(SX_DEBUG_FLOW, "sx: enter %s (port %d)\n", \
184				__FUNCTION__, port->line)
185
186/*
187 *  Firmware loader driver specific routines
188 *
189 */
190
191static const struct file_operations sx_fw_fops = {
192	.owner = THIS_MODULE,
193	.ioctl = sx_fw_ioctl,
194};
195
196static struct miscdevice sx_fw_device = {
197	SXCTL_MISC_MINOR, "sxctl", &sx_fw_fops
198};
199
200#ifdef SX_PARANOIA_CHECK
201
202/* This doesn't work. Who's paranoid around here? Not me! */
203
204static inline int sx_paranoia_check(struct sx_port const *port,
205				    char *name, const char *routine)
206{
207	static const char *badmagic = KERN_ERR "sx: Warning: bad sx port magic "
208			"number for device %s in %s\n";
209	static const char *badinfo = KERN_ERR "sx: Warning: null sx port for "
210			"device %s in %s\n";
211
212	if (!port) {
213		printk(badinfo, name, routine);
214		return 1;
215	}
216	if (port->magic != SX_MAGIC) {
217		printk(badmagic, name, routine);
218		return 1;
219	}
220
221	return 0;
222}
223#else
224#define sx_paranoia_check(a,b,c) 0
225#endif
226
227/* The timeouts. First try 30 times as fast as possible. Then give
228   the card some time to breathe between accesses. (Otherwise the
229   processor on the card might not be able to access its OWN bus... */
230
231#define TIMEOUT_1 30
232#define TIMEOUT_2 1000000
233
234#ifdef DEBUG
235static void my_hd_io(void __iomem *p, int len)
236{
237	int i, j, ch;
238	unsigned char __iomem *addr = p;
239
240	for (i = 0; i < len; i += 16) {
241		printk("%p ", addr + i);
242		for (j = 0; j < 16; j++) {
243			printk("%02x %s", readb(addr + j + i),
244					(j == 7) ? " " : "");
245		}
246		for (j = 0; j < 16; j++) {
247			ch = readb(addr + j + i);
248			printk("%c", (ch < 0x20) ? '.' :
249					((ch > 0x7f) ? '.' : ch));
250		}
251		printk("\n");
252	}
253}
254static void my_hd(void *p, int len)
255{
256	int i, j, ch;
257	unsigned char *addr = p;
258
259	for (i = 0; i < len; i += 16) {
260		printk("%p ", addr + i);
261		for (j = 0; j < 16; j++) {
262			printk("%02x %s", addr[j + i], (j == 7) ? " " : "");
263		}
264		for (j = 0; j < 16; j++) {
265			ch = addr[j + i];
266			printk("%c", (ch < 0x20) ? '.' :
267					((ch > 0x7f) ? '.' : ch));
268		}
269		printk("\n");
270	}
271}
272#endif
273
274/* This needs redoing for Alpha -- REW -- Done. */
275
276static inline void write_sx_byte(struct sx_board *board, int offset, u8 byte)
277{
278	writeb(byte, board->base + offset);
279}
280
281static inline u8 read_sx_byte(struct sx_board *board, int offset)
282{
283	return readb(board->base + offset);
284}
285
286static inline void write_sx_word(struct sx_board *board, int offset, u16 word)
287{
288	writew(word, board->base + offset);
289}
290
291static inline u16 read_sx_word(struct sx_board *board, int offset)
292{
293	return readw(board->base + offset);
294}
295
296static int sx_busy_wait_eq(struct sx_board *board,
297		int offset, int mask, int correctval)
298{
299	int i;
300
301	func_enter();
302
303	for (i = 0; i < TIMEOUT_1; i++)
304		if ((read_sx_byte(board, offset) & mask) == correctval) {
305			func_exit();
306			return 1;
307		}
308
309	for (i = 0; i < TIMEOUT_2; i++) {
310		if ((read_sx_byte(board, offset) & mask) == correctval) {
311			func_exit();
312			return 1;
313		}
314		udelay(1);
315	}
316
317	func_exit();
318	return 0;
319}
320
321static int sx_busy_wait_neq(struct sx_board *board,
322		int offset, int mask, int badval)
323{
324	int i;
325
326	func_enter();
327
328	for (i = 0; i < TIMEOUT_1; i++)
329		if ((read_sx_byte(board, offset) & mask) != badval) {
330			func_exit();
331			return 1;
332		}
333
334	for (i = 0; i < TIMEOUT_2; i++) {
335		if ((read_sx_byte(board, offset) & mask) != badval) {
336			func_exit();
337			return 1;
338		}
339		udelay(1);
340	}
341
342	func_exit();
343	return 0;
344}
345
346/* 5.6.4 of 6210028 r2.3 */
347static int sx_reset(struct sx_board *board)
348{
349	func_enter();
350
351	if (IS_SX_BOARD(board)) {
352
353		write_sx_byte(board, SX_CONFIG, 0);
354		write_sx_byte(board, SX_RESET, 1); /* Value doesn't matter */
355
356		if (!sx_busy_wait_eq(board, SX_RESET_STATUS, 1, 0)) {
357			printk(KERN_INFO "sx: Card doesn't respond to "
358					"reset...\n");
359			return 0;
360		}
361	} else if (IS_EISA_BOARD(board)) {
362		outb(board->irq << 4, board->eisa_base + 0xc02);
363	} else if (IS_SI1_BOARD(board)) {
364		write_sx_byte(board, SI1_ISA_RESET, 0);	/*value doesn't matter*/
365	} else {
366		/* Gory details of the SI/ISA board */
367		write_sx_byte(board, SI2_ISA_RESET, SI2_ISA_RESET_SET);
368		write_sx_byte(board, SI2_ISA_IRQ11, SI2_ISA_IRQ11_CLEAR);
369		write_sx_byte(board, SI2_ISA_IRQ12, SI2_ISA_IRQ12_CLEAR);
370		write_sx_byte(board, SI2_ISA_IRQ15, SI2_ISA_IRQ15_CLEAR);
371		write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_CLEAR);
372		write_sx_byte(board, SI2_ISA_IRQSET, SI2_ISA_IRQSET_CLEAR);
373	}
374
375	func_exit();
376	return 1;
377}
378
379/* This doesn't work on machines where "NULL" isn't 0 */
380/* If you have one of those, someone will need to write
381   the equivalent of this, which will amount to about 3 lines. I don't
382   want to complicate this right now. -- REW
383   (See, I do write comments every now and then :-) */
384#define OFFSETOF(strct, elem)	((long)&(((struct strct *)NULL)->elem))
385
386#define CHAN_OFFSET(port,elem)	(port->ch_base + OFFSETOF (_SXCHANNEL, elem))
387#define MODU_OFFSET(board,addr,elem)	(addr + OFFSETOF (_SXMODULE, elem))
388#define  BRD_OFFSET(board,elem)	(OFFSETOF (_SXCARD, elem))
389
390#define sx_write_channel_byte(port, elem, val) \
391	write_sx_byte (port->board, CHAN_OFFSET (port, elem), val)
392
393#define sx_read_channel_byte(port, elem) \
394	read_sx_byte (port->board, CHAN_OFFSET (port, elem))
395
396#define sx_write_channel_word(port, elem, val) \
397	write_sx_word (port->board, CHAN_OFFSET (port, elem), val)
398
399#define sx_read_channel_word(port, elem) \
400	read_sx_word (port->board, CHAN_OFFSET (port, elem))
401
402#define sx_write_module_byte(board, addr, elem, val) \
403	write_sx_byte (board, MODU_OFFSET (board, addr, elem), val)
404
405#define sx_read_module_byte(board, addr, elem) \
406	read_sx_byte (board, MODU_OFFSET (board, addr, elem))
407
408#define sx_write_module_word(board, addr, elem, val) \
409	write_sx_word (board, MODU_OFFSET (board, addr, elem), val)
410
411#define sx_read_module_word(board, addr, elem) \
412	read_sx_word (board, MODU_OFFSET (board, addr, elem))
413
414#define sx_write_board_byte(board, elem, val) \
415	write_sx_byte (board, BRD_OFFSET (board, elem), val)
416
417#define sx_read_board_byte(board, elem) \
418	read_sx_byte (board, BRD_OFFSET (board, elem))
419
420#define sx_write_board_word(board, elem, val) \
421	write_sx_word (board, BRD_OFFSET (board, elem), val)
422
423#define sx_read_board_word(board, elem) \
424	read_sx_word (board, BRD_OFFSET (board, elem))
425
426static int sx_start_board(struct sx_board *board)
427{
428	if (IS_SX_BOARD(board)) {
429		write_sx_byte(board, SX_CONFIG, SX_CONF_BUSEN);
430	} else if (IS_EISA_BOARD(board)) {
431		write_sx_byte(board, SI2_EISA_OFF, SI2_EISA_VAL);
432		outb((board->irq << 4) | 4, board->eisa_base + 0xc02);
433	} else if (IS_SI1_BOARD(board)) {
434		write_sx_byte(board, SI1_ISA_RESET_CLEAR, 0);
435		write_sx_byte(board, SI1_ISA_INTCL, 0);
436	} else {
437		/* Don't bug me about the clear_set.
438		   I haven't the foggiest idea what it's about -- REW */
439		write_sx_byte(board, SI2_ISA_RESET, SI2_ISA_RESET_CLEAR);
440		write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
441	}
442	return 1;
443}
444
445#define SX_IRQ_REG_VAL(board) \
446	((board->flags & SX_ISA_BOARD) ? (board->irq << 4) : 0)
447
448/* Note. The SX register is write-only. Therefore, we have to enable the
449   bus too. This is a no-op, if you don't mess with this driver... */
450static int sx_start_interrupts(struct sx_board *board)
451{
452
453	/* Don't call this with board->irq == 0 */
454
455	if (IS_SX_BOARD(board)) {
456		write_sx_byte(board, SX_CONFIG, SX_IRQ_REG_VAL(board) |
457				SX_CONF_BUSEN | SX_CONF_HOSTIRQ);
458	} else if (IS_EISA_BOARD(board)) {
459		inb(board->eisa_base + 0xc03);
460	} else if (IS_SI1_BOARD(board)) {
461		write_sx_byte(board, SI1_ISA_INTCL, 0);
462		write_sx_byte(board, SI1_ISA_INTCL_CLEAR, 0);
463	} else {
464		switch (board->irq) {
465		case 11:
466			write_sx_byte(board, SI2_ISA_IRQ11, SI2_ISA_IRQ11_SET);
467			break;
468		case 12:
469			write_sx_byte(board, SI2_ISA_IRQ12, SI2_ISA_IRQ12_SET);
470			break;
471		case 15:
472			write_sx_byte(board, SI2_ISA_IRQ15, SI2_ISA_IRQ15_SET);
473			break;
474		default:
475			printk(KERN_INFO "sx: SI/XIO card doesn't support "
476					"interrupt %d.\n", board->irq);
477			return 0;
478		}
479		write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
480	}
481
482	return 1;
483}
484
485static int sx_send_command(struct sx_port *port,
486		int command, int mask, int newstat)
487{
488	func_enter2();
489	write_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat), command);
490	func_exit();
491	return sx_busy_wait_eq(port->board, CHAN_OFFSET(port, hi_hstat), mask,
492			newstat);
493}
494
495static char *mod_type_s(int module_type)
496{
497	switch (module_type) {
498	case TA4:
499		return "TA4";
500	case TA8:
501		return "TA8";
502	case TA4_ASIC:
503		return "TA4_ASIC";
504	case TA8_ASIC:
505		return "TA8_ASIC";
506	case MTA_CD1400:
507		return "MTA_CD1400";
508	case SXDC:
509		return "SXDC";
510	default:
511		return "Unknown/invalid";
512	}
513}
514
515static char *pan_type_s(int pan_type)
516{
517	switch (pan_type) {
518	case MOD_RS232DB25:
519		return "MOD_RS232DB25";
520	case MOD_RS232RJ45:
521		return "MOD_RS232RJ45";
522	case MOD_RS422DB25:
523		return "MOD_RS422DB25";
524	case MOD_PARALLEL:
525		return "MOD_PARALLEL";
526	case MOD_2_RS232DB25:
527		return "MOD_2_RS232DB25";
528	case MOD_2_RS232RJ45:
529		return "MOD_2_RS232RJ45";
530	case MOD_2_RS422DB25:
531		return "MOD_2_RS422DB25";
532	case MOD_RS232DB25MALE:
533		return "MOD_RS232DB25MALE";
534	case MOD_2_PARALLEL:
535		return "MOD_2_PARALLEL";
536	case MOD_BLANK:
537		return "empty";
538	default:
539		return "invalid";
540	}
541}
542
543static int mod_compat_type(int module_type)
544{
545	return module_type >> 4;
546}
547
548static void sx_reconfigure_port(struct sx_port *port)
549{
550	if (sx_read_channel_byte(port, hi_hstat) == HS_IDLE_OPEN) {
551		if (sx_send_command(port, HS_CONFIG, -1, HS_IDLE_OPEN) != 1) {
552			printk(KERN_WARNING "sx: Sent reconfigure command, but "
553					"card didn't react.\n");
554		}
555	} else {
556		sx_dprintk(SX_DEBUG_TERMIOS, "sx: Not sending reconfigure: "
557				"port isn't open (%02x).\n",
558				sx_read_channel_byte(port, hi_hstat));
559	}
560}
561
562static void sx_setsignals(struct sx_port *port, int dtr, int rts)
563{
564	int t;
565	func_enter2();
566
567	t = sx_read_channel_byte(port, hi_op);
568	if (dtr >= 0)
569		t = dtr ? (t | OP_DTR) : (t & ~OP_DTR);
570	if (rts >= 0)
571		t = rts ? (t | OP_RTS) : (t & ~OP_RTS);
572	sx_write_channel_byte(port, hi_op, t);
573	sx_dprintk(SX_DEBUG_MODEMSIGNALS, "setsignals: %d/%d\n", dtr, rts);
574
575	func_exit();
576}
577
578static int sx_getsignals(struct sx_port *port)
579{
580	int i_stat, o_stat;
581
582	o_stat = sx_read_channel_byte(port, hi_op);
583	i_stat = sx_read_channel_byte(port, hi_ip);
584
585	sx_dprintk(SX_DEBUG_MODEMSIGNALS, "getsignals: %d/%d  (%d/%d) "
586			"%02x/%02x\n",
587			(o_stat & OP_DTR) != 0, (o_stat & OP_RTS) != 0,
588			port->c_dcd, sx_get_CD(port),
589			sx_read_channel_byte(port, hi_ip),
590			sx_read_channel_byte(port, hi_state));
591
592	return (((o_stat & OP_DTR) ? TIOCM_DTR : 0) |
593		((o_stat & OP_RTS) ? TIOCM_RTS : 0) |
594		((i_stat & IP_CTS) ? TIOCM_CTS : 0) |
595		((i_stat & IP_DCD) ? TIOCM_CAR : 0) |
596		((i_stat & IP_DSR) ? TIOCM_DSR : 0) |
597		((i_stat & IP_RI) ? TIOCM_RNG : 0));
598}
599
600static void sx_set_baud(struct sx_port *port)
601{
602	int t;
603
604	if (port->board->ta_type == MOD_SXDC) {
605		switch (port->gs.baud) {
606			/* Save some typing work... */
607#define e(x) case x: t = BAUD_ ## x; break
608			e(50);
609			e(75);
610			e(110);
611			e(150);
612			e(200);
613			e(300);
614			e(600);
615			e(1200);
616			e(1800);
617			e(2000);
618			e(2400);
619			e(4800);
620			e(7200);
621			e(9600);
622			e(14400);
623			e(19200);
624			e(28800);
625			e(38400);
626			e(56000);
627			e(57600);
628			e(64000);
629			e(76800);
630			e(115200);
631			e(128000);
632			e(150000);
633			e(230400);
634			e(256000);
635			e(460800);
636			e(921600);
637		case 134:
638			t = BAUD_134_5;
639			break;
640		case 0:
641			t = -1;
642			break;
643		default:
644			/* Can I return "invalid"? */
645			t = BAUD_9600;
646			printk(KERN_INFO "sx: unsupported baud rate: %d.\n",
647					port->gs.baud);
648			break;
649		}
650#undef e
651		if (t > 0) {
652/* The baud rate is not set to 0, so we're enabeling DTR... -- REW */
653			sx_setsignals(port, 1, -1);
654			sx_write_channel_byte(port, hi_csr, 0xff);
655
656			sx_write_channel_byte(port, hi_txbaud, t);
657			sx_write_channel_byte(port, hi_rxbaud, t);
658		} else {
659			sx_setsignals(port, 0, -1);
660		}
661	} else {
662		switch (port->gs.baud) {
663#define e(x) case x: t = CSR_ ## x; break
664			e(75);
665			e(150);
666			e(300);
667			e(600);
668			e(1200);
669			e(2400);
670			e(4800);
671			e(1800);
672			e(9600);
673			e(19200);
674			e(57600);
675			e(38400);
676/* TA supports 110, but not 115200, MTA supports 115200, but not 110 */
677		case 110:
678			if (port->board->ta_type == MOD_TA) {
679				t = CSR_110;
680				break;
681			} else {
682				t = CSR_9600;
683				printk(KERN_INFO "sx: Unsupported baud rate: "
684						"%d.\n", port->gs.baud);
685				break;
686			}
687		case 115200:
688			if (port->board->ta_type == MOD_TA) {
689				t = CSR_9600;
690				printk(KERN_INFO "sx: Unsupported baud rate: "
691						"%d.\n", port->gs.baud);
692				break;
693			} else {
694				t = CSR_110;
695				break;
696			}
697		case 0:
698			t = -1;
699			break;
700		default:
701			t = CSR_9600;
702			printk(KERN_INFO "sx: Unsupported baud rate: %d.\n",
703					port->gs.baud);
704			break;
705		}
706#undef e
707		if (t >= 0) {
708			sx_setsignals(port, 1, -1);
709			sx_write_channel_byte(port, hi_csr, t * 0x11);
710		} else {
711			sx_setsignals(port, 0, -1);
712		}
713	}
714}
715
716/* Simon Allen's version of this routine was 225 lines long. 85 is a lot
717   better. -- REW */
718
719static int sx_set_real_termios(void *ptr)
720{
721	struct sx_port *port = ptr;
722
723	func_enter2();
724
725	if (!port->gs.tty)
726		return 0;
727
728	/* What is this doing here? -- REW
729	   Ha! figured it out. It is to allow you to get DTR active again
730	   if you've dropped it with stty 0. Moved to set_baud, where it
731	   belongs (next to the drop dtr if baud == 0) -- REW */
732	/* sx_setsignals (port, 1, -1); */
733
734	sx_set_baud(port);
735
736#define CFLAG port->gs.tty->termios->c_cflag
737	sx_write_channel_byte(port, hi_mr1,
738			(C_PARENB(port->gs.tty) ? MR1_WITH : MR1_NONE) |
739			(C_PARODD(port->gs.tty) ? MR1_ODD : MR1_EVEN) |
740			(C_CRTSCTS(port->gs.tty) ? MR1_RTS_RXFLOW : 0) |
741			(((CFLAG & CSIZE) == CS8) ? MR1_8_BITS : 0) |
742			(((CFLAG & CSIZE) == CS7) ? MR1_7_BITS : 0) |
743			(((CFLAG & CSIZE) == CS6) ? MR1_6_BITS : 0) |
744			(((CFLAG & CSIZE) == CS5) ? MR1_5_BITS : 0));
745
746	sx_write_channel_byte(port, hi_mr2,
747			(C_CRTSCTS(port->gs.tty) ? MR2_CTS_TXFLOW : 0) |
748			(C_CSTOPB(port->gs.tty) ? MR2_2_STOP :
749			MR2_1_STOP));
750
751	switch (CFLAG & CSIZE) {
752	case CS8:
753		sx_write_channel_byte(port, hi_mask, 0xff);
754		break;
755	case CS7:
756		sx_write_channel_byte(port, hi_mask, 0x7f);
757		break;
758	case CS6:
759		sx_write_channel_byte(port, hi_mask, 0x3f);
760		break;
761	case CS5:
762		sx_write_channel_byte(port, hi_mask, 0x1f);
763		break;
764	default:
765		printk(KERN_INFO "sx: Invalid wordsize: %u\n", CFLAG & CSIZE);
766		break;
767	}
768
769	sx_write_channel_byte(port, hi_prtcl,
770			(I_IXON(port->gs.tty) ? SP_TXEN : 0) |
771			(I_IXOFF(port->gs.tty) ? SP_RXEN : 0) |
772			(I_IXANY(port->gs.tty) ? SP_TANY : 0) | SP_DCEN);
773
774	sx_write_channel_byte(port, hi_break,
775			(I_IGNBRK(port->gs.tty) ? BR_IGN : 0 |
776			I_BRKINT(port->gs.tty) ? BR_INT : 0));
777
778	sx_write_channel_byte(port, hi_txon, START_CHAR(port->gs.tty));
779	sx_write_channel_byte(port, hi_rxon, START_CHAR(port->gs.tty));
780	sx_write_channel_byte(port, hi_txoff, STOP_CHAR(port->gs.tty));
781	sx_write_channel_byte(port, hi_rxoff, STOP_CHAR(port->gs.tty));
782
783	sx_reconfigure_port(port);
784
785	/* Tell line discipline whether we will do input cooking */
786	if (I_OTHER(port->gs.tty)) {
787		clear_bit(TTY_HW_COOK_IN, &port->gs.tty->flags);
788	} else {
789		set_bit(TTY_HW_COOK_IN, &port->gs.tty->flags);
790	}
791	sx_dprintk(SX_DEBUG_TERMIOS, "iflags: %x(%d) ",
792			port->gs.tty->termios->c_iflag, I_OTHER(port->gs.tty));
793
794/* Tell line discipline whether we will do output cooking.
795 * If OPOST is set and no other output flags are set then we can do output
796 * processing.  Even if only *one* other flag in the O_OTHER group is set
797 * we do cooking in software.
798 */
799	if (O_OPOST(port->gs.tty) && !O_OTHER(port->gs.tty)) {
800		set_bit(TTY_HW_COOK_OUT, &port->gs.tty->flags);
801	} else {
802		clear_bit(TTY_HW_COOK_OUT, &port->gs.tty->flags);
803	}
804	sx_dprintk(SX_DEBUG_TERMIOS, "oflags: %x(%d)\n",
805			port->gs.tty->termios->c_oflag, O_OTHER(port->gs.tty));
806	/* port->c_dcd = sx_get_CD (port); */
807	func_exit();
808	return 0;
809}
810
811/* ********************************************************************** *
812 *                   the interrupt related routines                       *
813 * ********************************************************************** */
814
815/* Note:
816   Other drivers use the macro "MIN" to calculate how much to copy.
817   This has the disadvantage that it will evaluate parts twice. That's
818   expensive when it's IO (and the compiler cannot optimize those away!).
819   Moreover, I'm not sure that you're race-free.
820
821   I assign a value, and then only allow the value to decrease. This
822   is always safe. This makes the code a few lines longer, and you
823   know I'm dead against that, but I think it is required in this
824   case.  */
825
826static void sx_transmit_chars(struct sx_port *port)
827{
828	int c;
829	int tx_ip;
830	int txroom;
831
832	func_enter2();
833	sx_dprintk(SX_DEBUG_TRANSMIT, "Port %p: transmit %d chars\n",
834			port, port->gs.xmit_cnt);
835
836	if (test_and_set_bit(SX_PORT_TRANSMIT_LOCK, &port->locks)) {
837		return;
838	}
839
840	while (1) {
841		c = port->gs.xmit_cnt;
842
843		sx_dprintk(SX_DEBUG_TRANSMIT, "Copying %d ", c);
844		tx_ip = sx_read_channel_byte(port, hi_txipos);
845
846		/* Took me 5 minutes to deduce this formula.
847		   Luckily it is literally in the manual in section 6.5.4.3.5 */
848		txroom = (sx_read_channel_byte(port, hi_txopos) - tx_ip - 1) &
849				0xff;
850
851		/* Don't copy more bytes than there is room for in the buffer */
852		if (c > txroom)
853			c = txroom;
854		sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%d) ", c, txroom);
855
856		/* Don't copy past the end of the hardware transmit buffer */
857		if (c > 0x100 - tx_ip)
858			c = 0x100 - tx_ip;
859
860		sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%d) ", c, 0x100 - tx_ip);
861
862		/* Don't copy pas the end of the source buffer */
863		if (c > SERIAL_XMIT_SIZE - port->gs.xmit_tail)
864			c = SERIAL_XMIT_SIZE - port->gs.xmit_tail;
865
866		sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%ld) \n",
867				c, SERIAL_XMIT_SIZE - port->gs.xmit_tail);
868
869		/* If for one reason or another, we can't copy more data, we're
870		   done! */
871		if (c == 0)
872			break;
873
874		memcpy_toio(port->board->base + CHAN_OFFSET(port, hi_txbuf) +
875			tx_ip, port->gs.xmit_buf + port->gs.xmit_tail, c);
876
877		/* Update the pointer in the card */
878		sx_write_channel_byte(port, hi_txipos, (tx_ip + c) & 0xff);
879
880		/* Update the kernel buffer end */
881		port->gs.xmit_tail = (port->gs.xmit_tail + c) &
882				(SERIAL_XMIT_SIZE - 1);
883
884		/* This one last. (this is essential)
885		   It would allow others to start putting more data into the
886		   buffer! */
887		port->gs.xmit_cnt -= c;
888	}
889
890	if (port->gs.xmit_cnt == 0) {
891		sx_disable_tx_interrupts(port);
892	}
893
894	if ((port->gs.xmit_cnt <= port->gs.wakeup_chars) && port->gs.tty) {
895		tty_wakeup(port->gs.tty);
896		sx_dprintk(SX_DEBUG_TRANSMIT, "Waking up.... ldisc (%d)....\n",
897				port->gs.wakeup_chars);
898	}
899
900	clear_bit(SX_PORT_TRANSMIT_LOCK, &port->locks);
901	func_exit();
902}
903
904/* Note the symmetry between receiving chars and transmitting them!
905   Note: The kernel should have implemented both a receive buffer and
906   a transmit buffer. */
907
908/* Inlined: Called only once. Remove the inline when you add another call */
909static inline void sx_receive_chars(struct sx_port *port)
910{
911	int c;
912	int rx_op;
913	struct tty_struct *tty;
914	int copied = 0;
915	unsigned char *rp;
916
917	func_enter2();
918	tty = port->gs.tty;
919	while (1) {
920		rx_op = sx_read_channel_byte(port, hi_rxopos);
921		c = (sx_read_channel_byte(port, hi_rxipos) - rx_op) & 0xff;
922
923		sx_dprintk(SX_DEBUG_RECEIVE, "rxop=%d, c = %d.\n", rx_op, c);
924
925		/* Don't copy past the end of the hardware receive buffer */
926		if (rx_op + c > 0x100)
927			c = 0x100 - rx_op;
928
929		sx_dprintk(SX_DEBUG_RECEIVE, "c = %d.\n", c);
930
931		/* Don't copy more bytes than there is room for in the buffer */
932
933		c = tty_prepare_flip_string(tty, &rp, c);
934
935		sx_dprintk(SX_DEBUG_RECEIVE, "c = %d.\n", c);
936
937		/* If for one reason or another, we can't copy more data, we're done! */
938		if (c == 0)
939			break;
940
941		sx_dprintk(SX_DEBUG_RECEIVE, "Copying over %d chars. First is "
942				"%d at %lx\n", c, read_sx_byte(port->board,
943					CHAN_OFFSET(port, hi_rxbuf) + rx_op),
944				CHAN_OFFSET(port, hi_rxbuf));
945		memcpy_fromio(rp, port->board->base +
946				CHAN_OFFSET(port, hi_rxbuf) + rx_op, c);
947
948		/* This one last. ( Not essential.)
949		   It allows the card to start putting more data into the
950		   buffer!
951		   Update the pointer in the card */
952		sx_write_channel_byte(port, hi_rxopos, (rx_op + c) & 0xff);
953
954		copied += c;
955	}
956	if (copied) {
957		struct timeval tv;
958
959		do_gettimeofday(&tv);
960		sx_dprintk(SX_DEBUG_RECEIVE, "pushing flipq port %d (%3d "
961				"chars): %d.%06d  (%d/%d)\n", port->line,
962				copied, (int)(tv.tv_sec % 60), (int)tv.tv_usec,
963				tty->raw, tty->real_raw);
964
965		/* Tell the rest of the system the news. Great news. New
966		   characters! */
967		tty_flip_buffer_push(tty);
968		/*    tty_schedule_flip (tty); */
969	}
970
971	func_exit();
972}
973
974/* Inlined: it is called only once. Remove the inline if you add another
975   call */
976static inline void sx_check_modem_signals(struct sx_port *port)
977{
978	int hi_state;
979	int c_dcd;
980
981	hi_state = sx_read_channel_byte(port, hi_state);
982	sx_dprintk(SX_DEBUG_MODEMSIGNALS, "Checking modem signals (%d/%d)\n",
983			port->c_dcd, sx_get_CD(port));
984
985	if (hi_state & ST_BREAK) {
986		hi_state &= ~ST_BREAK;
987		sx_dprintk(SX_DEBUG_MODEMSIGNALS, "got a break.\n");
988		sx_write_channel_byte(port, hi_state, hi_state);
989		gs_got_break(&port->gs);
990	}
991	if (hi_state & ST_DCD) {
992		hi_state &= ~ST_DCD;
993		sx_dprintk(SX_DEBUG_MODEMSIGNALS, "got a DCD change.\n");
994		sx_write_channel_byte(port, hi_state, hi_state);
995		c_dcd = sx_get_CD(port);
996		sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD is now %d\n", c_dcd);
997		if (c_dcd != port->c_dcd) {
998			port->c_dcd = c_dcd;
999			if (sx_get_CD(port)) {
1000				/* DCD went UP */
1001				if ((sx_read_channel_byte(port, hi_hstat) !=
1002						HS_IDLE_CLOSED) &&
1003						!(port->gs.tty->termios->
1004							c_cflag & CLOCAL)) {
1005					/* Are we blocking in open? */
1006					sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
1007						"active, unblocking open\n");
1008					wake_up_interruptible(&port->gs.
1009							open_wait);
1010				} else {
1011					sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
1012						"raised. Ignoring.\n");
1013				}
1014			} else {
1015				/* DCD went down! */
1016				if (!(port->gs.tty->termios->c_cflag & CLOCAL)){
1017					sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
1018						"dropped. hanging up....\n");
1019					tty_hangup(port->gs.tty);
1020				} else {
1021					sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
1022						"dropped. ignoring.\n");
1023				}
1024			}
1025		} else {
1026			sx_dprintk(SX_DEBUG_MODEMSIGNALS, "Hmmm. card told us "
1027				"DCD changed, but it didn't.\n");
1028		}
1029	}
1030}
1031
1032/* This is what an interrupt routine should look like.
1033 * Small, elegant, clear.
1034 */
1035
1036static irqreturn_t sx_interrupt(int irq, void *ptr)
1037{
1038	struct sx_board *board = ptr;
1039	struct sx_port *port;
1040	int i;
1041
1042	func_enter();
1043	sx_dprintk(SX_DEBUG_FLOW, "sx: enter sx_interrupt (%d/%d)\n", irq,
1044			board->irq);
1045
1046	/* AAargh! The order in which to do these things is essential and
1047	   not trivial.
1048
1049	   - Rate limit goes before "recursive". Otherwise a series of
1050	   recursive calls will hang the machine in the interrupt routine.
1051
1052	   - hardware twiddling goes before "recursive". Otherwise when we
1053	   poll the card, and a recursive interrupt happens, we won't
1054	   ack the card, so it might keep on interrupting us. (especially
1055	   level sensitive interrupt systems like PCI).
1056
1057	   - Rate limit goes before hardware twiddling. Otherwise we won't
1058	   catch a card that has gone bonkers.
1059
1060	   - The "initialized" test goes after the hardware twiddling. Otherwise
1061	   the card will stick us in the interrupt routine again.
1062
1063	   - The initialized test goes before recursive.
1064	 */
1065
1066#ifdef IRQ_RATE_LIMIT
1067	/* Aaargh! I'm ashamed. This costs more lines-of-code than the
1068	   actual interrupt routine!. (Well, used to when I wrote that
1069	   comment) */
1070	{
1071		static int lastjif;
1072		static int nintr = 0;
1073
1074		if (lastjif == jiffies) {
1075			if (++nintr > IRQ_RATE_LIMIT) {
1076				free_irq(board->irq, board);
1077				printk(KERN_ERR "sx: Too many interrupts. "
1078						"Turning off interrupt %d.\n",
1079						board->irq);
1080			}
1081		} else {
1082			lastjif = jiffies;
1083			nintr = 0;
1084		}
1085	}
1086#endif
1087
1088	if (board->irq == irq) {
1089		/* Tell the card we've noticed the interrupt. */
1090
1091		sx_write_board_word(board, cc_int_pending, 0);
1092		if (IS_SX_BOARD(board)) {
1093			write_sx_byte(board, SX_RESET_IRQ, 1);
1094		} else if (IS_EISA_BOARD(board)) {
1095			inb(board->eisa_base + 0xc03);
1096			write_sx_word(board, 8, 0);
1097		} else {
1098			write_sx_byte(board, SI2_ISA_INTCLEAR,
1099					SI2_ISA_INTCLEAR_CLEAR);
1100			write_sx_byte(board, SI2_ISA_INTCLEAR,
1101					SI2_ISA_INTCLEAR_SET);
1102		}
1103	}
1104
1105	if (!sx_initialized)
1106		return IRQ_HANDLED;
1107	if (!(board->flags & SX_BOARD_INITIALIZED))
1108		return IRQ_HANDLED;
1109
1110	if (test_and_set_bit(SX_BOARD_INTR_LOCK, &board->locks)) {
1111		printk(KERN_ERR "Recursive interrupt! (%d)\n", board->irq);
1112		return IRQ_HANDLED;
1113	}
1114
1115	for (i = 0; i < board->nports; i++) {
1116		port = &board->ports[i];
1117		if (port->gs.flags & GS_ACTIVE) {
1118			if (sx_read_channel_byte(port, hi_state)) {
1119				sx_dprintk(SX_DEBUG_INTERRUPTS, "Port %d: "
1120						"modem signal change?... \n",i);
1121				sx_check_modem_signals(port);
1122			}
1123			if (port->gs.xmit_cnt) {
1124				sx_transmit_chars(port);
1125			}
1126			if (!(port->gs.flags & SX_RX_THROTTLE)) {
1127				sx_receive_chars(port);
1128			}
1129		}
1130	}
1131
1132	clear_bit(SX_BOARD_INTR_LOCK, &board->locks);
1133
1134	sx_dprintk(SX_DEBUG_FLOW, "sx: exit sx_interrupt (%d/%d)\n", irq,
1135			board->irq);
1136	func_exit();
1137	return IRQ_HANDLED;
1138}
1139
1140static void sx_pollfunc(unsigned long data)
1141{
1142	struct sx_board *board = (struct sx_board *)data;
1143
1144	func_enter();
1145
1146	sx_interrupt(0, board);
1147
1148	mod_timer(&board->timer, jiffies + sx_poll);
1149	func_exit();
1150}
1151
1152/* ********************************************************************** *
1153 *                Here are the routines that actually                     *
1154 *              interface with the generic_serial driver                  *
1155 * ********************************************************************** */
1156
1157/* Ehhm. I don't know how to fiddle with interrupts on the SX card. --REW */
1158/* Hmm. Ok I figured it out. You don't.  */
1159
1160static void sx_disable_tx_interrupts(void *ptr)
1161{
1162	struct sx_port *port = ptr;
1163	func_enter2();
1164
1165	port->gs.flags &= ~GS_TX_INTEN;
1166
1167	func_exit();
1168}
1169
1170static void sx_enable_tx_interrupts(void *ptr)
1171{
1172	struct sx_port *port = ptr;
1173	int data_in_buffer;
1174	func_enter2();
1175
1176	/* First transmit the characters that we're supposed to */
1177	sx_transmit_chars(port);
1178
1179	/* The sx card will never interrupt us if we don't fill the buffer
1180	   past 25%. So we keep considering interrupts off if that's the case. */
1181	data_in_buffer = (sx_read_channel_byte(port, hi_txipos) -
1182			  sx_read_channel_byte(port, hi_txopos)) & 0xff;
1183
1184	if (data_in_buffer < LOW_WATER)
1185		port->gs.flags &= ~GS_TX_INTEN;
1186
1187	func_exit();
1188}
1189
1190static void sx_disable_rx_interrupts(void *ptr)
1191{
1192	/*  struct sx_port *port = ptr; */
1193	func_enter();
1194
1195	func_exit();
1196}
1197
1198static void sx_enable_rx_interrupts(void *ptr)
1199{
1200	/*  struct sx_port *port = ptr; */
1201	func_enter();
1202
1203	func_exit();
1204}
1205
1206/* Jeez. Isn't this simple? */
1207static int sx_get_CD(void *ptr)
1208{
1209	struct sx_port *port = ptr;
1210	func_enter2();
1211
1212	func_exit();
1213	return ((sx_read_channel_byte(port, hi_ip) & IP_DCD) != 0);
1214}
1215
1216/* Jeez. Isn't this simple? */
1217static int sx_chars_in_buffer(void *ptr)
1218{
1219	struct sx_port *port = ptr;
1220	func_enter2();
1221
1222	func_exit();
1223	return ((sx_read_channel_byte(port, hi_txipos) -
1224		 sx_read_channel_byte(port, hi_txopos)) & 0xff);
1225}
1226
1227static void sx_shutdown_port(void *ptr)
1228{
1229	struct sx_port *port = ptr;
1230
1231	func_enter();
1232
1233	port->gs.flags &= ~GS_ACTIVE;
1234	if (port->gs.tty && (port->gs.tty->termios->c_cflag & HUPCL)) {
1235		sx_setsignals(port, 0, 0);
1236		sx_reconfigure_port(port);
1237	}
1238
1239	func_exit();
1240}
1241
1242/* ********************************************************************** *
1243 *                Here are the routines that actually                     *
1244 *               interface with the rest of the system                    *
1245 * ********************************************************************** */
1246
1247static int sx_open(struct tty_struct *tty, struct file *filp)
1248{
1249	struct sx_port *port;
1250	int retval, line;
1251	unsigned long flags;
1252
1253	func_enter();
1254
1255	if (!sx_initialized) {
1256		return -EIO;
1257	}
1258
1259	line = tty->index;
1260	sx_dprintk(SX_DEBUG_OPEN, "%d: opening line %d. tty=%p ctty=%p, "
1261			"np=%d)\n", current->pid, line, tty,
1262			current->signal->tty, sx_nports);
1263
1264	if ((line < 0) || (line >= SX_NPORTS) || (line >= sx_nports))
1265		return -ENODEV;
1266
1267	port = &sx_ports[line];
1268	port->c_dcd = 0; /* Make sure that the first interrupt doesn't detect a
1269			    1 -> 0 transition. */
1270
1271	sx_dprintk(SX_DEBUG_OPEN, "port = %p c_dcd = %d\n", port, port->c_dcd);
1272
1273	spin_lock_irqsave(&port->gs.driver_lock, flags);
1274
1275	tty->driver_data = port;
1276	port->gs.tty = tty;
1277	port->gs.count++;
1278	spin_unlock_irqrestore(&port->gs.driver_lock, flags);
1279
1280	sx_dprintk(SX_DEBUG_OPEN, "starting port\n");
1281
1282	/*
1283	 * Start up serial port
1284	 */
1285	retval = gs_init_port(&port->gs);
1286	sx_dprintk(SX_DEBUG_OPEN, "done gs_init\n");
1287	if (retval) {
1288		port->gs.count--;
1289		return retval;
1290	}
1291
1292	port->gs.flags |= GS_ACTIVE;
1293	if (port->gs.count <= 1)
1294		sx_setsignals(port, 1, 1);
1295
1296	if (sx_debug & SX_DEBUG_OPEN)
1297		my_hd_io(port->board->base + port->ch_base, sizeof(*port));
1298
1299	if (port->gs.count <= 1) {
1300		if (sx_send_command(port, HS_LOPEN, -1, HS_IDLE_OPEN) != 1) {
1301			printk(KERN_ERR "sx: Card didn't respond to LOPEN "
1302					"command.\n");
1303			spin_lock_irqsave(&port->gs.driver_lock, flags);
1304			port->gs.count--;
1305			spin_unlock_irqrestore(&port->gs.driver_lock, flags);
1306			return -EIO;
1307		}
1308	}
1309
1310	retval = gs_block_til_ready(port, filp);
1311	sx_dprintk(SX_DEBUG_OPEN, "Block til ready returned %d. Count=%d\n",
1312			retval, port->gs.count);
1313
1314	if (retval) {
1315/*
1316 * Don't lower gs.count here because sx_close() will be called later
1317 */
1318
1319		return retval;
1320	}
1321	/* tty->low_latency = 1; */
1322
1323	port->c_dcd = sx_get_CD(port);
1324	sx_dprintk(SX_DEBUG_OPEN, "at open: cd=%d\n", port->c_dcd);
1325
1326	func_exit();
1327	return 0;
1328
1329}
1330
1331static void sx_close(void *ptr)
1332{
1333	struct sx_port *port = ptr;
1334	/* Give the port 5 seconds to close down. */
1335	int to = 5 * HZ;
1336
1337	func_enter();
1338
1339	sx_setsignals(port, 0, 0);
1340	sx_reconfigure_port(port);
1341	sx_send_command(port, HS_CLOSE, 0, 0);
1342
1343	while (to-- && (sx_read_channel_byte(port, hi_hstat) != HS_IDLE_CLOSED))
1344		if (msleep_interruptible(10))
1345			break;
1346	if (sx_read_channel_byte(port, hi_hstat) != HS_IDLE_CLOSED) {
1347		if (sx_send_command(port, HS_FORCE_CLOSED, -1, HS_IDLE_CLOSED)
1348				!= 1) {
1349			printk(KERN_ERR "sx: sent the force_close command, but "
1350					"card didn't react\n");
1351		} else
1352			sx_dprintk(SX_DEBUG_CLOSE, "sent the force_close "
1353					"command.\n");
1354	}
1355
1356	sx_dprintk(SX_DEBUG_CLOSE, "waited %d jiffies for close. count=%d\n",
1357			5 * HZ - to - 1, port->gs.count);
1358
1359	if (port->gs.count) {
1360		sx_dprintk(SX_DEBUG_CLOSE, "WARNING port count:%d\n",
1361				port->gs.count);
1362		/*printk("%s SETTING port count to zero: %p count: %d\n",
1363				__FUNCTION__, port, port->gs.count);
1364		port->gs.count = 0;*/
1365	}
1366
1367	func_exit();
1368}
1369
1370/* This is relatively thorough. But then again it is only 20 lines. */
1371#define MARCHUP		for (i = min; i < max; i++)
1372#define MARCHDOWN	for (i = max - 1; i >= min; i--)
1373#define W0		write_sx_byte(board, i, 0x55)
1374#define W1		write_sx_byte(board, i, 0xaa)
1375#define R0		if (read_sx_byte(board, i) != 0x55) return 1
1376#define R1		if (read_sx_byte(board, i) != 0xaa) return 1
1377
1378/* This memtest takes a human-noticable time. You normally only do it
1379   once a boot, so I guess that it is worth it. */
1380static int do_memtest(struct sx_board *board, int min, int max)
1381{
1382	int i;
1383
1384	/* This is a marchb. Theoretically, marchb catches much more than
1385	   simpler tests. In practise, the longer test just catches more
1386	   intermittent errors. -- REW
1387	   (For the theory behind memory testing see:
1388	   Testing Semiconductor Memories by A.J. van de Goor.) */
1389	MARCHUP {
1390		W0;
1391	}
1392	MARCHUP {
1393		R0;
1394		W1;
1395		R1;
1396		W0;
1397		R0;
1398		W1;
1399	}
1400	MARCHUP {
1401		R1;
1402		W0;
1403		W1;
1404	}
1405	MARCHDOWN {
1406		R1;
1407		W0;
1408		W1;
1409		W0;
1410	}
1411	MARCHDOWN {
1412		R0;
1413		W1;
1414		W0;
1415	}
1416
1417	return 0;
1418}
1419
1420#undef MARCHUP
1421#undef MARCHDOWN
1422#undef W0
1423#undef W1
1424#undef R0
1425#undef R1
1426
1427#define MARCHUP		for (i = min; i < max; i += 2)
1428#define MARCHDOWN	for (i = max - 1; i >= min; i -= 2)
1429#define W0		write_sx_word(board, i, 0x55aa)
1430#define W1		write_sx_word(board, i, 0xaa55)
1431#define R0		if (read_sx_word(board, i) != 0x55aa) return 1
1432#define R1		if (read_sx_word(board, i) != 0xaa55) return 1
1433
1434
1435static int sx_fw_ioctl(struct inode *inode, struct file *filp,
1436		unsigned int cmd, unsigned long arg)
1437{
1438	int rc = 0;
1439	int __user *descr = (int __user *)arg;
1440	int i;
1441	static struct sx_board *board = NULL;
1442	int nbytes, offset;
1443	unsigned long data;
1444	char *tmp;
1445
1446	func_enter();
1447
1448
1449	sx_dprintk(SX_DEBUG_FIRMWARE, "IOCTL %x: %lx\n", cmd, arg);
1450
1451	if (!board)
1452		board = &boards[0];
1453	if (board->flags & SX_BOARD_PRESENT) {
1454		sx_dprintk(SX_DEBUG_FIRMWARE, "Board present! (%x)\n",
1455				board->flags);
1456	} else {
1457		sx_dprintk(SX_DEBUG_FIRMWARE, "Board not present! (%x) all:",
1458				board->flags);
1459		for (i = 0; i < SX_NBOARDS; i++)
1460			sx_dprintk(SX_DEBUG_FIRMWARE, "<%x> ", boards[i].flags);
1461		sx_dprintk(SX_DEBUG_FIRMWARE, "\n");
1462		return -EIO;
1463	}
1464
1465	switch (cmd) {
1466	case SXIO_SET_BOARD:
1467		sx_dprintk(SX_DEBUG_FIRMWARE, "set board to %ld\n", arg);
1468		if (arg >= SX_NBOARDS)
1469			return -EIO;
1470		sx_dprintk(SX_DEBUG_FIRMWARE, "not out of range\n");
1471		if (!(boards[arg].flags & SX_BOARD_PRESENT))
1472			return -EIO;
1473		sx_dprintk(SX_DEBUG_FIRMWARE, ".. and present!\n");
1474		board = &boards[arg];
1475		break;
1476	case SXIO_GET_TYPE:
1477		rc = -ENOENT;	/* If we manage to miss one, return error. */
1478		if (IS_SX_BOARD(board))
1479			rc = SX_TYPE_SX;
1480		if (IS_CF_BOARD(board))
1481			rc = SX_TYPE_CF;
1482		if (IS_SI_BOARD(board))
1483			rc = SX_TYPE_SI;
1484		if (IS_SI1_BOARD(board))
1485			rc = SX_TYPE_SI;
1486		if (IS_EISA_BOARD(board))
1487			rc = SX_TYPE_SI;
1488		sx_dprintk(SX_DEBUG_FIRMWARE, "returning type= %d\n", rc);
1489		break;
1490	case SXIO_DO_RAMTEST:
1491		if (sx_initialized)	/* Already initialized: better not ramtest the board.  */
1492			return -EPERM;
1493		if (IS_SX_BOARD(board)) {
1494			rc = do_memtest(board, 0, 0x7000);
1495			if (!rc)
1496				rc = do_memtest(board, 0, 0x7000);
1497			/*if (!rc) rc = do_memtest_w (board, 0, 0x7000); */
1498		} else {
1499			rc = do_memtest(board, 0, 0x7ff8);
1500			/* if (!rc) rc = do_memtest_w (board, 0, 0x7ff8); */
1501		}
1502		sx_dprintk(SX_DEBUG_FIRMWARE, "returning memtest result= %d\n",
1503			   rc);
1504		break;
1505	case SXIO_DOWNLOAD:
1506		if (sx_initialized)	/* Already initialized */
1507			return -EEXIST;
1508		if (!sx_reset(board))
1509			return -EIO;
1510		sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
1511
1512		tmp = kmalloc(SX_CHUNK_SIZE, GFP_USER);
1513		if (!tmp)
1514			return -ENOMEM;
1515		get_user(nbytes, descr++);
1516		get_user(offset, descr++);
1517		get_user(data, descr++);
1518		while (nbytes && data) {
1519			for (i = 0; i < nbytes; i += SX_CHUNK_SIZE) {
1520				if (copy_from_user(tmp, (char __user *)data + i,
1521						(i + SX_CHUNK_SIZE > nbytes) ?
1522						nbytes - i : SX_CHUNK_SIZE)) {
1523					kfree(tmp);
1524					return -EFAULT;
1525				}
1526				memcpy_toio(board->base2 + offset + i, tmp,
1527						(i + SX_CHUNK_SIZE > nbytes) ?
1528						nbytes - i : SX_CHUNK_SIZE);
1529			}
1530
1531			get_user(nbytes, descr++);
1532			get_user(offset, descr++);
1533			get_user(data, descr++);
1534		}
1535		kfree(tmp);
1536		sx_nports += sx_init_board(board);
1537		rc = sx_nports;
1538		break;
1539	case SXIO_INIT:
1540		if (sx_initialized)	/* Already initialized */
1541			return -EEXIST;
1542		/* This is not allowed until all boards are initialized... */
1543		for (i = 0; i < SX_NBOARDS; i++) {
1544			if ((boards[i].flags & SX_BOARD_PRESENT) &&
1545				!(boards[i].flags & SX_BOARD_INITIALIZED))
1546				return -EIO;
1547		}
1548		for (i = 0; i < SX_NBOARDS; i++)
1549			if (!(boards[i].flags & SX_BOARD_PRESENT))
1550				break;
1551
1552		sx_dprintk(SX_DEBUG_FIRMWARE, "initing portstructs, %d boards, "
1553				"%d channels, first board: %d ports\n",
1554				i, sx_nports, boards[0].nports);
1555		rc = sx_init_portstructs(i, sx_nports);
1556		sx_init_drivers();
1557		if (rc >= 0)
1558			sx_initialized++;
1559		break;
1560	case SXIO_SETDEBUG:
1561		sx_debug = arg;
1562		break;
1563	case SXIO_GETDEBUG:
1564		rc = sx_debug;
1565		break;
1566	case SXIO_GETGSDEBUG:
1567	case SXIO_SETGSDEBUG:
1568		rc = -EINVAL;
1569		break;
1570	case SXIO_GETNPORTS:
1571		rc = sx_nports;
1572		break;
1573	default:
1574		printk(KERN_WARNING "Unknown ioctl on firmware device (%x).\n",
1575				cmd);
1576		break;
1577	}
1578	func_exit();
1579	return rc;
1580}
1581
1582static void sx_break(struct tty_struct *tty, int flag)
1583{
1584	struct sx_port *port = tty->driver_data;
1585	int rv;
1586
1587	func_enter();
1588
1589	if (flag)
1590		rv = sx_send_command(port, HS_START, -1, HS_IDLE_BREAK);
1591	else
1592		rv = sx_send_command(port, HS_STOP, -1, HS_IDLE_OPEN);
1593	if (rv != 1)
1594		printk(KERN_ERR "sx: couldn't send break (%x).\n",
1595			read_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat)));
1596
1597	func_exit();
1598}
1599
1600static int sx_tiocmget(struct tty_struct *tty, struct file *file)
1601{
1602	struct sx_port *port = tty->driver_data;
1603	return sx_getsignals(port);
1604}
1605
1606static int sx_tiocmset(struct tty_struct *tty, struct file *file,
1607		unsigned int set, unsigned int clear)
1608{
1609	struct sx_port *port = tty->driver_data;
1610	int rts = -1, dtr = -1;
1611
1612	if (set & TIOCM_RTS)
1613		rts = 1;
1614	if (set & TIOCM_DTR)
1615		dtr = 1;
1616	if (clear & TIOCM_RTS)
1617		rts = 0;
1618	if (clear & TIOCM_DTR)
1619		dtr = 0;
1620
1621	sx_setsignals(port, dtr, rts);
1622	sx_reconfigure_port(port);
1623	return 0;
1624}
1625
1626static int sx_ioctl(struct tty_struct *tty, struct file *filp,
1627		unsigned int cmd, unsigned long arg)
1628{
1629	int rc;
1630	struct sx_port *port = tty->driver_data;
1631	void __user *argp = (void __user *)arg;
1632	int ival;
1633
1634	/* func_enter2(); */
1635
1636	rc = 0;
1637	switch (cmd) {
1638	case TIOCGSOFTCAR:
1639		rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
1640				(unsigned __user *)argp);
1641		break;
1642	case TIOCSSOFTCAR:
1643		if ((rc = get_user(ival, (unsigned __user *)argp)) == 0) {
1644			tty->termios->c_cflag =
1645				(tty->termios->c_cflag & ~CLOCAL) |
1646				(ival ? CLOCAL : 0);
1647		}
1648		break;
1649	case TIOCGSERIAL:
1650		rc = gs_getserial(&port->gs, argp);
1651		break;
1652	case TIOCSSERIAL:
1653		rc = gs_setserial(&port->gs, argp);
1654		break;
1655	default:
1656		rc = -ENOIOCTLCMD;
1657		break;
1658	}
1659
1660	/* func_exit(); */
1661	return rc;
1662}
1663
1664/* The throttle/unthrottle scheme for the Specialix card is different
1665 * from other drivers and deserves some explanation.
1666 * The Specialix hardware takes care of XON/XOFF
1667 * and CTS/RTS flow control itself.  This means that all we have to
1668 * do when signalled by the upper tty layer to throttle/unthrottle is
1669 * to make a note of it here.  When we come to read characters from the
1670 * rx buffers on the card (sx_receive_chars()) we look to see if the
1671 * upper layer can accept more (as noted here in sx_rx_throt[]).
1672 * If it can't we simply don't remove chars from the cards buffer.
1673 * When the tty layer can accept chars, we again note that here and when
1674 * sx_receive_chars() is called it will remove them from the cards buffer.
1675 * The card will notice that a ports buffer has drained below some low
1676 * water mark and will unflow control the line itself, using whatever
1677 * flow control scheme is in use for that port. -- Simon Allen
1678 */
1679
1680static void sx_throttle(struct tty_struct *tty)
1681{
1682	struct sx_port *port = (struct sx_port *)tty->driver_data;
1683
1684	func_enter2();
1685	/* If the port is using any type of input flow
1686	 * control then throttle the port.
1687	 */
1688	if ((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty))) {
1689		port->gs.flags |= SX_RX_THROTTLE;
1690	}
1691	func_exit();
1692}
1693
1694static void sx_unthrottle(struct tty_struct *tty)
1695{
1696	struct sx_port *port = (struct sx_port *)tty->driver_data;
1697
1698	func_enter2();
1699	/* Always unthrottle even if flow control is not enabled on
1700	 * this port in case we disabled flow control while the port
1701	 * was throttled
1702	 */
1703	port->gs.flags &= ~SX_RX_THROTTLE;
1704	func_exit();
1705	return;
1706}
1707
1708/* ********************************************************************** *
1709 *                    Here are the initialization routines.               *
1710 * ********************************************************************** */
1711
1712static int sx_init_board(struct sx_board *board)
1713{
1714	int addr;
1715	int chans;
1716	int type;
1717
1718	func_enter();
1719
1720	/* This is preceded by downloading the download code. */
1721
1722	board->flags |= SX_BOARD_INITIALIZED;
1723
1724	if (read_sx_byte(board, 0))
1725		/* CF boards may need this. */
1726		write_sx_byte(board, 0, 0);
1727
1728	/* This resets the processor again, to make sure it didn't do any
1729	   foolish things while we were downloading the image */
1730	if (!sx_reset(board))
1731		return 0;
1732
1733	sx_start_board(board);
1734	udelay(10);
1735	if (!sx_busy_wait_neq(board, 0, 0xff, 0)) {
1736		printk(KERN_ERR "sx: Ooops. Board won't initialize.\n");
1737		return 0;
1738	}
1739
1740	/* Ok. So now the processor on the card is running. It gathered
1741	   some info for us... */
1742	sx_dprintk(SX_DEBUG_INIT, "The sxcard structure:\n");
1743	if (sx_debug & SX_DEBUG_INIT)
1744		my_hd_io(board->base, 0x10);
1745	sx_dprintk(SX_DEBUG_INIT, "the first sx_module structure:\n");
1746	if (sx_debug & SX_DEBUG_INIT)
1747		my_hd_io(board->base + 0x80, 0x30);
1748
1749	sx_dprintk(SX_DEBUG_INIT, "init_status: %x, %dk memory, firmware "
1750			"V%x.%02x,\n",
1751			read_sx_byte(board, 0), read_sx_byte(board, 1),
1752			read_sx_byte(board, 5), read_sx_byte(board, 4));
1753
1754	if (read_sx_byte(board, 0) == 0xff) {
1755		printk(KERN_INFO "sx: No modules found. Sorry.\n");
1756		board->nports = 0;
1757		return 0;
1758	}
1759
1760	chans = 0;
1761
1762	if (IS_SX_BOARD(board)) {
1763		sx_write_board_word(board, cc_int_count, sx_maxints);
1764	} else {
1765		if (sx_maxints)
1766			sx_write_board_word(board, cc_int_count,
1767					SI_PROCESSOR_CLOCK / 8 / sx_maxints);
1768	}
1769
1770	/* grab the first module type... */
1771	/* board->ta_type = mod_compat_type (read_sx_byte (board, 0x80 + 0x08)); */
1772	board->ta_type = mod_compat_type(sx_read_module_byte(board, 0x80,
1773				mc_chip));
1774
1775	for (addr = 0x80; addr != 0; addr = read_sx_word(board, addr) & 0x7fff){
1776		type = sx_read_module_byte(board, addr, mc_chip);
1777		sx_dprintk(SX_DEBUG_INIT, "Module at %x: %d channels\n",
1778				addr, read_sx_byte(board, addr + 2));
1779
1780		chans += sx_read_module_byte(board, addr, mc_type);
1781
1782		sx_dprintk(SX_DEBUG_INIT, "module is an %s, which has %s/%s "
1783				"panels\n",
1784				mod_type_s(type),
1785				pan_type_s(sx_read_module_byte(board, addr,
1786						mc_mods) & 0xf),
1787				pan_type_s(sx_read_module_byte(board, addr,
1788						mc_mods) >> 4));
1789
1790		sx_dprintk(SX_DEBUG_INIT, "CD1400 versions: %x/%x, ASIC "
1791			"version: %x\n",
1792			sx_read_module_byte(board, addr, mc_rev1),
1793			sx_read_module_byte(board, addr, mc_rev2),
1794			sx_read_module_byte(board, addr, mc_mtaasic_rev));
1795
1796		/* The following combinations are illegal: It should theoretically
1797		   work, but timing problems make the bus HANG. */
1798
1799		if (mod_compat_type(type) != board->ta_type) {
1800			printk(KERN_ERR "sx: This is an invalid "
1801				"configuration.\nDon't mix TA/MTA/SXDC on the "
1802				"same hostadapter.\n");
1803			chans = 0;
1804			break;
1805		}
1806		if ((IS_EISA_BOARD(board) ||
1807				IS_SI_BOARD(board)) &&
1808				(mod_compat_type(type) == 4)) {
1809			printk(KERN_ERR	"sx: This is an invalid "
1810				"configuration.\nDon't use SXDCs on an SI/XIO "
1811				"adapter.\n");
1812			chans = 0;
1813			break;
1814		}
1815	}
1816
1817	if (chans) {
1818		if (board->irq > 0) {
1819			/* fixed irq, probably PCI */
1820			if (sx_irqmask & (1 << board->irq)) {	/* may we use this irq? */
1821				if (request_irq(board->irq, sx_interrupt,
1822						IRQF_SHARED | IRQF_DISABLED,
1823						"sx", board)) {
1824					printk(KERN_ERR "sx: Cannot allocate "
1825						"irq %d.\n", board->irq);
1826					board->irq = 0;
1827				}
1828			} else
1829				board->irq = 0;
1830		} else if (board->irq < 0 && sx_irqmask) {
1831			/* auto-allocate irq */
1832			int irqnr;
1833			int irqmask = sx_irqmask & (IS_SX_BOARD(board) ?
1834					SX_ISA_IRQ_MASK : SI2_ISA_IRQ_MASK);
1835			for (irqnr = 15; irqnr > 0; irqnr--)
1836				if (irqmask & (1 << irqnr))
1837					if (!request_irq(irqnr, sx_interrupt,
1838						IRQF_SHARED | IRQF_DISABLED,
1839						"sx", board))
1840						break;
1841			if (!irqnr)
1842				printk(KERN_ERR "sx: Cannot allocate IRQ.\n");
1843			board->irq = irqnr;
1844		} else
1845			board->irq = 0;
1846
1847		if (board->irq) {
1848			/* Found a valid interrupt, start up interrupts! */
1849			sx_dprintk(SX_DEBUG_INIT, "Using irq %d.\n",
1850					board->irq);
1851			sx_start_interrupts(board);
1852			board->poll = sx_slowpoll;
1853			board->flags |= SX_IRQ_ALLOCATED;
1854		} else {
1855			/* no irq: setup board for polled operation */
1856			board->poll = sx_poll;
1857			sx_dprintk(SX_DEBUG_INIT, "Using poll-interval %d.\n",
1858					board->poll);
1859		}
1860
1861		/* The timer should be initialized anyway: That way we can
1862		   safely del_timer it when the module is unloaded. */
1863		setup_timer(&board->timer, sx_pollfunc, (unsigned long)board);
1864
1865		if (board->poll)
1866			mod_timer(&board->timer, jiffies + board->poll);
1867	} else {
1868		board->irq = 0;
1869	}
1870
1871	board->nports = chans;
1872	sx_dprintk(SX_DEBUG_INIT, "returning %d ports.", board->nports);
1873
1874	func_exit();
1875	return chans;
1876}
1877
1878static void __devinit printheader(void)
1879{
1880	static int header_printed;
1881
1882	if (!header_printed) {
1883		printk(KERN_INFO "Specialix SX driver "
1884			"(C) 1998/1999 R.E.Wolff@BitWizard.nl\n");
1885		printk(KERN_INFO "sx: version " __stringify(SX_VERSION) "\n");
1886		header_printed = 1;
1887	}
1888}
1889
1890static int __devinit probe_sx(struct sx_board *board)
1891{
1892	struct vpd_prom vpdp;
1893	char *p;
1894	int i;
1895
1896	func_enter();
1897
1898	if (!IS_CF_BOARD(board)) {
1899		sx_dprintk(SX_DEBUG_PROBE, "Going to verify vpd prom at %p.\n",
1900				board->base + SX_VPD_ROM);
1901
1902		if (sx_debug & SX_DEBUG_PROBE)
1903			my_hd_io(board->base + SX_VPD_ROM, 0x40);
1904
1905		p = (char *)&vpdp;
1906		for (i = 0; i < sizeof(struct vpd_prom); i++)
1907			*p++ = read_sx_byte(board, SX_VPD_ROM + i * 2);
1908
1909		if (sx_debug & SX_DEBUG_PROBE)
1910			my_hd(&vpdp, 0x20);
1911
1912		sx_dprintk(SX_DEBUG_PROBE, "checking identifier...\n");
1913
1914		if (strncmp(vpdp.identifier, SX_VPD_IDENT_STRING, 16) != 0) {
1915			sx_dprintk(SX_DEBUG_PROBE, "Got non-SX identifier: "
1916					"'%s'\n", vpdp.identifier);
1917			return 0;
1918		}
1919	}
1920
1921	printheader();
1922
1923	if (!IS_CF_BOARD(board)) {
1924		printk(KERN_DEBUG "sx: Found an SX board at %lx\n",
1925			board->hw_base);
1926		printk(KERN_DEBUG "sx: hw_rev: %d, assembly level: %d, "
1927				"uniq ID:%08x, ",
1928				vpdp.hwrev, vpdp.hwass, vpdp.uniqid);
1929		printk("Manufactured: %d/%d\n", 1970 + vpdp.myear, vpdp.mweek);
1930
1931		if ((((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) !=
1932				SX_PCI_UNIQUEID1) && (((vpdp.uniqid >> 24) &
1933				SX_UNIQUEID_MASK) != SX_ISA_UNIQUEID1)) {
1934			/* This might be a bit harsh. This was the primary
1935			   reason the SX/ISA card didn't work at first... */
1936			printk(KERN_ERR "sx: Hmm. Not an SX/PCI or SX/ISA "
1937					"card. Sorry: giving up.\n");
1938			return (0);
1939		}
1940
1941		if (((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) ==
1942				SX_ISA_UNIQUEID1) {
1943			if (((unsigned long)board->hw_base) & 0x8000) {
1944				printk(KERN_WARNING "sx: Warning: There may be "
1945					"hardware problems with the card at "
1946					"%lx.\n", board->hw_base);
1947				printk(KERN_WARNING "sx: Read sx.txt for more "
1948						"info.\n");
1949			}
1950		}
1951	}
1952
1953	board->nports = -1;
1954
1955	/* This resets the processor, and keeps it off the bus. */
1956	if (!sx_reset(board))
1957		return 0;
1958	sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
1959
1960	func_exit();
1961	return 1;
1962}
1963
1964#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
1965
1966/* Specialix probes for this card at 32k increments from 640k to 16M.
1967   I consider machines with less than 16M unlikely nowadays, so I'm
1968   not probing above 1Mb. Also, 0xa0000, 0xb0000, are taken by the VGA
1969   card. 0xe0000 and 0xf0000 are taken by the BIOS. That only leaves
1970   0xc0000, 0xc8000, 0xd0000 and 0xd8000 . */
1971
1972static int __devinit probe_si(struct sx_board *board)
1973{
1974	int i;
1975
1976	func_enter();
1977	sx_dprintk(SX_DEBUG_PROBE, "Going to verify SI signature hw %lx at "
1978		"%p.\n", board->hw_base, board->base + SI2_ISA_ID_BASE);
1979
1980	if (sx_debug & SX_DEBUG_PROBE)
1981		my_hd_io(board->base + SI2_ISA_ID_BASE, 0x8);
1982
1983	if (!IS_EISA_BOARD(board)) {
1984		if (IS_SI1_BOARD(board)) {
1985			for (i = 0; i < 8; i++) {
1986				write_sx_byte(board, SI2_ISA_ID_BASE + 7 - i,i);
1987			}
1988		}
1989		for (i = 0; i < 8; i++) {
1990			if ((read_sx_byte(board, SI2_ISA_ID_BASE + 7 - i) & 7)
1991					!= i) {
1992				func_exit();
1993				return 0;
1994			}
1995		}
1996	}
1997
1998	/* Now we're pretty much convinced that there is an SI board here,
1999	   but to prevent trouble, we'd better double check that we don't
2000	   have an SI1 board when we're probing for an SI2 board.... */
2001
2002	write_sx_byte(board, SI2_ISA_ID_BASE, 0x10);
2003	if (IS_SI1_BOARD(board)) {
2004		/* This should be an SI1 board, which has this
2005		   location writable... */
2006		if (read_sx_byte(board, SI2_ISA_ID_BASE) != 0x10) {
2007			func_exit();
2008			return 0;
2009		}
2010	} else {
2011		/* This should be an SI2 board, which has the bottom
2012		   3 bits non-writable... */
2013		if (read_sx_byte(board, SI2_ISA_ID_BASE) == 0x10) {
2014			func_exit();
2015			return 0;
2016		}
2017	}
2018
2019	/* Now we're pretty much convinced that there is an SI board here,
2020	   but to prevent trouble, we'd better double check that we don't
2021	   have an SI1 board when we're probing for an SI2 board.... */
2022
2023	write_sx_byte(board, SI2_ISA_ID_BASE, 0x10);
2024	if (IS_SI1_BOARD(board)) {
2025		/* This should be an SI1 board, which has this
2026		   location writable... */
2027		if (read_sx_byte(board, SI2_ISA_ID_BASE) != 0x10) {
2028			func_exit();
2029			return 0;
2030		}
2031	} else {
2032		/* This should be an SI2 board, which has the bottom
2033		   3 bits non-writable... */
2034		if (read_sx_byte(board, SI2_ISA_ID_BASE) == 0x10) {
2035			func_exit();
2036			return 0;
2037		}
2038	}
2039
2040	printheader();
2041
2042	printk(KERN_DEBUG "sx: Found an SI board at %lx\n", board->hw_base);
2043	/* Compared to the SX boards, it is a complete guess as to what
2044	   this card is up to... */
2045
2046	board->nports = -1;
2047
2048	/* This resets the processor, and keeps it off the bus. */
2049	if (!sx_reset(board))
2050		return 0;
2051	sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
2052
2053	func_exit();
2054	return 1;
2055}
2056#endif
2057
2058static const struct tty_operations sx_ops = {
2059	.break_ctl = sx_break,
2060	.open = sx_open,
2061	.close = gs_close,
2062	.write = gs_write,
2063	.put_char = gs_put_char,
2064	.flush_chars = gs_flush_chars,
2065	.write_room = gs_write_room,
2066	.chars_in_buffer = gs_chars_in_buffer,
2067	.flush_buffer = gs_flush_buffer,
2068	.ioctl = sx_ioctl,
2069	.throttle = sx_throttle,
2070	.unthrottle = sx_unthrottle,
2071	.set_termios = gs_set_termios,
2072	.stop = gs_stop,
2073	.start = gs_start,
2074	.hangup = gs_hangup,
2075	.tiocmget = sx_tiocmget,
2076	.tiocmset = sx_tiocmset,
2077};
2078
2079static int sx_init_drivers(void)
2080{
2081	int error;
2082
2083	func_enter();
2084
2085	sx_driver = alloc_tty_driver(sx_nports);
2086	if (!sx_driver)
2087		return 1;
2088	sx_driver->owner = THIS_MODULE;
2089	sx_driver->driver_name = "specialix_sx";
2090	sx_driver->name = "ttyX";
2091	sx_driver->major = SX_NORMAL_MAJOR;
2092	sx_driver->type = TTY_DRIVER_TYPE_SERIAL;
2093	sx_driver->subtype = SERIAL_TYPE_NORMAL;
2094	sx_driver->init_termios = tty_std_termios;
2095	sx_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2096	sx_driver->init_termios.c_ispeed = 9600;
2097	sx_driver->init_termios.c_ospeed = 9600;
2098	sx_driver->flags = TTY_DRIVER_REAL_RAW;
2099	tty_set_operations(sx_driver, &sx_ops);
2100
2101	if ((error = tty_register_driver(sx_driver))) {
2102		put_tty_driver(sx_driver);
2103		printk(KERN_ERR "sx: Couldn't register sx driver, error = %d\n",
2104			error);
2105		return 1;
2106	}
2107	func_exit();
2108	return 0;
2109}
2110
2111static int sx_init_portstructs(int nboards, int nports)
2112{
2113	struct sx_board *board;
2114	struct sx_port *port;
2115	int i, j;
2116	int addr, chans;
2117	int portno;
2118
2119	func_enter();
2120
2121	/* Many drivers statically allocate the maximum number of ports
2122	   There is no reason not to allocate them dynamically.
2123	   Is there? -- REW */
2124	sx_ports = kcalloc(nports, sizeof(struct sx_port), GFP_KERNEL);
2125	if (!sx_ports)
2126		return -ENOMEM;
2127
2128	port = sx_ports;
2129	for (i = 0; i < nboards; i++) {
2130		board = &boards[i];
2131		board->ports = port;
2132		for (j = 0; j < boards[i].nports; j++) {
2133			sx_dprintk(SX_DEBUG_INIT, "initing port %d\n", j);
2134			port->gs.magic = SX_MAGIC;
2135			port->gs.close_delay = HZ / 2;
2136			port->gs.closing_wait = 30 * HZ;
2137			port->board = board;
2138			port->gs.rd = &sx_real_driver;
2139#ifdef NEW_WRITE_LOCKING
2140			port->gs.port_write_mutex = MUTEX;
2141#endif
2142			spin_lock_init(&port->gs.driver_lock);
2143			/*
2144			 * Initializing wait queue
2145			 */
2146			init_waitqueue_head(&port->gs.open_wait);
2147			init_waitqueue_head(&port->gs.close_wait);
2148
2149			port++;
2150		}
2151	}
2152
2153	port = sx_ports;
2154	portno = 0;
2155	for (i = 0; i < nboards; i++) {
2156		board = &boards[i];
2157		board->port_base = portno;
2158		/* Possibly the configuration was rejected. */
2159		sx_dprintk(SX_DEBUG_PROBE, "Board has %d channels\n",
2160				board->nports);
2161		if (board->nports <= 0)
2162			continue;
2163		for (addr = 0x80; addr != 0;
2164				addr = read_sx_word(board, addr) & 0x7fff) {
2165			chans = sx_read_module_byte(board, addr, mc_type);
2166			sx_dprintk(SX_DEBUG_PROBE, "Module at %x: %d "
2167					"channels\n", addr, chans);
2168			sx_dprintk(SX_DEBUG_PROBE, "Port at");
2169			for (j = 0; j < chans; j++) {
2170				/* The "sx-way" is the way it SHOULD be done.
2171				   That way in the future, the firmware may for
2172				   example pack the structures a bit more
2173				   efficient. Neil tells me it isn't going to
2174				   happen anytime soon though. */
2175				if (IS_SX_BOARD(board))
2176					port->ch_base = sx_read_module_word(
2177							board, addr + j * 2,
2178							mc_chan_pointer);
2179				else
2180					port->ch_base = addr + 0x100 + 0x300 *j;
2181
2182				sx_dprintk(SX_DEBUG_PROBE, " %x",
2183						port->ch_base);
2184				port->line = portno++;
2185				port++;
2186			}
2187			sx_dprintk(SX_DEBUG_PROBE, "\n");
2188		}
2189		/* This has to be done earlier. */
2190		/* board->flags |= SX_BOARD_INITIALIZED; */
2191	}
2192
2193	func_exit();
2194	return 0;
2195}
2196
2197static unsigned int sx_find_free_board(void)
2198{
2199	unsigned int i;
2200
2201	for (i = 0; i < SX_NBOARDS; i++)
2202		if (!(boards[i].flags & SX_BOARD_PRESENT))
2203			break;
2204
2205	return i;
2206}
2207
2208static void __exit sx_release_drivers(void)
2209{
2210	func_enter();
2211	tty_unregister_driver(sx_driver);
2212	put_tty_driver(sx_driver);
2213	func_exit();
2214}
2215
2216static void __devexit sx_remove_card(struct sx_board *board,
2217		struct pci_dev *pdev)
2218{
2219	if (board->flags & SX_BOARD_INITIALIZED) {
2220		/* The board should stop messing with us. (actually I mean the
2221		   interrupt) */
2222		sx_reset(board);
2223		if ((board->irq) && (board->flags & SX_IRQ_ALLOCATED))
2224			free_irq(board->irq, board);
2225
2226		/* It is safe/allowed to del_timer a non-active timer */
2227		del_timer(&board->timer);
2228		if (pdev) {
2229#ifdef CONFIG_PCI
2230			pci_iounmap(pdev, board->base);
2231			pci_release_region(pdev, IS_CF_BOARD(board) ? 3 : 2);
2232#endif
2233		} else {
2234			iounmap(board->base);
2235			release_region(board->hw_base, board->hw_len);
2236		}
2237
2238		board->flags &= ~(SX_BOARD_INITIALIZED | SX_BOARD_PRESENT);
2239	}
2240}
2241
2242#ifdef CONFIG_EISA
2243
2244static int __devinit sx_eisa_probe(struct device *dev)
2245{
2246	struct eisa_device *edev = to_eisa_device(dev);
2247	struct sx_board *board;
2248	unsigned long eisa_slot = edev->base_addr;
2249	unsigned int i;
2250	int retval = -EIO;
2251
2252	mutex_lock(&sx_boards_lock);
2253	i = sx_find_free_board();
2254	if (i == SX_NBOARDS) {
2255		mutex_unlock(&sx_boards_lock);
2256		goto err;
2257	}
2258	board = &boards[i];
2259	board->flags |= SX_BOARD_PRESENT;
2260	mutex_unlock(&sx_boards_lock);
2261
2262	dev_info(dev, "XIO : Signature found in EISA slot %lu, "
2263		 "Product %d Rev %d (REPORT THIS TO LKLM)\n",
2264		 eisa_slot >> 12,
2265		 inb(eisa_slot + EISA_VENDOR_ID_OFFSET + 2),
2266		 inb(eisa_slot + EISA_VENDOR_ID_OFFSET + 3));
2267
2268	board->eisa_base = eisa_slot;
2269	board->flags &= ~SX_BOARD_TYPE;
2270	board->flags |= SI_EISA_BOARD;
2271
2272	board->hw_base = ((inb(eisa_slot + 0xc01) << 8) +
2273			  inb(eisa_slot + 0xc00)) << 16;
2274	board->hw_len = SI2_EISA_WINDOW_LEN;
2275	if (!request_region(board->hw_base, board->hw_len, "sx")) {
2276		dev_err(dev, "can't request region\n");
2277		goto err_flag;
2278	}
2279	board->base2 =
2280	board->base = ioremap(board->hw_base, SI2_EISA_WINDOW_LEN);
2281	if (!board->base) {
2282		dev_err(dev, "can't remap memory\n");
2283		goto err_reg;
2284	}
2285
2286	sx_dprintk(SX_DEBUG_PROBE, "IO hw_base address: %lx\n", board->hw_base);
2287	sx_dprintk(SX_DEBUG_PROBE, "base: %p\n", board->base);
2288	board->irq = inb(eisa_slot + 0xc02) >> 4;
2289	sx_dprintk(SX_DEBUG_PROBE, "IRQ: %d\n", board->irq);
2290
2291	if (!probe_si(board))
2292		goto err_unmap;
2293
2294	dev_set_drvdata(dev, board);
2295
2296	return 0;
2297err_unmap:
2298	iounmap(board->base);
2299err_reg:
2300	release_region(board->hw_base, board->hw_len);
2301err_flag:
2302	board->flags &= ~SX_BOARD_PRESENT;
2303err:
2304	return retval;
2305}
2306
2307static int __devexit sx_eisa_remove(struct device *dev)
2308{
2309	struct sx_board *board = dev_get_drvdata(dev);
2310
2311	sx_remove_card(board, NULL);
2312
2313	return 0;
2314}
2315
2316static struct eisa_device_id sx_eisa_tbl[] = {
2317	{ "SLX" },
2318	{ "" }
2319};
2320
2321MODULE_DEVICE_TABLE(eisa, sx_eisa_tbl);
2322
2323static struct eisa_driver sx_eisadriver = {
2324	.id_table = sx_eisa_tbl,
2325	.driver = {
2326		.name = "sx",
2327		.probe = sx_eisa_probe,
2328		.remove = __devexit_p(sx_eisa_remove),
2329	}
2330};
2331
2332#endif
2333
2334#ifdef CONFIG_PCI
2335 /********************************************************
2336 * Setting bit 17 in the CNTRL register of the PLX 9050  *
2337 * chip forces a retry on writes while a read is pending.*
2338 * This is to prevent the card locking up on Intel Xeon  *
2339 * multiprocessor systems with the NX chipset.    -- NV  *
2340 ********************************************************/
2341
2342/* Newer cards are produced with this bit set from the configuration
2343   EEprom.  As the bit is read/write for the CPU, we can fix it here,
2344   if we detect that it isn't set correctly. -- REW */
2345
2346static void __devinit fix_sx_pci(struct pci_dev *pdev, struct sx_board *board)
2347{
2348	unsigned int hwbase;
2349	void __iomem *rebase;
2350	unsigned int t;
2351
2352#define CNTRL_REG_OFFSET	0x50
2353#define CNTRL_REG_GOODVALUE	0x18260000
2354
2355	pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &hwbase);
2356	hwbase &= PCI_BASE_ADDRESS_MEM_MASK;
2357	rebase = ioremap(hwbase, 0x80);
2358	t = readl(rebase + CNTRL_REG_OFFSET);
2359	if (t != CNTRL_REG_GOODVALUE) {
2360		printk(KERN_DEBUG "sx: performing cntrl reg fix: %08x -> "
2361			"%08x\n", t, CNTRL_REG_GOODVALUE);
2362		writel(CNTRL_REG_GOODVALUE, rebase + CNTRL_REG_OFFSET);
2363	}
2364	iounmap(rebase);
2365}
2366#endif
2367
2368static int __devinit sx_pci_probe(struct pci_dev *pdev,
2369				  const struct pci_device_id *ent)
2370{
2371#ifdef CONFIG_PCI
2372	struct sx_board *board;
2373	unsigned int i, reg;
2374	int retval = -EIO;
2375
2376	mutex_lock(&sx_boards_lock);
2377	i = sx_find_free_board();
2378	if (i == SX_NBOARDS) {
2379		mutex_unlock(&sx_boards_lock);
2380		goto err;
2381	}
2382	board = &boards[i];
2383	board->flags |= SX_BOARD_PRESENT;
2384	mutex_unlock(&sx_boards_lock);
2385
2386	retval = pci_enable_device(pdev);
2387	if (retval)
2388		goto err_flag;
2389
2390	board->flags &= ~SX_BOARD_TYPE;
2391	board->flags |= (pdev->subsystem_vendor == 0x200) ? SX_PCI_BOARD :
2392		SX_CFPCI_BOARD;
2393
2394	/* CF boards use base address 3.... */
2395	reg = IS_CF_BOARD(board) ? 3 : 2;
2396	retval = pci_request_region(pdev, reg, "sx");
2397	if (retval) {
2398		dev_err(&pdev->dev, "can't request region\n");
2399		goto err_flag;
2400	}
2401	board->hw_base = pci_resource_start(pdev, reg);
2402	board->base2 =
2403	board->base = pci_iomap(pdev, reg, WINDOW_LEN(board));
2404	if (!board->base) {
2405		dev_err(&pdev->dev, "ioremap failed\n");
2406		goto err_reg;
2407	}
2408
2409	/* Most of the stuff on the CF board is offset by 0x18000 ....  */
2410	if (IS_CF_BOARD(board))
2411		board->base += 0x18000;
2412
2413	board->irq = pdev->irq;
2414
2415	dev_info(&pdev->dev, "Got a specialix card: %p(%d) %x.\n", board->base,
2416		 board->irq, board->flags);
2417
2418	if (!probe_sx(board)) {
2419		retval = -EIO;
2420		goto err_unmap;
2421	}
2422
2423	fix_sx_pci(pdev, board);
2424
2425	pci_set_drvdata(pdev, board);
2426
2427	return 0;
2428err_unmap:
2429	pci_iounmap(pdev, board->base);
2430err_reg:
2431	pci_release_region(pdev, reg);
2432err_flag:
2433	board->flags &= ~SX_BOARD_PRESENT;
2434err:
2435	return retval;
2436#else
2437	return -ENODEV;
2438#endif
2439}
2440
2441static void __devexit sx_pci_remove(struct pci_dev *pdev)
2442{
2443	struct sx_board *board = pci_get_drvdata(pdev);
2444
2445	sx_remove_card(board, pdev);
2446}
2447
2448/* Specialix has a whole bunch of cards with 0x2000 as the device ID. They say
2449   its because the standard requires it. So check for SUBVENDOR_ID. */
2450static struct pci_device_id sx_pci_tbl[] = {
2451	{ PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8,
2452		.subvendor = 0x0200,.subdevice = PCI_ANY_ID },
2453	{ PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8,
2454		.subvendor = 0x0300,.subdevice = PCI_ANY_ID },
2455	{ 0 }
2456};
2457
2458MODULE_DEVICE_TABLE(pci, sx_pci_tbl);
2459
2460static struct pci_driver sx_pcidriver = {
2461	.name = "sx",
2462	.id_table = sx_pci_tbl,
2463	.probe = sx_pci_probe,
2464	.remove = __devexit_p(sx_pci_remove)
2465};
2466
2467static int __init sx_init(void)
2468{
2469#ifdef CONFIG_EISA
2470	int retval1;
2471#endif
2472#ifdef CONFIG_ISA
2473	struct sx_board *board;
2474	unsigned int i;
2475#endif
2476	unsigned int found = 0;
2477	int retval;
2478
2479	func_enter();
2480	sx_dprintk(SX_DEBUG_INIT, "Initing sx module... (sx_debug=%d)\n",
2481			sx_debug);
2482	if (abs((long)(&sx_debug) - sx_debug) < 0x10000) {
2483		printk(KERN_WARNING "sx: sx_debug is an address, instead of a "
2484				"value. Assuming -1.\n(%p)\n", &sx_debug);
2485		sx_debug = -1;
2486	}
2487
2488	if (misc_register(&sx_fw_device) < 0) {
2489		printk(KERN_ERR "SX: Unable to register firmware loader "
2490				"driver.\n");
2491		return -EIO;
2492	}
2493#ifdef CONFIG_ISA
2494	for (i = 0; i < NR_SX_ADDRS; i++) {
2495		board = &boards[found];
2496		board->hw_base = sx_probe_addrs[i];
2497		board->hw_len = SX_WINDOW_LEN;
2498		if (!request_region(board->hw_base, board->hw_len, "sx"))
2499			continue;
2500		board->base2 =
2501		board->base = ioremap(board->hw_base, board->hw_len);
2502		if (!board->base)
2503			goto err_sx_reg;
2504		board->flags &= ~SX_BOARD_TYPE;
2505		board->flags |= SX_ISA_BOARD;
2506		board->irq = sx_irqmask ? -1 : 0;
2507
2508		if (probe_sx(board)) {
2509			board->flags |= SX_BOARD_PRESENT;
2510			found++;
2511		} else {
2512			iounmap(board->base);
2513err_sx_reg:
2514			release_region(board->hw_base, board->hw_len);
2515		}
2516	}
2517
2518	for (i = 0; i < NR_SI_ADDRS; i++) {
2519		board = &boards[found];
2520		board->hw_base = si_probe_addrs[i];
2521		board->hw_len = SI2_ISA_WINDOW_LEN;
2522		if (!request_region(board->hw_base, board->hw_len, "sx"))
2523			continue;
2524		board->base2 =
2525		board->base = ioremap(board->hw_base, board->hw_len);
2526		if (!board->base)
2527			goto err_si_reg;
2528		board->flags &= ~SX_BOARD_TYPE;
2529		board->flags |= SI_ISA_BOARD;
2530		board->irq = sx_irqmask ? -1 : 0;
2531
2532		if (probe_si(board)) {
2533			board->flags |= SX_BOARD_PRESENT;
2534			found++;
2535		} else {
2536			iounmap(board->base);
2537err_si_reg:
2538			release_region(board->hw_base, board->hw_len);
2539		}
2540	}
2541	for (i = 0; i < NR_SI1_ADDRS; i++) {
2542		board = &boards[found];
2543		board->hw_base = si1_probe_addrs[i];
2544		board->hw_len = SI1_ISA_WINDOW_LEN;
2545		if (!request_region(board->hw_base, board->hw_len, "sx"))
2546			continue;
2547		board->base2 =
2548		board->base = ioremap(board->hw_base, board->hw_len);
2549		if (!board->base)
2550			goto err_si1_reg;
2551		board->flags &= ~SX_BOARD_TYPE;
2552		board->flags |= SI1_ISA_BOARD;
2553		board->irq = sx_irqmask ? -1 : 0;
2554
2555		if (probe_si(board)) {
2556			board->flags |= SX_BOARD_PRESENT;
2557			found++;
2558		} else {
2559			iounmap(board->base);
2560err_si1_reg:
2561			release_region(board->hw_base, board->hw_len);
2562		}
2563	}
2564#endif
2565#ifdef CONFIG_EISA
2566	retval1 = eisa_driver_register(&sx_eisadriver);
2567#endif
2568	retval = pci_register_driver(&sx_pcidriver);
2569
2570	if (found) {
2571		printk(KERN_INFO "sx: total of %d boards detected.\n", found);
2572		retval = 0;
2573	} else if (retval) {
2574#ifdef CONFIG_EISA
2575		retval = retval1;
2576		if (retval1)
2577#endif
2578			misc_deregister(&sx_fw_device);
2579	}
2580
2581	func_exit();
2582	return retval;
2583}
2584
2585static void __exit sx_exit(void)
2586{
2587	int i;
2588
2589	func_enter();
2590#ifdef CONFIG_EISA
2591	eisa_driver_unregister(&sx_eisadriver);
2592#endif
2593	pci_unregister_driver(&sx_pcidriver);
2594
2595	for (i = 0; i < SX_NBOARDS; i++)
2596		sx_remove_card(&boards[i], NULL);
2597
2598	if (misc_deregister(&sx_fw_device) < 0) {
2599		printk(KERN_INFO "sx: couldn't deregister firmware loader "
2600				"device\n");
2601	}
2602	sx_dprintk(SX_DEBUG_CLEANUP, "Cleaning up drivers (%d)\n",
2603			sx_initialized);
2604	if (sx_initialized)
2605		sx_release_drivers();
2606
2607	kfree(sx_ports);
2608	func_exit();
2609}
2610
2611module_init(sx_init);
2612module_exit(sx_exit);
2613