File: /usr/src/linux/drivers/char/sx.c

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