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