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