Merge branch 'origin'
[pandora-kernel.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 - 2004  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 ARRAY_SIZE(sx_probe_addrs)
349 #define NR_SI_ADDRS ARRAY_SIZE(si_probe_addrs)
350 #define NR_SI1_ADDRS ARRAY_SIZE(si1_probe_addrs)
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_param_array(sx_probe_addrs, int, NULL, 0);
358 module_param_array(si_probe_addrs, int, NULL, 0);
359 module_param(sx_poll, int, 0);
360 module_param(sx_slowpoll, int, 0);
361 module_param(sx_maxints, int, 0);
362 module_param(sx_debug, int, 0);
363 module_param(sx_irqmask, int, 0);
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\n",__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: %u\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         unsigned char *rp;
1089
1090         func_enter2 ();
1091         tty = port->gs.tty;
1092         while (1) {
1093                 rx_op = sx_read_channel_byte (port, hi_rxopos);
1094                 c = (sx_read_channel_byte (port, hi_rxipos) - rx_op) & 0xff;
1095
1096                 sx_dprintk (SX_DEBUG_RECEIVE, "rxop=%d, c = %d.\n", rx_op, c); 
1097
1098                 /* Don't copy past the end of the hardware receive buffer */
1099                 if (rx_op + c > 0x100) c = 0x100 - rx_op;
1100
1101                 sx_dprintk (SX_DEBUG_RECEIVE, "c = %d.\n", c);
1102
1103                 /* Don't copy more bytes than there is room for in the buffer */
1104
1105                 c = tty_prepare_flip_string(tty, &rp, c);
1106
1107                 sx_dprintk (SX_DEBUG_RECEIVE, "c = %d.\n", c); 
1108
1109                 /* If for one reason or another, we can't copy more data, we're done! */
1110                 if (c == 0) break;
1111
1112                 sx_dprintk (SX_DEBUG_RECEIVE , "Copying over %d chars. First is %d at %lx\n", c, 
1113                             read_sx_byte (port->board, CHAN_OFFSET(port,hi_rxbuf) + rx_op),
1114                             CHAN_OFFSET(port, hi_rxbuf)); 
1115                 memcpy_fromio (rp,
1116                                port->board->base + CHAN_OFFSET(port,hi_rxbuf) + rx_op, c);
1117
1118                 /* This one last. ( Not essential.)
1119                    It allows the card to start putting more data into the buffer! 
1120                    Update the pointer in the card */
1121                 sx_write_channel_byte (port, hi_rxopos, (rx_op + c) & 0xff);
1122
1123                 copied += c;
1124         }
1125         if (copied) {
1126                 struct timeval tv;
1127
1128                 do_gettimeofday (&tv);
1129                 sx_dprintk (SX_DEBUG_RECEIVE, 
1130                             "pushing flipq port %d (%3d chars): %d.%06d  (%d/%d)\n", 
1131                             port->line, copied, 
1132                             (int) (tv.tv_sec % 60), (int)tv.tv_usec, tty->raw, tty->real_raw);
1133
1134                 /* Tell the rest of the system the news. Great news. New characters! */
1135                 tty_flip_buffer_push (tty);
1136                 /*    tty_schedule_flip (tty); */
1137         }
1138
1139         func_exit ();
1140 }
1141
1142 /* Inlined: it is called only once. Remove the inline if you add another 
1143    call */
1144 static inline void sx_check_modem_signals (struct sx_port *port)
1145 {
1146         int hi_state;
1147         int c_dcd;
1148
1149         hi_state = sx_read_channel_byte (port, hi_state);
1150         sx_dprintk (SX_DEBUG_MODEMSIGNALS, "Checking modem signals (%d/%d)\n",
1151                     port->c_dcd, sx_get_CD (port));
1152
1153         if (hi_state & ST_BREAK) {
1154                 hi_state &= ~ST_BREAK;
1155                 sx_dprintk (SX_DEBUG_MODEMSIGNALS, "got a break.\n");
1156                 sx_write_channel_byte (port, hi_state, hi_state);
1157                 gs_got_break (&port->gs);
1158         }
1159         if (hi_state & ST_DCD) {
1160                 hi_state &= ~ST_DCD;
1161                 sx_dprintk (SX_DEBUG_MODEMSIGNALS, "got a DCD change.\n");
1162                 sx_write_channel_byte (port, hi_state, hi_state);
1163                 c_dcd = sx_get_CD (port);
1164                 sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD is now %d\n", c_dcd);
1165                 if (c_dcd != port->c_dcd) {
1166                         port->c_dcd = c_dcd;
1167                         if (sx_get_CD (port)) {
1168                                 /* DCD went UP */
1169                                 if ((sx_read_channel_byte(port, hi_hstat) != HS_IDLE_CLOSED) &&
1170                                                 !(port->gs.tty->termios->c_cflag & CLOCAL) ) {
1171                                         /* Are we blocking in open?*/
1172                                         sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD active, unblocking open\n");
1173                                         wake_up_interruptible(&port->gs.open_wait);
1174                                 } else {
1175                                         sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD raised. Ignoring.\n");
1176                                 }
1177                         } else {
1178                                 /* DCD went down! */
1179                                 if (!(port->gs.tty->termios->c_cflag & CLOCAL) ) {
1180                                         sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD dropped. hanging up....\n");
1181                                         tty_hangup (port->gs.tty);
1182                                 } else {
1183                                         sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD dropped. ignoring.\n");
1184                                 }
1185                         }
1186                 } else {
1187                         sx_dprintk (SX_DEBUG_MODEMSIGNALS, "Hmmm. card told us DCD changed, but it didn't.\n");
1188                 }
1189         }
1190 }
1191
1192
1193 /* This is what an interrupt routine should look like. 
1194  * Small, elegant, clear.
1195  */
1196
1197 static irqreturn_t sx_interrupt (int irq, void *ptr, struct pt_regs *regs)
1198 {
1199         struct sx_board *board = ptr;
1200         struct sx_port *port;
1201         int i;
1202
1203         func_enter ();
1204         sx_dprintk (SX_DEBUG_FLOW, "sx: enter sx_interrupt (%d/%d)\n", irq, board->irq); 
1205
1206         /* AAargh! The order in which to do these things is essential and
1207            not trivial. 
1208
1209            - Rate limit goes before "recursive". Otherwise a series of
1210              recursive calls will hang the machine in the interrupt routine. 
1211
1212            - hardware twiddling goes before "recursive". Otherwise when we
1213              poll the card, and a recursive interrupt happens, we won't
1214              ack the card, so it might keep on interrupting us. (especially
1215              level sensitive interrupt systems like PCI).
1216
1217            - Rate limit goes before hardware twiddling. Otherwise we won't
1218              catch a card that has gone bonkers.
1219
1220            - The "initialized" test goes after the hardware twiddling. Otherwise
1221              the card will stick us in the interrupt routine again.
1222
1223            - The initialized test goes before recursive. 
1224         */
1225
1226
1227
1228 #ifdef IRQ_RATE_LIMIT
1229         /* Aaargh! I'm ashamed. This costs more lines-of-code than the
1230            actual interrupt routine!. (Well, used to when I wrote that comment) */
1231         {
1232                 static int lastjif;
1233                 static int nintr=0;
1234
1235                 if (lastjif == jiffies) {
1236                         if (++nintr > IRQ_RATE_LIMIT) {
1237                                 free_irq (board->irq, board);
1238                                 printk (KERN_ERR "sx: Too many interrupts. Turning off interrupt %d.\n", 
1239                                               board->irq);
1240                         }
1241                 } else {
1242                         lastjif = jiffies;
1243                         nintr = 0;
1244                 }
1245         }
1246 #endif
1247
1248
1249         if (board->irq == irq) {
1250                 /* Tell the card we've noticed the interrupt. */
1251
1252                 sx_write_board_word (board, cc_int_pending, 0);
1253                 if (IS_SX_BOARD (board)) {
1254                         write_sx_byte (board, SX_RESET_IRQ, 1);
1255                 } else if (IS_EISA_BOARD(board)) {
1256                         inb(board->eisa_base+0xc03);
1257                         write_sx_word(board, 8, 0); 
1258                 } else {
1259                         write_sx_byte (board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_CLEAR);
1260                         write_sx_byte (board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
1261                 }
1262         }
1263
1264         if (!sx_initialized)
1265                 return IRQ_HANDLED;
1266         if (!(board->flags & SX_BOARD_INITIALIZED))
1267                 return IRQ_HANDLED;
1268
1269         if (test_and_set_bit (SX_BOARD_INTR_LOCK, &board->locks)) {
1270                 printk (KERN_ERR "Recursive interrupt! (%d)\n", board->irq);
1271                 return IRQ_HANDLED;
1272         }
1273
1274          for (i=0;i<board->nports;i++) {
1275                 port = &board->ports[i];
1276                 if (port->gs.flags & GS_ACTIVE) {
1277                         if (sx_read_channel_byte (port, hi_state)) {
1278                                 sx_dprintk (SX_DEBUG_INTERRUPTS, 
1279                                             "Port %d: modem signal change?... \n", i);
1280                                 sx_check_modem_signals (port); 
1281                         }
1282                         if (port->gs.xmit_cnt) {
1283                                 sx_transmit_chars (port);
1284                         }
1285                         if (!(port->gs.flags & SX_RX_THROTTLE)) {
1286                                 sx_receive_chars (port);
1287                         }
1288                 }
1289         }
1290
1291         clear_bit (SX_BOARD_INTR_LOCK, &board->locks);
1292
1293         sx_dprintk (SX_DEBUG_FLOW, "sx: exit sx_interrupt (%d/%d)\n", irq, board->irq); 
1294         func_exit ();
1295         return IRQ_HANDLED;
1296 }
1297
1298
1299 static void sx_pollfunc (unsigned long data)
1300 {
1301         struct sx_board *board = (struct sx_board *) data;
1302
1303         func_enter ();
1304
1305         sx_interrupt (0, board, NULL);
1306
1307         init_timer(&board->timer);
1308
1309         board->timer.expires = jiffies + sx_poll;
1310         add_timer (&board->timer);
1311         func_exit ();
1312 }
1313
1314
1315
1316 /* ********************************************************************** *
1317  *                Here are the routines that actually                     *
1318  *              interface with the generic_serial driver                  *
1319  * ********************************************************************** */
1320
1321 /* Ehhm. I don't know how to fiddle with interrupts on the SX card. --REW */
1322 /* Hmm. Ok I figured it out. You don't.  */
1323
1324 static void sx_disable_tx_interrupts (void * ptr) 
1325 {
1326         struct sx_port *port = ptr; 
1327         func_enter2();
1328
1329         port->gs.flags &= ~GS_TX_INTEN;
1330
1331         func_exit();
1332 }
1333
1334
1335 static void sx_enable_tx_interrupts (void * ptr) 
1336 {
1337         struct sx_port *port = ptr; 
1338         int data_in_buffer;
1339         func_enter2();
1340
1341         /* First transmit the characters that we're supposed to */
1342         sx_transmit_chars (port);
1343
1344         /* The sx card will never interrupt us if we don't fill the buffer
1345            past 25%. So we keep considering interrupts off if that's the case. */
1346         data_in_buffer = (sx_read_channel_byte (port, hi_txipos) - 
1347                           sx_read_channel_byte (port, hi_txopos)) & 0xff;
1348
1349         /* XXX Must be "HIGH_WATER" for SI card according to doc. */
1350         if (data_in_buffer < LOW_WATER) 
1351                 port->gs.flags &= ~GS_TX_INTEN;
1352
1353         func_exit();
1354 }
1355
1356
1357 static void sx_disable_rx_interrupts (void * ptr) 
1358 {
1359         /*  struct sx_port *port = ptr; */
1360         func_enter();
1361
1362         func_exit();
1363 }
1364
1365 static void sx_enable_rx_interrupts (void * ptr) 
1366 {
1367         /*  struct sx_port *port = ptr; */
1368         func_enter();
1369
1370         func_exit();
1371 }
1372
1373
1374 /* Jeez. Isn't this simple? */
1375 static int sx_get_CD (void * ptr) 
1376 {
1377         struct sx_port *port = ptr;
1378         func_enter2();
1379
1380         func_exit();
1381         return ((sx_read_channel_byte (port, hi_ip) & IP_DCD) != 0);
1382 }
1383
1384
1385 /* Jeez. Isn't this simple? */
1386 static int sx_chars_in_buffer (void * ptr) 
1387 {
1388         struct sx_port *port = ptr;
1389         func_enter2();
1390
1391         func_exit();
1392         return ((sx_read_channel_byte (port, hi_txipos) - 
1393                  sx_read_channel_byte (port, hi_txopos)) & 0xff);
1394 }
1395
1396
1397 static void sx_shutdown_port (void * ptr) 
1398 {
1399         struct sx_port *port = ptr; 
1400
1401         func_enter();
1402
1403         port->gs.flags &= ~ GS_ACTIVE;
1404         if (port->gs.tty && (port->gs.tty->termios->c_cflag & HUPCL)) {
1405                 sx_setsignals (port, 0, 0);
1406                 sx_reconfigure_port(port);
1407         }
1408
1409         func_exit();
1410 }
1411
1412
1413
1414
1415
1416 /* ********************************************************************** *
1417  *                Here are the routines that actually                     *
1418  *               interface with the rest of the system                    *
1419  * ********************************************************************** */
1420
1421 static int sx_open  (struct tty_struct * tty, struct file * filp)
1422 {
1423         struct sx_port *port;
1424         int retval, line;
1425         unsigned long flags;
1426
1427         func_enter();
1428
1429         if (!sx_initialized) {
1430                 return -EIO;
1431         }
1432
1433         line = tty->index;
1434         sx_dprintk (SX_DEBUG_OPEN, "%d: opening line %d. tty=%p ctty=%p, np=%d)\n", 
1435                     current->pid, line, tty, current->signal->tty, sx_nports);
1436
1437         if ((line < 0) || (line >= SX_NPORTS) || (line >= sx_nports))
1438                 return -ENODEV;
1439
1440         port = & sx_ports[line];
1441         port->c_dcd = 0; /* Make sure that the first interrupt doesn't detect a
1442                             1 -> 0 transition. */
1443
1444
1445         sx_dprintk (SX_DEBUG_OPEN, "port = %p c_dcd = %d\n", port, port->c_dcd);
1446
1447         spin_lock_irqsave(&port->gs.driver_lock, flags);
1448
1449         tty->driver_data = port;
1450         port->gs.tty = tty;
1451         port->gs.count++;
1452         spin_unlock_irqrestore(&port->gs.driver_lock, flags);
1453
1454         sx_dprintk (SX_DEBUG_OPEN, "starting port\n");
1455
1456         /*
1457          * Start up serial port
1458          */
1459         retval = gs_init_port(&port->gs);
1460         sx_dprintk (SX_DEBUG_OPEN, "done gs_init\n");
1461         if (retval) {
1462                 port->gs.count--;
1463                 return retval;
1464         }
1465
1466         port->gs.flags |= GS_ACTIVE;
1467         if (port->gs.count <= 1)
1468                 sx_setsignals (port, 1,1);
1469
1470 #if 0
1471         if (sx_debug & SX_DEBUG_OPEN)
1472                 my_hd (port, sizeof (*port));
1473 #else
1474         if (sx_debug & SX_DEBUG_OPEN)
1475                 my_hd_io (port->board->base + port->ch_base, sizeof (*port));
1476 #endif
1477
1478         if (port->gs.count <= 1) {
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                         spin_lock_irqsave(&port->gs.driver_lock, flags);
1482                         port->gs.count--;
1483                         spin_unlock_irqrestore(&port->gs.driver_lock, flags);
1484                         return -EIO;
1485                 }
1486         }
1487
1488         retval = gs_block_til_ready(port, filp);
1489         sx_dprintk (SX_DEBUG_OPEN, "Block til ready returned %d. Count=%d\n", 
1490                     retval, port->gs.count);
1491
1492         if (retval) {
1493                 /* 
1494                  * Don't lower gs.count here because sx_close() will be called later
1495                  */ 
1496
1497                 return retval;
1498         }
1499         /* tty->low_latency = 1; */
1500
1501         port->c_dcd = sx_get_CD (port);
1502         sx_dprintk (SX_DEBUG_OPEN, "at open: cd=%d\n", port->c_dcd);
1503
1504         func_exit();
1505         return 0;
1506
1507 }
1508
1509
1510 static void sx_close (void *ptr)
1511 {
1512         struct sx_port *port = ptr; 
1513         /* Give the port 5 seconds to close down. */
1514         int to = 5 * HZ; 
1515
1516         func_enter ();
1517
1518         sx_setsignals (port, 0, 0);
1519         sx_reconfigure_port(port);      
1520         sx_send_command (port, HS_CLOSE, 0, 0);
1521
1522         while (to-- && (sx_read_channel_byte (port, hi_hstat) != HS_IDLE_CLOSED))
1523                 if (msleep_interruptible(10))
1524                         break;
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                 //printk ("%s SETTING port count to zero: %p count: %d\n", __FUNCTION__, port, port->gs.count);
1539                 //port->gs.count = 0;
1540         }
1541
1542         func_exit ();
1543 }
1544
1545
1546
1547 /* This is relatively thorough. But then again it is only 20 lines. */
1548 #define MARCHUP    for (i=min;i<max;i++) 
1549 #define MARCHDOWN  for (i=max-1;i>=min;i--)
1550 #define W0         write_sx_byte (board, i, 0x55)
1551 #define W1         write_sx_byte (board, i, 0xaa)
1552 #define R0         if (read_sx_byte (board, i) != 0x55) return 1
1553 #define R1         if (read_sx_byte (board, i) != 0xaa) return 1
1554
1555 /* This memtest takes a human-noticable time. You normally only do it
1556    once a boot, so I guess that it is worth it. */
1557 static int do_memtest (struct sx_board *board, int min, int max)
1558 {
1559         int i;
1560
1561         /* This is a marchb. Theoretically, marchb catches much more than
1562            simpler tests. In practise, the longer test just catches more
1563            intermittent errors. -- REW
1564            (For the theory behind memory testing see: 
1565            Testing Semiconductor Memories by A.J. van de Goor.) */
1566         MARCHUP  {W0;}
1567         MARCHUP   {R0;W1;R1;W0;R0;W1;}
1568         MARCHUP   {R1;W0;W1;}
1569         MARCHDOWN {R1;W0;W1;W0;}
1570         MARCHDOWN {R0;W1;W0;}
1571
1572         return 0;
1573 }
1574
1575
1576 #undef MARCHUP
1577 #undef MARCHDOWN
1578 #undef W0
1579 #undef W1
1580 #undef R0
1581 #undef R1
1582
1583 #define MARCHUP    for (i=min;i<max;i+=2) 
1584 #define MARCHDOWN  for (i=max-1;i>=min;i-=2)
1585 #define W0         write_sx_word (board, i, 0x55aa)
1586 #define W1         write_sx_word (board, i, 0xaa55)
1587 #define R0         if (read_sx_word (board, i) != 0x55aa) return 1
1588 #define R1         if (read_sx_word (board, i) != 0xaa55) return 1
1589
1590 #if 0
1591 /* This memtest takes a human-noticable time. You normally only do it
1592    once a boot, so I guess that it is worth it. */
1593 static int do_memtest_w (struct sx_board *board, int min, int max)
1594 {
1595         int i;
1596
1597         MARCHUP   {W0;}
1598         MARCHUP   {R0;W1;R1;W0;R0;W1;}
1599         MARCHUP   {R1;W0;W1;}
1600         MARCHDOWN {R1;W0;W1;W0;}
1601         MARCHDOWN {R0;W1;W0;}
1602
1603         return 0;
1604 }
1605 #endif
1606
1607
1608 static int sx_fw_ioctl (struct inode *inode, struct file *filp,
1609                         unsigned int cmd, unsigned long arg)
1610 {
1611         int rc = 0;
1612         int __user *descr = (int __user *)arg;
1613         int i;
1614         static struct sx_board *board = NULL;
1615         int nbytes, offset;
1616         unsigned long data;
1617         char *tmp;
1618
1619         func_enter();
1620
1621 #if 0 
1622         /* Removed superuser check: Sysops can use the permissions on the device
1623            file to restrict access. Recommendation: Root only. (root.root 600) */
1624         if (!capable(CAP_SYS_ADMIN)) {
1625                 return -EPERM;
1626         }
1627 #endif
1628
1629         sx_dprintk (SX_DEBUG_FIRMWARE, "IOCTL %x: %lx\n", cmd, arg);
1630
1631         if (!board) board = &boards[0];
1632         if (board->flags & SX_BOARD_PRESENT) {
1633                 sx_dprintk (SX_DEBUG_FIRMWARE, "Board present! (%x)\n", 
1634                             board->flags);
1635         } else {
1636                 sx_dprintk (SX_DEBUG_FIRMWARE, "Board not present! (%x) all:", 
1637                             board->flags);
1638                 for (i=0;i< SX_NBOARDS;i++)
1639                         sx_dprintk (SX_DEBUG_FIRMWARE, "<%x> ", boards[i].flags);
1640                 sx_dprintk (SX_DEBUG_FIRMWARE, "\n");
1641                 return -EIO;
1642         }
1643
1644         switch (cmd) {
1645         case SXIO_SET_BOARD:
1646                 sx_dprintk (SX_DEBUG_FIRMWARE, "set board to %ld\n", arg);
1647                 if (arg >= SX_NBOARDS) return -EIO;
1648                 sx_dprintk (SX_DEBUG_FIRMWARE, "not out of range\n");
1649                 if (!(boards[arg].flags & SX_BOARD_PRESENT)) return -EIO;
1650                 sx_dprintk (SX_DEBUG_FIRMWARE, ".. and present!\n");
1651                 board = &boards[arg];
1652                 break;
1653         case SXIO_GET_TYPE:
1654                 rc = -ENOENT; /* If we manage to miss one, return error. */
1655                 if (IS_SX_BOARD (board)) rc = SX_TYPE_SX;
1656                 if (IS_CF_BOARD (board)) rc = SX_TYPE_CF;
1657                 if (IS_SI_BOARD (board)) rc = SX_TYPE_SI;
1658                 if (IS_SI1_BOARD (board)) rc = SX_TYPE_SI;
1659                 if (IS_EISA_BOARD (board)) rc = SX_TYPE_SI;
1660                 sx_dprintk (SX_DEBUG_FIRMWARE, "returning type= %d\n", rc);
1661                 break;
1662         case SXIO_DO_RAMTEST:
1663                 if (sx_initialized) /* Already initialized: better not ramtest the board.  */
1664                         return -EPERM;
1665                 if (IS_SX_BOARD (board)) {
1666                         rc          = do_memtest   (board, 0, 0x7000);
1667                         if (!rc) rc = do_memtest   (board, 0, 0x7000);
1668                         /*if (!rc) rc = do_memtest_w (board, 0, 0x7000);*/
1669                 } else {
1670                         rc             = do_memtest   (board, 0, 0x7ff8);
1671                         /* if (!rc) rc = do_memtest_w (board, 0, 0x7ff8); */
1672                 }
1673                 sx_dprintk (SX_DEBUG_FIRMWARE, "returning memtest result= %d\n", rc);
1674                 break;
1675         case SXIO_DOWNLOAD:
1676                 if (sx_initialized) /* Already initialized */
1677                         return -EEXIST;
1678                 if (!sx_reset (board)) 
1679                         return -EIO;
1680                 sx_dprintk (SX_DEBUG_INIT, "reset the board...\n");
1681
1682                 tmp = kmalloc (SX_CHUNK_SIZE, GFP_USER);
1683                 if (!tmp) return -ENOMEM;
1684                 get_user (nbytes, descr++);
1685                 get_user (offset, descr++); 
1686                 get_user (data,  descr++);
1687                 while (nbytes && data) {
1688                         for (i=0;i<nbytes;i += SX_CHUNK_SIZE) {
1689                                 if (copy_from_user(tmp, (char __user *)data+i, 
1690                                                    (i + SX_CHUNK_SIZE >
1691                                                     nbytes) ? nbytes - i :
1692                                                               SX_CHUNK_SIZE)) {
1693                                         kfree (tmp);
1694                                         return -EFAULT;
1695                                 }
1696                                 memcpy_toio(board->base2 + offset + i, tmp, 
1697                                                 (i+SX_CHUNK_SIZE>nbytes)?nbytes-i:SX_CHUNK_SIZE);
1698                         }
1699
1700                         get_user (nbytes, descr++);
1701                         get_user (offset, descr++); 
1702                         get_user (data,   descr++);
1703                 }
1704                 kfree (tmp);
1705                 sx_nports += sx_init_board (board);
1706                 rc = sx_nports;
1707                 break;
1708         case SXIO_INIT:
1709                 if (sx_initialized) /* Already initialized */
1710                         return -EEXIST;
1711                 /* This is not allowed until all boards are initialized... */
1712                 for (i=0;i<SX_NBOARDS;i++) {
1713                         if ( (boards[i].flags & SX_BOARD_PRESENT) &&
1714                              !(boards[i].flags & SX_BOARD_INITIALIZED))
1715                                 return -EIO;
1716                 }
1717                 for (i=0;i<SX_NBOARDS;i++)
1718                         if (!(boards[i].flags & SX_BOARD_PRESENT)) break;
1719
1720                 sx_dprintk (SX_DEBUG_FIRMWARE, "initing portstructs, %d boards, "
1721                             "%d channels, first board: %d ports\n", 
1722                             i, sx_nports, boards[0].nports);
1723                 rc = sx_init_portstructs (i, sx_nports);
1724                 sx_init_drivers ();
1725                 if (rc >= 0) 
1726                         sx_initialized++;
1727                 break;
1728         case SXIO_SETDEBUG:
1729                 sx_debug = arg;
1730                 break;
1731         case SXIO_GETDEBUG:
1732                 rc = sx_debug;
1733                 break;
1734         case SXIO_GETGSDEBUG:
1735         case SXIO_SETGSDEBUG:
1736                 rc = -EINVAL;
1737                 break;
1738         case SXIO_GETNPORTS:
1739                 rc = sx_nports;
1740                 break;
1741         default:
1742                 printk (KERN_WARNING "Unknown ioctl on firmware device (%x).\n", cmd);
1743                 break;
1744         }
1745         func_exit ();
1746         return rc;
1747 }
1748
1749
1750 static void sx_break (struct tty_struct * tty, int flag)
1751 {
1752         struct sx_port *port = tty->driver_data;
1753         int rv;
1754
1755         func_enter ();
1756
1757         if (flag) 
1758                 rv = sx_send_command (port, HS_START, -1, HS_IDLE_BREAK);
1759         else 
1760                 rv = sx_send_command (port, HS_STOP, -1, HS_IDLE_OPEN);
1761         if (rv != 1) printk (KERN_ERR "sx: couldn't send break (%x).\n",
1762                         read_sx_byte (port->board, CHAN_OFFSET (port, hi_hstat)));
1763
1764         func_exit ();
1765 }
1766
1767
1768 static int sx_tiocmget(struct tty_struct *tty, struct file *file)
1769 {
1770         struct sx_port *port = tty->driver_data;
1771         return sx_getsignals(port);
1772 }
1773
1774 static int sx_tiocmset(struct tty_struct *tty, struct file *file,
1775                        unsigned int set, unsigned int clear)
1776 {
1777         struct sx_port *port = tty->driver_data;
1778         int rts = -1, dtr = -1;
1779
1780         if (set & TIOCM_RTS)
1781                 rts = 1;
1782         if (set & TIOCM_DTR)
1783                 dtr = 1;
1784         if (clear & TIOCM_RTS)
1785                 rts = 0;
1786         if (clear & TIOCM_DTR)
1787                 dtr = 0;
1788
1789         sx_setsignals(port, dtr, rts);
1790         sx_reconfigure_port(port);
1791         return 0;
1792 }
1793
1794 static int sx_ioctl (struct tty_struct * tty, struct file * filp, 
1795                      unsigned int cmd, unsigned long arg)
1796 {
1797         int rc;
1798         struct sx_port *port = tty->driver_data;
1799         void __user *argp = (void __user *)arg;
1800         int ival;
1801
1802         /* func_enter2(); */
1803
1804         rc = 0;
1805         switch (cmd) {
1806         case TIOCGSOFTCAR:
1807                 rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
1808                               (unsigned __user *) argp);
1809                 break;
1810         case TIOCSSOFTCAR:
1811                 if ((rc = get_user(ival, (unsigned __user *) argp)) == 0) {
1812                         tty->termios->c_cflag =
1813                                 (tty->termios->c_cflag & ~CLOCAL) |
1814                                 (ival ? CLOCAL : 0);
1815                 }
1816                 break;
1817         case TIOCGSERIAL:
1818                 rc = gs_getserial(&port->gs, argp);
1819                 break;
1820         case TIOCSSERIAL:
1821                 rc = gs_setserial(&port->gs, argp);
1822                 break;
1823         default:
1824                 rc = -ENOIOCTLCMD;
1825                 break;
1826         }
1827
1828         /* func_exit(); */
1829         return rc;
1830 }
1831
1832
1833 /* The throttle/unthrottle scheme for the Specialix card is different
1834  * from other drivers and deserves some explanation. 
1835  * The Specialix hardware takes care of XON/XOFF
1836  * and CTS/RTS flow control itself.  This means that all we have to
1837  * do when signalled by the upper tty layer to throttle/unthrottle is
1838  * to make a note of it here.  When we come to read characters from the
1839  * rx buffers on the card (sx_receive_chars()) we look to see if the
1840  * upper layer can accept more (as noted here in sx_rx_throt[]). 
1841  * If it can't we simply don't remove chars from the cards buffer. 
1842  * When the tty layer can accept chars, we again note that here and when
1843  * sx_receive_chars() is called it will remove them from the cards buffer.
1844  * The card will notice that a ports buffer has drained below some low
1845  * water mark and will unflow control the line itself, using whatever
1846  * flow control scheme is in use for that port. -- Simon Allen
1847  */
1848
1849 static void sx_throttle (struct tty_struct * tty)
1850 {
1851         struct sx_port *port = (struct sx_port *)tty->driver_data;
1852
1853         func_enter2();
1854         /* If the port is using any type of input flow
1855          * control then throttle the port.
1856          */
1857         if((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty)) ) {
1858                 port->gs.flags |= SX_RX_THROTTLE;
1859         }
1860         func_exit();
1861 }
1862
1863
1864 static void sx_unthrottle (struct tty_struct * tty)
1865 {
1866         struct sx_port *port = (struct sx_port *)tty->driver_data;
1867
1868         func_enter2();
1869         /* Always unthrottle even if flow control is not enabled on
1870          * this port in case we disabled flow control while the port
1871          * was throttled
1872          */
1873         port->gs.flags &= ~SX_RX_THROTTLE;
1874         func_exit();
1875         return;
1876 }
1877
1878
1879 /* ********************************************************************** *
1880  *                    Here are the initialization routines.               *
1881  * ********************************************************************** */
1882
1883
1884
1885
1886 static int sx_init_board (struct sx_board *board)
1887 {
1888         int addr;
1889         int chans;
1890         int type;
1891
1892         func_enter();
1893
1894         /* This is preceded by downloading the download code. */
1895
1896         board->flags |= SX_BOARD_INITIALIZED;
1897
1898         if (read_sx_byte (board, 0))
1899                 /* CF boards may need this. */
1900                 write_sx_byte(board,0, 0);
1901
1902         /* This resets the processor again, to make sure it didn't do any
1903            foolish things while we were downloading the image */
1904         if (!sx_reset (board))
1905                 return 0;
1906
1907         sx_start_board (board);
1908         udelay (10);
1909         if (!sx_busy_wait_neq (board, 0, 0xff, 0)) {
1910                 printk (KERN_ERR "sx: Ooops. Board won't initialize.\n");
1911                 return 0;
1912         }
1913
1914         /* Ok. So now the processor on the card is running. It gathered
1915            some info for us... */
1916         sx_dprintk (SX_DEBUG_INIT, "The sxcard structure:\n");
1917         if (sx_debug & SX_DEBUG_INIT) my_hd_io (board->base, 0x10);
1918         sx_dprintk (SX_DEBUG_INIT, "the first sx_module structure:\n");
1919         if (sx_debug & SX_DEBUG_INIT) my_hd_io (board->base + 0x80, 0x30);
1920
1921         sx_dprintk (SX_DEBUG_INIT, 
1922                     "init_status: %x, %dk memory, firmware V%x.%02x,\n", 
1923                     read_sx_byte (board, 0), read_sx_byte(board, 1), 
1924                     read_sx_byte (board, 5), read_sx_byte(board, 4));
1925
1926         if (read_sx_byte (board, 0) == 0xff) {
1927                 printk (KERN_INFO "sx: No modules found. Sorry.\n");
1928                 board->nports = 0;
1929                 return 0;
1930         }
1931
1932         chans = 0;
1933
1934         if (IS_SX_BOARD(board)) {
1935                 sx_write_board_word (board, cc_int_count, sx_maxints);
1936         } else {
1937                 if (sx_maxints)
1938                         sx_write_board_word (board, cc_int_count, SI_PROCESSOR_CLOCK/8/sx_maxints);
1939         }
1940
1941         /* grab the first module type... */
1942         /*  board->ta_type = mod_compat_type (read_sx_byte (board, 0x80 + 0x08)); */
1943         board->ta_type = mod_compat_type (sx_read_module_byte (board, 0x80, mc_chip));
1944
1945         /* XXX byteorder */
1946         for (addr = 0x80;addr != 0;addr = read_sx_word (board, addr) & 0x7fff) {
1947                 type = sx_read_module_byte (board, addr, mc_chip);
1948                 sx_dprintk (SX_DEBUG_INIT, "Module at %x: %d channels\n", 
1949                             addr, read_sx_byte (board, addr + 2));
1950
1951                 chans += sx_read_module_byte (board, addr, mc_type);
1952
1953                 sx_dprintk (SX_DEBUG_INIT, "module is an %s, which has %s/%s panels\n", 
1954                             mod_type_s (type),
1955                             pan_type_s (sx_read_module_byte (board, addr, mc_mods) & 0xf),
1956                             pan_type_s (sx_read_module_byte (board, addr, mc_mods) >> 4));
1957
1958                 sx_dprintk (SX_DEBUG_INIT, "CD1400 versions: %x/%x, ASIC version: %x\n", 
1959                             sx_read_module_byte (board, addr, mc_rev1),
1960                             sx_read_module_byte (board, addr, mc_rev2),
1961                             sx_read_module_byte (board, addr, mc_mtaasic_rev));
1962
1963                 /* The following combinations are illegal: It should theoretically
1964                    work, but timing problems make the bus HANG. */
1965
1966                 if (mod_compat_type (type) != board->ta_type) {
1967                         printk (KERN_ERR "sx: This is an invalid configuration.\n"
1968                                 "Don't mix TA/MTA/SXDC on the same hostadapter.\n");
1969                         chans=0;
1970                         break;
1971                 }
1972                 if ((IS_EISA_BOARD(board) || 
1973                      IS_SI_BOARD(board)) && (mod_compat_type(type) == 4)) {
1974                         printk (KERN_ERR "sx: This is an invalid configuration.\n"
1975                                 "Don't use SXDCs on an SI/XIO adapter.\n");
1976                         chans=0;
1977                         break;
1978                 }
1979 #if 0 /* Problem fixed: firmware 3.05 */
1980                 if (IS_SX_BOARD(board) && (type == TA8)) {
1981                         /* There are some issues with the firmware and the DCD/RTS
1982                            lines. It might work if you tie them together or something.
1983                            It might also work if you get a newer sx_firmware.   Therefore
1984                            this is just a warning. */
1985                         printk (KERN_WARNING "sx: The SX host doesn't work too well "
1986                                 "with the TA8 adapters.\nSpecialix is working on it.\n");
1987                 }
1988 #endif
1989         }
1990
1991         if (chans) {
1992                 /* board->flags |= SX_BOARD_PRESENT; */
1993                 if(board->irq > 0) {
1994                         /* fixed irq, probably PCI */
1995                         if(sx_irqmask & (1 << board->irq)) { /* may we use this irq? */
1996                                 if(request_irq(board->irq, sx_interrupt, SA_SHIRQ | SA_INTERRUPT, "sx", board)) {
1997                                         printk(KERN_ERR "sx: Cannot allocate irq %d.\n", board->irq);
1998                                         board->irq = 0;
1999                                 }
2000                         } else
2001                                 board->irq = 0;
2002                 } else if(board->irq < 0 && sx_irqmask) {
2003                         /* auto-allocate irq */
2004                         int irqnr;
2005                         int irqmask = sx_irqmask & (IS_SX_BOARD(board) ? SX_ISA_IRQ_MASK : SI2_ISA_IRQ_MASK);
2006                         for(irqnr = 15; irqnr > 0; irqnr--)
2007                                 if(irqmask & (1 << irqnr))
2008                                         if(! request_irq(irqnr, sx_interrupt, SA_SHIRQ | SA_INTERRUPT, "sx", board))
2009                                                 break;
2010                         if(! irqnr)
2011                                 printk(KERN_ERR "sx: Cannot allocate IRQ.\n");
2012                         board->irq = irqnr;
2013                 } else
2014                         board->irq = 0;
2015
2016                 if (board->irq) {
2017                         /* Found a valid interrupt, start up interrupts! */
2018                         sx_dprintk (SX_DEBUG_INIT, "Using irq %d.\n", board->irq);
2019                         sx_start_interrupts (board);
2020                         board->poll = sx_slowpoll;
2021                         board->flags |= SX_IRQ_ALLOCATED;
2022                 } else {
2023                         /* no irq: setup board for polled operation */
2024                         board->poll = sx_poll;
2025                         sx_dprintk (SX_DEBUG_INIT, "Using poll-interval %d.\n", board->poll);
2026                 }
2027
2028                 /* The timer should be initialized anyway: That way we can safely
2029                          del_timer it when the module is unloaded. */
2030                 init_timer (&board->timer);
2031
2032                 if (board->poll) {
2033                         board->timer.data = (unsigned long) board;
2034                         board->timer.function = sx_pollfunc;
2035                         board->timer.expires = jiffies + board->poll;
2036                         add_timer (&board->timer);
2037                 }
2038         } else {
2039                 board->irq = 0;
2040         }
2041
2042         board->nports = chans;
2043         sx_dprintk (SX_DEBUG_INIT, "returning %d ports.", board->nports);
2044
2045         func_exit();
2046         return chans;
2047 }
2048
2049
2050 static void printheader(void)
2051 {
2052         static int header_printed;
2053
2054         if (!header_printed) {
2055                 printk (KERN_INFO "Specialix SX driver "
2056                         "(C) 1998/1999 R.E.Wolff@BitWizard.nl \n");
2057                 printk (KERN_INFO "sx: version %s\n", RCS_ID);
2058                 header_printed = 1;
2059         }
2060 }
2061
2062
2063 static int probe_sx (struct sx_board *board)
2064 {
2065         struct vpd_prom vpdp;
2066         char *p;
2067         int i;
2068
2069         func_enter();
2070
2071         if (!IS_CF_BOARD (board)) {    
2072                 sx_dprintk (SX_DEBUG_PROBE, "Going to verify vpd prom at %p.\n", 
2073                             board->base + SX_VPD_ROM);
2074
2075                 if (sx_debug & SX_DEBUG_PROBE)
2076                         my_hd_io(board->base + SX_VPD_ROM, 0x40);
2077
2078                 p = (char *) &vpdp;
2079                 for (i=0;i< sizeof (struct vpd_prom);i++)
2080                         *p++ = read_sx_byte (board, SX_VPD_ROM + i*2);
2081
2082                 if (sx_debug & SX_DEBUG_PROBE)
2083                         my_hd (&vpdp, 0x20);
2084
2085                 sx_dprintk (SX_DEBUG_PROBE, "checking identifier...\n");
2086
2087                 if (strncmp (vpdp.identifier, SX_VPD_IDENT_STRING, 16) != 0) {
2088                         sx_dprintk (SX_DEBUG_PROBE, "Got non-SX identifier: '%s'\n", 
2089                                     vpdp.identifier); 
2090                         return 0;
2091                 }
2092         }
2093
2094         printheader ();
2095
2096         if (!IS_CF_BOARD (board)) {
2097                 printk (KERN_DEBUG "sx: Found an SX board at %lx\n", board->hw_base);
2098                 printk (KERN_DEBUG "sx: hw_rev: %d, assembly level: %d, uniq ID:%08x, ", 
2099                         vpdp.hwrev, vpdp.hwass, vpdp.uniqid);
2100                 printk (           "Manufactured: %d/%d\n", 
2101                         1970 + vpdp.myear, vpdp.mweek);
2102
2103
2104                 if ((((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) != SX_PCI_UNIQUEID1) &&
2105                     (((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) != SX_ISA_UNIQUEID1)) {
2106                         /* This might be a bit harsh. This was the primary reason the
2107                            SX/ISA card didn't work at first... */
2108                         printk (KERN_ERR "sx: Hmm. Not an SX/PCI or SX/ISA card. Sorry: giving up.\n");
2109                         return (0);
2110                 }
2111
2112                 if (((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) == SX_ISA_UNIQUEID1) {
2113                         if (((unsigned long)board->hw_base) & 0x8000) {
2114                                 printk (KERN_WARNING "sx: Warning: There may be hardware problems with the card at %lx.\n", board->hw_base);
2115                                 printk (KERN_WARNING "sx: Read sx.txt for more info.\n");
2116                         }
2117                 }
2118         }
2119
2120         board->nports = -1;
2121
2122         /* This resets the processor, and keeps it off the bus. */
2123         if (!sx_reset (board)) 
2124                 return 0;
2125         sx_dprintk (SX_DEBUG_INIT, "reset the board...\n");
2126
2127         board->flags |= SX_BOARD_PRESENT;
2128
2129         func_exit();
2130         return 1;
2131 }
2132
2133
2134
2135 /* Specialix probes for this card at 32k increments from 640k to 16M.
2136    I consider machines with less than 16M unlikely nowadays, so I'm
2137    not probing above 1Mb. Also, 0xa0000, 0xb0000, are taken by the VGA
2138    card. 0xe0000 and 0xf0000 are taken by the BIOS. That only leaves 
2139    0xc0000, 0xc8000, 0xd0000 and 0xd8000 . */
2140
2141 static int probe_si (struct sx_board *board)
2142 {
2143         int i;
2144
2145         func_enter();
2146         sx_dprintk (SX_DEBUG_PROBE, "Going to verify SI signature hw %lx at %p.\n", board->hw_base,
2147                     board->base + SI2_ISA_ID_BASE);
2148
2149         if (sx_debug & SX_DEBUG_PROBE)
2150                 my_hd_io(board->base + SI2_ISA_ID_BASE, 0x8);
2151
2152         if (!IS_EISA_BOARD(board)) {
2153           if( IS_SI1_BOARD(board) ) 
2154             {
2155                 for (i=0;i<8;i++) {
2156                   write_sx_byte (board, SI2_ISA_ID_BASE+7-i,i); 
2157
2158                 }
2159             }
2160                 for (i=0;i<8;i++) {
2161                         if ((read_sx_byte (board, SI2_ISA_ID_BASE+7-i) & 7) != i) {
2162                                 func_exit ();
2163                                 return 0;
2164                         }
2165                 }
2166         }
2167
2168         /* Now we're pretty much convinced that there is an SI board here, 
2169            but to prevent trouble, we'd better double check that we don't
2170            have an SI1 board when we're probing for an SI2 board.... */
2171
2172         write_sx_byte (board, SI2_ISA_ID_BASE,0x10); 
2173         if ( IS_SI1_BOARD(board)) {
2174                 /* This should be an SI1 board, which has this
2175                    location writable... */
2176                 if (read_sx_byte (board, SI2_ISA_ID_BASE) != 0x10) {
2177                         func_exit ();
2178                         return 0; 
2179                 }
2180         } else {
2181                 /* This should be an SI2 board, which has the bottom
2182                    3 bits non-writable... */
2183                 if (read_sx_byte (board, SI2_ISA_ID_BASE) == 0x10) {
2184                         func_exit ();
2185                         return 0; 
2186                 }
2187         }
2188
2189         /* Now we're pretty much convinced that there is an SI board here, 
2190            but to prevent trouble, we'd better double check that we don't
2191            have an SI1 board when we're probing for an SI2 board.... */
2192
2193         write_sx_byte (board, SI2_ISA_ID_BASE,0x10); 
2194         if ( IS_SI1_BOARD(board)) {
2195                 /* This should be an SI1 board, which has this
2196                    location writable... */
2197                 if (read_sx_byte (board, SI2_ISA_ID_BASE) != 0x10) {
2198                         func_exit();
2199                         return 0; 
2200                 }
2201         } else {
2202                 /* This should be an SI2 board, which has the bottom
2203                    3 bits non-writable... */
2204                 if (read_sx_byte (board, SI2_ISA_ID_BASE) == 0x10) {
2205                         func_exit ();
2206                         return 0; 
2207                 }
2208         }
2209
2210         printheader ();
2211
2212         printk (KERN_DEBUG "sx: Found an SI board at %lx\n", board->hw_base);
2213         /* Compared to the SX boards, it is a complete guess as to what
2214                  this card is up to... */
2215
2216         board->nports = -1;
2217
2218         /* This resets the processor, and keeps it off the bus. */
2219         if (!sx_reset (board)) 
2220                 return 0;
2221         sx_dprintk (SX_DEBUG_INIT, "reset the board...\n");
2222
2223         board->flags |= SX_BOARD_PRESENT;
2224
2225         func_exit();
2226         return 1;
2227 }
2228
2229 static struct tty_operations sx_ops = {
2230         .break_ctl = sx_break,
2231         .open   = sx_open,
2232         .close = gs_close,
2233         .write = gs_write,
2234         .put_char = gs_put_char,
2235         .flush_chars = gs_flush_chars,
2236         .write_room = gs_write_room,
2237         .chars_in_buffer = gs_chars_in_buffer,
2238         .flush_buffer = gs_flush_buffer,
2239         .ioctl = sx_ioctl,
2240         .throttle = sx_throttle,
2241         .unthrottle = sx_unthrottle,
2242         .set_termios = gs_set_termios,
2243         .stop = gs_stop,
2244         .start = gs_start,
2245         .hangup = gs_hangup,
2246         .tiocmget = sx_tiocmget,
2247         .tiocmset = sx_tiocmset,
2248 };
2249
2250 static int sx_init_drivers(void)
2251 {
2252         int error;
2253
2254         func_enter();
2255
2256         sx_driver = alloc_tty_driver(sx_nports);
2257         if (!sx_driver)
2258                 return 1;
2259         sx_driver->owner = THIS_MODULE;
2260         sx_driver->driver_name = "specialix_sx";
2261         sx_driver->name = "ttyX";
2262         sx_driver->major = SX_NORMAL_MAJOR;
2263         sx_driver->type = TTY_DRIVER_TYPE_SERIAL;
2264         sx_driver->subtype = SERIAL_TYPE_NORMAL;
2265         sx_driver->init_termios = tty_std_termios;
2266         sx_driver->init_termios.c_cflag =
2267           B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2268         sx_driver->flags = TTY_DRIVER_REAL_RAW;
2269         tty_set_operations(sx_driver, &sx_ops);
2270
2271         if ((error = tty_register_driver(sx_driver))) {
2272                 put_tty_driver(sx_driver);
2273                 printk(KERN_ERR "sx: Couldn't register sx driver, error = %d\n",
2274                        error);
2275                 return 1;
2276         }
2277         func_exit();
2278         return 0;
2279 }
2280
2281
2282 static void * ckmalloc (int size)
2283 {
2284         void *p;
2285
2286         p = kmalloc(size, GFP_KERNEL);
2287         if (p) 
2288                 memset(p, 0, size);
2289         return p;
2290 }
2291
2292
2293 static int sx_init_portstructs (int nboards, int nports)
2294 {
2295         struct sx_board *board;
2296         struct sx_port *port;
2297         int i, j;
2298         int addr, chans;
2299         int portno;
2300
2301         func_enter();
2302
2303         /* Many drivers statically allocate the maximum number of ports
2304            There is no reason not to allocate them dynamically. Is there? -- REW */
2305         sx_ports          = ckmalloc(nports * sizeof (struct sx_port));
2306         if (!sx_ports)
2307                 return -ENOMEM;
2308
2309         port = sx_ports;
2310         for (i = 0; i < nboards; i++) {
2311                 board = &boards[i];
2312                 board->ports = port;
2313                 for (j=0; j < boards[i].nports;j++) {
2314                         sx_dprintk (SX_DEBUG_INIT, "initing port %d\n", j);
2315                         port->gs.magic = SX_MAGIC;
2316                         port->gs.close_delay = HZ/2;
2317                         port->gs.closing_wait = 30 * HZ;
2318                         port->board = board;
2319                         port->gs.rd = &sx_real_driver;
2320 #ifdef NEW_WRITE_LOCKING
2321                         port->gs.port_write_sem = MUTEX;
2322 #endif
2323                         port->gs.driver_lock = SPIN_LOCK_UNLOCKED;
2324                         /*
2325                          * Initializing wait queue
2326                          */
2327                         init_waitqueue_head(&port->gs.open_wait);
2328                         init_waitqueue_head(&port->gs.close_wait);              
2329                         
2330                         port++;
2331                 }
2332         }
2333
2334         port = sx_ports;
2335         portno = 0;
2336         for (i = 0; i < nboards; i++) {
2337                 board = &boards[i];
2338                 board->port_base = portno;
2339                 /* Possibly the configuration was rejected. */
2340                 sx_dprintk (SX_DEBUG_PROBE, "Board has %d channels\n", board->nports);
2341                 if (board->nports <= 0) continue;
2342                 /* XXX byteorder ?? */
2343                 for (addr = 0x80;addr != 0;addr = read_sx_word (board, addr) & 0x7fff) {
2344                         chans = sx_read_module_byte (board, addr, mc_type); 
2345                         sx_dprintk (SX_DEBUG_PROBE, "Module at %x: %d channels\n", addr, chans);
2346                         sx_dprintk (SX_DEBUG_PROBE, "Port at");
2347                         for (j=0;j<chans;j++) {
2348                                 /* The "sx-way" is the way it SHOULD be done. That way in the 
2349                                    future, the firmware may for example pack the structures a bit
2350                                    more efficient. Neil tells me it isn't going to happen anytime
2351                                    soon though. */
2352                                 if (IS_SX_BOARD(board))
2353                                         port->ch_base = sx_read_module_word (board, addr+j*2, mc_chan_pointer);
2354                                 else
2355                                         port->ch_base = addr + 0x100 + 0x300*j;
2356
2357                                 sx_dprintk (SX_DEBUG_PROBE, " %x", port->ch_base);
2358                                 port->line = portno++;
2359                                 port++;
2360                         }
2361                         sx_dprintk (SX_DEBUG_PROBE, "\n");
2362                 }
2363                 /* This has to be done earlier. */
2364                 /* board->flags |= SX_BOARD_INITIALIZED; */
2365         }
2366
2367         func_exit();
2368         return 0;
2369 }
2370
2371 static void __exit sx_release_drivers(void)
2372 {
2373         func_enter();
2374         tty_unregister_driver(sx_driver);
2375         put_tty_driver(sx_driver);
2376         func_exit();
2377 }
2378
2379 #ifdef CONFIG_PCI
2380  /******************************************************** 
2381  * Setting bit 17 in the CNTRL register of the PLX 9050  * 
2382  * chip forces a retry on writes while a read is pending.*
2383  * This is to prevent the card locking up on Intel Xeon  *
2384  * multiprocessor systems with the NX chipset.    -- NV  *
2385  ********************************************************/
2386
2387 /* Newer cards are produced with this bit set from the configuration
2388    EEprom.  As the bit is read/write for the CPU, we can fix it here,
2389    if we detect that it isn't set correctly. -- REW */
2390
2391 static void fix_sx_pci (struct pci_dev *pdev, struct sx_board *board)
2392 {
2393         unsigned int hwbase;
2394         void __iomem *rebase;
2395         unsigned int t;
2396
2397 #define CNTRL_REG_OFFSET        0x50
2398 #define CNTRL_REG_GOODVALUE     0x18260000
2399
2400         pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &hwbase);
2401         hwbase &= PCI_BASE_ADDRESS_MEM_MASK;
2402         rebase = ioremap(hwbase, 0x80);
2403         t = readl (rebase + CNTRL_REG_OFFSET);
2404         if (t != CNTRL_REG_GOODVALUE) {
2405                 printk (KERN_DEBUG "sx: performing cntrl reg fix: %08x -> %08x\n", t, CNTRL_REG_GOODVALUE); 
2406                 writel (CNTRL_REG_GOODVALUE, rebase + CNTRL_REG_OFFSET);
2407         }
2408         iounmap(rebase);
2409 }
2410 #endif
2411
2412
2413 static int __init sx_init(void) 
2414 {
2415         int i;
2416         int found = 0;
2417         int eisa_slot;
2418         struct sx_board *board;
2419
2420 #ifdef CONFIG_PCI
2421         struct pci_dev *pdev = NULL;
2422         unsigned int tint;
2423         unsigned short tshort;
2424 #endif
2425
2426         func_enter();
2427         sx_dprintk (SX_DEBUG_INIT, "Initing sx module... (sx_debug=%d)\n", sx_debug);
2428         if (abs ((long) (&sx_debug) - sx_debug) < 0x10000) {
2429                 printk (KERN_WARNING "sx: sx_debug is an address, instead of a value. "
2430                         "Assuming -1.\n");
2431                 printk ("(%p)\n", &sx_debug);
2432                 sx_debug=-1;
2433         }
2434
2435         if (misc_register(&sx_fw_device) < 0) {
2436                 printk(KERN_ERR "SX: Unable to register firmware loader driver.\n");
2437                 return -EIO;
2438         }
2439
2440 #ifdef CONFIG_PCI
2441         while ((pdev = pci_find_device (PCI_VENDOR_ID_SPECIALIX, 
2442                                         PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8, 
2443                                               pdev))) {
2444                 if (pci_enable_device(pdev))
2445                         continue;
2446
2447                 /* Specialix has a whole bunch of cards with
2448                    0x2000 as the device ID. They say its because
2449                    the standard requires it. Stupid standard. */
2450                 /* It seems that reading a word doesn't work reliably on 2.0.
2451                    Also, reading a non-aligned dword doesn't work. So we read the
2452                    whole dword at 0x2c and extract the word at 0x2e (SUBSYSTEM_ID)
2453                    ourselves */
2454                 /* I don't know why the define doesn't work, constant 0x2c does --REW */ 
2455                 pci_read_config_dword (pdev, 0x2c, &tint);
2456                 tshort = (tint >> 16) & 0xffff;
2457                 sx_dprintk (SX_DEBUG_PROBE, "Got a specialix card: %x.\n", tint);
2458                 /* sx_dprintk (SX_DEBUG_PROBE, "pdev = %d/%d    (%x)\n", pdev, tint); */ 
2459                 if ((tshort != 0x0200) && (tshort != 0x0300)) {
2460                         sx_dprintk (SX_DEBUG_PROBE, "But it's not an SX card (%d)...\n", 
2461                                     tshort);
2462                         continue;
2463                 }
2464                 board = &boards[found];
2465
2466                 board->flags &= ~SX_BOARD_TYPE;
2467                 board->flags |= (tshort == 0x200)?SX_PCI_BOARD:
2468                                                   SX_CFPCI_BOARD;
2469
2470                 /* CF boards use base address 3.... */
2471                 if (IS_CF_BOARD (board))
2472                         board->hw_base = pci_resource_start (pdev, 3);
2473                 else
2474                         board->hw_base = pci_resource_start (pdev, 2);
2475                 board->base2 = 
2476                 board->base = ioremap(board->hw_base, WINDOW_LEN (board));
2477                 if (!board->base) {
2478                         printk(KERN_ERR "ioremap failed\n");
2479                         /* XXX handle error */
2480                 }
2481
2482                 /* Most of the stuff on the CF board is offset by
2483                    0x18000 ....  */
2484                 if (IS_CF_BOARD (board)) board->base += 0x18000;
2485
2486                 board->irq = pdev->irq;
2487
2488                 sx_dprintk (SX_DEBUG_PROBE, "Got a specialix card: %x/%p(%d) %x.\n", 
2489                             tint, boards[found].base, board->irq, board->flags);
2490
2491                 if (probe_sx (board)) {
2492                         found++;
2493                         fix_sx_pci (pdev, board);
2494                 } else 
2495                         iounmap(board->base2);
2496         }
2497 #endif
2498
2499         for (i=0;i<NR_SX_ADDRS;i++) {
2500                 board = &boards[found];
2501                 board->hw_base = sx_probe_addrs[i];
2502                 board->base2 =
2503                 board->base = ioremap(board->hw_base, SX_WINDOW_LEN);
2504                 board->flags &= ~SX_BOARD_TYPE;
2505                 board->flags |= SX_ISA_BOARD;
2506                 board->irq = sx_irqmask?-1:0;
2507
2508                 if (probe_sx (board)) {
2509                         found++;
2510                 } else {
2511                         iounmap(board->base);
2512                 }
2513         }
2514
2515         for (i=0;i<NR_SI_ADDRS;i++) {
2516                 board = &boards[found];
2517                 board->hw_base = si_probe_addrs[i];
2518                 board->base2 =
2519                 board->base = ioremap(board->hw_base, SI2_ISA_WINDOW_LEN);
2520                 board->flags &= ~SX_BOARD_TYPE;
2521                 board->flags |=  SI_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         for (i=0;i<NR_SI1_ADDRS;i++) {
2531                 board = &boards[found];
2532                 board->hw_base = si1_probe_addrs[i];
2533                 board->base2 =
2534                 board->base = ioremap(board->hw_base, SI1_ISA_WINDOW_LEN);
2535                 board->flags &= ~SX_BOARD_TYPE;
2536                 board->flags |=  SI1_ISA_BOARD;
2537                 board->irq = sx_irqmask ?-1:0;
2538
2539                 if (probe_si (board)) {
2540                         found++;
2541                 } else {
2542                         iounmap (board->base);
2543                 }
2544         }
2545
2546         sx_dprintk(SX_DEBUG_PROBE, "Probing for EISA cards\n");
2547         for(eisa_slot=0x1000; eisa_slot<0x10000; eisa_slot+=0x1000)
2548         {
2549                 if((inb(eisa_slot+0xc80)==0x4d) &&
2550                    (inb(eisa_slot+0xc81)==0x98))
2551                 {
2552                         sx_dprintk(SX_DEBUG_PROBE, "%s : Signature found in EISA slot %d, Product %d Rev %d\n",
2553                                                 "XIO", (eisa_slot>>12), inb(eisa_slot+0xc82), inb(eisa_slot+0xc83));
2554
2555                         board = &boards[found];
2556                         board->eisa_base = eisa_slot;
2557                         board->flags &= ~SX_BOARD_TYPE;
2558                         board->flags |= SI_EISA_BOARD;
2559
2560                         board->hw_base = (((inb(0xc01+eisa_slot) << 8) + inb(0xc00+eisa_slot)) << 16);
2561                         board->base2 =
2562                         board->base = ioremap(board->hw_base, SI2_EISA_WINDOW_LEN);
2563
2564                         sx_dprintk(SX_DEBUG_PROBE, "IO hw_base address: %lx\n", board->hw_base);
2565                         sx_dprintk(SX_DEBUG_PROBE, "base: %p\n", board->base);
2566                         board->irq = inb(board->eisa_base+0xc02)>>4; 
2567                         sx_dprintk(SX_DEBUG_PROBE, "IRQ: %d\n", board->irq);
2568                         
2569                         probe_si(board);
2570
2571                         found++;
2572                 }
2573         }
2574         if (found) {
2575                 printk (KERN_INFO "sx: total of %d boards detected.\n", found);
2576         } else {
2577                 misc_deregister(&sx_fw_device);
2578         }
2579
2580         func_exit();
2581         return found?0:-EIO;
2582 }
2583
2584
2585 static void __exit sx_exit (void)
2586 {
2587         int i; 
2588         struct sx_board *board;
2589
2590         func_enter();
2591         for (i = 0; i < SX_NBOARDS; i++) {
2592                 board = &boards[i];
2593                 if (board->flags & SX_BOARD_INITIALIZED) {
2594                         sx_dprintk (SX_DEBUG_CLEANUP, "Cleaning up board at %p\n", board->base);
2595                         /* The board should stop messing with us.
2596                            (actually I mean the interrupt) */
2597                         sx_reset (board);
2598                         if ((board->irq) && (board->flags & SX_IRQ_ALLOCATED))
2599                                 free_irq (board->irq, board);
2600
2601                         /* It is safe/allowed to del_timer a non-active timer */
2602                         del_timer (& board->timer);
2603                         iounmap(board->base);
2604                 }
2605         }
2606         if (misc_deregister(&sx_fw_device) < 0) {
2607                 printk (KERN_INFO "sx: couldn't deregister firmware loader devic\n");
2608         }
2609         sx_dprintk (SX_DEBUG_CLEANUP, "Cleaning up drivers (%d)\n", sx_initialized);
2610         if (sx_initialized)
2611                 sx_release_drivers ();
2612
2613         kfree (sx_ports);
2614         func_exit();
2615 }
2616
2617 module_init(sx_init);
2618 module_exit(sx_exit);
2619
2620