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