1 /* sx.c -- driver for the Specialix SX series cards.
3 * This driver will also support the older SI, and XIO cards.
6 * (C) 1998 - 2004 R.E.Wolff@BitWizard.nl
8 * Simon Allen (simonallen@cix.compulink.co.uk) wrote a previous
9 * version of this driver. Some fragments may have been copied. (none
12 * Specialix pays for the development and support of this driver.
13 * Please DO contact support@specialix.co.uk if you require
14 * support. But please read the documentation (sx.txt) first.
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License as
20 * published by the Free Software Foundation; either version 2 of
21 * the License, or (at your option) any later version.
23 * This program is distributed in the hope that it will be
24 * useful, but WITHOUT ANY WARRANTY; without even the implied
25 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
26 * PURPOSE. See the GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public
29 * License along with this program; if not, write to the Free
30 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
34 * Revision 1.33 2000/03/09 10:00:00 pvdl,wolff
35 * - Fixed module and port counting
36 * - Fixed signal handling
39 * Revision 1.32 2000/03/07 09:00:00 wolff,pvdl
40 * - Fixed some sx_dprintk typos
41 * - added detection for an invalid board/module configuration
43 * Revision 1.31 2000/03/06 12:00:00 wolff,pvdl
44 * - Added support for EISA
46 * Revision 1.30 2000/01/21 17:43:06 wolff
47 * - Added support for SX+
49 * Revision 1.26 1999/08/05 15:22:14 wolff
51 * - Reformatted to Linus' liking.
53 * Revision 1.25 1999/07/30 14:24:08 wolff
54 * Had accidentally left "gs_debug" set to "-1" instead of "off" (=0).
56 * Revision 1.24 1999/07/28 09:41:52 wolff
57 * - I noticed the remark about use-count straying in sx.txt. I checked
58 * sx_open, and found a few places where that could happen. I hope it's
61 * Revision 1.23 1999/07/28 08:56:06 wolff
62 * - Fixed crash when sx_firmware run twice.
63 * - Added sx_slowpoll as a module parameter (I guess nobody really wanted
64 * to change it from the default... )
65 * - Fixed a stupid editing problem I introduced in 1.22.
66 * - Fixed dropping characters on a termios change.
68 * Revision 1.22 1999/07/26 21:01:43 wolff
69 * Russell Brown noticed that I had overlooked 4 out of six modem control
70 * signals in sx_getsignals. Ooops.
72 * Revision 1.21 1999/07/23 09:11:33 wolff
73 * I forgot to free dynamically allocated memory when the driver is unloaded.
75 * Revision 1.20 1999/07/20 06:25:26 wolff
76 * The "closing wait" wasn't honoured. Thanks to James Griffiths for
79 * Revision 1.19 1999/07/11 08:59:59 wolff
80 * Fixed an oops in close, when an open was pending. Changed the memtest
81 * a bit. Should also test the board in word-mode, however my card fails the
82 * memtest then. I still have to figure out what is wrong...
84 * Revision 1.18 1999/06/10 09:38:42 wolff
85 * Changed the format of the firmware revision from %04x to %x.%02x .
87 * Revision 1.17 1999/06/04 09:44:35 wolff
88 * fixed problem: reference to pci stuff when config_pci was off...
89 * Thanks to Jorge Novo for noticing this.
91 * Revision 1.16 1999/06/02 08:30:15 wolff
92 * added/removed the workaround for the DCD bug in the Firmware.
93 * A bit more debugging code to locate that...
95 * Revision 1.15 1999/06/01 11:35:30 wolff
96 * when DCD is left low (floating?), on TA's the firmware first tells us
97 * that DCD is high, but after a short while suddenly comes to the
98 * conclusion that it is low. All this would be fine, if it weren't that
99 * Unix requires us to send a "hangup" signal in that case. This usually
100 * all happens BEFORE the program has had a chance to ioctl the device
103 * Revision 1.14 1999/05/25 11:18:59 wolff
105 * Added checks for return code of sx_sendcommand.
106 * Don't issue "reconfig" if port isn't open yet. (bit us on TA modules...)
108 * Revision 1.13 1999/04/29 15:18:01 wolff
109 * Fixed an "oops" that showed on SuSE 6.0 systems.
110 * Activate DTR again after stty 0.
112 * Revision 1.12 1999/04/29 07:49:52 wolff
113 * Improved "stty 0" handling a bit. (used to change baud to 9600 assuming
114 * the connection would be dropped anyway. That is not always the case,
115 * and confuses people).
116 * Told the card to always monitor the modem signals.
117 * Added support for dynamic gs_debug adjustments.
118 * Now tells the rest of the system the number of ports.
120 * Revision 1.11 1999/04/24 11:11:30 wolff
121 * Fixed two stupid typos in the memory test.
123 * Revision 1.10 1999/04/24 10:53:39 wolff
124 * Added some of Christian's suggestions.
125 * Fixed an HW_COOK_IN bug (ISIG was not in I_OTHER. We used to trust the
126 * card to send the signal to the process.....)
128 * Revision 1.9 1999/04/23 07:26:38 wolff
129 * Included Christian Lademann's 2.0 compile-warning fixes and interrupt
130 * assignment redesign.
131 * Cleanup of some other stuff.
133 * Revision 1.8 1999/04/16 13:05:30 wolff
134 * fixed a DCD change unnoticed bug.
136 * Revision 1.7 1999/04/14 22:19:51 wolff
137 * Fixed typo that showed up in 2.0.x builds (get_user instead of Get_user!)
139 * Revision 1.6 1999/04/13 18:40:20 wolff
140 * changed misc-minor to 161, as assigned by HPA.
142 * Revision 1.5 1999/04/13 15:12:25 wolff
143 * Fixed use-count leak when "hangup" occurred.
144 * Added workaround for a stupid-PCIBIOS bug.
147 * Revision 1.4 1999/04/01 22:47:40 wolff
148 * Fixed < 1M linux-2.0 problem.
149 * (vremap isn't compatible with ioremap in that case)
151 * Revision 1.3 1999/03/31 13:45:45 wolff
152 * Firmware loading is now done through a separate IOCTL.
154 * Revision 1.2 1999/03/28 12:22:29 wolff
157 * Revision 1.1 1999/03/28 12:10:34 wolff
158 * Readying for release on 2.0.x (sorry David, 1.01 becomes 1.1 for RCS).
160 * Revision 0.12 1999/03/28 09:20:10 wolff
161 * Fixed problem in 0.11, continueing cleanup.
163 * Revision 0.11 1999/03/28 08:46:44 wolff
166 * Revision 0.10 1999/03/28 08:09:43 wolff
167 * Fixed loosing characters on close.
169 * Revision 0.9 1999/03/21 22:52:01 wolff
170 * Ported back to 2.2.... (minor things)
172 * Revision 0.8 1999/03/21 22:40:33 wolff
175 * Revision 0.7 1999/03/21 19:06:34 wolff
176 * Fixed hangup processing.
178 * Revision 0.6 1999/02/05 08:45:14 wolff
179 * fixed real_raw problems. Inclusion into kernel imminent.
181 * Revision 0.5 1998/12/21 23:51:06 wolff
182 * Snatched a nasty bug: sx_transmit_chars was getting re-entered, and it
183 * shouldn't have. THATs why I want to have transmit interrupts even when
184 * the buffer is empty.
186 * Revision 0.4 1998/12/17 09:34:46 wolff
187 * PPP works. ioctl works. Basically works!
189 * Revision 0.3 1998/12/15 13:05:18 wolff
190 * It works! Wow! Gotta start implementing IOCTL and stuff....
192 * Revision 0.2 1998/12/01 08:33:53 wolff
193 * moved over to 2.1.130
195 * Revision 0.1 1998/11/03 21:23:51 wolff
196 * Initial revision. Detects SX card.
200 #define SX_VERSION 1.33
202 #include <linux/module.h>
203 #include <linux/kdev_t.h>
204 #include <linux/kernel.h>
205 #include <linux/sched.h>
206 #include <linux/ioport.h>
207 #include <linux/interrupt.h>
208 #include <linux/errno.h>
209 #include <linux/tty.h>
210 #include <linux/tty_flip.h>
211 #include <linux/mm.h>
212 #include <linux/serial.h>
213 #include <linux/fcntl.h>
214 #include <linux/major.h>
215 #include <linux/delay.h>
216 #include <linux/eisa.h>
217 #include <linux/pci.h>
218 #include <linux/slab.h>
219 #include <linux/smp_lock.h>
220 #include <linux/init.h>
221 #include <linux/miscdevice.h>
222 #include <linux/bitops.h>
225 #include <asm/uaccess.h>
227 /* The 3.0.0 version of sxboards/sxwindow.h uses BYTE and WORD.... */
231 /* .... but the 3.0.4 version uses _u8 and _u16. */
235 #include "sxboards.h"
236 #include "sxwindow.h"
238 #include <linux/generic_serial.h>
241 /* I don't think that this driver can handle more than 256 ports on
242 one machine. You'll have to increase the number of boards in sx.h
243 if you want more than 4 boards. */
245 #ifndef PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8
246 #define PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8 0x2000
249 /* Configurable options:
250 (Don't be too sure that it'll work if you toggle them) */
252 /* Am I paranoid or not ? ;-) */
253 #undef SX_PARANOIA_CHECK
255 /* 20 -> 2000 per second. The card should rate-limit interrupts at 100
256 Hz, but it is user configurable. I don't recommend going above 1000
257 Hz. The interrupt ratelimit might trigger if the interrupt is
258 shared with a very active other device. */
259 #define IRQ_RATE_LIMIT 20
261 /* Sharing interrupts is possible now. If the other device wants more
262 than 2000 interrupts per second, we'd gracefully decline further
263 interrupts. That's not what we want. On the other hand, if the
264 other device interrupts 2000 times a second, don't use the SX
265 interrupt. Use polling. */
266 #undef IRQ_RATE_LIMIT
269 /* Not implemented */
271 * The following defines are mostly for testing purposes. But if you need
272 * some nice reporting in your syslog, you can define them also.
274 #define SX_REPORT_FIFO
275 #define SX_REPORT_OVERRUN
278 /* Function prototypes */
279 static void sx_disable_tx_interrupts(void *ptr);
280 static void sx_enable_tx_interrupts(void *ptr);
281 static void sx_disable_rx_interrupts(void *ptr);
282 static void sx_enable_rx_interrupts(void *ptr);
283 static int sx_carrier_raised(struct tty_port *port);
284 static void sx_shutdown_port(void *ptr);
285 static int sx_set_real_termios(void *ptr);
286 static void sx_close(void *ptr);
287 static int sx_chars_in_buffer(void *ptr);
288 static int sx_init_board(struct sx_board *board);
289 static int sx_init_portstructs(int nboards, int nports);
290 static long sx_fw_ioctl(struct file *filp, unsigned int cmd,
292 static int sx_init_drivers(void);
294 static struct tty_driver *sx_driver;
296 static DEFINE_MUTEX(sx_boards_lock);
297 static struct sx_board boards[SX_NBOARDS];
298 static struct sx_port *sx_ports;
299 static int sx_initialized;
300 static int sx_nports;
303 /* You can have the driver poll your card.
304 - Set sx_poll to 1 to poll every timer tick (10ms on Intel).
305 This is used when the card cannot use an interrupt for some reason.
307 - set sx_slowpoll to 100 to do an extra poll once a second (on Intel). If
308 the driver misses an interrupt (report this if it DOES happen to you!)
309 everything will continue to work....
311 static int sx_poll = 1;
312 static int sx_slowpoll;
314 /* The card limits the number of interrupts per second.
315 At 115k2 "100" should be sufficient.
316 If you're using higher baudrates, you can increase this...
319 static int sx_maxints = 100;
323 /* These are the only open spaces in my computer. Yours may have more
325 duh: Card at 0xa0000 is possible on HP Netserver?? -- pvdl
327 static int sx_probe_addrs[] = {
328 0xc0000, 0xd0000, 0xe0000,
329 0xc8000, 0xd8000, 0xe8000
331 static int si_probe_addrs[] = {
332 0xc0000, 0xd0000, 0xe0000,
333 0xc8000, 0xd8000, 0xe8000, 0xa0000
335 static int si1_probe_addrs[] = {
339 #define NR_SX_ADDRS ARRAY_SIZE(sx_probe_addrs)
340 #define NR_SI_ADDRS ARRAY_SIZE(si_probe_addrs)
341 #define NR_SI1_ADDRS ARRAY_SIZE(si1_probe_addrs)
343 module_param_array(sx_probe_addrs, int, NULL, 0);
344 module_param_array(si_probe_addrs, int, NULL, 0);
347 /* Set the mask to all-ones. This alas, only supports 32 interrupts.
348 Some architectures may need more. */
349 static int sx_irqmask = -1;
351 module_param(sx_poll, int, 0);
352 module_param(sx_slowpoll, int, 0);
353 module_param(sx_maxints, int, 0);
354 module_param(sx_debug, int, 0);
355 module_param(sx_irqmask, int, 0);
357 MODULE_LICENSE("GPL");
359 static struct real_driver sx_real_driver = {
360 sx_disable_tx_interrupts,
361 sx_enable_tx_interrupts,
362 sx_disable_rx_interrupts,
363 sx_enable_rx_interrupts,
371 This driver can spew a whole lot of debugging output at you. If you
372 need maximum performance, you should disable the DEBUG define. To
373 aid in debugging in the field, I'm leaving the compile-time debug
374 features enabled, and disable them "runtime". That allows me to
375 instruct people with problems to enable debugging without requiring
381 #define sx_dprintk(f, str...) if (sx_debug & f) printk (str)
383 #define sx_dprintk(f, str...) /* nothing */
386 #define func_enter() sx_dprintk(SX_DEBUG_FLOW, "sx: enter %s\n",__func__)
387 #define func_exit() sx_dprintk(SX_DEBUG_FLOW, "sx: exit %s\n",__func__)
389 #define func_enter2() sx_dprintk(SX_DEBUG_FLOW, "sx: enter %s (port %d)\n", \
390 __func__, port->line)
393 * Firmware loader driver specific routines
397 static const struct file_operations sx_fw_fops = {
398 .owner = THIS_MODULE,
399 .unlocked_ioctl = sx_fw_ioctl,
400 .llseek = noop_llseek,
403 static struct miscdevice sx_fw_device = {
404 SXCTL_MISC_MINOR, "sxctl", &sx_fw_fops
407 #ifdef SX_PARANOIA_CHECK
409 /* This doesn't work. Who's paranoid around here? Not me! */
411 static inline int sx_paranoia_check(struct sx_port const *port,
412 char *name, const char *routine)
414 static const char *badmagic = KERN_ERR "sx: Warning: bad sx port magic "
415 "number for device %s in %s\n";
416 static const char *badinfo = KERN_ERR "sx: Warning: null sx port for "
420 printk(badinfo, name, routine);
423 if (port->magic != SX_MAGIC) {
424 printk(badmagic, name, routine);
431 #define sx_paranoia_check(a,b,c) 0
434 /* The timeouts. First try 30 times as fast as possible. Then give
435 the card some time to breathe between accesses. (Otherwise the
436 processor on the card might not be able to access its OWN bus... */
439 #define TIMEOUT_2 1000000
442 static void my_hd_io(void __iomem *p, int len)
445 unsigned char __iomem *addr = p;
447 for (i = 0; i < len; i += 16) {
448 printk("%p ", addr + i);
449 for (j = 0; j < 16; j++) {
450 printk("%02x %s", readb(addr + j + i),
451 (j == 7) ? " " : "");
453 for (j = 0; j < 16; j++) {
454 ch = readb(addr + j + i);
455 printk("%c", (ch < 0x20) ? '.' :
456 ((ch > 0x7f) ? '.' : ch));
461 static void my_hd(void *p, int len)
464 unsigned char *addr = p;
466 for (i = 0; i < len; i += 16) {
467 printk("%p ", addr + i);
468 for (j = 0; j < 16; j++) {
469 printk("%02x %s", addr[j + i], (j == 7) ? " " : "");
471 for (j = 0; j < 16; j++) {
473 printk("%c", (ch < 0x20) ? '.' :
474 ((ch > 0x7f) ? '.' : ch));
481 /* This needs redoing for Alpha -- REW -- Done. */
483 static inline void write_sx_byte(struct sx_board *board, int offset, u8 byte)
485 writeb(byte, board->base + offset);
488 static inline u8 read_sx_byte(struct sx_board *board, int offset)
490 return readb(board->base + offset);
493 static inline void write_sx_word(struct sx_board *board, int offset, u16 word)
495 writew(word, board->base + offset);
498 static inline u16 read_sx_word(struct sx_board *board, int offset)
500 return readw(board->base + offset);
503 static int sx_busy_wait_eq(struct sx_board *board,
504 int offset, int mask, int correctval)
510 for (i = 0; i < TIMEOUT_1; i++)
511 if ((read_sx_byte(board, offset) & mask) == correctval) {
516 for (i = 0; i < TIMEOUT_2; i++) {
517 if ((read_sx_byte(board, offset) & mask) == correctval) {
528 static int sx_busy_wait_neq(struct sx_board *board,
529 int offset, int mask, int badval)
535 for (i = 0; i < TIMEOUT_1; i++)
536 if ((read_sx_byte(board, offset) & mask) != badval) {
541 for (i = 0; i < TIMEOUT_2; i++) {
542 if ((read_sx_byte(board, offset) & mask) != badval) {
553 /* 5.6.4 of 6210028 r2.3 */
554 static int sx_reset(struct sx_board *board)
558 if (IS_SX_BOARD(board)) {
560 write_sx_byte(board, SX_CONFIG, 0);
561 write_sx_byte(board, SX_RESET, 1); /* Value doesn't matter */
563 if (!sx_busy_wait_eq(board, SX_RESET_STATUS, 1, 0)) {
564 printk(KERN_INFO "sx: Card doesn't respond to "
568 } else if (IS_EISA_BOARD(board)) {
569 outb(board->irq << 4, board->eisa_base + 0xc02);
570 } else if (IS_SI1_BOARD(board)) {
571 write_sx_byte(board, SI1_ISA_RESET, 0); /*value doesn't matter*/
573 /* Gory details of the SI/ISA board */
574 write_sx_byte(board, SI2_ISA_RESET, SI2_ISA_RESET_SET);
575 write_sx_byte(board, SI2_ISA_IRQ11, SI2_ISA_IRQ11_CLEAR);
576 write_sx_byte(board, SI2_ISA_IRQ12, SI2_ISA_IRQ12_CLEAR);
577 write_sx_byte(board, SI2_ISA_IRQ15, SI2_ISA_IRQ15_CLEAR);
578 write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_CLEAR);
579 write_sx_byte(board, SI2_ISA_IRQSET, SI2_ISA_IRQSET_CLEAR);
586 /* This doesn't work on machines where "NULL" isn't 0 */
587 /* If you have one of those, someone will need to write
588 the equivalent of this, which will amount to about 3 lines. I don't
589 want to complicate this right now. -- REW
590 (See, I do write comments every now and then :-) */
591 #define OFFSETOF(strct, elem) ((long)&(((struct strct *)NULL)->elem))
593 #define CHAN_OFFSET(port,elem) (port->ch_base + OFFSETOF (_SXCHANNEL, elem))
594 #define MODU_OFFSET(board,addr,elem) (addr + OFFSETOF (_SXMODULE, elem))
595 #define BRD_OFFSET(board,elem) (OFFSETOF (_SXCARD, elem))
597 #define sx_write_channel_byte(port, elem, val) \
598 write_sx_byte (port->board, CHAN_OFFSET (port, elem), val)
600 #define sx_read_channel_byte(port, elem) \
601 read_sx_byte (port->board, CHAN_OFFSET (port, elem))
603 #define sx_write_channel_word(port, elem, val) \
604 write_sx_word (port->board, CHAN_OFFSET (port, elem), val)
606 #define sx_read_channel_word(port, elem) \
607 read_sx_word (port->board, CHAN_OFFSET (port, elem))
609 #define sx_write_module_byte(board, addr, elem, val) \
610 write_sx_byte (board, MODU_OFFSET (board, addr, elem), val)
612 #define sx_read_module_byte(board, addr, elem) \
613 read_sx_byte (board, MODU_OFFSET (board, addr, elem))
615 #define sx_write_module_word(board, addr, elem, val) \
616 write_sx_word (board, MODU_OFFSET (board, addr, elem), val)
618 #define sx_read_module_word(board, addr, elem) \
619 read_sx_word (board, MODU_OFFSET (board, addr, elem))
621 #define sx_write_board_byte(board, elem, val) \
622 write_sx_byte (board, BRD_OFFSET (board, elem), val)
624 #define sx_read_board_byte(board, elem) \
625 read_sx_byte (board, BRD_OFFSET (board, elem))
627 #define sx_write_board_word(board, elem, val) \
628 write_sx_word (board, BRD_OFFSET (board, elem), val)
630 #define sx_read_board_word(board, elem) \
631 read_sx_word (board, BRD_OFFSET (board, elem))
633 static int sx_start_board(struct sx_board *board)
635 if (IS_SX_BOARD(board)) {
636 write_sx_byte(board, SX_CONFIG, SX_CONF_BUSEN);
637 } else if (IS_EISA_BOARD(board)) {
638 write_sx_byte(board, SI2_EISA_OFF, SI2_EISA_VAL);
639 outb((board->irq << 4) | 4, board->eisa_base + 0xc02);
640 } else if (IS_SI1_BOARD(board)) {
641 write_sx_byte(board, SI1_ISA_RESET_CLEAR, 0);
642 write_sx_byte(board, SI1_ISA_INTCL, 0);
644 /* Don't bug me about the clear_set.
645 I haven't the foggiest idea what it's about -- REW */
646 write_sx_byte(board, SI2_ISA_RESET, SI2_ISA_RESET_CLEAR);
647 write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
652 #define SX_IRQ_REG_VAL(board) \
653 ((board->flags & SX_ISA_BOARD) ? (board->irq << 4) : 0)
655 /* Note. The SX register is write-only. Therefore, we have to enable the
656 bus too. This is a no-op, if you don't mess with this driver... */
657 static int sx_start_interrupts(struct sx_board *board)
660 /* Don't call this with board->irq == 0 */
662 if (IS_SX_BOARD(board)) {
663 write_sx_byte(board, SX_CONFIG, SX_IRQ_REG_VAL(board) |
664 SX_CONF_BUSEN | SX_CONF_HOSTIRQ);
665 } else if (IS_EISA_BOARD(board)) {
666 inb(board->eisa_base + 0xc03);
667 } else if (IS_SI1_BOARD(board)) {
668 write_sx_byte(board, SI1_ISA_INTCL, 0);
669 write_sx_byte(board, SI1_ISA_INTCL_CLEAR, 0);
671 switch (board->irq) {
673 write_sx_byte(board, SI2_ISA_IRQ11, SI2_ISA_IRQ11_SET);
676 write_sx_byte(board, SI2_ISA_IRQ12, SI2_ISA_IRQ12_SET);
679 write_sx_byte(board, SI2_ISA_IRQ15, SI2_ISA_IRQ15_SET);
682 printk(KERN_INFO "sx: SI/XIO card doesn't support "
683 "interrupt %d.\n", board->irq);
686 write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
692 static int sx_send_command(struct sx_port *port,
693 int command, int mask, int newstat)
696 write_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat), command);
698 return sx_busy_wait_eq(port->board, CHAN_OFFSET(port, hi_hstat), mask,
702 static char *mod_type_s(int module_type)
704 switch (module_type) {
718 return "Unknown/invalid";
722 static char *pan_type_s(int pan_type)
726 return "MOD_RS232DB25";
728 return "MOD_RS232RJ45";
730 return "MOD_RS422DB25";
732 return "MOD_PARALLEL";
733 case MOD_2_RS232DB25:
734 return "MOD_2_RS232DB25";
735 case MOD_2_RS232RJ45:
736 return "MOD_2_RS232RJ45";
737 case MOD_2_RS422DB25:
738 return "MOD_2_RS422DB25";
739 case MOD_RS232DB25MALE:
740 return "MOD_RS232DB25MALE";
742 return "MOD_2_PARALLEL";
750 static int mod_compat_type(int module_type)
752 return module_type >> 4;
755 static void sx_reconfigure_port(struct sx_port *port)
757 if (sx_read_channel_byte(port, hi_hstat) == HS_IDLE_OPEN) {
758 if (sx_send_command(port, HS_CONFIG, -1, HS_IDLE_OPEN) != 1) {
759 printk(KERN_WARNING "sx: Sent reconfigure command, but "
760 "card didn't react.\n");
763 sx_dprintk(SX_DEBUG_TERMIOS, "sx: Not sending reconfigure: "
764 "port isn't open (%02x).\n",
765 sx_read_channel_byte(port, hi_hstat));
769 static void sx_setsignals(struct sx_port *port, int dtr, int rts)
774 t = sx_read_channel_byte(port, hi_op);
776 t = dtr ? (t | OP_DTR) : (t & ~OP_DTR);
778 t = rts ? (t | OP_RTS) : (t & ~OP_RTS);
779 sx_write_channel_byte(port, hi_op, t);
780 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "setsignals: %d/%d\n", dtr, rts);
785 static int sx_getsignals(struct sx_port *port)
789 o_stat = sx_read_channel_byte(port, hi_op);
790 i_stat = sx_read_channel_byte(port, hi_ip);
792 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "getsignals: %d/%d (%d/%d) "
794 (o_stat & OP_DTR) != 0, (o_stat & OP_RTS) != 0,
795 port->c_dcd, tty_port_carrier_raised(&port->gs.port),
796 sx_read_channel_byte(port, hi_ip),
797 sx_read_channel_byte(port, hi_state));
799 return (((o_stat & OP_DTR) ? TIOCM_DTR : 0) |
800 ((o_stat & OP_RTS) ? TIOCM_RTS : 0) |
801 ((i_stat & IP_CTS) ? TIOCM_CTS : 0) |
802 ((i_stat & IP_DCD) ? TIOCM_CAR : 0) |
803 ((i_stat & IP_DSR) ? TIOCM_DSR : 0) |
804 ((i_stat & IP_RI) ? TIOCM_RNG : 0));
807 static void sx_set_baud(struct sx_port *port)
811 if (port->board->ta_type == MOD_SXDC) {
812 switch (port->gs.baud) {
813 /* Save some typing work... */
814 #define e(x) case x: t = BAUD_ ## x; break
851 /* Can I return "invalid"? */
853 printk(KERN_INFO "sx: unsupported baud rate: %d.\n",
859 /* The baud rate is not set to 0, so we're enabeling DTR... -- REW */
860 sx_setsignals(port, 1, -1);
861 /* XXX This is not TA & MTA compatible */
862 sx_write_channel_byte(port, hi_csr, 0xff);
864 sx_write_channel_byte(port, hi_txbaud, t);
865 sx_write_channel_byte(port, hi_rxbaud, t);
867 sx_setsignals(port, 0, -1);
870 switch (port->gs.baud) {
871 #define e(x) case x: t = CSR_ ## x; break
884 /* TA supports 110, but not 115200, MTA supports 115200, but not 110 */
886 if (port->board->ta_type == MOD_TA) {
891 printk(KERN_INFO "sx: Unsupported baud rate: "
892 "%d.\n", port->gs.baud);
896 if (port->board->ta_type == MOD_TA) {
898 printk(KERN_INFO "sx: Unsupported baud rate: "
899 "%d.\n", port->gs.baud);
910 printk(KERN_INFO "sx: Unsupported baud rate: %d.\n",
916 sx_setsignals(port, 1, -1);
917 sx_write_channel_byte(port, hi_csr, t * 0x11);
919 sx_setsignals(port, 0, -1);
924 /* Simon Allen's version of this routine was 225 lines long. 85 is a lot
927 static int sx_set_real_termios(void *ptr)
929 struct sx_port *port = ptr;
933 if (!port->gs.port.tty)
936 /* What is this doing here? -- REW
937 Ha! figured it out. It is to allow you to get DTR active again
938 if you've dropped it with stty 0. Moved to set_baud, where it
939 belongs (next to the drop dtr if baud == 0) -- REW */
940 /* sx_setsignals (port, 1, -1); */
944 #define CFLAG port->gs.port.tty->termios->c_cflag
945 sx_write_channel_byte(port, hi_mr1,
946 (C_PARENB(port->gs.port.tty) ? MR1_WITH : MR1_NONE) |
947 (C_PARODD(port->gs.port.tty) ? MR1_ODD : MR1_EVEN) |
948 (C_CRTSCTS(port->gs.port.tty) ? MR1_RTS_RXFLOW : 0) |
949 (((CFLAG & CSIZE) == CS8) ? MR1_8_BITS : 0) |
950 (((CFLAG & CSIZE) == CS7) ? MR1_7_BITS : 0) |
951 (((CFLAG & CSIZE) == CS6) ? MR1_6_BITS : 0) |
952 (((CFLAG & CSIZE) == CS5) ? MR1_5_BITS : 0));
954 sx_write_channel_byte(port, hi_mr2,
955 (C_CRTSCTS(port->gs.port.tty) ? MR2_CTS_TXFLOW : 0) |
956 (C_CSTOPB(port->gs.port.tty) ? MR2_2_STOP :
959 switch (CFLAG & CSIZE) {
961 sx_write_channel_byte(port, hi_mask, 0xff);
964 sx_write_channel_byte(port, hi_mask, 0x7f);
967 sx_write_channel_byte(port, hi_mask, 0x3f);
970 sx_write_channel_byte(port, hi_mask, 0x1f);
973 printk(KERN_INFO "sx: Invalid wordsize: %u\n",
974 (unsigned int)CFLAG & CSIZE);
978 sx_write_channel_byte(port, hi_prtcl,
979 (I_IXON(port->gs.port.tty) ? SP_TXEN : 0) |
980 (I_IXOFF(port->gs.port.tty) ? SP_RXEN : 0) |
981 (I_IXANY(port->gs.port.tty) ? SP_TANY : 0) | SP_DCEN);
983 sx_write_channel_byte(port, hi_break,
984 (I_IGNBRK(port->gs.port.tty) ? BR_IGN : 0 |
985 I_BRKINT(port->gs.port.tty) ? BR_INT : 0));
987 sx_write_channel_byte(port, hi_txon, START_CHAR(port->gs.port.tty));
988 sx_write_channel_byte(port, hi_rxon, START_CHAR(port->gs.port.tty));
989 sx_write_channel_byte(port, hi_txoff, STOP_CHAR(port->gs.port.tty));
990 sx_write_channel_byte(port, hi_rxoff, STOP_CHAR(port->gs.port.tty));
992 sx_reconfigure_port(port);
994 /* Tell line discipline whether we will do input cooking */
995 if (I_OTHER(port->gs.port.tty)) {
996 clear_bit(TTY_HW_COOK_IN, &port->gs.port.tty->flags);
998 set_bit(TTY_HW_COOK_IN, &port->gs.port.tty->flags);
1000 sx_dprintk(SX_DEBUG_TERMIOS, "iflags: %x(%d) ",
1001 (unsigned int)port->gs.port.tty->termios->c_iflag,
1002 I_OTHER(port->gs.port.tty));
1004 /* Tell line discipline whether we will do output cooking.
1005 * If OPOST is set and no other output flags are set then we can do output
1006 * processing. Even if only *one* other flag in the O_OTHER group is set
1007 * we do cooking in software.
1009 if (O_OPOST(port->gs.port.tty) && !O_OTHER(port->gs.port.tty)) {
1010 set_bit(TTY_HW_COOK_OUT, &port->gs.port.tty->flags);
1012 clear_bit(TTY_HW_COOK_OUT, &port->gs.port.tty->flags);
1014 sx_dprintk(SX_DEBUG_TERMIOS, "oflags: %x(%d)\n",
1015 (unsigned int)port->gs.port.tty->termios->c_oflag,
1016 O_OTHER(port->gs.port.tty));
1017 /* port->c_dcd = sx_get_CD (port); */
1022 /* ********************************************************************** *
1023 * the interrupt related routines *
1024 * ********************************************************************** */
1027 Other drivers use the macro "MIN" to calculate how much to copy.
1028 This has the disadvantage that it will evaluate parts twice. That's
1029 expensive when it's IO (and the compiler cannot optimize those away!).
1030 Moreover, I'm not sure that you're race-free.
1032 I assign a value, and then only allow the value to decrease. This
1033 is always safe. This makes the code a few lines longer, and you
1034 know I'm dead against that, but I think it is required in this
1037 static void sx_transmit_chars(struct sx_port *port)
1044 sx_dprintk(SX_DEBUG_TRANSMIT, "Port %p: transmit %d chars\n",
1045 port, port->gs.xmit_cnt);
1047 if (test_and_set_bit(SX_PORT_TRANSMIT_LOCK, &port->locks)) {
1052 c = port->gs.xmit_cnt;
1054 sx_dprintk(SX_DEBUG_TRANSMIT, "Copying %d ", c);
1055 tx_ip = sx_read_channel_byte(port, hi_txipos);
1057 /* Took me 5 minutes to deduce this formula.
1058 Luckily it is literally in the manual in section 6.5.4.3.5 */
1059 txroom = (sx_read_channel_byte(port, hi_txopos) - tx_ip - 1) &
1062 /* Don't copy more bytes than there is room for in the buffer */
1065 sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%d) ", c, txroom);
1067 /* Don't copy past the end of the hardware transmit buffer */
1068 if (c > 0x100 - tx_ip)
1071 sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%d) ", c, 0x100 - tx_ip);
1073 /* Don't copy pas the end of the source buffer */
1074 if (c > SERIAL_XMIT_SIZE - port->gs.xmit_tail)
1075 c = SERIAL_XMIT_SIZE - port->gs.xmit_tail;
1077 sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%ld) \n",
1078 c, SERIAL_XMIT_SIZE - port->gs.xmit_tail);
1080 /* If for one reason or another, we can't copy more data, we're
1085 memcpy_toio(port->board->base + CHAN_OFFSET(port, hi_txbuf) +
1086 tx_ip, port->gs.xmit_buf + port->gs.xmit_tail, c);
1088 /* Update the pointer in the card */
1089 sx_write_channel_byte(port, hi_txipos, (tx_ip + c) & 0xff);
1091 /* Update the kernel buffer end */
1092 port->gs.xmit_tail = (port->gs.xmit_tail + c) &
1093 (SERIAL_XMIT_SIZE - 1);
1095 /* This one last. (this is essential)
1096 It would allow others to start putting more data into the
1098 port->gs.xmit_cnt -= c;
1101 if (port->gs.xmit_cnt == 0) {
1102 sx_disable_tx_interrupts(port);
1105 if ((port->gs.xmit_cnt <= port->gs.wakeup_chars) && port->gs.port.tty) {
1106 tty_wakeup(port->gs.port.tty);
1107 sx_dprintk(SX_DEBUG_TRANSMIT, "Waking up.... ldisc (%d)....\n",
1108 port->gs.wakeup_chars);
1111 clear_bit(SX_PORT_TRANSMIT_LOCK, &port->locks);
1115 /* Note the symmetry between receiving chars and transmitting them!
1116 Note: The kernel should have implemented both a receive buffer and
1117 a transmit buffer. */
1119 /* Inlined: Called only once. Remove the inline when you add another call */
1120 static inline void sx_receive_chars(struct sx_port *port)
1124 struct tty_struct *tty;
1129 tty = port->gs.port.tty;
1131 rx_op = sx_read_channel_byte(port, hi_rxopos);
1132 c = (sx_read_channel_byte(port, hi_rxipos) - rx_op) & 0xff;
1134 sx_dprintk(SX_DEBUG_RECEIVE, "rxop=%d, c = %d.\n", rx_op, c);
1136 /* Don't copy past the end of the hardware receive buffer */
1137 if (rx_op + c > 0x100)
1140 sx_dprintk(SX_DEBUG_RECEIVE, "c = %d.\n", c);
1142 /* Don't copy more bytes than there is room for in the buffer */
1144 c = tty_prepare_flip_string(tty, &rp, c);
1146 sx_dprintk(SX_DEBUG_RECEIVE, "c = %d.\n", c);
1148 /* If for one reason or another, we can't copy more data, we're done! */
1152 sx_dprintk(SX_DEBUG_RECEIVE, "Copying over %d chars. First is "
1153 "%d at %lx\n", c, read_sx_byte(port->board,
1154 CHAN_OFFSET(port, hi_rxbuf) + rx_op),
1155 CHAN_OFFSET(port, hi_rxbuf));
1156 memcpy_fromio(rp, port->board->base +
1157 CHAN_OFFSET(port, hi_rxbuf) + rx_op, c);
1159 /* This one last. ( Not essential.)
1160 It allows the card to start putting more data into the
1162 Update the pointer in the card */
1163 sx_write_channel_byte(port, hi_rxopos, (rx_op + c) & 0xff);
1170 do_gettimeofday(&tv);
1171 sx_dprintk(SX_DEBUG_RECEIVE, "pushing flipq port %d (%3d "
1172 "chars): %d.%06d (%d/%d)\n", port->line,
1173 copied, (int)(tv.tv_sec % 60), (int)tv.tv_usec,
1174 tty->raw, tty->real_raw);
1176 /* Tell the rest of the system the news. Great news. New
1178 tty_flip_buffer_push(tty);
1179 /* tty_schedule_flip (tty); */
1185 /* Inlined: it is called only once. Remove the inline if you add another
1187 static inline void sx_check_modem_signals(struct sx_port *port)
1192 hi_state = sx_read_channel_byte(port, hi_state);
1193 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "Checking modem signals (%d/%d)\n",
1194 port->c_dcd, tty_port_carrier_raised(&port->gs.port));
1196 if (hi_state & ST_BREAK) {
1197 hi_state &= ~ST_BREAK;
1198 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "got a break.\n");
1199 sx_write_channel_byte(port, hi_state, hi_state);
1200 gs_got_break(&port->gs);
1202 if (hi_state & ST_DCD) {
1203 hi_state &= ~ST_DCD;
1204 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "got a DCD change.\n");
1205 sx_write_channel_byte(port, hi_state, hi_state);
1206 c_dcd = tty_port_carrier_raised(&port->gs.port);
1207 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD is now %d\n", c_dcd);
1208 if (c_dcd != port->c_dcd) {
1209 port->c_dcd = c_dcd;
1210 if (tty_port_carrier_raised(&port->gs.port)) {
1212 if ((sx_read_channel_byte(port, hi_hstat) !=
1214 !(port->gs.port.tty->termios->
1215 c_cflag & CLOCAL)) {
1216 /* Are we blocking in open? */
1217 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
1218 "active, unblocking open\n");
1219 wake_up_interruptible(&port->gs.port.
1222 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
1223 "raised. Ignoring.\n");
1226 /* DCD went down! */
1227 if (!(port->gs.port.tty->termios->c_cflag & CLOCAL)){
1228 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
1229 "dropped. hanging up....\n");
1230 tty_hangup(port->gs.port.tty);
1232 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
1233 "dropped. ignoring.\n");
1237 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "Hmmm. card told us "
1238 "DCD changed, but it didn't.\n");
1243 /* This is what an interrupt routine should look like.
1244 * Small, elegant, clear.
1247 static irqreturn_t sx_interrupt(int irq, void *ptr)
1249 struct sx_board *board = ptr;
1250 struct sx_port *port;
1254 sx_dprintk(SX_DEBUG_FLOW, "sx: enter sx_interrupt (%d/%d)\n", irq,
1257 /* AAargh! The order in which to do these things is essential and
1260 - Rate limit goes before "recursive". Otherwise a series of
1261 recursive calls will hang the machine in the interrupt routine.
1263 - hardware twiddling goes before "recursive". Otherwise when we
1264 poll the card, and a recursive interrupt happens, we won't
1265 ack the card, so it might keep on interrupting us. (especially
1266 level sensitive interrupt systems like PCI).
1268 - Rate limit goes before hardware twiddling. Otherwise we won't
1269 catch a card that has gone bonkers.
1271 - The "initialized" test goes after the hardware twiddling. Otherwise
1272 the card will stick us in the interrupt routine again.
1274 - The initialized test goes before recursive.
1277 #ifdef IRQ_RATE_LIMIT
1278 /* Aaargh! I'm ashamed. This costs more lines-of-code than the
1279 actual interrupt routine!. (Well, used to when I wrote that
1283 static int nintr = 0;
1285 if (lastjif == jiffies) {
1286 if (++nintr > IRQ_RATE_LIMIT) {
1287 free_irq(board->irq, board);
1288 printk(KERN_ERR "sx: Too many interrupts. "
1289 "Turning off interrupt %d.\n",
1299 if (board->irq == irq) {
1300 /* Tell the card we've noticed the interrupt. */
1302 sx_write_board_word(board, cc_int_pending, 0);
1303 if (IS_SX_BOARD(board)) {
1304 write_sx_byte(board, SX_RESET_IRQ, 1);
1305 } else if (IS_EISA_BOARD(board)) {
1306 inb(board->eisa_base + 0xc03);
1307 write_sx_word(board, 8, 0);
1309 write_sx_byte(board, SI2_ISA_INTCLEAR,
1310 SI2_ISA_INTCLEAR_CLEAR);
1311 write_sx_byte(board, SI2_ISA_INTCLEAR,
1312 SI2_ISA_INTCLEAR_SET);
1316 if (!sx_initialized)
1318 if (!(board->flags & SX_BOARD_INITIALIZED))
1321 if (test_and_set_bit(SX_BOARD_INTR_LOCK, &board->locks)) {
1322 printk(KERN_ERR "Recursive interrupt! (%d)\n", board->irq);
1326 for (i = 0; i < board->nports; i++) {
1327 port = &board->ports[i];
1328 if (port->gs.port.flags & GS_ACTIVE) {
1329 if (sx_read_channel_byte(port, hi_state)) {
1330 sx_dprintk(SX_DEBUG_INTERRUPTS, "Port %d: "
1331 "modem signal change?... \n",i);
1332 sx_check_modem_signals(port);
1334 if (port->gs.xmit_cnt) {
1335 sx_transmit_chars(port);
1337 if (!(port->gs.port.flags & SX_RX_THROTTLE)) {
1338 sx_receive_chars(port);
1343 clear_bit(SX_BOARD_INTR_LOCK, &board->locks);
1345 sx_dprintk(SX_DEBUG_FLOW, "sx: exit sx_interrupt (%d/%d)\n", irq,
1351 static void sx_pollfunc(unsigned long data)
1353 struct sx_board *board = (struct sx_board *)data;
1357 sx_interrupt(0, board);
1359 mod_timer(&board->timer, jiffies + sx_poll);
1363 /* ********************************************************************** *
1364 * Here are the routines that actually *
1365 * interface with the generic_serial driver *
1366 * ********************************************************************** */
1368 /* Ehhm. I don't know how to fiddle with interrupts on the SX card. --REW */
1369 /* Hmm. Ok I figured it out. You don't. */
1371 static void sx_disable_tx_interrupts(void *ptr)
1373 struct sx_port *port = ptr;
1376 port->gs.port.flags &= ~GS_TX_INTEN;
1381 static void sx_enable_tx_interrupts(void *ptr)
1383 struct sx_port *port = ptr;
1387 /* First transmit the characters that we're supposed to */
1388 sx_transmit_chars(port);
1390 /* The sx card will never interrupt us if we don't fill the buffer
1391 past 25%. So we keep considering interrupts off if that's the case. */
1392 data_in_buffer = (sx_read_channel_byte(port, hi_txipos) -
1393 sx_read_channel_byte(port, hi_txopos)) & 0xff;
1395 /* XXX Must be "HIGH_WATER" for SI card according to doc. */
1396 if (data_in_buffer < LOW_WATER)
1397 port->gs.port.flags &= ~GS_TX_INTEN;
1402 static void sx_disable_rx_interrupts(void *ptr)
1404 /* struct sx_port *port = ptr; */
1410 static void sx_enable_rx_interrupts(void *ptr)
1412 /* struct sx_port *port = ptr; */
1418 /* Jeez. Isn't this simple? */
1419 static int sx_carrier_raised(struct tty_port *port)
1421 struct sx_port *sp = container_of(port, struct sx_port, gs.port);
1422 return ((sx_read_channel_byte(sp, hi_ip) & IP_DCD) != 0);
1425 /* Jeez. Isn't this simple? */
1426 static int sx_chars_in_buffer(void *ptr)
1428 struct sx_port *port = ptr;
1432 return ((sx_read_channel_byte(port, hi_txipos) -
1433 sx_read_channel_byte(port, hi_txopos)) & 0xff);
1436 static void sx_shutdown_port(void *ptr)
1438 struct sx_port *port = ptr;
1442 port->gs.port.flags &= ~GS_ACTIVE;
1443 if (port->gs.port.tty && (port->gs.port.tty->termios->c_cflag & HUPCL)) {
1444 sx_setsignals(port, 0, 0);
1445 sx_reconfigure_port(port);
1451 /* ********************************************************************** *
1452 * Here are the routines that actually *
1453 * interface with the rest of the system *
1454 * ********************************************************************** */
1456 static int sx_open(struct tty_struct *tty, struct file *filp)
1458 struct sx_port *port;
1460 unsigned long flags;
1464 if (!sx_initialized) {
1469 sx_dprintk(SX_DEBUG_OPEN, "%d: opening line %d. tty=%p ctty=%p, "
1470 "np=%d)\n", task_pid_nr(current), line, tty,
1471 current->signal->tty, sx_nports);
1473 if ((line < 0) || (line >= SX_NPORTS) || (line >= sx_nports))
1476 port = &sx_ports[line];
1477 port->c_dcd = 0; /* Make sure that the first interrupt doesn't detect a
1478 1 -> 0 transition. */
1480 sx_dprintk(SX_DEBUG_OPEN, "port = %p c_dcd = %d\n", port, port->c_dcd);
1482 spin_lock_irqsave(&port->gs.driver_lock, flags);
1484 tty->driver_data = port;
1485 port->gs.port.tty = tty;
1486 port->gs.port.count++;
1487 spin_unlock_irqrestore(&port->gs.driver_lock, flags);
1489 sx_dprintk(SX_DEBUG_OPEN, "starting port\n");
1492 * Start up serial port
1494 retval = gs_init_port(&port->gs);
1495 sx_dprintk(SX_DEBUG_OPEN, "done gs_init\n");
1497 port->gs.port.count--;
1501 port->gs.port.flags |= GS_ACTIVE;
1502 if (port->gs.port.count <= 1)
1503 sx_setsignals(port, 1, 1);
1506 if (sx_debug & SX_DEBUG_OPEN)
1507 my_hd(port, sizeof(*port));
1509 if (sx_debug & SX_DEBUG_OPEN)
1510 my_hd_io(port->board->base + port->ch_base, sizeof(*port));
1513 if (port->gs.port.count <= 1) {
1514 if (sx_send_command(port, HS_LOPEN, -1, HS_IDLE_OPEN) != 1) {
1515 printk(KERN_ERR "sx: Card didn't respond to LOPEN "
1517 spin_lock_irqsave(&port->gs.driver_lock, flags);
1518 port->gs.port.count--;
1519 spin_unlock_irqrestore(&port->gs.driver_lock, flags);
1524 retval = gs_block_til_ready(port, filp);
1525 sx_dprintk(SX_DEBUG_OPEN, "Block til ready returned %d. Count=%d\n",
1526 retval, port->gs.port.count);
1530 * Don't lower gs.port.count here because sx_close() will be called later
1535 /* tty->low_latency = 1; */
1537 port->c_dcd = sx_carrier_raised(&port->gs.port);
1538 sx_dprintk(SX_DEBUG_OPEN, "at open: cd=%d\n", port->c_dcd);
1545 static void sx_close(void *ptr)
1547 struct sx_port *port = ptr;
1548 /* Give the port 5 seconds to close down. */
1553 sx_setsignals(port, 0, 0);
1554 sx_reconfigure_port(port);
1555 sx_send_command(port, HS_CLOSE, 0, 0);
1557 while (to-- && (sx_read_channel_byte(port, hi_hstat) != HS_IDLE_CLOSED))
1558 if (msleep_interruptible(10))
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)
1563 printk(KERN_ERR "sx: sent the force_close command, but "
1564 "card didn't react\n");
1566 sx_dprintk(SX_DEBUG_CLOSE, "sent the force_close "
1570 sx_dprintk(SX_DEBUG_CLOSE, "waited %d jiffies for close. count=%d\n",
1571 5 * HZ - to - 1, port->gs.port.count);
1573 if (port->gs.port.count) {
1574 sx_dprintk(SX_DEBUG_CLOSE, "WARNING port count:%d\n",
1575 port->gs.port.count);
1576 /*printk("%s SETTING port count to zero: %p count: %d\n",
1577 __func__, port, port->gs.port.count);
1578 port->gs.port.count = 0;*/
1584 /* This is relatively thorough. But then again it is only 20 lines. */
1585 #define MARCHUP for (i = min; i < max; i++)
1586 #define MARCHDOWN for (i = max - 1; i >= min; i--)
1587 #define W0 write_sx_byte(board, i, 0x55)
1588 #define W1 write_sx_byte(board, i, 0xaa)
1589 #define R0 if (read_sx_byte(board, i) != 0x55) return 1
1590 #define R1 if (read_sx_byte(board, i) != 0xaa) return 1
1592 /* This memtest takes a human-noticable time. You normally only do it
1593 once a boot, so I guess that it is worth it. */
1594 static int do_memtest(struct sx_board *board, int min, int max)
1598 /* This is a marchb. Theoretically, marchb catches much more than
1599 simpler tests. In practise, the longer test just catches more
1600 intermittent errors. -- REW
1601 (For the theory behind memory testing see:
1602 Testing Semiconductor Memories by A.J. van de Goor.) */
1641 #define MARCHUP for (i = min; i < max; i += 2)
1642 #define MARCHDOWN for (i = max - 1; i >= min; i -= 2)
1643 #define W0 write_sx_word(board, i, 0x55aa)
1644 #define W1 write_sx_word(board, i, 0xaa55)
1645 #define R0 if (read_sx_word(board, i) != 0x55aa) return 1
1646 #define R1 if (read_sx_word(board, i) != 0xaa55) return 1
1649 /* This memtest takes a human-noticable time. You normally only do it
1650 once a boot, so I guess that it is worth it. */
1651 static int do_memtest_w(struct sx_board *board, int min, int max)
1687 static long sx_fw_ioctl(struct file *filp, unsigned int cmd,
1691 int __user *descr = (int __user *)arg;
1693 static struct sx_board *board = NULL;
1700 if (!capable(CAP_SYS_RAWIO))
1705 sx_dprintk(SX_DEBUG_FIRMWARE, "IOCTL %x: %lx\n", cmd, arg);
1709 if (board->flags & SX_BOARD_PRESENT) {
1710 sx_dprintk(SX_DEBUG_FIRMWARE, "Board present! (%x)\n",
1713 sx_dprintk(SX_DEBUG_FIRMWARE, "Board not present! (%x) all:",
1715 for (i = 0; i < SX_NBOARDS; i++)
1716 sx_dprintk(SX_DEBUG_FIRMWARE, "<%x> ", boards[i].flags);
1717 sx_dprintk(SX_DEBUG_FIRMWARE, "\n");
1723 case SXIO_SET_BOARD:
1724 sx_dprintk(SX_DEBUG_FIRMWARE, "set board to %ld\n", arg);
1726 if (arg >= SX_NBOARDS)
1728 sx_dprintk(SX_DEBUG_FIRMWARE, "not out of range\n");
1729 if (!(boards[arg].flags & SX_BOARD_PRESENT))
1731 sx_dprintk(SX_DEBUG_FIRMWARE, ".. and present!\n");
1732 board = &boards[arg];
1734 /* FIXME: And this does ... nothing?? */
1737 rc = -ENOENT; /* If we manage to miss one, return error. */
1738 if (IS_SX_BOARD(board))
1740 if (IS_CF_BOARD(board))
1742 if (IS_SI_BOARD(board))
1744 if (IS_SI1_BOARD(board))
1746 if (IS_EISA_BOARD(board))
1748 sx_dprintk(SX_DEBUG_FIRMWARE, "returning type= %ld\n", rc);
1750 case SXIO_DO_RAMTEST:
1751 if (sx_initialized) { /* Already initialized: better not ramtest the board. */
1755 if (IS_SX_BOARD(board)) {
1756 rc = do_memtest(board, 0, 0x7000);
1758 rc = do_memtest(board, 0, 0x7000);
1759 /*if (!rc) rc = do_memtest_w (board, 0, 0x7000); */
1761 rc = do_memtest(board, 0, 0x7ff8);
1762 /* if (!rc) rc = do_memtest_w (board, 0, 0x7ff8); */
1764 sx_dprintk(SX_DEBUG_FIRMWARE,
1765 "returning memtest result= %ld\n", rc);
1768 if (sx_initialized) {/* Already initialized */
1772 if (!sx_reset(board)) {
1776 sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
1778 tmp = kmalloc(SX_CHUNK_SIZE, GFP_USER);
1783 /* FIXME: check returns */
1784 get_user(nbytes, descr++);
1785 get_user(offset, descr++);
1786 get_user(data, descr++);
1787 while (nbytes && data) {
1788 for (i = 0; i < nbytes; i += SX_CHUNK_SIZE) {
1789 if (copy_from_user(tmp, (char __user *)data + i,
1790 (i + SX_CHUNK_SIZE > nbytes) ?
1791 nbytes - i : SX_CHUNK_SIZE)) {
1796 memcpy_toio(board->base2 + offset + i, tmp,
1797 (i + SX_CHUNK_SIZE > nbytes) ?
1798 nbytes - i : SX_CHUNK_SIZE);
1801 get_user(nbytes, descr++);
1802 get_user(offset, descr++);
1803 get_user(data, descr++);
1806 sx_nports += sx_init_board(board);
1810 if (sx_initialized) { /* Already initialized */
1814 /* This is not allowed until all boards are initialized... */
1815 for (i = 0; i < SX_NBOARDS; i++) {
1816 if ((boards[i].flags & SX_BOARD_PRESENT) &&
1817 !(boards[i].flags & SX_BOARD_INITIALIZED)) {
1822 for (i = 0; i < SX_NBOARDS; i++)
1823 if (!(boards[i].flags & SX_BOARD_PRESENT))
1826 sx_dprintk(SX_DEBUG_FIRMWARE, "initing portstructs, %d boards, "
1827 "%d channels, first board: %d ports\n",
1828 i, sx_nports, boards[0].nports);
1829 rc = sx_init_portstructs(i, sx_nports);
1840 case SXIO_GETGSDEBUG:
1841 case SXIO_SETGSDEBUG:
1844 case SXIO_GETNPORTS:
1857 static int sx_break(struct tty_struct *tty, int flag)
1859 struct sx_port *port = tty->driver_data;
1866 rv = sx_send_command(port, HS_START, -1, HS_IDLE_BREAK);
1868 rv = sx_send_command(port, HS_STOP, -1, HS_IDLE_OPEN);
1870 printk(KERN_ERR "sx: couldn't send break (%x).\n",
1871 read_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat)));
1877 static int sx_tiocmget(struct tty_struct *tty, struct file *file)
1879 struct sx_port *port = tty->driver_data;
1880 return sx_getsignals(port);
1883 static int sx_tiocmset(struct tty_struct *tty, struct file *file,
1884 unsigned int set, unsigned int clear)
1886 struct sx_port *port = tty->driver_data;
1887 int rts = -1, dtr = -1;
1889 if (set & TIOCM_RTS)
1891 if (set & TIOCM_DTR)
1893 if (clear & TIOCM_RTS)
1895 if (clear & TIOCM_DTR)
1898 sx_setsignals(port, dtr, rts);
1899 sx_reconfigure_port(port);
1903 static int sx_ioctl(struct tty_struct *tty, struct file *filp,
1904 unsigned int cmd, unsigned long arg)
1907 struct sx_port *port = tty->driver_data;
1908 void __user *argp = (void __user *)arg;
1910 /* func_enter2(); */
1916 rc = gs_getserial(&port->gs, argp);
1919 rc = gs_setserial(&port->gs, argp);
1931 /* The throttle/unthrottle scheme for the Specialix card is different
1932 * from other drivers and deserves some explanation.
1933 * The Specialix hardware takes care of XON/XOFF
1934 * and CTS/RTS flow control itself. This means that all we have to
1935 * do when signalled by the upper tty layer to throttle/unthrottle is
1936 * to make a note of it here. When we come to read characters from the
1937 * rx buffers on the card (sx_receive_chars()) we look to see if the
1938 * upper layer can accept more (as noted here in sx_rx_throt[]).
1939 * If it can't we simply don't remove chars from the cards buffer.
1940 * When the tty layer can accept chars, we again note that here and when
1941 * sx_receive_chars() is called it will remove them from the cards buffer.
1942 * The card will notice that a ports buffer has drained below some low
1943 * water mark and will unflow control the line itself, using whatever
1944 * flow control scheme is in use for that port. -- Simon Allen
1947 static void sx_throttle(struct tty_struct *tty)
1949 struct sx_port *port = tty->driver_data;
1952 /* If the port is using any type of input flow
1953 * control then throttle the port.
1955 if ((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty))) {
1956 port->gs.port.flags |= SX_RX_THROTTLE;
1961 static void sx_unthrottle(struct tty_struct *tty)
1963 struct sx_port *port = tty->driver_data;
1966 /* Always unthrottle even if flow control is not enabled on
1967 * this port in case we disabled flow control while the port
1970 port->gs.port.flags &= ~SX_RX_THROTTLE;
1975 /* ********************************************************************** *
1976 * Here are the initialization routines. *
1977 * ********************************************************************** */
1979 static int sx_init_board(struct sx_board *board)
1987 /* This is preceded by downloading the download code. */
1989 board->flags |= SX_BOARD_INITIALIZED;
1991 if (read_sx_byte(board, 0))
1992 /* CF boards may need this. */
1993 write_sx_byte(board, 0, 0);
1995 /* This resets the processor again, to make sure it didn't do any
1996 foolish things while we were downloading the image */
1997 if (!sx_reset(board))
2000 sx_start_board(board);
2002 if (!sx_busy_wait_neq(board, 0, 0xff, 0)) {
2003 printk(KERN_ERR "sx: Ooops. Board won't initialize.\n");
2007 /* Ok. So now the processor on the card is running. It gathered
2008 some info for us... */
2009 sx_dprintk(SX_DEBUG_INIT, "The sxcard structure:\n");
2010 if (sx_debug & SX_DEBUG_INIT)
2011 my_hd_io(board->base, 0x10);
2012 sx_dprintk(SX_DEBUG_INIT, "the first sx_module structure:\n");
2013 if (sx_debug & SX_DEBUG_INIT)
2014 my_hd_io(board->base + 0x80, 0x30);
2016 sx_dprintk(SX_DEBUG_INIT, "init_status: %x, %dk memory, firmware "
2018 read_sx_byte(board, 0), read_sx_byte(board, 1),
2019 read_sx_byte(board, 5), read_sx_byte(board, 4));
2021 if (read_sx_byte(board, 0) == 0xff) {
2022 printk(KERN_INFO "sx: No modules found. Sorry.\n");
2029 if (IS_SX_BOARD(board)) {
2030 sx_write_board_word(board, cc_int_count, sx_maxints);
2033 sx_write_board_word(board, cc_int_count,
2034 SI_PROCESSOR_CLOCK / 8 / sx_maxints);
2037 /* grab the first module type... */
2038 /* board->ta_type = mod_compat_type (read_sx_byte (board, 0x80 + 0x08)); */
2039 board->ta_type = mod_compat_type(sx_read_module_byte(board, 0x80,
2043 for (addr = 0x80; addr != 0; addr = read_sx_word(board, addr) & 0x7fff){
2044 type = sx_read_module_byte(board, addr, mc_chip);
2045 sx_dprintk(SX_DEBUG_INIT, "Module at %x: %d channels\n",
2046 addr, read_sx_byte(board, addr + 2));
2048 chans += sx_read_module_byte(board, addr, mc_type);
2050 sx_dprintk(SX_DEBUG_INIT, "module is an %s, which has %s/%s "
2053 pan_type_s(sx_read_module_byte(board, addr,
2055 pan_type_s(sx_read_module_byte(board, addr,
2058 sx_dprintk(SX_DEBUG_INIT, "CD1400 versions: %x/%x, ASIC "
2060 sx_read_module_byte(board, addr, mc_rev1),
2061 sx_read_module_byte(board, addr, mc_rev2),
2062 sx_read_module_byte(board, addr, mc_mtaasic_rev));
2064 /* The following combinations are illegal: It should theoretically
2065 work, but timing problems make the bus HANG. */
2067 if (mod_compat_type(type) != board->ta_type) {
2068 printk(KERN_ERR "sx: This is an invalid "
2069 "configuration.\nDon't mix TA/MTA/SXDC on the "
2070 "same hostadapter.\n");
2074 if ((IS_EISA_BOARD(board) ||
2075 IS_SI_BOARD(board)) &&
2076 (mod_compat_type(type) == 4)) {
2077 printk(KERN_ERR "sx: This is an invalid "
2078 "configuration.\nDon't use SXDCs on an SI/XIO "
2083 #if 0 /* Problem fixed: firmware 3.05 */
2084 if (IS_SX_BOARD(board) && (type == TA8)) {
2085 /* There are some issues with the firmware and the DCD/RTS
2086 lines. It might work if you tie them together or something.
2087 It might also work if you get a newer sx_firmware. Therefore
2088 this is just a warning. */
2090 "sx: The SX host doesn't work too well "
2091 "with the TA8 adapters.\nSpecialix is working on it.\n");
2097 if (board->irq > 0) {
2098 /* fixed irq, probably PCI */
2099 if (sx_irqmask & (1 << board->irq)) { /* may we use this irq? */
2100 if (request_irq(board->irq, sx_interrupt,
2101 IRQF_SHARED | IRQF_DISABLED,
2103 printk(KERN_ERR "sx: Cannot allocate "
2104 "irq %d.\n", board->irq);
2109 } else if (board->irq < 0 && sx_irqmask) {
2110 /* auto-allocate irq */
2112 int irqmask = sx_irqmask & (IS_SX_BOARD(board) ?
2113 SX_ISA_IRQ_MASK : SI2_ISA_IRQ_MASK);
2114 for (irqnr = 15; irqnr > 0; irqnr--)
2115 if (irqmask & (1 << irqnr))
2116 if (!request_irq(irqnr, sx_interrupt,
2117 IRQF_SHARED | IRQF_DISABLED,
2121 printk(KERN_ERR "sx: Cannot allocate IRQ.\n");
2127 /* Found a valid interrupt, start up interrupts! */
2128 sx_dprintk(SX_DEBUG_INIT, "Using irq %d.\n",
2130 sx_start_interrupts(board);
2131 board->poll = sx_slowpoll;
2132 board->flags |= SX_IRQ_ALLOCATED;
2134 /* no irq: setup board for polled operation */
2135 board->poll = sx_poll;
2136 sx_dprintk(SX_DEBUG_INIT, "Using poll-interval %d.\n",
2140 /* The timer should be initialized anyway: That way we can
2141 safely del_timer it when the module is unloaded. */
2142 setup_timer(&board->timer, sx_pollfunc, (unsigned long)board);
2145 mod_timer(&board->timer, jiffies + board->poll);
2150 board->nports = chans;
2151 sx_dprintk(SX_DEBUG_INIT, "returning %d ports.", board->nports);
2157 static void __devinit printheader(void)
2159 static int header_printed;
2161 if (!header_printed) {
2162 printk(KERN_INFO "Specialix SX driver "
2163 "(C) 1998/1999 R.E.Wolff@BitWizard.nl\n");
2164 printk(KERN_INFO "sx: version " __stringify(SX_VERSION) "\n");
2169 static int __devinit probe_sx(struct sx_board *board)
2171 struct vpd_prom vpdp;
2177 if (!IS_CF_BOARD(board)) {
2178 sx_dprintk(SX_DEBUG_PROBE, "Going to verify vpd prom at %p.\n",
2179 board->base + SX_VPD_ROM);
2181 if (sx_debug & SX_DEBUG_PROBE)
2182 my_hd_io(board->base + SX_VPD_ROM, 0x40);
2185 for (i = 0; i < sizeof(struct vpd_prom); i++)
2186 *p++ = read_sx_byte(board, SX_VPD_ROM + i * 2);
2188 if (sx_debug & SX_DEBUG_PROBE)
2191 sx_dprintk(SX_DEBUG_PROBE, "checking identifier...\n");
2193 if (strncmp(vpdp.identifier, SX_VPD_IDENT_STRING, 16) != 0) {
2194 sx_dprintk(SX_DEBUG_PROBE, "Got non-SX identifier: "
2195 "'%s'\n", vpdp.identifier);
2202 if (!IS_CF_BOARD(board)) {
2203 printk(KERN_DEBUG "sx: Found an SX board at %lx\n",
2205 printk(KERN_DEBUG "sx: hw_rev: %d, assembly level: %d, "
2207 vpdp.hwrev, vpdp.hwass, vpdp.uniqid);
2208 printk("Manufactured: %d/%d\n", 1970 + vpdp.myear, vpdp.mweek);
2210 if ((((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) !=
2211 SX_PCI_UNIQUEID1) && (((vpdp.uniqid >> 24) &
2212 SX_UNIQUEID_MASK) != SX_ISA_UNIQUEID1)) {
2213 /* This might be a bit harsh. This was the primary
2214 reason the SX/ISA card didn't work at first... */
2215 printk(KERN_ERR "sx: Hmm. Not an SX/PCI or SX/ISA "
2216 "card. Sorry: giving up.\n");
2220 if (((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) ==
2222 if (((unsigned long)board->hw_base) & 0x8000) {
2223 printk(KERN_WARNING "sx: Warning: There may be "
2224 "hardware problems with the card at "
2225 "%lx.\n", board->hw_base);
2226 printk(KERN_WARNING "sx: Read sx.txt for more "
2234 /* This resets the processor, and keeps it off the bus. */
2235 if (!sx_reset(board))
2237 sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
2243 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
2245 /* Specialix probes for this card at 32k increments from 640k to 16M.
2246 I consider machines with less than 16M unlikely nowadays, so I'm
2247 not probing above 1Mb. Also, 0xa0000, 0xb0000, are taken by the VGA
2248 card. 0xe0000 and 0xf0000 are taken by the BIOS. That only leaves
2249 0xc0000, 0xc8000, 0xd0000 and 0xd8000 . */
2251 static int __devinit probe_si(struct sx_board *board)
2256 sx_dprintk(SX_DEBUG_PROBE, "Going to verify SI signature hw %lx at "
2257 "%p.\n", board->hw_base, board->base + SI2_ISA_ID_BASE);
2259 if (sx_debug & SX_DEBUG_PROBE)
2260 my_hd_io(board->base + SI2_ISA_ID_BASE, 0x8);
2262 if (!IS_EISA_BOARD(board)) {
2263 if (IS_SI1_BOARD(board)) {
2264 for (i = 0; i < 8; i++) {
2265 write_sx_byte(board, SI2_ISA_ID_BASE + 7 - i,i);
2268 for (i = 0; i < 8; i++) {
2269 if ((read_sx_byte(board, SI2_ISA_ID_BASE + 7 - i) & 7)
2277 /* Now we're pretty much convinced that there is an SI board here,
2278 but to prevent trouble, we'd better double check that we don't
2279 have an SI1 board when we're probing for an SI2 board.... */
2281 write_sx_byte(board, SI2_ISA_ID_BASE, 0x10);
2282 if (IS_SI1_BOARD(board)) {
2283 /* This should be an SI1 board, which has this
2284 location writable... */
2285 if (read_sx_byte(board, SI2_ISA_ID_BASE) != 0x10) {
2290 /* This should be an SI2 board, which has the bottom
2291 3 bits non-writable... */
2292 if (read_sx_byte(board, SI2_ISA_ID_BASE) == 0x10) {
2298 /* Now we're pretty much convinced that there is an SI board here,
2299 but to prevent trouble, we'd better double check that we don't
2300 have an SI1 board when we're probing for an SI2 board.... */
2302 write_sx_byte(board, SI2_ISA_ID_BASE, 0x10);
2303 if (IS_SI1_BOARD(board)) {
2304 /* This should be an SI1 board, which has this
2305 location writable... */
2306 if (read_sx_byte(board, SI2_ISA_ID_BASE) != 0x10) {
2311 /* This should be an SI2 board, which has the bottom
2312 3 bits non-writable... */
2313 if (read_sx_byte(board, SI2_ISA_ID_BASE) == 0x10) {
2321 printk(KERN_DEBUG "sx: Found an SI board at %lx\n", board->hw_base);
2322 /* Compared to the SX boards, it is a complete guess as to what
2323 this card is up to... */
2327 /* This resets the processor, and keeps it off the bus. */
2328 if (!sx_reset(board))
2330 sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
2337 static const struct tty_operations sx_ops = {
2338 .break_ctl = sx_break,
2342 .put_char = gs_put_char,
2343 .flush_chars = gs_flush_chars,
2344 .write_room = gs_write_room,
2345 .chars_in_buffer = gs_chars_in_buffer,
2346 .flush_buffer = gs_flush_buffer,
2348 .throttle = sx_throttle,
2349 .unthrottle = sx_unthrottle,
2350 .set_termios = gs_set_termios,
2353 .hangup = gs_hangup,
2354 .tiocmget = sx_tiocmget,
2355 .tiocmset = sx_tiocmset,
2358 static const struct tty_port_operations sx_port_ops = {
2359 .carrier_raised = sx_carrier_raised,
2362 static int sx_init_drivers(void)
2368 sx_driver = alloc_tty_driver(sx_nports);
2371 sx_driver->owner = THIS_MODULE;
2372 sx_driver->driver_name = "specialix_sx";
2373 sx_driver->name = "ttyX";
2374 sx_driver->major = SX_NORMAL_MAJOR;
2375 sx_driver->type = TTY_DRIVER_TYPE_SERIAL;
2376 sx_driver->subtype = SERIAL_TYPE_NORMAL;
2377 sx_driver->init_termios = tty_std_termios;
2378 sx_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2379 sx_driver->init_termios.c_ispeed = 9600;
2380 sx_driver->init_termios.c_ospeed = 9600;
2381 sx_driver->flags = TTY_DRIVER_REAL_RAW;
2382 tty_set_operations(sx_driver, &sx_ops);
2384 if ((error = tty_register_driver(sx_driver))) {
2385 put_tty_driver(sx_driver);
2386 printk(KERN_ERR "sx: Couldn't register sx driver, error = %d\n",
2394 static int sx_init_portstructs(int nboards, int nports)
2396 struct sx_board *board;
2397 struct sx_port *port;
2404 /* Many drivers statically allocate the maximum number of ports
2405 There is no reason not to allocate them dynamically.
2407 sx_ports = kcalloc(nports, sizeof(struct sx_port), GFP_KERNEL);
2412 for (i = 0; i < nboards; i++) {
2414 board->ports = port;
2415 for (j = 0; j < boards[i].nports; j++) {
2416 sx_dprintk(SX_DEBUG_INIT, "initing port %d\n", j);
2417 tty_port_init(&port->gs.port);
2418 port->gs.port.ops = &sx_port_ops;
2419 port->gs.magic = SX_MAGIC;
2420 port->gs.close_delay = HZ / 2;
2421 port->gs.closing_wait = 30 * HZ;
2422 port->board = board;
2423 port->gs.rd = &sx_real_driver;
2424 #ifdef NEW_WRITE_LOCKING
2425 port->gs.port_write_mutex = MUTEX;
2427 spin_lock_init(&port->gs.driver_lock);
2429 * Initializing wait queue
2437 for (i = 0; i < nboards; i++) {
2439 board->port_base = portno;
2440 /* Possibly the configuration was rejected. */
2441 sx_dprintk(SX_DEBUG_PROBE, "Board has %d channels\n",
2443 if (board->nports <= 0)
2445 /* XXX byteorder ?? */
2446 for (addr = 0x80; addr != 0;
2447 addr = read_sx_word(board, addr) & 0x7fff) {
2448 chans = sx_read_module_byte(board, addr, mc_type);
2449 sx_dprintk(SX_DEBUG_PROBE, "Module at %x: %d "
2450 "channels\n", addr, chans);
2451 sx_dprintk(SX_DEBUG_PROBE, "Port at");
2452 for (j = 0; j < chans; j++) {
2453 /* The "sx-way" is the way it SHOULD be done.
2454 That way in the future, the firmware may for
2455 example pack the structures a bit more
2456 efficient. Neil tells me it isn't going to
2457 happen anytime soon though. */
2458 if (IS_SX_BOARD(board))
2459 port->ch_base = sx_read_module_word(
2460 board, addr + j * 2,
2463 port->ch_base = addr + 0x100 + 0x300 *j;
2465 sx_dprintk(SX_DEBUG_PROBE, " %x",
2467 port->line = portno++;
2470 sx_dprintk(SX_DEBUG_PROBE, "\n");
2472 /* This has to be done earlier. */
2473 /* board->flags |= SX_BOARD_INITIALIZED; */
2480 static unsigned int sx_find_free_board(void)
2484 for (i = 0; i < SX_NBOARDS; i++)
2485 if (!(boards[i].flags & SX_BOARD_PRESENT))
2491 static void __exit sx_release_drivers(void)
2494 tty_unregister_driver(sx_driver);
2495 put_tty_driver(sx_driver);
2499 static void __devexit sx_remove_card(struct sx_board *board,
2500 struct pci_dev *pdev)
2502 if (board->flags & SX_BOARD_INITIALIZED) {
2503 /* The board should stop messing with us. (actually I mean the
2506 if ((board->irq) && (board->flags & SX_IRQ_ALLOCATED))
2507 free_irq(board->irq, board);
2509 /* It is safe/allowed to del_timer a non-active timer */
2510 del_timer(&board->timer);
2513 iounmap(board->base2);
2514 pci_release_region(pdev, IS_CF_BOARD(board) ? 3 : 2);
2517 iounmap(board->base);
2518 release_region(board->hw_base, board->hw_len);
2521 board->flags &= ~(SX_BOARD_INITIALIZED | SX_BOARD_PRESENT);
2527 static int __devinit sx_eisa_probe(struct device *dev)
2529 struct eisa_device *edev = to_eisa_device(dev);
2530 struct sx_board *board;
2531 unsigned long eisa_slot = edev->base_addr;
2535 mutex_lock(&sx_boards_lock);
2536 i = sx_find_free_board();
2537 if (i == SX_NBOARDS) {
2538 mutex_unlock(&sx_boards_lock);
2542 board->flags |= SX_BOARD_PRESENT;
2543 mutex_unlock(&sx_boards_lock);
2545 dev_info(dev, "XIO : Signature found in EISA slot %lu, "
2546 "Product %d Rev %d (REPORT THIS TO LKLM)\n",
2548 inb(eisa_slot + EISA_VENDOR_ID_OFFSET + 2),
2549 inb(eisa_slot + EISA_VENDOR_ID_OFFSET + 3));
2551 board->eisa_base = eisa_slot;
2552 board->flags &= ~SX_BOARD_TYPE;
2553 board->flags |= SI_EISA_BOARD;
2555 board->hw_base = ((inb(eisa_slot + 0xc01) << 8) +
2556 inb(eisa_slot + 0xc00)) << 16;
2557 board->hw_len = SI2_EISA_WINDOW_LEN;
2558 if (!request_region(board->hw_base, board->hw_len, "sx")) {
2559 dev_err(dev, "can't request region\n");
2563 board->base = ioremap_nocache(board->hw_base, SI2_EISA_WINDOW_LEN);
2565 dev_err(dev, "can't remap memory\n");
2569 sx_dprintk(SX_DEBUG_PROBE, "IO hw_base address: %lx\n", board->hw_base);
2570 sx_dprintk(SX_DEBUG_PROBE, "base: %p\n", board->base);
2571 board->irq = inb(eisa_slot + 0xc02) >> 4;
2572 sx_dprintk(SX_DEBUG_PROBE, "IRQ: %d\n", board->irq);
2574 if (!probe_si(board))
2577 dev_set_drvdata(dev, board);
2581 iounmap(board->base);
2583 release_region(board->hw_base, board->hw_len);
2585 board->flags &= ~SX_BOARD_PRESENT;
2590 static int __devexit sx_eisa_remove(struct device *dev)
2592 struct sx_board *board = dev_get_drvdata(dev);
2594 sx_remove_card(board, NULL);
2599 static struct eisa_device_id sx_eisa_tbl[] = {
2604 MODULE_DEVICE_TABLE(eisa, sx_eisa_tbl);
2606 static struct eisa_driver sx_eisadriver = {
2607 .id_table = sx_eisa_tbl,
2610 .probe = sx_eisa_probe,
2611 .remove = __devexit_p(sx_eisa_remove),
2618 /********************************************************
2619 * Setting bit 17 in the CNTRL register of the PLX 9050 *
2620 * chip forces a retry on writes while a read is pending.*
2621 * This is to prevent the card locking up on Intel Xeon *
2622 * multiprocessor systems with the NX chipset. -- NV *
2623 ********************************************************/
2625 /* Newer cards are produced with this bit set from the configuration
2626 EEprom. As the bit is read/write for the CPU, we can fix it here,
2627 if we detect that it isn't set correctly. -- REW */
2629 static void __devinit fix_sx_pci(struct pci_dev *pdev, struct sx_board *board)
2631 unsigned int hwbase;
2632 void __iomem *rebase;
2635 #define CNTRL_REG_OFFSET 0x50
2636 #define CNTRL_REG_GOODVALUE 0x18260000
2638 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &hwbase);
2639 hwbase &= PCI_BASE_ADDRESS_MEM_MASK;
2640 rebase = ioremap_nocache(hwbase, 0x80);
2641 t = readl(rebase + CNTRL_REG_OFFSET);
2642 if (t != CNTRL_REG_GOODVALUE) {
2643 printk(KERN_DEBUG "sx: performing cntrl reg fix: %08x -> "
2644 "%08x\n", t, CNTRL_REG_GOODVALUE);
2645 writel(CNTRL_REG_GOODVALUE, rebase + CNTRL_REG_OFFSET);
2651 static int __devinit sx_pci_probe(struct pci_dev *pdev,
2652 const struct pci_device_id *ent)
2655 struct sx_board *board;
2656 unsigned int i, reg;
2659 mutex_lock(&sx_boards_lock);
2660 i = sx_find_free_board();
2661 if (i == SX_NBOARDS) {
2662 mutex_unlock(&sx_boards_lock);
2666 board->flags |= SX_BOARD_PRESENT;
2667 mutex_unlock(&sx_boards_lock);
2669 retval = pci_enable_device(pdev);
2673 board->flags &= ~SX_BOARD_TYPE;
2674 board->flags |= (pdev->subsystem_vendor == 0x200) ? SX_PCI_BOARD :
2677 /* CF boards use base address 3.... */
2678 reg = IS_CF_BOARD(board) ? 3 : 2;
2679 retval = pci_request_region(pdev, reg, "sx");
2681 dev_err(&pdev->dev, "can't request region\n");
2684 board->hw_base = pci_resource_start(pdev, reg);
2686 board->base = ioremap_nocache(board->hw_base, WINDOW_LEN(board));
2688 dev_err(&pdev->dev, "ioremap failed\n");
2692 /* Most of the stuff on the CF board is offset by 0x18000 .... */
2693 if (IS_CF_BOARD(board))
2694 board->base += 0x18000;
2696 board->irq = pdev->irq;
2698 dev_info(&pdev->dev, "Got a specialix card: %p(%d) %x.\n", board->base,
2699 board->irq, board->flags);
2701 if (!probe_sx(board)) {
2706 fix_sx_pci(pdev, board);
2708 pci_set_drvdata(pdev, board);
2712 iounmap(board->base2);
2714 pci_release_region(pdev, reg);
2716 board->flags &= ~SX_BOARD_PRESENT;
2724 static void __devexit sx_pci_remove(struct pci_dev *pdev)
2726 struct sx_board *board = pci_get_drvdata(pdev);
2728 sx_remove_card(board, pdev);
2731 /* Specialix has a whole bunch of cards with 0x2000 as the device ID. They say
2732 its because the standard requires it. So check for SUBVENDOR_ID. */
2733 static struct pci_device_id sx_pci_tbl[] = {
2734 { PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8,
2735 .subvendor = PCI_ANY_ID, .subdevice = 0x0200 },
2736 { PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8,
2737 .subvendor = PCI_ANY_ID, .subdevice = 0x0300 },
2741 MODULE_DEVICE_TABLE(pci, sx_pci_tbl);
2743 static struct pci_driver sx_pcidriver = {
2745 .id_table = sx_pci_tbl,
2746 .probe = sx_pci_probe,
2747 .remove = __devexit_p(sx_pci_remove)
2750 static int __init sx_init(void)
2756 struct sx_board *board;
2759 unsigned int found = 0;
2763 sx_dprintk(SX_DEBUG_INIT, "Initing sx module... (sx_debug=%d)\n",
2765 if (abs((long)(&sx_debug) - sx_debug) < 0x10000) {
2766 printk(KERN_WARNING "sx: sx_debug is an address, instead of a "
2767 "value. Assuming -1.\n(%p)\n", &sx_debug);
2771 if (misc_register(&sx_fw_device) < 0) {
2772 printk(KERN_ERR "SX: Unable to register firmware loader "
2777 for (i = 0; i < NR_SX_ADDRS; i++) {
2778 board = &boards[found];
2779 board->hw_base = sx_probe_addrs[i];
2780 board->hw_len = SX_WINDOW_LEN;
2781 if (!request_region(board->hw_base, board->hw_len, "sx"))
2784 board->base = ioremap_nocache(board->hw_base, board->hw_len);
2787 board->flags &= ~SX_BOARD_TYPE;
2788 board->flags |= SX_ISA_BOARD;
2789 board->irq = sx_irqmask ? -1 : 0;
2791 if (probe_sx(board)) {
2792 board->flags |= SX_BOARD_PRESENT;
2795 iounmap(board->base);
2797 release_region(board->hw_base, board->hw_len);
2801 for (i = 0; i < NR_SI_ADDRS; i++) {
2802 board = &boards[found];
2803 board->hw_base = si_probe_addrs[i];
2804 board->hw_len = SI2_ISA_WINDOW_LEN;
2805 if (!request_region(board->hw_base, board->hw_len, "sx"))
2808 board->base = ioremap_nocache(board->hw_base, board->hw_len);
2811 board->flags &= ~SX_BOARD_TYPE;
2812 board->flags |= SI_ISA_BOARD;
2813 board->irq = sx_irqmask ? -1 : 0;
2815 if (probe_si(board)) {
2816 board->flags |= SX_BOARD_PRESENT;
2819 iounmap(board->base);
2821 release_region(board->hw_base, board->hw_len);
2824 for (i = 0; i < NR_SI1_ADDRS; i++) {
2825 board = &boards[found];
2826 board->hw_base = si1_probe_addrs[i];
2827 board->hw_len = SI1_ISA_WINDOW_LEN;
2828 if (!request_region(board->hw_base, board->hw_len, "sx"))
2831 board->base = ioremap_nocache(board->hw_base, board->hw_len);
2834 board->flags &= ~SX_BOARD_TYPE;
2835 board->flags |= SI1_ISA_BOARD;
2836 board->irq = sx_irqmask ? -1 : 0;
2838 if (probe_si(board)) {
2839 board->flags |= SX_BOARD_PRESENT;
2842 iounmap(board->base);
2844 release_region(board->hw_base, board->hw_len);
2849 retval1 = eisa_driver_register(&sx_eisadriver);
2851 retval = pci_register_driver(&sx_pcidriver);
2854 printk(KERN_INFO "sx: total of %d boards detected.\n", found);
2856 } else if (retval) {
2861 misc_deregister(&sx_fw_device);
2868 static void __exit sx_exit(void)
2874 eisa_driver_unregister(&sx_eisadriver);
2876 pci_unregister_driver(&sx_pcidriver);
2878 for (i = 0; i < SX_NBOARDS; i++)
2879 sx_remove_card(&boards[i], NULL);
2881 if (misc_deregister(&sx_fw_device) < 0) {
2882 printk(KERN_INFO "sx: couldn't deregister firmware loader "
2885 sx_dprintk(SX_DEBUG_CLEANUP, "Cleaning up drivers (%d)\n",
2888 sx_release_drivers();
2894 module_init(sx_init);
2895 module_exit(sx_exit);