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