Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / sx.c
blob82e3cae1ec4d43b3537d907d99218be84a4e49ba
2 /* sx.c -- driver for the Specialix SX series cards.
4 * This driver will also support the older SI, and XIO cards.
7 * (C) 1998 - 2000 R.E.Wolff@BitWizard.nl
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 :-)
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.
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.
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.
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.
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
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
45 * Revision 1.31 2000/03/06 12:00:00 wolff,pvdl
46 * - Added support for EISA
48 * Revision 1.30 2000/01/21 17:43:06 wolff
49 * - Added support for SX+
51 * Revision 1.26 1999/08/05 15:22:14 wolff
52 * - Port to 2.3.x
53 * - Reformatted to Linus' liking.
55 * Revision 1.25 1999/07/30 14:24:08 wolff
56 * Had accidentally left "gs_debug" set to "-1" instead of "off" (=0).
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.
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.
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.
74 * Revision 1.21 1999/07/23 09:11:33 wolff
75 * I forgot to free dynamically allocated memory when the driver is unloaded.
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.
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...
86 * Revision 1.18 1999/06/10 09:38:42 wolff
87 * Changed the format of the firmware revision from %04x to %x.%02x .
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.
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...
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..
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...)
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.
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.
122 * Revision 1.11 1999/04/24 11:11:30 wolff
123 * Fixed two stupid typos in the memory test.
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.....)
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.
135 * Revision 1.8 1999/04/16 13:05:30 wolff
136 * fixed a DCD change unnoticed bug.
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!)
141 * Revision 1.6 1999/04/13 18:40:20 wolff
142 * changed misc-minor to 161, as assigned by HPA.
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.
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)
153 * Revision 1.3 1999/03/31 13:45:45 wolff
154 * Firmware loading is now done through a separate IOCTL.
156 * Revision 1.2 1999/03/28 12:22:29 wolff
157 * rcs cleanup
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).
162 * Revision 0.12 1999/03/28 09:20:10 wolff
163 * Fixed problem in 0.11, continueing cleanup.
165 * Revision 0.11 1999/03/28 08:46:44 wolff
166 * cleanup. Not good.
168 * Revision 0.10 1999/03/28 08:09:43 wolff
169 * Fixed loosing characters on close.
171 * Revision 0.9 1999/03/21 22:52:01 wolff
172 * Ported back to 2.2.... (minor things)
174 * Revision 0.8 1999/03/21 22:40:33 wolff
175 * Port to 2.0
177 * Revision 0.7 1999/03/21 19:06:34 wolff
178 * Fixed hangup processing.
180 * Revision 0.6 1999/02/05 08:45:14 wolff
181 * fixed real_raw problems. Inclusion into kernel imminent.
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.
188 * Revision 0.4 1998/12/17 09:34:46 wolff
189 * PPP works. ioctl works. Basically works!
191 * Revision 0.3 1998/12/15 13:05:18 wolff
192 * It works! Wow! Gotta start implementing IOCTL and stuff....
194 * Revision 0.2 1998/12/01 08:33:53 wolff
195 * moved over to 2.1.130
197 * Revision 0.1 1998/11/03 21:23:51 wolff
198 * Initial revision. Detects SX card.
200 * */
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 $"
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/malloc.h>
227 #include <linux/miscdevice.h>
229 /* The 3.0.0 version of sxboards/sxwindow.h uses BYTE and WORD.... */
230 #define BYTE u8
231 #define WORD u16
233 /* .... but the 3.0.4 version uses _u8 and _u16. */
234 #define _u8 u8
235 #define _u16 u16
237 #include "sxboards.h"
238 #include "sxwindow.h"
240 #include <linux/compatmac.h>
241 #include <linux/generic_serial.h>
242 #include "sx.h"
245 /* I don't think that this driver can handle more than 256 ports on
246 one machine. You'll have to increase the number of boards in sx.h
247 if you want more than 4 boards. */
250 /* Why the hell am I defining these here? */
251 #define SX_TYPE_NORMAL 1
252 #define SX_TYPE_CALLOUT 2
255 #ifndef PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8
256 #define PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8 0x2000
257 #endif
260 /* Configurable options:
261 (Don't be too sure that it'll work if you toggle them) */
263 /* Am I paranoid or not ? ;-) */
264 #undef SX_PARANOIA_CHECK
267 /* 20 -> 2000 per second. The card should rate-limit interrupts at 100
268 Hz, but it is user configurable. I don't recommend going above 1000
269 Hz. The interrupt ratelimit might trigger if the interrupt is
270 shared with a very active other device. */
271 #define IRQ_RATE_LIMIT 20
273 /* Sharing interrupts is possible now. If the other device wants more
274 than 2000 interrupts per second, we'd gracefully decline further
275 interrupts. That's not what we want. On the other hand, if the
276 other device interrupts 2000 times a second, don't use the SX
277 interrupt. Use polling. */
278 #undef IRQ_RATE_LIMIT
281 #if 0
282 /* Not implemented */
284 * The following defines are mostly for testing purposes. But if you need
285 * some nice reporting in your syslog, you can define them also.
287 #define SX_REPORT_FIFO
288 #define SX_REPORT_OVERRUN
289 #endif
292 /* Function prototypes */
293 static void sx_disable_tx_interrupts (void * ptr);
294 static void sx_enable_tx_interrupts (void * ptr);
295 static void sx_disable_rx_interrupts (void * ptr);
296 static void sx_enable_rx_interrupts (void * ptr);
297 static int sx_get_CD (void * ptr);
298 static void sx_shutdown_port (void * ptr);
299 static int sx_set_real_termios (void *ptr);
300 static void sx_hungup (void *ptr);
301 static void sx_close (void *ptr);
302 static int sx_chars_in_buffer (void * ptr);
303 static int sx_init_board (struct sx_board *board);
304 static int sx_init_portstructs (int nboards, int nports);
305 static int sx_fw_ioctl (struct inode *inode, struct file *filp,
306 unsigned int cmd, unsigned long arg);
307 static int sx_init_drivers(void);
310 static struct tty_driver sx_driver, sx_callout_driver;
312 static struct tty_struct * sx_table[SX_NPORTS] = { NULL, };
313 static struct termios ** sx_termios;
314 static struct termios ** sx_termios_locked;
316 struct sx_board boards[SX_NBOARDS];
317 struct sx_port *sx_ports;
318 int sx_refcount;
319 int sx_initialized = 0;
320 int sx_nports = 0;
321 int sx_debug = 0;
324 /* You can have the driver poll your card.
325 - Set sx_poll to 1 to poll every timer tick (10ms on Intel).
326 This is used when the card cannot use an interrupt for some reason.
328 - set sx_slowpoll to 100 to do an extra poll once a second (on Intel). If
329 the driver misses an interrupt (report this if it DOES happen to you!)
330 everything will continue to work....
332 int sx_poll = 1;
333 int sx_slowpoll = 0;
335 /* The card limits the number of interrupts per second.
336 At 115k2 "100" should be sufficient.
337 If you're using higher baudrates, you can increase this...
340 int sx_maxints = 100;
342 /* These are the only open spaces in my computer. Yours may have more
343 or less.... -- REW
344 duh: Card at 0xa0000 is possible on HP Netserver?? -- pvdl
346 int sx_probe_addrs[]= {0xc0000, 0xd0000, 0xe0000,
347 0xc8000, 0xd8000, 0xe8000};
348 int si_probe_addrs[]= {0xc0000, 0xd0000, 0xe0000,
349 0xc8000, 0xd8000, 0xe8000, 0xa0000};
351 #define NR_SX_ADDRS (sizeof(sx_probe_addrs)/sizeof (int))
352 #define NR_SI_ADDRS (sizeof(si_probe_addrs)/sizeof (int))
355 /* Set the mask to all-ones. This alas, only supports 32 interrupts.
356 Some architectures may need more. */
357 int sx_irqmask = -1;
359 #ifndef TWO_ZERO
360 #ifdef MODULE
361 MODULE_PARM(sx_probe_addrs, "i");
362 MODULE_PARM(si_probe_addrs, "i");
363 MODULE_PARM(sx_poll, "i");
364 MODULE_PARM(sx_slowpoll, "i");
365 MODULE_PARM(sx_maxints, "i");
366 MODULE_PARM(sx_debug, "i");
367 MODULE_PARM(sx_irqmask, "i");
368 #endif
369 #endif
371 static struct real_driver sx_real_driver = {
372 sx_disable_tx_interrupts,
373 sx_enable_tx_interrupts,
374 sx_disable_rx_interrupts,
375 sx_enable_rx_interrupts,
376 sx_get_CD,
377 sx_shutdown_port,
378 sx_set_real_termios,
379 sx_chars_in_buffer,
380 sx_close,
381 sx_hungup,
382 NULL
387 This driver can spew a whole lot of debugging output at you. If you
388 need maximum performance, you should disable the DEBUG define. To
389 aid in debugging in the field, I'm leaving the compile-time debug
390 features enabled, and disable them "runtime". That allows me to
391 instruct people with problems to enable debugging without requiring
392 them to recompile...
394 #define DEBUG
397 #ifdef DEBUG
398 #define sx_dprintk(f, str...) if (sx_debug & f) printk (str)
399 #else
400 #define sx_dprintk(f, str...) /* nothing */
401 #endif
405 #define func_enter() sx_dprintk (SX_DEBUG_FLOW, "sx: enter " __FUNCTION__ "\n")
406 #define func_exit() sx_dprintk (SX_DEBUG_FLOW, "sx: exit " __FUNCTION__ "\n")
408 #define func_enter2() sx_dprintk (SX_DEBUG_FLOW, "sx: enter " __FUNCTION__ \
409 "(port %d)\n", port->line)
415 * Firmware loader driver specific routines
419 static struct file_operations sx_fw_fops = {
420 owner: THIS_MODULE,
421 ioctl: sx_fw_ioctl,
424 struct miscdevice sx_fw_device = {
425 SXCTL_MISC_MINOR, "sxctl", &sx_fw_fops
432 #ifdef SX_PARANOIA_CHECK
434 /* This doesn't work. Who's paranoid around here? Not me! */
436 static inline int sx_paranoia_check(struct sx_port const * port,
437 kdev_t device, const char *routine)
440 static const char *badmagic =
441 KERN_ERR "sx: Warning: bad sx port magic number for device %s in %s\n";
442 static const char *badinfo =
443 KERN_ERR "sx: Warning: null sx port for device %s in %s\n";
445 if (!port) {
446 printk(badinfo, kdevname(device), routine);
447 return 1;
449 if (port->magic != SX_MAGIC) {
450 printk(badmagic, kdevname(device), routine);
451 return 1;
454 return 0;
456 #else
457 #define sx_paranoia_check(a,b,c) 0
458 #endif
460 /* The timeouts. First try 30 times as fast as possible. Then give
461 the card some time to breathe between accesses. (Otherwise the
462 processor on the card might not be able to access its OWN bus... */
464 #define TIMEOUT_1 30
465 #define TIMEOUT_2 1000000
468 #ifdef DEBUG
469 static void my_hd (unsigned char *addr, int len)
471 int i, j, ch;
473 for (i=0;i<len;i+=16) {
474 printk ("%08x ", (int) addr+i);
475 for (j=0;j<16;j++) {
476 printk ("%02x %s", addr[j+i], (j==7)?" ":"");
478 for (j=0;j<16;j++) {
479 ch = addr[j+i];
480 printk ("%c", (ch < 0x20)?'.':((ch > 0x7f)?'.':ch));
482 printk ("\n");
485 #endif
489 /* This needs redoing for Alpha -- REW -- Done. */
491 inline void write_sx_byte (struct sx_board *board, int offset, u8 byte)
493 writeb (byte, board->base+offset);
496 inline u8 read_sx_byte (struct sx_board *board, int offset)
498 return readb (board->base+offset);
502 inline void write_sx_word (struct sx_board *board, int offset, u16 word)
504 writew (word, board->base+offset);
507 inline u16 read_sx_word (struct sx_board *board, int offset)
509 return readw (board->base + offset);
513 int sx_busy_wait_eq (struct sx_board *board,
514 int offset,
515 int mask,
516 int correctval)
518 int i;
520 func_enter ();
522 for (i=0; i < TIMEOUT_1 > 0;i++)
523 if ((read_sx_byte (board, offset) & mask) == correctval) {
524 func_exit ();
525 return 1;
528 for (i=0; i < TIMEOUT_2 > 0;i++) {
529 if ((read_sx_byte (board, offset) & mask) == correctval) {
530 func_exit ();
531 return 1;
533 udelay (1);
536 func_exit ();
537 return 0;
541 int sx_busy_wait_neq (struct sx_board *board,
542 int offset,
543 int mask,
544 int badval)
546 int i;
548 func_enter ();
550 for (i=0; i < TIMEOUT_1 > 0;i++)
551 if ((read_sx_byte (board, offset) & mask) != badval) {
552 func_exit ();
553 return 1;
556 for (i=0; i < TIMEOUT_2 > 0;i++) {
557 if ((read_sx_byte (board, offset) & mask) != badval) {
558 func_exit ();
559 return 1;
561 udelay (1);
564 func_exit ();
565 return 0;
570 /* 5.6.4 of 6210028 r2.3 */
571 int sx_reset (struct sx_board *board)
573 func_enter ();
575 if (IS_SX_BOARD (board)) {
577 write_sx_byte (board, SX_CONFIG, 0);
578 write_sx_byte (board, SX_RESET, 1); /* Value doesn't matter */
580 if (!sx_busy_wait_eq (board, SX_RESET_STATUS, 1, 0)) {
581 printk (KERN_INFO "sx: Card doesn't respond to reset....\n");
582 return 0;
584 } else if (IS_EISA_BOARD(board)) {
585 outb(board->irq<<4, board->eisa_base+0xc02);
586 } else {
587 /* Gory details of the SI/ISA board */
588 write_sx_byte (board, SI2_ISA_RESET, SI2_ISA_RESET_SET);
589 write_sx_byte (board, SI2_ISA_IRQ11, SI2_ISA_IRQ11_CLEAR);
590 write_sx_byte (board, SI2_ISA_IRQ12, SI2_ISA_IRQ12_CLEAR);
591 write_sx_byte (board, SI2_ISA_IRQ15, SI2_ISA_IRQ15_CLEAR);
592 write_sx_byte (board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_CLEAR);
593 write_sx_byte (board, SI2_ISA_IRQSET, SI2_ISA_IRQSET_CLEAR);
596 func_exit ();
597 return 1;
601 /* This doesn't work on machines where "NULL" isn't 0 */
602 /* If you have one of those, someone will need to write
603 the equivalent of this, which will amount to about 3 lines. I don't
604 want to complicate this right now. -- REW
605 (See, I do write comments every now and then :-) */
606 #define OFFSETOF(strct, elem) ((long)&(((struct strct *)NULL)->elem))
609 #define CHAN_OFFSET(port,elem) (port->ch_base + OFFSETOF (_SXCHANNEL, elem))
610 #define MODU_OFFSET(board,addr,elem) (addr + OFFSETOF (_SXMODULE, elem))
611 #define BRD_OFFSET(board,elem) (OFFSETOF (_SXCARD, elem))
614 #define sx_write_channel_byte(port, elem, val) \
615 write_sx_byte (port->board, CHAN_OFFSET (port, elem), val)
617 #define sx_read_channel_byte(port, elem) \
618 read_sx_byte (port->board, CHAN_OFFSET (port, elem))
620 #define sx_write_channel_word(port, elem, val) \
621 write_sx_word (port->board, CHAN_OFFSET (port, elem), val)
623 #define sx_read_channel_word(port, elem) \
624 read_sx_word (port->board, CHAN_OFFSET (port, elem))
627 #define sx_write_module_byte(board, addr, elem, val) \
628 write_sx_byte (board, MODU_OFFSET (board, addr, elem), val)
630 #define sx_read_module_byte(board, addr, elem) \
631 read_sx_byte (board, MODU_OFFSET (board, addr, elem))
633 #define sx_write_module_word(board, addr, elem, val) \
634 write_sx_word (board, MODU_OFFSET (board, addr, elem), val)
636 #define sx_read_module_word(board, addr, elem) \
637 read_sx_word (board, MODU_OFFSET (board, addr, elem))
640 #define sx_write_board_byte(board, elem, val) \
641 write_sx_byte (board, BRD_OFFSET (board, elem), val)
643 #define sx_read_board_byte(board, elem) \
644 read_sx_byte (board, BRD_OFFSET (board, elem))
646 #define sx_write_board_word(board, elem, val) \
647 write_sx_word (board, BRD_OFFSET (board, elem), val)
649 #define sx_read_board_word(board, elem) \
650 read_sx_word (board, BRD_OFFSET (board, elem))
653 int sx_start_board (struct sx_board *board)
655 if (IS_SX_BOARD (board)) {
656 write_sx_byte (board, SX_CONFIG, SX_CONF_BUSEN);
657 } else if (IS_EISA_BOARD(board)) {
658 write_sx_byte(board, SI2_EISA_OFF, SI2_EISA_VAL);
659 outb((board->irq<<4)|4, board->eisa_base+0xc02);
660 } else {
661 /* Don't bug me about the clear_set.
662 I haven't the foggiest idea what it's about -- REW */
663 write_sx_byte (board, SI2_ISA_RESET, SI2_ISA_RESET_CLEAR);
664 write_sx_byte (board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
666 return 1;
669 #define SX_IRQ_REG_VAL(board) \
670 ((board->flags & SX_ISA_BOARD)?(board->irq << 4):0)
672 /* Note. The SX register is write-only. Therefore, we have to enable the
673 bus too. This is a no-op, if you don't mess with this driver... */
674 int sx_start_interrupts (struct sx_board *board)
677 /* Don't call this with board->irq == 0 */
679 if (IS_SX_BOARD(board)) {
680 write_sx_byte (board, SX_CONFIG, SX_IRQ_REG_VAL (board) |
681 SX_CONF_BUSEN |
682 SX_CONF_HOSTIRQ);
683 } else if (IS_EISA_BOARD(board)) {
684 inb(board->eisa_base+0xc03);
685 } else {
686 switch (board->irq) {
687 case 11:write_sx_byte (board, SI2_ISA_IRQ11, SI2_ISA_IRQ11_SET);break;
688 case 12:write_sx_byte (board, SI2_ISA_IRQ12, SI2_ISA_IRQ12_SET);break;
689 case 15:write_sx_byte (board, SI2_ISA_IRQ15, SI2_ISA_IRQ15_SET);break;
690 default:printk (KERN_INFO "sx: SI/XIO card doesn't support interrupt %d.\n",
691 board->irq);
692 return 0;
694 write_sx_byte (board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
697 return 1;
701 int sx_send_command (struct sx_port *port,
702 int command,
703 int mask,
704 int newstat)
706 func_enter2 ();
707 write_sx_byte (port->board, CHAN_OFFSET (port, hi_hstat), command);
708 func_exit ();
709 return sx_busy_wait_eq (port->board, CHAN_OFFSET (port, hi_hstat), mask, newstat);
713 char *mod_type_s (int module_type)
715 switch (module_type) {
716 case TA4: return "TA4";
717 case TA8: return "TA8";
718 case TA4_ASIC: return "TA4_ASIC";
719 case TA8_ASIC: return "TA8_ASIC";
720 case MTA_CD1400:return "MTA_CD1400";
721 case SXDC: return "SXDC";
722 default:return "Unknown/invalid";
727 char *pan_type_s (int pan_type)
729 switch (pan_type) {
730 case MOD_RS232DB25: return "MOD_RS232DB25";
731 case MOD_RS232RJ45: return "MOD_RS232RJ45";
732 case MOD_RS422DB25: return "MOD_RS422DB25";
733 case MOD_PARALLEL: return "MOD_PARALLEL";
734 case MOD_2_RS232DB25: return "MOD_2_RS232DB25";
735 case MOD_2_RS232RJ45: return "MOD_2_RS232RJ45";
736 case MOD_2_RS422DB25: return "MOD_2_RS422DB25";
737 case MOD_RS232DB25MALE: return "MOD_RS232DB25MALE";
738 case MOD_2_PARALLEL: return "MOD_2_PARALLEL";
739 case MOD_BLANK: return "empty";
740 default:return "invalid";
745 int mod_compat_type (int module_type)
747 return module_type >> 4;
750 static void sx_reconfigure_port(struct sx_port *port)
752 if (sx_read_channel_byte (port, hi_hstat) == HS_IDLE_OPEN) {
753 if (sx_send_command (port, HS_CONFIG, -1, HS_IDLE_OPEN) != 1) {
754 printk (KERN_WARNING "sx: Sent reconfigure command, but card didn't react.\n");
756 } else {
757 sx_dprintk (SX_DEBUG_TERMIOS,
758 "sx: Not sending reconfigure: port isn't open (%02x).\n",
759 sx_read_channel_byte (port, hi_hstat));
763 static void sx_setsignals (struct sx_port *port, int dtr, int rts)
765 int t;
766 func_enter2 ();
768 t = sx_read_channel_byte (port, hi_op);
769 if (dtr >= 0) t = dtr? (t | OP_DTR): (t & ~OP_DTR);
770 if (rts >= 0) t = rts? (t | OP_RTS): (t & ~OP_RTS);
771 sx_write_channel_byte (port, hi_op, t);
772 sx_dprintk (SX_DEBUG_MODEMSIGNALS, "setsignals: %d/%d\n", dtr, rts);
774 func_exit ();
779 static int sx_getsignals (struct sx_port *port)
781 int i_stat,o_stat;
783 o_stat = sx_read_channel_byte (port, hi_op);
784 i_stat = sx_read_channel_byte (port, hi_ip);
786 sx_dprintk (SX_DEBUG_MODEMSIGNALS, "getsignals: %d/%d (%d/%d) %02x/%02x\n",
787 (o_stat & OP_DTR) != 0, (o_stat & OP_RTS) != 0,
788 port->c_dcd, sx_get_CD (port),
789 sx_read_channel_byte (port, hi_ip),
790 sx_read_channel_byte (port, hi_state));
792 return (((o_stat & OP_DTR)?TIOCM_DTR:0) |
793 ((o_stat & OP_RTS)?TIOCM_RTS:0) |
794 ((i_stat & IP_CTS)?TIOCM_CTS:0) |
795 ((i_stat & IP_DCD)?TIOCM_CAR:0) |
796 ((i_stat & IP_DSR)?TIOCM_DSR:0) |
797 ((i_stat & IP_RI)?TIOCM_RNG:0)
802 static void sx_set_baud (struct sx_port *port)
804 int t;
806 if (port->board->ta_type == MOD_SXDC) {
807 switch (port->gs.baud) {
808 /* Save some typing work... */
809 #define e(x) case x:t= BAUD_ ## x ; break
810 e(50);e(75);e(110);e(150);e(200);e(300);e(600);
811 e(1200);e(1800);e(2000);e(2400);e(4800);e(7200);
812 e(9600);e(14400);e(19200);e(28800);e(38400);
813 e(56000);e(57600);e(64000);e(76800);e(115200);
814 e(128000);e(150000);e(230400);e(256000);e(460800);
815 e(921600);
816 case 134 :t = BAUD_134_5; break;
817 case 0 :t = -1;
818 break;
819 default:
820 /* Can I return "invalid"? */
821 t = BAUD_9600;
822 printk (KERN_INFO "sx: unsupported baud rate: %d.\n", port->gs.baud);
823 break;
825 #undef e
826 if (t > 0) {
827 /* The baud rate is not set to 0, so we're enabeling DTR... -- REW */
828 sx_setsignals (port, 1, -1);
829 /* XXX This is not TA & MTA compatible */
830 sx_write_channel_byte (port, hi_csr, 0xff);
832 sx_write_channel_byte (port, hi_txbaud, t);
833 sx_write_channel_byte (port, hi_rxbaud, t);
834 } else {
835 sx_setsignals (port, 0, -1);
837 } else {
838 switch (port->gs.baud) {
839 #define e(x) case x:t= CSR_ ## x ; break
840 e(75);e(150);e(300);e(600);e(1200);e(2400);e(4800);
841 e(1800);e(9600);
842 e(19200);e(57600);e(38400);
843 /* TA supports 110, but not 115200, MTA supports 115200, but not 110 */
844 case 110:
845 if (port->board->ta_type == MOD_TA) {
846 t = CSR_110;
847 break;
848 } else {
849 t = CSR_9600;
850 printk (KERN_INFO "sx: Unsupported baud rate: %d.\n", port->gs.baud);
851 break;
853 case 115200:
854 if (port->board->ta_type == MOD_TA) {
855 t = CSR_9600;
856 printk (KERN_INFO "sx: Unsupported baud rate: %d.\n", port->gs.baud);
857 break;
858 } else {
859 t = CSR_110;
860 break;
862 case 0 :t = -1;
863 break;
864 default:
865 t = CSR_9600;
866 printk (KERN_INFO "sx: Unsupported baud rate: %d.\n", port->gs.baud);
867 break;
869 #undef e
870 if (t >= 0) {
871 sx_setsignals (port, 1, -1);
872 sx_write_channel_byte (port, hi_csr, t * 0x11);
873 } else {
874 sx_setsignals (port, 0, -1);
880 /* Simon Allen's version of this routine was 225 lines long. 85 is a lot
881 better. -- REW */
883 static int sx_set_real_termios (void *ptr)
885 struct sx_port *port = ptr;
887 func_enter2();
889 if (!port->gs.tty)
890 return 0;
892 /* What is this doing here? -- REW
893 Ha! figured it out. It is to allow you to get DTR active again
894 if you've dropped it with stty 0. Moved to set_baud, where it
895 belongs (next to the drop dtr if baud == 0) -- REW */
896 /* sx_setsignals (port, 1, -1); */
898 sx_set_baud (port);
900 #define CFLAG port->gs.tty->termios->c_cflag
901 sx_write_channel_byte (port, hi_mr1,
902 (C_PARENB (port->gs.tty)? MR1_WITH:MR1_NONE) |
903 (C_PARODD (port->gs.tty)? MR1_ODD:MR1_EVEN) |
904 (C_CRTSCTS(port->gs.tty)? MR1_RTS_RXFLOW:0) |
905 (((CFLAG & CSIZE)==CS8) ? MR1_8_BITS:0) |
906 (((CFLAG & CSIZE)==CS7) ? MR1_7_BITS:0) |
907 (((CFLAG & CSIZE)==CS6) ? MR1_6_BITS:0) |
908 (((CFLAG & CSIZE)==CS5) ? MR1_5_BITS:0) );
910 sx_write_channel_byte (port, hi_mr2,
911 (C_CRTSCTS(port->gs.tty)?MR2_CTS_TXFLOW:0) |
912 (C_CSTOPB (port->gs.tty)?MR2_2_STOP:MR2_1_STOP));
914 switch (CFLAG & CSIZE) {
915 case CS8:sx_write_channel_byte (port, hi_mask, 0xff);break;
916 case CS7:sx_write_channel_byte (port, hi_mask, 0x7f);break;
917 case CS6:sx_write_channel_byte (port, hi_mask, 0x3f);break;
918 case CS5:sx_write_channel_byte (port, hi_mask, 0x1f);break;
919 default:
920 printk (KERN_INFO "sx: Invalid wordsize: %d\n", CFLAG & CSIZE);
921 break;
924 sx_write_channel_byte (port, hi_prtcl,
925 (I_IXON (port->gs.tty)?SP_TXEN:0) |
926 (I_IXOFF (port->gs.tty)?SP_RXEN:0) |
927 (I_IXANY (port->gs.tty)?SP_TANY:0) |
928 SP_DCEN);
930 sx_write_channel_byte (port, hi_break,
931 I_OTHER(port->gs.tty) ? 0:
932 (I_IGNBRK(port->gs.tty)?BR_IGN:0 |
933 I_BRKINT(port->gs.tty)?BR_INT:0));
935 sx_write_channel_byte (port, hi_txon, START_CHAR (port->gs.tty));
936 sx_write_channel_byte (port, hi_rxon, START_CHAR (port->gs.tty));
937 sx_write_channel_byte (port, hi_txoff, STOP_CHAR (port->gs.tty));
938 sx_write_channel_byte (port, hi_rxoff, STOP_CHAR (port->gs.tty));
940 sx_reconfigure_port(port);
942 /* Tell line discipline whether we will do input cooking */
943 if(I_OTHER(port->gs.tty)) {
944 clear_bit(TTY_HW_COOK_IN, &port->gs.tty->flags);
945 } else {
946 set_bit(TTY_HW_COOK_IN, &port->gs.tty->flags);
948 sx_dprintk (SX_DEBUG_TERMIOS, "iflags: %x(%d) ",
949 port->gs.tty->termios->c_iflag,
950 I_OTHER(port->gs.tty));
953 /* Tell line discipline whether we will do output cooking.
954 * If OPOST is set and no other output flags are set then we can do output
955 * processing. Even if only *one* other flag in the O_OTHER group is set
956 * we do cooking in software.
958 if(O_OPOST(port->gs.tty) && !O_OTHER(port->gs.tty)) {
959 set_bit(TTY_HW_COOK_OUT, &port->gs.tty->flags);
960 } else {
961 clear_bit(TTY_HW_COOK_OUT, &port->gs.tty->flags);
963 sx_dprintk (SX_DEBUG_TERMIOS, "oflags: %x(%d)\n",
964 port->gs.tty->termios->c_oflag,
965 O_OTHER(port->gs.tty));
966 /* port->c_dcd = sx_get_CD (port); */
967 func_exit ();
968 return 0;
973 /* ********************************************************************** *
974 * the interrupt related routines *
975 * ********************************************************************** */
977 /* Note:
978 Other drivers use the macro "MIN" to calculate how much to copy.
979 This has the disadvantage that it will evaluate parts twice. That's
980 expensive when it's IO (and the compiler cannot optimize those away!).
981 Moreover, I'm not sure that you're race-free.
983 I assign a value, and then only allow the value to decrease. This
984 is always safe. This makes the code a few lines longer, and you
985 know I'm dead against that, but I think it is required in this
986 case. */
989 void sx_transmit_chars (struct sx_port *port)
991 int c;
992 int tx_ip;
993 int txroom;
995 func_enter2 ();
996 sx_dprintk (SX_DEBUG_TRANSMIT, "Port %p: transmit %d chars\n",
997 port, port->gs.xmit_cnt);
999 if (test_and_set_bit (SX_PORT_TRANSMIT_LOCK, &port->locks)) {
1000 return;
1003 while (1) {
1004 c = port->gs.xmit_cnt;
1006 sx_dprintk (SX_DEBUG_TRANSMIT, "Copying %d ", c);
1007 tx_ip = sx_read_channel_byte (port, hi_txipos);
1009 /* Took me 5 minutes to deduce this formula.
1010 Luckily it is literally in the manual in section 6.5.4.3.5 */
1011 txroom = (sx_read_channel_byte (port, hi_txopos) - tx_ip - 1) & 0xff;
1013 /* Don't copy more bytes than there is room for in the buffer */
1014 if (c > txroom)
1015 c = txroom;
1016 sx_dprintk (SX_DEBUG_TRANSMIT, " %d(%d) ", c, txroom );
1018 /* Don't copy past the end of the hardware transmit buffer */
1019 if (c > 0x100 - tx_ip)
1020 c = 0x100 - tx_ip;
1022 sx_dprintk (SX_DEBUG_TRANSMIT, " %d(%d) ", c, 0x100-tx_ip );
1024 /* Don't copy pas the end of the source buffer */
1025 if (c > SERIAL_XMIT_SIZE - port->gs.xmit_tail)
1026 c = SERIAL_XMIT_SIZE - port->gs.xmit_tail;
1028 sx_dprintk (SX_DEBUG_TRANSMIT, " %d(%ld) \n",
1029 c, SERIAL_XMIT_SIZE- port->gs.xmit_tail);
1031 /* If for one reason or another, we can't copy more data, we're done! */
1032 if (c == 0) break;
1035 memcpy_toio (port->board->base + CHAN_OFFSET(port,hi_txbuf) + tx_ip,
1036 port->gs.xmit_buf + port->gs.xmit_tail, c);
1038 /* Update the pointer in the card */
1039 sx_write_channel_byte (port, hi_txipos, (tx_ip+c) & 0xff);
1041 /* Update the kernel buffer end */
1042 port->gs.xmit_tail = (port->gs.xmit_tail + c) & (SERIAL_XMIT_SIZE-1);
1044 /* This one last. (this is essential)
1045 It would allow others to start putting more data into the buffer! */
1046 port->gs.xmit_cnt -= c;
1049 if (port->gs.xmit_cnt == 0) {
1050 sx_disable_tx_interrupts (port);
1053 if ((port->gs.xmit_cnt <= port->gs.wakeup_chars) && port->gs.tty) {
1054 if ((port->gs.tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1055 port->gs.tty->ldisc.write_wakeup)
1056 (port->gs.tty->ldisc.write_wakeup)(port->gs.tty);
1057 sx_dprintk (SX_DEBUG_TRANSMIT, "Waking up.... ldisc (%d)....\n",
1058 port->gs.wakeup_chars);
1059 wake_up_interruptible(&port->gs.tty->write_wait);
1062 clear_bit (SX_PORT_TRANSMIT_LOCK, &port->locks);
1063 func_exit ();
1067 /* Note the symmetry between receiving chars and transmitting them!
1068 Note: The kernel should have implemented both a receive buffer and
1069 a transmit buffer. */
1071 /* Inlined: Called only once. Remove the inline when you add another call */
1072 inline void sx_receive_chars (struct sx_port *port)
1074 int c;
1075 int rx_op;
1076 struct tty_struct *tty;
1077 int copied=0;
1079 func_enter2 ();
1080 tty = port->gs.tty;
1081 while (1) {
1082 rx_op = sx_read_channel_byte (port, hi_rxopos);
1083 c = (sx_read_channel_byte (port, hi_rxipos) - rx_op) & 0xff;
1085 sx_dprintk (SX_DEBUG_RECEIVE, "rxop=%d, c = %d.\n", rx_op, c);
1087 /* Don't copy more bytes than there is room for in the buffer */
1088 if (tty->flip.count + c > TTY_FLIPBUF_SIZE)
1089 c = TTY_FLIPBUF_SIZE - tty->flip.count;
1091 sx_dprintk (SX_DEBUG_RECEIVE, "c = %d.\n", c);
1093 /* Don't copy past the end of the hardware receive buffer */
1094 if (rx_op + c > 0x100) c = 0x100 - rx_op;
1096 sx_dprintk (SX_DEBUG_RECEIVE, "c = %d.\n", c);
1098 /* If for one reason or another, we can't copy more data, we're done! */
1099 if (c == 0) break;
1101 sx_dprintk (SX_DEBUG_RECEIVE , "Copying over %d chars. First is %d at %lx\n", c,
1102 read_sx_byte (port->board, CHAN_OFFSET(port,hi_rxbuf) + rx_op),
1103 CHAN_OFFSET(port, hi_rxbuf));
1104 memcpy_fromio (tty->flip.char_buf_ptr,
1105 port->board->base + CHAN_OFFSET(port,hi_rxbuf) + rx_op, c);
1106 memset(tty->flip.flag_buf_ptr, TTY_NORMAL, c);
1108 /* Update the kernel buffer end */
1109 tty->flip.count += c;
1110 tty->flip.char_buf_ptr += c;
1111 tty->flip.flag_buf_ptr += c;
1113 /* This one last. ( Not essential.)
1114 It allows the card to start putting more data into the buffer!
1115 Update the pointer in the card */
1116 sx_write_channel_byte (port, hi_rxopos, (rx_op + c) & 0xff);
1118 copied += c;
1120 if (copied) {
1121 struct timeval tv;
1123 do_gettimeofday (&tv);
1124 sx_dprintk (SX_DEBUG_RECEIVE,
1125 "pushing flipq port %d (%3d chars): %d.%06d (%d/%d)\n",
1126 port->line, copied,
1127 (int) (tv.tv_sec % 60), (int)tv.tv_usec, tty->raw, tty->real_raw);
1129 /* Tell the rest of the system the news. Great news. New characters! */
1130 tty_flip_buffer_push (tty);
1131 /* tty_schedule_flip (tty); */
1134 func_exit ();
1137 /* Inlined: it is called only once. Remove the inline if you add another
1138 call */
1139 inline void sx_check_modem_signals (struct sx_port *port)
1141 int hi_state;
1142 int c_dcd;
1144 hi_state = sx_read_channel_byte (port, hi_state);
1145 sx_dprintk (SX_DEBUG_MODEMSIGNALS, "Checking modem signals (%d/%d)\n",
1146 port->c_dcd, sx_get_CD (port));
1148 if (hi_state & ST_BREAK) {
1149 hi_state &= ~ST_BREAK;
1150 sx_dprintk (SX_DEBUG_MODEMSIGNALS, "got a break.\n");
1152 sx_write_channel_byte (port, hi_state, hi_state);
1153 if (port->gs.flags & ASYNC_SAK) {
1154 do_SAK (port->gs.tty);
1157 if (hi_state & ST_DCD) {
1158 hi_state &= ~ST_DCD;
1159 sx_dprintk (SX_DEBUG_MODEMSIGNALS, "got a DCD change.\n");
1160 sx_write_channel_byte (port, hi_state, hi_state);
1161 c_dcd = sx_get_CD (port);
1162 sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD is now %d\n", c_dcd);
1163 if (c_dcd != port->c_dcd) {
1164 port->c_dcd = c_dcd;
1165 if (sx_get_CD (port)) {
1166 /* DCD went UP */
1167 if( (~(port->gs.flags & ASYNC_NORMAL_ACTIVE) ||
1168 ~(port->gs.flags & ASYNC_CALLOUT_ACTIVE)) &&
1169 (sx_read_channel_byte(port, hi_hstat) != HS_IDLE_CLOSED)) {
1170 /* Are we blocking in open?*/
1171 sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD active, unblocking open\n");
1172 wake_up_interruptible(&port->gs.open_wait);
1173 } else {
1174 sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD raised. Ignoring.\n");
1176 } else {
1177 /* DCD went down! */
1178 if (!((port->gs.flags & ASYNC_CALLOUT_ACTIVE) &&
1179 (port->gs.flags & ASYNC_CALLOUT_NOHUP))) {
1180 sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD dropped. hanging up....\n");
1181 tty_hangup (port->gs.tty);
1182 } else {
1183 sx_dprintk (SX_DEBUG_MODEMSIGNALS, "DCD dropped. ignoring.\n");
1186 } else {
1187 sx_dprintk (SX_DEBUG_MODEMSIGNALS, "Hmmm. card told us DCD changed, but it didn't.\n");
1193 /* This is what an interrupt routine should look like.
1194 * Small, elegant, clear.
1197 static void sx_interrupt (int irq, void *ptr, struct pt_regs *regs)
1199 struct sx_board *board = ptr;
1200 struct sx_port *port;
1201 int i;
1203 /* func_enter (); */
1204 sx_dprintk (SX_DEBUG_FLOW, "sx: enter sx_interrupt (%d/%d)\n", irq, board->irq);
1206 /* AAargh! The order in which to do these things is essential and
1207 not trivial.
1209 - Rate limit goes before "recursive". Otherwise a series of
1210 recursive calls will hang the machine in the interrupt routine.
1212 - hardware twiddling goes before "recursive". Otherwise when we
1213 poll the card, and a recursive interrupt happens, we wont
1214 ack the card, so it might keep on interrupting us. (especially
1215 level sensitive interrupt systems like PCI).
1217 - Rate limit goes before hardware twiddling. Otherwise we won't
1218 catch a card that has gone bonkers.
1220 - The "initialized" test goes after the hardware twiddling. Otherwise
1221 the card will stick us in the interrupt routine again.
1223 - The initialized test goes before recursive.
1228 #ifdef IRQ_RATE_LIMIT
1229 /* Aaargh! I'm ashamed. This costs more lines-of-code than the
1230 actual interrupt routine!. (Well, used to when I wrote that comment) */
1232 static int lastjif;
1233 static int nintr=0;
1235 if (lastjif == jiffies) {
1236 if (++nintr > IRQ_RATE_LIMIT) {
1237 free_irq (board->irq, board);
1238 printk (KERN_ERR "sx: Too many interrupts. Turning off interrupt %d.\n",
1239 board->irq);
1241 } else {
1242 lastjif = jiffies;
1243 nintr = 0;
1246 #endif
1249 if (board->irq == irq) {
1250 /* Tell the card we've noticed the interrupt. */
1252 sx_write_board_word (board, cc_int_pending, 0);
1253 if (IS_SX_BOARD (board)) {
1254 write_sx_byte (board, SX_RESET_IRQ, 1);
1255 } else if (IS_EISA_BOARD(board)) {
1256 inb(board->eisa_base+0xc03);
1257 write_sx_word(board, 8, 0);
1258 } else {
1259 write_sx_byte (board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_CLEAR);
1260 write_sx_byte (board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
1264 if (!sx_initialized) return;
1265 if (!(board->flags & SX_BOARD_INITIALIZED)) return;
1267 if (test_and_set_bit (SX_BOARD_INTR_LOCK, &board->locks)) {
1268 printk (KERN_ERR "Recursive interrupt! (%d)\n", board->irq);
1269 return;
1272 for (i=0;i<board->nports;i++) {
1273 port = &board->ports[i];
1274 if (port->gs.flags & GS_ACTIVE) {
1275 if (sx_read_channel_byte (port, hi_state)) {
1276 sx_dprintk (SX_DEBUG_INTERRUPTS,
1277 "Port %d: modem signal change?... \n", i);
1278 sx_check_modem_signals (port);
1280 if (port->gs.xmit_cnt) {
1281 sx_transmit_chars (port);
1283 if (!(port->gs.flags & SX_RX_THROTTLE)) {
1284 sx_receive_chars (port);
1289 clear_bit (SX_BOARD_INTR_LOCK, &board->locks);
1291 sx_dprintk (SX_DEBUG_FLOW, "sx: exit sx_interrupt (%d/%d)\n", irq, board->irq);
1292 /* func_exit (); */
1296 static void sx_pollfunc (unsigned long data)
1298 struct sx_board *board = (struct sx_board *) data;
1300 func_enter ();
1302 sx_interrupt (0, board, NULL);
1304 init_timer(&board->timer);
1306 board->timer.expires = jiffies + sx_poll;
1307 add_timer (&board->timer);
1308 func_exit ();
1313 /* ********************************************************************** *
1314 * Here are the routines that actually *
1315 * interface with the generic_serial driver *
1316 * ********************************************************************** */
1318 /* Ehhm. I don't know how to fiddle with interrupts on the SX card. --REW */
1319 /* Hmm. Ok I figured it out. You don't. */
1321 static void sx_disable_tx_interrupts (void * ptr)
1323 struct sx_port *port = ptr;
1324 func_enter2();
1326 port->gs.flags &= ~GS_TX_INTEN;
1328 func_exit();
1332 static void sx_enable_tx_interrupts (void * ptr)
1334 struct sx_port *port = ptr;
1335 int data_in_buffer;
1336 func_enter2();
1338 /* First transmit the characters that we're supposed to */
1339 sx_transmit_chars (port);
1341 /* The sx card will never interrupt us if we don't fill the buffer
1342 past 25%. So we keep considering interrupts off if that's the case. */
1343 data_in_buffer = (sx_read_channel_byte (port, hi_txipos) -
1344 sx_read_channel_byte (port, hi_txopos)) & 0xff;
1346 /* XXX Must be "HIGH_WATER" for SI card according to doc. */
1347 if (data_in_buffer < LOW_WATER)
1348 port->gs.flags &= ~GS_TX_INTEN;
1350 func_exit();
1354 static void sx_disable_rx_interrupts (void * ptr)
1356 /* struct sx_port *port = ptr; */
1357 func_enter();
1359 func_exit();
1362 static void sx_enable_rx_interrupts (void * ptr)
1364 /* struct sx_port *port = ptr; */
1365 func_enter();
1367 func_exit();
1371 /* Jeez. Isn't this simple? */
1372 static int sx_get_CD (void * ptr)
1374 struct sx_port *port = ptr;
1375 func_enter2();
1377 func_exit();
1378 return ((sx_read_channel_byte (port, hi_ip) & IP_DCD) != 0);
1382 /* Jeez. Isn't this simple? */
1383 static int sx_chars_in_buffer (void * ptr)
1385 struct sx_port *port = ptr;
1386 func_enter2();
1388 func_exit();
1389 return ((sx_read_channel_byte (port, hi_txipos) -
1390 sx_read_channel_byte (port, hi_txopos)) & 0xff);
1394 static void sx_shutdown_port (void * ptr)
1396 struct sx_port *port = ptr;
1398 func_enter();
1400 port->gs.flags &= ~ GS_ACTIVE;
1401 if (port->gs.tty && (port->gs.tty->termios->c_cflag & HUPCL)) {
1402 sx_setsignals (port, 0, 0);
1403 sx_reconfigure_port(port);
1406 func_exit();
1413 /* ********************************************************************** *
1414 * Here are the routines that actually *
1415 * interface with the rest of the system *
1416 * ********************************************************************** */
1418 static int sx_open (struct tty_struct * tty, struct file * filp)
1420 struct sx_port *port;
1421 int retval, line;
1423 func_enter();
1425 if (!sx_initialized) {
1426 return -EIO;
1429 line = MINOR(tty->device);
1430 sx_dprintk (SX_DEBUG_OPEN, "%d: opening line %d. tty=%p ctty=%p, np=%d)\n",
1431 current->pid, line, tty, current->tty, sx_nports);
1433 if ((line < 0) || (line >= SX_NPORTS) || (line >= sx_nports))
1434 return -ENODEV;
1436 port = & sx_ports[line];
1437 port->c_dcd = 0; /* Make sure that the first interrupt doesn't detect a
1438 1 -> 0 transition. */
1441 sx_dprintk (SX_DEBUG_OPEN, "port = %p c_dcd = %d\n", port, port->c_dcd);
1443 tty->driver_data = port;
1444 port->gs.tty = tty;
1445 if (!port->gs.count)
1446 MOD_INC_USE_COUNT;
1447 port->gs.count++;
1449 sx_dprintk (SX_DEBUG_OPEN, "starting port\n");
1452 * Start up serial port
1454 retval = gs_init_port(&port->gs);
1455 sx_dprintk (SX_DEBUG_OPEN, "done gs_init\n");
1456 if (retval) {
1457 port->gs.count--;
1458 if (port->gs.count) MOD_DEC_USE_COUNT;
1459 return retval;
1462 port->gs.flags |= GS_ACTIVE;
1463 sx_setsignals (port, 1,1);
1465 #if 0
1466 if (sx_debug & SX_DEBUG_OPEN)
1467 my_hd ((unsigned char *)port, sizeof (*port));
1468 #else
1469 if (sx_debug & SX_DEBUG_OPEN)
1470 my_hd ((unsigned char *)port->board->base + port->ch_base,
1471 sizeof (*port));
1472 #endif
1474 if (sx_send_command (port, HS_LOPEN, -1, HS_IDLE_OPEN) != 1) {
1475 printk (KERN_ERR "sx: Card didn't respond to LOPEN command.\n");
1476 port->gs.count--;
1477 if (!port->gs.count) MOD_DEC_USE_COUNT;
1478 return -EIO;
1481 retval = block_til_ready(port, filp);
1482 sx_dprintk (SX_DEBUG_OPEN, "Block til ready returned %d. Count=%d\n",
1483 retval, port->gs.count);
1485 if (retval) {
1487 * Don't lower gs.count here because sx_close() will be called later
1490 return retval;
1492 /* tty->low_latency = 1; */
1494 if ((port->gs.count == 1) && (port->gs.flags & ASYNC_SPLIT_TERMIOS)) {
1495 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1496 *tty->termios = port->gs.normal_termios;
1497 else
1498 *tty->termios = port->gs.callout_termios;
1499 sx_set_real_termios (port);
1502 port->gs.session = current->session;
1503 port->gs.pgrp = current->pgrp;
1504 port->c_dcd = sx_get_CD (port);
1505 sx_dprintk (SX_DEBUG_OPEN, "at open: cd=%d\n", port->c_dcd);
1506 func_exit();
1507 return 0;
1512 /* I haven't the foggiest why the decrement use count has to happen
1513 here. The whole linux serial drivers stuff needs to be redesigned.
1514 My guess is that this is a hack to minimize the impact of a bug
1515 elsewhere. Thinking about it some more. (try it sometime) Try
1516 running minicom on a serial port that is driven by a modularized
1517 driver. Have the modem hangup. Then remove the driver module. Then
1518 exit minicom. I expect an "oops". -- REW */
1519 static void sx_hungup (void *ptr)
1521 struct sx_port *port = ptr;
1522 func_enter ();
1524 sx_setsignals (port, 0, 0);
1525 sx_reconfigure_port(port);
1526 sx_send_command (port, HS_CLOSE, 0, 0);
1528 if (sx_read_channel_byte (port, hi_hstat) != HS_IDLE_CLOSED) {
1529 if (sx_send_command (port, HS_FORCE_CLOSED, -1, HS_IDLE_CLOSED) != 1) {
1530 printk (KERN_ERR
1531 "sx: sent the force_close command, but card didn't react\n");
1532 } else
1533 sx_dprintk (SX_DEBUG_CLOSE, "sent the force_close command.\n");
1536 MOD_DEC_USE_COUNT;
1537 func_exit ();
1541 static void sx_close (void *ptr)
1543 struct sx_port *port = ptr;
1544 /* Give the port 5 seconds to close down. */
1545 int to = 5 * HZ;
1547 func_enter ();
1549 sx_setsignals (port, 0, 0);
1550 sx_reconfigure_port(port);
1551 sx_send_command (port, HS_CLOSE, 0, 0);
1553 while (to-- && (sx_read_channel_byte (port, hi_hstat) != HS_IDLE_CLOSED)) {
1554 current->state = TASK_INTERRUPTIBLE;
1555 schedule_timeout (1);
1556 if (signal_pending (current))
1557 break;
1559 current->state = TASK_RUNNING;
1560 if (sx_read_channel_byte (port, hi_hstat) != HS_IDLE_CLOSED) {
1561 if (sx_send_command (port, HS_FORCE_CLOSED, -1, HS_IDLE_CLOSED) != 1) {
1562 printk (KERN_ERR
1563 "sx: sent the force_close command, but card didn't react\n");
1564 } else
1565 sx_dprintk (SX_DEBUG_CLOSE, "sent the force_close command.\n");
1568 sx_dprintk (SX_DEBUG_CLOSE, "waited %d jiffies for close. count=%d\n",
1569 5 * HZ - to - 1, port->gs.count);
1571 if(port->gs.count) {
1572 sx_dprintk(SX_DEBUG_CLOSE, "WARNING port count:%d\n", port->gs.count);
1573 port->gs.count = 0;
1576 MOD_DEC_USE_COUNT;
1577 func_exit ();
1582 /* This is relatively thorough. But then again it is only 20 lines. */
1583 #define MARCHUP for (i=min;i<max;i++)
1584 #define MARCHDOWN for (i=max-1;i>=min;i--)
1585 #define W0 write_sx_byte (board, i, 0x55)
1586 #define W1 write_sx_byte (board, i, 0xaa)
1587 #define R0 if (read_sx_byte (board, i) != 0x55) return 1
1588 #define R1 if (read_sx_byte (board, i) != 0xaa) return 1
1590 /* This memtest takes a human-noticable time. You normally only do it
1591 once a boot, so I guess that it is worth it. */
1592 int do_memtest (struct sx_board *board, int min, int max)
1594 int i;
1596 /* This is a marchb. Theoretically, marchb catches much more than
1597 simpler tests. In practise, the longer test just catches more
1598 intermittent errors. -- REW
1599 (For the theory behind memory testing see:
1600 Testing Semiconductor Memories by A.J. van de Goor.) */
1601 MARCHUP {W0;}
1602 MARCHUP {R0;W1;R1;W0;R0;W1;}
1603 MARCHUP {R1;W0;W1;}
1604 MARCHDOWN {R1;W0;W1;W0;}
1605 MARCHDOWN {R0;W1;W0;}
1607 return 0;
1611 #undef MARCHUP
1612 #undef MARCHDOWN
1613 #undef W0
1614 #undef W1
1615 #undef R0
1616 #undef R1
1618 #define MARCHUP for (i=min;i<max;i+=2)
1619 #define MARCHDOWN for (i=max-1;i>=min;i-=2)
1620 #define W0 write_sx_word (board, i, 0x55aa)
1621 #define W1 write_sx_word (board, i, 0xaa55)
1622 #define R0 if (read_sx_word (board, i) != 0x55aa) return 1
1623 #define R1 if (read_sx_word (board, i) != 0xaa55) return 1
1625 /* This memtest takes a human-noticable time. You normally only do it
1626 once a boot, so I guess that it is worth it. */
1627 int do_memtest_w (struct sx_board *board, int min, int max)
1629 int i;
1631 MARCHUP {W0;}
1632 MARCHUP {R0;W1;R1;W0;R0;W1;}
1633 MARCHUP {R1;W0;W1;}
1634 MARCHDOWN {R1;W0;W1;W0;}
1635 MARCHDOWN {R0;W1;W0;}
1637 return 0;
1641 static int sx_fw_ioctl (struct inode *inode, struct file *filp,
1642 unsigned int cmd, unsigned long arg)
1644 int rc = 0;
1645 int *descr = (int *)arg, i;
1646 static struct sx_board *board = NULL;
1647 int nbytes, offset, data;
1648 char *tmp;
1650 func_enter();
1652 #if 0
1653 /* Removed superuser check: Sysops can use the permissions on the device
1654 file to restrict access. Recommendation: Root only. (root.root 600) */
1655 if (!suser ()) {
1656 return -EPERM;
1658 #endif
1660 sx_dprintk (SX_DEBUG_FIRMWARE, "IOCTL %x: %lx\n", cmd, arg);
1662 if (!board) board = &boards[0];
1663 if (board->flags & SX_BOARD_PRESENT) {
1664 sx_dprintk (SX_DEBUG_FIRMWARE, "Board present! (%x)\n",
1665 board->flags);
1666 } else {
1667 sx_dprintk (SX_DEBUG_FIRMWARE, "Board not present! (%x) all:",
1668 board->flags);
1669 for (i=0;i< SX_NBOARDS;i++)
1670 sx_dprintk (SX_DEBUG_FIRMWARE, "<%x> ", boards[i].flags);
1671 sx_dprintk (SX_DEBUG_FIRMWARE, "\n");
1672 return -EIO;
1675 switch (cmd) {
1676 case SXIO_SET_BOARD:
1677 sx_dprintk (SX_DEBUG_FIRMWARE, "set board to %ld\n", arg);
1678 if (arg > SX_NBOARDS) return -EIO;
1679 sx_dprintk (SX_DEBUG_FIRMWARE, "not out of range\n");
1680 if (!(boards[arg].flags & SX_BOARD_PRESENT)) return -EIO;
1681 sx_dprintk (SX_DEBUG_FIRMWARE, ".. and present!\n");
1682 board = &boards[arg];
1683 break;
1684 case SXIO_GET_TYPE:
1685 rc = -ENOENT; /* If we manage to miss one, return error. */
1686 if (IS_SX_BOARD (board)) rc = SX_TYPE_SX;
1687 if (IS_CF_BOARD (board)) rc = SX_TYPE_CF;
1688 if (IS_SI_BOARD (board)) rc = SX_TYPE_SI;
1689 if (IS_EISA_BOARD (board)) rc = SX_TYPE_SI;
1690 sx_dprintk (SX_DEBUG_FIRMWARE, "returning type= %d\n", rc);
1691 break;
1692 case SXIO_DO_RAMTEST:
1693 if (sx_initialized) /* Already initialized: better not ramtest the board. */
1694 return -EPERM;
1695 if (IS_SX_BOARD (board)) {
1696 rc = do_memtest (board, 0, 0x7000);
1697 if (!rc) rc = do_memtest (board, 0, 0x7000);
1698 /*if (!rc) rc = do_memtest_w (board, 0, 0x7000);*/
1699 } else {
1700 rc = do_memtest (board, 0, 0x7ff8);
1701 /* if (!rc) rc = do_memtest_w (board, 0, 0x7ff8); */
1703 sx_dprintk (SX_DEBUG_FIRMWARE, "returning memtest result= %d\n", rc);
1704 break;
1705 case SXIO_DOWNLOAD:
1706 if (sx_initialized) /* Already initialized */
1707 return -EEXIST;
1708 if (!sx_reset (board))
1709 return -EIO;
1710 sx_dprintk (SX_DEBUG_INIT, "reset the board...\n");
1712 tmp = kmalloc (SX_CHUNK_SIZE, GFP_USER);
1713 if (!tmp) return -ENOMEM;
1714 Get_user (nbytes, descr++);
1715 Get_user (offset, descr++);
1716 Get_user (data, descr++);
1717 while (nbytes && data) {
1718 for (i=0;i<nbytes;i += SX_CHUNK_SIZE) {
1719 copy_from_user (tmp, (char *)data+i,
1720 (i+SX_CHUNK_SIZE>nbytes)?nbytes-i:SX_CHUNK_SIZE);
1721 memcpy_toio ((char *) (board->base2 + offset + i), tmp,
1722 (i+SX_CHUNK_SIZE>nbytes)?nbytes-i:SX_CHUNK_SIZE);
1725 Get_user (nbytes, descr++);
1726 Get_user (offset, descr++);
1727 Get_user (data, descr++);
1729 kfree (tmp);
1730 sx_nports += sx_init_board (board);
1731 rc = sx_nports;
1732 break;
1733 case SXIO_INIT:
1734 if (sx_initialized) /* Already initialized */
1735 return -EEXIST;
1736 /* This is not allowed until all boards are initialized... */
1737 for (i=0;i<SX_NBOARDS;i++) {
1738 if ( (boards[i].flags & SX_BOARD_PRESENT) &&
1739 !(boards[i].flags & SX_BOARD_INITIALIZED))
1740 return -EIO;
1742 for (i=0;i<SX_NBOARDS;i++)
1743 if (!(boards[i].flags & SX_BOARD_PRESENT)) break;
1745 sx_dprintk (SX_DEBUG_FIRMWARE, "initing portstructs, %d boards, "
1746 "%d channels, first board: %d ports\n",
1747 i, sx_nports, boards[0].nports);
1748 rc = sx_init_portstructs (i, sx_nports);
1749 sx_init_drivers ();
1750 if (rc >= 0)
1751 sx_initialized++;
1752 break;
1753 case SXIO_SETDEBUG:
1754 sx_debug = arg;
1755 break;
1756 case SXIO_GETDEBUG:
1757 rc = sx_debug;
1758 break;
1759 case SXIO_SETGSDEBUG:
1760 gs_debug = arg;
1761 break;
1762 case SXIO_GETGSDEBUG:
1763 rc = gs_debug;
1764 break;
1765 case SXIO_GETNPORTS:
1766 rc = sx_nports;
1767 break;
1768 default:
1769 printk (KERN_WARNING "Unknown ioctl on firmware device (%x).\n", cmd);
1770 break;
1772 func_exit ();
1773 return rc;
1777 static int sx_ioctl (struct tty_struct * tty, struct file * filp,
1778 unsigned int cmd, unsigned long arg)
1780 int rc;
1781 struct sx_port *port = tty->driver_data;
1782 int ival;
1784 /* func_enter2(); */
1786 rc = 0;
1787 switch (cmd) {
1788 case TIOCGSOFTCAR:
1789 rc = Put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
1790 (unsigned int *) arg);
1791 break;
1792 case TIOCSSOFTCAR:
1793 if ((rc = verify_area(VERIFY_READ, (void *) arg,
1794 sizeof(int))) == 0) {
1795 Get_user(ival, (unsigned int *) arg);
1796 tty->termios->c_cflag =
1797 (tty->termios->c_cflag & ~CLOCAL) |
1798 (ival ? CLOCAL : 0);
1800 break;
1801 case TIOCGSERIAL:
1802 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
1803 sizeof(struct serial_struct))) == 0)
1804 gs_getserial(&port->gs, (struct serial_struct *) arg);
1805 break;
1806 case TIOCSSERIAL:
1807 if ((rc = verify_area(VERIFY_READ, (void *) arg,
1808 sizeof(struct serial_struct))) == 0)
1809 rc = gs_setserial(&port->gs, (struct serial_struct *) arg);
1810 break;
1811 case TIOCMGET:
1812 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
1813 sizeof(unsigned int))) == 0) {
1814 ival = sx_getsignals(port);
1815 put_user(ival, (unsigned int *) arg);
1817 break;
1818 case TIOCMBIS:
1819 if ((rc = verify_area(VERIFY_READ, (void *) arg,
1820 sizeof(unsigned int))) == 0) {
1821 Get_user(ival, (unsigned int *) arg);
1822 sx_setsignals(port, ((ival & TIOCM_DTR) ? 1 : -1),
1823 ((ival & TIOCM_RTS) ? 1 : -1));
1824 sx_reconfigure_port(port);
1826 break;
1827 case TIOCMBIC:
1828 if ((rc = verify_area(VERIFY_READ, (void *) arg,
1829 sizeof(unsigned int))) == 0) {
1830 Get_user(ival, (unsigned int *) arg);
1831 sx_setsignals(port, ((ival & TIOCM_DTR) ? 0 : -1),
1832 ((ival & TIOCM_RTS) ? 0 : -1));
1833 sx_reconfigure_port(port);
1835 break;
1836 case TIOCMSET:
1837 if ((rc = verify_area(VERIFY_READ, (void *) arg,
1838 sizeof(unsigned int))) == 0) {
1839 Get_user(ival, (unsigned int *) arg);
1840 sx_setsignals(port, ((ival & TIOCM_DTR) ? 1 : 0),
1841 ((ival & TIOCM_RTS) ? 1 : 0));
1842 sx_reconfigure_port(port);
1844 break;
1846 default:
1847 rc = -ENOIOCTLCMD;
1848 break;
1851 /* func_exit(); */
1852 return rc;
1856 /* The throttle/unthrottle scheme for the Specialix card is different
1857 * from other drivers and deserves some explanation.
1858 * The Specialix hardware takes care of XON/XOFF
1859 * and CTS/RTS flow control itself. This means that all we have to
1860 * do when signalled by the upper tty layer to throttle/unthrottle is
1861 * to make a note of it here. When we come to read characters from the
1862 * rx buffers on the card (sx_receive_chars()) we look to see if the
1863 * upper layer can accept more (as noted here in sx_rx_throt[]).
1864 * If it can't we simply don't remove chars from the cards buffer.
1865 * When the tty layer can accept chars, we again note that here and when
1866 * sx_receive_chars() is called it will remove them from the cards buffer.
1867 * The card will notice that a ports buffer has drained below some low
1868 * water mark and will unflow control the line itself, using whatever
1869 * flow control scheme is in use for that port. -- Simon Allen
1872 static void sx_throttle (struct tty_struct * tty)
1874 struct sx_port *port = (struct sx_port *)tty->driver_data;
1876 func_enter2();
1877 /* If the port is using any type of input flow
1878 * control then throttle the port.
1880 if((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty)) ) {
1881 port->gs.flags |= SX_RX_THROTTLE;
1883 func_exit();
1887 static void sx_unthrottle (struct tty_struct * tty)
1889 struct sx_port *port = (struct sx_port *)tty->driver_data;
1891 func_enter2();
1892 /* Always unthrottle even if flow control is not enabled on
1893 * this port in case we disabled flow control while the port
1894 * was throttled
1896 port->gs.flags &= ~SX_RX_THROTTLE;
1897 func_exit();
1898 return;
1902 /* ********************************************************************** *
1903 * Here are the initialization routines. *
1904 * ********************************************************************** */
1909 static int sx_init_board (struct sx_board *board)
1911 int addr;
1912 int chans;
1913 int type;
1915 func_enter();
1917 /* This is preceded by downloading the download code. */
1919 board->flags |= SX_BOARD_INITIALIZED;
1921 if (read_sx_byte (board, 0))
1922 /* CF boards may need this. */
1923 write_sx_byte(board,0, 0);
1925 /* This resets the processor again, to make sure it didn't do any
1926 foolish things while we were downloading the image */
1927 if (!sx_reset (board))
1928 return 0;
1930 sx_start_board (board);
1931 udelay (10);
1932 if (!sx_busy_wait_neq (board, 0, 0xff, 0)) {
1933 printk (KERN_ERR "sx: Ooops. Board won't initialize.\n");
1934 return 0;
1937 /* Ok. So now the processor on the card is running. It gathered
1938 some info for us... */
1939 sx_dprintk (SX_DEBUG_INIT, "The sxcard structure:\n");
1940 if (sx_debug & SX_DEBUG_INIT) my_hd ((char *)(board->base), 0x10);
1941 sx_dprintk (SX_DEBUG_INIT, "the first sx_module structure:\n");
1942 if (sx_debug & SX_DEBUG_INIT) my_hd ((char *)(board->base + 0x80), 0x30);
1944 sx_dprintk (SX_DEBUG_INIT,
1945 "init_status: %x, %dk memory, firmware V%x.%02x,\n",
1946 read_sx_byte (board, 0), read_sx_byte(board, 1),
1947 read_sx_byte (board, 5), read_sx_byte(board, 4));
1949 if (read_sx_byte (board, 0) == 0xff) {
1950 printk (KERN_INFO "sx: No modules found. Sorry.\n");
1951 board->nports = 0;
1952 return 0;
1955 chans = 0;
1957 if (IS_SX_BOARD(board)) {
1958 sx_write_board_word (board, cc_int_count, sx_maxints);
1959 } else {
1960 if (sx_maxints)
1961 sx_write_board_word (board, cc_int_count, SI_PROCESSOR_CLOCK/8/sx_maxints);
1964 /* grab the first module type... */
1965 /* board->ta_type = mod_compat_type (read_sx_byte (board, 0x80 + 0x08)); */
1966 board->ta_type = mod_compat_type (sx_read_module_byte (board, 0x80, mc_chip));
1968 /* XXX byteorder */
1969 for (addr = 0x80;addr != 0;addr = read_sx_word (board, addr) & 0x7fff) {
1970 type = sx_read_module_byte (board, addr, mc_chip);
1971 sx_dprintk (SX_DEBUG_INIT, "Module at %x: %d channels\n",
1972 addr, read_sx_byte (board, addr + 2));
1974 chans += sx_read_module_byte (board, addr, mc_type);
1976 sx_dprintk (SX_DEBUG_INIT, "module is an %s, which has %s/%s panels\n",
1977 mod_type_s (type),
1978 pan_type_s (sx_read_module_byte (board, addr, mc_mods) & 0xf),
1979 pan_type_s (sx_read_module_byte (board, addr, mc_mods) >> 4));
1981 sx_dprintk (SX_DEBUG_INIT, "CD1400 versions: %x/%x, ASIC version: %x\n",
1982 sx_read_module_byte (board, addr, mc_rev1),
1983 sx_read_module_byte (board, addr, mc_rev2),
1984 sx_read_module_byte (board, addr, mc_mtaasic_rev));
1986 /* The following combinations are illegal: It should theoretically
1987 work, but timing problems make the bus HANG. */
1989 if (mod_compat_type (type) != board->ta_type) {
1990 printk (KERN_ERR "sx: This is an invalid configuration.\n"
1991 "Don't mix TA/MTA/SXDC on the same hostadapter.\n");
1992 chans=0;
1993 break;
1995 if ((IS_EISA_BOARD(board) ||
1996 IS_SI_BOARD(board)) && (mod_compat_type(type) == 4)) {
1997 printk (KERN_ERR "sx: This is an invalid configuration.\n"
1998 "Don't use SXDCs on an SI/XIO adapter.\n");
1999 chans=0;
2000 break;
2002 #if 0 /* Problem fixed: firmware 3.05 */
2003 if (IS_SX_BOARD(board) && (type == TA8)) {
2004 /* There are some issues with the firmware and the DCD/RTS
2005 lines. It might work if you tie them together or something.
2006 It might also work if you get a newer sx_firmware. Therefore
2007 this is just a warning. */
2008 printk (KERN_WARNING "sx: The SX host doesn't work too well "
2009 "with the TA8 adapters.\nSpecialix is working on it.\n");
2011 #endif
2014 if (chans) {
2015 /* board->flags |= SX_BOARD_PRESENT; */
2016 if(board->irq > 0) {
2017 /* fixed irq, probably PCI */
2018 if(sx_irqmask & (1 << board->irq)) { /* may we use this irq? */
2019 if(request_irq(board->irq, sx_interrupt, SA_SHIRQ | SA_INTERRUPT, "sx", board)) {
2020 printk(KERN_ERR "sx: Cannot allocate irq %d.\n", board->irq);
2021 board->irq = 0;
2023 } else
2024 board->irq = 0;
2025 } else if(board->irq < 0 && sx_irqmask) {
2026 /* auto-allocate irq */
2027 int irqnr;
2028 int irqmask = sx_irqmask & (IS_SX_BOARD(board) ? SX_ISA_IRQ_MASK : SI2_ISA_IRQ_MASK);
2029 for(irqnr = 15; irqnr > 0; irqnr--)
2030 if(irqmask & (1 << irqnr))
2031 if(! request_irq(irqnr, sx_interrupt, SA_SHIRQ | SA_INTERRUPT, "sx", board))
2032 break;
2033 if(! irqnr)
2034 printk(KERN_ERR "sx: Cannot allocate IRQ.\n");
2035 board->irq = irqnr;
2036 } else
2037 board->irq = 0;
2039 if (board->irq) {
2040 /* Found a valid interrupt, start up interrupts! */
2041 sx_dprintk (SX_DEBUG_INIT, "Using irq %d.\n", board->irq);
2042 sx_start_interrupts (board);
2043 board->poll = sx_slowpoll;
2044 board->flags |= SX_IRQ_ALLOCATED;
2045 } else {
2046 /* no irq: setup board for polled operation */
2047 board->poll = sx_poll;
2048 sx_dprintk (SX_DEBUG_INIT, "Using poll-interval %d.\n", board->poll);
2051 /* The timer should be initialized anyway: That way we can safely
2052 del_timer it when the module is unloaded. */
2053 init_timer (&board->timer);
2055 if (board->poll) {
2056 board->timer.data = (unsigned long) board;
2057 board->timer.function = sx_pollfunc;
2058 board->timer.expires = jiffies + board->poll;
2059 add_timer (&board->timer);
2061 } else {
2062 board->irq = 0;
2065 board->nports = chans;
2066 sx_dprintk (SX_DEBUG_INIT, "returning %d ports.", board->nports);
2068 func_exit();
2069 return chans;
2073 void printheader(void)
2075 static int header_printed = 0;
2077 if (!header_printed) {
2078 printk (KERN_INFO "Specialix SX driver "
2079 "(C) 1998/1999 R.E.Wolff@BitWizard.nl \n");
2080 printk (KERN_INFO "sx: version %s\n", RCS_ID);
2081 header_printed = 1;
2086 int probe_sx (struct sx_board *board)
2088 struct vpd_prom vpdp;
2089 char *p;
2090 int i;
2092 func_enter();
2094 if (!IS_CF_BOARD (board)) {
2095 sx_dprintk (SX_DEBUG_PROBE, "Going to verify vpd prom at %x.\n",
2096 board->base + SX_VPD_ROM);
2098 if (sx_debug & SX_DEBUG_PROBE)
2099 my_hd ((char *)(board->base + SX_VPD_ROM), 0x40);
2101 p = (char *) &vpdp;
2102 for (i=0;i< sizeof (struct vpd_prom);i++)
2103 *p++ = read_sx_byte (board, SX_VPD_ROM + i*2);
2105 if (sx_debug & SX_DEBUG_PROBE)
2106 my_hd ((char *)&vpdp, 0x20);
2108 sx_dprintk (SX_DEBUG_PROBE, "checking identifier...\n");
2110 if (strncmp (vpdp.identifier, SX_VPD_IDENT_STRING, 16) != 0) {
2111 sx_dprintk (SX_DEBUG_PROBE, "Got non-SX identifier: '%s'\n",
2112 vpdp.identifier);
2113 return 0;
2117 printheader ();
2119 if (!IS_CF_BOARD (board)) {
2120 printk (KERN_DEBUG "sx: Found an SX board at %x\n", board->hw_base);
2121 printk (KERN_DEBUG "sx: hw_rev: %d, assembly level: %d, uniq ID:%08x, ",
2122 vpdp.hwrev, vpdp.hwass, vpdp.uniqid);
2123 printk ( "Manufactured: %d/%d\n",
2124 1970 + vpdp.myear, vpdp.mweek);
2127 if ((((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) != SX_PCI_UNIQUEID1) &&
2128 (((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) != SX_ISA_UNIQUEID1)) {
2129 /* This might be a bit harsh. This was the primary reason the
2130 SX/ISA card didn't work at first... */
2131 printk (KERN_ERR "sx: Hmm. Not an SX/PCI or SX/ISA card. Sorry: giving up.\n");
2132 return (0);
2135 if (((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) == SX_ISA_UNIQUEID1) {
2136 if (board->base & 0x8000) {
2137 printk (KERN_WARNING "sx: Warning: There may be hardware problems with the card at %x.\n", board->base);
2138 printk (KERN_WARNING "sx: Read sx.txt for more info.\n");
2143 board->nports = -1;
2145 /* This resets the processor, and keeps it off the bus. */
2146 if (!sx_reset (board))
2147 return 0;
2148 sx_dprintk (SX_DEBUG_INIT, "reset the board...\n");
2150 board->flags |= SX_BOARD_PRESENT;
2152 func_exit();
2153 return 1;
2158 /* Specialix probes for this card at 32k increments from 640k to 16M.
2159 I consider machines with less than 16M unlikely nowadays, so I'm
2160 not probing above 1Mb. Also, 0xa0000, 0xb0000, are taken by the VGA
2161 card. 0xe0000 and 0xf0000 are taken by the BIOS. That only leaves
2162 0xc0000, 0xc8000, 0xd0000 and 0xd8000 . */
2164 int probe_si (struct sx_board *board)
2166 int i;
2168 func_enter();
2169 sx_dprintk (SX_DEBUG_PROBE, "Going to verify SI signature %x.\n",
2170 board->base + SI2_ISA_ID_BASE);
2172 if (sx_debug & SX_DEBUG_PROBE)
2173 my_hd ((char *)(board->base + SI2_ISA_ID_BASE), 0x8);
2175 if (!IS_EISA_BOARD(board)) {
2176 for (i=0;i<8;i++) {
2177 if ((read_sx_byte (board, SI2_ISA_ID_BASE+7-i) & 7) != i) {
2178 return 0;
2183 printheader ();
2185 printk (KERN_DEBUG "sx: Found an SI board at %x\n", board->hw_base);
2186 /* Compared to the SX boards, it is a complete guess as to what
2187 this card is up to... */
2189 board->nports = -1;
2191 /* This resets the processor, and keeps it off the bus. */
2192 if (!sx_reset (board))
2193 return 0;
2194 sx_dprintk (SX_DEBUG_INIT, "reset the board...\n");
2196 board->flags |= SX_BOARD_PRESENT;
2198 func_exit();
2199 return 1;
2203 static int sx_init_drivers(void)
2205 int error;
2207 func_enter();
2209 memset(&sx_driver, 0, sizeof(sx_driver));
2210 sx_driver.magic = TTY_DRIVER_MAGIC;
2211 sx_driver.driver_name = "specialix_sx";
2212 sx_driver.name = "ttyX";
2213 sx_driver.major = SX_NORMAL_MAJOR;
2214 sx_driver.num = sx_nports;
2215 sx_driver.type = TTY_DRIVER_TYPE_SERIAL;
2216 sx_driver.subtype = SX_TYPE_NORMAL;
2217 sx_driver.init_termios = tty_std_termios;
2218 sx_driver.init_termios.c_cflag =
2219 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2220 sx_driver.flags = TTY_DRIVER_REAL_RAW;
2221 sx_driver.refcount = &sx_refcount;
2222 sx_driver.table = sx_table;
2223 sx_driver.termios = sx_termios;
2224 sx_driver.termios_locked = sx_termios_locked;
2226 sx_driver.open = sx_open;
2227 sx_driver.close = gs_close;
2228 sx_driver.write = gs_write;
2229 sx_driver.put_char = gs_put_char;
2230 sx_driver.flush_chars = gs_flush_chars;
2231 sx_driver.write_room = gs_write_room;
2232 sx_driver.chars_in_buffer = gs_chars_in_buffer;
2233 sx_driver.flush_buffer = gs_flush_buffer;
2234 sx_driver.ioctl = sx_ioctl;
2235 sx_driver.throttle = sx_throttle;
2236 sx_driver.unthrottle = sx_unthrottle;
2237 sx_driver.set_termios = gs_set_termios;
2238 sx_driver.stop = gs_stop;
2239 sx_driver.start = gs_start;
2240 sx_driver.hangup = gs_hangup;
2242 sx_callout_driver = sx_driver;
2243 sx_callout_driver.name = "cux";
2244 sx_callout_driver.major = SX_CALLOUT_MAJOR;
2245 sx_callout_driver.subtype = SX_TYPE_CALLOUT;
2247 if ((error = tty_register_driver(&sx_driver))) {
2248 printk(KERN_ERR "sx: Couldn't register sx driver, error = %d\n",
2249 error);
2250 return 1;
2252 if ((error = tty_register_driver(&sx_callout_driver))) {
2253 tty_unregister_driver(&sx_driver);
2254 printk(KERN_ERR "sx: Couldn't register sx callout driver, error = %d\n",
2255 error);
2256 return 1;
2259 func_exit();
2260 return 0;
2264 void * ckmalloc (int size)
2266 void *p;
2268 p = kmalloc(size, GFP_KERNEL);
2269 if (p)
2270 memset(p, 0, size);
2271 return p;
2275 static int sx_init_portstructs (int nboards, int nports)
2277 struct sx_board *board;
2278 struct sx_port *port;
2279 int i, j;
2280 int addr, chans;
2281 int portno;
2283 func_enter();
2285 /* Many drivers statically allocate the maximum number of ports
2286 There is no reason not to allocate them dynamically. Is there? -- REW */
2287 sx_ports = ckmalloc(nports * sizeof (struct sx_port));
2288 if (!sx_ports)
2289 return -ENOMEM;
2291 sx_termios = ckmalloc(nports * sizeof (struct termios *));
2292 if (!sx_termios) {
2293 kfree (sx_ports);
2294 return -ENOMEM;
2297 sx_termios_locked = ckmalloc(nports * sizeof (struct termios *));
2298 if (!sx_termios_locked) {
2299 kfree (sx_ports);
2300 kfree (sx_termios);
2301 return -ENOMEM;
2304 /* Adjust the values in the "driver" */
2305 sx_driver.termios = sx_termios;
2306 sx_driver.termios_locked = sx_termios_locked;
2308 port = sx_ports;
2309 for (i = 0; i < nboards; i++) {
2310 board = &boards[i];
2311 board->ports = port;
2312 for (j=0; j < boards[i].nports;j++) {
2313 sx_dprintk (SX_DEBUG_INIT, "initing port %d\n", j);
2314 port->gs.callout_termios = tty_std_termios;
2315 port->gs.normal_termios = tty_std_termios;
2316 port->gs.magic = SX_MAGIC;
2317 port->gs.close_delay = HZ/2;
2318 port->gs.closing_wait = 30 * HZ;
2319 port->board = board;
2320 port->gs.rd = &sx_real_driver;
2321 #ifdef NEW_WRITE_LOCKING
2322 port->gs.port_write_sem = MUTEX;
2323 #endif
2325 * Initializing wait queue
2327 init_waitqueue_head(&port->gs.open_wait);
2328 init_waitqueue_head(&port->gs.close_wait);
2330 port++;
2334 port = sx_ports;
2335 portno = 0;
2336 for (i = 0; i < nboards; i++) {
2337 board = &boards[i];
2338 board->port_base = portno;
2339 /* Possibly the configuration was rejected. */
2340 sx_dprintk (SX_DEBUG_PROBE, "Board has %d channels\n", board->nports);
2341 if (board->nports <= 0) continue;
2342 /* XXX byteorder ?? */
2343 for (addr = 0x80;addr != 0;addr = read_sx_word (board, addr) & 0x7fff) {
2344 chans = sx_read_module_byte (board, addr, mc_type);
2345 sx_dprintk (SX_DEBUG_PROBE, "Module at %x: %d channels\n", addr, chans);
2346 sx_dprintk (SX_DEBUG_PROBE, "Port at");
2347 for (j=0;j<chans;j++) {
2348 /* The "sx-way" is the way it SHOULD be done. That way in the
2349 future, the firmware may for example pack the structures a bit
2350 more efficient. Neil tells me it isn't going to happen anytime
2351 soon though. */
2352 if (IS_SX_BOARD(board))
2353 port->ch_base = sx_read_module_word (board, addr+j*2, mc_chan_pointer);
2354 else
2355 port->ch_base = addr + 0x100 + 0x300*j;
2357 sx_dprintk (SX_DEBUG_PROBE, " %x", port->ch_base);
2358 port->line = portno++;
2359 port++;
2361 sx_dprintk (SX_DEBUG_PROBE, "\n");
2363 /* This has to be done earlier. */
2364 /* board->flags |= SX_BOARD_INITIALIZED; */
2367 func_exit();
2368 return 0;
2372 static void sx_release_drivers(void)
2374 func_enter();
2375 tty_unregister_driver(&sx_driver);
2376 tty_unregister_driver(&sx_callout_driver);
2377 func_exit();
2380 #ifdef TWO_ZERO
2381 #define PDEV unsigned char pci_bus, unsigned pci_fun
2382 #define pdev pci_bus, pci_fun
2383 #else
2384 #define PDEV struct pci_dev *pdev
2385 #endif
2388 #ifdef CONFIG_PCI
2389 /********************************************************
2390 * Setting bit 17 in the CNTRL register of the PLX 9050 *
2391 * chip forces a retry on writes while a read is pending.*
2392 * This is to prevent the card locking up on Intel Xeon *
2393 * multiprocessor systems with the NX chipset. -- NV *
2394 ********************************************************/
2396 /* Newer cards are produced with this bit set from the configuration
2397 EEprom. As the bit is read/write for the CPU, we can fix it here,
2398 if we detect that it isn't set correctly. -- REW */
2400 void fix_sx_pci (PDEV, struct sx_board *board)
2402 unsigned int hwbase;
2403 unsigned long rebase;
2404 unsigned int t;
2406 #define CNTRL_REG_OFFSET 0x50
2407 #define CNTRL_REG_GOODVALUE 0x18260000
2409 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &hwbase);
2410 hwbase &= PCI_BASE_ADDRESS_MEM_MASK;
2411 rebase = (ulong) ioremap(hwbase, 0x80);
2412 t = readl (rebase + CNTRL_REG_OFFSET);
2413 if (t != CNTRL_REG_GOODVALUE) {
2414 printk (KERN_DEBUG "sx: performing cntrl reg fix: %08x -> %08x\n", t, CNTRL_REG_GOODVALUE);
2415 writel (CNTRL_REG_GOODVALUE, rebase + CNTRL_REG_OFFSET);
2417 my_iounmap (hwbase, rebase);
2419 #endif
2422 #ifdef MODULE
2423 #define sx_init init_module
2424 #endif
2426 int sx_init(void)
2428 int i;
2429 int found = 0;
2430 int eisa_slot;
2431 struct sx_board *board;
2433 #ifdef CONFIG_PCI
2434 #ifndef TWO_ZERO
2435 struct pci_dev *pdev = NULL;
2436 #else
2437 unsigned char pci_bus, pci_fun;
2438 /* in 2.2.x pdev is a pointer defining a PCI device. In 2.0 its the bus/fn */
2439 #endif
2440 unsigned int tint;
2441 unsigned short tshort;
2442 #endif
2444 func_enter();
2445 sx_dprintk (SX_DEBUG_INIT, "Initing sx module... (sx_debug=%d)\n", sx_debug);
2446 if (abs ((long) (&sx_debug) - sx_debug) < 0x10000) {
2447 printk (KERN_WARNING "sx: sx_debug is an address, instead of a value. "
2448 "Assuming -1.\n");
2449 printk ("(%p)\n", &sx_debug);
2450 sx_debug=-1;
2453 #ifdef CONFIG_PCI
2454 if (pci_present ()) {
2455 #ifndef TWO_ZERO
2456 while ((pdev = pci_find_device (PCI_VENDOR_ID_SPECIALIX,
2457 PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8,
2458 pdev))) {
2459 if (pci_enable_device(pdev))
2460 continue;
2461 #else
2462 for (i=0;i< SX_NBOARDS;i++) {
2463 if (pcibios_find_device (PCI_VENDOR_ID_SPECIALIX,
2464 PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8, i,
2465 &pci_bus, &pci_fun)) break;
2466 #endif
2467 /* Specialix has a whole bunch of cards with
2468 0x2000 as the device ID. They say its because
2469 the standard requires it. Stupid standard. */
2470 /* It seems that reading a word doesn't work reliably on 2.0.
2471 Also, reading a non-aligned dword doesn't work. So we read the
2472 whole dword at 0x2c and extract the word at 0x2e (SUBSYSTEM_ID)
2473 ourselves */
2474 /* I don't know why the define doesn't work, constant 0x2c does --REW */
2475 pci_read_config_dword (pdev, 0x2c, &tint);
2476 tshort = (tint >> 16) & 0xffff;
2477 sx_dprintk (SX_DEBUG_PROBE, "Got a specialix card: %x.\n", tint);
2478 /* sx_dprintk (SX_DEBUG_PROBE, "pdev = %d/%d (%x)\n", pdev, tint); */
2479 if ((tshort != 0x0200) && (tshort != 0x0300)) {
2480 sx_dprintk (SX_DEBUG_PROBE, "But it's not an SX card (%d)...\n",
2481 tshort);
2482 continue;
2484 board = &boards[found];
2486 board->flags &= ~SX_BOARD_TYPE;
2487 board->flags |= (tshort == 0x200)?SX_PCI_BOARD:
2488 SX_CFPCI_BOARD;
2490 /* CF boards use base address 3.... */
2491 if (IS_CF_BOARD (board))
2492 board->hw_base = pci_resource_start (pdev, 3);
2493 else
2494 board->hw_base = pci_resource_start (pdev, 2);
2495 board->base2 =
2496 board->base = (ulong) ioremap(board->hw_base, WINDOW_LEN (board));
2497 if (!board->base) {
2498 printk(KERN_ERR "ioremap failed\n");
2499 /* XXX handle error */
2502 /* Most of the stuff on the CF board is offset by
2503 0x18000 .... */
2504 if (IS_CF_BOARD (board)) board->base += 0x18000;
2506 board->irq = get_irq (pdev);
2508 sx_dprintk (SX_DEBUG_PROBE, "Got a specialix card: %x/%x(%d) %x.\n",
2509 tint, boards[found].base, board->irq, board->flags);
2511 if (probe_sx (board)) {
2512 found++;
2513 fix_sx_pci (pdev, board);
2514 } else
2515 my_iounmap (board->hw_base, board->base);
2518 #endif
2520 for (i=0;i<NR_SX_ADDRS;i++) {
2521 board = &boards[found];
2522 board->hw_base = sx_probe_addrs[i];
2523 board->base2 =
2524 board->base = (ulong) ioremap(board->hw_base, SX_WINDOW_LEN);
2525 board->flags &= ~SX_BOARD_TYPE;
2526 board->flags |= SX_ISA_BOARD;
2527 board->irq = sx_irqmask?-1:0;
2529 if (probe_sx (board)) {
2530 found++;
2531 } else {
2532 my_iounmap (board->hw_base, board->base);
2536 for (i=0;i<NR_SI_ADDRS;i++) {
2537 board = &boards[found];
2538 board->hw_base = si_probe_addrs[i];
2539 board->base2 =
2540 board->base = (ulong) ioremap(board->hw_base, SI2_ISA_WINDOW_LEN);
2541 board->flags &= ~SX_BOARD_TYPE;
2542 board->flags |= SI_ISA_BOARD;
2543 board->irq = sx_irqmask ?-1:0;
2545 if (probe_si (board)) {
2546 found++;
2547 } else {
2548 my_iounmap (board->hw_base, board->base);
2552 sx_dprintk(SX_DEBUG_PROBE, "Probing for EISA cards\n");
2553 for(eisa_slot=0x1000; eisa_slot<0x10000; eisa_slot+=0x1000)
2555 if((inb(eisa_slot+0xc80)==0x4d) &&
2556 (inb(eisa_slot+0xc81)==0x98))
2558 sx_dprintk(SX_DEBUG_PROBE, "%s : Signature found in EISA slot %d, Product %d Rev %d\n",
2559 "XIO", (eisa_slot>>12), inb(eisa_slot+0xc82), inb(eisa_slot+0xc83));
2561 board = &boards[found];
2562 board->eisa_base = eisa_slot;
2563 board->flags &= ~SX_BOARD_TYPE;
2564 board->flags |= SI_EISA_BOARD;
2566 board->hw_base = (((inb(0xc01+eisa_slot) << 8) + inb(0xc00+eisa_slot)) << 16);
2567 board->base2 =
2568 board->base = (ulong) ioremap(board->hw_base, SI2_EISA_WINDOW_LEN);
2570 sx_dprintk(SX_DEBUG_PROBE, "IO hw_base address: %x\n", board->hw_base);
2571 sx_dprintk(SX_DEBUG_PROBE, "base: %x\n", board->base);
2572 board->irq = inb(board->eisa_base+0xc02)>>4;
2573 sx_dprintk(SX_DEBUG_PROBE, "IRQ: %d\n", board->irq);
2575 probe_si(board);
2577 found++;
2580 if (found) {
2581 printk (KERN_INFO "sx: total of %d boards detected.\n", found);
2583 if (misc_register(&sx_fw_device) < 0) {
2584 printk(KERN_ERR "SX: Unable to register firmware loader driver.\n");
2585 return -EIO;
2589 func_exit();
2590 return found?0:-EIO;
2594 #ifdef MODULE
2595 void cleanup_module(void)
2597 int i;
2598 struct sx_board *board;
2600 func_enter();
2601 for (i = 0; i < SX_NBOARDS; i++) {
2602 board = &boards[i];
2603 if (board->flags & SX_BOARD_INITIALIZED) {
2604 sx_dprintk (SX_DEBUG_CLEANUP, "Cleaning up board at %x\n", board->base);
2605 /* The board should stop messing with us.
2606 (actually I mean the interrupt) */
2607 sx_reset (board);
2608 if ((board->irq) && (board->flags & SX_IRQ_ALLOCATED))
2609 free_irq (board->irq, board);
2611 /* It is safe/allowed to del_timer a non-active timer */
2612 del_timer (& board->timer);
2613 my_iounmap (board->hw_base, board->base);
2616 if (misc_deregister(&sx_fw_device) < 0) {
2617 printk (KERN_INFO "sx: couldn't deregister firmware loader device\n");
2619 sx_dprintk (SX_DEBUG_CLEANUP, "Cleaning up drivers (%d)\n", sx_initialized);
2620 if (sx_initialized)
2621 sx_release_drivers ();
2623 kfree (sx_ports);
2624 kfree (sx_termios);
2625 kfree (sx_termios_locked);
2626 func_exit();
2628 #endif