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