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