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