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