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