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