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