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