[PATCH] rocket.c: Fix ldisc ref count handling
[safe/jmp/linux-2.6] / drivers / char / rocket.c
1 /*
2  * RocketPort device driver for Linux
3  *
4  * Written by Theodore Ts'o, 1995, 1996, 1997, 1998, 1999, 2000.
5  * 
6  * Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2003 by Comtrol, Inc.
7  * 
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 /*
24  * Kernel Synchronization:
25  *
26  * This driver has 2 kernel control paths - exception handlers (calls into the driver
27  * from user mode) and the timer bottom half (tasklet).  This is a polled driver, interrupts
28  * are not used.
29  *
30  * Critical data: 
31  * -  rp_table[], accessed through passed "info" pointers, is a global (static) array of 
32  *    serial port state information and the xmit_buf circular buffer.  Protected by 
33  *    a per port spinlock.
34  * -  xmit_flags[], an array of ints indexed by line (port) number, indicating that there
35  *    is data to be transmitted.  Protected by atomic bit operations.
36  * -  rp_num_ports, int indicating number of open ports, protected by atomic operations.
37  * 
38  * rp_write() and rp_write_char() functions use a per port semaphore to protect against
39  * simultaneous access to the same port by more than one process.
40  */
41
42 /****** Defines ******/
43 #ifdef PCI_NUM_RESOURCES
44 #define PCI_BASE_ADDRESS(dev, r) ((dev)->resource[r].start)
45 #else
46 #define PCI_BASE_ADDRESS(dev, r) ((dev)->base_address[r])
47 #endif
48
49 #define ROCKET_PARANOIA_CHECK
50 #define ROCKET_DISABLE_SIMUSAGE
51
52 #undef ROCKET_SOFT_FLOW
53 #undef ROCKET_DEBUG_OPEN
54 #undef ROCKET_DEBUG_INTR
55 #undef ROCKET_DEBUG_WRITE
56 #undef ROCKET_DEBUG_FLOW
57 #undef ROCKET_DEBUG_THROTTLE
58 #undef ROCKET_DEBUG_WAIT_UNTIL_SENT
59 #undef ROCKET_DEBUG_RECEIVE
60 #undef ROCKET_DEBUG_HANGUP
61 #undef REV_PCI_ORDER
62 #undef ROCKET_DEBUG_IO
63
64 #define POLL_PERIOD HZ/100      /*  Polling period .01 seconds (10ms) */
65
66 /****** Kernel includes ******/
67
68 #ifdef MODVERSIONS
69 #include <config/modversions.h>
70 #endif                          
71
72 #include <linux/module.h>
73 #include <linux/errno.h>
74 #include <linux/major.h>
75 #include <linux/kernel.h>
76 #include <linux/signal.h>
77 #include <linux/slab.h>
78 #include <linux/mm.h>
79 #include <linux/sched.h>
80 #include <linux/timer.h>
81 #include <linux/interrupt.h>
82 #include <linux/tty.h>
83 #include <linux/tty_driver.h>
84 #include <linux/tty_flip.h>
85 #include <linux/string.h>
86 #include <linux/fcntl.h>
87 #include <linux/ptrace.h>
88 #include <linux/ioport.h>
89 #include <linux/delay.h>
90 #include <linux/wait.h>
91 #include <linux/pci.h>
92 #include <asm/uaccess.h>
93 #include <asm/atomic.h>
94 #include <linux/bitops.h>
95 #include <linux/spinlock.h>
96 #include <asm/semaphore.h>
97 #include <linux/init.h>
98
99 /****** RocketPort includes ******/
100
101 #include "rocket_int.h"
102 #include "rocket.h"
103
104 #define ROCKET_VERSION "2.09"
105 #define ROCKET_DATE "12-June-2003"
106
107 /****** RocketPort Local Variables ******/
108
109 static struct tty_driver *rocket_driver;
110
111 static struct rocket_version driver_version = { 
112         ROCKET_VERSION, ROCKET_DATE
113 };
114
115 static struct r_port *rp_table[MAX_RP_PORTS];          /*  The main repository of serial port state information. */
116 static unsigned int xmit_flags[NUM_BOARDS];            /*  Bit significant, indicates port had data to transmit. */
117                                                        /*  eg.  Bit 0 indicates port 0 has xmit data, ...        */
118 static atomic_t rp_num_ports_open;                     /*  Number of serial ports open                           */
119 static struct timer_list rocket_timer;
120
121 static unsigned long board1;                           /* ISA addresses, retrieved from rocketport.conf          */
122 static unsigned long board2;
123 static unsigned long board3;
124 static unsigned long board4;
125 static unsigned long controller;
126 static int support_low_speed;
127 static unsigned long modem1;
128 static unsigned long modem2;
129 static unsigned long modem3;
130 static unsigned long modem4;
131 static unsigned long pc104_1[8];
132 static unsigned long pc104_2[8];
133 static unsigned long pc104_3[8];
134 static unsigned long pc104_4[8];
135 static unsigned long *pc104[4] = { pc104_1, pc104_2, pc104_3, pc104_4 };
136
137 static int rp_baud_base[NUM_BOARDS];                   /*  Board config info (Someday make a per-board structure)  */
138 static unsigned long rcktpt_io_addr[NUM_BOARDS];
139 static int rcktpt_type[NUM_BOARDS];
140 static int is_PCI[NUM_BOARDS];
141 static rocketModel_t rocketModel[NUM_BOARDS];
142 static int max_board;
143
144 /*
145  * The following arrays define the interrupt bits corresponding to each AIOP.
146  * These bits are different between the ISA and regular PCI boards and the
147  * Universal PCI boards.
148  */
149
150 static Word_t aiop_intr_bits[AIOP_CTL_SIZE] = {
151         AIOP_INTR_BIT_0,
152         AIOP_INTR_BIT_1,
153         AIOP_INTR_BIT_2,
154         AIOP_INTR_BIT_3
155 };
156
157 static Word_t upci_aiop_intr_bits[AIOP_CTL_SIZE] = {
158         UPCI_AIOP_INTR_BIT_0,
159         UPCI_AIOP_INTR_BIT_1,
160         UPCI_AIOP_INTR_BIT_2,
161         UPCI_AIOP_INTR_BIT_3
162 };
163
164 static Byte_t RData[RDATASIZE] = {
165         0x00, 0x09, 0xf6, 0x82,
166         0x02, 0x09, 0x86, 0xfb,
167         0x04, 0x09, 0x00, 0x0a,
168         0x06, 0x09, 0x01, 0x0a,
169         0x08, 0x09, 0x8a, 0x13,
170         0x0a, 0x09, 0xc5, 0x11,
171         0x0c, 0x09, 0x86, 0x85,
172         0x0e, 0x09, 0x20, 0x0a,
173         0x10, 0x09, 0x21, 0x0a,
174         0x12, 0x09, 0x41, 0xff,
175         0x14, 0x09, 0x82, 0x00,
176         0x16, 0x09, 0x82, 0x7b,
177         0x18, 0x09, 0x8a, 0x7d,
178         0x1a, 0x09, 0x88, 0x81,
179         0x1c, 0x09, 0x86, 0x7a,
180         0x1e, 0x09, 0x84, 0x81,
181         0x20, 0x09, 0x82, 0x7c,
182         0x22, 0x09, 0x0a, 0x0a
183 };
184
185 static Byte_t RRegData[RREGDATASIZE] = {
186         0x00, 0x09, 0xf6, 0x82, /* 00: Stop Rx processor */
187         0x08, 0x09, 0x8a, 0x13, /* 04: Tx software flow control */
188         0x0a, 0x09, 0xc5, 0x11, /* 08: XON char */
189         0x0c, 0x09, 0x86, 0x85, /* 0c: XANY */
190         0x12, 0x09, 0x41, 0xff, /* 10: Rx mask char */
191         0x14, 0x09, 0x82, 0x00, /* 14: Compare/Ignore #0 */
192         0x16, 0x09, 0x82, 0x7b, /* 18: Compare #1 */
193         0x18, 0x09, 0x8a, 0x7d, /* 1c: Compare #2 */
194         0x1a, 0x09, 0x88, 0x81, /* 20: Interrupt #1 */
195         0x1c, 0x09, 0x86, 0x7a, /* 24: Ignore/Replace #1 */
196         0x1e, 0x09, 0x84, 0x81, /* 28: Interrupt #2 */
197         0x20, 0x09, 0x82, 0x7c, /* 2c: Ignore/Replace #2 */
198         0x22, 0x09, 0x0a, 0x0a  /* 30: Rx FIFO Enable */
199 };
200
201 static CONTROLLER_T sController[CTL_SIZE] = {
202         {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0},
203          {0, 0, 0, 0}, {-1, -1, -1, -1}, {0, 0, 0, 0}},
204         {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0},
205          {0, 0, 0, 0}, {-1, -1, -1, -1}, {0, 0, 0, 0}},
206         {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0},
207          {0, 0, 0, 0}, {-1, -1, -1, -1}, {0, 0, 0, 0}},
208         {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0},
209          {0, 0, 0, 0}, {-1, -1, -1, -1}, {0, 0, 0, 0}}
210 };
211
212 static Byte_t sBitMapClrTbl[8] = {
213         0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f
214 };
215
216 static Byte_t sBitMapSetTbl[8] = {
217         0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
218 };
219
220 static int sClockPrescale = 0x14;
221
222 /*
223  *  Line number is the ttySIx number (x), the Minor number.  We 
224  *  assign them sequentially, starting at zero.  The following 
225  *  array keeps track of the line number assigned to a given board/aiop/channel.
226  */
227 static unsigned char lineNumbers[MAX_RP_PORTS];
228 static unsigned long nextLineNumber;
229
230 /*****  RocketPort Static Prototypes   *********/
231 static int __init init_ISA(int i);
232 static void rp_wait_until_sent(struct tty_struct *tty, int timeout);
233 static void rp_flush_buffer(struct tty_struct *tty);
234 static void rmSpeakerReset(CONTROLLER_T * CtlP, unsigned long model);
235 static unsigned char GetLineNumber(int ctrl, int aiop, int ch);
236 static unsigned char SetLineNumber(int ctrl, int aiop, int ch);
237 static void rp_start(struct tty_struct *tty);
238 static int sInitChan(CONTROLLER_T * CtlP, CHANNEL_T * ChP, int AiopNum,
239                      int ChanNum);
240 static void sSetInterfaceMode(CHANNEL_T * ChP, Byte_t mode);
241 static void sFlushRxFIFO(CHANNEL_T * ChP);
242 static void sFlushTxFIFO(CHANNEL_T * ChP);
243 static void sEnInterrupts(CHANNEL_T * ChP, Word_t Flags);
244 static void sDisInterrupts(CHANNEL_T * ChP, Word_t Flags);
245 static void sModemReset(CONTROLLER_T * CtlP, int chan, int on);
246 static void sPCIModemReset(CONTROLLER_T * CtlP, int chan, int on);
247 static int sWriteTxPrioByte(CHANNEL_T * ChP, Byte_t Data);
248 static int sPCIInitController(CONTROLLER_T * CtlP, int CtlNum,
249                               ByteIO_t * AiopIOList, int AiopIOListSize,
250                               WordIO_t ConfigIO, int IRQNum, Byte_t Frequency,
251                               int PeriodicOnly, int altChanRingIndicator,
252                               int UPCIRingInd);
253 static int sInitController(CONTROLLER_T * CtlP, int CtlNum, ByteIO_t MudbacIO,
254                            ByteIO_t * AiopIOList, int AiopIOListSize,
255                            int IRQNum, Byte_t Frequency, int PeriodicOnly);
256 static int sReadAiopID(ByteIO_t io);
257 static int sReadAiopNumChan(WordIO_t io);
258
259 #ifdef MODULE
260 MODULE_AUTHOR("Theodore Ts'o");
261 MODULE_DESCRIPTION("Comtrol RocketPort driver");
262 module_param(board1, ulong, 0);
263 MODULE_PARM_DESC(board1, "I/O port for (ISA) board #1");
264 module_param(board2, ulong, 0);
265 MODULE_PARM_DESC(board2, "I/O port for (ISA) board #2");
266 module_param(board3, ulong, 0);
267 MODULE_PARM_DESC(board3, "I/O port for (ISA) board #3");
268 module_param(board4, ulong, 0);
269 MODULE_PARM_DESC(board4, "I/O port for (ISA) board #4");
270 module_param(controller, ulong, 0);
271 MODULE_PARM_DESC(controller, "I/O port for (ISA) rocketport controller");
272 module_param(support_low_speed, bool, 0);
273 MODULE_PARM_DESC(support_low_speed, "1 means support 50 baud, 0 means support 460400 baud");
274 module_param(modem1, ulong, 0);
275 MODULE_PARM_DESC(modem1, "1 means (ISA) board #1 is a RocketModem");
276 module_param(modem2, ulong, 0);
277 MODULE_PARM_DESC(modem2, "1 means (ISA) board #2 is a RocketModem");
278 module_param(modem3, ulong, 0);
279 MODULE_PARM_DESC(modem3, "1 means (ISA) board #3 is a RocketModem");
280 module_param(modem4, ulong, 0);
281 MODULE_PARM_DESC(modem4, "1 means (ISA) board #4 is a RocketModem");
282 module_param_array(pc104_1, ulong, NULL, 0);
283 MODULE_PARM_DESC(pc104_1, "set interface types for ISA(PC104) board #1 (e.g. pc104_1=232,232,485,485,...");
284 module_param_array(pc104_2, ulong, NULL, 0);
285 MODULE_PARM_DESC(pc104_2, "set interface types for ISA(PC104) board #2 (e.g. pc104_2=232,232,485,485,...");
286 module_param_array(pc104_3, ulong, NULL, 0);
287 MODULE_PARM_DESC(pc104_3, "set interface types for ISA(PC104) board #3 (e.g. pc104_3=232,232,485,485,...");
288 module_param_array(pc104_4, ulong, NULL, 0);
289 MODULE_PARM_DESC(pc104_4, "set interface types for ISA(PC104) board #4 (e.g. pc104_4=232,232,485,485,...");
290
291 int rp_init(void);
292 static void rp_cleanup_module(void);
293
294 module_init(rp_init);
295 module_exit(rp_cleanup_module);
296
297 #endif
298
299 #ifdef MODULE_LICENSE
300 MODULE_LICENSE("Dual BSD/GPL");
301 #endif
302
303 /*************************************************************************/
304 /*                     Module code starts here                           */
305
306 static inline int rocket_paranoia_check(struct r_port *info,
307                                         const char *routine)
308 {
309 #ifdef ROCKET_PARANOIA_CHECK
310         if (!info)
311                 return 1;
312         if (info->magic != RPORT_MAGIC) {
313                 printk(KERN_INFO "Warning: bad magic number for rocketport struct in %s\n",
314                      routine);
315                 return 1;
316         }
317 #endif
318         return 0;
319 }
320
321
322 /*  Serial port receive data function.  Called (from timer poll) when an AIOPIC signals 
323  *  that receive data is present on a serial port.  Pulls data from FIFO, moves it into the 
324  *  tty layer.  
325  */
326 static void rp_do_receive(struct r_port *info,
327                           struct tty_struct *tty,
328                           CHANNEL_t * cp, unsigned int ChanStatus)
329 {
330         unsigned int CharNStat;
331         int ToRecv, wRecv, space = 0, count;
332         unsigned char *cbuf;
333         char *fbuf;
334         struct tty_ldisc *ld;
335
336         ld = tty_ldisc_ref(tty);
337
338         ToRecv = sGetRxCnt(cp);
339         if (ld)
340                 space = ld->receive_room(tty);
341         if (space > 2 * TTY_FLIPBUF_SIZE)
342                 space = 2 * TTY_FLIPBUF_SIZE;
343         cbuf = tty->flip.char_buf;
344         fbuf = tty->flip.flag_buf;
345         count = 0;
346 #ifdef ROCKET_DEBUG_INTR
347         printk(KERN_INFO "rp_do_receive(%d, %d)...", ToRecv, space);
348 #endif
349
350         /*
351          * determine how many we can actually read in.  If we can't
352          * read any in then we have a software overrun condition.
353          */
354         if (ToRecv > space)
355                 ToRecv = space;
356
357         if (ToRecv <= 0)
358                 goto done;
359
360         /*
361          * if status indicates there are errored characters in the
362          * FIFO, then enter status mode (a word in FIFO holds
363          * character and status).
364          */
365         if (ChanStatus & (RXFOVERFL | RXBREAK | RXFRAME | RXPARITY)) {
366                 if (!(ChanStatus & STATMODE)) {
367 #ifdef ROCKET_DEBUG_RECEIVE
368                         printk(KERN_INFO "Entering STATMODE...");
369 #endif
370                         ChanStatus |= STATMODE;
371                         sEnRxStatusMode(cp);
372                 }
373         }
374
375         /* 
376          * if we previously entered status mode, then read down the
377          * FIFO one word at a time, pulling apart the character and
378          * the status.  Update error counters depending on status
379          */
380         if (ChanStatus & STATMODE) {
381 #ifdef ROCKET_DEBUG_RECEIVE
382                 printk(KERN_INFO "Ignore %x, read %x...", info->ignore_status_mask,
383                        info->read_status_mask);
384 #endif
385                 while (ToRecv) {
386                         CharNStat = sInW(sGetTxRxDataIO(cp));
387 #ifdef ROCKET_DEBUG_RECEIVE
388                         printk(KERN_INFO "%x...", CharNStat);
389 #endif
390                         if (CharNStat & STMBREAKH)
391                                 CharNStat &= ~(STMFRAMEH | STMPARITYH);
392                         if (CharNStat & info->ignore_status_mask) {
393                                 ToRecv--;
394                                 continue;
395                         }
396                         CharNStat &= info->read_status_mask;
397                         if (CharNStat & STMBREAKH)
398                                 *fbuf++ = TTY_BREAK;
399                         else if (CharNStat & STMPARITYH)
400                                 *fbuf++ = TTY_PARITY;
401                         else if (CharNStat & STMFRAMEH)
402                                 *fbuf++ = TTY_FRAME;
403                         else if (CharNStat & STMRCVROVRH)
404                                 *fbuf++ = TTY_OVERRUN;
405                         else
406                                 *fbuf++ = 0;
407                         *cbuf++ = CharNStat & 0xff;
408                         count++;
409                         ToRecv--;
410                 }
411
412                 /*
413                  * after we've emptied the FIFO in status mode, turn
414                  * status mode back off
415                  */
416                 if (sGetRxCnt(cp) == 0) {
417 #ifdef ROCKET_DEBUG_RECEIVE
418                         printk(KERN_INFO "Status mode off.\n");
419 #endif
420                         sDisRxStatusMode(cp);
421                 }
422         } else {
423                 /*
424                  * we aren't in status mode, so read down the FIFO two
425                  * characters at time by doing repeated word IO
426                  * transfer.
427                  */
428                 wRecv = ToRecv >> 1;
429                 if (wRecv)
430                         sInStrW(sGetTxRxDataIO(cp), (unsigned short *) cbuf, wRecv);
431                 if (ToRecv & 1)
432                         cbuf[ToRecv - 1] = sInB(sGetTxRxDataIO(cp));
433                 memset(fbuf, 0, ToRecv);
434                 cbuf += ToRecv;
435                 fbuf += ToRecv;
436                 count += ToRecv;
437         }
438         /*  Push the data up to the tty layer */
439         ld->receive_buf(tty, tty->flip.char_buf, tty->flip.flag_buf, count);
440 done:
441         tty_ldisc_deref(ld);
442 }
443
444 /*
445  *  Serial port transmit data function.  Called from the timer polling loop as a 
446  *  result of a bit set in xmit_flags[], indicating data (from the tty layer) is ready
447  *  to be sent out the serial port.  Data is buffered in rp_table[line].xmit_buf, it is 
448  *  moved to the port's xmit FIFO.  *info is critical data, protected by spinlocks.
449  */
450 static void rp_do_transmit(struct r_port *info)
451 {
452         int c;
453         CHANNEL_t *cp = &info->channel;
454         struct tty_struct *tty;
455         unsigned long flags;
456
457 #ifdef ROCKET_DEBUG_INTR
458         printk(KERN_INFO "rp_do_transmit ");
459 #endif
460         if (!info)
461                 return;
462         if (!info->tty) {
463                 printk(KERN_INFO  "rp: WARNING rp_do_transmit called with info->tty==NULL\n");
464                 clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
465                 return;
466         }
467
468         spin_lock_irqsave(&info->slock, flags);
469         tty = info->tty;
470         info->xmit_fifo_room = TXFIFO_SIZE - sGetTxCnt(cp);
471
472         /*  Loop sending data to FIFO until done or FIFO full */
473         while (1) {
474                 if (tty->stopped || tty->hw_stopped)
475                         break;
476                 c = min(info->xmit_fifo_room, min(info->xmit_cnt, XMIT_BUF_SIZE - info->xmit_tail));
477                 if (c <= 0 || info->xmit_fifo_room <= 0)
478                         break;
479                 sOutStrW(sGetTxRxDataIO(cp), (unsigned short *) (info->xmit_buf + info->xmit_tail), c / 2);
480                 if (c & 1)
481                         sOutB(sGetTxRxDataIO(cp), info->xmit_buf[info->xmit_tail + c - 1]);
482                 info->xmit_tail += c;
483                 info->xmit_tail &= XMIT_BUF_SIZE - 1;
484                 info->xmit_cnt -= c;
485                 info->xmit_fifo_room -= c;
486 #ifdef ROCKET_DEBUG_INTR
487                 printk(KERN_INFO "tx %d chars...", c);
488 #endif
489         }
490
491         if (info->xmit_cnt == 0)
492                 clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
493
494         if (info->xmit_cnt < WAKEUP_CHARS) {
495                 tty_wakeup(tty);
496                 wake_up_interruptible(&tty->write_wait);
497 #ifdef ROCKETPORT_HAVE_POLL_WAIT
498                 wake_up_interruptible(&tty->poll_wait);
499 #endif
500         }
501
502         spin_unlock_irqrestore(&info->slock, flags);
503
504 #ifdef ROCKET_DEBUG_INTR
505         printk(KERN_INFO "(%d,%d,%d,%d)...", info->xmit_cnt, info->xmit_head,
506                info->xmit_tail, info->xmit_fifo_room);
507 #endif
508 }
509
510 /*
511  *  Called when a serial port signals it has read data in it's RX FIFO.
512  *  It checks what interrupts are pending and services them, including
513  *  receiving serial data.  
514  */
515 static void rp_handle_port(struct r_port *info)
516 {
517         CHANNEL_t *cp;
518         struct tty_struct *tty;
519         unsigned int IntMask, ChanStatus;
520
521         if (!info)
522                 return;
523
524         if ((info->flags & ROCKET_INITIALIZED) == 0) {
525                 printk(KERN_INFO "rp: WARNING: rp_handle_port called with info->flags & NOT_INIT\n");
526                 return;
527         }
528         if (!info->tty) {
529                 printk(KERN_INFO "rp: WARNING: rp_handle_port called with info->tty==NULL\n");
530                 return;
531         }
532         cp = &info->channel;
533         tty = info->tty;
534
535         IntMask = sGetChanIntID(cp) & info->intmask;
536 #ifdef ROCKET_DEBUG_INTR
537         printk(KERN_INFO "rp_interrupt %02x...", IntMask);
538 #endif
539         ChanStatus = sGetChanStatus(cp);
540         if (IntMask & RXF_TRIG) {       /* Rx FIFO trigger level */
541                 rp_do_receive(info, tty, cp, ChanStatus);
542         }
543         if (IntMask & DELTA_CD) {       /* CD change  */
544 #if (defined(ROCKET_DEBUG_OPEN) || defined(ROCKET_DEBUG_INTR) || defined(ROCKET_DEBUG_HANGUP))
545                 printk(KERN_INFO "ttyR%d CD now %s...", info->line,
546                        (ChanStatus & CD_ACT) ? "on" : "off");
547 #endif
548                 if (!(ChanStatus & CD_ACT) && info->cd_status) {
549 #ifdef ROCKET_DEBUG_HANGUP
550                         printk(KERN_INFO "CD drop, calling hangup.\n");
551 #endif
552                         tty_hangup(tty);
553                 }
554                 info->cd_status = (ChanStatus & CD_ACT) ? 1 : 0;
555                 wake_up_interruptible(&info->open_wait);
556         }
557 #ifdef ROCKET_DEBUG_INTR
558         if (IntMask & DELTA_CTS) {      /* CTS change */
559                 printk(KERN_INFO "CTS change...\n");
560         }
561         if (IntMask & DELTA_DSR) {      /* DSR change */
562                 printk(KERN_INFO "DSR change...\n");
563         }
564 #endif
565 }
566
567 /*
568  *  The top level polling routine.  Repeats every 1/100 HZ (10ms).
569  */
570 static void rp_do_poll(unsigned long dummy)
571 {
572         CONTROLLER_t *ctlp;
573         int ctrl, aiop, ch, line, i;
574         unsigned int xmitmask;
575         unsigned int CtlMask;
576         unsigned char AiopMask;
577         Word_t bit;
578
579         /*  Walk through all the boards (ctrl's) */
580         for (ctrl = 0; ctrl < max_board; ctrl++) {
581                 if (rcktpt_io_addr[ctrl] <= 0)
582                         continue;
583
584                 /*  Get a ptr to the board's control struct */
585                 ctlp = sCtlNumToCtlPtr(ctrl);
586
587                 /*  Get the interupt status from the board */
588 #ifdef CONFIG_PCI
589                 if (ctlp->BusType == isPCI)
590                         CtlMask = sPCIGetControllerIntStatus(ctlp);
591                 else
592 #endif
593                         CtlMask = sGetControllerIntStatus(ctlp);
594
595                 /*  Check if any AIOP read bits are set */
596                 for (aiop = 0; CtlMask; aiop++) {
597                         bit = ctlp->AiopIntrBits[aiop];
598                         if (CtlMask & bit) {
599                                 CtlMask &= ~bit;
600                                 AiopMask = sGetAiopIntStatus(ctlp, aiop);
601
602                                 /*  Check if any port read bits are set */
603                                 for (ch = 0; AiopMask;  AiopMask >>= 1, ch++) {
604                                         if (AiopMask & 1) {
605
606                                                 /*  Get the line number (/dev/ttyRx number). */
607                                                 /*  Read the data from the port. */
608                                                 line = GetLineNumber(ctrl, aiop, ch);
609                                                 rp_handle_port(rp_table[line]);
610                                         }
611                                 }
612                         }
613                 }
614
615                 xmitmask = xmit_flags[ctrl];
616
617                 /*
618                  *  xmit_flags contains bit-significant flags, indicating there is data
619                  *  to xmit on the port. Bit 0 is port 0 on this board, bit 1 is port 
620                  *  1, ... (32 total possible).  The variable i has the aiop and ch 
621                  *  numbers encoded in it (port 0-7 are aiop0, 8-15 are aiop1, etc).
622                  */
623                 if (xmitmask) {
624                         for (i = 0; i < rocketModel[ctrl].numPorts; i++) {
625                                 if (xmitmask & (1 << i)) {
626                                         aiop = (i & 0x18) >> 3;
627                                         ch = i & 0x07;
628                                         line = GetLineNumber(ctrl, aiop, ch);
629                                         rp_do_transmit(rp_table[line]);
630                                 }
631                         }
632                 }
633         }
634
635         /*
636          * Reset the timer so we get called at the next clock tick (10ms).
637          */
638         if (atomic_read(&rp_num_ports_open))
639                 mod_timer(&rocket_timer, jiffies + POLL_PERIOD);
640 }
641
642 /*
643  *  Initializes the r_port structure for a port, as well as enabling the port on 
644  *  the board.  
645  *  Inputs:  board, aiop, chan numbers
646  */
647 static void init_r_port(int board, int aiop, int chan, struct pci_dev *pci_dev)
648 {
649         unsigned rocketMode;
650         struct r_port *info;
651         int line;
652         CONTROLLER_T *ctlp;
653
654         /*  Get the next available line number */
655         line = SetLineNumber(board, aiop, chan);
656
657         ctlp = sCtlNumToCtlPtr(board);
658
659         /*  Get a r_port struct for the port, fill it in and save it globally, indexed by line number */
660         info = kmalloc(sizeof (struct r_port), GFP_KERNEL);
661         if (!info) {
662                 printk(KERN_INFO "Couldn't allocate info struct for line #%d\n", line);
663                 return;
664         }
665         memset(info, 0, sizeof (struct r_port));
666
667         info->magic = RPORT_MAGIC;
668         info->line = line;
669         info->ctlp = ctlp;
670         info->board = board;
671         info->aiop = aiop;
672         info->chan = chan;
673         info->closing_wait = 3000;
674         info->close_delay = 50;
675         init_waitqueue_head(&info->open_wait);
676         init_waitqueue_head(&info->close_wait);
677         info->flags &= ~ROCKET_MODE_MASK;
678         switch (pc104[board][line]) {
679         case 422:
680                 info->flags |= ROCKET_MODE_RS422;
681                 break;
682         case 485:
683                 info->flags |= ROCKET_MODE_RS485;
684                 break;
685         case 232:
686         default:
687                 info->flags |= ROCKET_MODE_RS232;
688                 break;
689         }
690
691         info->intmask = RXF_TRIG | TXFIFO_MT | SRC_INT | DELTA_CD | DELTA_CTS | DELTA_DSR;
692         if (sInitChan(ctlp, &info->channel, aiop, chan) == 0) {
693                 printk(KERN_INFO "RocketPort sInitChan(%d, %d, %d) failed!\n", board, aiop, chan);
694                 kfree(info);
695                 return;
696         }
697
698         rocketMode = info->flags & ROCKET_MODE_MASK;
699
700         if ((info->flags & ROCKET_RTS_TOGGLE) || (rocketMode == ROCKET_MODE_RS485))
701                 sEnRTSToggle(&info->channel);
702         else
703                 sDisRTSToggle(&info->channel);
704
705         if (ctlp->boardType == ROCKET_TYPE_PC104) {
706                 switch (rocketMode) {
707                 case ROCKET_MODE_RS485:
708                         sSetInterfaceMode(&info->channel, InterfaceModeRS485);
709                         break;
710                 case ROCKET_MODE_RS422:
711                         sSetInterfaceMode(&info->channel, InterfaceModeRS422);
712                         break;
713                 case ROCKET_MODE_RS232:
714                 default:
715                         if (info->flags & ROCKET_RTS_TOGGLE)
716                                 sSetInterfaceMode(&info->channel, InterfaceModeRS232T);
717                         else
718                                 sSetInterfaceMode(&info->channel, InterfaceModeRS232);
719                         break;
720                 }
721         }
722         spin_lock_init(&info->slock);
723         sema_init(&info->write_sem, 1);
724         rp_table[line] = info;
725         if (pci_dev)
726                 tty_register_device(rocket_driver, line, &pci_dev->dev);
727 }
728
729 /*
730  *  Configures a rocketport port according to its termio settings.  Called from 
731  *  user mode into the driver (exception handler).  *info CD manipulation is spinlock protected.
732  */
733 static void configure_r_port(struct r_port *info,
734                              struct termios *old_termios)
735 {
736         unsigned cflag;
737         unsigned long flags;
738         unsigned rocketMode;
739         int bits, baud, divisor;
740         CHANNEL_t *cp;
741
742         if (!info->tty || !info->tty->termios)
743                 return;
744         cp = &info->channel;
745         cflag = info->tty->termios->c_cflag;
746
747         /* Byte size and parity */
748         if ((cflag & CSIZE) == CS8) {
749                 sSetData8(cp);
750                 bits = 10;
751         } else {
752                 sSetData7(cp);
753                 bits = 9;
754         }
755         if (cflag & CSTOPB) {
756                 sSetStop2(cp);
757                 bits++;
758         } else {
759                 sSetStop1(cp);
760         }
761
762         if (cflag & PARENB) {
763                 sEnParity(cp);
764                 bits++;
765                 if (cflag & PARODD) {
766                         sSetOddParity(cp);
767                 } else {
768                         sSetEvenParity(cp);
769                 }
770         } else {
771                 sDisParity(cp);
772         }
773
774         /* baud rate */
775         baud = tty_get_baud_rate(info->tty);
776         if (!baud)
777                 baud = 9600;
778         divisor = ((rp_baud_base[info->board] + (baud >> 1)) / baud) - 1;
779         if ((divisor >= 8192 || divisor < 0) && old_termios) {
780                 info->tty->termios->c_cflag &= ~CBAUD;
781                 info->tty->termios->c_cflag |=
782                     (old_termios->c_cflag & CBAUD);
783                 baud = tty_get_baud_rate(info->tty);
784                 if (!baud)
785                         baud = 9600;
786                 divisor = (rp_baud_base[info->board] / baud) - 1;
787         }
788         if (divisor >= 8192 || divisor < 0) {
789                 baud = 9600;
790                 divisor = (rp_baud_base[info->board] / baud) - 1;
791         }
792         info->cps = baud / bits;
793         sSetBaud(cp, divisor);
794
795         if (cflag & CRTSCTS) {
796                 info->intmask |= DELTA_CTS;
797                 sEnCTSFlowCtl(cp);
798         } else {
799                 info->intmask &= ~DELTA_CTS;
800                 sDisCTSFlowCtl(cp);
801         }
802         if (cflag & CLOCAL) {
803                 info->intmask &= ~DELTA_CD;
804         } else {
805                 spin_lock_irqsave(&info->slock, flags);
806                 if (sGetChanStatus(cp) & CD_ACT)
807                         info->cd_status = 1;
808                 else
809                         info->cd_status = 0;
810                 info->intmask |= DELTA_CD;
811                 spin_unlock_irqrestore(&info->slock, flags);
812         }
813
814         /*
815          * Handle software flow control in the board
816          */
817 #ifdef ROCKET_SOFT_FLOW
818         if (I_IXON(info->tty)) {
819                 sEnTxSoftFlowCtl(cp);
820                 if (I_IXANY(info->tty)) {
821                         sEnIXANY(cp);
822                 } else {
823                         sDisIXANY(cp);
824                 }
825                 sSetTxXONChar(cp, START_CHAR(info->tty));
826                 sSetTxXOFFChar(cp, STOP_CHAR(info->tty));
827         } else {
828                 sDisTxSoftFlowCtl(cp);
829                 sDisIXANY(cp);
830                 sClrTxXOFF(cp);
831         }
832 #endif
833
834         /*
835          * Set up ignore/read mask words
836          */
837         info->read_status_mask = STMRCVROVRH | 0xFF;
838         if (I_INPCK(info->tty))
839                 info->read_status_mask |= STMFRAMEH | STMPARITYH;
840         if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
841                 info->read_status_mask |= STMBREAKH;
842
843         /*
844          * Characters to ignore
845          */
846         info->ignore_status_mask = 0;
847         if (I_IGNPAR(info->tty))
848                 info->ignore_status_mask |= STMFRAMEH | STMPARITYH;
849         if (I_IGNBRK(info->tty)) {
850                 info->ignore_status_mask |= STMBREAKH;
851                 /*
852                  * If we're ignoring parity and break indicators,
853                  * ignore overruns too.  (For real raw support).
854                  */
855                 if (I_IGNPAR(info->tty))
856                         info->ignore_status_mask |= STMRCVROVRH;
857         }
858
859         rocketMode = info->flags & ROCKET_MODE_MASK;
860
861         if ((info->flags & ROCKET_RTS_TOGGLE)
862             || (rocketMode == ROCKET_MODE_RS485))
863                 sEnRTSToggle(cp);
864         else
865                 sDisRTSToggle(cp);
866
867         sSetRTS(&info->channel);
868
869         if (cp->CtlP->boardType == ROCKET_TYPE_PC104) {
870                 switch (rocketMode) {
871                 case ROCKET_MODE_RS485:
872                         sSetInterfaceMode(cp, InterfaceModeRS485);
873                         break;
874                 case ROCKET_MODE_RS422:
875                         sSetInterfaceMode(cp, InterfaceModeRS422);
876                         break;
877                 case ROCKET_MODE_RS232:
878                 default:
879                         if (info->flags & ROCKET_RTS_TOGGLE)
880                                 sSetInterfaceMode(cp, InterfaceModeRS232T);
881                         else
882                                 sSetInterfaceMode(cp, InterfaceModeRS232);
883                         break;
884                 }
885         }
886 }
887
888 /*  info->count is considered critical, protected by spinlocks.  */
889 static int block_til_ready(struct tty_struct *tty, struct file *filp,
890                            struct r_port *info)
891 {
892         DECLARE_WAITQUEUE(wait, current);
893         int retval;
894         int do_clocal = 0, extra_count = 0;
895         unsigned long flags;
896
897         /*
898          * If the device is in the middle of being closed, then block
899          * until it's done, and then try again.
900          */
901         if (tty_hung_up_p(filp))
902                 return ((info->flags & ROCKET_HUP_NOTIFY) ? -EAGAIN : -ERESTARTSYS);
903         if (info->flags & ROCKET_CLOSING) {
904                 interruptible_sleep_on(&info->close_wait);
905                 return ((info->flags & ROCKET_HUP_NOTIFY) ? -EAGAIN : -ERESTARTSYS);
906         }
907
908         /*
909          * If non-blocking mode is set, or the port is not enabled,
910          * then make the check up front and then exit.
911          */
912         if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
913                 info->flags |= ROCKET_NORMAL_ACTIVE;
914                 return 0;
915         }
916         if (tty->termios->c_cflag & CLOCAL)
917                 do_clocal = 1;
918
919         /*
920          * Block waiting for the carrier detect and the line to become free.  While we are in
921          * this loop, info->count is dropped by one, so that rp_close() knows when to free things.  
922          * We restore it upon exit, either normal or abnormal.
923          */
924         retval = 0;
925         add_wait_queue(&info->open_wait, &wait);
926 #ifdef ROCKET_DEBUG_OPEN
927         printk(KERN_INFO "block_til_ready before block: ttyR%d, count = %d\n", info->line, info->count);
928 #endif
929         spin_lock_irqsave(&info->slock, flags);
930
931 #ifdef ROCKET_DISABLE_SIMUSAGE
932         info->flags |= ROCKET_NORMAL_ACTIVE;
933 #else
934         if (!tty_hung_up_p(filp)) {
935                 extra_count = 1;
936                 info->count--;
937         }
938 #endif
939         info->blocked_open++;
940
941         spin_unlock_irqrestore(&info->slock, flags);
942
943         while (1) {
944                 if (tty->termios->c_cflag & CBAUD) {
945                         sSetDTR(&info->channel);
946                         sSetRTS(&info->channel);
947                 }
948                 set_current_state(TASK_INTERRUPTIBLE);
949                 if (tty_hung_up_p(filp) || !(info->flags & ROCKET_INITIALIZED)) {
950                         if (info->flags & ROCKET_HUP_NOTIFY)
951                                 retval = -EAGAIN;
952                         else
953                                 retval = -ERESTARTSYS;
954                         break;
955                 }
956                 if (!(info->flags & ROCKET_CLOSING) && (do_clocal || (sGetChanStatusLo(&info->channel) & CD_ACT)))
957                         break;
958                 if (signal_pending(current)) {
959                         retval = -ERESTARTSYS;
960                         break;
961                 }
962 #ifdef ROCKET_DEBUG_OPEN
963                 printk(KERN_INFO "block_til_ready blocking: ttyR%d, count = %d, flags=0x%0x\n",
964                      info->line, info->count, info->flags);
965 #endif
966                 schedule();     /*  Don't hold spinlock here, will hang PC */
967         }
968         current->state = TASK_RUNNING;
969         remove_wait_queue(&info->open_wait, &wait);
970
971         spin_lock_irqsave(&info->slock, flags);
972
973         if (extra_count)
974                 info->count++;
975         info->blocked_open--;
976
977         spin_unlock_irqrestore(&info->slock, flags);
978
979 #ifdef ROCKET_DEBUG_OPEN
980         printk(KERN_INFO "block_til_ready after blocking: ttyR%d, count = %d\n",
981                info->line, info->count);
982 #endif
983         if (retval)
984                 return retval;
985         info->flags |= ROCKET_NORMAL_ACTIVE;
986         return 0;
987 }
988
989 /*
990  *  Exception handler that opens a serial port.  Creates xmit_buf storage, fills in 
991  *  port's r_port struct.  Initializes the port hardware.  
992  */
993 static int rp_open(struct tty_struct *tty, struct file *filp)
994 {
995         struct r_port *info;
996         int line = 0, retval;
997         CHANNEL_t *cp;
998         unsigned long page;
999
1000         line = TTY_GET_LINE(tty);
1001         if ((line < 0) || (line >= MAX_RP_PORTS) || ((info = rp_table[line]) == NULL))
1002                 return -ENXIO;
1003
1004         page = __get_free_page(GFP_KERNEL);
1005         if (!page)
1006                 return -ENOMEM;
1007
1008         if (info->flags & ROCKET_CLOSING) {
1009                 interruptible_sleep_on(&info->close_wait);
1010                 free_page(page);
1011                 return ((info->flags & ROCKET_HUP_NOTIFY) ? -EAGAIN : -ERESTARTSYS);
1012         }
1013
1014         /*
1015          * We must not sleep from here until the port is marked fully in use.
1016          */
1017         if (info->xmit_buf)
1018                 free_page(page);
1019         else
1020                 info->xmit_buf = (unsigned char *) page;
1021
1022         tty->driver_data = info;
1023         info->tty = tty;
1024
1025         if (info->count++ == 0) {
1026                 atomic_inc(&rp_num_ports_open);
1027
1028 #ifdef ROCKET_DEBUG_OPEN
1029                 printk(KERN_INFO "rocket mod++ = %d...", atomic_read(&rp_num_ports_open));
1030 #endif
1031         }
1032 #ifdef ROCKET_DEBUG_OPEN
1033         printk(KERN_INFO "rp_open ttyR%d, count=%d\n", info->line, info->count);
1034 #endif
1035
1036         /*
1037          * Info->count is now 1; so it's safe to sleep now.
1038          */
1039         info->session = current->signal->session;
1040         info->pgrp = process_group(current);
1041
1042         if ((info->flags & ROCKET_INITIALIZED) == 0) {
1043                 cp = &info->channel;
1044                 sSetRxTrigger(cp, TRIG_1);
1045                 if (sGetChanStatus(cp) & CD_ACT)
1046                         info->cd_status = 1;
1047                 else
1048                         info->cd_status = 0;
1049                 sDisRxStatusMode(cp);
1050                 sFlushRxFIFO(cp);
1051                 sFlushTxFIFO(cp);
1052
1053                 sEnInterrupts(cp, (TXINT_EN | MCINT_EN | RXINT_EN | SRCINT_EN | CHANINT_EN));
1054                 sSetRxTrigger(cp, TRIG_1);
1055
1056                 sGetChanStatus(cp);
1057                 sDisRxStatusMode(cp);
1058                 sClrTxXOFF(cp);
1059
1060                 sDisCTSFlowCtl(cp);
1061                 sDisTxSoftFlowCtl(cp);
1062
1063                 sEnRxFIFO(cp);
1064                 sEnTransmit(cp);
1065
1066                 info->flags |= ROCKET_INITIALIZED;
1067
1068                 /*
1069                  * Set up the tty->alt_speed kludge
1070                  */
1071                 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_HI)
1072                         info->tty->alt_speed = 57600;
1073                 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_VHI)
1074                         info->tty->alt_speed = 115200;
1075                 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_SHI)
1076                         info->tty->alt_speed = 230400;
1077                 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_WARP)
1078                         info->tty->alt_speed = 460800;
1079
1080                 configure_r_port(info, NULL);
1081                 if (tty->termios->c_cflag & CBAUD) {
1082                         sSetDTR(cp);
1083                         sSetRTS(cp);
1084                 }
1085         }
1086         /*  Starts (or resets) the maint polling loop */
1087         mod_timer(&rocket_timer, jiffies + POLL_PERIOD);
1088
1089         retval = block_til_ready(tty, filp, info);
1090         if (retval) {
1091 #ifdef ROCKET_DEBUG_OPEN
1092                 printk(KERN_INFO "rp_open returning after block_til_ready with %d\n", retval);
1093 #endif
1094                 return retval;
1095         }
1096         return 0;
1097 }
1098
1099 /*
1100  *  Exception handler that closes a serial port. info->count is considered critical. 
1101  */
1102 static void rp_close(struct tty_struct *tty, struct file *filp)
1103 {
1104         struct r_port *info = (struct r_port *) tty->driver_data;
1105         unsigned long flags;
1106         int timeout;
1107         CHANNEL_t *cp;
1108         
1109         if (rocket_paranoia_check(info, "rp_close"))
1110                 return;
1111
1112 #ifdef ROCKET_DEBUG_OPEN
1113         printk(KERN_INFO "rp_close ttyR%d, count = %d\n", info->line, info->count);
1114 #endif
1115
1116         if (tty_hung_up_p(filp))
1117                 return;
1118         spin_lock_irqsave(&info->slock, flags);
1119
1120         if ((tty->count == 1) && (info->count != 1)) {
1121                 /*
1122                  * Uh, oh.  tty->count is 1, which means that the tty
1123                  * structure will be freed.  Info->count should always
1124                  * be one in these conditions.  If it's greater than
1125                  * one, we've got real problems, since it means the
1126                  * serial port won't be shutdown.
1127                  */
1128                 printk(KERN_INFO "rp_close: bad serial port count; tty->count is 1, "
1129                        "info->count is %d\n", info->count);
1130                 info->count = 1;
1131         }
1132         if (--info->count < 0) {
1133                 printk(KERN_INFO "rp_close: bad serial port count for ttyR%d: %d\n",
1134                        info->line, info->count);
1135                 info->count = 0;
1136         }
1137         if (info->count) {
1138                 spin_unlock_irqrestore(&info->slock, flags);
1139                 return;
1140         }
1141         info->flags |= ROCKET_CLOSING;
1142         spin_unlock_irqrestore(&info->slock, flags);
1143
1144         cp = &info->channel;
1145
1146         /*
1147          * Notify the line discpline to only process XON/XOFF characters
1148          */
1149         tty->closing = 1;
1150
1151         /*
1152          * If transmission was throttled by the application request,
1153          * just flush the xmit buffer.
1154          */
1155         if (tty->flow_stopped)
1156                 rp_flush_buffer(tty);
1157
1158         /*
1159          * Wait for the transmit buffer to clear
1160          */
1161         if (info->closing_wait != ROCKET_CLOSING_WAIT_NONE)
1162                 tty_wait_until_sent(tty, info->closing_wait);
1163         /*
1164          * Before we drop DTR, make sure the UART transmitter
1165          * has completely drained; this is especially
1166          * important if there is a transmit FIFO!
1167          */
1168         timeout = (sGetTxCnt(cp) + 1) * HZ / info->cps;
1169         if (timeout == 0)
1170                 timeout = 1;
1171         rp_wait_until_sent(tty, timeout);
1172         clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
1173
1174         sDisTransmit(cp);
1175         sDisInterrupts(cp, (TXINT_EN | MCINT_EN | RXINT_EN | SRCINT_EN | CHANINT_EN));
1176         sDisCTSFlowCtl(cp);
1177         sDisTxSoftFlowCtl(cp);
1178         sClrTxXOFF(cp);
1179         sFlushRxFIFO(cp);
1180         sFlushTxFIFO(cp);
1181         sClrRTS(cp);
1182         if (C_HUPCL(tty))
1183                 sClrDTR(cp);
1184
1185         if (TTY_DRIVER_FLUSH_BUFFER_EXISTS(tty))
1186                 TTY_DRIVER_FLUSH_BUFFER(tty);
1187                 
1188         tty_ldisc_flush(tty);
1189
1190         clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
1191
1192         if (info->blocked_open) {
1193                 if (info->close_delay) {
1194                         msleep_interruptible(jiffies_to_msecs(info->close_delay));
1195                 }
1196                 wake_up_interruptible(&info->open_wait);
1197         } else {
1198                 if (info->xmit_buf) {
1199                         free_page((unsigned long) info->xmit_buf);
1200                         info->xmit_buf = NULL;
1201                 }
1202         }
1203         info->flags &= ~(ROCKET_INITIALIZED | ROCKET_CLOSING | ROCKET_NORMAL_ACTIVE);
1204         tty->closing = 0;
1205         wake_up_interruptible(&info->close_wait);
1206         atomic_dec(&rp_num_ports_open);
1207
1208 #ifdef ROCKET_DEBUG_OPEN
1209         printk(KERN_INFO "rocket mod-- = %d...", atomic_read(&rp_num_ports_open));
1210         printk(KERN_INFO "rp_close ttyR%d complete shutdown\n", info->line);
1211 #endif
1212
1213 }
1214
1215 static void rp_set_termios(struct tty_struct *tty,
1216                            struct termios *old_termios)
1217 {
1218         struct r_port *info = (struct r_port *) tty->driver_data;
1219         CHANNEL_t *cp;
1220         unsigned cflag;
1221
1222         if (rocket_paranoia_check(info, "rp_set_termios"))
1223                 return;
1224
1225         cflag = tty->termios->c_cflag;
1226
1227         if (cflag == old_termios->c_cflag)
1228                 return;
1229
1230         /*
1231          * This driver doesn't support CS5 or CS6
1232          */
1233         if (((cflag & CSIZE) == CS5) || ((cflag & CSIZE) == CS6))
1234                 tty->termios->c_cflag =
1235                     ((cflag & ~CSIZE) | (old_termios->c_cflag & CSIZE));
1236
1237         configure_r_port(info, old_termios);
1238
1239         cp = &info->channel;
1240
1241         /* Handle transition to B0 status */
1242         if ((old_termios->c_cflag & CBAUD) && !(tty->termios->c_cflag & CBAUD)) {
1243                 sClrDTR(cp);
1244                 sClrRTS(cp);
1245         }
1246
1247         /* Handle transition away from B0 status */
1248         if (!(old_termios->c_cflag & CBAUD) && (tty->termios->c_cflag & CBAUD)) {
1249                 if (!tty->hw_stopped || !(tty->termios->c_cflag & CRTSCTS))
1250                         sSetRTS(cp);
1251                 sSetDTR(cp);
1252         }
1253
1254         if ((old_termios->c_cflag & CRTSCTS) && !(tty->termios->c_cflag & CRTSCTS)) {
1255                 tty->hw_stopped = 0;
1256                 rp_start(tty);
1257         }
1258 }
1259
1260 static void rp_break(struct tty_struct *tty, int break_state)
1261 {
1262         struct r_port *info = (struct r_port *) tty->driver_data;
1263         unsigned long flags;
1264
1265         if (rocket_paranoia_check(info, "rp_break"))
1266                 return;
1267
1268         spin_lock_irqsave(&info->slock, flags);
1269         if (break_state == -1)
1270                 sSendBreak(&info->channel);
1271         else
1272                 sClrBreak(&info->channel);
1273         spin_unlock_irqrestore(&info->slock, flags);
1274 }
1275
1276 /*
1277  * sGetChanRI used to be a macro in rocket_int.h. When the functionality for
1278  * the UPCI boards was added, it was decided to make this a function because
1279  * the macro was getting too complicated. All cases except the first one
1280  * (UPCIRingInd) are taken directly from the original macro.
1281  */
1282 static int sGetChanRI(CHANNEL_T * ChP)
1283 {
1284         CONTROLLER_t *CtlP = ChP->CtlP;
1285         int ChanNum = ChP->ChanNum;
1286         int RingInd = 0;
1287
1288         if (CtlP->UPCIRingInd)
1289                 RingInd = !(sInB(CtlP->UPCIRingInd) & sBitMapSetTbl[ChanNum]);
1290         else if (CtlP->AltChanRingIndicator)
1291                 RingInd = sInB((ByteIO_t) (ChP->ChanStat + 8)) & DSR_ACT;
1292         else if (CtlP->boardType == ROCKET_TYPE_PC104)
1293                 RingInd = !(sInB(CtlP->AiopIO[3]) & sBitMapSetTbl[ChanNum]);
1294
1295         return RingInd;
1296 }
1297
1298 /********************************************************************************************/
1299 /*  Here are the routines used by rp_ioctl.  These are all called from exception handlers.  */
1300
1301 /*
1302  *  Returns the state of the serial modem control lines.  These next 2 functions 
1303  *  are the way kernel versions > 2.5 handle modem control lines rather than IOCTLs.
1304  */
1305 static int rp_tiocmget(struct tty_struct *tty, struct file *file)
1306 {
1307         struct r_port *info = (struct r_port *)tty->driver_data;
1308         unsigned int control, result, ChanStatus;
1309
1310         ChanStatus = sGetChanStatusLo(&info->channel);
1311         control = info->channel.TxControl[3];
1312         result = ((control & SET_RTS) ? TIOCM_RTS : 0) | 
1313                 ((control & SET_DTR) ?  TIOCM_DTR : 0) |
1314                 ((ChanStatus & CD_ACT) ? TIOCM_CAR : 0) |
1315                 (sGetChanRI(&info->channel) ? TIOCM_RNG : 0) |
1316                 ((ChanStatus & DSR_ACT) ? TIOCM_DSR : 0) |
1317                 ((ChanStatus & CTS_ACT) ? TIOCM_CTS : 0);
1318
1319         return result;
1320 }
1321
1322 /* 
1323  *  Sets the modem control lines
1324  */
1325 static int rp_tiocmset(struct tty_struct *tty, struct file *file,
1326                     unsigned int set, unsigned int clear)
1327 {
1328         struct r_port *info = (struct r_port *)tty->driver_data;
1329
1330         if (set & TIOCM_RTS)
1331                 info->channel.TxControl[3] |= SET_RTS;
1332         if (set & TIOCM_DTR)
1333                 info->channel.TxControl[3] |= SET_DTR;
1334         if (clear & TIOCM_RTS)
1335                 info->channel.TxControl[3] &= ~SET_RTS;
1336         if (clear & TIOCM_DTR)
1337                 info->channel.TxControl[3] &= ~SET_DTR;
1338
1339         sOutDW(info->channel.IndexAddr, *(DWord_t *) & (info->channel.TxControl[0]));
1340         return 0;
1341 }
1342
1343 static int get_config(struct r_port *info, struct rocket_config __user *retinfo)
1344 {
1345         struct rocket_config tmp;
1346
1347         if (!retinfo)
1348                 return -EFAULT;
1349         memset(&tmp, 0, sizeof (tmp));
1350         tmp.line = info->line;
1351         tmp.flags = info->flags;
1352         tmp.close_delay = info->close_delay;
1353         tmp.closing_wait = info->closing_wait;
1354         tmp.port = rcktpt_io_addr[(info->line >> 5) & 3];
1355
1356         if (copy_to_user(retinfo, &tmp, sizeof (*retinfo)))
1357                 return -EFAULT;
1358         return 0;
1359 }
1360
1361 static int set_config(struct r_port *info, struct rocket_config __user *new_info)
1362 {
1363         struct rocket_config new_serial;
1364
1365         if (copy_from_user(&new_serial, new_info, sizeof (new_serial)))
1366                 return -EFAULT;
1367
1368         if (!capable(CAP_SYS_ADMIN))
1369         {
1370                 if ((new_serial.flags & ~ROCKET_USR_MASK) != (info->flags & ~ROCKET_USR_MASK))
1371                         return -EPERM;
1372                 info->flags = ((info->flags & ~ROCKET_USR_MASK) | (new_serial.flags & ROCKET_USR_MASK));
1373                 configure_r_port(info, NULL);
1374                 return 0;
1375         }
1376
1377         info->flags = ((info->flags & ~ROCKET_FLAGS) | (new_serial.flags & ROCKET_FLAGS));
1378         info->close_delay = new_serial.close_delay;
1379         info->closing_wait = new_serial.closing_wait;
1380
1381         if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_HI)
1382                 info->tty->alt_speed = 57600;
1383         if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_VHI)
1384                 info->tty->alt_speed = 115200;
1385         if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_SHI)
1386                 info->tty->alt_speed = 230400;
1387         if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_WARP)
1388                 info->tty->alt_speed = 460800;
1389
1390         configure_r_port(info, NULL);
1391         return 0;
1392 }
1393
1394 /*
1395  *  This function fills in a rocket_ports struct with information
1396  *  about what boards/ports are in the system.  This info is passed
1397  *  to user space.  See setrocket.c where the info is used to create
1398  *  the /dev/ttyRx ports.
1399  */
1400 static int get_ports(struct r_port *info, struct rocket_ports __user *retports)
1401 {
1402         struct rocket_ports tmp;
1403         int board;
1404
1405         if (!retports)
1406                 return -EFAULT;
1407         memset(&tmp, 0, sizeof (tmp));
1408         tmp.tty_major = rocket_driver->major;
1409
1410         for (board = 0; board < 4; board++) {
1411                 tmp.rocketModel[board].model = rocketModel[board].model;
1412                 strcpy(tmp.rocketModel[board].modelString, rocketModel[board].modelString);
1413                 tmp.rocketModel[board].numPorts = rocketModel[board].numPorts;
1414                 tmp.rocketModel[board].loadrm2 = rocketModel[board].loadrm2;
1415                 tmp.rocketModel[board].startingPortNumber = rocketModel[board].startingPortNumber;
1416         }
1417         if (copy_to_user(retports, &tmp, sizeof (*retports)))
1418                 return -EFAULT;
1419         return 0;
1420 }
1421
1422 static int reset_rm2(struct r_port *info, void __user *arg)
1423 {
1424         int reset;
1425
1426         if (copy_from_user(&reset, arg, sizeof (int)))
1427                 return -EFAULT;
1428         if (reset)
1429                 reset = 1;
1430
1431         if (rcktpt_type[info->board] != ROCKET_TYPE_MODEMII &&
1432             rcktpt_type[info->board] != ROCKET_TYPE_MODEMIII)
1433                 return -EINVAL;
1434
1435         if (info->ctlp->BusType == isISA)
1436                 sModemReset(info->ctlp, info->chan, reset);
1437         else
1438                 sPCIModemReset(info->ctlp, info->chan, reset);
1439
1440         return 0;
1441 }
1442
1443 static int get_version(struct r_port *info, struct rocket_version __user *retvers)
1444 {
1445         if (copy_to_user(retvers, &driver_version, sizeof (*retvers)))
1446                 return -EFAULT;
1447         return 0;
1448 }
1449
1450 /*  IOCTL call handler into the driver */
1451 static int rp_ioctl(struct tty_struct *tty, struct file *file,
1452                     unsigned int cmd, unsigned long arg)
1453 {
1454         struct r_port *info = (struct r_port *) tty->driver_data;
1455         void __user *argp = (void __user *)arg;
1456
1457         if (cmd != RCKP_GET_PORTS && rocket_paranoia_check(info, "rp_ioctl"))
1458                 return -ENXIO;
1459
1460         switch (cmd) {
1461         case RCKP_GET_STRUCT:
1462                 if (copy_to_user(argp, info, sizeof (struct r_port)))
1463                         return -EFAULT;
1464                 return 0;
1465         case RCKP_GET_CONFIG:
1466                 return get_config(info, argp);
1467         case RCKP_SET_CONFIG:
1468                 return set_config(info, argp);
1469         case RCKP_GET_PORTS:
1470                 return get_ports(info, argp);
1471         case RCKP_RESET_RM2:
1472                 return reset_rm2(info, argp);
1473         case RCKP_GET_VERSION:
1474                 return get_version(info, argp);
1475         default:
1476                 return -ENOIOCTLCMD;
1477         }
1478         return 0;
1479 }
1480
1481 static void rp_send_xchar(struct tty_struct *tty, char ch)
1482 {
1483         struct r_port *info = (struct r_port *) tty->driver_data;
1484         CHANNEL_t *cp;
1485
1486         if (rocket_paranoia_check(info, "rp_send_xchar"))
1487                 return;
1488
1489         cp = &info->channel;
1490         if (sGetTxCnt(cp))
1491                 sWriteTxPrioByte(cp, ch);
1492         else
1493                 sWriteTxByte(sGetTxRxDataIO(cp), ch);
1494 }
1495
1496 static void rp_throttle(struct tty_struct *tty)
1497 {
1498         struct r_port *info = (struct r_port *) tty->driver_data;
1499         CHANNEL_t *cp;
1500
1501 #ifdef ROCKET_DEBUG_THROTTLE
1502         printk(KERN_INFO "throttle %s: %d....\n", tty->name,
1503                tty->ldisc.chars_in_buffer(tty));
1504 #endif
1505
1506         if (rocket_paranoia_check(info, "rp_throttle"))
1507                 return;
1508
1509         cp = &info->channel;
1510         if (I_IXOFF(tty))
1511                 rp_send_xchar(tty, STOP_CHAR(tty));
1512
1513         sClrRTS(&info->channel);
1514 }
1515
1516 static void rp_unthrottle(struct tty_struct *tty)
1517 {
1518         struct r_port *info = (struct r_port *) tty->driver_data;
1519         CHANNEL_t *cp;
1520 #ifdef ROCKET_DEBUG_THROTTLE
1521         printk(KERN_INFO "unthrottle %s: %d....\n", tty->name,
1522                tty->ldisc.chars_in_buffer(tty));
1523 #endif
1524
1525         if (rocket_paranoia_check(info, "rp_throttle"))
1526                 return;
1527
1528         cp = &info->channel;
1529         if (I_IXOFF(tty))
1530                 rp_send_xchar(tty, START_CHAR(tty));
1531
1532         sSetRTS(&info->channel);
1533 }
1534
1535 /*
1536  * ------------------------------------------------------------
1537  * rp_stop() and rp_start()
1538  *
1539  * This routines are called before setting or resetting tty->stopped.
1540  * They enable or disable transmitter interrupts, as necessary.
1541  * ------------------------------------------------------------
1542  */
1543 static void rp_stop(struct tty_struct *tty)
1544 {
1545         struct r_port *info = (struct r_port *) tty->driver_data;
1546
1547 #ifdef ROCKET_DEBUG_FLOW
1548         printk(KERN_INFO "stop %s: %d %d....\n", tty->name,
1549                info->xmit_cnt, info->xmit_fifo_room);
1550 #endif
1551
1552         if (rocket_paranoia_check(info, "rp_stop"))
1553                 return;
1554
1555         if (sGetTxCnt(&info->channel))
1556                 sDisTransmit(&info->channel);
1557 }
1558
1559 static void rp_start(struct tty_struct *tty)
1560 {
1561         struct r_port *info = (struct r_port *) tty->driver_data;
1562
1563 #ifdef ROCKET_DEBUG_FLOW
1564         printk(KERN_INFO "start %s: %d %d....\n", tty->name,
1565                info->xmit_cnt, info->xmit_fifo_room);
1566 #endif
1567
1568         if (rocket_paranoia_check(info, "rp_stop"))
1569                 return;
1570
1571         sEnTransmit(&info->channel);
1572         set_bit((info->aiop * 8) + info->chan,
1573                 (void *) &xmit_flags[info->board]);
1574 }
1575
1576 /*
1577  * rp_wait_until_sent() --- wait until the transmitter is empty
1578  */
1579 static void rp_wait_until_sent(struct tty_struct *tty, int timeout)
1580 {
1581         struct r_port *info = (struct r_port *) tty->driver_data;
1582         CHANNEL_t *cp;
1583         unsigned long orig_jiffies;
1584         int check_time, exit_time;
1585         int txcnt;
1586
1587         if (rocket_paranoia_check(info, "rp_wait_until_sent"))
1588                 return;
1589
1590         cp = &info->channel;
1591
1592         orig_jiffies = jiffies;
1593 #ifdef ROCKET_DEBUG_WAIT_UNTIL_SENT
1594         printk(KERN_INFO "In RP_wait_until_sent(%d) (jiff=%lu)...", timeout,
1595                jiffies);
1596         printk(KERN_INFO "cps=%d...", info->cps);
1597 #endif
1598         while (1) {
1599                 txcnt = sGetTxCnt(cp);
1600                 if (!txcnt) {
1601                         if (sGetChanStatusLo(cp) & TXSHRMT)
1602                                 break;
1603                         check_time = (HZ / info->cps) / 5;
1604                 } else {
1605                         check_time = HZ * txcnt / info->cps;
1606                 }
1607                 if (timeout) {
1608                         exit_time = orig_jiffies + timeout - jiffies;
1609                         if (exit_time <= 0)
1610                                 break;
1611                         if (exit_time < check_time)
1612                                 check_time = exit_time;
1613                 }
1614                 if (check_time == 0)
1615                         check_time = 1;
1616 #ifdef ROCKET_DEBUG_WAIT_UNTIL_SENT
1617                 printk(KERN_INFO "txcnt = %d (jiff=%lu,check=%d)...", txcnt, jiffies, check_time);
1618 #endif
1619                 msleep_interruptible(jiffies_to_msecs(check_time));
1620                 if (signal_pending(current))
1621                         break;
1622         }
1623         current->state = TASK_RUNNING;
1624 #ifdef ROCKET_DEBUG_WAIT_UNTIL_SENT
1625         printk(KERN_INFO "txcnt = %d (jiff=%lu)...done\n", txcnt, jiffies);
1626 #endif
1627 }
1628
1629 /*
1630  * rp_hangup() --- called by tty_hangup() when a hangup is signaled.
1631  */
1632 static void rp_hangup(struct tty_struct *tty)
1633 {
1634         CHANNEL_t *cp;
1635         struct r_port *info = (struct r_port *) tty->driver_data;
1636
1637         if (rocket_paranoia_check(info, "rp_hangup"))
1638                 return;
1639
1640 #if (defined(ROCKET_DEBUG_OPEN) || defined(ROCKET_DEBUG_HANGUP))
1641         printk(KERN_INFO "rp_hangup of ttyR%d...", info->line);
1642 #endif
1643         rp_flush_buffer(tty);
1644         if (info->flags & ROCKET_CLOSING)
1645                 return;
1646         if (info->count) 
1647                 atomic_dec(&rp_num_ports_open);
1648         clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
1649
1650         info->count = 0;
1651         info->flags &= ~ROCKET_NORMAL_ACTIVE;
1652         info->tty = NULL;
1653
1654         cp = &info->channel;
1655         sDisRxFIFO(cp);
1656         sDisTransmit(cp);
1657         sDisInterrupts(cp, (TXINT_EN | MCINT_EN | RXINT_EN | SRCINT_EN | CHANINT_EN));
1658         sDisCTSFlowCtl(cp);
1659         sDisTxSoftFlowCtl(cp);
1660         sClrTxXOFF(cp);
1661         info->flags &= ~ROCKET_INITIALIZED;
1662
1663         wake_up_interruptible(&info->open_wait);
1664 }
1665
1666 /*
1667  *  Exception handler - write char routine.  The RocketPort driver uses a
1668  *  double-buffering strategy, with the twist that if the in-memory CPU
1669  *  buffer is empty, and there's space in the transmit FIFO, the
1670  *  writing routines will write directly to transmit FIFO.
1671  *  Write buffer and counters protected by spinlocks
1672  */
1673 static void rp_put_char(struct tty_struct *tty, unsigned char ch)
1674 {
1675         struct r_port *info = (struct r_port *) tty->driver_data;
1676         CHANNEL_t *cp;
1677         unsigned long flags;
1678
1679         if (rocket_paranoia_check(info, "rp_put_char"))
1680                 return;
1681
1682         /*  Grab the port write semaphore, locking out other processes that try to write to this port */
1683         down(&info->write_sem);
1684
1685 #ifdef ROCKET_DEBUG_WRITE
1686         printk(KERN_INFO "rp_put_char %c...", ch);
1687 #endif
1688
1689         spin_lock_irqsave(&info->slock, flags);
1690         cp = &info->channel;
1691
1692         if (!tty->stopped && !tty->hw_stopped && info->xmit_fifo_room == 0)
1693                 info->xmit_fifo_room = TXFIFO_SIZE - sGetTxCnt(cp);
1694
1695         if (tty->stopped || tty->hw_stopped || info->xmit_fifo_room == 0 || info->xmit_cnt != 0) {
1696                 info->xmit_buf[info->xmit_head++] = ch;
1697                 info->xmit_head &= XMIT_BUF_SIZE - 1;
1698                 info->xmit_cnt++;
1699                 set_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
1700         } else {
1701                 sOutB(sGetTxRxDataIO(cp), ch);
1702                 info->xmit_fifo_room--;
1703         }
1704         spin_unlock_irqrestore(&info->slock, flags);
1705         up(&info->write_sem);
1706 }
1707
1708 /*
1709  *  Exception handler - write routine, called when user app writes to the device.
1710  *  A per port write semaphore is used to protect from another process writing to
1711  *  this port at the same time.  This other process could be running on the other CPU
1712  *  or get control of the CPU if the copy_from_user() blocks due to a page fault (swapped out). 
1713  *  Spinlocks protect the info xmit members.
1714  */
1715 static int rp_write(struct tty_struct *tty,
1716                     const unsigned char *buf, int count)
1717 {
1718         struct r_port *info = (struct r_port *) tty->driver_data;
1719         CHANNEL_t *cp;
1720         const unsigned char *b;
1721         int c, retval = 0;
1722         unsigned long flags;
1723
1724         if (count <= 0 || rocket_paranoia_check(info, "rp_write"))
1725                 return 0;
1726
1727         down_interruptible(&info->write_sem);
1728
1729 #ifdef ROCKET_DEBUG_WRITE
1730         printk(KERN_INFO "rp_write %d chars...", count);
1731 #endif
1732         cp = &info->channel;
1733
1734         if (!tty->stopped && !tty->hw_stopped && info->xmit_fifo_room < count)
1735                 info->xmit_fifo_room = TXFIFO_SIZE - sGetTxCnt(cp);
1736
1737         /*
1738          *  If the write queue for the port is empty, and there is FIFO space, stuff bytes 
1739          *  into FIFO.  Use the write queue for temp storage.
1740          */
1741         if (!tty->stopped && !tty->hw_stopped && info->xmit_cnt == 0 && info->xmit_fifo_room > 0) {
1742                 c = min(count, info->xmit_fifo_room);
1743                 b = buf;
1744
1745                 /*  Push data into FIFO, 2 bytes at a time */
1746                 sOutStrW(sGetTxRxDataIO(cp), (unsigned short *) b, c / 2);
1747
1748                 /*  If there is a byte remaining, write it */
1749                 if (c & 1)
1750                         sOutB(sGetTxRxDataIO(cp), b[c - 1]);
1751
1752                 retval += c;
1753                 buf += c;
1754                 count -= c;
1755
1756                 spin_lock_irqsave(&info->slock, flags);
1757                 info->xmit_fifo_room -= c;
1758                 spin_unlock_irqrestore(&info->slock, flags);
1759         }
1760
1761         /* If count is zero, we wrote it all and are done */
1762         if (!count)
1763                 goto end;
1764
1765         /*  Write remaining data into the port's xmit_buf */
1766         while (1) {
1767                 if (info->tty == 0)     /*   Seemingly obligatory check... */
1768                         goto end;
1769
1770                 c = min(count, min(XMIT_BUF_SIZE - info->xmit_cnt - 1, XMIT_BUF_SIZE - info->xmit_head));
1771                 if (c <= 0)
1772                         break;
1773
1774                 b = buf;
1775                 memcpy(info->xmit_buf + info->xmit_head, b, c);
1776
1777                 spin_lock_irqsave(&info->slock, flags);
1778                 info->xmit_head =
1779                     (info->xmit_head + c) & (XMIT_BUF_SIZE - 1);
1780                 info->xmit_cnt += c;
1781                 spin_unlock_irqrestore(&info->slock, flags);
1782
1783                 buf += c;
1784                 count -= c;
1785                 retval += c;
1786         }
1787
1788         if ((retval > 0) && !tty->stopped && !tty->hw_stopped)
1789                 set_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
1790         
1791 end:
1792         if (info->xmit_cnt < WAKEUP_CHARS) {
1793                 tty_wakeup(tty);
1794                 wake_up_interruptible(&tty->write_wait);
1795 #ifdef ROCKETPORT_HAVE_POLL_WAIT
1796                 wake_up_interruptible(&tty->poll_wait);
1797 #endif
1798         }
1799         up(&info->write_sem);
1800         return retval;
1801 }
1802
1803 /*
1804  * Return the number of characters that can be sent.  We estimate
1805  * only using the in-memory transmit buffer only, and ignore the
1806  * potential space in the transmit FIFO.
1807  */
1808 static int rp_write_room(struct tty_struct *tty)
1809 {
1810         struct r_port *info = (struct r_port *) tty->driver_data;
1811         int ret;
1812
1813         if (rocket_paranoia_check(info, "rp_write_room"))
1814                 return 0;
1815
1816         ret = XMIT_BUF_SIZE - info->xmit_cnt - 1;
1817         if (ret < 0)
1818                 ret = 0;
1819 #ifdef ROCKET_DEBUG_WRITE
1820         printk(KERN_INFO "rp_write_room returns %d...", ret);
1821 #endif
1822         return ret;
1823 }
1824
1825 /*
1826  * Return the number of characters in the buffer.  Again, this only
1827  * counts those characters in the in-memory transmit buffer.
1828  */
1829 static int rp_chars_in_buffer(struct tty_struct *tty)
1830 {
1831         struct r_port *info = (struct r_port *) tty->driver_data;
1832         CHANNEL_t *cp;
1833
1834         if (rocket_paranoia_check(info, "rp_chars_in_buffer"))
1835                 return 0;
1836
1837         cp = &info->channel;
1838
1839 #ifdef ROCKET_DEBUG_WRITE
1840         printk(KERN_INFO "rp_chars_in_buffer returns %d...", info->xmit_cnt);
1841 #endif
1842         return info->xmit_cnt;
1843 }
1844
1845 /*
1846  *  Flushes the TX fifo for a port, deletes data in the xmit_buf stored in the
1847  *  r_port struct for the port.  Note that spinlock are used to protect info members,
1848  *  do not call this function if the spinlock is already held.
1849  */
1850 static void rp_flush_buffer(struct tty_struct *tty)
1851 {
1852         struct r_port *info = (struct r_port *) tty->driver_data;
1853         CHANNEL_t *cp;
1854         unsigned long flags;
1855
1856         if (rocket_paranoia_check(info, "rp_flush_buffer"))
1857                 return;
1858
1859         spin_lock_irqsave(&info->slock, flags);
1860         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1861         spin_unlock_irqrestore(&info->slock, flags);
1862
1863         wake_up_interruptible(&tty->write_wait);
1864 #ifdef ROCKETPORT_HAVE_POLL_WAIT
1865         wake_up_interruptible(&tty->poll_wait);
1866 #endif
1867         tty_wakeup(tty);
1868
1869         cp = &info->channel;
1870         sFlushTxFIFO(cp);
1871 }
1872
1873 #ifdef CONFIG_PCI
1874
1875 /*
1876  *  Called when a PCI card is found.  Retrieves and stores model information,
1877  *  init's aiopic and serial port hardware.
1878  *  Inputs:  i is the board number (0-n)
1879  */
1880 static __init int register_PCI(int i, struct pci_dev *dev)
1881 {
1882         int num_aiops, aiop, max_num_aiops, num_chan, chan;
1883         unsigned int aiopio[MAX_AIOPS_PER_BOARD];
1884         char *str, *board_type;
1885         CONTROLLER_t *ctlp;
1886
1887         int fast_clock = 0;
1888         int altChanRingIndicator = 0;
1889         int ports_per_aiop = 8;
1890         int ret;
1891         unsigned int class_rev;
1892         WordIO_t ConfigIO = 0;
1893         ByteIO_t UPCIRingInd = 0;
1894
1895         if (!dev || pci_enable_device(dev))
1896                 return 0;
1897
1898         rcktpt_io_addr[i] = pci_resource_start(dev, 0);
1899         ret = pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
1900
1901         if (ret) {
1902                 printk(KERN_INFO "  Error during register_PCI(), unable to read config dword \n");
1903                 return 0;
1904         }
1905
1906         rcktpt_type[i] = ROCKET_TYPE_NORMAL;
1907         rocketModel[i].loadrm2 = 0;
1908         rocketModel[i].startingPortNumber = nextLineNumber;
1909
1910         /*  Depending on the model, set up some config variables */
1911         switch (dev->device) {
1912         case PCI_DEVICE_ID_RP4QUAD:
1913                 str = "Quadcable";
1914                 max_num_aiops = 1;
1915                 ports_per_aiop = 4;
1916                 rocketModel[i].model = MODEL_RP4QUAD;
1917                 strcpy(rocketModel[i].modelString, "RocketPort 4 port w/quad cable");
1918                 rocketModel[i].numPorts = 4;
1919                 break;
1920         case PCI_DEVICE_ID_RP8OCTA:
1921                 str = "Octacable";
1922                 max_num_aiops = 1;
1923                 rocketModel[i].model = MODEL_RP8OCTA;
1924                 strcpy(rocketModel[i].modelString, "RocketPort 8 port w/octa cable");
1925                 rocketModel[i].numPorts = 8;
1926                 break;
1927         case PCI_DEVICE_ID_URP8OCTA:
1928                 str = "Octacable";
1929                 max_num_aiops = 1;
1930                 rocketModel[i].model = MODEL_UPCI_RP8OCTA;
1931                 strcpy(rocketModel[i].modelString, "RocketPort UPCI 8 port w/octa cable");
1932                 rocketModel[i].numPorts = 8;
1933                 break;
1934         case PCI_DEVICE_ID_RP8INTF:
1935                 str = "8";
1936                 max_num_aiops = 1;
1937                 rocketModel[i].model = MODEL_RP8INTF;
1938                 strcpy(rocketModel[i].modelString, "RocketPort 8 port w/external I/F");
1939                 rocketModel[i].numPorts = 8;
1940                 break;
1941         case PCI_DEVICE_ID_URP8INTF:
1942                 str = "8";
1943                 max_num_aiops = 1;
1944                 rocketModel[i].model = MODEL_UPCI_RP8INTF;
1945                 strcpy(rocketModel[i].modelString, "RocketPort UPCI 8 port w/external I/F");
1946                 rocketModel[i].numPorts = 8;
1947                 break;
1948         case PCI_DEVICE_ID_RP8J:
1949                 str = "8J";
1950                 max_num_aiops = 1;
1951                 rocketModel[i].model = MODEL_RP8J;
1952                 strcpy(rocketModel[i].modelString, "RocketPort 8 port w/RJ11 connectors");
1953                 rocketModel[i].numPorts = 8;
1954                 break;
1955         case PCI_DEVICE_ID_RP4J:
1956                 str = "4J";
1957                 max_num_aiops = 1;
1958                 ports_per_aiop = 4;
1959                 rocketModel[i].model = MODEL_RP4J;
1960                 strcpy(rocketModel[i].modelString, "RocketPort 4 port w/RJ45 connectors");
1961                 rocketModel[i].numPorts = 4;
1962                 break;
1963         case PCI_DEVICE_ID_RP8SNI:
1964                 str = "8 (DB78 Custom)";
1965                 max_num_aiops = 1;
1966                 rocketModel[i].model = MODEL_RP8SNI;
1967                 strcpy(rocketModel[i].modelString, "RocketPort 8 port w/ custom DB78");
1968                 rocketModel[i].numPorts = 8;
1969                 break;
1970         case PCI_DEVICE_ID_RP16SNI:
1971                 str = "16 (DB78 Custom)";
1972                 max_num_aiops = 2;
1973                 rocketModel[i].model = MODEL_RP16SNI;
1974                 strcpy(rocketModel[i].modelString, "RocketPort 16 port w/ custom DB78");
1975                 rocketModel[i].numPorts = 16;
1976                 break;
1977         case PCI_DEVICE_ID_RP16INTF:
1978                 str = "16";
1979                 max_num_aiops = 2;
1980                 rocketModel[i].model = MODEL_RP16INTF;
1981                 strcpy(rocketModel[i].modelString, "RocketPort 16 port w/external I/F");
1982                 rocketModel[i].numPorts = 16;
1983                 break;
1984         case PCI_DEVICE_ID_URP16INTF:
1985                 str = "16";
1986                 max_num_aiops = 2;
1987                 rocketModel[i].model = MODEL_UPCI_RP16INTF;
1988                 strcpy(rocketModel[i].modelString, "RocketPort UPCI 16 port w/external I/F");
1989                 rocketModel[i].numPorts = 16;
1990                 break;
1991         case PCI_DEVICE_ID_CRP16INTF:
1992                 str = "16";
1993                 max_num_aiops = 2;
1994                 rocketModel[i].model = MODEL_CPCI_RP16INTF;
1995                 strcpy(rocketModel[i].modelString, "RocketPort Compact PCI 16 port w/external I/F");
1996                 rocketModel[i].numPorts = 16;
1997                 break;
1998         case PCI_DEVICE_ID_RP32INTF:
1999                 str = "32";
2000                 max_num_aiops = 4;
2001                 rocketModel[i].model = MODEL_RP32INTF;
2002                 strcpy(rocketModel[i].modelString, "RocketPort 32 port w/external I/F");
2003                 rocketModel[i].numPorts = 32;
2004                 break;
2005         case PCI_DEVICE_ID_URP32INTF:
2006                 str = "32";
2007                 max_num_aiops = 4;
2008                 rocketModel[i].model = MODEL_UPCI_RP32INTF;
2009                 strcpy(rocketModel[i].modelString, "RocketPort UPCI 32 port w/external I/F");
2010                 rocketModel[i].numPorts = 32;
2011                 break;
2012         case PCI_DEVICE_ID_RPP4:
2013                 str = "Plus Quadcable";
2014                 max_num_aiops = 1;
2015                 ports_per_aiop = 4;
2016                 altChanRingIndicator++;
2017                 fast_clock++;
2018                 rocketModel[i].model = MODEL_RPP4;
2019                 strcpy(rocketModel[i].modelString, "RocketPort Plus 4 port");
2020                 rocketModel[i].numPorts = 4;
2021                 break;
2022         case PCI_DEVICE_ID_RPP8:
2023                 str = "Plus Octacable";
2024                 max_num_aiops = 2;
2025                 ports_per_aiop = 4;
2026                 altChanRingIndicator++;
2027                 fast_clock++;
2028                 rocketModel[i].model = MODEL_RPP8;
2029                 strcpy(rocketModel[i].modelString, "RocketPort Plus 8 port");
2030                 rocketModel[i].numPorts = 8;
2031                 break;
2032         case PCI_DEVICE_ID_RP2_232:
2033                 str = "Plus 2 (RS-232)";
2034                 max_num_aiops = 1;
2035                 ports_per_aiop = 2;
2036                 altChanRingIndicator++;
2037                 fast_clock++;
2038                 rocketModel[i].model = MODEL_RP2_232;
2039                 strcpy(rocketModel[i].modelString, "RocketPort Plus 2 port RS232");
2040                 rocketModel[i].numPorts = 2;
2041                 break;
2042         case PCI_DEVICE_ID_RP2_422:
2043                 str = "Plus 2 (RS-422)";
2044                 max_num_aiops = 1;
2045                 ports_per_aiop = 2;
2046                 altChanRingIndicator++;
2047                 fast_clock++;
2048                 rocketModel[i].model = MODEL_RP2_422;
2049                 strcpy(rocketModel[i].modelString, "RocketPort Plus 2 port RS422");
2050                 rocketModel[i].numPorts = 2;
2051                 break;
2052         case PCI_DEVICE_ID_RP6M:
2053
2054                 max_num_aiops = 1;
2055                 ports_per_aiop = 6;
2056                 str = "6-port";
2057
2058                 /*  If class_rev is 1, the rocketmodem flash must be loaded.  If it is 2 it is a "socketed" version. */
2059                 if ((class_rev & 0xFF) == 1) {
2060                         rcktpt_type[i] = ROCKET_TYPE_MODEMII;
2061                         rocketModel[i].loadrm2 = 1;
2062                 } else {
2063                         rcktpt_type[i] = ROCKET_TYPE_MODEM;
2064                 }
2065
2066                 rocketModel[i].model = MODEL_RP6M;
2067                 strcpy(rocketModel[i].modelString, "RocketModem 6 port");
2068                 rocketModel[i].numPorts = 6;
2069                 break;
2070         case PCI_DEVICE_ID_RP4M:
2071                 max_num_aiops = 1;
2072                 ports_per_aiop = 4;
2073                 str = "4-port";
2074                 if ((class_rev & 0xFF) == 1) {
2075                         rcktpt_type[i] = ROCKET_TYPE_MODEMII;
2076                         rocketModel[i].loadrm2 = 1;
2077                 } else {
2078                         rcktpt_type[i] = ROCKET_TYPE_MODEM;
2079                 }
2080
2081                 rocketModel[i].model = MODEL_RP4M;
2082                 strcpy(rocketModel[i].modelString, "RocketModem 4 port");
2083                 rocketModel[i].numPorts = 4;
2084                 break;
2085         default:
2086                 str = "(unknown/unsupported)";
2087                 max_num_aiops = 0;
2088                 break;
2089         }
2090
2091         /*
2092          * Check for UPCI boards.
2093          */
2094
2095         switch (dev->device) {
2096         case PCI_DEVICE_ID_URP32INTF:
2097         case PCI_DEVICE_ID_URP8INTF:
2098         case PCI_DEVICE_ID_URP16INTF:
2099         case PCI_DEVICE_ID_CRP16INTF:
2100         case PCI_DEVICE_ID_URP8OCTA:
2101                 rcktpt_io_addr[i] = pci_resource_start(dev, 2);
2102                 ConfigIO = pci_resource_start(dev, 1);
2103                 if (dev->device == PCI_DEVICE_ID_URP8OCTA) {
2104                         UPCIRingInd = rcktpt_io_addr[i] + _PCI_9030_RING_IND;
2105
2106                         /*
2107                          * Check for octa or quad cable.
2108                          */
2109                         if (!
2110                             (sInW(ConfigIO + _PCI_9030_GPIO_CTRL) &
2111                              PCI_GPIO_CTRL_8PORT)) {
2112                                 str = "Quadcable";
2113                                 ports_per_aiop = 4;
2114                                 rocketModel[i].numPorts = 4;
2115                         }
2116                 }
2117                 break;
2118         case PCI_DEVICE_ID_UPCI_RM3_8PORT:
2119                 str = "8 ports";
2120                 max_num_aiops = 1;
2121                 rocketModel[i].model = MODEL_UPCI_RM3_8PORT;
2122                 strcpy(rocketModel[i].modelString, "RocketModem III 8 port");
2123                 rocketModel[i].numPorts = 8;
2124                 rcktpt_io_addr[i] = pci_resource_start(dev, 2);
2125                 UPCIRingInd = rcktpt_io_addr[i] + _PCI_9030_RING_IND;
2126                 ConfigIO = pci_resource_start(dev, 1);
2127                 rcktpt_type[i] = ROCKET_TYPE_MODEMIII;
2128                 break;
2129         case PCI_DEVICE_ID_UPCI_RM3_4PORT:
2130                 str = "4 ports";
2131                 max_num_aiops = 1;
2132                 rocketModel[i].model = MODEL_UPCI_RM3_4PORT;
2133                 strcpy(rocketModel[i].modelString, "RocketModem III 4 port");
2134                 rocketModel[i].numPorts = 4;
2135                 rcktpt_io_addr[i] = pci_resource_start(dev, 2);
2136                 UPCIRingInd = rcktpt_io_addr[i] + _PCI_9030_RING_IND;
2137                 ConfigIO = pci_resource_start(dev, 1);
2138                 rcktpt_type[i] = ROCKET_TYPE_MODEMIII;
2139                 break;
2140         default:
2141                 break;
2142         }
2143
2144         switch (rcktpt_type[i]) {
2145         case ROCKET_TYPE_MODEM:
2146                 board_type = "RocketModem";
2147                 break;
2148         case ROCKET_TYPE_MODEMII:
2149                 board_type = "RocketModem II";
2150                 break;
2151         case ROCKET_TYPE_MODEMIII:
2152                 board_type = "RocketModem III";
2153                 break;
2154         default:
2155                 board_type = "RocketPort";
2156                 break;
2157         }
2158
2159         if (fast_clock) {
2160                 sClockPrescale = 0x12;  /* mod 2 (divide by 3) */
2161                 rp_baud_base[i] = 921600;
2162         } else {
2163                 /*
2164                  * If support_low_speed is set, use the slow clock
2165                  * prescale, which supports 50 bps
2166                  */
2167                 if (support_low_speed) {
2168                         /* mod 9 (divide by 10) prescale */
2169                         sClockPrescale = 0x19;
2170                         rp_baud_base[i] = 230400;
2171                 } else {
2172                         /* mod 4 (devide by 5) prescale */
2173                         sClockPrescale = 0x14;
2174                         rp_baud_base[i] = 460800;
2175                 }
2176         }
2177
2178         for (aiop = 0; aiop < max_num_aiops; aiop++)
2179                 aiopio[aiop] = rcktpt_io_addr[i] + (aiop * 0x40);
2180         ctlp = sCtlNumToCtlPtr(i);
2181         num_aiops = sPCIInitController(ctlp, i, aiopio, max_num_aiops, ConfigIO, 0, FREQ_DIS, 0, altChanRingIndicator, UPCIRingInd);
2182         for (aiop = 0; aiop < max_num_aiops; aiop++)
2183                 ctlp->AiopNumChan[aiop] = ports_per_aiop;
2184
2185         printk("Comtrol PCI controller #%d ID 0x%x found in bus:slot:fn %s at address %04lx, "
2186              "%d AIOP(s) (%s)\n", i, dev->device, pci_name(dev),
2187              rcktpt_io_addr[i], num_aiops, rocketModel[i].modelString);
2188         printk(KERN_INFO "Installing %s, creating /dev/ttyR%d - %ld\n",
2189                rocketModel[i].modelString,
2190                rocketModel[i].startingPortNumber,
2191                rocketModel[i].startingPortNumber +
2192                rocketModel[i].numPorts - 1);
2193
2194         if (num_aiops <= 0) {
2195                 rcktpt_io_addr[i] = 0;
2196                 return (0);
2197         }
2198         is_PCI[i] = 1;
2199
2200         /*  Reset the AIOPIC, init the serial ports */
2201         for (aiop = 0; aiop < num_aiops; aiop++) {
2202                 sResetAiopByNum(ctlp, aiop);
2203                 num_chan = ports_per_aiop;
2204                 for (chan = 0; chan < num_chan; chan++)
2205                         init_r_port(i, aiop, chan, dev);
2206         }
2207
2208         /*  Rocket modems must be reset */
2209         if ((rcktpt_type[i] == ROCKET_TYPE_MODEM) ||
2210             (rcktpt_type[i] == ROCKET_TYPE_MODEMII) ||
2211             (rcktpt_type[i] == ROCKET_TYPE_MODEMIII)) {
2212                 num_chan = ports_per_aiop;
2213                 for (chan = 0; chan < num_chan; chan++)
2214                         sPCIModemReset(ctlp, chan, 1);
2215                 mdelay(500);
2216                 for (chan = 0; chan < num_chan; chan++)
2217                         sPCIModemReset(ctlp, chan, 0);
2218                 mdelay(500);
2219                 rmSpeakerReset(ctlp, rocketModel[i].model);
2220         }
2221         return (1);
2222 }
2223
2224 /*
2225  *  Probes for PCI cards, inits them if found
2226  *  Input:   board_found = number of ISA boards already found, or the
2227  *           starting board number
2228  *  Returns: Number of PCI boards found
2229  */
2230 static int __init init_PCI(int boards_found)
2231 {
2232         struct pci_dev *dev = NULL;
2233         int count = 0;
2234
2235         /*  Work through the PCI device list, pulling out ours */
2236         while ((dev = pci_find_device(PCI_VENDOR_ID_RP, PCI_ANY_ID, dev))) {
2237                 if (register_PCI(count + boards_found, dev))
2238                         count++;
2239         }
2240         return (count);
2241 }
2242
2243 #endif                          /* CONFIG_PCI */
2244
2245 /*
2246  *  Probes for ISA cards
2247  *  Input:   i = the board number to look for
2248  *  Returns: 1 if board found, 0 else
2249  */
2250 static int __init init_ISA(int i)
2251 {
2252         int num_aiops, num_chan = 0, total_num_chan = 0;
2253         int aiop, chan;
2254         unsigned int aiopio[MAX_AIOPS_PER_BOARD];
2255         CONTROLLER_t *ctlp;
2256         char *type_string;
2257
2258         /*  If io_addr is zero, no board configured */
2259         if (rcktpt_io_addr[i] == 0)
2260                 return (0);
2261
2262         /*  Reserve the IO region */
2263         if (!request_region(rcktpt_io_addr[i], 64, "Comtrol RocketPort")) {
2264                 printk(KERN_INFO "Unable to reserve IO region for configured ISA RocketPort at address 0x%lx, board not installed...\n", rcktpt_io_addr[i]);
2265                 rcktpt_io_addr[i] = 0;
2266                 return (0);
2267         }
2268
2269         ctlp = sCtlNumToCtlPtr(i);
2270
2271         ctlp->boardType = rcktpt_type[i];
2272
2273         switch (rcktpt_type[i]) {
2274         case ROCKET_TYPE_PC104:
2275                 type_string = "(PC104)";
2276                 break;
2277         case ROCKET_TYPE_MODEM:
2278                 type_string = "(RocketModem)";
2279                 break;
2280         case ROCKET_TYPE_MODEMII:
2281                 type_string = "(RocketModem II)";
2282                 break;
2283         default:
2284                 type_string = "";
2285                 break;
2286         }
2287
2288         /*
2289          * If support_low_speed is set, use the slow clock prescale,
2290          * which supports 50 bps
2291          */
2292         if (support_low_speed) {
2293                 sClockPrescale = 0x19;  /* mod 9 (divide by 10) prescale */
2294                 rp_baud_base[i] = 230400;
2295         } else {
2296                 sClockPrescale = 0x14;  /* mod 4 (devide by 5) prescale */
2297                 rp_baud_base[i] = 460800;
2298         }
2299
2300         for (aiop = 0; aiop < MAX_AIOPS_PER_BOARD; aiop++)
2301                 aiopio[aiop] = rcktpt_io_addr[i] + (aiop * 0x400);
2302
2303         num_aiops = sInitController(ctlp, i, controller + (i * 0x400), aiopio,  MAX_AIOPS_PER_BOARD, 0, FREQ_DIS, 0);
2304
2305         if (ctlp->boardType == ROCKET_TYPE_PC104) {
2306                 sEnAiop(ctlp, 2);       /* only one AIOPIC, but these */
2307                 sEnAiop(ctlp, 3);       /* CSels used for other stuff */
2308         }
2309
2310         /*  If something went wrong initing the AIOP's release the ISA IO memory */
2311         if (num_aiops <= 0) {
2312                 release_region(rcktpt_io_addr[i], 64);
2313                 rcktpt_io_addr[i] = 0;
2314                 return (0);
2315         }
2316   
2317         rocketModel[i].startingPortNumber = nextLineNumber;
2318
2319         for (aiop = 0; aiop < num_aiops; aiop++) {
2320                 sResetAiopByNum(ctlp, aiop);
2321                 sEnAiop(ctlp, aiop);
2322                 num_chan = sGetAiopNumChan(ctlp, aiop);
2323                 total_num_chan += num_chan;
2324                 for (chan = 0; chan < num_chan; chan++)
2325                         init_r_port(i, aiop, chan, NULL);
2326         }
2327         is_PCI[i] = 0;
2328         if ((rcktpt_type[i] == ROCKET_TYPE_MODEM) || (rcktpt_type[i] == ROCKET_TYPE_MODEMII)) {
2329                 num_chan = sGetAiopNumChan(ctlp, 0);
2330                 total_num_chan = num_chan;
2331                 for (chan = 0; chan < num_chan; chan++)
2332                         sModemReset(ctlp, chan, 1);
2333                 mdelay(500);
2334                 for (chan = 0; chan < num_chan; chan++)
2335                         sModemReset(ctlp, chan, 0);
2336                 mdelay(500);
2337                 strcpy(rocketModel[i].modelString, "RocketModem ISA");
2338         } else {
2339                 strcpy(rocketModel[i].modelString, "RocketPort ISA");
2340         }
2341         rocketModel[i].numPorts = total_num_chan;
2342         rocketModel[i].model = MODEL_ISA;
2343
2344         printk(KERN_INFO "RocketPort ISA card #%d found at 0x%lx - %d AIOPs %s\n", 
2345                i, rcktpt_io_addr[i], num_aiops, type_string);
2346
2347         printk(KERN_INFO "Installing %s, creating /dev/ttyR%d - %ld\n",
2348                rocketModel[i].modelString,
2349                rocketModel[i].startingPortNumber,
2350                rocketModel[i].startingPortNumber +
2351                rocketModel[i].numPorts - 1);
2352
2353         return (1);
2354 }
2355
2356 static struct tty_operations rocket_ops = {
2357         .open = rp_open,
2358         .close = rp_close,
2359         .write = rp_write,
2360         .put_char = rp_put_char,
2361         .write_room = rp_write_room,
2362         .chars_in_buffer = rp_chars_in_buffer,
2363         .flush_buffer = rp_flush_buffer,
2364         .ioctl = rp_ioctl,
2365         .throttle = rp_throttle,
2366         .unthrottle = rp_unthrottle,
2367         .set_termios = rp_set_termios,
2368         .stop = rp_stop,
2369         .start = rp_start,
2370         .hangup = rp_hangup,
2371         .break_ctl = rp_break,
2372         .send_xchar = rp_send_xchar,
2373         .wait_until_sent = rp_wait_until_sent,
2374         .tiocmget = rp_tiocmget,
2375         .tiocmset = rp_tiocmset,
2376 };
2377
2378 /*
2379  * The module "startup" routine; it's run when the module is loaded.
2380  */
2381 int __init rp_init(void)
2382 {
2383         int retval, pci_boards_found, isa_boards_found, i;
2384
2385         printk(KERN_INFO "RocketPort device driver module, version %s, %s\n",
2386                ROCKET_VERSION, ROCKET_DATE);
2387
2388         rocket_driver = alloc_tty_driver(MAX_RP_PORTS);
2389         if (!rocket_driver)
2390                 return -ENOMEM;
2391
2392         /*
2393          * Set up the timer channel.
2394          */
2395         init_timer(&rocket_timer);
2396         rocket_timer.function = rp_do_poll;
2397
2398         /*
2399          * Initialize the array of pointers to our own internal state
2400          * structures.
2401          */
2402         memset(rp_table, 0, sizeof (rp_table));
2403         memset(xmit_flags, 0, sizeof (xmit_flags));
2404
2405         for (i = 0; i < MAX_RP_PORTS; i++)
2406                 lineNumbers[i] = 0;
2407         nextLineNumber = 0;
2408         memset(rocketModel, 0, sizeof (rocketModel));
2409
2410         /*
2411          *  If board 1 is non-zero, there is at least one ISA configured.  If controller is 
2412          *  zero, use the default controller IO address of board1 + 0x40.
2413          */
2414         if (board1) {
2415                 if (controller == 0)
2416                         controller = board1 + 0x40;
2417         } else {
2418                 controller = 0;  /*  Used as a flag, meaning no ISA boards */
2419         }
2420
2421         /*  If an ISA card is configured, reserve the 4 byte IO space for the Mudbac controller */
2422         if (controller && (!request_region(controller, 4, "Comtrol RocketPort"))) {
2423                 printk(KERN_INFO "Unable to reserve IO region for first configured ISA RocketPort controller 0x%lx.  Driver exiting \n", controller);
2424                 return -EBUSY;
2425         }
2426
2427         /*  Store ISA variable retrieved from command line or .conf file. */
2428         rcktpt_io_addr[0] = board1;
2429         rcktpt_io_addr[1] = board2;
2430         rcktpt_io_addr[2] = board3;
2431         rcktpt_io_addr[3] = board4;
2432
2433         rcktpt_type[0] = modem1 ? ROCKET_TYPE_MODEM : ROCKET_TYPE_NORMAL;
2434         rcktpt_type[0] = pc104_1[0] ? ROCKET_TYPE_PC104 : rcktpt_type[0];
2435         rcktpt_type[1] = modem2 ? ROCKET_TYPE_MODEM : ROCKET_TYPE_NORMAL;
2436         rcktpt_type[1] = pc104_2[0] ? ROCKET_TYPE_PC104 : rcktpt_type[1];
2437         rcktpt_type[2] = modem3 ? ROCKET_TYPE_MODEM : ROCKET_TYPE_NORMAL;
2438         rcktpt_type[2] = pc104_3[0] ? ROCKET_TYPE_PC104 : rcktpt_type[2];
2439         rcktpt_type[3] = modem4 ? ROCKET_TYPE_MODEM : ROCKET_TYPE_NORMAL;
2440         rcktpt_type[3] = pc104_4[0] ? ROCKET_TYPE_PC104 : rcktpt_type[3];
2441
2442         /*
2443          * Set up the tty driver structure and then register this
2444          * driver with the tty layer.
2445          */
2446
2447         rocket_driver->owner = THIS_MODULE;
2448         rocket_driver->flags = TTY_DRIVER_NO_DEVFS;
2449         rocket_driver->devfs_name = "tts/R";
2450         rocket_driver->name = "ttyR";
2451         rocket_driver->driver_name = "Comtrol RocketPort";
2452         rocket_driver->major = TTY_ROCKET_MAJOR;
2453         rocket_driver->minor_start = 0;
2454         rocket_driver->type = TTY_DRIVER_TYPE_SERIAL;
2455         rocket_driver->subtype = SERIAL_TYPE_NORMAL;
2456         rocket_driver->init_termios = tty_std_termios;
2457         rocket_driver->init_termios.c_cflag =
2458             B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2459 #ifdef ROCKET_SOFT_FLOW
2460         rocket_driver->flags |= TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
2461 #endif
2462         tty_set_operations(rocket_driver, &rocket_ops);
2463
2464         retval = tty_register_driver(rocket_driver);
2465         if (retval < 0) {
2466                 printk(KERN_INFO "Couldn't install tty RocketPort driver (error %d)\n", -retval);
2467                 put_tty_driver(rocket_driver);
2468                 return -1;
2469         }
2470
2471 #ifdef ROCKET_DEBUG_OPEN
2472         printk(KERN_INFO "RocketPort driver is major %d\n", rocket_driver.major);
2473 #endif
2474
2475         /*
2476          *  OK, let's probe each of the controllers looking for boards.  Any boards found
2477          *  will be initialized here.
2478          */
2479         isa_boards_found = 0;
2480         pci_boards_found = 0;
2481
2482         for (i = 0; i < NUM_BOARDS; i++) {
2483                 if (init_ISA(i))
2484                         isa_boards_found++;
2485         }
2486
2487 #ifdef CONFIG_PCI
2488         if (isa_boards_found < NUM_BOARDS)
2489                 pci_boards_found = init_PCI(isa_boards_found);
2490 #endif
2491
2492         max_board = pci_boards_found + isa_boards_found;
2493
2494         if (max_board == 0) {
2495                 printk(KERN_INFO "No rocketport ports found; unloading driver.\n");
2496                 del_timer_sync(&rocket_timer);
2497                 tty_unregister_driver(rocket_driver);
2498                 put_tty_driver(rocket_driver);
2499                 return -ENXIO;
2500         }
2501
2502         return 0;
2503 }
2504
2505 #ifdef MODULE
2506
2507 static void rp_cleanup_module(void)
2508 {
2509         int retval;
2510         int i;
2511
2512         del_timer_sync(&rocket_timer);
2513
2514         retval = tty_unregister_driver(rocket_driver);
2515         if (retval)
2516                 printk(KERN_INFO "Error %d while trying to unregister "
2517                        "rocketport driver\n", -retval);
2518         put_tty_driver(rocket_driver);
2519
2520         for (i = 0; i < MAX_RP_PORTS; i++) {
2521                 if (rp_table[i])
2522                         kfree(rp_table[i]);
2523         }
2524
2525         for (i = 0; i < NUM_BOARDS; i++) {
2526                 if (rcktpt_io_addr[i] <= 0 || is_PCI[i])
2527                         continue;
2528                 release_region(rcktpt_io_addr[i], 64);
2529         }
2530         if (controller)
2531                 release_region(controller, 4);
2532 }
2533 #endif
2534
2535 /***************************************************************************
2536 Function: sInitController
2537 Purpose:  Initialization of controller global registers and controller
2538           structure.
2539 Call:     sInitController(CtlP,CtlNum,MudbacIO,AiopIOList,AiopIOListSize,
2540                           IRQNum,Frequency,PeriodicOnly)
2541           CONTROLLER_T *CtlP; Ptr to controller structure
2542           int CtlNum; Controller number
2543           ByteIO_t MudbacIO; Mudbac base I/O address.
2544           ByteIO_t *AiopIOList; List of I/O addresses for each AIOP.
2545              This list must be in the order the AIOPs will be found on the
2546              controller.  Once an AIOP in the list is not found, it is
2547              assumed that there are no more AIOPs on the controller.
2548           int AiopIOListSize; Number of addresses in AiopIOList
2549           int IRQNum; Interrupt Request number.  Can be any of the following:
2550                          0: Disable global interrupts
2551                          3: IRQ 3
2552                          4: IRQ 4
2553                          5: IRQ 5
2554                          9: IRQ 9
2555                          10: IRQ 10
2556                          11: IRQ 11
2557                          12: IRQ 12
2558                          15: IRQ 15
2559           Byte_t Frequency: A flag identifying the frequency
2560                    of the periodic interrupt, can be any one of the following:
2561                       FREQ_DIS - periodic interrupt disabled
2562                       FREQ_137HZ - 137 Hertz
2563                       FREQ_69HZ - 69 Hertz
2564                       FREQ_34HZ - 34 Hertz
2565                       FREQ_17HZ - 17 Hertz
2566                       FREQ_9HZ - 9 Hertz
2567                       FREQ_4HZ - 4 Hertz
2568                    If IRQNum is set to 0 the Frequency parameter is
2569                    overidden, it is forced to a value of FREQ_DIS.
2570           int PeriodicOnly: 1 if all interrupts except the periodic
2571                                interrupt are to be blocked.
2572                             0 is both the periodic interrupt and
2573                                other channel interrupts are allowed.
2574                             If IRQNum is set to 0 the PeriodicOnly parameter is
2575                                overidden, it is forced to a value of 0.
2576 Return:   int: Number of AIOPs on the controller, or CTLID_NULL if controller
2577                initialization failed.
2578
2579 Comments:
2580           If periodic interrupts are to be disabled but AIOP interrupts
2581           are allowed, set Frequency to FREQ_DIS and PeriodicOnly to 0.
2582
2583           If interrupts are to be completely disabled set IRQNum to 0.
2584
2585           Setting Frequency to FREQ_DIS and PeriodicOnly to 1 is an
2586           invalid combination.
2587
2588           This function performs initialization of global interrupt modes,
2589           but it does not actually enable global interrupts.  To enable
2590           and disable global interrupts use functions sEnGlobalInt() and
2591           sDisGlobalInt().  Enabling of global interrupts is normally not
2592           done until all other initializations are complete.
2593
2594           Even if interrupts are globally enabled, they must also be
2595           individually enabled for each channel that is to generate
2596           interrupts.
2597
2598 Warnings: No range checking on any of the parameters is done.
2599
2600           No context switches are allowed while executing this function.
2601
2602           After this function all AIOPs on the controller are disabled,
2603           they can be enabled with sEnAiop().
2604 */
2605 static int sInitController(CONTROLLER_T * CtlP, int CtlNum, ByteIO_t MudbacIO,
2606                            ByteIO_t * AiopIOList, int AiopIOListSize,
2607                            int IRQNum, Byte_t Frequency, int PeriodicOnly)
2608 {
2609         int i;
2610         ByteIO_t io;
2611         int done;
2612
2613         CtlP->AiopIntrBits = aiop_intr_bits;
2614         CtlP->AltChanRingIndicator = 0;
2615         CtlP->CtlNum = CtlNum;
2616         CtlP->CtlID = CTLID_0001;       /* controller release 1 */
2617         CtlP->BusType = isISA;
2618         CtlP->MBaseIO = MudbacIO;
2619         CtlP->MReg1IO = MudbacIO + 1;
2620         CtlP->MReg2IO = MudbacIO + 2;
2621         CtlP->MReg3IO = MudbacIO + 3;
2622 #if 1
2623         CtlP->MReg2 = 0;        /* interrupt disable */
2624         CtlP->MReg3 = 0;        /* no periodic interrupts */
2625 #else
2626         if (sIRQMap[IRQNum] == 0) {     /* interrupts globally disabled */
2627                 CtlP->MReg2 = 0;        /* interrupt disable */
2628                 CtlP->MReg3 = 0;        /* no periodic interrupts */
2629         } else {
2630                 CtlP->MReg2 = sIRQMap[IRQNum];  /* set IRQ number */
2631                 CtlP->MReg3 = Frequency;        /* set frequency */
2632                 if (PeriodicOnly) {     /* periodic interrupt only */
2633                         CtlP->MReg3 |= PERIODIC_ONLY;
2634                 }
2635         }
2636 #endif
2637         sOutB(CtlP->MReg2IO, CtlP->MReg2);
2638         sOutB(CtlP->MReg3IO, CtlP->MReg3);
2639         sControllerEOI(CtlP);   /* clear EOI if warm init */
2640         /* Init AIOPs */
2641         CtlP->NumAiop = 0;
2642         for (i = done = 0; i < AiopIOListSize; i++) {
2643                 io = AiopIOList[i];
2644                 CtlP->AiopIO[i] = (WordIO_t) io;
2645                 CtlP->AiopIntChanIO[i] = io + _INT_CHAN;
2646                 sOutB(CtlP->MReg2IO, CtlP->MReg2 | (i & 0x03)); /* AIOP index */
2647                 sOutB(MudbacIO, (Byte_t) (io >> 6));    /* set up AIOP I/O in MUDBAC */
2648                 if (done)
2649                         continue;
2650                 sEnAiop(CtlP, i);       /* enable the AIOP */
2651                 CtlP->AiopID[i] = sReadAiopID(io);      /* read AIOP ID */
2652                 if (CtlP->AiopID[i] == AIOPID_NULL)     /* if AIOP does not exist */
2653                         done = 1;       /* done looking for AIOPs */
2654                 else {
2655                         CtlP->AiopNumChan[i] = sReadAiopNumChan((WordIO_t) io); /* num channels in AIOP */
2656                         sOutW((WordIO_t) io + _INDX_ADDR, _CLK_PRE);    /* clock prescaler */
2657                         sOutB(io + _INDX_DATA, sClockPrescale);
2658                         CtlP->NumAiop++;        /* bump count of AIOPs */
2659                 }
2660                 sDisAiop(CtlP, i);      /* disable AIOP */
2661         }
2662
2663         if (CtlP->NumAiop == 0)
2664                 return (-1);
2665         else
2666                 return (CtlP->NumAiop);
2667 }
2668
2669 /***************************************************************************
2670 Function: sPCIInitController
2671 Purpose:  Initialization of controller global registers and controller
2672           structure.
2673 Call:     sPCIInitController(CtlP,CtlNum,AiopIOList,AiopIOListSize,
2674                           IRQNum,Frequency,PeriodicOnly)
2675           CONTROLLER_T *CtlP; Ptr to controller structure
2676           int CtlNum; Controller number
2677           ByteIO_t *AiopIOList; List of I/O addresses for each AIOP.
2678              This list must be in the order the AIOPs will be found on the
2679              controller.  Once an AIOP in the list is not found, it is
2680              assumed that there are no more AIOPs on the controller.
2681           int AiopIOListSize; Number of addresses in AiopIOList
2682           int IRQNum; Interrupt Request number.  Can be any of the following:
2683                          0: Disable global interrupts
2684                          3: IRQ 3
2685                          4: IRQ 4
2686                          5: IRQ 5
2687                          9: IRQ 9
2688                          10: IRQ 10
2689                          11: IRQ 11
2690                          12: IRQ 12
2691                          15: IRQ 15
2692           Byte_t Frequency: A flag identifying the frequency
2693                    of the periodic interrupt, can be any one of the following:
2694                       FREQ_DIS - periodic interrupt disabled
2695                       FREQ_137HZ - 137 Hertz
2696                       FREQ_69HZ - 69 Hertz
2697                       FREQ_34HZ - 34 Hertz
2698                       FREQ_17HZ - 17 Hertz
2699                       FREQ_9HZ - 9 Hertz
2700                       FREQ_4HZ - 4 Hertz
2701                    If IRQNum is set to 0 the Frequency parameter is
2702                    overidden, it is forced to a value of FREQ_DIS.
2703           int PeriodicOnly: 1 if all interrupts except the periodic
2704                                interrupt are to be blocked.
2705                             0 is both the periodic interrupt and
2706                                other channel interrupts are allowed.
2707                             If IRQNum is set to 0 the PeriodicOnly parameter is
2708                                overidden, it is forced to a value of 0.
2709 Return:   int: Number of AIOPs on the controller, or CTLID_NULL if controller
2710                initialization failed.
2711
2712 Comments:
2713           If periodic interrupts are to be disabled but AIOP interrupts
2714           are allowed, set Frequency to FREQ_DIS and PeriodicOnly to 0.
2715
2716           If interrupts are to be completely disabled set IRQNum to 0.
2717
2718           Setting Frequency to FREQ_DIS and PeriodicOnly to 1 is an
2719           invalid combination.
2720
2721           This function performs initialization of global interrupt modes,
2722           but it does not actually enable global interrupts.  To enable
2723           and disable global interrupts use functions sEnGlobalInt() and
2724           sDisGlobalInt().  Enabling of global interrupts is normally not
2725           done until all other initializations are complete.
2726
2727           Even if interrupts are globally enabled, they must also be
2728           individually enabled for each channel that is to generate
2729           interrupts.
2730
2731 Warnings: No range checking on any of the parameters is done.
2732
2733           No context switches are allowed while executing this function.
2734
2735           After this function all AIOPs on the controller are disabled,
2736           they can be enabled with sEnAiop().
2737 */
2738 static int sPCIInitController(CONTROLLER_T * CtlP, int CtlNum,
2739                               ByteIO_t * AiopIOList, int AiopIOListSize,
2740                               WordIO_t ConfigIO, int IRQNum, Byte_t Frequency,
2741                               int PeriodicOnly, int altChanRingIndicator,
2742                               int UPCIRingInd)
2743 {
2744         int i;
2745         ByteIO_t io;
2746
2747         CtlP->AltChanRingIndicator = altChanRingIndicator;
2748         CtlP->UPCIRingInd = UPCIRingInd;
2749         CtlP->CtlNum = CtlNum;
2750         CtlP->CtlID = CTLID_0001;       /* controller release 1 */
2751         CtlP->BusType = isPCI;  /* controller release 1 */
2752
2753         if (ConfigIO) {
2754                 CtlP->isUPCI = 1;
2755                 CtlP->PCIIO = ConfigIO + _PCI_9030_INT_CTRL;
2756                 CtlP->PCIIO2 = ConfigIO + _PCI_9030_GPIO_CTRL;
2757                 CtlP->AiopIntrBits = upci_aiop_intr_bits;
2758         } else {
2759                 CtlP->isUPCI = 0;
2760                 CtlP->PCIIO =
2761                     (WordIO_t) ((ByteIO_t) AiopIOList[0] + _PCI_INT_FUNC);
2762                 CtlP->AiopIntrBits = aiop_intr_bits;
2763         }
2764
2765         sPCIControllerEOI(CtlP);        /* clear EOI if warm init */
2766         /* Init AIOPs */
2767         CtlP->NumAiop = 0;
2768         for (i = 0; i < AiopIOListSize; i++) {
2769                 io = AiopIOList[i];
2770                 CtlP->AiopIO[i] = (WordIO_t) io;
2771                 CtlP->AiopIntChanIO[i] = io + _INT_CHAN;
2772
2773                 CtlP->AiopID[i] = sReadAiopID(io);      /* read AIOP ID */
2774                 if (CtlP->AiopID[i] == AIOPID_NULL)     /* if AIOP does not exist */
2775                         break;  /* done looking for AIOPs */
2776
2777                 CtlP->AiopNumChan[i] = sReadAiopNumChan((WordIO_t) io); /* num channels in AIOP */
2778                 sOutW((WordIO_t) io + _INDX_ADDR, _CLK_PRE);    /* clock prescaler */
2779                 sOutB(io + _INDX_DATA, sClockPrescale);
2780                 CtlP->NumAiop++;        /* bump count of AIOPs */
2781         }
2782
2783         if (CtlP->NumAiop == 0)
2784                 return (-1);
2785         else
2786                 return (CtlP->NumAiop);
2787 }
2788
2789 /***************************************************************************
2790 Function: sReadAiopID
2791 Purpose:  Read the AIOP idenfication number directly from an AIOP.
2792 Call:     sReadAiopID(io)
2793           ByteIO_t io: AIOP base I/O address
2794 Return:   int: Flag AIOPID_XXXX if a valid AIOP is found, where X
2795                  is replace by an identifying number.
2796           Flag AIOPID_NULL if no valid AIOP is found
2797 Warnings: No context switches are allowed while executing this function.
2798
2799 */
2800 static int sReadAiopID(ByteIO_t io)
2801 {
2802         Byte_t AiopID;          /* ID byte from AIOP */
2803
2804         sOutB(io + _CMD_REG, RESET_ALL);        /* reset AIOP */
2805         sOutB(io + _CMD_REG, 0x0);
2806         AiopID = sInW(io + _CHN_STAT0) & 0x07;
2807         if (AiopID == 0x06)
2808                 return (1);
2809         else                    /* AIOP does not exist */
2810                 return (-1);
2811 }
2812
2813 /***************************************************************************
2814 Function: sReadAiopNumChan
2815 Purpose:  Read the number of channels available in an AIOP directly from
2816           an AIOP.
2817 Call:     sReadAiopNumChan(io)
2818           WordIO_t io: AIOP base I/O address
2819 Return:   int: The number of channels available
2820 Comments: The number of channels is determined by write/reads from identical
2821           offsets within the SRAM address spaces for channels 0 and 4.
2822           If the channel 4 space is mirrored to channel 0 it is a 4 channel
2823           AIOP, otherwise it is an 8 channel.
2824 Warnings: No context switches are allowed while executing this function.
2825 */
2826 static int sReadAiopNumChan(WordIO_t io)
2827 {
2828         Word_t x;
2829         static Byte_t R[4] = { 0x00, 0x00, 0x34, 0x12 };
2830
2831         /* write to chan 0 SRAM */
2832         sOutDW((DWordIO_t) io + _INDX_ADDR, *((DWord_t *) & R[0]));
2833         sOutW(io + _INDX_ADDR, 0);      /* read from SRAM, chan 0 */
2834         x = sInW(io + _INDX_DATA);
2835         sOutW(io + _INDX_ADDR, 0x4000); /* read from SRAM, chan 4 */
2836         if (x != sInW(io + _INDX_DATA)) /* if different must be 8 chan */
2837                 return (8);
2838         else
2839                 return (4);
2840 }
2841
2842 /***************************************************************************
2843 Function: sInitChan
2844 Purpose:  Initialization of a channel and channel structure
2845 Call:     sInitChan(CtlP,ChP,AiopNum,ChanNum)
2846           CONTROLLER_T *CtlP; Ptr to controller structure
2847           CHANNEL_T *ChP; Ptr to channel structure
2848           int AiopNum; AIOP number within controller
2849           int ChanNum; Channel number within AIOP
2850 Return:   int: 1 if initialization succeeded, 0 if it fails because channel
2851                number exceeds number of channels available in AIOP.
2852 Comments: This function must be called before a channel can be used.
2853 Warnings: No range checking on any of the parameters is done.
2854
2855           No context switches are allowed while executing this function.
2856 */
2857 static int sInitChan(CONTROLLER_T * CtlP, CHANNEL_T * ChP, int AiopNum,
2858                      int ChanNum)
2859 {
2860         int i;
2861         WordIO_t AiopIO;
2862         WordIO_t ChIOOff;
2863         Byte_t *ChR;
2864         Word_t ChOff;
2865         static Byte_t R[4];
2866         int brd9600;
2867
2868         if (ChanNum >= CtlP->AiopNumChan[AiopNum])
2869                 return 0;       /* exceeds num chans in AIOP */
2870
2871         /* Channel, AIOP, and controller identifiers */
2872         ChP->CtlP = CtlP;
2873         ChP->ChanID = CtlP->AiopID[AiopNum];
2874         ChP->AiopNum = AiopNum;
2875         ChP->ChanNum = ChanNum;
2876
2877         /* Global direct addresses */
2878         AiopIO = CtlP->AiopIO[AiopNum];
2879         ChP->Cmd = (ByteIO_t) AiopIO + _CMD_REG;
2880         ChP->IntChan = (ByteIO_t) AiopIO + _INT_CHAN;
2881         ChP->IntMask = (ByteIO_t) AiopIO + _INT_MASK;
2882         ChP->IndexAddr = (DWordIO_t) AiopIO + _INDX_ADDR;
2883         ChP->IndexData = AiopIO + _INDX_DATA;
2884
2885         /* Channel direct addresses */
2886         ChIOOff = AiopIO + ChP->ChanNum * 2;
2887         ChP->TxRxData = ChIOOff + _TD0;
2888         ChP->ChanStat = ChIOOff + _CHN_STAT0;
2889         ChP->TxRxCount = ChIOOff + _FIFO_CNT0;
2890         ChP->IntID = (ByteIO_t) AiopIO + ChP->ChanNum + _INT_ID0;
2891
2892         /* Initialize the channel from the RData array */
2893         for (i = 0; i < RDATASIZE; i += 4) {
2894                 R[0] = RData[i];
2895                 R[1] = RData[i + 1] + 0x10 * ChanNum;
2896                 R[2] = RData[i + 2];
2897                 R[3] = RData[i + 3];
2898                 sOutDW(ChP->IndexAddr, *((DWord_t *) & R[0]));
2899         }
2900
2901         ChR = ChP->R;
2902         for (i = 0; i < RREGDATASIZE; i += 4) {
2903                 ChR[i] = RRegData[i];
2904                 ChR[i + 1] = RRegData[i + 1] + 0x10 * ChanNum;
2905                 ChR[i + 2] = RRegData[i + 2];
2906                 ChR[i + 3] = RRegData[i + 3];
2907         }
2908
2909         /* Indexed registers */
2910         ChOff = (Word_t) ChanNum *0x1000;
2911
2912         if (sClockPrescale == 0x14)
2913                 brd9600 = 47;
2914         else
2915                 brd9600 = 23;
2916
2917         ChP->BaudDiv[0] = (Byte_t) (ChOff + _BAUD);
2918         ChP->BaudDiv[1] = (Byte_t) ((ChOff + _BAUD) >> 8);
2919         ChP->BaudDiv[2] = (Byte_t) brd9600;
2920         ChP->BaudDiv[3] = (Byte_t) (brd9600 >> 8);
2921         sOutDW(ChP->IndexAddr, *(DWord_t *) & ChP->BaudDiv[0]);
2922
2923         ChP->TxControl[0] = (Byte_t) (ChOff + _TX_CTRL);
2924         ChP->TxControl[1] = (Byte_t) ((ChOff + _TX_CTRL) >> 8);
2925         ChP->TxControl[2] = 0;
2926         ChP->TxControl[3] = 0;
2927         sOutDW(ChP->IndexAddr, *(DWord_t *) & ChP->TxControl[0]);
2928
2929         ChP->RxControl[0] = (Byte_t) (ChOff + _RX_CTRL);
2930         ChP->RxControl[1] = (Byte_t) ((ChOff + _RX_CTRL) >> 8);
2931         ChP->RxControl[2] = 0;
2932         ChP->RxControl[3] = 0;
2933         sOutDW(ChP->IndexAddr, *(DWord_t *) & ChP->RxControl[0]);
2934
2935         ChP->TxEnables[0] = (Byte_t) (ChOff + _TX_ENBLS);
2936         ChP->TxEnables[1] = (Byte_t) ((ChOff + _TX_ENBLS) >> 8);
2937         ChP->TxEnables[2] = 0;
2938         ChP->TxEnables[3] = 0;
2939         sOutDW(ChP->IndexAddr, *(DWord_t *) & ChP->TxEnables[0]);
2940
2941         ChP->TxCompare[0] = (Byte_t) (ChOff + _TXCMP1);
2942         ChP->TxCompare[1] = (Byte_t) ((ChOff + _TXCMP1) >> 8);
2943         ChP->TxCompare[2] = 0;
2944         ChP->TxCompare[3] = 0;
2945         sOutDW(ChP->IndexAddr, *(DWord_t *) & ChP->TxCompare[0]);
2946
2947         ChP->TxReplace1[0] = (Byte_t) (ChOff + _TXREP1B1);
2948         ChP->TxReplace1[1] = (Byte_t) ((ChOff + _TXREP1B1) >> 8);
2949         ChP->TxReplace1[2] = 0;
2950         ChP->TxReplace1[3] = 0;
2951         sOutDW(ChP->IndexAddr, *(DWord_t *) & ChP->TxReplace1[0]);
2952
2953         ChP->TxReplace2[0] = (Byte_t) (ChOff + _TXREP2);
2954         ChP->TxReplace2[1] = (Byte_t) ((ChOff + _TXREP2) >> 8);
2955         ChP->TxReplace2[2] = 0;
2956         ChP->TxReplace2[3] = 0;
2957         sOutDW(ChP->IndexAddr, *(DWord_t *) & ChP->TxReplace2[0]);
2958
2959         ChP->TxFIFOPtrs = ChOff + _TXF_OUTP;
2960         ChP->TxFIFO = ChOff + _TX_FIFO;
2961
2962         sOutB(ChP->Cmd, (Byte_t) ChanNum | RESTXFCNT);  /* apply reset Tx FIFO count */
2963         sOutB(ChP->Cmd, (Byte_t) ChanNum);      /* remove reset Tx FIFO count */
2964         sOutW((WordIO_t) ChP->IndexAddr, ChP->TxFIFOPtrs);      /* clear Tx in/out ptrs */
2965         sOutW(ChP->IndexData, 0);
2966         ChP->RxFIFOPtrs = ChOff + _RXF_OUTP;
2967         ChP->RxFIFO = ChOff + _RX_FIFO;
2968
2969         sOutB(ChP->Cmd, (Byte_t) ChanNum | RESRXFCNT);  /* apply reset Rx FIFO count */
2970         sOutB(ChP->Cmd, (Byte_t) ChanNum);      /* remove reset Rx FIFO count */
2971         sOutW((WordIO_t) ChP->IndexAddr, ChP->RxFIFOPtrs);      /* clear Rx out ptr */
2972         sOutW(ChP->IndexData, 0);
2973         sOutW((WordIO_t) ChP->IndexAddr, ChP->RxFIFOPtrs + 2);  /* clear Rx in ptr */
2974         sOutW(ChP->IndexData, 0);
2975         ChP->TxPrioCnt = ChOff + _TXP_CNT;
2976         sOutW((WordIO_t) ChP->IndexAddr, ChP->TxPrioCnt);
2977         sOutB(ChP->IndexData, 0);
2978         ChP->TxPrioPtr = ChOff + _TXP_PNTR;
2979         sOutW((WordIO_t) ChP->IndexAddr, ChP->TxPrioPtr);
2980         sOutB(ChP->IndexData, 0);
2981         ChP->TxPrioBuf = ChOff + _TXP_BUF;
2982         sEnRxProcessor(ChP);    /* start the Rx processor */
2983
2984         return 1;
2985 }
2986
2987 /***************************************************************************
2988 Function: sStopRxProcessor
2989 Purpose:  Stop the receive processor from processing a channel.
2990 Call:     sStopRxProcessor(ChP)
2991           CHANNEL_T *ChP; Ptr to channel structure
2992
2993 Comments: The receive processor can be started again with sStartRxProcessor().
2994           This function causes the receive processor to skip over the
2995           stopped channel.  It does not stop it from processing other channels.
2996
2997 Warnings: No context switches are allowed while executing this function.
2998
2999           Do not leave the receive processor stopped for more than one
3000           character time.
3001
3002           After calling this function a delay of 4 uS is required to ensure
3003           that the receive processor is no longer processing this channel.
3004 */
3005 static void sStopRxProcessor(CHANNEL_T * ChP)
3006 {
3007         Byte_t R[4];
3008
3009         R[0] = ChP->R[0];
3010         R[1] = ChP->R[1];
3011         R[2] = 0x0a;
3012         R[3] = ChP->R[3];
3013         sOutDW(ChP->IndexAddr, *(DWord_t *) & R[0]);
3014 }
3015
3016 /***************************************************************************
3017 Function: sFlushRxFIFO
3018 Purpose:  Flush the Rx FIFO
3019 Call:     sFlushRxFIFO(ChP)
3020           CHANNEL_T *ChP; Ptr to channel structure
3021 Return:   void
3022 Comments: To prevent data from being enqueued or dequeued in the Tx FIFO
3023           while it is being flushed the receive processor is stopped
3024           and the transmitter is disabled.  After these operations a
3025           4 uS delay is done before clearing the pointers to allow
3026           the receive processor to stop.  These items are handled inside
3027           this function.
3028 Warnings: No context switches are allowed while executing this function.
3029 */
3030 static void sFlushRxFIFO(CHANNEL_T * ChP)
3031 {
3032         int i;
3033         Byte_t Ch;              /* channel number within AIOP */
3034         int RxFIFOEnabled;      /* 1 if Rx FIFO enabled */
3035
3036         if (sGetRxCnt(ChP) == 0)        /* Rx FIFO empty */
3037                 return;         /* don't need to flush */
3038
3039         RxFIFOEnabled = 0;
3040         if (ChP->R[0x32] == 0x08) {     /* Rx FIFO is enabled */
3041                 RxFIFOEnabled = 1;
3042                 sDisRxFIFO(ChP);        /* disable it */
3043                 for (i = 0; i < 2000 / 200; i++)        /* delay 2 uS to allow proc to disable FIFO */
3044                         sInB(ChP->IntChan);     /* depends on bus i/o timing */
3045         }
3046         sGetChanStatus(ChP);    /* clear any pending Rx errors in chan stat */
3047         Ch = (Byte_t) sGetChanNum(ChP);
3048         sOutB(ChP->Cmd, Ch | RESRXFCNT);        /* apply reset Rx FIFO count */
3049         sOutB(ChP->Cmd, Ch);    /* remove reset Rx FIFO count */
3050         sOutW((WordIO_t) ChP->IndexAddr, ChP->RxFIFOPtrs);      /* clear Rx out ptr */
3051         sOutW(ChP->IndexData, 0);
3052         sOutW((WordIO_t) ChP->IndexAddr, ChP->RxFIFOPtrs + 2);  /* clear Rx in ptr */
3053         sOutW(ChP->IndexData, 0);
3054         if (RxFIFOEnabled)
3055                 sEnRxFIFO(ChP); /* enable Rx FIFO */
3056 }
3057
3058 /***************************************************************************
3059 Function: sFlushTxFIFO
3060 Purpose:  Flush the Tx FIFO
3061 Call:     sFlushTxFIFO(ChP)
3062           CHANNEL_T *ChP; Ptr to channel structure
3063 Return:   void
3064 Comments: To prevent data from being enqueued or dequeued in the Tx FIFO
3065           while it is being flushed the receive processor is stopped
3066           and the transmitter is disabled.  After these operations a
3067           4 uS delay is done before clearing the pointers to allow
3068           the receive processor to stop.  These items are handled inside
3069           this function.
3070 Warnings: No context switches are allowed while executing this function.
3071 */
3072 static void sFlushTxFIFO(CHANNEL_T * ChP)
3073 {
3074         int i;
3075         Byte_t Ch;              /* channel number within AIOP */
3076         int TxEnabled;          /* 1 if transmitter enabled */
3077
3078         if (sGetTxCnt(ChP) == 0)        /* Tx FIFO empty */
3079                 return;         /* don't need to flush */
3080
3081         TxEnabled = 0;
3082         if (ChP->TxControl[3] & TX_ENABLE) {
3083                 TxEnabled = 1;
3084                 sDisTransmit(ChP);      /* disable transmitter */
3085         }
3086         sStopRxProcessor(ChP);  /* stop Rx processor */
3087         for (i = 0; i < 4000 / 200; i++)        /* delay 4 uS to allow proc to stop */
3088                 sInB(ChP->IntChan);     /* depends on bus i/o timing */
3089         Ch = (Byte_t) sGetChanNum(ChP);
3090         sOutB(ChP->Cmd, Ch | RESTXFCNT);        /* apply reset Tx FIFO count */
3091         sOutB(ChP->Cmd, Ch);    /* remove reset Tx FIFO count */
3092         sOutW((WordIO_t) ChP->IndexAddr, ChP->TxFIFOPtrs);      /* clear Tx in/out ptrs */
3093         sOutW(ChP->IndexData, 0);
3094         if (TxEnabled)
3095                 sEnTransmit(ChP);       /* enable transmitter */
3096         sStartRxProcessor(ChP); /* restart Rx processor */
3097 }
3098
3099 /***************************************************************************
3100 Function: sWriteTxPrioByte
3101 Purpose:  Write a byte of priority transmit data to a channel
3102 Call:     sWriteTxPrioByte(ChP,Data)
3103           CHANNEL_T *ChP; Ptr to channel structure
3104           Byte_t Data; The transmit data byte
3105
3106 Return:   int: 1 if the bytes is successfully written, otherwise 0.
3107
3108 Comments: The priority byte is transmitted before any data in the Tx FIFO.
3109
3110 Warnings: No context switches are allowed while executing this function.
3111 */
3112 static int sWriteTxPrioByte(CHANNEL_T * ChP, Byte_t Data)
3113 {
3114         Byte_t DWBuf[4];        /* buffer for double word writes */
3115         Word_t *WordPtr;        /* must be far because Win SS != DS */
3116         register DWordIO_t IndexAddr;
3117
3118         if (sGetTxCnt(ChP) > 1) {       /* write it to Tx priority buffer */
3119                 IndexAddr = ChP->IndexAddr;
3120                 sOutW((WordIO_t) IndexAddr, ChP->TxPrioCnt);    /* get priority buffer status */
3121                 if (sInB((ByteIO_t) ChP->IndexData) & PRI_PEND) /* priority buffer busy */
3122                         return (0);     /* nothing sent */
3123
3124                 WordPtr = (Word_t *) (&DWBuf[0]);
3125                 *WordPtr = ChP->TxPrioBuf;      /* data byte address */
3126
3127                 DWBuf[2] = Data;        /* data byte value */
3128                 sOutDW(IndexAddr, *((DWord_t *) (&DWBuf[0])));  /* write it out */
3129
3130                 *WordPtr = ChP->TxPrioCnt;      /* Tx priority count address */
3131
3132                 DWBuf[2] = PRI_PEND + 1;        /* indicate 1 byte pending */
3133                 DWBuf[3] = 0;   /* priority buffer pointer */
3134                 sOutDW(IndexAddr, *((DWord_t *) (&DWBuf[0])));  /* write it out */
3135         } else {                /* write it to Tx FIFO */
3136
3137                 sWriteTxByte(sGetTxRxDataIO(ChP), Data);
3138         }
3139         return (1);             /* 1 byte sent */
3140 }
3141
3142 /***************************************************************************
3143 Function: sEnInterrupts
3144 Purpose:  Enable one or more interrupts for a channel
3145 Call:     sEnInterrupts(ChP,Flags)
3146           CHANNEL_T *ChP; Ptr to channel structure
3147           Word_t Flags: Interrupt enable flags, can be any combination
3148              of the following flags:
3149                 TXINT_EN:   Interrupt on Tx FIFO empty
3150                 RXINT_EN:   Interrupt on Rx FIFO at trigger level (see
3151                             sSetRxTrigger())
3152                 SRCINT_EN:  Interrupt on SRC (Special Rx Condition)
3153                 MCINT_EN:   Interrupt on modem input change
3154                 CHANINT_EN: Allow channel interrupt signal to the AIOP's
3155                             Interrupt Channel Register.
3156 Return:   void
3157 Comments: If an interrupt enable flag is set in Flags, that interrupt will be
3158           enabled.  If an interrupt enable flag is not set in Flags, that
3159           interrupt will not be changed.  Interrupts can be disabled with
3160           function sDisInterrupts().
3161
3162           This function sets the appropriate bit for the channel in the AIOP's
3163           Interrupt Mask Register if the CHANINT_EN flag is set.  This allows
3164           this channel's bit to be set in the AIOP's Interrupt Channel Register.
3165
3166           Interrupts must also be globally enabled before channel interrupts
3167           will be passed on to the host.  This is done with function
3168           sEnGlobalInt().
3169
3170           In some cases it may be desirable to disable interrupts globally but
3171           enable channel interrupts.  This would allow the global interrupt
3172           status register to be used to determine which AIOPs need service.
3173 */
3174 static void sEnInterrupts(CHANNEL_T * ChP, Word_t Flags)
3175 {
3176         Byte_t Mask;            /* Interrupt Mask Register */
3177
3178         ChP->RxControl[2] |=
3179             ((Byte_t) Flags & (RXINT_EN | SRCINT_EN | MCINT_EN));
3180
3181         sOutDW(ChP->IndexAddr, *(DWord_t *) & ChP->RxControl[0]);
3182
3183         ChP->TxControl[2] |= ((Byte_t) Flags & TXINT_EN);
3184
3185         sOutDW(ChP->IndexAddr, *(DWord_t *) & ChP->TxControl[0]);
3186
3187         if (Flags & CHANINT_EN) {
3188                 Mask = sInB(ChP->IntMask) | sBitMapSetTbl[ChP->ChanNum];
3189                 sOutB(ChP->IntMask, Mask);
3190         }
3191 }
3192
3193 /***************************************************************************
3194 Function: sDisInterrupts
3195 Purpose:  Disable one or more interrupts for a channel
3196 Call:     sDisInterrupts(ChP,Flags)
3197           CHANNEL_T *ChP; Ptr to channel structure
3198           Word_t Flags: Interrupt flags, can be any combination
3199              of the following flags:
3200                 TXINT_EN:   Interrupt on Tx FIFO empty
3201                 RXINT_EN:   Interrupt on Rx FIFO at trigger level (see
3202                             sSetRxTrigger())
3203                 SRCINT_EN:  Interrupt on SRC (Special Rx Condition)
3204                 MCINT_EN:   Interrupt on modem input change
3205                 CHANINT_EN: Disable channel interrupt signal to the
3206                             AIOP's Interrupt Channel Register.
3207 Return:   void
3208 Comments: If an interrupt flag is set in Flags, that interrupt will be
3209           disabled.  If an interrupt flag is not set in Flags, that
3210           interrupt will not be changed.  Interrupts can be enabled with
3211           function sEnInterrupts().
3212
3213           This function clears the appropriate bit for the channel in the AIOP's
3214           Interrupt Mask Register if the CHANINT_EN flag is set.  This blocks
3215           this channel's bit from being set in the AIOP's Interrupt Channel
3216           Register.
3217 */
3218 static void sDisInterrupts(CHANNEL_T * ChP, Word_t Flags)
3219 {
3220         Byte_t Mask;            /* Interrupt Mask Register */
3221
3222         ChP->RxControl[2] &=
3223             ~((Byte_t) Flags & (RXINT_EN | SRCINT_EN | MCINT_EN));
3224         sOutDW(ChP->IndexAddr, *(DWord_t *) & ChP->RxControl[0]);
3225         ChP->TxControl[2] &= ~((Byte_t) Flags & TXINT_EN);
3226         sOutDW(ChP->IndexAddr, *(DWord_t *) & ChP->TxControl[0]);
3227
3228         if (Flags & CHANINT_EN) {
3229                 Mask = sInB(ChP->IntMask) & sBitMapClrTbl[ChP->ChanNum];
3230                 sOutB(ChP->IntMask, Mask);
3231         }
3232 }
3233
3234 static void sSetInterfaceMode(CHANNEL_T * ChP, Byte_t mode)
3235 {
3236         sOutB(ChP->CtlP->AiopIO[2], (mode & 0x18) | ChP->ChanNum);
3237 }
3238
3239 /*
3240  *  Not an official SSCI function, but how to reset RocketModems.
3241  *  ISA bus version
3242  */
3243 static void sModemReset(CONTROLLER_T * CtlP, int chan, int on)
3244 {
3245         ByteIO_t addr;
3246         Byte_t val;
3247
3248         addr = CtlP->AiopIO[0] + 0x400;
3249         val = sInB(CtlP->MReg3IO);
3250         /* if AIOP[1] is not enabled, enable it */
3251         if ((val & 2) == 0) {
3252                 val = sInB(CtlP->MReg2IO);
3253                 sOutB(CtlP->MReg2IO, (val & 0xfc) | (1 & 0x03));
3254                 sOutB(CtlP->MBaseIO, (unsigned char) (addr >> 6));
3255         }
3256
3257         sEnAiop(CtlP, 1);
3258         if (!on)
3259                 addr += 8;
3260         sOutB(addr + chan, 0);  /* apply or remove reset */
3261         sDisAiop(CtlP, 1);
3262 }
3263
3264 /*
3265  *  Not an official SSCI function, but how to reset RocketModems.
3266  *  PCI bus version
3267  */
3268 static void sPCIModemReset(CONTROLLER_T * CtlP, int chan, int on)
3269 {
3270         ByteIO_t addr;
3271
3272         addr = CtlP->AiopIO[0] + 0x40;  /* 2nd AIOP */
3273         if (!on)
3274                 addr += 8;
3275         sOutB(addr + chan, 0);  /* apply or remove reset */
3276 }
3277
3278 /*  Resets the speaker controller on RocketModem II and III devices */
3279 static void rmSpeakerReset(CONTROLLER_T * CtlP, unsigned long model)
3280 {
3281         ByteIO_t addr;
3282
3283         /* RocketModem II speaker control is at the 8th port location of offset 0x40 */
3284         if ((model == MODEL_RP4M) || (model == MODEL_RP6M)) {
3285                 addr = CtlP->AiopIO[0] + 0x4F;
3286                 sOutB(addr, 0);
3287         }
3288
3289         /* RocketModem III speaker control is at the 1st port location of offset 0x80 */
3290         if ((model == MODEL_UPCI_RM3_8PORT)
3291             || (model == MODEL_UPCI_RM3_4PORT)) {
3292                 addr = CtlP->AiopIO[0] + 0x88;
3293                 sOutB(addr, 0);
3294         }
3295 }
3296
3297 /*  Returns the line number given the controller (board), aiop and channel number */
3298 static unsigned char GetLineNumber(int ctrl, int aiop, int ch)
3299 {
3300         return lineNumbers[(ctrl << 5) | (aiop << 3) | ch];
3301 }
3302
3303 /*
3304  *  Stores the line number associated with a given controller (board), aiop
3305  *  and channel number.  
3306  *  Returns:  The line number assigned 
3307  */
3308 static unsigned char SetLineNumber(int ctrl, int aiop, int ch)
3309 {
3310         lineNumbers[(ctrl << 5) | (aiop << 3) | ch] = nextLineNumber++;
3311         return (nextLineNumber - 1);
3312 }