Linux 2.4.0-test10pre4
[davej-history.git] / drivers / char / specialix.c
blob6e3c6a99c3773355f85db23d708f7a13f82595f3
1 /*
2 * specialix.c -- specialix IO8+ multiport serial driver.
4 * Copyright (C) 1997 Roger Wolff (R.E.Wolff@BitWizard.nl)
5 * Copyright (C) 1994-1996 Dmitry Gorodchanin (pgmdsg@ibi.com)
7 * Specialix pays for the development and support of this driver.
8 * Please DO contact io8-linux@specialix.co.uk if you require
9 * support. But please read the documentation (specialix.txt)
10 * first.
12 * This driver was developped in the BitWizard linux device
13 * driver service. If you require a linux device driver for your
14 * product, please contact devices@BitWizard.nl for a quote.
16 * This code is firmly based on the riscom/8 serial driver,
17 * written by Dmitry Gorodchanin. The specialix IO8+ card
18 * programming information was obtained from the CL-CD1865 Data
19 * Book, and Specialix document number 6200059: IO8+ Hardware
20 * Functional Specification.
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License as
24 * published by the Free Software Foundation; either version 2 of
25 * the License, or (at your option) any later version.
27 * This program is distributed in the hope that it will be
28 * useful, but WITHOUT ANY WARRANTY; without even the implied
29 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
30 * PURPOSE. See the GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public
33 * License along with this program; if not, write to the Free
34 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
35 * USA.
37 * Revision history:
39 * Revision 1.0: April 1st 1997.
40 * Initial release for alpha testing.
41 * Revision 1.1: April 14th 1997.
42 * Incorporated Richard Hudsons suggestions,
43 * removed some debugging printk's.
44 * Revision 1.2: April 15th 1997.
45 * Ported to 2.1.x kernels.
46 * Revision 1.3: April 17th 1997
47 * Backported to 2.0. (Compatibility macros).
48 * Revision 1.4: April 18th 1997
49 * Fixed DTR/RTS bug that caused the card to indicate
50 * "don't send data" to a modem after the password prompt.
51 * Fixed bug for premature (fake) interrupts.
52 * Revision 1.5: April 19th 1997
53 * fixed a minor typo in the header file, cleanup a little.
54 * performance warnings are now MAXed at once per minute.
55 * Revision 1.6: May 23 1997
56 * Changed the specialix=... format to include interrupt.
57 * Revision 1.7: May 27 1997
58 * Made many more debug printk's a compile time option.
59 * Revision 1.8: Jul 1 1997
60 * port to linux-2.1.43 kernel.
61 * Revision 1.9: Oct 9 1998
62 * Added stuff for the IO8+/PCI version.
63 * Revision 1.10: Oct 22 1999 / Jan 21 2000.
64 * Added stuff for setserial.
65 * Nicolas Mailhot (Nicolas.Mailhot@email.enst.fr)
69 #define VERSION "1.10"
73 * There is a bunch of documentation about the card, jumpers, config
74 * settings, restrictions, cables, device names and numbers in
75 * ../../Documentation/specialix.txt
78 #include <linux/config.h>
79 #include <linux/module.h>
81 #include <asm/io.h>
82 #include <linux/kernel.h>
83 #include <linux/sched.h>
84 #include <linux/ioport.h>
85 #include <linux/interrupt.h>
86 #include <linux/errno.h>
87 #include <linux/tty.h>
88 #include <linux/mm.h>
89 #include <linux/serial.h>
90 #include <linux/fcntl.h>
91 #include <linux/major.h>
92 #include <linux/delay.h>
93 #include <linux/tqueue.h>
94 #include <linux/version.h>
95 #include <linux/pci.h>
98 /* ************************************************************** */
99 /* * This section can be removed when 2.0 becomes outdated.... * */
100 /* ************************************************************** */
102 #if LINUX_VERSION_CODE < 131328 /* Less than 2.1.0 */
103 #define TWO_ZERO
104 #else
105 #if LINUX_VERSION_CODE < 131371 /* less than 2.1.43 */
106 /* This has not been extensively tested yet. Sorry. */
107 #warning "You're on your own between 2.1.0 and 2.1.43.... "
108 #warning "Please use a recent kernel."
109 #endif
110 #endif
113 #ifdef TWO_ZERO
114 #define Get_user(a,b) a = get_user(b)
115 #define copy_from_user(a,b,c) memcpy_fromfs(a,b,c)
116 #define copy_to_user(a,b,c) memcpy_tofs(a,b,c)
117 #define queue_task queue_task_irq_off
118 #else
119 #define Get_user(a,b) get_user(a,b)
120 #endif
122 /* ************************************************************** */
123 /* * End of compatibility section.. * */
124 /* ************************************************************** */
127 #ifndef TWO_ZERO
128 #include <asm/uaccess.h>
129 #endif
131 #include "specialix_io8.h"
132 #include "cd1865.h"
136 /* Configurable options: */
138 /* Am I paranoid or not ? ;-) */
139 #define SPECIALIX_PARANOIA_CHECK
141 /* Do I trust the IRQ from the card? (enabeling it doesn't seem to help)
142 When the IRQ routine leaves the chip in a state that is keeps on
143 requiring attention, the timer doesn't help either. */
144 #undef SPECIALIX_TIMER
147 * The following defines are mostly for testing purposes. But if you need
148 * some nice reporting in your syslog, you can define them also.
150 #undef SX_REPORT_FIFO
151 #undef SX_REPORT_OVERRUN
155 #ifdef CONFIG_SPECIALIX_RTSCTS
156 #define SX_CRTSCTS(bla) 1
157 #else
158 #define SX_CRTSCTS(tty) C_CRTSCTS(tty)
159 #endif
162 /* Used to be outb (0xff, 0x80); */
163 #define short_pause() udelay (1)
166 #define SPECIALIX_LEGAL_FLAGS \
167 (ASYNC_HUP_NOTIFY | ASYNC_SAK | ASYNC_SPLIT_TERMIOS | \
168 ASYNC_SPD_HI | ASYNC_SPEED_VHI | ASYNC_SESSION_LOCKOUT | \
169 ASYNC_PGRP_LOCKOUT | ASYNC_CALLOUT_NOHUP)
171 #ifndef MIN
172 #define MIN(a,b) ((a) < (b) ? (a) : (b))
173 #endif
175 DECLARE_TASK_QUEUE(tq_specialix);
177 #undef RS_EVENT_WRITE_WAKEUP
178 #define RS_EVENT_WRITE_WAKEUP 0
180 #define SPECIALIX_TYPE_NORMAL 1
181 #define SPECIALIX_TYPE_CALLOUT 2
183 static struct tty_driver specialix_driver, specialix_callout_driver;
184 static int specialix_refcount;
185 static struct tty_struct * specialix_table[SX_NBOARD * SX_NPORT];
186 static struct termios * specialix_termios[SX_NBOARD * SX_NPORT];
187 static struct termios * specialix_termios_locked[SX_NBOARD * SX_NPORT];
188 static unsigned char * tmp_buf;
189 static DECLARE_MUTEX(tmp_buf_sem);
191 static unsigned long baud_table[] = {
192 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
193 9600, 19200, 38400, 57600, 115200, 0,
196 static struct specialix_board sx_board[SX_NBOARD] = {
197 { 0, SX_IOBASE1, 9, },
198 { 0, SX_IOBASE2, 11, },
199 { 0, SX_IOBASE3, 12, },
200 { 0, SX_IOBASE4, 15, },
203 static struct specialix_port sx_port[SX_NBOARD * SX_NPORT];
206 #ifdef SPECIALIX_TIMER
207 static struct timer_list missed_irq_timer;
208 static void sx_interrupt(int irq, void * dev_id, struct pt_regs * regs);
209 #endif
213 static inline int sx_paranoia_check(struct specialix_port const * port,
214 kdev_t device, const char *routine)
216 #ifdef SPECIALIX_PARANOIA_CHECK
217 static const char *badmagic =
218 KERN_ERR "sx: Warning: bad specialix port magic number for device %s in %s\n";
219 static const char *badinfo =
220 KERN_ERR "sx: Warning: null specialix port for device %s in %s\n";
222 if (!port) {
223 printk(badinfo, kdevname(device), routine);
224 return 1;
226 if (port->magic != SPECIALIX_MAGIC) {
227 printk(badmagic, kdevname(device), routine);
228 return 1;
230 #endif
231 return 0;
237 * Service functions for specialix IO8+ driver.
241 /* Get board number from pointer */
242 extern inline int board_No (struct specialix_board * bp)
244 return bp - sx_board;
248 /* Get port number from pointer */
249 extern inline int port_No (struct specialix_port const * port)
251 return SX_PORT(port - sx_port);
255 /* Get pointer to board from pointer to port */
256 extern inline struct specialix_board * port_Board(struct specialix_port const * port)
258 return &sx_board[SX_BOARD(port - sx_port)];
262 /* Input Byte from CL CD186x register */
263 extern inline unsigned char sx_in(struct specialix_board * bp, unsigned short reg)
265 bp->reg = reg | 0x80;
266 outb (reg | 0x80, bp->base + SX_ADDR_REG);
267 return inb (bp->base + SX_DATA_REG);
271 /* Output Byte to CL CD186x register */
272 extern inline void sx_out(struct specialix_board * bp, unsigned short reg,
273 unsigned char val)
275 bp->reg = reg | 0x80;
276 outb (reg | 0x80, bp->base + SX_ADDR_REG);
277 outb (val, bp->base + SX_DATA_REG);
281 /* Input Byte from CL CD186x register */
282 extern inline unsigned char sx_in_off(struct specialix_board * bp, unsigned short reg)
284 bp->reg = reg;
285 outb (reg, bp->base + SX_ADDR_REG);
286 return inb (bp->base + SX_DATA_REG);
290 /* Output Byte to CL CD186x register */
291 extern inline void sx_out_off(struct specialix_board * bp, unsigned short reg,
292 unsigned char val)
294 bp->reg = reg;
295 outb (reg, bp->base + SX_ADDR_REG);
296 outb (val, bp->base + SX_DATA_REG);
300 /* Wait for Channel Command Register ready */
301 extern inline void sx_wait_CCR(struct specialix_board * bp)
303 unsigned long delay;
305 for (delay = SX_CCR_TIMEOUT; delay; delay--)
306 if (!sx_in(bp, CD186x_CCR))
307 return;
309 printk(KERN_ERR "sx%d: Timeout waiting for CCR.\n", board_No(bp));
313 /* Wait for Channel Command Register ready */
314 extern inline void sx_wait_CCR_off(struct specialix_board * bp)
316 unsigned long delay;
318 for (delay = SX_CCR_TIMEOUT; delay; delay--)
319 if (!sx_in_off(bp, CD186x_CCR))
320 return;
322 printk(KERN_ERR "sx%d: Timeout waiting for CCR.\n", board_No(bp));
327 * specialix IO8+ IO range functions.
330 extern inline int sx_check_io_range(struct specialix_board * bp)
332 return check_region (bp->base, SX_IO_SPACE);
336 extern inline void sx_request_io_range(struct specialix_board * bp)
338 request_region(bp->base,
339 bp->flags&SX_BOARD_IS_PCI?SX_PCI_IO_SPACE:SX_IO_SPACE,
340 "specialix IO8+" );
344 extern inline void sx_release_io_range(struct specialix_board * bp)
346 release_region(bp->base,
347 bp->flags&SX_BOARD_IS_PCI?SX_PCI_IO_SPACE:SX_IO_SPACE);
351 /* Must be called with enabled interrupts */
352 /* Ugly. Very ugly. Don't use this for anything else than initialization
353 code */
354 extern inline void sx_long_delay(unsigned long delay)
356 unsigned long i;
358 for (i = jiffies + delay; time_after(i, jiffies); ) ;
363 /* Set the IRQ using the RTS lines that run to the PAL on the board.... */
364 int sx_set_irq ( struct specialix_board *bp)
366 int virq;
367 int i;
369 if (bp->flags & SX_BOARD_IS_PCI)
370 return 1;
371 switch (bp->irq) {
372 /* In the same order as in the docs... */
373 case 15: virq = 0;break;
374 case 12: virq = 1;break;
375 case 11: virq = 2;break;
376 case 9: virq = 3;break;
377 default: printk (KERN_ERR "Speclialix: cannot set irq to %d.\n", bp->irq);
378 return 0;
381 for (i=0;i<2;i++) {
382 sx_out(bp, CD186x_CAR, i);
383 sx_out(bp, CD186x_MSVRTS, ((virq >> i) & 0x1)? MSVR_RTS:0);
385 return 1;
389 /* Reset and setup CD186x chip */
390 static int sx_init_CD186x(struct specialix_board * bp)
392 unsigned long flags;
393 int scaler;
394 int rv = 1;
396 save_flags(flags); cli();
398 sx_wait_CCR_off(bp); /* Wait for CCR ready */
399 sx_out_off(bp, CD186x_CCR, CCR_HARDRESET); /* Reset CD186x chip */
400 sti();
401 sx_long_delay(HZ/20); /* Delay 0.05 sec */
402 cli();
403 sx_out_off(bp, CD186x_GIVR, SX_ID); /* Set ID for this chip */
404 sx_out_off(bp, CD186x_GICR, 0); /* Clear all bits */
405 sx_out_off(bp, CD186x_PILR1, SX_ACK_MINT); /* Prio for modem intr */
406 sx_out_off(bp, CD186x_PILR2, SX_ACK_TINT); /* Prio for transmitter intr */
407 sx_out_off(bp, CD186x_PILR3, SX_ACK_RINT); /* Prio for receiver intr */
408 /* Set RegAckEn */
409 sx_out_off(bp, CD186x_SRCR, sx_in (bp, CD186x_SRCR) | SRCR_REGACKEN);
411 /* Setting up prescaler. We need 4 ticks per 1 ms */
412 scaler = SX_OSCFREQ/SPECIALIX_TPS;
414 sx_out_off(bp, CD186x_PPRH, scaler >> 8);
415 sx_out_off(bp, CD186x_PPRL, scaler & 0xff);
417 if (!sx_set_irq (bp)) {
418 /* Figure out how to pass this along... */
419 printk (KERN_ERR "Cannot set irq to %d.\n", bp->irq);
420 rv = 0;
423 restore_flags(flags);
424 return rv;
428 int read_cross_byte (struct specialix_board *bp, int reg, int bit)
430 int i;
431 int t;
433 for (i=0, t=0;i<8;i++) {
434 sx_out_off (bp, CD186x_CAR, i);
435 if (sx_in_off (bp, reg) & bit)
436 t |= 1 << i;
438 return t;
442 #ifdef SPECIALIX_TIMER
443 void missed_irq (unsigned long data)
445 if (sx_in ((struct specialix_board *)data, CD186x_SRSR) &
446 (SRSR_RREQint |
447 SRSR_TREQint |
448 SRSR_MREQint)) {
449 printk (KERN_INFO "Missed interrupt... Calling int from timer. \n");
450 sx_interrupt (((struct specialix_board *)data)->irq,
451 NULL, NULL);
453 missed_irq_timer.expires = jiffies + HZ;
454 add_timer (&missed_irq_timer);
456 #endif
460 /* Main probing routine, also sets irq. */
461 static int sx_probe(struct specialix_board *bp)
463 unsigned char val1, val2;
464 #if 0
465 int irqs = 0;
466 int retries;
467 #endif
468 int rev;
469 int chip;
471 if (sx_check_io_range(bp))
472 return 1;
474 /* Are the I/O ports here ? */
475 sx_out_off(bp, CD186x_PPRL, 0x5a);
476 short_pause ();
477 val1 = sx_in_off(bp, CD186x_PPRL);
479 sx_out_off(bp, CD186x_PPRL, 0xa5);
480 short_pause ();
481 val2 = sx_in_off(bp, CD186x_PPRL);
484 if ((val1 != 0x5a) || (val2 != 0xa5)) {
485 printk(KERN_INFO "sx%d: specialix IO8+ Board at 0x%03x not found.\n",
486 board_No(bp), bp->base);
487 return 1;
490 /* Check the DSR lines that Specialix uses as board
491 identification */
492 val1 = read_cross_byte (bp, CD186x_MSVR, MSVR_DSR);
493 val2 = read_cross_byte (bp, CD186x_MSVR, MSVR_RTS);
494 #ifdef SPECIALIX_DEBUG
495 printk (KERN_DEBUG "sx%d: DSR lines are: %02x, rts lines are: %02x\n",
496 board_No(bp), val1, val2);
497 #endif
498 /* They managed to switch the bit order between the docs and
499 the IO8+ card. The new PCI card now conforms to old docs.
500 They changed the PCI docs to reflect the situation on the
501 old card. */
502 val2 = (bp->flags & SX_BOARD_IS_PCI)?0x4d : 0xb2;
503 if (val1 != val2) {
504 printk(KERN_INFO "sx%d: specialix IO8+ ID %02x at 0x%03x not found (%02x).\n",
505 board_No(bp), val2, bp->base, val1);
506 return 1;
510 #if 0
511 /* It's time to find IRQ for this board */
512 for (retries = 0; retries < 5 && irqs <= 0; retries++) {
513 irqs = probe_irq_on();
514 sx_init_CD186x(bp); /* Reset CD186x chip */
515 sx_out(bp, CD186x_CAR, 2); /* Select port 2 */
516 sx_wait_CCR(bp);
517 sx_out(bp, CD186x_CCR, CCR_TXEN); /* Enable transmitter */
518 sx_out(bp, CD186x_IER, IER_TXRDY); /* Enable tx empty intr */
519 sx_long_delay(HZ/20);
520 irqs = probe_irq_off(irqs);
522 #if SPECIALIX_DEBUG > 2
523 printk (KERN_DEBUG "SRSR = %02x, ", sx_in(bp, CD186x_SRSR));
524 printk ( "TRAR = %02x, ", sx_in(bp, CD186x_TRAR));
525 printk ( "GIVR = %02x, ", sx_in(bp, CD186x_GIVR));
526 printk ( "GICR = %02x, ", sx_in(bp, CD186x_GICR));
527 printk ( "\n");
528 #endif
529 /* Reset CD186x again */
530 if (!sx_init_CD186x(bp)) {
531 /* Hmmm. This is dead code anyway. */
533 #if SPECIALIX_DEBUG > 2
534 printk (KERN_DEBUG "val1 = %02x, val2 = %02x, val3 = %02x.\n",
535 val1, val2, val3);
536 #endif
540 #if 0
541 if (irqs <= 0) {
542 printk(KERN_ERR "sx%d: Can't find IRQ for specialix IO8+ board at 0x%03x.\n",
543 board_No(bp), bp->base);
544 return 1;
546 #endif
547 printk (KERN_INFO "Started with irq=%d, but now have irq=%d.\n", bp->irq, irqs);
548 if (irqs > 0)
549 bp->irq = irqs;
550 #endif
551 /* Reset CD186x again */
552 if (!sx_init_CD186x(bp)) {
553 return -EIO;
556 sx_request_io_range(bp);
557 bp->flags |= SX_BOARD_PRESENT;
559 /* Chip revcode pkgtype
560 GFRCR SRCR bit 7
561 CD180 rev B 0x81 0
562 CD180 rev C 0x82 0
563 CD1864 rev A 0x82 1
564 CD1865 rev A 0x83 1 -- Do not use!!! Does not work.
565 CD1865 rev B 0x84 1
566 -- Thanks to Gwen Wang, Cirrus Logic.
569 switch (sx_in_off(bp, CD186x_GFRCR)) {
570 case 0x82:chip = 1864;rev='A';break;
571 case 0x83:chip = 1865;rev='A';break;
572 case 0x84:chip = 1865;rev='B';break;
573 case 0x85:chip = 1865;rev='C';break; /* Does not exist at this time */
574 default:chip=-1;rev='x';
577 #if SPECIALIX_DEBUG > 2
578 printk (KERN_DEBUG " GFCR = 0x%02x\n", sx_in_off(bp, CD186x_GFRCR) );
579 #endif
581 #ifdef SPECIALIX_TIMER
582 init_timer (&missed_irq_timer);
583 missed_irq_timer.function = missed_irq;
584 missed_irq_timer.data = (unsigned long) bp;
585 missed_irq_timer.expires = jiffies + HZ;
586 add_timer (&missed_irq_timer);
587 #endif
589 printk(KERN_INFO"sx%d: specialix IO8+ board detected at 0x%03x, IRQ %d, CD%d Rev. %c.\n",
590 board_No(bp),
591 bp->base, bp->irq,
592 chip, rev);
594 return 0;
599 * Interrupt processing routines.
600 * */
602 extern inline void sx_mark_event(struct specialix_port * port, int event)
605 * I'm not quite happy with current scheme all serial
606 * drivers use their own BH routine.
607 * It seems this easily can be done with one BH routine
608 * serving for all serial drivers.
609 * For now I must introduce another one - SPECIALIX_BH.
610 * Still hope this will be changed in near future.
611 * -- Dmitry.
613 set_bit(event, &port->event);
614 queue_task(&port->tqueue, &tq_specialix);
615 mark_bh(SPECIALIX_BH);
619 extern inline struct specialix_port * sx_get_port(struct specialix_board * bp,
620 unsigned char const * what)
622 unsigned char channel;
623 struct specialix_port * port;
625 channel = sx_in(bp, CD186x_GICR) >> GICR_CHAN_OFF;
626 if (channel < CD186x_NCH) {
627 port = &sx_port[board_No(bp) * SX_NPORT + channel];
628 if (port->flags & ASYNC_INITIALIZED) {
629 return port;
632 printk(KERN_INFO "sx%d: %s interrupt from invalid port %d\n",
633 board_No(bp), what, channel);
634 return NULL;
638 extern inline void sx_receive_exc(struct specialix_board * bp)
640 struct specialix_port *port;
641 struct tty_struct *tty;
642 unsigned char status;
643 unsigned char ch;
645 if (!(port = sx_get_port(bp, "Receive")))
646 return;
648 tty = port->tty;
649 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
650 printk(KERN_INFO "sx%d: port %d: Working around flip buffer overflow.\n",
651 board_No(bp), port_No(port));
652 return;
655 #ifdef SX_REPORT_OVERRUN
656 status = sx_in(bp, CD186x_RCSR);
657 if (status & RCSR_OE) {
658 port->overrun++;
659 #if SPECIALIX_DEBUG
660 printk(KERN_DEBUG "sx%d: port %d: Overrun. Total %ld overruns.\n",
661 board_No(bp), port_No(port), port->overrun);
662 #endif
664 status &= port->mark_mask;
665 #else
666 status = sx_in(bp, CD186x_RCSR) & port->mark_mask;
667 #endif
668 ch = sx_in(bp, CD186x_RDR);
669 if (!status) {
670 return;
672 if (status & RCSR_TOUT) {
673 printk(KERN_INFO "sx%d: port %d: Receiver timeout. Hardware problems ?\n",
674 board_No(bp), port_No(port));
675 return;
677 } else if (status & RCSR_BREAK) {
678 #ifdef SPECIALIX_DEBUG
679 printk(KERN_DEBUG "sx%d: port %d: Handling break...\n",
680 board_No(bp), port_No(port));
681 #endif
682 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
683 if (port->flags & ASYNC_SAK)
684 do_SAK(tty);
686 } else if (status & RCSR_PE)
687 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
689 else if (status & RCSR_FE)
690 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
692 else if (status & RCSR_OE)
693 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
695 else
696 *tty->flip.flag_buf_ptr++ = 0;
698 *tty->flip.char_buf_ptr++ = ch;
699 tty->flip.count++;
700 queue_task(&tty->flip.tqueue, &tq_timer);
704 extern inline void sx_receive(struct specialix_board * bp)
706 struct specialix_port *port;
707 struct tty_struct *tty;
708 unsigned char count;
710 if (!(port = sx_get_port(bp, "Receive")))
711 return;
713 tty = port->tty;
715 count = sx_in(bp, CD186x_RDCR);
717 #ifdef SX_REPORT_FIFO
718 port->hits[count > 8 ? 9 : count]++;
719 #endif
721 while (count--) {
722 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
723 printk(KERN_INFO "sx%d: port %d: Working around flip buffer overflow.\n",
724 board_No(bp), port_No(port));
725 break;
727 *tty->flip.char_buf_ptr++ = sx_in(bp, CD186x_RDR);
728 *tty->flip.flag_buf_ptr++ = 0;
729 tty->flip.count++;
731 queue_task(&tty->flip.tqueue, &tq_timer);
735 extern inline void sx_transmit(struct specialix_board * bp)
737 struct specialix_port *port;
738 struct tty_struct *tty;
739 unsigned char count;
742 if (!(port = sx_get_port(bp, "Transmit")))
743 return;
745 tty = port->tty;
747 if (port->IER & IER_TXEMPTY) {
748 /* FIFO drained */
749 sx_out(bp, CD186x_CAR, port_No(port));
750 port->IER &= ~IER_TXEMPTY;
751 sx_out(bp, CD186x_IER, port->IER);
752 return;
755 if ((port->xmit_cnt <= 0 && !port->break_length)
756 || tty->stopped || tty->hw_stopped) {
757 sx_out(bp, CD186x_CAR, port_No(port));
758 port->IER &= ~IER_TXRDY;
759 sx_out(bp, CD186x_IER, port->IER);
760 return;
763 if (port->break_length) {
764 if (port->break_length > 0) {
765 if (port->COR2 & COR2_ETC) {
766 sx_out(bp, CD186x_TDR, CD186x_C_ESC);
767 sx_out(bp, CD186x_TDR, CD186x_C_SBRK);
768 port->COR2 &= ~COR2_ETC;
770 count = MIN(port->break_length, 0xff);
771 sx_out(bp, CD186x_TDR, CD186x_C_ESC);
772 sx_out(bp, CD186x_TDR, CD186x_C_DELAY);
773 sx_out(bp, CD186x_TDR, count);
774 if (!(port->break_length -= count))
775 port->break_length--;
776 } else {
777 sx_out(bp, CD186x_TDR, CD186x_C_ESC);
778 sx_out(bp, CD186x_TDR, CD186x_C_EBRK);
779 sx_out(bp, CD186x_COR2, port->COR2);
780 sx_wait_CCR(bp);
781 sx_out(bp, CD186x_CCR, CCR_CORCHG2);
782 port->break_length = 0;
784 return;
787 count = CD186x_NFIFO;
788 do {
789 sx_out(bp, CD186x_TDR, port->xmit_buf[port->xmit_tail++]);
790 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
791 if (--port->xmit_cnt <= 0)
792 break;
793 } while (--count > 0);
795 if (port->xmit_cnt <= 0) {
796 sx_out(bp, CD186x_CAR, port_No(port));
797 port->IER &= ~IER_TXRDY;
798 sx_out(bp, CD186x_IER, port->IER);
800 if (port->xmit_cnt <= port->wakeup_chars)
801 sx_mark_event(port, RS_EVENT_WRITE_WAKEUP);
805 extern inline void sx_check_modem(struct specialix_board * bp)
807 struct specialix_port *port;
808 struct tty_struct *tty;
809 unsigned char mcr;
811 #ifdef SPECIALIX_DEBUG
812 printk (KERN_DEBUG "Modem intr. ");
813 #endif
814 if (!(port = sx_get_port(bp, "Modem")))
815 return;
817 tty = port->tty;
819 mcr = sx_in(bp, CD186x_MCR);
820 printk ("mcr = %02x.\n", mcr);
822 if ((mcr & MCR_CDCHG)) {
823 #ifdef SPECIALIX_DEBUG
824 printk (KERN_DEBUG "CD just changed... ");
825 #endif
826 if (sx_in(bp, CD186x_MSVR) & MSVR_CD) {
827 #ifdef SPECIALIX_DEBUG
828 printk ( "Waking up guys in open.\n");
829 #endif
830 wake_up_interruptible(&port->open_wait);
832 else if (!((port->flags & ASYNC_CALLOUT_ACTIVE) &&
833 (port->flags & ASYNC_CALLOUT_NOHUP))) {
834 #ifdef SPECIALIX_DEBUG
835 printk ( "Sending HUP.\n");
836 #endif
837 queue_task(&port->tqueue_hangup,
838 &tq_scheduler);
839 } else {
840 #ifdef SPECIALIX_DEBUG
841 printk ( "Don't need to send HUP.\n");
842 #endif
846 #ifdef SPECIALIX_BRAIN_DAMAGED_CTS
847 if (mcr & MCR_CTSCHG) {
848 if (sx_in(bp, CD186x_MSVR) & MSVR_CTS) {
849 tty->hw_stopped = 0;
850 port->IER |= IER_TXRDY;
851 if (port->xmit_cnt <= port->wakeup_chars)
852 sx_mark_event(port, RS_EVENT_WRITE_WAKEUP);
853 } else {
854 tty->hw_stopped = 1;
855 port->IER &= ~IER_TXRDY;
857 sx_out(bp, CD186x_IER, port->IER);
859 if (mcr & MCR_DSSXHG) {
860 if (sx_in(bp, CD186x_MSVR) & MSVR_DSR) {
861 tty->hw_stopped = 0;
862 port->IER |= IER_TXRDY;
863 if (port->xmit_cnt <= port->wakeup_chars)
864 sx_mark_event(port, RS_EVENT_WRITE_WAKEUP);
865 } else {
866 tty->hw_stopped = 1;
867 port->IER &= ~IER_TXRDY;
869 sx_out(bp, CD186x_IER, port->IER);
871 #endif /* SPECIALIX_BRAIN_DAMAGED_CTS */
873 /* Clear change bits */
874 sx_out(bp, CD186x_MCR, 0);
878 /* The main interrupt processing routine */
879 static void sx_interrupt(int irq, void * dev_id, struct pt_regs * regs)
881 unsigned char status;
882 unsigned char ack;
883 struct specialix_board *bp;
884 unsigned long loop = 0;
885 int saved_reg;
887 bp = dev_id;
889 if (!bp || !(bp->flags & SX_BOARD_ACTIVE)) {
890 #ifdef SPECIALIX_DEBUG
891 printk (KERN_DEBUG "sx: False interrupt. irq %d.\n", irq);
892 #endif
893 return;
896 saved_reg = bp->reg;
898 while ((++loop < 16) && (status = (sx_in(bp, CD186x_SRSR) &
899 (SRSR_RREQint |
900 SRSR_TREQint |
901 SRSR_MREQint)))) {
902 if (status & SRSR_RREQint) {
903 ack = sx_in(bp, CD186x_RRAR);
905 if (ack == (SX_ID | GIVR_IT_RCV))
906 sx_receive(bp);
907 else if (ack == (SX_ID | GIVR_IT_REXC))
908 sx_receive_exc(bp);
909 else
910 printk(KERN_ERR "sx%d: Bad receive ack 0x%02x.\n",
911 board_No(bp), ack);
913 } else if (status & SRSR_TREQint) {
914 ack = sx_in(bp, CD186x_TRAR);
916 if (ack == (SX_ID | GIVR_IT_TX))
917 sx_transmit(bp);
918 else
919 printk(KERN_ERR "sx%d: Bad transmit ack 0x%02x.\n",
920 board_No(bp), ack);
921 } else if (status & SRSR_MREQint) {
922 ack = sx_in(bp, CD186x_MRAR);
924 if (ack == (SX_ID | GIVR_IT_MODEM))
925 sx_check_modem(bp);
926 else
927 printk(KERN_ERR "sx%d: Bad modem ack 0x%02x.\n",
928 board_No(bp), ack);
932 sx_out(bp, CD186x_EOIR, 0); /* Mark end of interrupt */
934 bp->reg = saved_reg;
935 outb (bp->reg, bp->base + SX_ADDR_REG);
940 * Routines for open & close processing.
943 void turn_ints_off (struct specialix_board *bp)
945 if (bp->flags & SX_BOARD_IS_PCI) {
946 /* This was intended for enabeling the interrupt on the
947 * PCI card. However it seems that it's already enabled
948 * and as PCI interrupts can be shared, there is no real
949 * reason to have to turn it off. */
951 (void) sx_in_off (bp, 0); /* Turn off interrupts. */
954 void turn_ints_on (struct specialix_board *bp)
956 if (bp->flags & SX_BOARD_IS_PCI) {
957 /* play with the PCI chip. See comment above. */
959 (void) sx_in (bp, 0); /* Turn ON interrupts. */
963 /* Called with disabled interrupts */
964 extern inline int sx_setup_board(struct specialix_board * bp)
966 int error;
968 if (bp->flags & SX_BOARD_ACTIVE)
969 return 0;
971 error = request_irq(bp->irq, sx_interrupt, SA_INTERRUPT, "specialix IO8+", bp);
973 if (error)
974 return error;
976 turn_ints_on (bp);
977 bp->flags |= SX_BOARD_ACTIVE;
979 MOD_INC_USE_COUNT;
980 return 0;
984 /* Called with disabled interrupts */
985 extern inline void sx_shutdown_board(struct specialix_board *bp)
987 if (!(bp->flags & SX_BOARD_ACTIVE))
988 return;
990 bp->flags &= ~SX_BOARD_ACTIVE;
992 #if SPECIALIX_DEBUG > 2
993 printk ("Freeing IRQ%d for board %d.\n", bp->irq, board_No (bp));
994 #endif
995 free_irq(bp->irq, bp);
997 turn_ints_off (bp);
999 MOD_DEC_USE_COUNT;
1004 * Setting up port characteristics.
1005 * Must be called with disabled interrupts
1007 static void sx_change_speed(struct specialix_board *bp, struct specialix_port *port)
1009 struct tty_struct *tty;
1010 unsigned long baud;
1011 long tmp;
1012 unsigned char cor1 = 0, cor3 = 0;
1013 unsigned char mcor1 = 0, mcor2 = 0;
1014 static int again;
1016 if (!(tty = port->tty) || !tty->termios)
1017 return;
1019 port->IER = 0;
1020 port->COR2 = 0;
1021 /* Select port on the board */
1022 sx_out(bp, CD186x_CAR, port_No(port));
1024 /* The Specialix board doens't implement the RTS lines.
1025 They are used to set the IRQ level. Don't touch them. */
1026 if (SX_CRTSCTS(tty))
1027 port->MSVR = MSVR_DTR | (sx_in(bp, CD186x_MSVR) & MSVR_RTS);
1028 else
1029 port->MSVR = (sx_in(bp, CD186x_MSVR) & MSVR_RTS);
1030 #ifdef DEBUG_SPECIALIX
1031 printk (KERN_DEBUG "sx: got MSVR=%02x.\n", port->MSVR);
1032 #endif
1033 baud = C_BAUD(tty);
1035 if (baud & CBAUDEX) {
1036 baud &= ~CBAUDEX;
1037 if (baud < 1 || baud > 2)
1038 port->tty->termios->c_cflag &= ~CBAUDEX;
1039 else
1040 baud += 15;
1042 if (baud == 15) {
1043 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1044 baud ++;
1045 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1046 baud += 2;
1050 if (!baud_table[baud]) {
1051 /* Drop DTR & exit */
1052 #ifdef SPECIALIX_DEBUG
1053 printk (KERN_DEBUG "Dropping DTR... Hmm....\n");
1054 #endif
1055 if (!SX_CRTSCTS (tty)) {
1056 port -> MSVR &= ~ MSVR_DTR;
1057 sx_out(bp, CD186x_MSVR, port->MSVR );
1059 #ifdef DEBUG_SPECIALIX
1060 else
1061 printk (KERN_DEBUG "Can't drop DTR: no DTR.\n");
1062 #endif
1063 return;
1064 } else {
1065 /* Set DTR on */
1066 if (!SX_CRTSCTS (tty)) {
1067 port ->MSVR |= MSVR_DTR;
1072 * Now we must calculate some speed depended things
1075 /* Set baud rate for port */
1076 tmp = port->custom_divisor ;
1077 if ( tmp )
1078 printk (KERN_INFO "sx%d: Using custom baud rate divisor %ld. \n"
1079 "This is an untested option, please be carefull.\n",
1080 port_No (port), tmp);
1081 else
1082 tmp = (((SX_OSCFREQ + baud_table[baud]/2) / baud_table[baud] +
1083 CD186x_TPC/2) / CD186x_TPC);
1085 if ((tmp < 0x10) && time_before(again, jiffies)) {
1086 again = jiffies + HZ * 60;
1087 /* Page 48 of version 2.0 of the CL-CD1865 databook */
1088 if (tmp >= 12) {
1089 printk (KERN_INFO "sx%d: Baud rate divisor is %ld. \n"
1090 "Performance degradation is possible.\n"
1091 "Read specialix.txt for more info.\n",
1092 port_No (port), tmp);
1093 } else {
1094 printk (KERN_INFO "sx%d: Baud rate divisor is %ld. \n"
1095 "Warning: overstressing Cirrus chip. "
1096 "This might not work.\n"
1097 "Read specialix.txt for more info.\n",
1098 port_No (port), tmp);
1102 sx_out(bp, CD186x_RBPRH, (tmp >> 8) & 0xff);
1103 sx_out(bp, CD186x_TBPRH, (tmp >> 8) & 0xff);
1104 sx_out(bp, CD186x_RBPRL, tmp & 0xff);
1105 sx_out(bp, CD186x_TBPRL, tmp & 0xff);
1107 if (port->custom_divisor) {
1108 baud = (SX_OSCFREQ + port->custom_divisor/2) / port->custom_divisor;
1109 baud = ( baud + 5 ) / 10;
1110 } else
1111 baud = (baud_table[baud] + 5) / 10; /* Estimated CPS */
1113 /* Two timer ticks seems enough to wakeup something like SLIP driver */
1114 tmp = ((baud + HZ/2) / HZ) * 2 - CD186x_NFIFO;
1115 port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
1116 SERIAL_XMIT_SIZE - 1 : tmp);
1118 /* Receiver timeout will be transmission time for 1.5 chars */
1119 tmp = (SPECIALIX_TPS + SPECIALIX_TPS/2 + baud/2) / baud;
1120 tmp = (tmp > 0xff) ? 0xff : tmp;
1121 sx_out(bp, CD186x_RTPR, tmp);
1123 switch (C_CSIZE(tty)) {
1124 case CS5:
1125 cor1 |= COR1_5BITS;
1126 break;
1127 case CS6:
1128 cor1 |= COR1_6BITS;
1129 break;
1130 case CS7:
1131 cor1 |= COR1_7BITS;
1132 break;
1133 case CS8:
1134 cor1 |= COR1_8BITS;
1135 break;
1138 if (C_CSTOPB(tty))
1139 cor1 |= COR1_2SB;
1141 cor1 |= COR1_IGNORE;
1142 if (C_PARENB(tty)) {
1143 cor1 |= COR1_NORMPAR;
1144 if (C_PARODD(tty))
1145 cor1 |= COR1_ODDP;
1146 if (I_INPCK(tty))
1147 cor1 &= ~COR1_IGNORE;
1149 /* Set marking of some errors */
1150 port->mark_mask = RCSR_OE | RCSR_TOUT;
1151 if (I_INPCK(tty))
1152 port->mark_mask |= RCSR_FE | RCSR_PE;
1153 if (I_BRKINT(tty) || I_PARMRK(tty))
1154 port->mark_mask |= RCSR_BREAK;
1155 if (I_IGNPAR(tty))
1156 port->mark_mask &= ~(RCSR_FE | RCSR_PE);
1157 if (I_IGNBRK(tty)) {
1158 port->mark_mask &= ~RCSR_BREAK;
1159 if (I_IGNPAR(tty))
1160 /* Real raw mode. Ignore all */
1161 port->mark_mask &= ~RCSR_OE;
1163 /* Enable Hardware Flow Control */
1164 if (C_CRTSCTS(tty)) {
1165 #ifdef SPECIALIX_BRAIN_DAMAGED_CTS
1166 port->IER |= IER_DSR | IER_CTS;
1167 mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
1168 mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
1169 tty->hw_stopped = !(sx_in(bp, CD186x_MSVR) & (MSVR_CTS|MSVR_DSR));
1170 #else
1171 port->COR2 |= COR2_CTSAE;
1172 #endif
1174 /* Enable Software Flow Control. FIXME: I'm not sure about this */
1175 /* Some people reported that it works, but I still doubt it */
1176 if (I_IXON(tty)) {
1177 port->COR2 |= COR2_TXIBE;
1178 cor3 |= (COR3_FCT | COR3_SCDE);
1179 if (I_IXANY(tty))
1180 port->COR2 |= COR2_IXM;
1181 sx_out(bp, CD186x_SCHR1, START_CHAR(tty));
1182 sx_out(bp, CD186x_SCHR2, STOP_CHAR(tty));
1183 sx_out(bp, CD186x_SCHR3, START_CHAR(tty));
1184 sx_out(bp, CD186x_SCHR4, STOP_CHAR(tty));
1186 if (!C_CLOCAL(tty)) {
1187 /* Enable CD check */
1188 port->IER |= IER_CD;
1189 mcor1 |= MCOR1_CDZD;
1190 mcor2 |= MCOR2_CDOD;
1193 if (C_CREAD(tty))
1194 /* Enable receiver */
1195 port->IER |= IER_RXD;
1197 /* Set input FIFO size (1-8 bytes) */
1198 cor3 |= SPECIALIX_RXFIFO;
1199 /* Setting up CD186x channel registers */
1200 sx_out(bp, CD186x_COR1, cor1);
1201 sx_out(bp, CD186x_COR2, port->COR2);
1202 sx_out(bp, CD186x_COR3, cor3);
1203 /* Make CD186x know about registers change */
1204 sx_wait_CCR(bp);
1205 sx_out(bp, CD186x_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
1206 /* Setting up modem option registers */
1207 #ifdef DEBUG_SPECIALIX
1208 printk ("Mcor1 = %02x, mcor2 = %02x.\n", mcor1, mcor2);
1209 #endif
1210 sx_out(bp, CD186x_MCOR1, mcor1);
1211 sx_out(bp, CD186x_MCOR2, mcor2);
1212 /* Enable CD186x transmitter & receiver */
1213 sx_wait_CCR(bp);
1214 sx_out(bp, CD186x_CCR, CCR_TXEN | CCR_RXEN);
1215 /* Enable interrupts */
1216 sx_out(bp, CD186x_IER, port->IER);
1217 /* And finally set the modem lines... */
1218 sx_out(bp, CD186x_MSVR, port->MSVR);
1222 /* Must be called with interrupts enabled */
1223 static int sx_setup_port(struct specialix_board *bp, struct specialix_port *port)
1225 unsigned long flags;
1227 if (port->flags & ASYNC_INITIALIZED)
1228 return 0;
1230 if (!port->xmit_buf) {
1231 /* We may sleep in get_free_page() */
1232 unsigned long tmp;
1234 if (!(tmp = get_free_page(GFP_KERNEL)))
1235 return -ENOMEM;
1237 if (port->xmit_buf) {
1238 free_page(tmp);
1239 return -ERESTARTSYS;
1241 port->xmit_buf = (unsigned char *) tmp;
1244 save_flags(flags); cli();
1246 if (port->tty)
1247 clear_bit(TTY_IO_ERROR, &port->tty->flags);
1249 if (port->count == 1)
1250 bp->count++;
1252 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1253 sx_change_speed(bp, port);
1254 port->flags |= ASYNC_INITIALIZED;
1256 restore_flags(flags);
1257 return 0;
1261 /* Must be called with interrupts disabled */
1262 static void sx_shutdown_port(struct specialix_board *bp, struct specialix_port *port)
1264 struct tty_struct *tty;
1266 if (!(port->flags & ASYNC_INITIALIZED))
1267 return;
1269 #ifdef SX_REPORT_OVERRUN
1270 printk(KERN_INFO "sx%d: port %d: Total %ld overruns were detected.\n",
1271 board_No(bp), port_No(port), port->overrun);
1272 #endif
1273 #ifdef SX_REPORT_FIFO
1275 int i;
1277 printk(KERN_INFO "sx%d: port %d: FIFO hits [ ",
1278 board_No(bp), port_No(port));
1279 for (i = 0; i < 10; i++) {
1280 printk("%ld ", port->hits[i]);
1282 printk("].\n");
1284 #endif
1285 if (port->xmit_buf) {
1286 free_page((unsigned long) port->xmit_buf);
1287 port->xmit_buf = NULL;
1290 /* Select port */
1291 sx_out(bp, CD186x_CAR, port_No(port));
1293 if (!(tty = port->tty) || C_HUPCL(tty)) {
1294 /* Drop DTR */
1295 sx_out(bp, CD186x_MSVDTR, 0);
1298 /* Reset port */
1299 sx_wait_CCR(bp);
1300 sx_out(bp, CD186x_CCR, CCR_SOFTRESET);
1301 /* Disable all interrupts from this port */
1302 port->IER = 0;
1303 sx_out(bp, CD186x_IER, port->IER);
1305 if (tty)
1306 set_bit(TTY_IO_ERROR, &tty->flags);
1307 port->flags &= ~ASYNC_INITIALIZED;
1309 if (--bp->count < 0) {
1310 printk(KERN_ERR "sx%d: sx_shutdown_port: bad board count: %d\n",
1311 board_No(bp), bp->count);
1312 bp->count = 0;
1316 * If this is the last opened port on the board
1317 * shutdown whole board
1319 if (!bp->count)
1320 sx_shutdown_board(bp);
1324 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1325 struct specialix_port *port)
1327 DECLARE_WAITQUEUE(wait, current);
1328 struct specialix_board *bp = port_Board(port);
1329 int retval;
1330 int do_clocal = 0;
1331 int CD;
1334 * If the device is in the middle of being closed, then block
1335 * until it's done, and then try again.
1337 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
1338 interruptible_sleep_on(&port->close_wait);
1339 if (port->flags & ASYNC_HUP_NOTIFY)
1340 return -EAGAIN;
1341 else
1342 return -ERESTARTSYS;
1346 * If this is a callout device, then just make sure the normal
1347 * device isn't being used.
1349 if (tty->driver.subtype == SPECIALIX_TYPE_CALLOUT) {
1350 if (port->flags & ASYNC_NORMAL_ACTIVE)
1351 return -EBUSY;
1352 if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
1353 (port->flags & ASYNC_SESSION_LOCKOUT) &&
1354 (port->session != current->session))
1355 return -EBUSY;
1356 if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
1357 (port->flags & ASYNC_PGRP_LOCKOUT) &&
1358 (port->pgrp != current->pgrp))
1359 return -EBUSY;
1360 port->flags |= ASYNC_CALLOUT_ACTIVE;
1361 return 0;
1365 * If non-blocking mode is set, or the port is not enabled,
1366 * then make the check up front and then exit.
1368 if ((filp->f_flags & O_NONBLOCK) ||
1369 (tty->flags & (1 << TTY_IO_ERROR))) {
1370 if (port->flags & ASYNC_CALLOUT_ACTIVE)
1371 return -EBUSY;
1372 port->flags |= ASYNC_NORMAL_ACTIVE;
1373 return 0;
1376 if (port->flags & ASYNC_CALLOUT_ACTIVE) {
1377 if (port->normal_termios.c_cflag & CLOCAL)
1378 do_clocal = 1;
1379 } else {
1380 if (C_CLOCAL(tty))
1381 do_clocal = 1;
1385 * Block waiting for the carrier detect and the line to become
1386 * free (i.e., not in use by the callout). While we are in
1387 * this loop, info->count is dropped by one, so that
1388 * rs_close() knows when to free things. We restore it upon
1389 * exit, either normal or abnormal.
1391 retval = 0;
1392 add_wait_queue(&port->open_wait, &wait);
1393 cli();
1394 if (!tty_hung_up_p(filp))
1395 port->count--;
1396 sti();
1397 port->blocked_open++;
1398 while (1) {
1399 cli();
1400 sx_out(bp, CD186x_CAR, port_No(port));
1401 CD = sx_in(bp, CD186x_MSVR) & MSVR_CD;
1402 if (!(port->flags & ASYNC_CALLOUT_ACTIVE)) {
1403 if (SX_CRTSCTS (tty)) {
1404 /* Activate RTS */
1405 port->MSVR |= MSVR_DTR;
1406 sx_out (bp, CD186x_MSVR, port->MSVR);
1407 } else {
1408 /* Activate DTR */
1409 port->MSVR |= MSVR_DTR;
1410 sx_out (bp, CD186x_MSVR, port->MSVR);
1413 sti();
1414 set_current_state(TASK_INTERRUPTIBLE);
1415 if (tty_hung_up_p(filp) ||
1416 !(port->flags & ASYNC_INITIALIZED)) {
1417 if (port->flags & ASYNC_HUP_NOTIFY)
1418 retval = -EAGAIN;
1419 else
1420 retval = -ERESTARTSYS;
1421 break;
1423 if (!(port->flags & ASYNC_CALLOUT_ACTIVE) &&
1424 !(port->flags & ASYNC_CLOSING) &&
1425 (do_clocal || CD))
1426 break;
1427 if (signal_pending(current)) {
1428 retval = -ERESTARTSYS;
1429 break;
1431 schedule();
1433 current->state = TASK_RUNNING;
1434 remove_wait_queue(&port->open_wait, &wait);
1435 if (!tty_hung_up_p(filp))
1436 port->count++;
1437 port->blocked_open--;
1438 if (retval)
1439 return retval;
1441 port->flags |= ASYNC_NORMAL_ACTIVE;
1442 return 0;
1446 static int sx_open(struct tty_struct * tty, struct file * filp)
1448 int board;
1449 int error;
1450 struct specialix_port * port;
1451 struct specialix_board * bp;
1452 unsigned long flags;
1454 board = SX_BOARD(MINOR(tty->device));
1456 if (board > SX_NBOARD || !(sx_board[board].flags & SX_BOARD_PRESENT))
1457 return -ENODEV;
1459 bp = &sx_board[board];
1460 port = sx_port + board * SX_NPORT + SX_PORT(MINOR(tty->device));
1462 #ifdef DEBUG_SPECIALIX
1463 printk (KERN_DEBUG "Board = %d, bp = %p, port = %p, portno = %d.\n",
1464 board, bp, port, SX_PORT(MINOR(tty->device)));
1465 #endif
1467 if (sx_paranoia_check(port, tty->device, "sx_open"))
1468 return -ENODEV;
1470 if ((error = sx_setup_board(bp)))
1471 return error;
1473 port->count++;
1474 tty->driver_data = port;
1475 port->tty = tty;
1477 if ((error = sx_setup_port(bp, port)))
1478 return error;
1480 if ((error = block_til_ready(tty, filp, port)))
1481 return error;
1483 if ((port->count == 1) && (port->flags & ASYNC_SPLIT_TERMIOS)) {
1484 if (tty->driver.subtype == SPECIALIX_TYPE_NORMAL)
1485 *tty->termios = port->normal_termios;
1486 else
1487 *tty->termios = port->callout_termios;
1488 save_flags(flags); cli();
1489 sx_change_speed(bp, port);
1490 restore_flags(flags);
1493 port->session = current->session;
1494 port->pgrp = current->pgrp;
1495 return 0;
1499 static void sx_close(struct tty_struct * tty, struct file * filp)
1501 struct specialix_port *port = (struct specialix_port *) tty->driver_data;
1502 struct specialix_board *bp;
1503 unsigned long flags;
1504 unsigned long timeout;
1506 if (!port || sx_paranoia_check(port, tty->device, "close"))
1507 return;
1509 save_flags(flags); cli();
1510 if (tty_hung_up_p(filp)) {
1511 restore_flags(flags);
1512 return;
1515 bp = port_Board(port);
1516 if ((tty->count == 1) && (port->count != 1)) {
1517 printk(KERN_ERR "sx%d: sx_close: bad port count;"
1518 " tty->count is 1, port count is %d\n",
1519 board_No(bp), port->count);
1520 port->count = 1;
1522 if (--port->count < 0) {
1523 printk(KERN_ERR "sx%d: sx_close: bad port count for tty%d: %d\n",
1524 board_No(bp), port_No(port), port->count);
1525 port->count = 0;
1527 if (port->count) {
1528 restore_flags(flags);
1529 return;
1531 port->flags |= ASYNC_CLOSING;
1533 * Save the termios structure, since this port may have
1534 * separate termios for callout and dialin.
1536 if (port->flags & ASYNC_NORMAL_ACTIVE)
1537 port->normal_termios = *tty->termios;
1538 if (port->flags & ASYNC_CALLOUT_ACTIVE)
1539 port->callout_termios = *tty->termios;
1541 * Now we wait for the transmit buffer to clear; and we notify
1542 * the line discipline to only process XON/XOFF characters.
1544 tty->closing = 1;
1545 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1546 tty_wait_until_sent(tty, port->closing_wait);
1548 * At this point we stop accepting input. To do this, we
1549 * disable the receive line status interrupts, and tell the
1550 * interrupt driver to stop checking the data ready bit in the
1551 * line status register.
1553 port->IER &= ~IER_RXD;
1554 if (port->flags & ASYNC_INITIALIZED) {
1555 port->IER &= ~IER_TXRDY;
1556 port->IER |= IER_TXEMPTY;
1557 sx_out(bp, CD186x_CAR, port_No(port));
1558 sx_out(bp, CD186x_IER, port->IER);
1560 * Before we drop DTR, make sure the UART transmitter
1561 * has completely drained; this is especially
1562 * important if there is a transmit FIFO!
1564 timeout = jiffies+HZ;
1565 while(port->IER & IER_TXEMPTY) {
1566 current->state = TASK_INTERRUPTIBLE;
1567 schedule_timeout(port->timeout);
1568 if (time_after(jiffies, timeout)) {
1569 printk (KERN_INFO "Timeout waiting for close\n");
1570 break;
1575 sx_shutdown_port(bp, port);
1576 if (tty->driver.flush_buffer)
1577 tty->driver.flush_buffer(tty);
1578 if (tty->ldisc.flush_buffer)
1579 tty->ldisc.flush_buffer(tty);
1580 tty->closing = 0;
1581 port->event = 0;
1582 port->tty = 0;
1583 if (port->blocked_open) {
1584 if (port->close_delay) {
1585 current->state = TASK_INTERRUPTIBLE;
1586 schedule_timeout(port->close_delay);
1588 wake_up_interruptible(&port->open_wait);
1590 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1591 ASYNC_CLOSING);
1592 wake_up_interruptible(&port->close_wait);
1593 restore_flags(flags);
1597 static int sx_write(struct tty_struct * tty, int from_user,
1598 const unsigned char *buf, int count)
1600 struct specialix_port *port = (struct specialix_port *)tty->driver_data;
1601 struct specialix_board *bp;
1602 int c, total = 0;
1603 unsigned long flags;
1605 if (sx_paranoia_check(port, tty->device, "sx_write"))
1606 return 0;
1608 bp = port_Board(port);
1610 if (!tty || !port->xmit_buf || !tmp_buf)
1611 return 0;
1613 if (from_user)
1614 down(&tmp_buf_sem);
1616 save_flags(flags);
1617 while (1) {
1618 cli();
1619 c = MIN(count, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1620 SERIAL_XMIT_SIZE - port->xmit_head));
1621 if (c <= 0)
1622 break;
1624 if (from_user) {
1625 copy_from_user(tmp_buf, buf, c);
1626 c = MIN(c, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1627 SERIAL_XMIT_SIZE - port->xmit_head));
1628 memcpy(port->xmit_buf + port->xmit_head, tmp_buf, c);
1629 } else
1630 memcpy(port->xmit_buf + port->xmit_head, buf, c);
1631 port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1632 port->xmit_cnt += c;
1633 restore_flags(flags);
1634 buf += c;
1635 count -= c;
1636 total += c;
1638 if (from_user)
1639 up(&tmp_buf_sem);
1640 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1641 !(port->IER & IER_TXRDY)) {
1642 port->IER |= IER_TXRDY;
1643 sx_out(bp, CD186x_CAR, port_No(port));
1644 sx_out(bp, CD186x_IER, port->IER);
1646 restore_flags(flags);
1647 return total;
1651 static void sx_put_char(struct tty_struct * tty, unsigned char ch)
1653 struct specialix_port *port = (struct specialix_port *)tty->driver_data;
1654 unsigned long flags;
1656 if (sx_paranoia_check(port, tty->device, "sx_put_char"))
1657 return;
1659 if (!tty || !port->xmit_buf)
1660 return;
1662 save_flags(flags); cli();
1664 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1665 restore_flags(flags);
1666 return;
1669 port->xmit_buf[port->xmit_head++] = ch;
1670 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
1671 port->xmit_cnt++;
1672 restore_flags(flags);
1676 static void sx_flush_chars(struct tty_struct * tty)
1678 struct specialix_port *port = (struct specialix_port *)tty->driver_data;
1679 unsigned long flags;
1681 if (sx_paranoia_check(port, tty->device, "sx_flush_chars"))
1682 return;
1684 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1685 !port->xmit_buf)
1686 return;
1688 save_flags(flags); cli();
1689 port->IER |= IER_TXRDY;
1690 sx_out(port_Board(port), CD186x_CAR, port_No(port));
1691 sx_out(port_Board(port), CD186x_IER, port->IER);
1692 restore_flags(flags);
1696 static int sx_write_room(struct tty_struct * tty)
1698 struct specialix_port *port = (struct specialix_port *)tty->driver_data;
1699 int ret;
1701 if (sx_paranoia_check(port, tty->device, "sx_write_room"))
1702 return 0;
1704 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1705 if (ret < 0)
1706 ret = 0;
1707 return ret;
1711 static int sx_chars_in_buffer(struct tty_struct *tty)
1713 struct specialix_port *port = (struct specialix_port *)tty->driver_data;
1715 if (sx_paranoia_check(port, tty->device, "sx_chars_in_buffer"))
1716 return 0;
1718 return port->xmit_cnt;
1722 static void sx_flush_buffer(struct tty_struct *tty)
1724 struct specialix_port *port = (struct specialix_port *)tty->driver_data;
1725 unsigned long flags;
1727 if (sx_paranoia_check(port, tty->device, "sx_flush_buffer"))
1728 return;
1730 save_flags(flags); cli();
1731 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1732 restore_flags(flags);
1734 wake_up_interruptible(&tty->write_wait);
1735 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1736 tty->ldisc.write_wakeup)
1737 (tty->ldisc.write_wakeup)(tty);
1741 static int sx_get_modem_info(struct specialix_port * port, unsigned int *value)
1743 struct specialix_board * bp;
1744 unsigned char status;
1745 unsigned int result;
1746 unsigned long flags;
1748 bp = port_Board(port);
1749 save_flags(flags); cli();
1750 sx_out(bp, CD186x_CAR, port_No(port));
1751 status = sx_in(bp, CD186x_MSVR);
1752 restore_flags(flags);
1753 #ifdef DEBUG_SPECIALIX
1754 printk (KERN_DEBUG "Got msvr[%d] = %02x, car = %d.\n",
1755 port_No(port), status, sx_in (bp, CD186x_CAR));
1756 printk (KERN_DEBUG "sx_port = %p, port = %p\n", sx_port, port);
1757 #endif
1758 if (SX_CRTSCTS(port->tty)) {
1759 result = /* (status & MSVR_RTS) ? */ TIOCM_DTR /* : 0) */
1760 | ((status & MSVR_DTR) ? TIOCM_RTS : 0)
1761 | ((status & MSVR_CD) ? TIOCM_CAR : 0)
1762 |/* ((status & MSVR_DSR) ? */ TIOCM_DSR /* : 0) */
1763 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1764 } else {
1765 result = /* (status & MSVR_RTS) ? */ TIOCM_RTS /* : 0) */
1766 | ((status & MSVR_DTR) ? TIOCM_DTR : 0)
1767 | ((status & MSVR_CD) ? TIOCM_CAR : 0)
1768 |/* ((status & MSVR_DSR) ? */ TIOCM_DSR /* : 0) */
1769 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1771 put_user(result,(unsigned int *) value);
1772 return 0;
1776 static int sx_set_modem_info(struct specialix_port * port, unsigned int cmd,
1777 unsigned int *value)
1779 int error;
1780 unsigned int arg;
1781 unsigned long flags;
1782 struct specialix_board *bp = port_Board(port);
1784 error = verify_area(VERIFY_READ, value, sizeof(int));
1785 if (error)
1786 return error;
1788 Get_user(arg, (unsigned long *) value);
1789 switch (cmd) {
1790 case TIOCMBIS:
1791 /* if (arg & TIOCM_RTS)
1792 port->MSVR |= MSVR_RTS; */
1793 /* if (arg & TIOCM_DTR)
1794 port->MSVR |= MSVR_DTR; */
1796 if (SX_CRTSCTS(port->tty)) {
1797 if (arg & TIOCM_RTS)
1798 port->MSVR |= MSVR_DTR;
1799 } else {
1800 if (arg & TIOCM_DTR)
1801 port->MSVR |= MSVR_DTR;
1803 break;
1804 case TIOCMBIC:
1805 /* if (arg & TIOCM_RTS)
1806 port->MSVR &= ~MSVR_RTS; */
1807 /* if (arg & TIOCM_DTR)
1808 port->MSVR &= ~MSVR_DTR; */
1809 if (SX_CRTSCTS(port->tty)) {
1810 if (arg & TIOCM_RTS)
1811 port->MSVR &= ~MSVR_DTR;
1812 } else {
1813 if (arg & TIOCM_DTR)
1814 port->MSVR &= ~MSVR_DTR;
1816 break;
1817 case TIOCMSET:
1818 /* port->MSVR = (arg & TIOCM_RTS) ? (port->MSVR | MSVR_RTS) :
1819 (port->MSVR & ~MSVR_RTS); */
1820 /* port->MSVR = (arg & TIOCM_DTR) ? (port->MSVR | MSVR_DTR) :
1821 (port->MSVR & ~MSVR_DTR); */
1822 if (SX_CRTSCTS(port->tty)) {
1823 port->MSVR = (arg & TIOCM_RTS) ?
1824 (port->MSVR | MSVR_DTR) :
1825 (port->MSVR & ~MSVR_DTR);
1826 } else {
1827 port->MSVR = (arg & TIOCM_DTR) ?
1828 (port->MSVR | MSVR_DTR):
1829 (port->MSVR & ~MSVR_DTR);
1831 break;
1832 default:
1833 return -EINVAL;
1835 save_flags(flags); cli();
1836 sx_out(bp, CD186x_CAR, port_No(port));
1837 sx_out(bp, CD186x_MSVR, port->MSVR);
1838 restore_flags(flags);
1839 return 0;
1843 extern inline void sx_send_break(struct specialix_port * port, unsigned long length)
1845 struct specialix_board *bp = port_Board(port);
1846 unsigned long flags;
1848 save_flags(flags); cli();
1849 port->break_length = SPECIALIX_TPS / HZ * length;
1850 port->COR2 |= COR2_ETC;
1851 port->IER |= IER_TXRDY;
1852 sx_out(bp, CD186x_CAR, port_No(port));
1853 sx_out(bp, CD186x_COR2, port->COR2);
1854 sx_out(bp, CD186x_IER, port->IER);
1855 sx_wait_CCR(bp);
1856 sx_out(bp, CD186x_CCR, CCR_CORCHG2);
1857 sx_wait_CCR(bp);
1858 restore_flags(flags);
1862 extern inline int sx_set_serial_info(struct specialix_port * port,
1863 struct serial_struct * newinfo)
1865 struct serial_struct tmp;
1866 struct specialix_board *bp = port_Board(port);
1867 int change_speed;
1868 unsigned long flags;
1869 int error;
1871 error = verify_area(VERIFY_READ, (void *) newinfo, sizeof(tmp));
1872 if (error)
1873 return error;
1875 if (copy_from_user(&tmp, newinfo, sizeof(tmp)))
1876 return -EFAULT;
1878 #if 0
1879 if ((tmp.irq != bp->irq) ||
1880 (tmp.port != bp->base) ||
1881 (tmp.type != PORT_CIRRUS) ||
1882 (tmp.baud_base != (SX_OSCFREQ + CD186x_TPC/2) / CD186x_TPC) ||
1883 (tmp.custom_divisor != 0) ||
1884 (tmp.xmit_fifo_size != CD186x_NFIFO) ||
1885 (tmp.flags & ~SPECIALIX_LEGAL_FLAGS))
1886 return -EINVAL;
1887 #endif
1889 change_speed = ((port->flags & ASYNC_SPD_MASK) !=
1890 (tmp.flags & ASYNC_SPD_MASK));
1891 change_speed |= (tmp.custom_divisor != port->custom_divisor);
1893 if (!capable(CAP_SYS_ADMIN)) {
1894 if ((tmp.close_delay != port->close_delay) ||
1895 (tmp.closing_wait != port->closing_wait) ||
1896 ((tmp.flags & ~ASYNC_USR_MASK) !=
1897 (port->flags & ~ASYNC_USR_MASK)))
1898 return -EPERM;
1899 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
1900 (tmp.flags & ASYNC_USR_MASK));
1901 port->custom_divisor = tmp.custom_divisor;
1902 } else {
1903 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1904 (tmp.flags & ASYNC_FLAGS));
1905 port->close_delay = tmp.close_delay;
1906 port->closing_wait = tmp.closing_wait;
1907 port->custom_divisor = tmp.custom_divisor;
1909 if (change_speed) {
1910 save_flags(flags); cli();
1911 sx_change_speed(bp, port);
1912 restore_flags(flags);
1914 return 0;
1918 extern inline int sx_get_serial_info(struct specialix_port * port,
1919 struct serial_struct * retinfo)
1921 struct serial_struct tmp;
1922 struct specialix_board *bp = port_Board(port);
1923 int error;
1925 error = verify_area(VERIFY_WRITE, (void *) retinfo, sizeof(tmp));
1926 if (error)
1927 return error;
1929 memset(&tmp, 0, sizeof(tmp));
1930 tmp.type = PORT_CIRRUS;
1931 tmp.line = port - sx_port;
1932 tmp.port = bp->base;
1933 tmp.irq = bp->irq;
1934 tmp.flags = port->flags;
1935 tmp.baud_base = (SX_OSCFREQ + CD186x_TPC/2) / CD186x_TPC;
1936 tmp.close_delay = port->close_delay * HZ/100;
1937 tmp.closing_wait = port->closing_wait * HZ/100;
1938 tmp.custom_divisor = port->custom_divisor;
1939 tmp.xmit_fifo_size = CD186x_NFIFO;
1940 if (copy_to_user(retinfo, &tmp, sizeof(tmp)))
1941 return -EFAULT;
1942 return 0;
1946 static int sx_ioctl(struct tty_struct * tty, struct file * filp,
1947 unsigned int cmd, unsigned long arg)
1949 struct specialix_port *port = (struct specialix_port *)tty->driver_data;
1950 int error;
1951 int retval;
1953 if (sx_paranoia_check(port, tty->device, "sx_ioctl"))
1954 return -ENODEV;
1956 switch (cmd) {
1957 case TCSBRK: /* SVID version: non-zero arg --> no break */
1958 retval = tty_check_change(tty);
1959 if (retval)
1960 return retval;
1961 tty_wait_until_sent(tty, 0);
1962 if (!arg)
1963 sx_send_break(port, HZ/4); /* 1/4 second */
1964 return 0;
1965 case TCSBRKP: /* support for POSIX tcsendbreak() */
1966 retval = tty_check_change(tty);
1967 if (retval)
1968 return retval;
1969 tty_wait_until_sent(tty, 0);
1970 sx_send_break(port, arg ? arg*(HZ/10) : HZ/4);
1971 return 0;
1972 case TIOCGSOFTCAR:
1973 error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(long));
1974 if (error)
1975 return error;
1976 put_user(C_CLOCAL(tty) ? 1 : 0,
1977 (unsigned long *) arg);
1978 return 0;
1979 case TIOCSSOFTCAR:
1980 Get_user(arg, (unsigned long *) arg);
1981 tty->termios->c_cflag =
1982 ((tty->termios->c_cflag & ~CLOCAL) |
1983 (arg ? CLOCAL : 0));
1984 return 0;
1985 case TIOCMGET:
1986 error = verify_area(VERIFY_WRITE, (void *) arg,
1987 sizeof(unsigned int));
1988 if (error)
1989 return error;
1990 return sx_get_modem_info(port, (unsigned int *) arg);
1991 case TIOCMBIS:
1992 case TIOCMBIC:
1993 case TIOCMSET:
1994 return sx_set_modem_info(port, cmd, (unsigned int *) arg);
1995 case TIOCGSERIAL:
1996 return sx_get_serial_info(port, (struct serial_struct *) arg);
1997 case TIOCSSERIAL:
1998 return sx_set_serial_info(port, (struct serial_struct *) arg);
1999 default:
2000 return -ENOIOCTLCMD;
2002 return 0;
2006 static void sx_throttle(struct tty_struct * tty)
2008 struct specialix_port *port = (struct specialix_port *)tty->driver_data;
2009 struct specialix_board *bp;
2010 unsigned long flags;
2012 if (sx_paranoia_check(port, tty->device, "sx_throttle"))
2013 return;
2015 bp = port_Board(port);
2017 save_flags(flags); cli();
2019 /* Use DTR instead of RTS ! */
2020 if (SX_CRTSCTS (tty))
2021 port->MSVR &= ~MSVR_DTR;
2022 else {
2023 /* Auch!!! I think the system shouldn't call this then. */
2024 /* Or maybe we're supposed (allowed?) to do our side of hw
2025 handshake anyway, even when hardware handshake is off.
2026 When you see this in your logs, please report.... */
2027 printk (KERN_ERR "sx%d: Need to throttle, but can't (hardware hs is off)\n",
2028 port_No (port));
2030 sx_out(bp, CD186x_CAR, port_No(port));
2031 if (I_IXOFF(tty)) {
2032 sx_wait_CCR(bp);
2033 sx_out(bp, CD186x_CCR, CCR_SSCH2);
2034 sx_wait_CCR(bp);
2036 sx_out(bp, CD186x_MSVR, port->MSVR);
2037 restore_flags(flags);
2041 static void sx_unthrottle(struct tty_struct * tty)
2043 struct specialix_port *port = (struct specialix_port *)tty->driver_data;
2044 struct specialix_board *bp;
2045 unsigned long flags;
2047 if (sx_paranoia_check(port, tty->device, "sx_unthrottle"))
2048 return;
2050 bp = port_Board(port);
2052 save_flags(flags); cli();
2053 /* XXXX Use DTR INSTEAD???? */
2054 if (SX_CRTSCTS(tty)) {
2055 port->MSVR |= MSVR_DTR;
2056 } /* Else clause: see remark in "sx_throttle"... */
2058 sx_out(bp, CD186x_CAR, port_No(port));
2059 if (I_IXOFF(tty)) {
2060 sx_wait_CCR(bp);
2061 sx_out(bp, CD186x_CCR, CCR_SSCH1);
2062 sx_wait_CCR(bp);
2064 sx_out(bp, CD186x_MSVR, port->MSVR);
2065 restore_flags(flags);
2069 static void sx_stop(struct tty_struct * tty)
2071 struct specialix_port *port = (struct specialix_port *)tty->driver_data;
2072 struct specialix_board *bp;
2073 unsigned long flags;
2075 if (sx_paranoia_check(port, tty->device, "sx_stop"))
2076 return;
2078 bp = port_Board(port);
2080 save_flags(flags); cli();
2081 port->IER &= ~IER_TXRDY;
2082 sx_out(bp, CD186x_CAR, port_No(port));
2083 sx_out(bp, CD186x_IER, port->IER);
2084 restore_flags(flags);
2088 static void sx_start(struct tty_struct * tty)
2090 struct specialix_port *port = (struct specialix_port *)tty->driver_data;
2091 struct specialix_board *bp;
2092 unsigned long flags;
2094 if (sx_paranoia_check(port, tty->device, "sx_start"))
2095 return;
2097 bp = port_Board(port);
2099 save_flags(flags); cli();
2100 if (port->xmit_cnt && port->xmit_buf && !(port->IER & IER_TXRDY)) {
2101 port->IER |= IER_TXRDY;
2102 sx_out(bp, CD186x_CAR, port_No(port));
2103 sx_out(bp, CD186x_IER, port->IER);
2105 restore_flags(flags);
2110 * This routine is called from the scheduler tqueue when the interrupt
2111 * routine has signalled that a hangup has occurred. The path of
2112 * hangup processing is:
2114 * serial interrupt routine -> (scheduler tqueue) ->
2115 * do_sx_hangup() -> tty->hangup() -> sx_hangup()
2118 static void do_sx_hangup(void *private_)
2120 struct specialix_port *port = (struct specialix_port *) private_;
2121 struct tty_struct *tty;
2123 tty = port->tty;
2124 if (!tty)
2125 return;
2127 tty_hangup(tty);
2131 static void sx_hangup(struct tty_struct * tty)
2133 struct specialix_port *port = (struct specialix_port *)tty->driver_data;
2134 struct specialix_board *bp;
2136 if (sx_paranoia_check(port, tty->device, "sx_hangup"))
2137 return;
2139 bp = port_Board(port);
2141 sx_shutdown_port(bp, port);
2142 port->event = 0;
2143 port->count = 0;
2144 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2145 port->tty = 0;
2146 wake_up_interruptible(&port->open_wait);
2150 static void sx_set_termios(struct tty_struct * tty, struct termios * old_termios)
2152 struct specialix_port *port = (struct specialix_port *)tty->driver_data;
2153 unsigned long flags;
2155 if (sx_paranoia_check(port, tty->device, "sx_set_termios"))
2156 return;
2158 if (tty->termios->c_cflag == old_termios->c_cflag &&
2159 tty->termios->c_iflag == old_termios->c_iflag)
2160 return;
2162 save_flags(flags); cli();
2163 sx_change_speed(port_Board(port), port);
2164 restore_flags(flags);
2166 if ((old_termios->c_cflag & CRTSCTS) &&
2167 !(tty->termios->c_cflag & CRTSCTS)) {
2168 tty->hw_stopped = 0;
2169 sx_start(tty);
2174 static void do_specialix_bh(void)
2176 run_task_queue(&tq_specialix);
2180 static void do_softint(void *private_)
2182 struct specialix_port *port = (struct specialix_port *) private_;
2183 struct tty_struct *tty;
2185 if(!(tty = port->tty))
2186 return;
2188 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &port->event)) {
2189 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
2190 tty->ldisc.write_wakeup)
2191 (tty->ldisc.write_wakeup)(tty);
2192 wake_up_interruptible(&tty->write_wait);
2197 static int sx_init_drivers(void)
2199 int error;
2200 int i;
2203 if (!(tmp_buf = (unsigned char *) get_free_page(GFP_KERNEL))) {
2204 printk(KERN_ERR "sx: Couldn't get free page.\n");
2205 return 1;
2207 init_bh(SPECIALIX_BH, do_specialix_bh);
2208 memset(&specialix_driver, 0, sizeof(specialix_driver));
2209 specialix_driver.magic = TTY_DRIVER_MAGIC;
2210 specialix_driver.name = "ttyW";
2211 specialix_driver.major = SPECIALIX_NORMAL_MAJOR;
2212 specialix_driver.num = SX_NBOARD * SX_NPORT;
2213 specialix_driver.type = TTY_DRIVER_TYPE_SERIAL;
2214 specialix_driver.subtype = SPECIALIX_TYPE_NORMAL;
2215 specialix_driver.init_termios = tty_std_termios;
2216 specialix_driver.init_termios.c_cflag =
2217 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2218 specialix_driver.flags = TTY_DRIVER_REAL_RAW;
2219 specialix_driver.refcount = &specialix_refcount;
2220 specialix_driver.table = specialix_table;
2221 specialix_driver.termios = specialix_termios;
2222 specialix_driver.termios_locked = specialix_termios_locked;
2224 specialix_driver.open = sx_open;
2225 specialix_driver.close = sx_close;
2226 specialix_driver.write = sx_write;
2227 specialix_driver.put_char = sx_put_char;
2228 specialix_driver.flush_chars = sx_flush_chars;
2229 specialix_driver.write_room = sx_write_room;
2230 specialix_driver.chars_in_buffer = sx_chars_in_buffer;
2231 specialix_driver.flush_buffer = sx_flush_buffer;
2232 specialix_driver.ioctl = sx_ioctl;
2233 specialix_driver.throttle = sx_throttle;
2234 specialix_driver.unthrottle = sx_unthrottle;
2235 specialix_driver.set_termios = sx_set_termios;
2236 specialix_driver.stop = sx_stop;
2237 specialix_driver.start = sx_start;
2238 specialix_driver.hangup = sx_hangup;
2240 specialix_callout_driver = specialix_driver;
2241 specialix_callout_driver.name = "cuw";
2242 specialix_callout_driver.major = SPECIALIX_CALLOUT_MAJOR;
2243 specialix_callout_driver.subtype = SPECIALIX_TYPE_CALLOUT;
2245 if ((error = tty_register_driver(&specialix_driver))) {
2246 free_page((unsigned long)tmp_buf);
2247 printk(KERN_ERR "sx: Couldn't register specialix IO8+ driver, error = %d\n",
2248 error);
2249 return 1;
2251 if ((error = tty_register_driver(&specialix_callout_driver))) {
2252 free_page((unsigned long)tmp_buf);
2253 tty_unregister_driver(&specialix_driver);
2254 printk(KERN_ERR "sx: Couldn't register specialix IO8+ callout driver, error = %d\n",
2255 error);
2256 return 1;
2259 memset(sx_port, 0, sizeof(sx_port));
2260 for (i = 0; i < SX_NPORT * SX_NBOARD; i++) {
2261 sx_port[i].callout_termios = specialix_callout_driver.init_termios;
2262 sx_port[i].normal_termios = specialix_driver.init_termios;
2263 sx_port[i].magic = SPECIALIX_MAGIC;
2264 sx_port[i].tqueue.routine = do_softint;
2265 sx_port[i].tqueue.data = &sx_port[i];
2266 sx_port[i].tqueue_hangup.routine = do_sx_hangup;
2267 sx_port[i].tqueue_hangup.data = &sx_port[i];
2268 sx_port[i].close_delay = 50 * HZ/100;
2269 sx_port[i].closing_wait = 3000 * HZ/100;
2270 init_waitqueue_head(&sx_port[i].open_wait);
2271 init_waitqueue_head(&sx_port[i].close_wait);
2274 return 0;
2278 static void sx_release_drivers(void)
2280 free_page((unsigned long)tmp_buf);
2281 tty_unregister_driver(&specialix_driver);
2282 tty_unregister_driver(&specialix_callout_driver);
2286 #ifndef MODULE
2288 * Called at boot time.
2290 * You can specify IO base for up to SX_NBOARD cards,
2291 * using line "specialix=0xiobase1,0xiobase2,.." at LILO prompt.
2292 * Note that there will be no probing at default
2293 * addresses in this case.
2296 void specialix_setup(char *str, int * ints)
2298 int i;
2300 for (i=0;i<SX_NBOARD;i++) {
2301 sx_board[i].base = 0;
2304 for (i = 1; i <= ints[0]; i++) {
2305 if (i&1)
2306 sx_board[i/2].base = ints[i];
2307 else
2308 sx_board[i/2 -1].irq = ints[i];
2311 #endif
2314 * This routine must be called by kernel at boot time
2316 int specialix_init(void)
2318 int i;
2319 int found = 0;
2321 printk(KERN_INFO "sx: Specialix IO8+ driver v" VERSION ", (c) R.E.Wolff 1997/1998.\n");
2322 printk(KERN_INFO "sx: derived from work (c) D.Gorodchanin 1994-1996.\n");
2323 #ifdef CONFIG_SPECIALIX_RTSCTS
2324 printk (KERN_INFO "sx: DTR/RTS pin is always RTS.\n");
2325 #else
2326 printk (KERN_INFO "sx: DTR/RTS pin is RTS when CRTSCTS is on.\n");
2327 #endif
2329 if (sx_init_drivers())
2330 return -EIO;
2332 for (i = 0; i < SX_NBOARD; i++)
2333 if (sx_board[i].base && !sx_probe(&sx_board[i]))
2334 found++;
2336 #ifdef CONFIG_PCI
2337 if (pci_present()) {
2338 struct pci_dev *pdev = NULL;
2340 i=0;
2341 while (i <= SX_NBOARD) {
2342 if (sx_board[i].flags & SX_BOARD_PRESENT) {
2343 i++;
2344 continue;
2346 pdev = pci_find_device (PCI_VENDOR_ID_SPECIALIX,
2347 PCI_DEVICE_ID_SPECIALIX_IO8,
2348 pdev);
2349 if (!pdev) break;
2351 if (pci_enable_device(pdev))
2352 continue;
2354 sx_board[i].irq = pdev->irq;
2356 sx_board[i].base = pci_resource_start (pdev, 2);
2358 sx_board[i].flags |= SX_BOARD_IS_PCI;
2359 if (!sx_probe(&sx_board[i]))
2360 found ++;
2363 #endif
2365 if (!found) {
2366 sx_release_drivers();
2367 printk(KERN_INFO "sx: No specialix IO8+ boards detected.\n");
2368 return -EIO;
2371 return 0;
2374 #ifdef MODULE
2375 int iobase[SX_NBOARD] = {0,};
2377 int irq [SX_NBOARD] = {0,};
2379 MODULE_PARM(iobase,"1-" __MODULE_STRING(SX_NBOARD) "i");
2380 MODULE_PARM(irq,"1-" __MODULE_STRING(SX_NBOARD) "i");
2383 * You can setup up to 4 boards.
2384 * by specifying "iobase=0xXXX,0xXXX ..." as insmod parameter.
2385 * You should specify the IRQs too in that case "irq=....,...".
2387 * More than 4 boards in one computer is not possible, as the card can
2388 * only use 4 different interrupts.
2391 int init_module(void)
2393 int i;
2395 if (iobase[0] || iobase[1] || iobase[2] || iobase[3]) {
2396 for(i = 0; i < SX_NBOARD; i++) {
2397 sx_board[i].base = iobase[i];
2398 sx_board[i].irq = irq[i];
2402 return specialix_init();
2406 void cleanup_module(void)
2408 int i;
2410 sx_release_drivers();
2411 for (i = 0; i < SX_NBOARD; i++)
2412 if (sx_board[i].flags & SX_BOARD_PRESENT)
2413 sx_release_io_range(&sx_board[i]);
2414 #ifdef SPECIALIX_TIMER
2415 del_timer (&missed_irq_timer);
2416 #endif
2419 #endif /* MODULE */