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