GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / char / sx.c
blob5b57387e5407f04fde006e7eed1970fda1a5514d
3 #define SX_VERSION 1.33
5 #include <linux/module.h>
6 #include <linux/kdev_t.h>
7 #include <linux/kernel.h>
8 #include <linux/sched.h>
9 #include <linux/ioport.h>
10 #include <linux/interrupt.h>
11 #include <linux/errno.h>
12 #include <linux/tty.h>
13 #include <linux/tty_flip.h>
14 #include <linux/mm.h>
15 #include <linux/serial.h>
16 #include <linux/fcntl.h>
17 #include <linux/major.h>
18 #include <linux/delay.h>
19 #include <linux/eisa.h>
20 #include <linux/pci.h>
21 #include <linux/slab.h>
22 #include <linux/smp_lock.h>
23 #include <linux/init.h>
24 #include <linux/miscdevice.h>
25 #include <linux/bitops.h>
27 #include <asm/io.h>
28 #include <asm/uaccess.h>
30 /* The 3.0.0 version of sxboards/sxwindow.h uses BYTE and WORD.... */
31 #define BYTE u8
32 #define WORD u16
34 /* .... but the 3.0.4 version uses _u8 and _u16. */
35 #define _u8 u8
36 #define _u16 u16
38 #include "sxboards.h"
39 #include "sxwindow.h"
41 #include <linux/generic_serial.h>
42 #include "sx.h"
44 /* I don't think that this driver can handle more than 256 ports on
45 one machine. You'll have to increase the number of boards in sx.h
46 if you want more than 4 boards. */
48 #ifndef PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8
49 #define PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8 0x2000
50 #endif
52 /* Configurable options:
53 (Don't be too sure that it'll work if you toggle them) */
55 /* Am I paranoid or not ? ;-) */
56 #undef SX_PARANOIA_CHECK
58 /* 20 -> 2000 per second. The card should rate-limit interrupts at 100
59 Hz, but it is user configurable. I don't recommend going above 1000
60 Hz. The interrupt ratelimit might trigger if the interrupt is
61 shared with a very active other device. */
62 #define IRQ_RATE_LIMIT 20
64 /* Sharing interrupts is possible now. If the other device wants more
65 than 2000 interrupts per second, we'd gracefully decline further
66 interrupts. That's not what we want. On the other hand, if the
67 other device interrupts 2000 times a second, don't use the SX
68 interrupt. Use polling. */
69 #undef IRQ_RATE_LIMIT
72 /* Function prototypes */
73 static void sx_disable_tx_interrupts(void *ptr);
74 static void sx_enable_tx_interrupts(void *ptr);
75 static void sx_disable_rx_interrupts(void *ptr);
76 static void sx_enable_rx_interrupts(void *ptr);
77 static int sx_carrier_raised(struct tty_port *port);
78 static void sx_shutdown_port(void *ptr);
79 static int sx_set_real_termios(void *ptr);
80 static void sx_close(void *ptr);
81 static int sx_chars_in_buffer(void *ptr);
82 static int sx_init_board(struct sx_board *board);
83 static int sx_init_portstructs(int nboards, int nports);
84 static long sx_fw_ioctl(struct file *filp, unsigned int cmd,
85 unsigned long arg);
86 static int sx_init_drivers(void);
88 static struct tty_driver *sx_driver;
90 static DEFINE_MUTEX(sx_boards_lock);
91 static struct sx_board boards[SX_NBOARDS];
92 static struct sx_port *sx_ports;
93 static int sx_initialized;
94 static int sx_nports;
95 static int sx_debug;
97 /* You can have the driver poll your card.
98 - Set sx_poll to 1 to poll every timer tick (10ms on Intel).
99 This is used when the card cannot use an interrupt for some reason.
101 - set sx_slowpoll to 100 to do an extra poll once a second (on Intel). If
102 the driver misses an interrupt (report this if it DOES happen to you!)
103 everything will continue to work....
105 static int sx_poll = 1;
106 static int sx_slowpoll;
108 /* The card limits the number of interrupts per second.
109 At 115k2 "100" should be sufficient.
110 If you're using higher baudrates, you can increase this...
113 static int sx_maxints = 100;
115 #ifdef CONFIG_ISA
117 /* These are the only open spaces in my computer. Yours may have more
118 or less.... -- REW
119 duh: Card at 0xa0000 is possible on HP Netserver?? -- pvdl
121 static int sx_probe_addrs[] = {
122 0xc0000, 0xd0000, 0xe0000,
123 0xc8000, 0xd8000, 0xe8000
125 static int si_probe_addrs[] = {
126 0xc0000, 0xd0000, 0xe0000,
127 0xc8000, 0xd8000, 0xe8000, 0xa0000
129 static int si1_probe_addrs[] = {
130 0xd0000
133 #define NR_SX_ADDRS ARRAY_SIZE(sx_probe_addrs)
134 #define NR_SI_ADDRS ARRAY_SIZE(si_probe_addrs)
135 #define NR_SI1_ADDRS ARRAY_SIZE(si1_probe_addrs)
137 module_param_array(sx_probe_addrs, int, NULL, 0);
138 module_param_array(si_probe_addrs, int, NULL, 0);
139 #endif
141 /* Set the mask to all-ones. This alas, only supports 32 interrupts.
142 Some architectures may need more. */
143 static int sx_irqmask = -1;
145 module_param(sx_poll, int, 0);
146 module_param(sx_slowpoll, int, 0);
147 module_param(sx_maxints, int, 0);
148 module_param(sx_debug, int, 0);
149 module_param(sx_irqmask, int, 0);
151 MODULE_LICENSE("GPL");
153 static struct real_driver sx_real_driver = {
154 sx_disable_tx_interrupts,
155 sx_enable_tx_interrupts,
156 sx_disable_rx_interrupts,
157 sx_enable_rx_interrupts,
158 sx_shutdown_port,
159 sx_set_real_termios,
160 sx_chars_in_buffer,
161 sx_close,
165 This driver can spew a whole lot of debugging output at you. If you
166 need maximum performance, you should disable the DEBUG define. To
167 aid in debugging in the field, I'm leaving the compile-time debug
168 features enabled, and disable them "runtime". That allows me to
169 instruct people with problems to enable debugging without requiring
170 them to recompile...
172 #define DEBUG
174 #ifdef DEBUG
175 #define sx_dprintk(f, str...) if (sx_debug & f) printk (str)
176 #else
177 #define sx_dprintk(f, str...) /* nothing */
178 #endif
180 #define func_enter() sx_dprintk(SX_DEBUG_FLOW, "sx: enter %s\n",__func__)
181 #define func_exit() sx_dprintk(SX_DEBUG_FLOW, "sx: exit %s\n",__func__)
183 #define func_enter2() sx_dprintk(SX_DEBUG_FLOW, "sx: enter %s (port %d)\n", \
184 __func__, port->line)
187 * Firmware loader driver specific routines
191 static const struct file_operations sx_fw_fops = {
192 .owner = THIS_MODULE,
193 .unlocked_ioctl = sx_fw_ioctl,
196 static struct miscdevice sx_fw_device = {
197 SXCTL_MISC_MINOR, "sxctl", &sx_fw_fops
200 #ifdef SX_PARANOIA_CHECK
202 /* This doesn't work. Who's paranoid around here? Not me! */
204 static inline int sx_paranoia_check(struct sx_port const *port,
205 char *name, const char *routine)
207 static const char *badmagic = KERN_ERR "sx: Warning: bad sx port magic "
208 "number for device %s in %s\n";
209 static const char *badinfo = KERN_ERR "sx: Warning: null sx port for "
210 "device %s in %s\n";
212 if (!port) {
213 printk(badinfo, name, routine);
214 return 1;
216 if (port->magic != SX_MAGIC) {
217 printk(badmagic, name, routine);
218 return 1;
221 return 0;
223 #else
224 #define sx_paranoia_check(a,b,c) 0
225 #endif
227 /* The timeouts. First try 30 times as fast as possible. Then give
228 the card some time to breathe between accesses. (Otherwise the
229 processor on the card might not be able to access its OWN bus... */
231 #define TIMEOUT_1 30
232 #define TIMEOUT_2 1000000
234 #ifdef DEBUG
235 static void my_hd_io(void __iomem *p, int len)
237 int i, j, ch;
238 unsigned char __iomem *addr = p;
240 for (i = 0; i < len; i += 16) {
241 printk("%p ", addr + i);
242 for (j = 0; j < 16; j++) {
243 printk("%02x %s", readb(addr + j + i),
244 (j == 7) ? " " : "");
246 for (j = 0; j < 16; j++) {
247 ch = readb(addr + j + i);
248 printk("%c", (ch < 0x20) ? '.' :
249 ((ch > 0x7f) ? '.' : ch));
251 printk("\n");
254 static void my_hd(void *p, int len)
256 int i, j, ch;
257 unsigned char *addr = p;
259 for (i = 0; i < len; i += 16) {
260 printk("%p ", addr + i);
261 for (j = 0; j < 16; j++) {
262 printk("%02x %s", addr[j + i], (j == 7) ? " " : "");
264 for (j = 0; j < 16; j++) {
265 ch = addr[j + i];
266 printk("%c", (ch < 0x20) ? '.' :
267 ((ch > 0x7f) ? '.' : ch));
269 printk("\n");
272 #endif
274 /* This needs redoing for Alpha -- REW -- Done. */
276 static inline void write_sx_byte(struct sx_board *board, int offset, u8 byte)
278 writeb(byte, board->base + offset);
281 static inline u8 read_sx_byte(struct sx_board *board, int offset)
283 return readb(board->base + offset);
286 static inline void write_sx_word(struct sx_board *board, int offset, u16 word)
288 writew(word, board->base + offset);
291 static inline u16 read_sx_word(struct sx_board *board, int offset)
293 return readw(board->base + offset);
296 static int sx_busy_wait_eq(struct sx_board *board,
297 int offset, int mask, int correctval)
299 int i;
301 func_enter();
303 for (i = 0; i < TIMEOUT_1; i++)
304 if ((read_sx_byte(board, offset) & mask) == correctval) {
305 func_exit();
306 return 1;
309 for (i = 0; i < TIMEOUT_2; i++) {
310 if ((read_sx_byte(board, offset) & mask) == correctval) {
311 func_exit();
312 return 1;
314 udelay(1);
317 func_exit();
318 return 0;
321 static int sx_busy_wait_neq(struct sx_board *board,
322 int offset, int mask, int badval)
324 int i;
326 func_enter();
328 for (i = 0; i < TIMEOUT_1; i++)
329 if ((read_sx_byte(board, offset) & mask) != badval) {
330 func_exit();
331 return 1;
334 for (i = 0; i < TIMEOUT_2; i++) {
335 if ((read_sx_byte(board, offset) & mask) != badval) {
336 func_exit();
337 return 1;
339 udelay(1);
342 func_exit();
343 return 0;
346 /* 5.6.4 of 6210028 r2.3 */
347 static int sx_reset(struct sx_board *board)
349 func_enter();
351 if (IS_SX_BOARD(board)) {
353 write_sx_byte(board, SX_CONFIG, 0);
354 write_sx_byte(board, SX_RESET, 1); /* Value doesn't matter */
356 if (!sx_busy_wait_eq(board, SX_RESET_STATUS, 1, 0)) {
357 printk(KERN_INFO "sx: Card doesn't respond to "
358 "reset...\n");
359 return 0;
361 } else if (IS_EISA_BOARD(board)) {
362 outb(board->irq << 4, board->eisa_base + 0xc02);
363 } else if (IS_SI1_BOARD(board)) {
364 write_sx_byte(board, SI1_ISA_RESET, 0); /*value doesn't matter*/
365 } else {
366 /* Gory details of the SI/ISA board */
367 write_sx_byte(board, SI2_ISA_RESET, SI2_ISA_RESET_SET);
368 write_sx_byte(board, SI2_ISA_IRQ11, SI2_ISA_IRQ11_CLEAR);
369 write_sx_byte(board, SI2_ISA_IRQ12, SI2_ISA_IRQ12_CLEAR);
370 write_sx_byte(board, SI2_ISA_IRQ15, SI2_ISA_IRQ15_CLEAR);
371 write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_CLEAR);
372 write_sx_byte(board, SI2_ISA_IRQSET, SI2_ISA_IRQSET_CLEAR);
375 func_exit();
376 return 1;
379 /* This doesn't work on machines where "NULL" isn't 0 */
380 /* If you have one of those, someone will need to write
381 the equivalent of this, which will amount to about 3 lines. I don't
382 want to complicate this right now. -- REW
383 (See, I do write comments every now and then :-) */
384 #define OFFSETOF(strct, elem) ((long)&(((struct strct *)NULL)->elem))
386 #define CHAN_OFFSET(port,elem) (port->ch_base + OFFSETOF (_SXCHANNEL, elem))
387 #define MODU_OFFSET(board,addr,elem) (addr + OFFSETOF (_SXMODULE, elem))
388 #define BRD_OFFSET(board,elem) (OFFSETOF (_SXCARD, elem))
390 #define sx_write_channel_byte(port, elem, val) \
391 write_sx_byte (port->board, CHAN_OFFSET (port, elem), val)
393 #define sx_read_channel_byte(port, elem) \
394 read_sx_byte (port->board, CHAN_OFFSET (port, elem))
396 #define sx_write_channel_word(port, elem, val) \
397 write_sx_word (port->board, CHAN_OFFSET (port, elem), val)
399 #define sx_read_channel_word(port, elem) \
400 read_sx_word (port->board, CHAN_OFFSET (port, elem))
402 #define sx_write_module_byte(board, addr, elem, val) \
403 write_sx_byte (board, MODU_OFFSET (board, addr, elem), val)
405 #define sx_read_module_byte(board, addr, elem) \
406 read_sx_byte (board, MODU_OFFSET (board, addr, elem))
408 #define sx_write_module_word(board, addr, elem, val) \
409 write_sx_word (board, MODU_OFFSET (board, addr, elem), val)
411 #define sx_read_module_word(board, addr, elem) \
412 read_sx_word (board, MODU_OFFSET (board, addr, elem))
414 #define sx_write_board_byte(board, elem, val) \
415 write_sx_byte (board, BRD_OFFSET (board, elem), val)
417 #define sx_read_board_byte(board, elem) \
418 read_sx_byte (board, BRD_OFFSET (board, elem))
420 #define sx_write_board_word(board, elem, val) \
421 write_sx_word (board, BRD_OFFSET (board, elem), val)
423 #define sx_read_board_word(board, elem) \
424 read_sx_word (board, BRD_OFFSET (board, elem))
426 static int sx_start_board(struct sx_board *board)
428 if (IS_SX_BOARD(board)) {
429 write_sx_byte(board, SX_CONFIG, SX_CONF_BUSEN);
430 } else if (IS_EISA_BOARD(board)) {
431 write_sx_byte(board, SI2_EISA_OFF, SI2_EISA_VAL);
432 outb((board->irq << 4) | 4, board->eisa_base + 0xc02);
433 } else if (IS_SI1_BOARD(board)) {
434 write_sx_byte(board, SI1_ISA_RESET_CLEAR, 0);
435 write_sx_byte(board, SI1_ISA_INTCL, 0);
436 } else {
437 /* Don't bug me about the clear_set.
438 I haven't the foggiest idea what it's about -- REW */
439 write_sx_byte(board, SI2_ISA_RESET, SI2_ISA_RESET_CLEAR);
440 write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
442 return 1;
445 #define SX_IRQ_REG_VAL(board) \
446 ((board->flags & SX_ISA_BOARD) ? (board->irq << 4) : 0)
448 /* Note. The SX register is write-only. Therefore, we have to enable the
449 bus too. This is a no-op, if you don't mess with this driver... */
450 static int sx_start_interrupts(struct sx_board *board)
453 /* Don't call this with board->irq == 0 */
455 if (IS_SX_BOARD(board)) {
456 write_sx_byte(board, SX_CONFIG, SX_IRQ_REG_VAL(board) |
457 SX_CONF_BUSEN | SX_CONF_HOSTIRQ);
458 } else if (IS_EISA_BOARD(board)) {
459 inb(board->eisa_base + 0xc03);
460 } else if (IS_SI1_BOARD(board)) {
461 write_sx_byte(board, SI1_ISA_INTCL, 0);
462 write_sx_byte(board, SI1_ISA_INTCL_CLEAR, 0);
463 } else {
464 switch (board->irq) {
465 case 11:
466 write_sx_byte(board, SI2_ISA_IRQ11, SI2_ISA_IRQ11_SET);
467 break;
468 case 12:
469 write_sx_byte(board, SI2_ISA_IRQ12, SI2_ISA_IRQ12_SET);
470 break;
471 case 15:
472 write_sx_byte(board, SI2_ISA_IRQ15, SI2_ISA_IRQ15_SET);
473 break;
474 default:
475 printk(KERN_INFO "sx: SI/XIO card doesn't support "
476 "interrupt %d.\n", board->irq);
477 return 0;
479 write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
482 return 1;
485 static int sx_send_command(struct sx_port *port,
486 int command, int mask, int newstat)
488 func_enter2();
489 write_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat), command);
490 func_exit();
491 return sx_busy_wait_eq(port->board, CHAN_OFFSET(port, hi_hstat), mask,
492 newstat);
495 static char *mod_type_s(int module_type)
497 switch (module_type) {
498 case TA4:
499 return "TA4";
500 case TA8:
501 return "TA8";
502 case TA4_ASIC:
503 return "TA4_ASIC";
504 case TA8_ASIC:
505 return "TA8_ASIC";
506 case MTA_CD1400:
507 return "MTA_CD1400";
508 case SXDC:
509 return "SXDC";
510 default:
511 return "Unknown/invalid";
515 static char *pan_type_s(int pan_type)
517 switch (pan_type) {
518 case MOD_RS232DB25:
519 return "MOD_RS232DB25";
520 case MOD_RS232RJ45:
521 return "MOD_RS232RJ45";
522 case MOD_RS422DB25:
523 return "MOD_RS422DB25";
524 case MOD_PARALLEL:
525 return "MOD_PARALLEL";
526 case MOD_2_RS232DB25:
527 return "MOD_2_RS232DB25";
528 case MOD_2_RS232RJ45:
529 return "MOD_2_RS232RJ45";
530 case MOD_2_RS422DB25:
531 return "MOD_2_RS422DB25";
532 case MOD_RS232DB25MALE:
533 return "MOD_RS232DB25MALE";
534 case MOD_2_PARALLEL:
535 return "MOD_2_PARALLEL";
536 case MOD_BLANK:
537 return "empty";
538 default:
539 return "invalid";
543 static int mod_compat_type(int module_type)
545 return module_type >> 4;
548 static void sx_reconfigure_port(struct sx_port *port)
550 if (sx_read_channel_byte(port, hi_hstat) == HS_IDLE_OPEN) {
551 if (sx_send_command(port, HS_CONFIG, -1, HS_IDLE_OPEN) != 1) {
552 printk(KERN_WARNING "sx: Sent reconfigure command, but "
553 "card didn't react.\n");
555 } else {
556 sx_dprintk(SX_DEBUG_TERMIOS, "sx: Not sending reconfigure: "
557 "port isn't open (%02x).\n",
558 sx_read_channel_byte(port, hi_hstat));
562 static void sx_setsignals(struct sx_port *port, int dtr, int rts)
564 int t;
565 func_enter2();
567 t = sx_read_channel_byte(port, hi_op);
568 if (dtr >= 0)
569 t = dtr ? (t | OP_DTR) : (t & ~OP_DTR);
570 if (rts >= 0)
571 t = rts ? (t | OP_RTS) : (t & ~OP_RTS);
572 sx_write_channel_byte(port, hi_op, t);
573 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "setsignals: %d/%d\n", dtr, rts);
575 func_exit();
578 static int sx_getsignals(struct sx_port *port)
580 int i_stat, o_stat;
582 o_stat = sx_read_channel_byte(port, hi_op);
583 i_stat = sx_read_channel_byte(port, hi_ip);
585 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "getsignals: %d/%d (%d/%d) "
586 "%02x/%02x\n",
587 (o_stat & OP_DTR) != 0, (o_stat & OP_RTS) != 0,
588 port->c_dcd, tty_port_carrier_raised(&port->gs.port),
589 sx_read_channel_byte(port, hi_ip),
590 sx_read_channel_byte(port, hi_state));
592 return (((o_stat & OP_DTR) ? TIOCM_DTR : 0) |
593 ((o_stat & OP_RTS) ? TIOCM_RTS : 0) |
594 ((i_stat & IP_CTS) ? TIOCM_CTS : 0) |
595 ((i_stat & IP_DCD) ? TIOCM_CAR : 0) |
596 ((i_stat & IP_DSR) ? TIOCM_DSR : 0) |
597 ((i_stat & IP_RI) ? TIOCM_RNG : 0));
600 static void sx_set_baud(struct sx_port *port)
602 int t;
604 if (port->board->ta_type == MOD_SXDC) {
605 switch (port->gs.baud) {
606 /* Save some typing work... */
607 #define e(x) case x: t = BAUD_ ## x; break
608 e(50);
609 e(75);
610 e(110);
611 e(150);
612 e(200);
613 e(300);
614 e(600);
615 e(1200);
616 e(1800);
617 e(2000);
618 e(2400);
619 e(4800);
620 e(7200);
621 e(9600);
622 e(14400);
623 e(19200);
624 e(28800);
625 e(38400);
626 e(56000);
627 e(57600);
628 e(64000);
629 e(76800);
630 e(115200);
631 e(128000);
632 e(150000);
633 e(230400);
634 e(256000);
635 e(460800);
636 e(921600);
637 case 134:
638 t = BAUD_134_5;
639 break;
640 case 0:
641 t = -1;
642 break;
643 default:
644 /* Can I return "invalid"? */
645 t = BAUD_9600;
646 printk(KERN_INFO "sx: unsupported baud rate: %d.\n",
647 port->gs.baud);
648 break;
650 #undef e
651 if (t > 0) {
652 /* The baud rate is not set to 0, so we're enabeling DTR... -- REW */
653 sx_setsignals(port, 1, -1);
654 sx_write_channel_byte(port, hi_csr, 0xff);
656 sx_write_channel_byte(port, hi_txbaud, t);
657 sx_write_channel_byte(port, hi_rxbaud, t);
658 } else {
659 sx_setsignals(port, 0, -1);
661 } else {
662 switch (port->gs.baud) {
663 #define e(x) case x: t = CSR_ ## x; break
664 e(75);
665 e(150);
666 e(300);
667 e(600);
668 e(1200);
669 e(2400);
670 e(4800);
671 e(1800);
672 e(9600);
673 e(19200);
674 e(57600);
675 e(38400);
676 /* TA supports 110, but not 115200, MTA supports 115200, but not 110 */
677 case 110:
678 if (port->board->ta_type == MOD_TA) {
679 t = CSR_110;
680 break;
681 } else {
682 t = CSR_9600;
683 printk(KERN_INFO "sx: Unsupported baud rate: "
684 "%d.\n", port->gs.baud);
685 break;
687 case 115200:
688 if (port->board->ta_type == MOD_TA) {
689 t = CSR_9600;
690 printk(KERN_INFO "sx: Unsupported baud rate: "
691 "%d.\n", port->gs.baud);
692 break;
693 } else {
694 t = CSR_110;
695 break;
697 case 0:
698 t = -1;
699 break;
700 default:
701 t = CSR_9600;
702 printk(KERN_INFO "sx: Unsupported baud rate: %d.\n",
703 port->gs.baud);
704 break;
706 #undef e
707 if (t >= 0) {
708 sx_setsignals(port, 1, -1);
709 sx_write_channel_byte(port, hi_csr, t * 0x11);
710 } else {
711 sx_setsignals(port, 0, -1);
716 /* Simon Allen's version of this routine was 225 lines long. 85 is a lot
717 better. -- REW */
719 static int sx_set_real_termios(void *ptr)
721 struct sx_port *port = ptr;
723 func_enter2();
725 if (!port->gs.port.tty)
726 return 0;
728 /* What is this doing here? -- REW
729 Ha! figured it out. It is to allow you to get DTR active again
730 if you've dropped it with stty 0. Moved to set_baud, where it
731 belongs (next to the drop dtr if baud == 0) -- REW */
732 /* sx_setsignals (port, 1, -1); */
734 sx_set_baud(port);
736 #define CFLAG port->gs.port.tty->termios->c_cflag
737 sx_write_channel_byte(port, hi_mr1,
738 (C_PARENB(port->gs.port.tty) ? MR1_WITH : MR1_NONE) |
739 (C_PARODD(port->gs.port.tty) ? MR1_ODD : MR1_EVEN) |
740 (C_CRTSCTS(port->gs.port.tty) ? MR1_RTS_RXFLOW : 0) |
741 (((CFLAG & CSIZE) == CS8) ? MR1_8_BITS : 0) |
742 (((CFLAG & CSIZE) == CS7) ? MR1_7_BITS : 0) |
743 (((CFLAG & CSIZE) == CS6) ? MR1_6_BITS : 0) |
744 (((CFLAG & CSIZE) == CS5) ? MR1_5_BITS : 0));
746 sx_write_channel_byte(port, hi_mr2,
747 (C_CRTSCTS(port->gs.port.tty) ? MR2_CTS_TXFLOW : 0) |
748 (C_CSTOPB(port->gs.port.tty) ? MR2_2_STOP :
749 MR2_1_STOP));
751 switch (CFLAG & CSIZE) {
752 case CS8:
753 sx_write_channel_byte(port, hi_mask, 0xff);
754 break;
755 case CS7:
756 sx_write_channel_byte(port, hi_mask, 0x7f);
757 break;
758 case CS6:
759 sx_write_channel_byte(port, hi_mask, 0x3f);
760 break;
761 case CS5:
762 sx_write_channel_byte(port, hi_mask, 0x1f);
763 break;
764 default:
765 printk(KERN_INFO "sx: Invalid wordsize: %u\n",
766 (unsigned int)CFLAG & CSIZE);
767 break;
770 sx_write_channel_byte(port, hi_prtcl,
771 (I_IXON(port->gs.port.tty) ? SP_TXEN : 0) |
772 (I_IXOFF(port->gs.port.tty) ? SP_RXEN : 0) |
773 (I_IXANY(port->gs.port.tty) ? SP_TANY : 0) | SP_DCEN);
775 sx_write_channel_byte(port, hi_break,
776 (I_IGNBRK(port->gs.port.tty) ? BR_IGN : 0 |
777 I_BRKINT(port->gs.port.tty) ? BR_INT : 0));
779 sx_write_channel_byte(port, hi_txon, START_CHAR(port->gs.port.tty));
780 sx_write_channel_byte(port, hi_rxon, START_CHAR(port->gs.port.tty));
781 sx_write_channel_byte(port, hi_txoff, STOP_CHAR(port->gs.port.tty));
782 sx_write_channel_byte(port, hi_rxoff, STOP_CHAR(port->gs.port.tty));
784 sx_reconfigure_port(port);
786 /* Tell line discipline whether we will do input cooking */
787 if (I_OTHER(port->gs.port.tty)) {
788 clear_bit(TTY_HW_COOK_IN, &port->gs.port.tty->flags);
789 } else {
790 set_bit(TTY_HW_COOK_IN, &port->gs.port.tty->flags);
792 sx_dprintk(SX_DEBUG_TERMIOS, "iflags: %x(%d) ",
793 (unsigned int)port->gs.port.tty->termios->c_iflag,
794 I_OTHER(port->gs.port.tty));
796 /* Tell line discipline whether we will do output cooking.
797 * If OPOST is set and no other output flags are set then we can do output
798 * processing. Even if only *one* other flag in the O_OTHER group is set
799 * we do cooking in software.
801 if (O_OPOST(port->gs.port.tty) && !O_OTHER(port->gs.port.tty)) {
802 set_bit(TTY_HW_COOK_OUT, &port->gs.port.tty->flags);
803 } else {
804 clear_bit(TTY_HW_COOK_OUT, &port->gs.port.tty->flags);
806 sx_dprintk(SX_DEBUG_TERMIOS, "oflags: %x(%d)\n",
807 (unsigned int)port->gs.port.tty->termios->c_oflag,
808 O_OTHER(port->gs.port.tty));
809 /* port->c_dcd = sx_get_CD (port); */
810 func_exit();
811 return 0;
814 /* ********************************************************************** *
815 * the interrupt related routines *
816 * ********************************************************************** */
818 /* Note:
819 Other drivers use the macro "MIN" to calculate how much to copy.
820 This has the disadvantage that it will evaluate parts twice. That's
821 expensive when it's IO (and the compiler cannot optimize those away!).
822 Moreover, I'm not sure that you're race-free.
824 I assign a value, and then only allow the value to decrease. This
825 is always safe. This makes the code a few lines longer, and you
826 know I'm dead against that, but I think it is required in this
827 case. */
829 static void sx_transmit_chars(struct sx_port *port)
831 int c;
832 int tx_ip;
833 int txroom;
835 func_enter2();
836 sx_dprintk(SX_DEBUG_TRANSMIT, "Port %p: transmit %d chars\n",
837 port, port->gs.xmit_cnt);
839 if (test_and_set_bit(SX_PORT_TRANSMIT_LOCK, &port->locks)) {
840 return;
843 while (1) {
844 c = port->gs.xmit_cnt;
846 sx_dprintk(SX_DEBUG_TRANSMIT, "Copying %d ", c);
847 tx_ip = sx_read_channel_byte(port, hi_txipos);
849 /* Took me 5 minutes to deduce this formula.
850 Luckily it is literally in the manual in section 6.5.4.3.5 */
851 txroom = (sx_read_channel_byte(port, hi_txopos) - tx_ip - 1) &
852 0xff;
854 /* Don't copy more bytes than there is room for in the buffer */
855 if (c > txroom)
856 c = txroom;
857 sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%d) ", c, txroom);
859 /* Don't copy past the end of the hardware transmit buffer */
860 if (c > 0x100 - tx_ip)
861 c = 0x100 - tx_ip;
863 sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%d) ", c, 0x100 - tx_ip);
865 /* Don't copy pas the end of the source buffer */
866 if (c > SERIAL_XMIT_SIZE - port->gs.xmit_tail)
867 c = SERIAL_XMIT_SIZE - port->gs.xmit_tail;
869 sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%ld) \n",
870 c, SERIAL_XMIT_SIZE - port->gs.xmit_tail);
872 /* If for one reason or another, we can't copy more data, we're
873 done! */
874 if (c == 0)
875 break;
877 memcpy_toio(port->board->base + CHAN_OFFSET(port, hi_txbuf) +
878 tx_ip, port->gs.xmit_buf + port->gs.xmit_tail, c);
880 /* Update the pointer in the card */
881 sx_write_channel_byte(port, hi_txipos, (tx_ip + c) & 0xff);
883 /* Update the kernel buffer end */
884 port->gs.xmit_tail = (port->gs.xmit_tail + c) &
885 (SERIAL_XMIT_SIZE - 1);
887 /* This one last. (this is essential)
888 It would allow others to start putting more data into the
889 buffer! */
890 port->gs.xmit_cnt -= c;
893 if (port->gs.xmit_cnt == 0) {
894 sx_disable_tx_interrupts(port);
897 if ((port->gs.xmit_cnt <= port->gs.wakeup_chars) && port->gs.port.tty) {
898 tty_wakeup(port->gs.port.tty);
899 sx_dprintk(SX_DEBUG_TRANSMIT, "Waking up.... ldisc (%d)....\n",
900 port->gs.wakeup_chars);
903 clear_bit(SX_PORT_TRANSMIT_LOCK, &port->locks);
904 func_exit();
907 /* Note the symmetry between receiving chars and transmitting them!
908 Note: The kernel should have implemented both a receive buffer and
909 a transmit buffer. */
911 /* Inlined: Called only once. Remove the inline when you add another call */
912 static inline void sx_receive_chars(struct sx_port *port)
914 int c;
915 int rx_op;
916 struct tty_struct *tty;
917 int copied = 0;
918 unsigned char *rp;
920 func_enter2();
921 tty = port->gs.port.tty;
922 while (1) {
923 rx_op = sx_read_channel_byte(port, hi_rxopos);
924 c = (sx_read_channel_byte(port, hi_rxipos) - rx_op) & 0xff;
926 sx_dprintk(SX_DEBUG_RECEIVE, "rxop=%d, c = %d.\n", rx_op, c);
928 /* Don't copy past the end of the hardware receive buffer */
929 if (rx_op + c > 0x100)
930 c = 0x100 - rx_op;
932 sx_dprintk(SX_DEBUG_RECEIVE, "c = %d.\n", c);
934 /* Don't copy more bytes than there is room for in the buffer */
936 c = tty_prepare_flip_string(tty, &rp, c);
938 sx_dprintk(SX_DEBUG_RECEIVE, "c = %d.\n", c);
940 /* If for one reason or another, we can't copy more data, we're done! */
941 if (c == 0)
942 break;
944 sx_dprintk(SX_DEBUG_RECEIVE, "Copying over %d chars. First is "
945 "%d at %lx\n", c, read_sx_byte(port->board,
946 CHAN_OFFSET(port, hi_rxbuf) + rx_op),
947 CHAN_OFFSET(port, hi_rxbuf));
948 memcpy_fromio(rp, port->board->base +
949 CHAN_OFFSET(port, hi_rxbuf) + rx_op, c);
951 /* This one last. ( Not essential.)
952 It allows the card to start putting more data into the
953 buffer!
954 Update the pointer in the card */
955 sx_write_channel_byte(port, hi_rxopos, (rx_op + c) & 0xff);
957 copied += c;
959 if (copied) {
960 struct timeval tv;
962 do_gettimeofday(&tv);
963 sx_dprintk(SX_DEBUG_RECEIVE, "pushing flipq port %d (%3d "
964 "chars): %d.%06d (%d/%d)\n", port->line,
965 copied, (int)(tv.tv_sec % 60), (int)tv.tv_usec,
966 tty->raw, tty->real_raw);
968 /* Tell the rest of the system the news. Great news. New
969 characters! */
970 tty_flip_buffer_push(tty);
971 /* tty_schedule_flip (tty); */
974 func_exit();
977 /* Inlined: it is called only once. Remove the inline if you add another
978 call */
979 static inline void sx_check_modem_signals(struct sx_port *port)
981 int hi_state;
982 int c_dcd;
984 hi_state = sx_read_channel_byte(port, hi_state);
985 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "Checking modem signals (%d/%d)\n",
986 port->c_dcd, tty_port_carrier_raised(&port->gs.port));
988 if (hi_state & ST_BREAK) {
989 hi_state &= ~ST_BREAK;
990 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "got a break.\n");
991 sx_write_channel_byte(port, hi_state, hi_state);
992 gs_got_break(&port->gs);
994 if (hi_state & ST_DCD) {
995 hi_state &= ~ST_DCD;
996 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "got a DCD change.\n");
997 sx_write_channel_byte(port, hi_state, hi_state);
998 c_dcd = tty_port_carrier_raised(&port->gs.port);
999 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD is now %d\n", c_dcd);
1000 if (c_dcd != port->c_dcd) {
1001 port->c_dcd = c_dcd;
1002 if (tty_port_carrier_raised(&port->gs.port)) {
1003 /* DCD went UP */
1004 if ((sx_read_channel_byte(port, hi_hstat) !=
1005 HS_IDLE_CLOSED) &&
1006 !(port->gs.port.tty->termios->
1007 c_cflag & CLOCAL)) {
1008 /* Are we blocking in open? */
1009 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
1010 "active, unblocking open\n");
1011 wake_up_interruptible(&port->gs.port.
1012 open_wait);
1013 } else {
1014 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
1015 "raised. Ignoring.\n");
1017 } else {
1018 /* DCD went down! */
1019 if (!(port->gs.port.tty->termios->c_cflag & CLOCAL)){
1020 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
1021 "dropped. hanging up....\n");
1022 tty_hangup(port->gs.port.tty);
1023 } else {
1024 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
1025 "dropped. ignoring.\n");
1028 } else {
1029 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "Hmmm. card told us "
1030 "DCD changed, but it didn't.\n");
1035 /* This is what an interrupt routine should look like.
1036 * Small, elegant, clear.
1039 static irqreturn_t sx_interrupt(int irq, void *ptr)
1041 struct sx_board *board = ptr;
1042 struct sx_port *port;
1043 int i;
1045 func_enter();
1046 sx_dprintk(SX_DEBUG_FLOW, "sx: enter sx_interrupt (%d/%d)\n", irq,
1047 board->irq);
1049 /* AAargh! The order in which to do these things is essential and
1050 not trivial.
1052 - Rate limit goes before "recursive". Otherwise a series of
1053 recursive calls will hang the machine in the interrupt routine.
1055 - hardware twiddling goes before "recursive". Otherwise when we
1056 poll the card, and a recursive interrupt happens, we won't
1057 ack the card, so it might keep on interrupting us. (especially
1058 level sensitive interrupt systems like PCI).
1060 - Rate limit goes before hardware twiddling. Otherwise we won't
1061 catch a card that has gone bonkers.
1063 - The "initialized" test goes after the hardware twiddling. Otherwise
1064 the card will stick us in the interrupt routine again.
1066 - The initialized test goes before recursive.
1069 #ifdef IRQ_RATE_LIMIT
1070 /* Aaargh! I'm ashamed. This costs more lines-of-code than the
1071 actual interrupt routine!. (Well, used to when I wrote that
1072 comment) */
1074 static int lastjif;
1075 static int nintr = 0;
1077 if (lastjif == jiffies) {
1078 if (++nintr > IRQ_RATE_LIMIT) {
1079 free_irq(board->irq, board);
1080 printk(KERN_ERR "sx: Too many interrupts. "
1081 "Turning off interrupt %d.\n",
1082 board->irq);
1084 } else {
1085 lastjif = jiffies;
1086 nintr = 0;
1089 #endif
1091 if (board->irq == irq) {
1092 /* Tell the card we've noticed the interrupt. */
1094 sx_write_board_word(board, cc_int_pending, 0);
1095 if (IS_SX_BOARD(board)) {
1096 write_sx_byte(board, SX_RESET_IRQ, 1);
1097 } else if (IS_EISA_BOARD(board)) {
1098 inb(board->eisa_base + 0xc03);
1099 write_sx_word(board, 8, 0);
1100 } else {
1101 write_sx_byte(board, SI2_ISA_INTCLEAR,
1102 SI2_ISA_INTCLEAR_CLEAR);
1103 write_sx_byte(board, SI2_ISA_INTCLEAR,
1104 SI2_ISA_INTCLEAR_SET);
1108 if (!sx_initialized)
1109 return IRQ_HANDLED;
1110 if (!(board->flags & SX_BOARD_INITIALIZED))
1111 return IRQ_HANDLED;
1113 if (test_and_set_bit(SX_BOARD_INTR_LOCK, &board->locks)) {
1114 printk(KERN_ERR "Recursive interrupt! (%d)\n", board->irq);
1115 return IRQ_HANDLED;
1118 for (i = 0; i < board->nports; i++) {
1119 port = &board->ports[i];
1120 if (port->gs.port.flags & GS_ACTIVE) {
1121 if (sx_read_channel_byte(port, hi_state)) {
1122 sx_dprintk(SX_DEBUG_INTERRUPTS, "Port %d: "
1123 "modem signal change?... \n",i);
1124 sx_check_modem_signals(port);
1126 if (port->gs.xmit_cnt) {
1127 sx_transmit_chars(port);
1129 if (!(port->gs.port.flags & SX_RX_THROTTLE)) {
1130 sx_receive_chars(port);
1135 clear_bit(SX_BOARD_INTR_LOCK, &board->locks);
1137 sx_dprintk(SX_DEBUG_FLOW, "sx: exit sx_interrupt (%d/%d)\n", irq,
1138 board->irq);
1139 func_exit();
1140 return IRQ_HANDLED;
1143 static void sx_pollfunc(unsigned long data)
1145 struct sx_board *board = (struct sx_board *)data;
1147 func_enter();
1149 sx_interrupt(0, board);
1151 mod_timer(&board->timer, jiffies + sx_poll);
1152 func_exit();
1155 /* ********************************************************************** *
1156 * Here are the routines that actually *
1157 * interface with the generic_serial driver *
1158 * ********************************************************************** */
1160 /* Ehhm. I don't know how to fiddle with interrupts on the SX card. --REW */
1161 /* Hmm. Ok I figured it out. You don't. */
1163 static void sx_disable_tx_interrupts(void *ptr)
1165 struct sx_port *port = ptr;
1166 func_enter2();
1168 port->gs.port.flags &= ~GS_TX_INTEN;
1170 func_exit();
1173 static void sx_enable_tx_interrupts(void *ptr)
1175 struct sx_port *port = ptr;
1176 int data_in_buffer;
1177 func_enter2();
1179 /* First transmit the characters that we're supposed to */
1180 sx_transmit_chars(port);
1182 /* The sx card will never interrupt us if we don't fill the buffer
1183 past 25%. So we keep considering interrupts off if that's the case. */
1184 data_in_buffer = (sx_read_channel_byte(port, hi_txipos) -
1185 sx_read_channel_byte(port, hi_txopos)) & 0xff;
1187 if (data_in_buffer < LOW_WATER)
1188 port->gs.port.flags &= ~GS_TX_INTEN;
1190 func_exit();
1193 static void sx_disable_rx_interrupts(void *ptr)
1195 /* struct sx_port *port = ptr; */
1196 func_enter();
1198 func_exit();
1201 static void sx_enable_rx_interrupts(void *ptr)
1203 /* struct sx_port *port = ptr; */
1204 func_enter();
1206 func_exit();
1209 /* Jeez. Isn't this simple? */
1210 static int sx_carrier_raised(struct tty_port *port)
1212 struct sx_port *sp = container_of(port, struct sx_port, gs.port);
1213 return ((sx_read_channel_byte(sp, hi_ip) & IP_DCD) != 0);
1216 /* Jeez. Isn't this simple? */
1217 static int sx_chars_in_buffer(void *ptr)
1219 struct sx_port *port = ptr;
1220 func_enter2();
1222 func_exit();
1223 return ((sx_read_channel_byte(port, hi_txipos) -
1224 sx_read_channel_byte(port, hi_txopos)) & 0xff);
1227 static void sx_shutdown_port(void *ptr)
1229 struct sx_port *port = ptr;
1231 func_enter();
1233 port->gs.port.flags &= ~GS_ACTIVE;
1234 if (port->gs.port.tty && (port->gs.port.tty->termios->c_cflag & HUPCL)) {
1235 sx_setsignals(port, 0, 0);
1236 sx_reconfigure_port(port);
1239 func_exit();
1242 /* ********************************************************************** *
1243 * Here are the routines that actually *
1244 * interface with the rest of the system *
1245 * ********************************************************************** */
1247 static int sx_open(struct tty_struct *tty, struct file *filp)
1249 struct sx_port *port;
1250 int retval, line;
1251 unsigned long flags;
1253 func_enter();
1255 if (!sx_initialized) {
1256 return -EIO;
1259 line = tty->index;
1260 sx_dprintk(SX_DEBUG_OPEN, "%d: opening line %d. tty=%p ctty=%p, "
1261 "np=%d)\n", task_pid_nr(current), line, tty,
1262 current->signal->tty, sx_nports);
1264 if ((line < 0) || (line >= SX_NPORTS) || (line >= sx_nports))
1265 return -ENODEV;
1267 port = &sx_ports[line];
1268 port->c_dcd = 0; /* Make sure that the first interrupt doesn't detect a
1269 1 -> 0 transition. */
1271 sx_dprintk(SX_DEBUG_OPEN, "port = %p c_dcd = %d\n", port, port->c_dcd);
1273 spin_lock_irqsave(&port->gs.driver_lock, flags);
1275 tty->driver_data = port;
1276 port->gs.port.tty = tty;
1277 port->gs.port.count++;
1278 spin_unlock_irqrestore(&port->gs.driver_lock, flags);
1280 sx_dprintk(SX_DEBUG_OPEN, "starting port\n");
1283 * Start up serial port
1285 retval = gs_init_port(&port->gs);
1286 sx_dprintk(SX_DEBUG_OPEN, "done gs_init\n");
1287 if (retval) {
1288 port->gs.port.count--;
1289 return retval;
1292 port->gs.port.flags |= GS_ACTIVE;
1293 if (port->gs.port.count <= 1)
1294 sx_setsignals(port, 1, 1);
1296 if (sx_debug & SX_DEBUG_OPEN)
1297 my_hd_io(port->board->base + port->ch_base, sizeof(*port));
1299 if (port->gs.port.count <= 1) {
1300 if (sx_send_command(port, HS_LOPEN, -1, HS_IDLE_OPEN) != 1) {
1301 printk(KERN_ERR "sx: Card didn't respond to LOPEN "
1302 "command.\n");
1303 spin_lock_irqsave(&port->gs.driver_lock, flags);
1304 port->gs.port.count--;
1305 spin_unlock_irqrestore(&port->gs.driver_lock, flags);
1306 return -EIO;
1310 retval = gs_block_til_ready(port, filp);
1311 sx_dprintk(SX_DEBUG_OPEN, "Block til ready returned %d. Count=%d\n",
1312 retval, port->gs.port.count);
1314 if (retval) {
1316 * Don't lower gs.port.count here because sx_close() will be called later
1319 return retval;
1321 /* tty->low_latency = 1; */
1323 port->c_dcd = sx_carrier_raised(&port->gs.port);
1324 sx_dprintk(SX_DEBUG_OPEN, "at open: cd=%d\n", port->c_dcd);
1326 func_exit();
1327 return 0;
1331 static void sx_close(void *ptr)
1333 struct sx_port *port = ptr;
1334 /* Give the port 5 seconds to close down. */
1335 int to = 5 * HZ;
1337 func_enter();
1339 sx_setsignals(port, 0, 0);
1340 sx_reconfigure_port(port);
1341 sx_send_command(port, HS_CLOSE, 0, 0);
1343 while (to-- && (sx_read_channel_byte(port, hi_hstat) != HS_IDLE_CLOSED))
1344 if (msleep_interruptible(10))
1345 break;
1346 if (sx_read_channel_byte(port, hi_hstat) != HS_IDLE_CLOSED) {
1347 if (sx_send_command(port, HS_FORCE_CLOSED, -1, HS_IDLE_CLOSED)
1348 != 1) {
1349 printk(KERN_ERR "sx: sent the force_close command, but "
1350 "card didn't react\n");
1351 } else
1352 sx_dprintk(SX_DEBUG_CLOSE, "sent the force_close "
1353 "command.\n");
1356 sx_dprintk(SX_DEBUG_CLOSE, "waited %d jiffies for close. count=%d\n",
1357 5 * HZ - to - 1, port->gs.port.count);
1359 if (port->gs.port.count) {
1360 sx_dprintk(SX_DEBUG_CLOSE, "WARNING port count:%d\n",
1361 port->gs.port.count);
1362 /*printk("%s SETTING port count to zero: %p count: %d\n",
1363 __func__, port, port->gs.port.count);
1364 port->gs.port.count = 0;*/
1367 func_exit();
1370 /* This is relatively thorough. But then again it is only 20 lines. */
1371 #define MARCHUP for (i = min; i < max; i++)
1372 #define MARCHDOWN for (i = max - 1; i >= min; i--)
1373 #define W0 write_sx_byte(board, i, 0x55)
1374 #define W1 write_sx_byte(board, i, 0xaa)
1375 #define R0 if (read_sx_byte(board, i) != 0x55) return 1
1376 #define R1 if (read_sx_byte(board, i) != 0xaa) return 1
1378 /* This memtest takes a human-noticable time. You normally only do it
1379 once a boot, so I guess that it is worth it. */
1380 static int do_memtest(struct sx_board *board, int min, int max)
1382 int i;
1384 /* This is a marchb. Theoretically, marchb catches much more than
1385 simpler tests. In practise, the longer test just catches more
1386 intermittent errors. -- REW
1387 (For the theory behind memory testing see:
1388 Testing Semiconductor Memories by A.J. van de Goor.) */
1389 MARCHUP {
1392 MARCHUP {
1400 MARCHUP {
1405 MARCHDOWN {
1411 MARCHDOWN {
1417 return 0;
1420 #undef MARCHUP
1421 #undef MARCHDOWN
1422 #undef W0
1423 #undef W1
1424 #undef R0
1425 #undef R1
1427 #define MARCHUP for (i = min; i < max; i += 2)
1428 #define MARCHDOWN for (i = max - 1; i >= min; i -= 2)
1429 #define W0 write_sx_word(board, i, 0x55aa)
1430 #define W1 write_sx_word(board, i, 0xaa55)
1431 #define R0 if (read_sx_word(board, i) != 0x55aa) return 1
1432 #define R1 if (read_sx_word(board, i) != 0xaa55) return 1
1435 static long sx_fw_ioctl(struct file *filp, unsigned int cmd,
1436 unsigned long arg)
1438 long rc = 0;
1439 int __user *descr = (int __user *)arg;
1440 int i;
1441 static struct sx_board *board = NULL;
1442 int nbytes, offset;
1443 unsigned long data;
1444 char *tmp;
1446 func_enter();
1448 if (!capable(CAP_SYS_RAWIO))
1449 return -EPERM;
1451 tty_lock();
1453 sx_dprintk(SX_DEBUG_FIRMWARE, "IOCTL %x: %lx\n", cmd, arg);
1455 if (!board)
1456 board = &boards[0];
1457 if (board->flags & SX_BOARD_PRESENT) {
1458 sx_dprintk(SX_DEBUG_FIRMWARE, "Board present! (%x)\n",
1459 board->flags);
1460 } else {
1461 sx_dprintk(SX_DEBUG_FIRMWARE, "Board not present! (%x) all:",
1462 board->flags);
1463 for (i = 0; i < SX_NBOARDS; i++)
1464 sx_dprintk(SX_DEBUG_FIRMWARE, "<%x> ", boards[i].flags);
1465 sx_dprintk(SX_DEBUG_FIRMWARE, "\n");
1466 rc = -EIO;
1467 goto out;
1470 switch (cmd) {
1471 case SXIO_SET_BOARD:
1472 sx_dprintk(SX_DEBUG_FIRMWARE, "set board to %ld\n", arg);
1473 rc = -EIO;
1474 if (arg >= SX_NBOARDS)
1475 break;
1476 sx_dprintk(SX_DEBUG_FIRMWARE, "not out of range\n");
1477 if (!(boards[arg].flags & SX_BOARD_PRESENT))
1478 break;
1479 sx_dprintk(SX_DEBUG_FIRMWARE, ".. and present!\n");
1480 board = &boards[arg];
1481 rc = 0;
1482 break;
1483 case SXIO_GET_TYPE:
1484 rc = -ENOENT; /* If we manage to miss one, return error. */
1485 if (IS_SX_BOARD(board))
1486 rc = SX_TYPE_SX;
1487 if (IS_CF_BOARD(board))
1488 rc = SX_TYPE_CF;
1489 if (IS_SI_BOARD(board))
1490 rc = SX_TYPE_SI;
1491 if (IS_SI1_BOARD(board))
1492 rc = SX_TYPE_SI;
1493 if (IS_EISA_BOARD(board))
1494 rc = SX_TYPE_SI;
1495 sx_dprintk(SX_DEBUG_FIRMWARE, "returning type= %ld\n", rc);
1496 break;
1497 case SXIO_DO_RAMTEST:
1498 if (sx_initialized) { /* Already initialized: better not ramtest the board. */
1499 rc = -EPERM;
1500 break;
1502 if (IS_SX_BOARD(board)) {
1503 rc = do_memtest(board, 0, 0x7000);
1504 if (!rc)
1505 rc = do_memtest(board, 0, 0x7000);
1506 /*if (!rc) rc = do_memtest_w (board, 0, 0x7000); */
1507 } else {
1508 rc = do_memtest(board, 0, 0x7ff8);
1509 /* if (!rc) rc = do_memtest_w (board, 0, 0x7ff8); */
1511 sx_dprintk(SX_DEBUG_FIRMWARE,
1512 "returning memtest result= %ld\n", rc);
1513 break;
1514 case SXIO_DOWNLOAD:
1515 if (sx_initialized) {/* Already initialized */
1516 rc = -EEXIST;
1517 break;
1519 if (!sx_reset(board)) {
1520 rc = -EIO;
1521 break;
1523 sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
1525 tmp = kmalloc(SX_CHUNK_SIZE, GFP_USER);
1526 if (!tmp) {
1527 rc = -ENOMEM;
1528 break;
1530 get_user(nbytes, descr++);
1531 get_user(offset, descr++);
1532 get_user(data, descr++);
1533 while (nbytes && data) {
1534 for (i = 0; i < nbytes; i += SX_CHUNK_SIZE) {
1535 if (copy_from_user(tmp, (char __user *)data + i,
1536 (i + SX_CHUNK_SIZE > nbytes) ?
1537 nbytes - i : SX_CHUNK_SIZE)) {
1538 kfree(tmp);
1539 rc = -EFAULT;
1540 goto out;
1542 memcpy_toio(board->base2 + offset + i, tmp,
1543 (i + SX_CHUNK_SIZE > nbytes) ?
1544 nbytes - i : SX_CHUNK_SIZE);
1547 get_user(nbytes, descr++);
1548 get_user(offset, descr++);
1549 get_user(data, descr++);
1551 kfree(tmp);
1552 sx_nports += sx_init_board(board);
1553 rc = sx_nports;
1554 break;
1555 case SXIO_INIT:
1556 if (sx_initialized) { /* Already initialized */
1557 rc = -EEXIST;
1558 break;
1560 /* This is not allowed until all boards are initialized... */
1561 for (i = 0; i < SX_NBOARDS; i++) {
1562 if ((boards[i].flags & SX_BOARD_PRESENT) &&
1563 !(boards[i].flags & SX_BOARD_INITIALIZED)) {
1564 rc = -EIO;
1565 break;
1568 for (i = 0; i < SX_NBOARDS; i++)
1569 if (!(boards[i].flags & SX_BOARD_PRESENT))
1570 break;
1572 sx_dprintk(SX_DEBUG_FIRMWARE, "initing portstructs, %d boards, "
1573 "%d channels, first board: %d ports\n",
1574 i, sx_nports, boards[0].nports);
1575 rc = sx_init_portstructs(i, sx_nports);
1576 sx_init_drivers();
1577 if (rc >= 0)
1578 sx_initialized++;
1579 break;
1580 case SXIO_SETDEBUG:
1581 sx_debug = arg;
1582 break;
1583 case SXIO_GETDEBUG:
1584 rc = sx_debug;
1585 break;
1586 case SXIO_GETGSDEBUG:
1587 case SXIO_SETGSDEBUG:
1588 rc = -EINVAL;
1589 break;
1590 case SXIO_GETNPORTS:
1591 rc = sx_nports;
1592 break;
1593 default:
1594 rc = -ENOTTY;
1595 break;
1597 out:
1598 tty_unlock();
1599 func_exit();
1600 return rc;
1603 static int sx_break(struct tty_struct *tty, int flag)
1605 struct sx_port *port = tty->driver_data;
1606 int rv;
1608 func_enter();
1609 tty_lock();
1611 if (flag)
1612 rv = sx_send_command(port, HS_START, -1, HS_IDLE_BREAK);
1613 else
1614 rv = sx_send_command(port, HS_STOP, -1, HS_IDLE_OPEN);
1615 if (rv != 1)
1616 printk(KERN_ERR "sx: couldn't send break (%x).\n",
1617 read_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat)));
1618 tty_unlock();
1619 func_exit();
1620 return 0;
1623 static int sx_tiocmget(struct tty_struct *tty, struct file *file)
1625 struct sx_port *port = tty->driver_data;
1626 return sx_getsignals(port);
1629 static int sx_tiocmset(struct tty_struct *tty, struct file *file,
1630 unsigned int set, unsigned int clear)
1632 struct sx_port *port = tty->driver_data;
1633 int rts = -1, dtr = -1;
1635 if (set & TIOCM_RTS)
1636 rts = 1;
1637 if (set & TIOCM_DTR)
1638 dtr = 1;
1639 if (clear & TIOCM_RTS)
1640 rts = 0;
1641 if (clear & TIOCM_DTR)
1642 dtr = 0;
1644 sx_setsignals(port, dtr, rts);
1645 sx_reconfigure_port(port);
1646 return 0;
1649 static int sx_ioctl(struct tty_struct *tty, struct file *filp,
1650 unsigned int cmd, unsigned long arg)
1652 int rc;
1653 struct sx_port *port = tty->driver_data;
1654 void __user *argp = (void __user *)arg;
1656 /* func_enter2(); */
1658 rc = 0;
1659 tty_lock();
1660 switch (cmd) {
1661 case TIOCGSERIAL:
1662 rc = gs_getserial(&port->gs, argp);
1663 break;
1664 case TIOCSSERIAL:
1665 rc = gs_setserial(&port->gs, argp);
1666 break;
1667 default:
1668 rc = -ENOIOCTLCMD;
1669 break;
1671 tty_unlock();
1673 /* func_exit(); */
1674 return rc;
1677 /* The throttle/unthrottle scheme for the Specialix card is different
1678 * from other drivers and deserves some explanation.
1679 * The Specialix hardware takes care of XON/XOFF
1680 * and CTS/RTS flow control itself. This means that all we have to
1681 * do when signalled by the upper tty layer to throttle/unthrottle is
1682 * to make a note of it here. When we come to read characters from the
1683 * rx buffers on the card (sx_receive_chars()) we look to see if the
1684 * upper layer can accept more (as noted here in sx_rx_throt[]).
1685 * If it can't we simply don't remove chars from the cards buffer.
1686 * When the tty layer can accept chars, we again note that here and when
1687 * sx_receive_chars() is called it will remove them from the cards buffer.
1688 * The card will notice that a ports buffer has drained below some low
1689 * water mark and will unflow control the line itself, using whatever
1690 * flow control scheme is in use for that port. -- Simon Allen
1693 static void sx_throttle(struct tty_struct *tty)
1695 struct sx_port *port = tty->driver_data;
1697 func_enter2();
1698 /* If the port is using any type of input flow
1699 * control then throttle the port.
1701 if ((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty))) {
1702 port->gs.port.flags |= SX_RX_THROTTLE;
1704 func_exit();
1707 static void sx_unthrottle(struct tty_struct *tty)
1709 struct sx_port *port = tty->driver_data;
1711 func_enter2();
1712 /* Always unthrottle even if flow control is not enabled on
1713 * this port in case we disabled flow control while the port
1714 * was throttled
1716 port->gs.port.flags &= ~SX_RX_THROTTLE;
1717 func_exit();
1718 return;
1721 /* ********************************************************************** *
1722 * Here are the initialization routines. *
1723 * ********************************************************************** */
1725 static int sx_init_board(struct sx_board *board)
1727 int addr;
1728 int chans;
1729 int type;
1731 func_enter();
1733 /* This is preceded by downloading the download code. */
1735 board->flags |= SX_BOARD_INITIALIZED;
1737 if (read_sx_byte(board, 0))
1738 /* CF boards may need this. */
1739 write_sx_byte(board, 0, 0);
1741 /* This resets the processor again, to make sure it didn't do any
1742 foolish things while we were downloading the image */
1743 if (!sx_reset(board))
1744 return 0;
1746 sx_start_board(board);
1747 udelay(10);
1748 if (!sx_busy_wait_neq(board, 0, 0xff, 0)) {
1749 printk(KERN_ERR "sx: Ooops. Board won't initialize.\n");
1750 return 0;
1753 /* Ok. So now the processor on the card is running. It gathered
1754 some info for us... */
1755 sx_dprintk(SX_DEBUG_INIT, "The sxcard structure:\n");
1756 if (sx_debug & SX_DEBUG_INIT)
1757 my_hd_io(board->base, 0x10);
1758 sx_dprintk(SX_DEBUG_INIT, "the first sx_module structure:\n");
1759 if (sx_debug & SX_DEBUG_INIT)
1760 my_hd_io(board->base + 0x80, 0x30);
1762 sx_dprintk(SX_DEBUG_INIT, "init_status: %x, %dk memory, firmware "
1763 "V%x.%02x,\n",
1764 read_sx_byte(board, 0), read_sx_byte(board, 1),
1765 read_sx_byte(board, 5), read_sx_byte(board, 4));
1767 if (read_sx_byte(board, 0) == 0xff) {
1768 printk(KERN_INFO "sx: No modules found. Sorry.\n");
1769 board->nports = 0;
1770 return 0;
1773 chans = 0;
1775 if (IS_SX_BOARD(board)) {
1776 sx_write_board_word(board, cc_int_count, sx_maxints);
1777 } else {
1778 if (sx_maxints)
1779 sx_write_board_word(board, cc_int_count,
1780 SI_PROCESSOR_CLOCK / 8 / sx_maxints);
1783 /* grab the first module type... */
1784 /* board->ta_type = mod_compat_type (read_sx_byte (board, 0x80 + 0x08)); */
1785 board->ta_type = mod_compat_type(sx_read_module_byte(board, 0x80,
1786 mc_chip));
1788 for (addr = 0x80; addr != 0; addr = read_sx_word(board, addr) & 0x7fff){
1789 type = sx_read_module_byte(board, addr, mc_chip);
1790 sx_dprintk(SX_DEBUG_INIT, "Module at %x: %d channels\n",
1791 addr, read_sx_byte(board, addr + 2));
1793 chans += sx_read_module_byte(board, addr, mc_type);
1795 sx_dprintk(SX_DEBUG_INIT, "module is an %s, which has %s/%s "
1796 "panels\n",
1797 mod_type_s(type),
1798 pan_type_s(sx_read_module_byte(board, addr,
1799 mc_mods) & 0xf),
1800 pan_type_s(sx_read_module_byte(board, addr,
1801 mc_mods) >> 4));
1803 sx_dprintk(SX_DEBUG_INIT, "CD1400 versions: %x/%x, ASIC "
1804 "version: %x\n",
1805 sx_read_module_byte(board, addr, mc_rev1),
1806 sx_read_module_byte(board, addr, mc_rev2),
1807 sx_read_module_byte(board, addr, mc_mtaasic_rev));
1809 /* The following combinations are illegal: It should theoretically
1810 work, but timing problems make the bus HANG. */
1812 if (mod_compat_type(type) != board->ta_type) {
1813 printk(KERN_ERR "sx: This is an invalid "
1814 "configuration.\nDon't mix TA/MTA/SXDC on the "
1815 "same hostadapter.\n");
1816 chans = 0;
1817 break;
1819 if ((IS_EISA_BOARD(board) ||
1820 IS_SI_BOARD(board)) &&
1821 (mod_compat_type(type) == 4)) {
1822 printk(KERN_ERR "sx: This is an invalid "
1823 "configuration.\nDon't use SXDCs on an SI/XIO "
1824 "adapter.\n");
1825 chans = 0;
1826 break;
1830 if (chans) {
1831 if (board->irq > 0) {
1832 /* fixed irq, probably PCI */
1833 if (sx_irqmask & (1 << board->irq)) { /* may we use this irq? */
1834 if (request_irq(board->irq, sx_interrupt,
1835 IRQF_SHARED | IRQF_DISABLED,
1836 "sx", board)) {
1837 printk(KERN_ERR "sx: Cannot allocate "
1838 "irq %d.\n", board->irq);
1839 board->irq = 0;
1841 } else
1842 board->irq = 0;
1843 } else if (board->irq < 0 && sx_irqmask) {
1844 /* auto-allocate irq */
1845 int irqnr;
1846 int irqmask = sx_irqmask & (IS_SX_BOARD(board) ?
1847 SX_ISA_IRQ_MASK : SI2_ISA_IRQ_MASK);
1848 for (irqnr = 15; irqnr > 0; irqnr--)
1849 if (irqmask & (1 << irqnr))
1850 if (!request_irq(irqnr, sx_interrupt,
1851 IRQF_SHARED | IRQF_DISABLED,
1852 "sx", board))
1853 break;
1854 if (!irqnr)
1855 printk(KERN_ERR "sx: Cannot allocate IRQ.\n");
1856 board->irq = irqnr;
1857 } else
1858 board->irq = 0;
1860 if (board->irq) {
1861 /* Found a valid interrupt, start up interrupts! */
1862 sx_dprintk(SX_DEBUG_INIT, "Using irq %d.\n",
1863 board->irq);
1864 sx_start_interrupts(board);
1865 board->poll = sx_slowpoll;
1866 board->flags |= SX_IRQ_ALLOCATED;
1867 } else {
1868 /* no irq: setup board for polled operation */
1869 board->poll = sx_poll;
1870 sx_dprintk(SX_DEBUG_INIT, "Using poll-interval %d.\n",
1871 board->poll);
1874 /* The timer should be initialized anyway: That way we can
1875 safely del_timer it when the module is unloaded. */
1876 setup_timer(&board->timer, sx_pollfunc, (unsigned long)board);
1878 if (board->poll)
1879 mod_timer(&board->timer, jiffies + board->poll);
1880 } else {
1881 board->irq = 0;
1884 board->nports = chans;
1885 sx_dprintk(SX_DEBUG_INIT, "returning %d ports.", board->nports);
1887 func_exit();
1888 return chans;
1891 static void __devinit printheader(void)
1893 static int header_printed;
1895 if (!header_printed) {
1896 printk(KERN_INFO "Specialix SX driver "
1897 "(C) 1998/1999 R.E.Wolff@BitWizard.nl\n");
1898 printk(KERN_INFO "sx: version " __stringify(SX_VERSION) "\n");
1899 header_printed = 1;
1903 static int __devinit probe_sx(struct sx_board *board)
1905 struct vpd_prom vpdp;
1906 char *p;
1907 int i;
1909 func_enter();
1911 if (!IS_CF_BOARD(board)) {
1912 sx_dprintk(SX_DEBUG_PROBE, "Going to verify vpd prom at %p.\n",
1913 board->base + SX_VPD_ROM);
1915 if (sx_debug & SX_DEBUG_PROBE)
1916 my_hd_io(board->base + SX_VPD_ROM, 0x40);
1918 p = (char *)&vpdp;
1919 for (i = 0; i < sizeof(struct vpd_prom); i++)
1920 *p++ = read_sx_byte(board, SX_VPD_ROM + i * 2);
1922 if (sx_debug & SX_DEBUG_PROBE)
1923 my_hd(&vpdp, 0x20);
1925 sx_dprintk(SX_DEBUG_PROBE, "checking identifier...\n");
1927 if (strncmp(vpdp.identifier, SX_VPD_IDENT_STRING, 16) != 0) {
1928 sx_dprintk(SX_DEBUG_PROBE, "Got non-SX identifier: "
1929 "'%s'\n", vpdp.identifier);
1930 return 0;
1934 printheader();
1936 if (!IS_CF_BOARD(board)) {
1937 printk(KERN_DEBUG "sx: Found an SX board at %lx\n",
1938 board->hw_base);
1939 printk(KERN_DEBUG "sx: hw_rev: %d, assembly level: %d, "
1940 "uniq ID:%08x, ",
1941 vpdp.hwrev, vpdp.hwass, vpdp.uniqid);
1942 printk("Manufactured: %d/%d\n", 1970 + vpdp.myear, vpdp.mweek);
1944 if ((((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) !=
1945 SX_PCI_UNIQUEID1) && (((vpdp.uniqid >> 24) &
1946 SX_UNIQUEID_MASK) != SX_ISA_UNIQUEID1)) {
1947 /* This might be a bit harsh. This was the primary
1948 reason the SX/ISA card didn't work at first... */
1949 printk(KERN_ERR "sx: Hmm. Not an SX/PCI or SX/ISA "
1950 "card. Sorry: giving up.\n");
1951 return (0);
1954 if (((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) ==
1955 SX_ISA_UNIQUEID1) {
1956 if (((unsigned long)board->hw_base) & 0x8000) {
1957 printk(KERN_WARNING "sx: Warning: There may be "
1958 "hardware problems with the card at "
1959 "%lx.\n", board->hw_base);
1960 printk(KERN_WARNING "sx: Read sx.txt for more "
1961 "info.\n");
1966 board->nports = -1;
1968 /* This resets the processor, and keeps it off the bus. */
1969 if (!sx_reset(board))
1970 return 0;
1971 sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
1973 func_exit();
1974 return 1;
1977 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
1979 /* Specialix probes for this card at 32k increments from 640k to 16M.
1980 I consider machines with less than 16M unlikely nowadays, so I'm
1981 not probing above 1Mb. Also, 0xa0000, 0xb0000, are taken by the VGA
1982 card. 0xe0000 and 0xf0000 are taken by the BIOS. That only leaves
1983 0xc0000, 0xc8000, 0xd0000 and 0xd8000 . */
1985 static int __devinit probe_si(struct sx_board *board)
1987 int i;
1989 func_enter();
1990 sx_dprintk(SX_DEBUG_PROBE, "Going to verify SI signature hw %lx at "
1991 "%p.\n", board->hw_base, board->base + SI2_ISA_ID_BASE);
1993 if (sx_debug & SX_DEBUG_PROBE)
1994 my_hd_io(board->base + SI2_ISA_ID_BASE, 0x8);
1996 if (!IS_EISA_BOARD(board)) {
1997 if (IS_SI1_BOARD(board)) {
1998 for (i = 0; i < 8; i++) {
1999 write_sx_byte(board, SI2_ISA_ID_BASE + 7 - i,i);
2002 for (i = 0; i < 8; i++) {
2003 if ((read_sx_byte(board, SI2_ISA_ID_BASE + 7 - i) & 7)
2004 != i) {
2005 func_exit();
2006 return 0;
2011 /* Now we're pretty much convinced that there is an SI board here,
2012 but to prevent trouble, we'd better double check that we don't
2013 have an SI1 board when we're probing for an SI2 board.... */
2015 write_sx_byte(board, SI2_ISA_ID_BASE, 0x10);
2016 if (IS_SI1_BOARD(board)) {
2017 /* This should be an SI1 board, which has this
2018 location writable... */
2019 if (read_sx_byte(board, SI2_ISA_ID_BASE) != 0x10) {
2020 func_exit();
2021 return 0;
2023 } else {
2024 /* This should be an SI2 board, which has the bottom
2025 3 bits non-writable... */
2026 if (read_sx_byte(board, SI2_ISA_ID_BASE) == 0x10) {
2027 func_exit();
2028 return 0;
2032 /* Now we're pretty much convinced that there is an SI board here,
2033 but to prevent trouble, we'd better double check that we don't
2034 have an SI1 board when we're probing for an SI2 board.... */
2036 write_sx_byte(board, SI2_ISA_ID_BASE, 0x10);
2037 if (IS_SI1_BOARD(board)) {
2038 /* This should be an SI1 board, which has this
2039 location writable... */
2040 if (read_sx_byte(board, SI2_ISA_ID_BASE) != 0x10) {
2041 func_exit();
2042 return 0;
2044 } else {
2045 /* This should be an SI2 board, which has the bottom
2046 3 bits non-writable... */
2047 if (read_sx_byte(board, SI2_ISA_ID_BASE) == 0x10) {
2048 func_exit();
2049 return 0;
2053 printheader();
2055 printk(KERN_DEBUG "sx: Found an SI board at %lx\n", board->hw_base);
2056 /* Compared to the SX boards, it is a complete guess as to what
2057 this card is up to... */
2059 board->nports = -1;
2061 /* This resets the processor, and keeps it off the bus. */
2062 if (!sx_reset(board))
2063 return 0;
2064 sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
2066 func_exit();
2067 return 1;
2069 #endif
2071 static const struct tty_operations sx_ops = {
2072 .break_ctl = sx_break,
2073 .open = sx_open,
2074 .close = gs_close,
2075 .write = gs_write,
2076 .put_char = gs_put_char,
2077 .flush_chars = gs_flush_chars,
2078 .write_room = gs_write_room,
2079 .chars_in_buffer = gs_chars_in_buffer,
2080 .flush_buffer = gs_flush_buffer,
2081 .ioctl = sx_ioctl,
2082 .throttle = sx_throttle,
2083 .unthrottle = sx_unthrottle,
2084 .set_termios = gs_set_termios,
2085 .stop = gs_stop,
2086 .start = gs_start,
2087 .hangup = gs_hangup,
2088 .tiocmget = sx_tiocmget,
2089 .tiocmset = sx_tiocmset,
2092 static const struct tty_port_operations sx_port_ops = {
2093 .carrier_raised = sx_carrier_raised,
2096 static int sx_init_drivers(void)
2098 int error;
2100 func_enter();
2102 sx_driver = alloc_tty_driver(sx_nports);
2103 if (!sx_driver)
2104 return 1;
2105 sx_driver->owner = THIS_MODULE;
2106 sx_driver->driver_name = "specialix_sx";
2107 sx_driver->name = "ttyX";
2108 sx_driver->major = SX_NORMAL_MAJOR;
2109 sx_driver->type = TTY_DRIVER_TYPE_SERIAL;
2110 sx_driver->subtype = SERIAL_TYPE_NORMAL;
2111 sx_driver->init_termios = tty_std_termios;
2112 sx_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2113 sx_driver->init_termios.c_ispeed = 9600;
2114 sx_driver->init_termios.c_ospeed = 9600;
2115 sx_driver->flags = TTY_DRIVER_REAL_RAW;
2116 tty_set_operations(sx_driver, &sx_ops);
2118 if ((error = tty_register_driver(sx_driver))) {
2119 put_tty_driver(sx_driver);
2120 printk(KERN_ERR "sx: Couldn't register sx driver, error = %d\n",
2121 error);
2122 return 1;
2124 func_exit();
2125 return 0;
2128 static int sx_init_portstructs(int nboards, int nports)
2130 struct sx_board *board;
2131 struct sx_port *port;
2132 int i, j;
2133 int addr, chans;
2134 int portno;
2136 func_enter();
2138 /* Many drivers statically allocate the maximum number of ports
2139 There is no reason not to allocate them dynamically.
2140 Is there? -- REW */
2141 sx_ports = kcalloc(nports, sizeof(struct sx_port), GFP_KERNEL);
2142 if (!sx_ports)
2143 return -ENOMEM;
2145 port = sx_ports;
2146 for (i = 0; i < nboards; i++) {
2147 board = &boards[i];
2148 board->ports = port;
2149 for (j = 0; j < boards[i].nports; j++) {
2150 sx_dprintk(SX_DEBUG_INIT, "initing port %d\n", j);
2151 tty_port_init(&port->gs.port);
2152 port->gs.port.ops = &sx_port_ops;
2153 port->gs.magic = SX_MAGIC;
2154 port->gs.close_delay = HZ / 2;
2155 port->gs.closing_wait = 30 * HZ;
2156 port->board = board;
2157 port->gs.rd = &sx_real_driver;
2158 #ifdef NEW_WRITE_LOCKING
2159 port->gs.port_write_mutex = MUTEX;
2160 #endif
2161 spin_lock_init(&port->gs.driver_lock);
2163 * Initializing wait queue
2165 port++;
2169 port = sx_ports;
2170 portno = 0;
2171 for (i = 0; i < nboards; i++) {
2172 board = &boards[i];
2173 board->port_base = portno;
2174 /* Possibly the configuration was rejected. */
2175 sx_dprintk(SX_DEBUG_PROBE, "Board has %d channels\n",
2176 board->nports);
2177 if (board->nports <= 0)
2178 continue;
2179 for (addr = 0x80; addr != 0;
2180 addr = read_sx_word(board, addr) & 0x7fff) {
2181 chans = sx_read_module_byte(board, addr, mc_type);
2182 sx_dprintk(SX_DEBUG_PROBE, "Module at %x: %d "
2183 "channels\n", addr, chans);
2184 sx_dprintk(SX_DEBUG_PROBE, "Port at");
2185 for (j = 0; j < chans; j++) {
2186 /* The "sx-way" is the way it SHOULD be done.
2187 That way in the future, the firmware may for
2188 example pack the structures a bit more
2189 efficient. Neil tells me it isn't going to
2190 happen anytime soon though. */
2191 if (IS_SX_BOARD(board))
2192 port->ch_base = sx_read_module_word(
2193 board, addr + j * 2,
2194 mc_chan_pointer);
2195 else
2196 port->ch_base = addr + 0x100 + 0x300 *j;
2198 sx_dprintk(SX_DEBUG_PROBE, " %x",
2199 port->ch_base);
2200 port->line = portno++;
2201 port++;
2203 sx_dprintk(SX_DEBUG_PROBE, "\n");
2205 /* This has to be done earlier. */
2206 /* board->flags |= SX_BOARD_INITIALIZED; */
2209 func_exit();
2210 return 0;
2213 static unsigned int sx_find_free_board(void)
2215 unsigned int i;
2217 for (i = 0; i < SX_NBOARDS; i++)
2218 if (!(boards[i].flags & SX_BOARD_PRESENT))
2219 break;
2221 return i;
2224 static void __exit sx_release_drivers(void)
2226 func_enter();
2227 tty_unregister_driver(sx_driver);
2228 put_tty_driver(sx_driver);
2229 func_exit();
2232 static void __devexit sx_remove_card(struct sx_board *board,
2233 struct pci_dev *pdev)
2235 if (board->flags & SX_BOARD_INITIALIZED) {
2236 /* The board should stop messing with us. (actually I mean the
2237 interrupt) */
2238 sx_reset(board);
2239 if ((board->irq) && (board->flags & SX_IRQ_ALLOCATED))
2240 free_irq(board->irq, board);
2242 /* It is safe/allowed to del_timer a non-active timer */
2243 del_timer(&board->timer);
2244 if (pdev) {
2245 #ifdef CONFIG_PCI
2246 iounmap(board->base2);
2247 pci_release_region(pdev, IS_CF_BOARD(board) ? 3 : 2);
2248 #endif
2249 } else {
2250 iounmap(board->base);
2251 release_region(board->hw_base, board->hw_len);
2254 board->flags &= ~(SX_BOARD_INITIALIZED | SX_BOARD_PRESENT);
2258 #ifdef CONFIG_EISA
2260 static int __devinit sx_eisa_probe(struct device *dev)
2262 struct eisa_device *edev = to_eisa_device(dev);
2263 struct sx_board *board;
2264 unsigned long eisa_slot = edev->base_addr;
2265 unsigned int i;
2266 int retval = -EIO;
2268 mutex_lock(&sx_boards_lock);
2269 i = sx_find_free_board();
2270 if (i == SX_NBOARDS) {
2271 mutex_unlock(&sx_boards_lock);
2272 goto err;
2274 board = &boards[i];
2275 board->flags |= SX_BOARD_PRESENT;
2276 mutex_unlock(&sx_boards_lock);
2278 dev_info(dev, "XIO : Signature found in EISA slot %lu, "
2279 "Product %d Rev %d (REPORT THIS TO LKLM)\n",
2280 eisa_slot >> 12,
2281 inb(eisa_slot + EISA_VENDOR_ID_OFFSET + 2),
2282 inb(eisa_slot + EISA_VENDOR_ID_OFFSET + 3));
2284 board->eisa_base = eisa_slot;
2285 board->flags &= ~SX_BOARD_TYPE;
2286 board->flags |= SI_EISA_BOARD;
2288 board->hw_base = ((inb(eisa_slot + 0xc01) << 8) +
2289 inb(eisa_slot + 0xc00)) << 16;
2290 board->hw_len = SI2_EISA_WINDOW_LEN;
2291 if (!request_region(board->hw_base, board->hw_len, "sx")) {
2292 dev_err(dev, "can't request region\n");
2293 goto err_flag;
2295 board->base2 =
2296 board->base = ioremap_nocache(board->hw_base, SI2_EISA_WINDOW_LEN);
2297 if (!board->base) {
2298 dev_err(dev, "can't remap memory\n");
2299 goto err_reg;
2302 sx_dprintk(SX_DEBUG_PROBE, "IO hw_base address: %lx\n", board->hw_base);
2303 sx_dprintk(SX_DEBUG_PROBE, "base: %p\n", board->base);
2304 board->irq = inb(eisa_slot + 0xc02) >> 4;
2305 sx_dprintk(SX_DEBUG_PROBE, "IRQ: %d\n", board->irq);
2307 if (!probe_si(board))
2308 goto err_unmap;
2310 dev_set_drvdata(dev, board);
2312 return 0;
2313 err_unmap:
2314 iounmap(board->base);
2315 err_reg:
2316 release_region(board->hw_base, board->hw_len);
2317 err_flag:
2318 board->flags &= ~SX_BOARD_PRESENT;
2319 err:
2320 return retval;
2323 static int __devexit sx_eisa_remove(struct device *dev)
2325 struct sx_board *board = dev_get_drvdata(dev);
2327 sx_remove_card(board, NULL);
2329 return 0;
2332 static struct eisa_device_id sx_eisa_tbl[] = {
2333 { "SLX" },
2334 { "" }
2337 MODULE_DEVICE_TABLE(eisa, sx_eisa_tbl);
2339 static struct eisa_driver sx_eisadriver = {
2340 .id_table = sx_eisa_tbl,
2341 .driver = {
2342 .name = "sx",
2343 .probe = sx_eisa_probe,
2344 .remove = __devexit_p(sx_eisa_remove),
2348 #endif
2350 #ifdef CONFIG_PCI
2351 /********************************************************
2352 * Setting bit 17 in the CNTRL register of the PLX 9050 *
2353 * chip forces a retry on writes while a read is pending.*
2354 * This is to prevent the card locking up on Intel Xeon *
2355 * multiprocessor systems with the NX chipset. -- NV *
2356 ********************************************************/
2358 /* Newer cards are produced with this bit set from the configuration
2359 EEprom. As the bit is read/write for the CPU, we can fix it here,
2360 if we detect that it isn't set correctly. -- REW */
2362 static void __devinit fix_sx_pci(struct pci_dev *pdev, struct sx_board *board)
2364 unsigned int hwbase;
2365 void __iomem *rebase;
2366 unsigned int t;
2368 #define CNTRL_REG_OFFSET 0x50
2369 #define CNTRL_REG_GOODVALUE 0x18260000
2371 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &hwbase);
2372 hwbase &= PCI_BASE_ADDRESS_MEM_MASK;
2373 rebase = ioremap_nocache(hwbase, 0x80);
2374 t = readl(rebase + CNTRL_REG_OFFSET);
2375 if (t != CNTRL_REG_GOODVALUE) {
2376 printk(KERN_DEBUG "sx: performing cntrl reg fix: %08x -> "
2377 "%08x\n", t, CNTRL_REG_GOODVALUE);
2378 writel(CNTRL_REG_GOODVALUE, rebase + CNTRL_REG_OFFSET);
2380 iounmap(rebase);
2382 #endif
2384 static int __devinit sx_pci_probe(struct pci_dev *pdev,
2385 const struct pci_device_id *ent)
2387 #ifdef CONFIG_PCI
2388 struct sx_board *board;
2389 unsigned int i, reg;
2390 int retval = -EIO;
2392 mutex_lock(&sx_boards_lock);
2393 i = sx_find_free_board();
2394 if (i == SX_NBOARDS) {
2395 mutex_unlock(&sx_boards_lock);
2396 goto err;
2398 board = &boards[i];
2399 board->flags |= SX_BOARD_PRESENT;
2400 mutex_unlock(&sx_boards_lock);
2402 retval = pci_enable_device(pdev);
2403 if (retval)
2404 goto err_flag;
2406 board->flags &= ~SX_BOARD_TYPE;
2407 board->flags |= (pdev->subsystem_vendor == 0x200) ? SX_PCI_BOARD :
2408 SX_CFPCI_BOARD;
2410 /* CF boards use base address 3.... */
2411 reg = IS_CF_BOARD(board) ? 3 : 2;
2412 retval = pci_request_region(pdev, reg, "sx");
2413 if (retval) {
2414 dev_err(&pdev->dev, "can't request region\n");
2415 goto err_flag;
2417 board->hw_base = pci_resource_start(pdev, reg);
2418 board->base2 =
2419 board->base = ioremap_nocache(board->hw_base, WINDOW_LEN(board));
2420 if (!board->base) {
2421 dev_err(&pdev->dev, "ioremap failed\n");
2422 goto err_reg;
2425 /* Most of the stuff on the CF board is offset by 0x18000 .... */
2426 if (IS_CF_BOARD(board))
2427 board->base += 0x18000;
2429 board->irq = pdev->irq;
2431 dev_info(&pdev->dev, "Got a specialix card: %p(%d) %x.\n", board->base,
2432 board->irq, board->flags);
2434 if (!probe_sx(board)) {
2435 retval = -EIO;
2436 goto err_unmap;
2439 fix_sx_pci(pdev, board);
2441 pci_set_drvdata(pdev, board);
2443 return 0;
2444 err_unmap:
2445 iounmap(board->base2);
2446 err_reg:
2447 pci_release_region(pdev, reg);
2448 err_flag:
2449 board->flags &= ~SX_BOARD_PRESENT;
2450 err:
2451 return retval;
2452 #else
2453 return -ENODEV;
2454 #endif
2457 static void __devexit sx_pci_remove(struct pci_dev *pdev)
2459 struct sx_board *board = pci_get_drvdata(pdev);
2461 sx_remove_card(board, pdev);
2464 /* Specialix has a whole bunch of cards with 0x2000 as the device ID. They say
2465 its because the standard requires it. So check for SUBVENDOR_ID. */
2466 static struct pci_device_id sx_pci_tbl[] = {
2467 { PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8,
2468 .subvendor = PCI_ANY_ID, .subdevice = 0x0200 },
2469 { PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8,
2470 .subvendor = PCI_ANY_ID, .subdevice = 0x0300 },
2471 { 0 }
2474 MODULE_DEVICE_TABLE(pci, sx_pci_tbl);
2476 static struct pci_driver sx_pcidriver = {
2477 .name = "sx",
2478 .id_table = sx_pci_tbl,
2479 .probe = sx_pci_probe,
2480 .remove = __devexit_p(sx_pci_remove)
2483 static int __init sx_init(void)
2485 #ifdef CONFIG_EISA
2486 int retval1;
2487 #endif
2488 #ifdef CONFIG_ISA
2489 struct sx_board *board;
2490 unsigned int i;
2491 #endif
2492 unsigned int found = 0;
2493 int retval;
2495 func_enter();
2496 sx_dprintk(SX_DEBUG_INIT, "Initing sx module... (sx_debug=%d)\n",
2497 sx_debug);
2498 if (abs((long)(&sx_debug) - sx_debug) < 0x10000) {
2499 printk(KERN_WARNING "sx: sx_debug is an address, instead of a "
2500 "value. Assuming -1.\n(%p)\n", &sx_debug);
2501 sx_debug = -1;
2504 if (misc_register(&sx_fw_device) < 0) {
2505 printk(KERN_ERR "SX: Unable to register firmware loader "
2506 "driver.\n");
2507 return -EIO;
2509 #ifdef CONFIG_ISA
2510 for (i = 0; i < NR_SX_ADDRS; i++) {
2511 board = &boards[found];
2512 board->hw_base = sx_probe_addrs[i];
2513 board->hw_len = SX_WINDOW_LEN;
2514 if (!request_region(board->hw_base, board->hw_len, "sx"))
2515 continue;
2516 board->base2 =
2517 board->base = ioremap_nocache(board->hw_base, board->hw_len);
2518 if (!board->base)
2519 goto err_sx_reg;
2520 board->flags &= ~SX_BOARD_TYPE;
2521 board->flags |= SX_ISA_BOARD;
2522 board->irq = sx_irqmask ? -1 : 0;
2524 if (probe_sx(board)) {
2525 board->flags |= SX_BOARD_PRESENT;
2526 found++;
2527 } else {
2528 iounmap(board->base);
2529 err_sx_reg:
2530 release_region(board->hw_base, board->hw_len);
2534 for (i = 0; i < NR_SI_ADDRS; i++) {
2535 board = &boards[found];
2536 board->hw_base = si_probe_addrs[i];
2537 board->hw_len = SI2_ISA_WINDOW_LEN;
2538 if (!request_region(board->hw_base, board->hw_len, "sx"))
2539 continue;
2540 board->base2 =
2541 board->base = ioremap_nocache(board->hw_base, board->hw_len);
2542 if (!board->base)
2543 goto err_si_reg;
2544 board->flags &= ~SX_BOARD_TYPE;
2545 board->flags |= SI_ISA_BOARD;
2546 board->irq = sx_irqmask ? -1 : 0;
2548 if (probe_si(board)) {
2549 board->flags |= SX_BOARD_PRESENT;
2550 found++;
2551 } else {
2552 iounmap(board->base);
2553 err_si_reg:
2554 release_region(board->hw_base, board->hw_len);
2557 for (i = 0; i < NR_SI1_ADDRS; i++) {
2558 board = &boards[found];
2559 board->hw_base = si1_probe_addrs[i];
2560 board->hw_len = SI1_ISA_WINDOW_LEN;
2561 if (!request_region(board->hw_base, board->hw_len, "sx"))
2562 continue;
2563 board->base2 =
2564 board->base = ioremap_nocache(board->hw_base, board->hw_len);
2565 if (!board->base)
2566 goto err_si1_reg;
2567 board->flags &= ~SX_BOARD_TYPE;
2568 board->flags |= SI1_ISA_BOARD;
2569 board->irq = sx_irqmask ? -1 : 0;
2571 if (probe_si(board)) {
2572 board->flags |= SX_BOARD_PRESENT;
2573 found++;
2574 } else {
2575 iounmap(board->base);
2576 err_si1_reg:
2577 release_region(board->hw_base, board->hw_len);
2580 #endif
2581 #ifdef CONFIG_EISA
2582 retval1 = eisa_driver_register(&sx_eisadriver);
2583 #endif
2584 retval = pci_register_driver(&sx_pcidriver);
2586 if (found) {
2587 printk(KERN_INFO "sx: total of %d boards detected.\n", found);
2588 retval = 0;
2589 } else if (retval) {
2590 #ifdef CONFIG_EISA
2591 retval = retval1;
2592 if (retval1)
2593 #endif
2594 misc_deregister(&sx_fw_device);
2597 func_exit();
2598 return retval;
2601 static void __exit sx_exit(void)
2603 int i;
2605 func_enter();
2606 #ifdef CONFIG_EISA
2607 eisa_driver_unregister(&sx_eisadriver);
2608 #endif
2609 pci_unregister_driver(&sx_pcidriver);
2611 for (i = 0; i < SX_NBOARDS; i++)
2612 sx_remove_card(&boards[i], NULL);
2614 if (misc_deregister(&sx_fw_device) < 0) {
2615 printk(KERN_INFO "sx: couldn't deregister firmware loader "
2616 "device\n");
2618 sx_dprintk(SX_DEBUG_CLEANUP, "Cleaning up drivers (%d)\n",
2619 sx_initialized);
2620 if (sx_initialized)
2621 sx_release_drivers();
2623 kfree(sx_ports);
2624 func_exit();
2627 module_init(sx_init);
2628 module_exit(sx_exit);