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