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