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