vserver 1.9.5.x5
[linux-2.6.git] / drivers / char / rio / riotty.c
1 /*
2 ** -----------------------------------------------------------------------------
3 **
4 **  Perle Specialix driver for Linux
5 **  Ported from existing RIO Driver for SCO sources.
6  *
7  *  (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8  *
9  *      This program is free software; you can redistribute it and/or modify
10  *      it under the terms of the GNU General Public License as published by
11  *      the Free Software Foundation; either version 2 of the License, or
12  *      (at your option) any later version.
13  *
14  *      This program is distributed in the hope that it will be useful,
15  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *      GNU General Public License for more details.
18  *
19  *      You should have received a copy of the GNU General Public License
20  *      along with this program; if not, write to the Free Software
21  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 **
23 **      Module          : riotty.c
24 **      SID             : 1.3
25 **      Last Modified   : 11/6/98 10:33:47
26 **      Retrieved       : 11/6/98 10:33:50
27 **
28 **  ident @(#)riotty.c  1.3
29 **
30 ** -----------------------------------------------------------------------------
31 */
32 #ifdef SCCS_LABELS
33 static char *_riotty_c_sccs_ = "@(#)riotty.c    1.3";
34 #endif
35
36
37 #define __EXPLICIT_DEF_H__
38
39 #include <linux/module.h>
40 #include <linux/slab.h>
41 #include <linux/errno.h>
42 #include <linux/tty.h>
43 #include <linux/string.h>
44 #include <asm/io.h>
45 #include <asm/system.h>
46 #include <asm/string.h>
47 #include <asm/semaphore.h>
48 #include <asm/uaccess.h>
49
50 #include <linux/termios.h>
51
52 #include <linux/serial.h>
53
54 #include <linux/generic_serial.h>
55
56
57 #include "linux_compat.h"
58 #include "rio_linux.h"
59 #include "typdef.h"
60 #include "pkt.h"
61 #include "daemon.h"
62 #include "rio.h"
63 #include "riospace.h"
64 #include "top.h"
65 #include "cmdpkt.h"
66 #include "map.h"
67 #include "riotypes.h"
68 #include "rup.h"
69 #include "port.h"
70 #include "riodrvr.h"
71 #include "rioinfo.h"
72 #include "func.h"
73 #include "errors.h"
74 #include "pci.h"
75
76 #include "parmmap.h"
77 #include "unixrup.h"
78 #include "board.h"
79 #include "host.h"
80 #include "error.h"
81 #include "phb.h"
82 #include "link.h"
83 #include "cmdblk.h"
84 #include "route.h"
85 #include "control.h"
86 #include "cirrus.h"
87 #include "rioioctl.h"
88 #include "param.h"
89 #include "list.h"
90 #include "sam.h"
91
92 #if 0
93 static void ttyseth_pv(struct Port *, struct ttystatics *, 
94                                 struct termios *sg, int);
95 #endif
96
97 static void RIOClearUp(struct Port *PortP);
98 int RIOShortCommand(struct rio_info *p, struct Port *PortP, 
99                            int command, int len, int arg);
100
101 #if 0
102 static int RIOCookMode(struct ttystatics *);
103 #endif
104
105 extern int      conv_vb[];      /* now defined in ttymgr.c */
106 extern int      conv_bv[];      /* now defined in ttymgr.c */
107  
108 /*
109 ** 16.09.1998 ARG - Fix to build riotty.k.o for Modular Kernel Support
110 **
111 ** ep.def.h is necessary for Modular Kernel Support
112 ** DO NOT place any kernel 'extern's after this line
113 ** or this source file will not build riotty.k.o
114 */
115 #ifdef uLYNX
116 #include <ep.def.h>
117 #endif
118
119 #ifdef NEED_THIS2
120 static struct old_sgttyb 
121 default_sg = 
122
123         B19200, B19200,                         /* input and output speed */ 
124         'H' - '@',                                      /* erase char */ 
125         -1,                                                     /* 2nd erase char */ 
126         'U' - '@',                                      /* kill char */ 
127         ECHO | CRMOD,                           /* mode */ 
128         'C' - '@',                                      /* interrupt character */ 
129         '\\' - '@',                                     /* quit char */ 
130         'Q' - '@',                                      /* start char */
131         'S' - '@',                                      /* stop char */ 
132         'D' - '@',                                      /* EOF */
133         -1,                                                     /* brk */
134         (LCRTBS | LCRTERA | LCRTKIL | LCTLECH), /* local mode word */ 
135         'Z' - '@',                                      /* process stop */
136         'Y' - '@',                                      /* delayed stop */
137         'R' - '@',                                      /* reprint line */ 
138         'O' - '@',                                      /* flush output */
139         'W' - '@',                                      /* word erase */
140         'V' - '@'                                       /* literal next char */
141 };
142 #endif
143
144
145 extern struct rio_info *p;
146
147
148 int
149 riotopen(struct tty_struct * tty, struct file * filp)
150 {
151         register uint SysPort;
152         int Modem;
153         int repeat_this = 250;
154         struct Port *PortP;              /* pointer to the port structure */
155         unsigned long flags;
156         int retval = 0;
157
158         func_enter ();
159
160         /* Make sure driver_data is NULL in case the rio isn't booted jet. Else gs_close
161            is going to oops.
162         */
163         tty->driver_data = NULL;
164         
165         SysPort = rio_minor(tty);
166         Modem   = rio_ismodem(tty);
167
168         if ( p->RIOFailed ) {
169                 rio_dprintk (RIO_DEBUG_TTY, "System initialisation failed\n");
170                 pseterr(ENXIO);
171                 func_exit ();
172                 return -ENXIO;
173         }
174
175         rio_dprintk (RIO_DEBUG_TTY, "port open SysPort %d (%s) (mapped:%d)\n",
176                SysPort,  Modem ? "Modem" : "tty",
177                                    p->RIOPortp[SysPort]->Mapped);
178
179         /*
180         ** Validate that we have received a legitimate request.
181         ** Currently, just check that we are opening a port on
182         ** a host card that actually exists, and that the port
183         ** has been mapped onto a host.
184         */
185         if (SysPort >= RIO_PORTS) {     /* out of range ? */
186                 rio_dprintk (RIO_DEBUG_TTY, "Illegal port number %d\n",SysPort);
187                 pseterr(ENXIO);
188                 func_exit();
189                 return -ENXIO;
190         }
191
192         /*
193         ** Grab pointer to the port stucture
194         */
195         PortP = p->RIOPortp[SysPort];   /* Get control struc */
196         rio_dprintk (RIO_DEBUG_TTY, "PortP: %p\n", PortP);
197         if ( !PortP->Mapped ) { /* we aren't mapped yet! */
198                 /*
199                 ** The system doesn't know which RTA this port
200                 ** corresponds to.
201                 */
202                 rio_dprintk (RIO_DEBUG_TTY, "port not mapped into system\n");
203                 func_exit ();
204                 pseterr(ENXIO);
205                 return -ENXIO;
206         }
207
208         tty->driver_data = PortP;
209
210         PortP->gs.tty = tty;
211         PortP->gs.count++;
212
213         rio_dprintk (RIO_DEBUG_TTY, "%d bytes in tx buffer\n",
214                                    PortP->gs.xmit_cnt);
215
216         retval = gs_init_port (&PortP->gs);
217         if (retval) {
218                 PortP->gs.count--;
219                 return -ENXIO;
220         }
221         /*
222         ** If the host hasn't been booted yet, then 
223         ** fail
224         */
225         if ( (PortP->HostP->Flags & RUN_STATE) != RC_RUNNING ) {
226                 rio_dprintk (RIO_DEBUG_TTY, "Host not running\n");
227                 pseterr(ENXIO);
228                 func_exit ();
229                 return -ENXIO;
230         }
231
232         /*
233         ** If the RTA has not booted yet and the user has choosen to block
234         ** until the RTA is present then we must spin here waiting for
235         ** the RTA to boot.
236         */
237 #if 0
238         if (!(PortP->HostP->Mapping[PortP->RupNum].Flags & RTA_BOOTED)) {
239                 if (PortP->WaitUntilBooted) {
240                         rio_dprintk (RIO_DEBUG_TTY, "Waiting for RTA to boot\n");
241                         do {
242                                 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
243                                         rio_dprintk (RIO_DEBUG_TTY, "RTA EINTR in delay \n");
244                                         func_exit ();
245                                         return -EINTR;
246                                 }
247                                 if (repeat_this -- <= 0) {
248                                         rio_dprintk (RIO_DEBUG_TTY, "Waiting for RTA to boot timeout\n");
249                                         RIOPreemptiveCmd(p, PortP, FCLOSE ); 
250                                         pseterr(EINTR);
251                                         func_exit ();
252                                         return -EIO;
253                                 }
254                         } while(!(PortP->HostP->Mapping[PortP->RupNum].Flags & RTA_BOOTED));
255                         rio_dprintk (RIO_DEBUG_TTY, "RTA has been booted\n");
256                 } else {
257                         rio_dprintk (RIO_DEBUG_TTY, "RTA never booted\n");
258                         pseterr(ENXIO);
259                         func_exit ();
260                         return 0;
261                 }
262         }
263 #else
264         /* I find the above code a bit hairy. I find the below code
265            easier to read and shorter. Now, if it works too that would
266            be great... -- REW 
267         */
268         rio_dprintk (RIO_DEBUG_TTY, "Checking if RTA has booted... \n");
269         while (!(PortP->HostP->Mapping[PortP->RupNum].Flags & RTA_BOOTED)) {
270           if (!PortP->WaitUntilBooted) {
271             rio_dprintk (RIO_DEBUG_TTY, "RTA never booted\n");
272             func_exit ();
273             return -ENXIO;
274           }
275
276           /* Under Linux you'd normally use a wait instead of this
277              busy-waiting. I'll stick with the old implementation for
278              now. --REW 
279           */
280           if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
281             rio_dprintk (RIO_DEBUG_TTY, "RTA_wait_for_boot: EINTR in delay \n");
282             func_exit ();
283             return -EINTR;
284           }
285           if (repeat_this -- <= 0) {
286             rio_dprintk (RIO_DEBUG_TTY, "Waiting for RTA to boot timeout\n");
287             func_exit ();
288             return -EIO;
289           }
290         }
291         rio_dprintk (RIO_DEBUG_TTY, "RTA has been booted\n");
292 #endif
293 #if 0
294         tp =  PortP->TtyP;              /* get tty struct */
295 #endif
296         rio_spin_lock_irqsave(&PortP->portSem, flags);
297         if ( p->RIOHalted ) {
298                 goto bombout;
299         }
300 #if 0
301         retval = gs_init_port(&PortP->gs);
302         if (retval){
303                 func_exit ();
304                 return retval;
305         }
306 #endif
307
308         /*
309         ** If the port is in the final throws of being closed,
310         ** we should wait here (politely), waiting
311         ** for it to finish, so that it doesn't close us!
312         */
313         while ( (PortP->State & RIO_CLOSING) && !p->RIOHalted ) {
314                 rio_dprintk (RIO_DEBUG_TTY, "Waiting for RIO_CLOSING to go away\n");
315                 if (repeat_this -- <= 0) {
316                         rio_dprintk (RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
317                         RIOPreemptiveCmd(p, PortP, FCLOSE ); 
318                         retval = -EINTR;
319                         goto bombout;
320                 }
321                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
322                 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
323                         rio_spin_lock_irqsave(&PortP->portSem, flags); 
324                         retval = -EINTR;
325                         goto bombout;
326                 }
327                 rio_spin_lock_irqsave(&PortP->portSem, flags); 
328         }
329
330         if ( !PortP->Mapped ) {
331                 rio_dprintk (RIO_DEBUG_TTY, "Port unmapped while closing!\n");
332                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
333                 retval = -ENXIO;
334                 func_exit ();
335                 return retval;
336         }
337
338         if ( p->RIOHalted ) {
339                 goto bombout;
340         }
341
342 /*
343 ** 15.10.1998 ARG - ESIL 0761 part fix
344 ** RIO has it's own CTSFLOW and RTSFLOW flags in 'Config' in the port structure,
345 ** we need to make sure that the flags are clear when the port is opened.
346 */
347         /* Uh? Suppose I turn these on and then another process opens
348            the port again? The flags get cleared! Not good. -- REW */
349         if ( !(PortP->State & (RIO_LOPEN | RIO_MOPEN)) ) {
350                 PortP->Config &= ~(RIO_CTSFLOW|RIO_RTSFLOW);
351         }
352
353         if (!(PortP->firstOpen)) {      /* First time ? */
354                 rio_dprintk (RIO_DEBUG_TTY, "First open for this port\n");
355         
356
357                 PortP->firstOpen++;
358                 PortP->CookMode = 0; /* XXX RIOCookMode(tp); */
359                 PortP->InUse = NOT_INUSE;
360
361                 /* Tentative fix for bug PR27. Didn't work. */
362                 /* PortP->gs.xmit_cnt = 0; */
363
364                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
365 #ifdef NEED_THIS
366                 ttyseth(PortP, tp, (struct old_sgttyb *)&default_sg);
367 #endif
368
369                 /* Someone explain to me why this delay/config is
370                    here. If I read the docs correctly the "open"
371                    command piggybacks the parameters immediately. 
372                    -- REW */
373                 RIOParam(PortP,OPEN,Modem,OK_TO_SLEEP);         /* Open the port */
374 #if 0
375                 /* This delay of 1 second was annoying. I removed it. -- REW */
376                 RIODelay(PortP, HUNDRED_MS*10);
377                 RIOParam(PortP,CONFIG,Modem,OK_TO_SLEEP);       /* Config the port */
378 #endif
379                 rio_spin_lock_irqsave(&PortP->portSem, flags);
380
381                 /*
382                 ** wait for the port to be not closed.
383                 */
384                 while ( !(PortP->PortState & PORT_ISOPEN) && !p->RIOHalted ) {
385                         rio_dprintk (RIO_DEBUG_TTY, "Waiting for PORT_ISOPEN-currently %x\n",PortP->PortState);
386 /*
387 ** 15.10.1998 ARG - ESIL 0759
388 ** (Part) fix for port being trashed when opened whilst RTA "disconnected"
389 ** Take out the limited wait - now wait for ever or until user
390 ** bangs us out.
391 **
392                         if (repeat_this -- <= 0) {
393                                 rio_dprint(RIO_DEBUG_TTY, ("Waiting for open to finish timed out.\n"));
394                                 RIOPreemptiveCmd(p, PortP, FCLOSE ); 
395                                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
396                                 return -EINTR;
397                         }
398 **
399 */
400                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
401                         if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
402                                 rio_dprintk (RIO_DEBUG_TTY, "Waiting for open to finish broken by signal\n");
403                                 RIOPreemptiveCmd(p, PortP, FCLOSE );
404                                 func_exit ();
405                                 return -EINTR;
406                         }
407                         rio_spin_lock_irqsave(&PortP->portSem, flags);
408                 }
409
410                 if ( p->RIOHalted ) {
411                   retval = -EIO;
412 bombout:
413                   /*                    RIOClearUp( PortP ); */
414                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
415                         return retval;
416                 }
417                 rio_dprintk (RIO_DEBUG_TTY, "PORT_ISOPEN found\n");
418         }
419
420 #ifdef MODEM_SUPPORT 
421         if (Modem) {
422                 rio_dprintk (RIO_DEBUG_TTY, "Modem - test for carrier\n");
423                 /*
424                 ** ACTION
425                 ** insert test for carrier here. -- ???
426                 ** I already see that test here. What's the deal? -- REW
427                 */
428                 if ((PortP->gs.tty->termios->c_cflag & CLOCAL) || (PortP->ModemState & MSVR1_CD))
429                 {
430                         rio_dprintk (RIO_DEBUG_TTY, "open(%d) Modem carr on\n", SysPort);
431                         /*
432                         tp->tm.c_state |= CARR_ON;
433                         wakeup((caddr_t) &tp->tm.c_canq);
434                         */
435                         PortP->State |= RIO_CARR_ON;
436                         wake_up_interruptible (&PortP->gs.open_wait);
437                 }
438                 else /* no carrier - wait for DCD */
439                 {
440                   /*
441                         while (!(PortP->gs.tty->termios->c_state & CARR_ON) && 
442                                !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted )
443                   */
444                         while (!(PortP->State & RIO_CARR_ON) && 
445                                !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted ) {
446
447                                 rio_dprintk (RIO_DEBUG_TTY, "open(%d) sleeping for carr on\n",SysPort);
448                                 /*
449                                 PortP->gs.tty->termios->c_state |= WOPEN;
450                                 */
451                                 PortP->State |= RIO_WOPEN;
452                                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
453                                 if (RIODelay (PortP, HUNDRED_MS) == RIO_FAIL)
454 #if 0
455                                 if ( sleep((caddr_t)&tp->tm.c_canqo, TTIPRI|PCATCH))
456 #endif
457                                 {
458                                         /*
459                                         ** ACTION: verify that this is a good thing
460                                         ** to do here. -- ???
461                                         ** I think it's OK. -- REW
462                                         */
463                                         rio_dprintk (RIO_DEBUG_TTY, "open(%d) sleeping for carr broken by signal\n",
464                                                SysPort);
465                                         RIOPreemptiveCmd( p, PortP, FCLOSE );
466                                         /*
467                                         tp->tm.c_state &= ~WOPEN;
468                                         */
469                                         PortP->State &= ~RIO_WOPEN;
470                                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
471                                         func_exit ();
472                                         return -EINTR;
473                                 }
474                         }
475                         PortP->State &= ~RIO_WOPEN;
476                 }
477                 if ( p->RIOHalted )
478                         goto bombout;
479                 rio_dprintk (RIO_DEBUG_TTY, "Setting RIO_MOPEN\n");
480                 PortP->State |= RIO_MOPEN;
481         }
482         else
483 #endif
484         {
485                 /*
486                 ** ACTION
487                 ** Direct line open - force carrier (will probably mean
488                 ** that sleeping Modem line fubar)
489                 */
490                 PortP->State |= RIO_LOPEN;
491         }
492
493         if ( p->RIOHalted ) {
494                 goto bombout;
495         }
496
497         rio_dprintk (RIO_DEBUG_TTY, "high level open done\n");
498
499 #ifdef STATS
500         PortP->Stat.OpenCnt++;
501 #endif
502         /*
503         ** Count opens for port statistics reporting
504         */
505         if (PortP->statsGather)
506                 PortP->opens++;
507
508         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
509         rio_dprintk (RIO_DEBUG_TTY, "Returning from open\n");
510         func_exit ();
511         return 0;
512 }
513
514 /*
515 ** RIOClose the port.
516 ** The operating system thinks that this is last close for the device.
517 ** As there are two interfaces to the port (Modem and tty), we need to
518 ** check that both are closed before we close the device.
519 */ 
520 int
521 riotclose(void  *ptr)
522 {
523 #if 0
524         register uint SysPort = dev;
525         struct ttystatics *tp;          /* pointer to our ttystruct */
526 #endif
527         struct Port *PortP =ptr;        /* pointer to the port structure */
528         int deleted = 0;
529         int     try = -1; /* Disable the timeouts by setting them to -1 */
530         int     repeat_this = -1; /* Congrats to those having 15 years of 
531                                      uptime! (You get to break the driver.) */
532         long end_time;
533         struct tty_struct * tty;
534         unsigned long flags;
535         int Modem;
536         int rv =0;
537         
538         rio_dprintk (RIO_DEBUG_TTY, "port close SysPort %d\n",PortP->PortNum);
539
540         /* PortP = p->RIOPortp[SysPort]; */
541         rio_dprintk (RIO_DEBUG_TTY, "Port is at address 0x%x\n",(int)PortP);
542         /* tp = PortP->TtyP;*/                  /* Get tty */
543         tty = PortP->gs.tty;
544         rio_dprintk (RIO_DEBUG_TTY, "TTY is at address 0x%x\n",(int)tty);
545
546         if (PortP->gs.closing_wait) 
547                 end_time = jiffies + PortP->gs.closing_wait;
548         else 
549                 end_time = jiffies + MAX_SCHEDULE_TIMEOUT;
550
551         Modem = rio_ismodem(tty);
552 #if 0
553         /* What F.CKING cache? Even then, a higly idle multiprocessor,
554            system with large caches this won't work . Better find out when 
555            this doesn't work asap, and fix the cause.  -- REW */
556         
557         RIODelay(PortP, HUNDRED_MS*10); /* To flush the cache */
558 #endif
559         rio_spin_lock_irqsave(&PortP->portSem, flags);
560
561         /*
562         ** Setting this flag will make any process trying to open
563         ** this port block until we are complete closing it.
564         */
565         PortP->State |= RIO_CLOSING;
566
567         if ( (PortP->State & RIO_DELETED) ) {
568                 rio_dprintk (RIO_DEBUG_TTY, "Close on deleted RTA\n");
569                 deleted = 1;
570         }
571         
572         if ( p->RIOHalted ) {
573                 RIOClearUp( PortP );
574                 rv = -EIO;
575                 goto close_end;
576         }
577
578         rio_dprintk (RIO_DEBUG_TTY, "Clear bits\n");
579         /*
580         ** clear the open bits for this device
581         */
582         PortP->State &= (Modem ? ~RIO_MOPEN : ~RIO_LOPEN);
583         PortP->State &= ~RIO_CARR_ON;
584         PortP->ModemState &= ~MSVR1_CD;
585         /*
586         ** If the device was open as both a Modem and a tty line
587         ** then we need to wimp out here, as the port has not really
588         ** been finally closed (gee, whizz!) The test here uses the
589         ** bit for the OTHER mode of operation, to see if THAT is
590         ** still active!
591         */
592         if ( (PortP->State & (RIO_LOPEN|RIO_MOPEN)) ) {
593                 /*
594                 ** The port is still open for the other task -
595                 ** return, pretending that we are still active.
596                 */
597                 rio_dprintk (RIO_DEBUG_TTY, "Channel %d still open !\n",PortP->PortNum);
598                 PortP->State &= ~RIO_CLOSING;
599                 if (PortP->firstOpen)
600                         PortP->firstOpen--;
601                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
602                 return -EIO;
603         }
604
605         rio_dprintk (RIO_DEBUG_TTY, "Closing down - everything must go!\n");
606
607         PortP->State &= ~RIO_DYNOROD;
608
609         /*
610         ** This is where we wait for the port
611         ** to drain down before closing. Bye-bye....
612         ** (We never meant to do this)
613         */
614         rio_dprintk (RIO_DEBUG_TTY, "Timeout 1 starts\n");
615
616         if (!deleted)
617         while ( (PortP->InUse != NOT_INUSE) && !p->RIOHalted && 
618                 (PortP->TxBufferIn != PortP->TxBufferOut) ) {
619                 cprintf("Need to flush the ttyport\n");
620                 if (repeat_this -- <= 0) {
621                         rv = -EINTR;
622                         rio_dprintk (RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
623                         RIOPreemptiveCmd(p, PortP, FCLOSE ); 
624                         goto close_end;
625                 }
626                 rio_dprintk (RIO_DEBUG_TTY, "Calling timeout to flush in closing\n");
627                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
628                 if (RIODelay_ni(PortP, HUNDRED_MS*10) == RIO_FAIL) {
629                         rio_dprintk (RIO_DEBUG_TTY, "RTA EINTR in delay \n");
630                         rv = -EINTR;
631                         rio_spin_lock_irqsave(&PortP->portSem, flags);
632                         goto close_end;
633                 }
634                 rio_spin_lock_irqsave(&PortP->portSem, flags);
635         }
636
637         PortP->TxBufferIn = PortP->TxBufferOut = 0;
638         repeat_this = 0xff;
639
640         PortP->InUse = 0;
641         if ( (PortP->State & (RIO_LOPEN|RIO_MOPEN)) ) {
642                 /*
643                 ** The port has been re-opened for the other task -
644                 ** return, pretending that we are still active.
645                 */
646                 rio_dprintk (RIO_DEBUG_TTY, "Channel %d re-open!\n", PortP->PortNum);
647                 PortP->State &= ~RIO_CLOSING;
648                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
649                 if (PortP->firstOpen)
650                         PortP->firstOpen--;
651                 return -EIO;
652         }
653
654         if ( p->RIOHalted ) {
655                 RIOClearUp( PortP );
656                 goto close_end;
657         }
658
659         
660
661         /* Can't call RIOShortCommand with the port locked. */
662         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
663
664         if (RIOShortCommand(p, PortP, CLOSE, 1, 0) == RIO_FAIL) {
665           RIOPreemptiveCmd(p, PortP,FCLOSE);
666           goto close_end;
667         }
668
669         if (!deleted)
670           while (try && (PortP->PortState & PORT_ISOPEN)) {
671                 try--;
672                 if (time_after (jiffies, end_time)) {
673                   rio_dprintk (RIO_DEBUG_TTY, "Run out of tries - force the bugger shut!\n" );
674                   RIOPreemptiveCmd(p, PortP,FCLOSE);
675                   break;
676                 }
677                 rio_dprintk (RIO_DEBUG_TTY, "Close: PortState:ISOPEN is %d\n", 
678                                            PortP->PortState & PORT_ISOPEN);
679
680                 if ( p->RIOHalted ) {
681                         RIOClearUp( PortP );
682                         goto close_end;
683                 }
684                 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
685                         rio_dprintk (RIO_DEBUG_TTY, "RTA EINTR in delay \n");
686                         RIOPreemptiveCmd(p, PortP,FCLOSE);
687                         break;
688                 }
689         }
690         rio_spin_lock_irqsave(&PortP->portSem, flags);
691         rio_dprintk (RIO_DEBUG_TTY, "Close: try was %d on completion\n", try );
692  
693         /* RIOPreemptiveCmd(p, PortP, FCLOSE); */
694
695 /*
696 ** 15.10.1998 ARG - ESIL 0761 part fix
697 ** RIO has it's own CTSFLOW and RTSFLOW flags in 'Config' in the port structure,** we need to make sure that the flags are clear when the port is opened.
698 */
699         PortP->Config &= ~(RIO_CTSFLOW|RIO_RTSFLOW);
700
701
702 #ifdef STATS
703         PortP->Stat.CloseCnt++;
704 #endif
705         /*
706         ** Count opens for port statistics reporting
707         */
708         if (PortP->statsGather)
709                 PortP->closes++;
710
711 close_end:
712         /* XXX: Why would a "DELETED" flag be reset here? I'd have
713            thought that a "deleted" flag means that the port was
714            permanently gone, but here we can make it reappear by it
715            being in close during the "deletion".
716         */
717         PortP->State &= ~(RIO_CLOSING|RIO_DELETED);
718         if (PortP->firstOpen)
719                 PortP->firstOpen--;
720         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
721         rio_dprintk (RIO_DEBUG_TTY, "Return from close\n");
722         return rv;
723 }
724
725
726 /*
727 ** decide if we need to use the line discipline.
728 ** This routine can return one of three values:
729 ** COOK_RAW if no processing has to be done by the line discipline or the card
730 ** COOK_WELL if the line discipline must be used to do the processing
731 ** COOK_MEDIUM if the card can do all the processing necessary.
732 */
733 #if 0
734 static int
735 RIOCookMode(struct ttystatics *tp)
736 {
737         /*
738         ** We can't handle tm.c_mstate != 0 on SCO
739         ** We can't handle mapping
740         ** We can't handle non-ttwrite line disc.
741         ** We can't handle lflag XCASE
742         ** We can handle oflag OPOST & (OCRNL, ONLCR, TAB3)
743         */
744
745 #ifdef CHECK
746         CheckTtyP( tp );
747 #endif
748         if (!(tp->tm.c_oflag & OPOST))  /* No post processing */
749                 return COOK_RAW;        /* Raw mode o/p */
750
751         if ( tp->tm.c_lflag & XCASE )
752                 return COOK_WELL;       /* Use line disc */
753
754         if (tp->tm.c_oflag & ~(OPOST | ONLCR | OCRNL | TAB3 ) )
755                 return COOK_WELL;       /* Use line disc for strange modes */
756
757         if ( tp->tm.c_oflag == OPOST )  /* If only OPOST is set, do RAW */
758                 return COOK_RAW;
759
760         /*
761         ** So, we need to output process!
762         */
763         return COOK_MEDIUM;
764 }
765 #endif
766
767 static void
768 RIOClearUp(PortP)
769 struct Port *PortP;
770 {
771         rio_dprintk (RIO_DEBUG_TTY, "RIOHalted set\n");
772         PortP->Config = 0;        /* Direct semaphore */
773         PortP->PortState = 0;
774         PortP->firstOpen = 0;
775         PortP->FlushCmdBodge = 0;
776         PortP->ModemState = PortP->CookMode = 0;
777         PortP->Mapped = 0;
778         PortP->WflushFlag = 0;
779         PortP->MagicFlags       = 0;
780         PortP->RxDataStart = 0;
781         PortP->TxBufferIn = 0;
782         PortP->TxBufferOut = 0;
783 }
784
785 /*
786 ** Put a command onto a port.
787 ** The PortPointer, command, length and arg are passed.
788 ** The len is the length *inclusive* of the command byte,
789 ** and so for a command that takes no data, len==1.
790 ** The arg is a single byte, and is only used if len==2.
791 ** Other values of len aren't allowed, and will cause
792 ** a panic.
793 */
794 int RIOShortCommand(struct rio_info *p, struct Port *PortP,
795                 int command, int len, int arg)
796 {
797         PKT *PacketP;
798         int             retries = 20; /* at 10 per second -> 2 seconds */
799         unsigned long flags;
800
801         rio_dprintk (RIO_DEBUG_TTY, "entering shortcommand.\n");
802 #ifdef CHECK
803         CheckPortP( PortP );
804         if ( len < 1 || len > 2 )
805                 cprintf(("STUPID LENGTH %d\n",len));
806 #endif
807
808         if ( PortP->State & RIO_DELETED ) {
809                 rio_dprintk (RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
810                 return RIO_FAIL;
811         }
812         rio_spin_lock_irqsave(&PortP->portSem, flags);
813
814         /*
815         ** If the port is in use for pre-emptive command, then wait for it to 
816         ** be free again.
817         */
818         while ( (PortP->InUse != NOT_INUSE) && !p->RIOHalted ) {
819                 rio_dprintk (RIO_DEBUG_TTY, "Waiting for not in use (%d)\n", 
820                                            retries);
821                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
822                 if (retries-- <= 0) {
823                         return RIO_FAIL;
824                 }
825                 if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
826                         return RIO_FAIL;
827                 }
828                 rio_spin_lock_irqsave(&PortP->portSem, flags);
829         }
830         if ( PortP->State & RIO_DELETED ) {
831                 rio_dprintk (RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
832                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
833                 return RIO_FAIL;
834         }
835
836         while ( !can_add_transmit(&PacketP,PortP) && !p->RIOHalted ) {
837                 rio_dprintk (RIO_DEBUG_TTY, "Waiting to add short command to queue (%d)\n", retries);
838                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
839                 if (retries-- <= 0) {
840                   rio_dprintk (RIO_DEBUG_TTY, "out of tries. Failing\n");
841                         return RIO_FAIL;
842                 }
843                 if ( RIODelay_ni(PortP, HUNDRED_MS)==RIO_FAIL ) {
844                         return RIO_FAIL;
845                 }
846                 rio_spin_lock_irqsave(&PortP->portSem, flags);
847         }
848
849         if ( p->RIOHalted ) {
850                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
851                 return RIO_FAIL;
852         }
853
854         /*
855         ** set the command byte and the argument byte
856         */
857         WBYTE(PacketP->data[0] , command);
858
859         if ( len==2 )
860                 WBYTE(PacketP->data[1] , arg);
861
862         /*
863         ** set the length of the packet and set the command bit.
864         */
865         WBYTE(PacketP->len , PKT_CMD_BIT | len);
866
867         add_transmit(PortP);
868         /*
869         ** Count characters transmitted for port statistics reporting
870         */
871         if (PortP->statsGather)
872                 PortP->txchars += len;
873
874         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
875         return p->RIOHalted ? RIO_FAIL : ~RIO_FAIL;
876 }
877
878
879 #if 0
880 /*
881 ** This is an ioctl interface. This is the twentieth century. You know what
882 ** its all about.
883 */
884 int
885 riotioctl(struct rio_info *p, struct tty_struct *tty, int cmd, caddr_t arg)
886 {
887         register struct         Port *PortP;
888         register struct         ttystatics *tp;
889         int                                     current;
890         int                                     ParamSemIncremented = 0;
891         int                                     old_oflag, old_cflag, old_iflag, changed, oldcook;
892         int                                     i;
893         unsigned char           sio_regs[5];            /* Here be magic */
894         short                           vpix_cflag;
895         short                           divisor;
896         int                                     baud;
897         uint                            SysPort = rio_minor(tty);
898         int                             Modem = rio_ismodem(tty);
899         int                                     ioctl_processed;
900
901         rio_dprintk (RIO_DEBUG_TTY, "port ioctl SysPort %d command 0x%x argument 0x%x %s\n",
902                         SysPort, cmd, arg, Modem?"Modem":"tty") ;
903
904         if ( SysPort >= RIO_PORTS ) {
905                 rio_dprintk (RIO_DEBUG_TTY, "Bad port number %d\n", SysPort);
906                 return -ENXIO;
907         }
908
909         PortP = p->RIOPortp[SysPort];
910         tp = PortP->TtyP;
911
912         rio_spin_lock_irqsave(&PortP->portSem, flags);
913
914 #ifdef STATS
915         PortP->Stat.IoctlCnt++;
916 #endif
917
918         if ( PortP->State & RIO_DELETED ) {
919                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
920                 return -EIO;
921         }
922
923
924         if ( p->RIOHalted ) {
925                 RIOClearUp( PortP );
926                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
927                 return -EIO;
928         }
929
930         /*
931         ** Count ioctls for port statistics reporting
932         */
933         if (PortP->statsGather)
934                 PortP->ioctls++;
935
936         /*
937         ** Specialix RIO Ioctl calls
938         */
939         switch (cmd) {
940
941                 case TCRIOTRIAD:
942                         if ( arg )
943                                 PortP->State |= RIO_TRIAD_MODE;
944                         else
945                                 PortP->State &= ~RIO_TRIAD_MODE;
946                         /*
947                         ** Normally, when istrip is set on a port, a config is
948                         ** sent to the RTA instructing the CD1400 to do the
949                         ** stripping. In TRIAD mode, the interrupt receive routine
950                         ** must do the stripping instead, since it has to detect
951                         ** an 8 bit function key sequence. If istrip is set with
952                         ** TRIAD mode on(off), and 8 bit data is being read by
953                         ** the port, the user then turns TRIAD mode off(on), the RTA
954                         ** must be reconfigured (not) to do the stripping.
955                         ** Hence we call RIOParam here.
956                         */
957                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
958                         RIOParam(PortP,CONFIG,Modem,OK_TO_SLEEP);       
959                         return 0;
960
961                 case TCRIOTSTATE:
962                         rio_dprintk (RIO_DEBUG_TTY, "tbusy/tstop monitoring %sabled\n",
963                                 arg ? "en" : "dis");
964                         /* MonitorTstate = 0 ;*/
965                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
966                         RIOParam(PortP, CONFIG, Modem, OK_TO_SLEEP);
967                         return 0;
968
969                 case TCRIOSTATE: /* current state of Modem input pins */
970                         rio_dprintk (RIO_DEBUG_TTY, "TCRIOSTATE\n");
971                         if (RIOPreemptiveCmd(p, PortP, MGET) == RIO_FAIL)
972                                 rio_dprintk (RIO_DEBUG_TTY, "TCRIOSTATE command failed\n");
973                         PortP->State |= RIO_BUSY;
974                         current = PortP->ModemState;
975                         if ( copyout((caddr_t)&current, (int)arg,
976                                                         sizeof(current))==COPYFAIL ) {
977                                 rio_dprintk (RIO_DEBUG_TTY, "Copyout failed\n");
978                                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
979                                 pseterr(EFAULT);
980                         }
981                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
982                         return 0;
983
984                 case TCRIOMBIS:         /* Set modem lines */
985                 case TCRIOMBIC:         /* Clear modem lines */
986                         rio_dprintk (RIO_DEBUG_TTY, "TCRIOMBIS/TCRIOMBIC\n");
987                         if (cmd == TCRIOMBIS) {
988                                 uint            state;
989                                 state = (uint)arg;
990                                 PortP->ModemState |= (ushort)state;
991                                 PortP->ModemLines = (ulong) arg;
992                                 if (RIOPreemptiveCmd(p, PortP, MBIS) == RIO_FAIL)
993                                         rio_dprintk (RIO_DEBUG_TTY, 
994                                          "TCRIOMBIS command failed\n");
995                         }
996                         else {
997                                 uint            state;
998
999                                 state = (uint)arg;
1000                                 PortP->ModemState &= ~(ushort)state;
1001                                 PortP->ModemLines = (ulong) arg;
1002                                 if (RIOPreemptiveCmd(p, PortP, MBIC) == RIO_FAIL)
1003                                         rio_dprintk (RIO_DEBUG_TTY, "TCRIOMBIC command failed\n");
1004                         }
1005                         PortP->State |= RIO_BUSY;
1006                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1007                         return 0;
1008
1009                 case TCRIOXPON: /* set Xprint ON string */
1010                         rio_dprintk (RIO_DEBUG_TTY, "TCRIOXPON\n");
1011                         if ( copyin((int)arg, (caddr_t)PortP->Xprint.XpOn,
1012                                                 MAX_XP_CTRL_LEN)==COPYFAIL ) {
1013                                 rio_dprintk (RIO_DEBUG_TTY, "Copyin failed\n");
1014                                 PortP->Xprint.XpOn[0] = '\0';
1015                                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1016                                 pseterr(EFAULT);
1017                         }
1018                         PortP->Xprint.XpOn[MAX_XP_CTRL_LEN-1] = '\0';
1019                         PortP->Xprint.XpLen = strlen(PortP->Xprint.XpOn)+
1020                                                                                                 strlen(PortP->Xprint.XpOff);
1021                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1022                         return 0;
1023
1024                 case TCRIOXPOFF: /* set Xprint OFF string */
1025                         rio_dprintk (RIO_DEBUG_TTY, "TCRIOXPOFF\n");
1026                         if ( copyin( (int)arg, (caddr_t)PortP->Xprint.XpOff,
1027                                                 MAX_XP_CTRL_LEN)==COPYFAIL ) {
1028                                 rio_dprintk (RIO_DEBUG_TTY, "Copyin failed\n");
1029                                 PortP->Xprint.XpOff[0] = '\0';
1030                                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1031                                 pseterr(EFAULT);
1032                         }
1033                         PortP->Xprint.XpOff[MAX_XP_CTRL_LEN-1] = '\0';
1034                         PortP->Xprint.XpLen = strlen(PortP->Xprint.XpOn)+
1035                                                                                 strlen(PortP->Xprint.XpOff);
1036                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1037                         return 0;
1038
1039                 case TCRIOXPCPS: /* set Xprint CPS string */
1040                         rio_dprintk (RIO_DEBUG_TTY, "TCRIOXPCPS\n");
1041                         if ( (uint)arg > p->RIOConf.MaxXpCps || 
1042                                         (uint)arg < p->RIOConf.MinXpCps ) {
1043                                 rio_dprintk (RIO_DEBUG_TTY, "%d CPS out of range\n",arg);
1044                                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1045                                 pseterr(EINVAL);
1046                                 return 0;
1047                         }
1048                         PortP->Xprint.XpCps = (uint)arg;
1049                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1050                         return 0;
1051
1052                 case TCRIOXPRINT:
1053                         rio_dprintk (RIO_DEBUG_TTY, "TCRIOXPRINT\n");
1054                         if ( copyout((caddr_t)&PortP->Xprint, (int)arg,
1055                                         sizeof(struct Xprint))==COPYFAIL ) {
1056                                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1057                                 pseterr(EFAULT);
1058                         }
1059                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1060                         return 0;
1061
1062                 case TCRIOIXANYON:
1063                         rio_dprintk (RIO_DEBUG_TTY, "TCRIOIXANYON\n");
1064                         PortP->Config |= RIO_IXANY;
1065                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1066                         return 0;
1067
1068                 case TCRIOIXANYOFF:
1069                         rio_dprintk (RIO_DEBUG_TTY, "TCRIOIXANYOFF\n");
1070                         PortP->Config &= ~RIO_IXANY;
1071                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1072                         return 0;
1073
1074                 case TCRIOIXONON:
1075                         rio_dprintk (RIO_DEBUG_TTY, "TCRIOIXONON\n");
1076                         PortP->Config |= RIO_IXON;
1077                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1078                         return 0;
1079
1080                 case TCRIOIXONOFF:
1081                         rio_dprintk (RIO_DEBUG_TTY, "TCRIOIXONOFF\n");
1082                         PortP->Config &= ~RIO_IXON;
1083                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1084                         return 0;
1085
1086 /*
1087 ** 15.10.1998 ARG - ESIL 0761 part fix
1088 ** Added support for CTS and RTS flow control ioctls :
1089 */
1090                 case TCRIOCTSFLOWEN:
1091                         rio_dprintk (RIO_DEBUG_TTY, "TCRIOCTSFLOWEN\n");
1092                         PortP->Config |= RIO_CTSFLOW;
1093                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1094                         RIOParam(PortP,CONFIG,Modem,OK_TO_SLEEP);       
1095                         return 0;
1096
1097                 case TCRIOCTSFLOWDIS:
1098                         rio_dprintk (RIO_DEBUG_TTY, "TCRIOCTSFLOWDIS\n");
1099                         PortP->Config &= ~RIO_CTSFLOW;
1100                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1101                         RIOParam(PortP,CONFIG,Modem,OK_TO_SLEEP);       
1102                         return 0;
1103
1104                 case TCRIORTSFLOWEN:
1105                         rio_dprintk (RIO_DEBUG_TTY, "TCRIORTSFLOWEN\n");
1106                         PortP->Config |= RIO_RTSFLOW;
1107                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1108                         RIOParam(PortP,CONFIG,Modem,OK_TO_SLEEP);       
1109                         return 0;
1110
1111                 case TCRIORTSFLOWDIS:
1112                         rio_dprintk (RIO_DEBUG_TTY, "TCRIORTSFLOWDIS\n");
1113                         PortP->Config &= ~RIO_RTSFLOW;
1114                         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1115                         RIOParam(PortP,CONFIG,Modem,OK_TO_SLEEP);       
1116                         return 0;
1117
1118 /* end ESIL 0761 part fix */
1119
1120         }
1121
1122
1123         /* Lynx IOCTLS */
1124         switch (cmd) {
1125                 case TIOCSETP:
1126                 case TIOCSETN:
1127                 case OTIOCSETP:
1128                 case OTIOCSETN:
1129                         ioctl_processed++;
1130                         ttyseth(PortP, tp, (struct old_sgttyb *)arg);
1131                         break;
1132                 case TCSETA:
1133                 case TCSETAW:
1134                 case TCSETAF:
1135                         ioctl_processed++;
1136                         rio_dprintk (RIO_DEBUG_TTY, "NON POSIX ioctl\n");
1137                         ttyseth_pv(PortP, tp, (struct termios *)arg, 0);
1138                         break;
1139                 case TCSETAP:   /* posix tcsetattr() */
1140                 case TCSETAWP:  /* posix tcsetattr() */
1141                 case TCSETAFP:  /* posix tcsetattr() */
1142                         rio_dprintk (RIO_DEBUG_TTY, "NON POSIX SYSV ioctl\n");
1143                         ttyseth_pv(PortP, tp, (struct termios *)arg, 1);
1144                         ioctl_processed++;
1145                         break;
1146         }
1147
1148         /*
1149         ** If its any of the commands that require the port to be in the
1150         ** non-busy state wait until all output has drained 
1151         */
1152         if (!ioctl_processed)
1153         switch(cmd) {
1154                 case TCSETAW:
1155                 case TCSETAF:
1156                 case TCSETA:
1157                 case TCSBRK:
1158 #define OLD_POSIX ('x' << 8)
1159 #define OLD_POSIX_SETA (OLD_POSIX | 2)
1160 #define OLD_POSIX_SETAW (OLD_POSIX | 3)
1161 #define OLD_POSIX_SETAF (OLD_POSIX | 4)
1162 #define NEW_POSIX (('i' << 24) | ('X' << 16))
1163 #define NEW_POSIX_SETA (NEW_POSIX | 2)
1164 #define NEW_POSIX_SETAW (NEW_POSIX | 3)
1165 #define NEW_POSIX_SETAF (NEW_POSIX | 4)
1166                 case OLD_POSIX_SETA:
1167                 case OLD_POSIX_SETAW:
1168                 case OLD_POSIX_SETAF:
1169                 case NEW_POSIX_SETA:
1170                 case NEW_POSIX_SETAW:
1171                 case NEW_POSIX_SETAF:
1172 #ifdef TIOCSETP
1173                 case TIOCSETP:
1174 #endif
1175                 case TIOCSETD:
1176                 case TIOCSETN:
1177                         rio_dprintk (RIO_DEBUG_TTY, "wait for non-BUSY, semaphore set\n");
1178                         /*
1179                         ** Wait for drain here, at least as far as the double buffer
1180                         ** being empty.
1181                         */
1182                         /* XXX Does the above comment mean that this has
1183                            still to be implemented? -- REW */
1184                         /* XXX Is the locking OK together with locking
1185                            in txenable? (Deadlock?) -- REW */
1186                         
1187                         RIOTxEnable((char *)PortP);
1188                         break;
1189                 default:
1190                         break;
1191         }
1192
1193         old_cflag = tp->tm.c_cflag;
1194         old_iflag = tp->tm.c_iflag;
1195         old_oflag = tp->tm.c_oflag;
1196         oldcook = PortP->CookMode;
1197
1198         if ( p->RIOHalted ) {
1199                 RIOClearUp( PortP );
1200                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1201                 pseterr(EIO);
1202                 return 0;
1203         }
1204
1205         PortP->FlushCmdBodge = 0;
1206
1207         /*
1208         ** If the port is locked, and it is reconfigured, we want
1209         ** to restore the state of the tty structure so the change is NOT
1210         ** made.
1211         */
1212         if (PortP->Lock) {
1213                 tp->tm.c_iflag = PortP->StoredTty.iflag;
1214                 tp->tm.c_oflag = PortP->StoredTty.oflag;
1215                 tp->tm.c_cflag = PortP->StoredTty.cflag;
1216                 tp->tm.c_lflag = PortP->StoredTty.lflag;
1217                 tp->tm.c_line = PortP->StoredTty.line;
1218                 for (i = 0; i < NCC + 1; i++)
1219                         tp->tm.c_cc[i] = PortP->StoredTty.cc[i];
1220         }
1221         else {
1222                 /*
1223                 ** If the port is set to store the parameters, and it is
1224                 ** reconfigured, we want to save the current tty struct so it
1225                 ** may be restored on the next open.
1226                 */
1227                 if (PortP->Store) {
1228                         PortP->StoredTty.iflag = tp->tm.c_iflag;
1229                         PortP->StoredTty.oflag = tp->tm.c_oflag;
1230                         PortP->StoredTty.cflag = tp->tm.c_cflag;
1231                         PortP->StoredTty.lflag = tp->tm.c_lflag;
1232                         PortP->StoredTty.line = tp->tm.c_line;
1233                         for (i = 0; i < NCC + 1; i++)
1234                                 PortP->StoredTty.cc[i] = tp->tm.c_cc[i];
1235                 }
1236         }
1237
1238         changed = (tp->tm.c_cflag != old_cflag) ||
1239                                 (tp->tm.c_iflag != old_iflag) ||
1240                                 (tp->tm.c_oflag != old_oflag);
1241
1242         PortP->CookMode = RIOCookMode(tp);      /* Set new cooking mode */
1243
1244         rio_dprintk (RIO_DEBUG_TTY, "RIOIoctl changed %d newcook %d oldcook %d\n",
1245                         changed,PortP->CookMode,oldcook);
1246
1247 #ifdef MODEM_SUPPORT
1248         /*
1249         ** kludge to force CARR_ON if CLOCAL set
1250         */
1251         if ((tp->tm.c_cflag & CLOCAL) || (PortP->ModemState & MSVR1_CD))        {
1252                 tp->tm.c_state |= CARR_ON;
1253                 wakeup ((caddr_t)&tp->tm.c_canq);
1254         }
1255 #endif
1256
1257         if ( p->RIOHalted ) {
1258                 RIOClearUp( PortP );
1259                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1260                 pseterr(EIO);
1261                 return 0;
1262         }
1263         /*
1264         ** Re-configure if modes or cooking have changed
1265         */
1266         if (changed || oldcook != PortP->CookMode || (ioctl_processed)) {
1267                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1268                 rio_dprintk (RIO_DEBUG_TTY, "Ioctl changing the PORT settings\n");
1269                 RIOParam(PortP,CONFIG,Modem,OK_TO_SLEEP);       
1270                 rio_spin_lock_irqsave(&PortP->portSem, flags);
1271         }
1272
1273         if (p->RIOHalted) {
1274                 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1275                 RIOClearUp( PortP );
1276                 pseterr(EIO);
1277                 return 0;
1278         }
1279         rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1280         return 0;
1281 }
1282
1283 /*
1284         ttyseth -- set hardware dependent tty settings
1285 */
1286 void
1287 ttyseth(PortP, s, sg)
1288 struct Port *           PortP;
1289 struct ttystatics *             s;
1290 struct old_sgttyb *sg;
1291 {
1292         struct old_sgttyb *     tsg;
1293         struct termios *tp = &s->tm;
1294
1295         tsg = &s->sg;
1296
1297         if (sg->sg_flags & (EVENP|ODDP))  {
1298                 tp->c_cflag &= PARENB;
1299                 if (sg->sg_flags & EVENP) {
1300                         if (sg->sg_flags & ODDP) {
1301                                 tp->c_cflag &= V_CS7;
1302                                 tp->c_cflag &= ~PARENB;
1303                         }
1304                         else {
1305                                 tp->c_cflag &= V_CS7;
1306                                 tp->c_cflag &= PARENB;
1307                                 tp->c_cflag &= PARODD;
1308                         }
1309                 }
1310                 else if (sg->sg_flags & ODDP) {
1311                         tp->c_cflag &= V_CS7;
1312                         tp->c_cflag &= PARENB;
1313                         tp->c_cflag &= PARODD;
1314                 }
1315                 else {
1316                         tp->c_cflag &= V_CS7;
1317                         tp->c_cflag &= PARENB;
1318                 }
1319         }
1320 /*
1321  * Use ispeed as the desired speed.  Most implementations don't handle 
1322  * separate input and output speeds very well. If the RIO handles this, 
1323  * I will have to use separate sets of flags to store them in the 
1324  * Port structure.
1325  */
1326         if ( !sg->sg_ospeed )
1327                 sg->sg_ospeed = sg->sg_ispeed;
1328         else
1329                 sg->sg_ispeed = sg->sg_ospeed;
1330         if (sg->sg_ispeed > V_EXTB ) 
1331                 sg->sg_ispeed = V_EXTB;
1332         if (sg->sg_ispeed < V_B0)
1333                 sg->sg_ispeed = V_B0;
1334         *tsg = *sg;
1335    tp->c_cflag = (tp->c_cflag & ~V_CBAUD) | conv_bv[(int)sg->sg_ispeed];
1336 }
1337
1338 /*
1339         ttyseth_pv -- set hardware dependent tty settings using either the
1340                         POSIX termios structure or the System V termio structure.
1341                                 sysv = 0 => (POSIX):     struct termios *sg
1342                                 sysv != 0 => (System V): struct termio *sg
1343 */
1344 static void
1345 ttyseth_pv(PortP, s, sg, sysv)
1346 struct Port *PortP;
1347 struct ttystatics *s;
1348 struct termios *sg;
1349 int sysv;
1350 {
1351     int speed;
1352     unsigned char csize;
1353     unsigned char cread;
1354     unsigned int lcr_flags;
1355     int ps;
1356  
1357     if (sysv) {
1358         /* sg points to a System V termio structure */
1359         csize = ((struct termio *)sg)->c_cflag & CSIZE;
1360         cread = ((struct termio *)sg)->c_cflag & CREAD;
1361         speed = conv_vb[((struct termio *)sg)->c_cflag & V_CBAUD];
1362     }
1363     else {
1364         /* sg points to a POSIX termios structure */
1365         csize = sg->c_cflag & CSIZE;
1366         cread = sg->c_cflag & CREAD;
1367         speed = conv_vb[sg->c_cflag & V_CBAUD];
1368     }
1369     if (s->sg.sg_ispeed != speed || s->sg.sg_ospeed != speed) {
1370         s->sg.sg_ispeed = speed;
1371         s->sg.sg_ospeed = speed;
1372         s->tm.c_cflag = (s->tm.c_cflag & ~V_CBAUD) |
1373                          conv_bv[(int)s->sg.sg_ispeed];
1374     }
1375 }
1376 #endif