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