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