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		: rioctrl.c
24 **	SID		: 1.3
25 **	Last Modified	: 11/6/98 10:33:42
26 **	Retrieved	: 11/6/98 10:33:49
27 **
28 **  ident @(#)rioctrl.c	1.3
29 **
30 ** -----------------------------------------------------------------------------
31 */
32 #ifdef SCCS_LABELS
33 static char *_rioctrl_c_sccs_ = "@(#)rioctrl.c	1.3";
34 #endif
35 
36 
37 #define __NO_VERSION__
38 #include <linux/module.h>
39 #include <linux/slab.h>
40 #include <linux/errno.h>
41 #include <asm/io.h>
42 #include <asm/system.h>
43 #include <asm/string.h>
44 #include <asm/semaphore.h>
45 #include <asm/uaccess.h>
46 
47 #include <linux/termios.h>
48 #include <linux/serial.h>
49 
50 #include <linux/compatmac.h>
51 #include <linux/generic_serial.h>
52 
53 
54 #include "linux_compat.h"
55 #include "rio_linux.h"
56 #include "typdef.h"
57 #include "pkt.h"
58 #include "daemon.h"
59 #include "rio.h"
60 #include "riospace.h"
61 #include "top.h"
62 #include "cmdpkt.h"
63 #include "map.h"
64 #include "riotypes.h"
65 #include "rup.h"
66 #include "port.h"
67 #include "riodrvr.h"
68 #include "rioinfo.h"
69 #include "func.h"
70 #include "errors.h"
71 #include "pci.h"
72 
73 #include "parmmap.h"
74 #include "unixrup.h"
75 #include "board.h"
76 #include "host.h"
77 #include "error.h"
78 #include "phb.h"
79 #include "link.h"
80 #include "cmdblk.h"
81 #include "route.h"
82 #include "control.h"
83 #include "cirrus.h"
84 #include "rioioctl.h"
85 
86 
87 static struct LpbReq	 LpbReq;
88 static struct RupReq	 RupReq;
89 static struct PortReq	PortReq;
90 static struct HostReq	HostReq;
91 static struct HostDpRam HostDpRam;
92 static struct DebugCtrl DebugCtrl;
93 static struct Map		 MapEnt;
94 static struct PortSetup PortSetup;
95 static struct DownLoad	DownLoad;
96 static struct SendPack  SendPack;
97 /* static struct StreamInfo	StreamInfo; */
98 /* static char modemtable[RIO_PORTS]; */
99 static struct SpecialRupCmd SpecialRupCmd;
100 static struct PortParams PortParams;
101 static struct portStats portStats;
102 
103 static struct SubCmdStruct {
104 	ushort	Host;
105 	ushort	Rup;
106 	ushort	Port;
107 	ushort	Addr;
108 } SubCmd;
109 
110 struct PortTty {
111 	uint		port;
112 	struct ttystatics	Tty;
113 };
114 
115 static struct PortTty	PortTty;
116 typedef struct ttystatics TERMIO;
117 
118 /*
119 ** This table is used when the config.rio downloads bin code to the
120 ** driver. We index the table using the product code, 0-F, and call
121 ** the function pointed to by the entry, passing the information
122 ** about the boot.
123 ** The RIOBootCodeUNKNOWN entry is there to politely tell the calling
124 ** process to bog off.
125 */
126 static int
127 (*RIOBootTable[MAX_PRODUCT])(struct rio_info *, struct DownLoad *) =
128 {
129 /* 0 */	RIOBootCodeHOST,	/* Host Card */
130 /* 1 */	RIOBootCodeRTA,		/* RTA */
131 };
132 
133 #define drv_makedev(maj, min) ((((uint) maj & 0xff) << 8) | ((uint) min & 0xff))
134 
135 #ifdef linux
copyin(int arg,caddr_t dp,int siz)136 int copyin (int arg, caddr_t dp, int siz)
137 {
138   int rv;
139 
140   rio_dprintk (RIO_DEBUG_CTRL, "Copying %d bytes from user %p to %p.\n", siz, (void *)arg, dp);
141   rv = copy_from_user (dp, (void *)arg, siz);
142   if (rv < 0) return COPYFAIL;
143   else return rv;
144 }
145 
146 
copyout(caddr_t dp,int arg,int siz)147 int copyout (caddr_t dp, int arg, int siz)
148 {
149   int rv;
150 
151   rio_dprintk (RIO_DEBUG_CTRL, "Copying %d bytes to user %p from %p.\n", siz, (void *)arg, dp);
152   rv = copy_to_user ((void *)arg, dp, siz);
153   if (rv < 0) return COPYFAIL;
154   else return rv;
155 }
156 
157 #else
158 
159 int
copyin(arg,dp,siz)160 copyin(arg, dp, siz)
161 int arg;
162 caddr_t dp;
163 int siz;
164 {
165 	if (rbounds ((unsigned long) arg) >= siz) {
166 		bcopy ( arg, dp, siz );
167 		return OK;
168 	} else
169 		return ( COPYFAIL );
170 }
171 
172 int
copyout(dp,arg,siz)173 copyout (dp, arg, siz)
174 caddr_t dp;
175 int arg;
176 int siz;
177 {
178 	if (wbounds ((unsigned long) arg) >=  siz ) {
179 		bcopy ( dp, arg, siz );
180 		return OK;
181 	} else
182 		return ( COPYFAIL );
183 }
184 #endif
185 
186 int
riocontrol(p,dev,cmd,arg,su)187 riocontrol(p, dev, cmd, arg, su)
188 struct rio_info	* p;
189 dev_t		dev;
190 int		cmd;
191 caddr_t		arg;
192 int		su;
193 {
194 	uint	Host;	/* leave me unsigned! */
195 	uint	port;	/* and me! */
196 	struct Host	*HostP;
197 	ushort	loop;
198 	int		Entry;
199 	struct Port	*PortP;
200 	PKT	*PacketP;
201 	int		retval = 0;
202 	unsigned long flags;
203 
204 	func_enter ();
205 
206 	/* Confuse teh compiler to think that we've initialized these */
207 	Host=0;
208 	PortP = NULL;
209 
210 	rio_dprintk (RIO_DEBUG_CTRL, "control ioctl cmd: 0x%x arg: 0x%x\n", cmd, (int)arg);
211 
212 	switch (cmd) {
213 		/*
214 		** RIO_SET_TIMER
215 		**
216 		** Change the value of the host card interrupt timer.
217 		** If the host card number is -1 then all host cards are changed
218 		** otherwise just the specified host card will be changed.
219 		*/
220 		case RIO_SET_TIMER:
221 			rio_dprintk (RIO_DEBUG_CTRL, "RIO_SET_TIMER to %dms\n", (uint)arg);
222 			{
223 				int host, value;
224 				host = (uint)arg >> 16;
225 				value = (uint)arg & 0x0000ffff;
226 				if (host == -1) {
227 					for (host = 0; host < p->RIONumHosts; host++) {
228 						if (p->RIOHosts[host].Flags == RC_RUNNING) {
229 							WWORD(p->RIOHosts[host].ParmMapP->timer , value);
230 						}
231 					}
232 				} else if (host >= p->RIONumHosts) {
233 					return EINVAL;
234 				} else {
235 					if ( p->RIOHosts[host].Flags == RC_RUNNING ) {
236 						WWORD(p->RIOHosts[host].ParmMapP->timer , value);
237 					}
238 				}
239 			}
240 			return 0;
241 
242 		case RIO_IDENTIFY_DRIVER:
243 			/*
244 			** 15.10.1998 ARG - ESIL 0760 part fix
245 			** Added driver ident string output.
246 			**
247 #ifndef __THIS_RELEASE__
248 #warning Driver Version string not defined !
249 #endif
250 			cprintf("%s %s %s %s\n",
251 				RIO_DRV_STR,
252 				__THIS_RELEASE__,
253 				__DATE__, __TIME__ );
254 
255 			return 0;
256 
257 		case RIO_DISPLAY_HOST_CFG:
258 			**
259 			** 15.10.1998 ARG - ESIL 0760 part fix
260 			** Added driver host card ident string output.
261 			**
262 			** Note that the only types currently supported
263 			** are ISA and PCI. Also this driver does not
264 			** (yet) distinguish between the Old PCI card
265 			** and the Jet PCI card. In fact I think this
266 			** driver only supports JET PCI !
267 			**
268 
269 			for (Host = 0; Host < p->RIONumHosts; Host++)
270 			{
271 				HostP = &(p->RIOHosts[Host]);
272 
273 				switch ( HostP->Type )
274 				{
275 				    case RIO_AT :
276 					strcpy( host_type, RIO_AT_HOST_STR );
277 					break;
278 
279 				    case RIO_PCI :
280 					strcpy( host_type, RIO_PCI_HOST_STR );
281 					break;
282 
283 				    default :
284 					strcpy( host_type, "Unknown" );
285 					break;
286 				}
287 
288 				cprintf(
289 				  "RIO Host %d - Type:%s Addr:%X IRQ:%d\n",
290 					Host, host_type,
291 					(uint)HostP->PaddrP,
292 					(int)HostP->Ivec - 32  );
293 			}
294 			return 0;
295 			**
296 			*/
297 
298 		case RIO_FOAD_RTA:
299 			rio_dprintk (RIO_DEBUG_CTRL, "RIO_FOAD_RTA\n");
300 			return RIOCommandRta(p, (uint)arg, RIOFoadRta);
301 
302 		case RIO_ZOMBIE_RTA:
303 			rio_dprintk (RIO_DEBUG_CTRL, "RIO_ZOMBIE_RTA\n");
304 			return RIOCommandRta(p, (uint)arg, RIOZombieRta);
305 
306 		case RIO_IDENTIFY_RTA:
307 			rio_dprintk (RIO_DEBUG_CTRL, "RIO_IDENTIFY_RTA\n");
308 			return RIOIdentifyRta(p, arg);
309 
310 		case RIO_KILL_NEIGHBOUR:
311 			rio_dprintk (RIO_DEBUG_CTRL, "RIO_KILL_NEIGHBOUR\n");
312 			return RIOKillNeighbour(p, arg);
313 
314 		case SPECIAL_RUP_CMD:
315 			{
316 				struct CmdBlk *CmdBlkP;
317 
318 				rio_dprintk (RIO_DEBUG_CTRL, "SPECIAL_RUP_CMD\n");
319 				if (copyin((int)arg, (caddr_t)&SpecialRupCmd,
320 							sizeof(SpecialRupCmd)) == COPYFAIL ) {
321 					rio_dprintk (RIO_DEBUG_CTRL, "SPECIAL_RUP_CMD copy failed\n");
322 					p->RIOError.Error = COPYIN_FAILED;
323 		 			return EFAULT;
324 				}
325 				CmdBlkP = RIOGetCmdBlk();
326 				if ( !CmdBlkP ) {
327 					rio_dprintk (RIO_DEBUG_CTRL, "SPECIAL_RUP_CMD GetCmdBlk failed\n");
328 					return ENXIO;
329 				}
330 				CmdBlkP->Packet = SpecialRupCmd.Packet;
331 				if ( SpecialRupCmd.Host >= p->RIONumHosts )
332 					SpecialRupCmd.Host = 0;
333 					rio_dprintk (RIO_DEBUG_CTRL, "Queue special rup command for host %d rup %d\n",
334 						SpecialRupCmd.Host, SpecialRupCmd.RupNum);
335 					if (RIOQueueCmdBlk(&p->RIOHosts[SpecialRupCmd.Host],
336 							SpecialRupCmd.RupNum, CmdBlkP) == RIO_FAIL) {
337 						cprintf("FAILED TO QUEUE SPECIAL RUP COMMAND\n");
338 					}
339 					return 0;
340 				}
341 
342 			case RIO_DEBUG_MEM:
343 #ifdef DEBUG_MEM_SUPPORT
344 RIO_DEBUG_CTRL, 				if (su)
345 					return rio_RIODebugMemory(RIO_DEBUG_CTRL, arg);
346 				else
347 #endif
348 					return EPERM;
349 
350 			case RIO_ALL_MODEM:
351 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_ALL_MODEM\n");
352 				p->RIOError.Error = IOCTL_COMMAND_UNKNOWN;
353 				return EINVAL;
354 
355 			case RIO_GET_TABLE:
356 				/*
357 				** Read the routing table from the device driver to user space
358 				*/
359 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_GET_TABLE\n");
360 
361 				if ((retval = RIOApel(p)) != 0)
362 		 			return retval;
363 
364 				if (copyout((caddr_t)p->RIOConnectTable, (int)arg,
365 						TOTAL_MAP_ENTRIES*sizeof(struct Map)) == COPYFAIL) {
366 		 			rio_dprintk (RIO_DEBUG_CTRL, "RIO_GET_TABLE copy failed\n");
367 		 			p->RIOError.Error = COPYOUT_FAILED;
368 		 			return EFAULT;
369 				}
370 
371 				{
372 					int entry;
373 					rio_dprintk (RIO_DEBUG_CTRL,  "*****\nMAP ENTRIES\n");
374 					for ( entry=0; entry<TOTAL_MAP_ENTRIES; entry++ )
375 					{
376 					  if ((p->RIOConnectTable[entry].ID == 0) &&
377 					      (p->RIOConnectTable[entry].HostUniqueNum == 0) &&
378 					      (p->RIOConnectTable[entry].RtaUniqueNum == 0)) continue;
379 
380 						rio_dprintk (RIO_DEBUG_CTRL, "Map entry %d.HostUniqueNum = 0x%x\n", entry, p->RIOConnectTable[entry].HostUniqueNum );
381 						rio_dprintk (RIO_DEBUG_CTRL, "Map entry %d.RtaUniqueNum = 0x%x\n", entry, p->RIOConnectTable[entry].RtaUniqueNum );
382 						rio_dprintk (RIO_DEBUG_CTRL, "Map entry %d.ID = 0x%x\n", entry, p->RIOConnectTable[entry].ID );
383 						rio_dprintk (RIO_DEBUG_CTRL, "Map entry %d.ID2 = 0x%x\n", entry, p->RIOConnectTable[entry].ID2 );
384 						rio_dprintk (RIO_DEBUG_CTRL, "Map entry %d.Flags = 0x%x\n", entry, (int)p->RIOConnectTable[entry].Flags );
385 						rio_dprintk (RIO_DEBUG_CTRL, "Map entry %d.SysPort = 0x%x\n", entry, (int)p->RIOConnectTable[entry].SysPort );
386 						rio_dprintk (RIO_DEBUG_CTRL, "Map entry %d.Top[0].Unit = %x\n", entry, p->RIOConnectTable[entry].Topology[0].Unit );
387 						rio_dprintk (RIO_DEBUG_CTRL, "Map entry %d.Top[0].Link = %x\n", entry, p->RIOConnectTable[entry].Topology[0].Link );
388 						rio_dprintk (RIO_DEBUG_CTRL, "Map entry %d.Top[1].Unit = %x\n", entry, p->RIOConnectTable[entry].Topology[1].Unit );
389 						rio_dprintk (RIO_DEBUG_CTRL, "Map entry %d.Top[1].Link = %x\n", entry, p->RIOConnectTable[entry].Topology[1].Link );
390 						rio_dprintk (RIO_DEBUG_CTRL, "Map entry %d.Top[2].Unit = %x\n", entry, p->RIOConnectTable[entry].Topology[2].Unit );
391 						rio_dprintk (RIO_DEBUG_CTRL, "Map entry %d.Top[2].Link = %x\n", entry, p->RIOConnectTable[entry].Topology[2].Link );
392 						rio_dprintk (RIO_DEBUG_CTRL, "Map entry %d.Top[3].Unit = %x\n", entry, p->RIOConnectTable[entry].Topology[3].Unit );
393 						rio_dprintk (RIO_DEBUG_CTRL, "Map entry %d.Top[4].Link = %x\n", entry, p->RIOConnectTable[entry].Topology[3].Link );
394 						rio_dprintk (RIO_DEBUG_CTRL, "Map entry %d.Name = %s\n", entry, p->RIOConnectTable[entry].Name );
395 					}
396 					rio_dprintk (RIO_DEBUG_CTRL,  "*****\nEND MAP ENTRIES\n");
397 				}
398 				p->RIOQuickCheck = NOT_CHANGED;	/* a table has been gotten */
399 				return 0;
400 
401 			case RIO_PUT_TABLE:
402 				/*
403 				** Write the routing table to the device driver from user space
404 				*/
405 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_PUT_TABLE\n");
406 
407 				if ( !su ) {
408 		 			rio_dprintk (RIO_DEBUG_CTRL, "RIO_PUT_TABLE !Root\n");
409 		 			p->RIOError.Error = NOT_SUPER_USER;
410 		 			return EPERM;
411 				}
412 				if ( copyin((int)arg, (caddr_t)&p->RIOConnectTable[0],
413 					TOTAL_MAP_ENTRIES*sizeof(struct Map) ) == COPYFAIL ) {
414 		 			rio_dprintk (RIO_DEBUG_CTRL, "RIO_PUT_TABLE copy failed\n");
415 		 			p->RIOError.Error = COPYIN_FAILED;
416 		 			return EFAULT;
417 				}
418 /*
419 ***********************************
420 				{
421 					int entry;
422 					rio_dprint(RIO_DEBUG_CTRL,  ("*****\nMAP ENTRIES\n") );
423 					for ( entry=0; entry<TOTAL_MAP_ENTRIES; entry++ )
424 					{
425 						rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.HostUniqueNum = 0x%x\n", entry, p->RIOConnectTable[entry].HostUniqueNum ) );
426 						rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.RtaUniqueNum = 0x%x\n", entry, p->RIOConnectTable[entry].RtaUniqueNum ) );
427 						rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.ID = 0x%x\n", entry, p->RIOConnectTable[entry].ID ) );
428 						rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.ID2 = 0x%x\n", entry, p->RIOConnectTable[entry].ID2 ) );
429 						rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Flags = 0x%x\n", entry, p->RIOConnectTable[entry].Flags ) );
430 						rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.SysPort = 0x%x\n", entry, p->RIOConnectTable[entry].SysPort ) );
431 						rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Top[0].Unit = %b\n", entry, p->RIOConnectTable[entry].Topology[0].Unit ) );
432 						rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Top[0].Link = %b\n", entry, p->RIOConnectTable[entry].Topology[0].Link ) );
433 						rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Top[1].Unit = %b\n", entry, p->RIOConnectTable[entry].Topology[1].Unit ) );
434 						rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Top[1].Link = %b\n", entry, p->RIOConnectTable[entry].Topology[1].Link ) );
435 						rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Top[2].Unit = %b\n", entry, p->RIOConnectTable[entry].Topology[2].Unit ) );
436 						rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Top[2].Link = %b\n", entry, p->RIOConnectTable[entry].Topology[2].Link ) );
437 						rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Top[3].Unit = %b\n", entry, p->RIOConnectTable[entry].Topology[3].Unit ) );
438 						rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Top[4].Link = %b\n", entry, p->RIOConnectTable[entry].Topology[3].Link ) );
439 						rio_dprint(RIO_DEBUG_CTRL,  ("Map entry %d.Name = %s\n", entry, p->RIOConnectTable[entry].Name ) );
440 					}
441 					rio_dprint(RIO_DEBUG_CTRL,  ("*****\nEND MAP ENTRIES\n") );
442 				}
443 ***********************************
444 */
445 				return RIONewTable(p);
446 
447 	 		case RIO_GET_BINDINGS :
448 				/*
449 				** Send bindings table, containing unique numbers of RTAs owned
450 				** by this system to user space
451 				*/
452 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_GET_BINDINGS\n");
453 
454 				if ( !su )
455 				{
456 		 			rio_dprintk (RIO_DEBUG_CTRL, "RIO_GET_BINDINGS !Root\n");
457 		 			p->RIOError.Error = NOT_SUPER_USER;
458 		 			return EPERM;
459 				}
460 				if (copyout((caddr_t) p->RIOBindTab, (int)arg,
461 						(sizeof(ulong) * MAX_RTA_BINDINGS)) == COPYFAIL ) {
462 		 			rio_dprintk (RIO_DEBUG_CTRL, "RIO_GET_BINDINGS copy failed\n");
463 		 			p->RIOError.Error = COPYOUT_FAILED;
464 		 			return EFAULT;
465 				}
466 				return 0;
467 
468 	 		case RIO_PUT_BINDINGS :
469 			/*
470 			** Receive a bindings table, containing unique numbers of RTAs owned
471 			** by this system
472 			*/
473 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_PUT_BINDINGS\n");
474 
475 				if ( !su )
476 				{
477 		 			rio_dprintk (RIO_DEBUG_CTRL, "RIO_PUT_BINDINGS !Root\n");
478 		 			p->RIOError.Error = NOT_SUPER_USER;
479 		 			return EPERM;
480 				}
481 				if (copyin((int)arg, (caddr_t)&p->RIOBindTab[0],
482 						(sizeof(ulong) * MAX_RTA_BINDINGS))==COPYFAIL ) {
483 		 			rio_dprintk (RIO_DEBUG_CTRL, "RIO_PUT_BINDINGS copy failed\n");
484 		 			p->RIOError.Error = COPYIN_FAILED;
485 		 			return EFAULT;
486 				}
487 				return 0;
488 
489 			case RIO_BIND_RTA :
490 				{
491 					int	EmptySlot = -1;
492 					/*
493 					** Bind this RTA to host, so that it will be booted by
494 					** host in 'boot owned RTAs' mode.
495 					*/
496 					rio_dprintk (RIO_DEBUG_CTRL, "RIO_BIND_RTA\n");
497 
498 					if ( !su ) {
499 		 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_BIND_RTA !Root\n");
500 		 				p->RIOError.Error = NOT_SUPER_USER;
501 		 				return EPERM;
502 					}
503 					for (Entry = 0; Entry < MAX_RTA_BINDINGS; Entry++) {
504 		 				if ((EmptySlot == -1) && (p->RIOBindTab[Entry] == 0L))
505 							EmptySlot = Entry;
506 		 				else if (p->RIOBindTab[Entry] == (int) arg) {
507 							/*
508 							** Already exists - delete
509 							*/
510 							p->RIOBindTab[Entry] = 0L;
511 							rio_dprintk (RIO_DEBUG_CTRL, "Removing Rta %x from p->RIOBindTab\n",
512 		 												(int) arg);
513 							return 0;
514 		 				}
515 					}
516 					/*
517 					** Dosen't exist - add
518 					*/
519 					if (EmptySlot != -1) {
520 		 				p->RIOBindTab[EmptySlot] = (int) arg;
521 		 				rio_dprintk (RIO_DEBUG_CTRL, "Adding Rta %x to p->RIOBindTab\n",
522 		  					(int) arg);
523 					}
524 					else {
525 		 				rio_dprintk (RIO_DEBUG_CTRL, "p->RIOBindTab full! - Rta %x not added\n",
526 		  					(int) arg);
527 		 				return 1;
528 					}
529 					return 0;
530 				}
531 
532 			case RIO_RESUME :
533 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_RESUME\n");
534 				port = (uint) arg;
535 				if ((port < 0) || (port > 511)) {
536 		 			rio_dprintk (RIO_DEBUG_CTRL, "RIO_RESUME: Bad port number %d\n", port);
537 		 			p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
538 		 			return EINVAL;
539 				}
540 				PortP = p->RIOPortp[port];
541 				if (!PortP->Mapped) {
542 		 			rio_dprintk (RIO_DEBUG_CTRL, "RIO_RESUME: Port %d not mapped\n", port);
543 		 			p->RIOError.Error = PORT_NOT_MAPPED_INTO_SYSTEM;
544 		 			return EINVAL;
545 				}
546 				if (!(PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
547 					rio_dprintk (RIO_DEBUG_CTRL, "RIO_RESUME: Port %d not open\n", port);
548 		 			return EINVAL;
549 				}
550 
551 				rio_spin_lock_irqsave(&PortP->portSem, flags);
552 				if (RIOPreemptiveCmd(p, (p->RIOPortp[port]), RESUME) ==
553 										RIO_FAIL) {
554 					rio_dprintk (RIO_DEBUG_CTRL, "RIO_RESUME failed\n");
555 					rio_spin_unlock_irqrestore(&PortP->portSem, flags);
556 					return EBUSY;
557 				}
558 				else {
559 					rio_dprintk (RIO_DEBUG_CTRL, "RIO_RESUME: Port %d resumed\n", port);
560 					PortP->State |= RIO_BUSY;
561 				}
562 				rio_spin_unlock_irqrestore(&PortP->portSem, flags);
563 				return retval;
564 
565 			case RIO_ASSIGN_RTA:
566 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_ASSIGN_RTA\n");
567 				if ( !su ) {
568 					rio_dprintk (RIO_DEBUG_CTRL, "RIO_ASSIGN_RTA !Root\n");
569 					p->RIOError.Error = NOT_SUPER_USER;
570 					return EPERM;
571 				}
572 				if (copyin((int)arg, (caddr_t)&MapEnt, sizeof(MapEnt))
573 									== COPYFAIL) {
574 					rio_dprintk (RIO_DEBUG_CTRL, "Copy from user space failed\n");
575 					p->RIOError.Error = COPYIN_FAILED;
576 					return EFAULT;
577 				}
578 				return RIOAssignRta(p, &MapEnt);
579 
580 			case RIO_CHANGE_NAME:
581 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_CHANGE_NAME\n");
582 				if ( !su ) {
583 					rio_dprintk (RIO_DEBUG_CTRL, "RIO_CHANGE_NAME !Root\n");
584 					p->RIOError.Error = NOT_SUPER_USER;
585 					return EPERM;
586 				}
587 				if (copyin((int)arg, (caddr_t)&MapEnt, sizeof(MapEnt))
588 						== COPYFAIL) {
589 					rio_dprintk (RIO_DEBUG_CTRL, "Copy from user space failed\n");
590 					p->RIOError.Error = COPYIN_FAILED;
591 					return EFAULT;
592 				}
593 				return RIOChangeName(p, &MapEnt);
594 
595 			case RIO_DELETE_RTA:
596 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_DELETE_RTA\n");
597 				if ( !su ) {
598 		 			rio_dprintk (RIO_DEBUG_CTRL, "RIO_DELETE_RTA !Root\n");
599 		 			p->RIOError.Error = NOT_SUPER_USER;
600 		 			return EPERM;
601 				}
602 				if (copyin((int)arg, (caddr_t)&MapEnt, sizeof(MapEnt))
603 							== COPYFAIL ) {
604 		 			rio_dprintk (RIO_DEBUG_CTRL, "Copy from data space failed\n");
605 		 			p->RIOError.Error = COPYIN_FAILED;
606 		 			return EFAULT;
607 				}
608 				return RIODeleteRta(p, &MapEnt);
609 
610 			case RIO_QUICK_CHECK:
611 				/*
612 				** 09.12.1998 ARG - ESIL 0776 part fix
613 				** A customer was using this to get the RTAs
614 				** connect/disconnect status.
615 				** RIOConCon() had been botched use RIOHalted
616 				** to keep track of RTA connections and
617 				** disconnections. That has been changed and
618 				** RIORtaDisCons in the rio_info struct now
619 				** does the job. So we need to return the value
620 				** of RIORtaCons instead of RIOHalted.
621 				**
622 				if (copyout((caddr_t)&p->RIOHalted,(int)arg,
623 							sizeof(uint))==COPYFAIL) {
624 				**
625 				*/
626 
627 				if (copyout((caddr_t)&p->RIORtaDisCons,(int)arg,
628 							sizeof(uint))==COPYFAIL) {
629 					p->RIOError.Error = COPYOUT_FAILED;
630 					return EFAULT;
631 				}
632 				return 0;
633 
634 			case RIO_LAST_ERROR:
635 				if (copyout((caddr_t)&p->RIOError, (int)arg,
636 						sizeof(struct Error)) ==COPYFAIL )
637 					return EFAULT;
638 				return 0;
639 
640 			case RIO_GET_LOG:
641 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_GET_LOG\n");
642 #ifdef LOGGING
643 				RIOGetLog(arg);
644 				return 0;
645 #else
646 				return EINVAL;
647 #endif
648 
649 			case RIO_GET_MODTYPE:
650 				if ( copyin( (int)arg, (caddr_t)&port,
651 									sizeof(uint)) == COPYFAIL )
652 				{
653 		 			p->RIOError.Error = COPYIN_FAILED;
654 		 			return EFAULT;
655 				}
656 				rio_dprintk (RIO_DEBUG_CTRL, "Get module type for port %d\n", port);
657 				if ( port < 0 || port > 511 )
658 				{
659 		 			rio_dprintk (RIO_DEBUG_CTRL, "RIO_GET_MODTYPE: Bad port number %d\n", port);
660 		 			p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
661 		 			return EINVAL;
662 				}
663 				PortP = (p->RIOPortp[port]);
664 				if (!PortP->Mapped)
665 				{
666 		 			rio_dprintk (RIO_DEBUG_CTRL, "RIO_GET_MODTYPE: Port %d not mapped\n", port);
667 		 			p->RIOError.Error = PORT_NOT_MAPPED_INTO_SYSTEM;
668 		 			return EINVAL;
669 				}
670 				/*
671 				** Return module type of port
672 				*/
673 				port = PortP->HostP->UnixRups[PortP->RupNum].ModTypes;
674 				if (copyout((caddr_t)&port, (int)arg,
675 							sizeof(uint)) == COPYFAIL) {
676 		 			p->RIOError.Error = COPYOUT_FAILED;
677 		 			return EFAULT;
678 				}
679 				return(0);
680 			/*
681 			** 02.03.1999 ARG - ESIL 0820 fix
682 			** We are no longer using "Boot Mode", so these ioctls
683 			** are not required :
684 			**
685 	 		case RIO_GET_BOOT_MODE :
686 				rio_dprint(RIO_DEBUG_CTRL, ("Get boot mode - %x\n", p->RIOBootMode));
687 				**
688 				** Return boot state of system - BOOT_ALL, BOOT_OWN or BOOT_NONE
689 				**
690 				if (copyout((caddr_t)&p->RIOBootMode, (int)arg,
691 						sizeof(p->RIOBootMode)) == COPYFAIL) {
692 		 			p->RIOError.Error = COPYOUT_FAILED;
693 		 			return EFAULT;
694 				}
695 				return(0);
696 
697  			case RIO_SET_BOOT_MODE :
698 				p->RIOBootMode = (uint) arg;
699 				rio_dprint(RIO_DEBUG_CTRL, ("Set boot mode to 0x%x\n", p->RIOBootMode));
700 				return(0);
701 			**
702 			** End ESIL 0820 fix
703 			*/
704 
705 	 		case RIO_BLOCK_OPENS:
706 				rio_dprintk (RIO_DEBUG_CTRL, "Opens block until booted\n");
707 				for ( Entry=0; Entry < RIO_PORTS; Entry++ ) {
708 		 			rio_spin_lock_irqsave(&PortP->portSem, flags);
709 		 			p->RIOPortp[Entry]->WaitUntilBooted = 1;
710 		 			rio_spin_unlock_irqrestore(&PortP->portSem, flags);
711 				}
712 				return 0;
713 
714 	 		case RIO_SETUP_PORTS:
715 				rio_dprintk (RIO_DEBUG_CTRL, "Setup ports\n");
716 				if (copyin((int)arg, (caddr_t)&PortSetup, sizeof(PortSetup))
717 						== COPYFAIL ) {
718 					 p->RIOError.Error = COPYIN_FAILED;
719 					 rio_dprintk (RIO_DEBUG_CTRL, "EFAULT");
720 					 return EFAULT;
721 				}
722 				if ( PortSetup.From > PortSetup.To ||
723 								PortSetup.To >= RIO_PORTS ) {
724 					 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
725 					 rio_dprintk (RIO_DEBUG_CTRL, "ENXIO");
726 					 return ENXIO;
727 				}
728 				if ( PortSetup.XpCps > p->RIOConf.MaxXpCps ||
729 					 PortSetup.XpCps < p->RIOConf.MinXpCps ) {
730 					 p->RIOError.Error = XPRINT_CPS_OUT_OF_RANGE;
731 					 rio_dprintk (RIO_DEBUG_CTRL, "EINVAL");
732 					 return EINVAL;
733 				}
734 				if ( !p->RIOPortp ) {
735 					 cprintf("No p->RIOPortp array!\n");
736 					 rio_dprintk (RIO_DEBUG_CTRL, "No p->RIOPortp array!\n");
737 					 return EIO;
738 				}
739 				rio_dprintk (RIO_DEBUG_CTRL, "entering loop (%d %d)!\n", PortSetup.From, PortSetup.To);
740 				for (loop=PortSetup.From; loop<=PortSetup.To; loop++) {
741 				rio_dprintk (RIO_DEBUG_CTRL, "in loop (%d)!\n", loop);
742 #if 0
743 					PortP = p->RIOPortp[loop];
744 					if ( !PortP->TtyP )
745 						PortP->TtyP = &p->channel[loop];
746 
747 		 				rio_spin_lock_irqsave(&PortP->portSem, flags);
748 						if ( PortSetup.IxAny )
749 							PortP->Config |= RIO_IXANY;
750 						else
751 							PortP->Config &= ~RIO_IXANY;
752 						if ( PortSetup.IxOn )
753 							PortP->Config |= RIO_IXON;
754 						else
755 							PortP->Config &= ~RIO_IXON;
756 
757 					 /*
758 					 ** If the port needs to wait for all a processes output
759 					 ** to drain before closing then this flag will be set.
760 					 */
761 					 	if (PortSetup.Drain) {
762 							PortP->Config |= RIO_WAITDRAIN;
763 					 	} else {
764 							PortP->Config &= ~RIO_WAITDRAIN;
765 					 	}
766 					 /*
767 					 ** Store settings if locking or unlocking port or if the
768 					 ** port is not locked, when setting the store option.
769 					 */
770 					 if (PortP->Mapped &&
771 						 ((PortSetup.Lock && !PortP->Lock) ||
772 							(!PortP->Lock &&
773 							(PortSetup.Store && !PortP->Store)))) {
774 						PortP->StoredTty.iflag = PortP->TtyP->tm.c_iflag;
775 						PortP->StoredTty.oflag = PortP->TtyP->tm.c_oflag;
776 						PortP->StoredTty.cflag = PortP->TtyP->tm.c_cflag;
777 						PortP->StoredTty.lflag = PortP->TtyP->tm.c_lflag;
778 						PortP->StoredTty.line = PortP->TtyP->tm.c_line;
779 						bcopy(PortP->TtyP->tm.c_cc, PortP->StoredTty.cc,
780 					 		NCC + 5);
781 					 }
782 					 PortP->Lock = PortSetup.Lock;
783 					 PortP->Store = PortSetup.Store;
784 					 PortP->Xprint.XpCps = PortSetup.XpCps;
785 					 bcopy(PortSetup.XpOn,PortP->Xprint.XpOn,MAX_XP_CTRL_LEN);
786 					 bcopy(PortSetup.XpOff,PortP->Xprint.XpOff,MAX_XP_CTRL_LEN);
787 					 PortP->Xprint.XpOn[MAX_XP_CTRL_LEN-1] = '\0';
788 					 PortP->Xprint.XpOff[MAX_XP_CTRL_LEN-1] = '\0';
789 					 PortP->Xprint.XpLen = RIOStrlen(PortP->Xprint.XpOn)+
790 								RIOStrlen(PortP->Xprint.XpOff);
791 					 rio_spin_unlock_irqrestore( &PortP->portSem , flags);
792 #endif
793 				}
794 				rio_dprintk (RIO_DEBUG_CTRL, "after loop (%d)!\n", loop);
795 				rio_dprintk (RIO_DEBUG_CTRL, "Retval:%x\n", retval);
796 				return retval;
797 
798 			case RIO_GET_PORT_SETUP :
799 				rio_dprintk (RIO_DEBUG_CTRL, "Get port setup\n");
800 				if (copyin((int)arg, (caddr_t)&PortSetup, sizeof(PortSetup))
801 							== COPYFAIL ) {
802 					 p->RIOError.Error = COPYIN_FAILED;
803 					 return EFAULT;
804 				}
805 				if ( PortSetup.From >= RIO_PORTS ) {
806 					 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
807 					 return ENXIO;
808 				}
809 
810 				port = PortSetup.To = PortSetup.From;
811 				PortSetup.IxAny = (p->RIOPortp[port]->Config & RIO_IXANY) ?
812 													1 : 0;
813 				PortSetup.IxOn = (p->RIOPortp[port]->Config & RIO_IXON) ?
814 													1 : 0;
815 				PortSetup.Drain = (p->RIOPortp[port]->Config & RIO_WAITDRAIN) ?
816 												 	1 : 0;
817 				PortSetup.Store = p->RIOPortp[port]->Store;
818 				PortSetup.Lock = p->RIOPortp[port]->Lock;
819 				PortSetup.XpCps = p->RIOPortp[port]->Xprint.XpCps;
820 				bcopy(p->RIOPortp[port]->Xprint.XpOn, PortSetup.XpOn,
821 													MAX_XP_CTRL_LEN);
822 				bcopy(p->RIOPortp[port]->Xprint.XpOff, PortSetup.XpOff,
823 													MAX_XP_CTRL_LEN);
824 				PortSetup.XpOn[MAX_XP_CTRL_LEN-1] = '\0';
825 				PortSetup.XpOff[MAX_XP_CTRL_LEN-1] = '\0';
826 
827 				if ( copyout((caddr_t)&PortSetup,(int)arg,sizeof(PortSetup))
828 														==COPYFAIL ) {
829 					 p->RIOError.Error = COPYOUT_FAILED;
830 					 return EFAULT;
831 				}
832 				return retval;
833 
834 			case RIO_GET_PORT_PARAMS :
835 				rio_dprintk (RIO_DEBUG_CTRL, "Get port params\n");
836 				if (copyin( (int)arg, (caddr_t)&PortParams,
837 					sizeof(struct PortParams)) == COPYFAIL) {
838 					p->RIOError.Error = COPYIN_FAILED;
839 					return EFAULT;
840 				}
841 				if (PortParams.Port >= RIO_PORTS) {
842 					p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
843 					return ENXIO;
844 				}
845 				PortP = (p->RIOPortp[PortParams.Port]);
846 				PortParams.Config = PortP->Config;
847 				PortParams.State = PortP->State;
848 				rio_dprintk (RIO_DEBUG_CTRL, "Port %d\n", PortParams.Port);
849 
850 				if (copyout((caddr_t)&PortParams, (int)arg,
851 						sizeof(struct PortParams)) == COPYFAIL ) {
852 					 p->RIOError.Error = COPYOUT_FAILED;
853 					 return EFAULT;
854 				}
855 				return retval;
856 
857 			case RIO_GET_PORT_TTY :
858 				rio_dprintk (RIO_DEBUG_CTRL, "Get port tty\n");
859 				if (copyin((int)arg, (caddr_t)&PortTty, sizeof(struct PortTty))
860 						== COPYFAIL) {
861 					 p->RIOError.Error = COPYIN_FAILED;
862 					 return EFAULT;
863 				}
864 				if ( PortTty.port >= RIO_PORTS ) {
865 					 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
866 					 return ENXIO;
867 				}
868 
869 				rio_dprintk (RIO_DEBUG_CTRL, "Port %d\n", PortTty.port);
870 				PortP = (p->RIOPortp[PortTty.port]);
871 #if 0
872 				PortTty.Tty.tm.c_iflag = PortP->TtyP->tm.c_iflag;
873 				PortTty.Tty.tm.c_oflag = PortP->TtyP->tm.c_oflag;
874 				PortTty.Tty.tm.c_cflag = PortP->TtyP->tm.c_cflag;
875 				PortTty.Tty.tm.c_lflag = PortP->TtyP->tm.c_lflag;
876 #endif
877 				if (copyout((caddr_t)&PortTty, (int)arg,
878 							sizeof(struct PortTty)) == COPYFAIL) {
879 					p->RIOError.Error = COPYOUT_FAILED;
880 					return EFAULT;
881 				}
882 				return retval;
883 
884 			case RIO_SET_PORT_TTY :
885 				if (copyin((int)arg, (caddr_t)&PortTty,
886 						sizeof(struct PortTty)) == COPYFAIL) {
887 					 p->RIOError.Error = COPYIN_FAILED;
888 					 return EFAULT;
889 				}
890 				rio_dprintk (RIO_DEBUG_CTRL, "Set port %d tty\n", PortTty.port);
891 				if (PortTty.port >= (ushort) RIO_PORTS) {
892 					 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
893 					 return ENXIO;
894 				}
895 				PortP = (p->RIOPortp[PortTty.port]);
896 #if 0
897 		 		rio_spin_lock_irqsave(&PortP->portSem, flags);
898 				PortP->TtyP->tm.c_iflag = PortTty.Tty.tm.c_iflag;
899 				PortP->TtyP->tm.c_oflag = PortTty.Tty.tm.c_oflag;
900 				PortP->TtyP->tm.c_cflag = PortTty.Tty.tm.c_cflag;
901 				PortP->TtyP->tm.c_lflag = PortTty.Tty.tm.c_lflag;
902 				rio_spin_unlock_irqrestore( &PortP->portSem , flags);
903 #endif
904 
905 				RIOParam(PortP, CONFIG, PortP->State & RIO_MODEM, OK_TO_SLEEP);
906 				return retval;
907 
908 			case RIO_SET_PORT_PARAMS :
909 				rio_dprintk (RIO_DEBUG_CTRL, "Set port params\n");
910 				if ( copyin((int)arg, (caddr_t)&PortParams, sizeof(PortParams))
911 					== COPYFAIL ) {
912 					 p->RIOError.Error = COPYIN_FAILED;
913 					 return EFAULT;
914 				}
915 				if (PortParams.Port >= (ushort) RIO_PORTS) {
916 					 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
917 					 return ENXIO;
918 				}
919 				PortP = (p->RIOPortp[PortParams.Port]);
920 		 		rio_spin_lock_irqsave(&PortP->portSem, flags);
921 				PortP->Config = PortParams.Config;
922 				rio_spin_unlock_irqrestore( &PortP->portSem , flags);
923 				return retval;
924 
925 			case RIO_GET_PORT_STATS :
926 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_GET_PORT_STATS\n");
927 				if ( copyin((int)arg, (caddr_t)&portStats,
928 						sizeof(struct portStats)) == COPYFAIL ) {
929 					 p->RIOError.Error = COPYIN_FAILED;
930 					 return EFAULT;
931 				}
932 				if ( portStats.port >= RIO_PORTS ) {
933 					 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
934 					 return ENXIO;
935 				}
936 				PortP = (p->RIOPortp[portStats.port]);
937 				portStats.gather = PortP->statsGather;
938 				portStats.txchars = PortP->txchars;
939 				portStats.rxchars = PortP->rxchars;
940 				portStats.opens = PortP->opens;
941 				portStats.closes = PortP->closes;
942 				portStats.ioctls = PortP->ioctls;
943 				if ( copyout((caddr_t)&portStats, (int)arg,
944 							sizeof(struct portStats)) == COPYFAIL ) {
945 					 p->RIOError.Error = COPYOUT_FAILED;
946 					 return EFAULT;
947 				}
948 				return retval;
949 
950 			case RIO_RESET_PORT_STATS :
951 				port = (uint) arg;
952 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_RESET_PORT_STATS\n");
953 				if ( port >= RIO_PORTS ) {
954 					 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
955 					 return ENXIO;
956 				}
957 				PortP = (p->RIOPortp[port]);
958 				rio_spin_lock_irqsave(&PortP->portSem, flags);
959 				PortP->txchars	= 0;
960 				PortP->rxchars	= 0;
961 				PortP->opens	= 0;
962 				PortP->closes	= 0;
963 				PortP->ioctls	= 0;
964 				rio_spin_unlock_irqrestore(&PortP->portSem, flags);
965 				return retval;
966 
967 			case RIO_GATHER_PORT_STATS :
968 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_GATHER_PORT_STATS\n");
969 				if ( copyin( (int)arg, (caddr_t)&portStats,
970 						sizeof(struct portStats)) == COPYFAIL ) {
971 					 p->RIOError.Error = COPYIN_FAILED;
972 					 return EFAULT;
973 				}
974 				if ( portStats.port >= RIO_PORTS ) {
975 					 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
976 					 return ENXIO;
977 				}
978 				PortP = (p->RIOPortp[portStats.port]);
979 				rio_spin_lock_irqsave(&PortP->portSem, flags);
980 				PortP->statsGather = portStats.gather;
981 				rio_spin_unlock_irqrestore( &PortP->portSem , flags);
982 				return retval;
983 
984 #ifdef DEBUG_SUPPORTED
985 			case RIO_READ_LEVELS:
986 				{
987 					 int num;
988 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_READ_LEVELS\n");
989 					 for ( num=0; RIODbInf[num].Flag; num++ ) ;
990 					 rio_dprintk (RIO_DEBUG_CTRL, "%d levels to copy\n",num);
991 					 if (copyout((caddr_t)RIODbInf,(int)arg,
992 						sizeof(struct DbInf)*(num+1))==COPYFAIL) {
993 						rio_dprintk (RIO_DEBUG_CTRL, "ReadLevels Copy failed\n");
994 						p->RIOError.Error = COPYOUT_FAILED;
995 						return EFAULT;
996 					 }
997 					 rio_dprintk (RIO_DEBUG_CTRL, "%d levels to copied\n",num);
998 					 return retval;
999 				}
1000 #endif
1001 
1002 			 case RIO_READ_CONFIG:
1003 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_READ_CONFIG\n");
1004 				if (copyout((caddr_t)&p->RIOConf, (int)arg,
1005 							sizeof(struct Conf)) ==COPYFAIL ) {
1006 					 p->RIOError.Error = COPYOUT_FAILED;
1007 					 return EFAULT;
1008 				}
1009 				return retval;
1010 
1011 			case RIO_SET_CONFIG:
1012 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_SET_CONFIG\n");
1013 				if ( !su ) {
1014 					 p->RIOError.Error = NOT_SUPER_USER;
1015 					 return EPERM;
1016 				}
1017 				if ( copyin((int)arg, (caddr_t)&p->RIOConf, sizeof(struct Conf) )
1018 						==COPYFAIL ) {
1019 					 p->RIOError.Error = COPYIN_FAILED;
1020 					 return EFAULT;
1021 				}
1022 				/*
1023 				** move a few value around
1024 				*/
1025 				for (Host=0; Host < p->RIONumHosts; Host++)
1026 					 if ( (p->RIOHosts[Host].Flags & RUN_STATE) == RC_RUNNING )
1027 					 	WWORD(p->RIOHosts[Host].ParmMapP->timer ,
1028 								p->RIOConf.Timer);
1029 				return retval;
1030 
1031 			case RIO_START_POLLER:
1032 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_START_POLLER\n");
1033 				return EINVAL;
1034 
1035 			case RIO_STOP_POLLER:
1036 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_STOP_POLLER\n");
1037 				if ( !su ) {
1038 					 p->RIOError.Error = NOT_SUPER_USER;
1039 					 return EPERM;
1040 				}
1041 				p->RIOPolling = NOT_POLLING;
1042 				return retval;
1043 
1044 			case RIO_SETDEBUG:
1045 			case RIO_GETDEBUG:
1046 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_SETDEBUG/RIO_GETDEBUG\n");
1047 				if ( copyin( (int)arg, (caddr_t)&DebugCtrl, sizeof(DebugCtrl) )
1048 							==COPYFAIL ) {
1049 					 p->RIOError.Error = COPYIN_FAILED;
1050 					 return EFAULT;
1051 				}
1052 				if ( DebugCtrl.SysPort == NO_PORT ) {
1053 					if ( cmd == RIO_SETDEBUG ) {
1054 						if ( !su ) {
1055 							p->RIOError.Error = NOT_SUPER_USER;
1056 							return EPERM;
1057 						}
1058 						p->rio_debug = DebugCtrl.Debug;
1059 						p->RIODebugWait = DebugCtrl.Wait;
1060 						rio_dprintk (RIO_DEBUG_CTRL, "Set global debug to 0x%x set wait to 0x%x\n",
1061 							p->rio_debug,p->RIODebugWait);
1062 					}
1063 				 	else {
1064 						rio_dprintk (RIO_DEBUG_CTRL, "Get global debug 0x%x wait 0x%x\n",
1065 										p->rio_debug,p->RIODebugWait);
1066 						DebugCtrl.Debug = p->rio_debug;
1067 						DebugCtrl.Wait  = p->RIODebugWait;
1068 						if ( copyout((caddr_t)&DebugCtrl,(int)arg,
1069 								sizeof(DebugCtrl)) == COPYFAIL ) {
1070 							rio_dprintk (RIO_DEBUG_CTRL, "RIO_SET/GET DEBUG: bad port number %d\n",
1071 									DebugCtrl.SysPort);
1072 						 	p->RIOError.Error = COPYOUT_FAILED;
1073 						 	return EFAULT;
1074 						}
1075 					}
1076 				}
1077 				else if ( DebugCtrl.SysPort >= RIO_PORTS &&
1078 							DebugCtrl.SysPort != NO_PORT ) {
1079 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_SET/GET DEBUG: bad port number %d\n",
1080 									DebugCtrl.SysPort);
1081 					 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
1082 					 return ENXIO;
1083 				}
1084 				else if ( cmd == RIO_SETDEBUG ) {
1085 					if ( !su ) {
1086 						p->RIOError.Error = NOT_SUPER_USER;
1087 						return EPERM;
1088 					}
1089 					rio_spin_lock_irqsave(&PortP->portSem, flags);
1090 					p->RIOPortp[DebugCtrl.SysPort]->Debug = DebugCtrl.Debug;
1091 					rio_spin_unlock_irqrestore( &PortP->portSem , flags);
1092 					rio_dprintk (RIO_DEBUG_CTRL, "RIO_SETDEBUG 0x%x\n",
1093 								p->RIOPortp[DebugCtrl.SysPort]->Debug);
1094 				}
1095 				else {
1096 					rio_dprintk (RIO_DEBUG_CTRL, "RIO_GETDEBUG 0x%x\n",
1097 									 p->RIOPortp[DebugCtrl.SysPort]->Debug);
1098 					DebugCtrl.Debug = p->RIOPortp[DebugCtrl.SysPort]->Debug;
1099 					if ( copyout((caddr_t)&DebugCtrl,(int)arg,
1100 								sizeof(DebugCtrl))==COPYFAIL ) {
1101 						rio_dprintk (RIO_DEBUG_CTRL, "RIO_GETDEBUG: Bad copy to user space\n");
1102 						p->RIOError.Error = COPYOUT_FAILED;
1103 						return EFAULT;
1104 					}
1105 				}
1106 				return retval;
1107 
1108 			case RIO_VERSID:
1109 				/*
1110 				** Enquire about the release and version.
1111 				** We return MAX_VERSION_LEN bytes, being a
1112 				** textual null terminated string.
1113 				*/
1114 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_VERSID\n");
1115 				if ( copyout(	(caddr_t)RIOVersid(),
1116 						(int)arg,
1117 						sizeof(struct rioVersion) ) == COPYFAIL )
1118 				{
1119 					 rio_dprintk (RIO_DEBUG_CTRL,  "RIO_VERSID: Bad copy to user space (host=%d)\n", Host);
1120 					 p->RIOError.Error = COPYOUT_FAILED;
1121 					 return EFAULT;
1122 				}
1123 				return retval;
1124 
1125 			/*
1126 			** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1127 			** !! commented out previous 'RIO_VERSID' functionality !!
1128 			** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1129 			**
1130 			case RIO_VERSID:
1131 				**
1132 				** Enquire about the release and version.
1133 				** We return MAX_VERSION_LEN bytes, being a textual null
1134 				** terminated string.
1135 				**
1136 				rio_dprint(RIO_DEBUG_CTRL, ("RIO_VERSID\n"));
1137 				if (copyout((caddr_t)RIOVersid(),
1138 						(int)arg, MAX_VERSION_LEN ) == COPYFAIL ) {
1139 					 rio_dprint(RIO_DEBUG_CTRL, ("RIO_VERSID: Bad copy to user space\n",Host));
1140 					 p->RIOError.Error = COPYOUT_FAILED;
1141 					 return EFAULT;
1142 				}
1143 				return retval;
1144 			**
1145 			** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1146 			*/
1147 
1148 			case RIO_NUM_HOSTS:
1149 				/*
1150 				** Enquire as to the number of hosts located
1151 				** at init time.
1152 				*/
1153 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_NUM_HOSTS\n");
1154 				if (copyout((caddr_t)&p->RIONumHosts, (int)arg,
1155 							sizeof(p->RIONumHosts) )==COPYFAIL ) {
1156 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_NUM_HOSTS: Bad copy to user space\n");
1157 					 p->RIOError.Error = COPYOUT_FAILED;
1158 					 return EFAULT;
1159 				}
1160 				return retval;
1161 
1162 			case RIO_HOST_FOAD:
1163 				/*
1164 				** Kill host. This may not be in the final version...
1165 				*/
1166 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_FOAD %d\n", (int)arg);
1167 				if ( !su ) {
1168 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_FOAD: Not super user\n");
1169 					 p->RIOError.Error = NOT_SUPER_USER;
1170 					 return EPERM;
1171 				}
1172 				p->RIOHalted = 1;
1173 				p->RIOSystemUp = 0;
1174 
1175 				for ( Host=0; Host<p->RIONumHosts; Host++ ) {
1176 					 (void)RIOBoardTest( p->RIOHosts[Host].PaddrP,
1177 						p->RIOHosts[Host].Caddr, p->RIOHosts[Host].Type,
1178 								p->RIOHosts[Host].Slot );
1179 					 bzero( (caddr_t)&p->RIOHosts[Host].Flags,
1180 							((int)&p->RIOHosts[Host].____end_marker____) -
1181 								 ((int)&p->RIOHosts[Host].Flags) );
1182 					 p->RIOHosts[Host].Flags  = RC_WAITING;
1183 #if 0
1184 					 RIOSetupDataStructs(p);
1185 #endif
1186 				}
1187 				RIOFoadWakeup(p);
1188 				p->RIONumBootPkts = 0;
1189 				p->RIOBooting = 0;
1190 
1191 #ifdef RINGBUFFER_SUPPORT
1192 				for( loop=0; loop<RIO_PORTS; loop++ )
1193 					if ( p->RIOPortp[loop]->TxRingBuffer )
1194 						sysfree((void *)p->RIOPortp[loop]->TxRingBuffer,
1195 							RIOBufferSize );
1196 #endif
1197 #if 0
1198 				bzero((caddr_t)&p->RIOPortp[0],RIO_PORTS*sizeof(struct Port));
1199 #else
1200 				printk ("HEEEEELP!\n");
1201 #endif
1202 
1203 				for( loop=0; loop<RIO_PORTS; loop++ ) {
1204 #if 0
1205 					p->RIOPortp[loop]->TtyP = &p->channel[loop];
1206 #endif
1207 
1208 					p->RIOPortp[loop]->portSem = SPIN_LOCK_UNLOCKED;
1209 					p->RIOPortp[loop]->InUse = NOT_INUSE;
1210 				}
1211 
1212 				p->RIOSystemUp = 0;
1213 				return retval;
1214 
1215 			case RIO_DOWNLOAD:
1216 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_DOWNLOAD\n");
1217 				if ( !su ) {
1218 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_DOWNLOAD: Not super user\n");
1219 					 p->RIOError.Error = NOT_SUPER_USER;
1220 					 return EPERM;
1221 				}
1222 				if ( copyin((int)arg, (caddr_t)&DownLoad,
1223 							sizeof(DownLoad) )==COPYFAIL ) {
1224 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_DOWNLOAD: Copy in from user space failed\n");
1225 					 p->RIOError.Error = COPYIN_FAILED;
1226 					 return EFAULT;
1227 				}
1228 				rio_dprintk (RIO_DEBUG_CTRL, "Copied in download code for product code 0x%x\n",
1229 				    DownLoad.ProductCode);
1230 
1231 				/*
1232 				** It is important that the product code is an unsigned object!
1233 				*/
1234 				if ( DownLoad.ProductCode > MAX_PRODUCT ) {
1235 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_DOWNLOAD: Bad product code %d passed\n",
1236 							DownLoad.ProductCode);
1237 					 p->RIOError.Error = NO_SUCH_PRODUCT;
1238 					 return ENXIO;
1239 				}
1240 				/*
1241 				** do something!
1242 				*/
1243 				retval = (*(RIOBootTable[DownLoad.ProductCode]))(p, &DownLoad);
1244 										/* <-- Panic */
1245 				p->RIOHalted = 0;
1246 				/*
1247 				** and go back, content with a job well completed.
1248 				*/
1249 				return retval;
1250 
1251 			case RIO_PARMS:
1252 				{
1253 					uint host;
1254 
1255 					if (copyin((int)arg, (caddr_t)&host,
1256 							sizeof(host) ) == COPYFAIL ) {
1257 						rio_dprintk (RIO_DEBUG_CTRL,
1258 							"RIO_HOST_REQ: Copy in from user space failed\n");
1259 						p->RIOError.Error = COPYIN_FAILED;
1260 						return EFAULT;
1261 					}
1262 					/*
1263 					** Fetch the parmmap
1264 					*/
1265 					rio_dprintk (RIO_DEBUG_CTRL, "RIO_PARMS\n");
1266 					if ( copyout( (caddr_t)p->RIOHosts[host].ParmMapP,
1267 								(int)arg, sizeof(PARM_MAP) )==COPYFAIL ) {
1268 						p->RIOError.Error = COPYOUT_FAILED;
1269 						rio_dprintk (RIO_DEBUG_CTRL, "RIO_PARMS: Copy out to user space failed\n");
1270 						return EFAULT;
1271 					}
1272 				}
1273 				return retval;
1274 
1275 			case RIO_HOST_REQ:
1276 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_REQ\n");
1277 				if (copyin((int)arg, (caddr_t)&HostReq,
1278 							sizeof(HostReq) )==COPYFAIL ) {
1279 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_REQ: Copy in from user space failed\n");
1280 					 p->RIOError.Error = COPYIN_FAILED;
1281 					 return EFAULT;
1282 				}
1283 				if ( HostReq.HostNum >= p->RIONumHosts ) {
1284 					 p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
1285 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_REQ: Illegal host number %d\n",
1286 							HostReq.HostNum);
1287 					 return ENXIO;
1288 				}
1289 				rio_dprintk (RIO_DEBUG_CTRL, "Request for host %d\n", HostReq.HostNum);
1290 
1291 				if (copyout((caddr_t)&p->RIOHosts[HostReq.HostNum],
1292 					(int)HostReq.HostP,sizeof(struct Host) ) == COPYFAIL) {
1293 					p->RIOError.Error = COPYOUT_FAILED;
1294 					rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_REQ: Bad copy to user space\n");
1295 					return EFAULT;
1296 				}
1297 				return retval;
1298 
1299 			 case RIO_HOST_DPRAM:
1300 				rio_dprintk (RIO_DEBUG_CTRL, "Request for DPRAM\n");
1301 				if ( copyin( (int)arg, (caddr_t)&HostDpRam,
1302 								sizeof(HostDpRam) )==COPYFAIL ) {
1303 					rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_DPRAM: Copy in from user space failed\n");
1304 					p->RIOError.Error = COPYIN_FAILED;
1305 					return EFAULT;
1306 				}
1307 				if ( HostDpRam.HostNum >= p->RIONumHosts ) {
1308 					p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
1309 					rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_DPRAM: Illegal host number %d\n",
1310 										HostDpRam.HostNum);
1311 					return ENXIO;
1312 				}
1313 				rio_dprintk (RIO_DEBUG_CTRL, "Request for host %d\n", HostDpRam.HostNum);
1314 
1315 				if (p->RIOHosts[HostDpRam.HostNum].Type == RIO_PCI) {
1316 					 int off;
1317 					 /* It's hardware like this that really gets on my tits. */
1318 					 static unsigned char copy[sizeof(struct DpRam)];
1319 					for ( off=0; off<sizeof(struct DpRam); off++ )
1320 						copy[off] = p->RIOHosts[HostDpRam.HostNum].Caddr[off];
1321 					if ( copyout( (caddr_t)copy, (int)HostDpRam.DpRamP,
1322 							sizeof(struct DpRam) ) == COPYFAIL ) {
1323 						p->RIOError.Error = COPYOUT_FAILED;
1324 						rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_DPRAM: Bad copy to user space\n");
1325 						return EFAULT;
1326 					}
1327 				}
1328 				else if (copyout((caddr_t)p->RIOHosts[HostDpRam.HostNum].Caddr,
1329 					(int)HostDpRam.DpRamP,
1330 						sizeof(struct DpRam) ) == COPYFAIL ) {
1331 					 p->RIOError.Error = COPYOUT_FAILED;
1332 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_DPRAM: Bad copy to user space\n");
1333 					 return EFAULT;
1334 				}
1335 				return retval;
1336 
1337 			 case RIO_SET_BUSY:
1338 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_SET_BUSY\n");
1339 				if ( (int)arg < 0 || (int)arg > 511 ) {
1340 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_SET_BUSY: Bad port number %d\n",(int)arg);
1341 					 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
1342 					 return EINVAL;
1343 				}
1344 				rio_spin_lock_irqsave(&PortP->portSem, flags);
1345 				p->RIOPortp[(int)arg]->State |= RIO_BUSY;
1346 				rio_spin_unlock_irqrestore( &PortP->portSem , flags);
1347 				return retval;
1348 
1349 			 case RIO_HOST_PORT:
1350 				/*
1351 				** The daemon want port information
1352 				** (probably for debug reasons)
1353 				*/
1354 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_PORT\n");
1355 				if ( copyin((int)arg, (caddr_t)&PortReq,
1356 					sizeof(PortReq) )==COPYFAIL ) {
1357 					rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_PORT: Copy in from user space failed\n");
1358 					p->RIOError.Error = COPYIN_FAILED;
1359 					return EFAULT;
1360 				}
1361 
1362 				if (PortReq.SysPort >= RIO_PORTS) { /* SysPort is unsigned */
1363 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_PORT: Illegal port number %d\n",
1364 											PortReq.SysPort);
1365 					 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
1366 					 return ENXIO;
1367 				}
1368 				rio_dprintk (RIO_DEBUG_CTRL, "Request for port %d\n", PortReq.SysPort);
1369 				if (copyout((caddr_t)p->RIOPortp[PortReq.SysPort],
1370 							 (int)PortReq.PortP,
1371 								sizeof(struct Port) ) == COPYFAIL) {
1372 					 p->RIOError.Error = COPYOUT_FAILED;
1373 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_PORT: Bad copy to user space\n");
1374 					 return EFAULT;
1375 				}
1376 				return retval;
1377 
1378 			case RIO_HOST_RUP:
1379 				/*
1380 				** The daemon want rup information
1381 				** (probably for debug reasons)
1382 				*/
1383 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_RUP\n");
1384 				if (copyin((int)arg, (caddr_t)&RupReq,
1385 						sizeof(RupReq) )==COPYFAIL ) {
1386 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_RUP: Copy in from user space failed\n");
1387 					 p->RIOError.Error = COPYIN_FAILED;
1388 					 return EFAULT;
1389 				}
1390 				if (RupReq.HostNum >= p->RIONumHosts) { /* host is unsigned */
1391 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_RUP: Illegal host number %d\n",
1392 								RupReq.HostNum);
1393 					 p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
1394 					 return ENXIO;
1395 				}
1396 				if ( RupReq.RupNum >= MAX_RUP+LINKS_PER_UNIT ) { /* eek! */
1397 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_RUP: Illegal rup number %d\n",
1398 							RupReq.RupNum);
1399 					 p->RIOError.Error = RUP_NUMBER_OUT_OF_RANGE;
1400 					 return EINVAL;
1401 				}
1402 				HostP = &p->RIOHosts[RupReq.HostNum];
1403 
1404 				if ((HostP->Flags & RUN_STATE) != RC_RUNNING) {
1405 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_RUP: Host %d not running\n",
1406 							RupReq.HostNum);
1407 					 p->RIOError.Error = HOST_NOT_RUNNING;
1408 					 return EIO;
1409 				}
1410 				rio_dprintk (RIO_DEBUG_CTRL, "Request for rup %d from host %d\n",
1411 						RupReq.RupNum,RupReq.HostNum);
1412 
1413 				if (copyout((caddr_t)HostP->UnixRups[RupReq.RupNum].RupP,
1414 					(int)RupReq.RupP,sizeof(struct RUP) ) == COPYFAIL) {
1415 					p->RIOError.Error = COPYOUT_FAILED;
1416 					rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_RUP: Bad copy to user space\n");
1417 					return EFAULT;
1418 				}
1419 				return retval;
1420 
1421 			case RIO_HOST_LPB:
1422 				/*
1423 				** The daemon want lpb information
1424 				** (probably for debug reasons)
1425 				*/
1426 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_LPB\n");
1427 				if (copyin((int)arg, (caddr_t)&LpbReq,
1428 					sizeof(LpbReq) )==COPYFAIL ) {
1429 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_LPB: Bad copy from user space\n");
1430 					 p->RIOError.Error = COPYIN_FAILED;
1431 					 return EFAULT;
1432 				}
1433 				if (LpbReq.Host >= p->RIONumHosts) { /* host is unsigned */
1434 					rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_LPB: Illegal host number %d\n",
1435 							LpbReq.Host);
1436 					p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
1437 					return ENXIO;
1438 				}
1439 				if ( LpbReq.Link >= LINKS_PER_UNIT ) { /* eek! */
1440 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_LPB: Illegal link number %d\n",
1441 							LpbReq.Link);
1442 					 p->RIOError.Error = LINK_NUMBER_OUT_OF_RANGE;
1443 					 return EINVAL;
1444 				}
1445 				HostP = &p->RIOHosts[LpbReq.Host];
1446 
1447 				if ( (HostP->Flags & RUN_STATE) != RC_RUNNING ) {
1448 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_LPB: Host %d not running\n",
1449 						LpbReq.Host );
1450 					 p->RIOError.Error = HOST_NOT_RUNNING;
1451 					 return EIO;
1452 				}
1453 				rio_dprintk (RIO_DEBUG_CTRL, "Request for lpb %d from host %d\n",
1454 					LpbReq.Link, LpbReq.Host);
1455 
1456 				if (copyout((caddr_t)&HostP->LinkStrP[LpbReq.Link],
1457 					(int)LpbReq.LpbP,sizeof(struct LPB) ) == COPYFAIL) {
1458 					rio_dprintk (RIO_DEBUG_CTRL, "RIO_HOST_LPB: Bad copy to user space\n");
1459 					p->RIOError.Error = COPYOUT_FAILED;
1460 					return EFAULT;
1461 				}
1462 				return retval;
1463 
1464 				/*
1465 				** Here 3 IOCTL's that allow us to change the way in which
1466 				** rio logs errors. send them just to syslog or send them
1467 				** to both syslog and console or send them to just the console.
1468 				**
1469 				** See RioStrBuf() in util.c for the other half.
1470 				*/
1471 			case RIO_SYSLOG_ONLY:
1472 				p->RIOPrintLogState = PRINT_TO_LOG;	/* Just syslog */
1473 				return 0;
1474 
1475 			case RIO_SYSLOG_CONS:
1476 				p->RIOPrintLogState = PRINT_TO_LOG_CONS;/* syslog and console */
1477 				return 0;
1478 
1479 			case RIO_CONS_ONLY:
1480 				p->RIOPrintLogState = PRINT_TO_CONS;	/* Just console */
1481 				return 0;
1482 
1483 			case RIO_SIGNALS_ON:
1484 				if ( p->RIOSignalProcess ) {
1485 					 p->RIOError.Error = SIGNALS_ALREADY_SET;
1486 					 return EBUSY;
1487 				}
1488 				p->RIOSignalProcess = getpid();
1489 				p->RIOPrintDisabled = DONT_PRINT;
1490 				return retval;
1491 
1492 			case RIO_SIGNALS_OFF:
1493 				if ( p->RIOSignalProcess != getpid() ) {
1494 					 p->RIOError.Error = NOT_RECEIVING_PROCESS;
1495 					 return EPERM;
1496 				}
1497 				rio_dprintk (RIO_DEBUG_CTRL, "Clear signal process to zero\n");
1498 				p->RIOSignalProcess = 0;
1499 				return retval;
1500 
1501 			case RIO_SET_BYTE_MODE:
1502 				for ( Host=0; Host<p->RIONumHosts; Host++ )
1503 					 if ( p->RIOHosts[Host].Type == RIO_AT )
1504 						 p->RIOHosts[Host].Mode &= ~WORD_OPERATION;
1505 				return retval;
1506 
1507 			case RIO_SET_WORD_MODE:
1508 				for ( Host=0; Host<p->RIONumHosts; Host++ )
1509 					 if ( p->RIOHosts[Host].Type == RIO_AT )
1510 						 p->RIOHosts[Host].Mode |= WORD_OPERATION;
1511 				return retval;
1512 
1513 			case RIO_SET_FAST_BUS:
1514 				for ( Host=0; Host<p->RIONumHosts; Host++ )
1515 					 if ( p->RIOHosts[Host].Type == RIO_AT )
1516 						 p->RIOHosts[Host].Mode |= FAST_AT_BUS;
1517 				return retval;
1518 
1519 			case RIO_SET_SLOW_BUS:
1520 				for ( Host=0; Host<p->RIONumHosts; Host++ )
1521 					 if ( p->RIOHosts[Host].Type == RIO_AT )
1522 						 p->RIOHosts[Host].Mode &= ~FAST_AT_BUS;
1523 				return retval;
1524 
1525 			case RIO_MAP_B50_TO_50:
1526 			case RIO_MAP_B50_TO_57600:
1527 			case RIO_MAP_B110_TO_110:
1528 			case RIO_MAP_B110_TO_115200:
1529 				rio_dprintk (RIO_DEBUG_CTRL, "Baud rate mapping\n");
1530 				port = (uint) arg;
1531 				if ( port < 0 || port > 511 ) {
1532 					 rio_dprintk (RIO_DEBUG_CTRL, "Baud rate mapping: Bad port number %d\n", port);
1533 					 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
1534 					 return EINVAL;
1535 				}
1536 				rio_spin_lock_irqsave(&PortP->portSem, flags);
1537 				switch( cmd )
1538 				{
1539 					case RIO_MAP_B50_TO_50 :
1540 						p->RIOPortp[port]->Config |= RIO_MAP_50_TO_50;
1541 						break;
1542 					case RIO_MAP_B50_TO_57600 :
1543 						p->RIOPortp[port]->Config &= ~RIO_MAP_50_TO_50;
1544 						break;
1545 					case RIO_MAP_B110_TO_110 :
1546 						p->RIOPortp[port]->Config |= RIO_MAP_110_TO_110;
1547 						break;
1548 					case RIO_MAP_B110_TO_115200 :
1549 						p->RIOPortp[port]->Config &= ~RIO_MAP_110_TO_110;
1550 						break;
1551 				}
1552 				rio_spin_unlock_irqrestore( &PortP->portSem , flags);
1553 				return retval;
1554 
1555 			case RIO_STREAM_INFO:
1556 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_STREAM_INFO\n");
1557 				return EINVAL;
1558 
1559 			case RIO_SEND_PACKET:
1560 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_SEND_PACKET\n");
1561 				if ( copyin( (int)arg, (caddr_t)&SendPack,
1562 									sizeof(SendPack) )==COPYFAIL ) {
1563 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_SEND_PACKET: Bad copy from user space\n");
1564 					 p->RIOError.Error = COPYIN_FAILED;
1565 					 return EFAULT;
1566 				}
1567 				if ( SendPack.PortNum >= 128 ) {
1568 					 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
1569 					 return ENXIO;
1570 				}
1571 
1572 				PortP = p->RIOPortp[SendPack.PortNum];
1573 				rio_spin_lock_irqsave(&PortP->portSem, flags);
1574 
1575 				if ( !can_add_transmit(&PacketP,PortP) ) {
1576 					 p->RIOError.Error = UNIT_IS_IN_USE;
1577 					 rio_spin_unlock_irqrestore( &PortP->portSem , flags);
1578 					 return ENOSPC;
1579 				}
1580 
1581 				for ( loop=0; loop<(ushort)(SendPack.Len & 127); loop++ )
1582 					 WBYTE(PacketP->data[loop], SendPack.Data[loop] );
1583 
1584 				WBYTE(PacketP->len, SendPack.Len);
1585 
1586 				add_transmit( PortP );
1587 				/*
1588 				** Count characters transmitted for port statistics reporting
1589 				*/
1590 				if (PortP->statsGather)
1591 					 PortP->txchars += (SendPack.Len & 127);
1592 				rio_spin_unlock_irqrestore( &PortP->portSem , flags);
1593 				return retval;
1594 
1595 			case RIO_NO_MESG:
1596 				if ( su )
1597 					 p->RIONoMessage = 1;
1598 				return su ? 0 : EPERM;
1599 
1600 			case RIO_MESG:
1601 				if ( su )
1602 					p->RIONoMessage = 0;
1603 				return su ? 0 : EPERM;
1604 
1605 			case RIO_WHAT_MESG:
1606 				if ( copyout( (caddr_t)&p->RIONoMessage, (int)arg,
1607 					sizeof(p->RIONoMessage) )==COPYFAIL ) {
1608 					rio_dprintk (RIO_DEBUG_CTRL, "RIO_WHAT_MESG: Bad copy to user space\n");
1609 					p->RIOError.Error = COPYOUT_FAILED;
1610 					return EFAULT;
1611 				}
1612 				return 0;
1613 
1614 			case RIO_MEM_DUMP :
1615 				if (copyin((int)arg, (caddr_t)&SubCmd,
1616 						sizeof(struct SubCmdStruct)) == COPYFAIL) {
1617 					 p->RIOError.Error = COPYIN_FAILED;
1618 					 return EFAULT;
1619 				}
1620 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_MEM_DUMP host %d rup %d addr %x\n",
1621 						SubCmd.Host, SubCmd.Rup, SubCmd.Addr);
1622 
1623 				if (SubCmd.Rup >= MAX_RUP+LINKS_PER_UNIT ) {
1624 					 p->RIOError.Error = RUP_NUMBER_OUT_OF_RANGE;
1625 					 return EINVAL;
1626 				}
1627 
1628 				if (SubCmd.Host >= p->RIONumHosts ) {
1629 					 p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
1630 					 return EINVAL;
1631 				}
1632 
1633 				port = p->RIOHosts[SubCmd.Host].
1634 								UnixRups[SubCmd.Rup].BaseSysPort;
1635 
1636 				PortP = p->RIOPortp[port];
1637 
1638 				rio_spin_lock_irqsave(&PortP->portSem, flags);
1639 
1640 				if ( RIOPreemptiveCmd(p,  PortP, MEMDUMP ) == RIO_FAIL ) {
1641 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_MEM_DUMP failed\n");
1642 					 rio_spin_unlock_irqrestore( &PortP->portSem , flags);
1643 					 return EBUSY;
1644 				}
1645 				else
1646 					 PortP->State |= RIO_BUSY;
1647 
1648 				rio_spin_unlock_irqrestore( &PortP->portSem , flags);
1649 				if ( copyout( (caddr_t)p->RIOMemDump, (int)arg,
1650 							MEMDUMP_SIZE) == COPYFAIL ) {
1651 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_MEM_DUMP copy failed\n");
1652 					 p->RIOError.Error = COPYOUT_FAILED;
1653 					 return EFAULT;
1654 				}
1655 				return 0;
1656 
1657 			case RIO_TICK:
1658 				if ((int)arg < 0 || (int)arg >= p->RIONumHosts)
1659 					 return EINVAL;
1660 				rio_dprintk (RIO_DEBUG_CTRL, "Set interrupt for host %d\n", (int)arg);
1661 				WBYTE(p->RIOHosts[(int)arg].SetInt , 0xff);
1662 				return 0;
1663 
1664 			case RIO_TOCK:
1665 				if ((int)arg < 0 || (int)arg >= p->RIONumHosts)
1666 					 return EINVAL;
1667 				rio_dprintk (RIO_DEBUG_CTRL, "Clear interrupt for host %d\n", (int)arg);
1668 				WBYTE((p->RIOHosts[(int)arg].ResetInt) , 0xff);
1669 				return 0;
1670 
1671 			case RIO_READ_CHECK:
1672 				/* Check reads for pkts with data[0] the same */
1673 				p->RIOReadCheck = !p->RIOReadCheck;
1674 				if (copyout((caddr_t)&p->RIOReadCheck,(int)arg,
1675 							sizeof(uint))== COPYFAIL) {
1676 					 p->RIOError.Error = COPYOUT_FAILED;
1677 					 return EFAULT;
1678 				}
1679 				return 0;
1680 
1681 			case RIO_READ_REGISTER :
1682 				if (copyin((int)arg, (caddr_t)&SubCmd,
1683 							sizeof(struct SubCmdStruct)) == COPYFAIL) {
1684 					 p->RIOError.Error = COPYIN_FAILED;
1685 					 return EFAULT;
1686 				}
1687 				rio_dprintk (RIO_DEBUG_CTRL, "RIO_READ_REGISTER host %d rup %d port %d reg %x\n",
1688 						SubCmd.Host, SubCmd.Rup, SubCmd.Port, SubCmd.Addr);
1689 
1690 				if (SubCmd.Port > 511) {
1691 					 rio_dprintk (RIO_DEBUG_CTRL, "Baud rate mapping: Bad port number %d\n",
1692 								SubCmd.Port);
1693 					 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
1694 					 return EINVAL;
1695 				}
1696 
1697 				if (SubCmd.Rup >= MAX_RUP+LINKS_PER_UNIT ) {
1698 					 p->RIOError.Error = RUP_NUMBER_OUT_OF_RANGE;
1699 					 return EINVAL;
1700 				}
1701 
1702 				if (SubCmd.Host >= p->RIONumHosts ) {
1703 					 p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
1704 					 return EINVAL;
1705 				}
1706 
1707 				port = p->RIOHosts[SubCmd.Host].
1708 						UnixRups[SubCmd.Rup].BaseSysPort + SubCmd.Port;
1709 				PortP = p->RIOPortp[port];
1710 
1711 				rio_spin_lock_irqsave(&PortP->portSem, flags);
1712 
1713 				if (RIOPreemptiveCmd(p, PortP, READ_REGISTER) == RIO_FAIL) {
1714 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_READ_REGISTER failed\n");
1715 					 rio_spin_unlock_irqrestore( &PortP->portSem , flags);
1716 					 return EBUSY;
1717 				}
1718 				else
1719 					 PortP->State |= RIO_BUSY;
1720 
1721 				rio_spin_unlock_irqrestore( &PortP->portSem , flags);
1722 				if (copyout((caddr_t)&p->CdRegister, (int)arg,
1723 							sizeof(uint)) == COPYFAIL ) {
1724 					 rio_dprintk (RIO_DEBUG_CTRL, "RIO_READ_REGISTER copy failed\n");
1725 					 p->RIOError.Error = COPYOUT_FAILED;
1726 					 return EFAULT;
1727 				}
1728 				return 0;
1729 				/*
1730 				** rio_make_dev: given port number (0-511) ORed with port type
1731 				** (RIO_DEV_DIRECT, RIO_DEV_MODEM, RIO_DEV_XPRINT) return dev_t
1732 				** value to pass to mknod to create the correct device node.
1733 				*/
1734 			case RIO_MAKE_DEV:
1735 				{
1736 					uint port = (uint)arg & RIO_MODEM_MASK;
1737 
1738 					switch ( (uint)arg & RIO_DEV_MASK ) {
1739 						case RIO_DEV_DIRECT:
1740 							arg = (caddr_t)drv_makedev(major(dev), port);
1741 							rio_dprintk (RIO_DEBUG_CTRL, "Makedev direct 0x%x is 0x%x\n",port, (int)arg);
1742 							return (int)arg;
1743 					 	case RIO_DEV_MODEM:
1744 							arg =  (caddr_t)drv_makedev(major(dev), (port|RIO_MODEM_BIT) );
1745 							rio_dprintk (RIO_DEBUG_CTRL, "Makedev modem 0x%x is 0x%x\n",port, (int)arg);
1746 							return (int)arg;
1747 						case RIO_DEV_XPRINT:
1748 							arg = (caddr_t)drv_makedev(major(dev), port);
1749 							rio_dprintk (RIO_DEBUG_CTRL, "Makedev printer 0x%x is 0x%x\n",port, (int)arg);
1750 							return (int)arg;
1751 					}
1752 					rio_dprintk (RIO_DEBUG_CTRL, "MAKE Device is called\n");
1753 					return EINVAL;
1754 				}
1755 				/*
1756 				** rio_minor: given a dev_t from a stat() call, return
1757 				** the port number (0-511) ORed with the port type
1758 				** ( RIO_DEV_DIRECT, RIO_DEV_MODEM, RIO_DEV_XPRINT )
1759 				*/
1760 			case RIO_MINOR:
1761 				{
1762 					dev_t dv;
1763 					int mino;
1764 
1765 					dv = (dev_t)((int)arg);
1766 					mino = RIO_UNMODEM(dv);
1767 
1768 					if ( RIO_ISMODEM(dv) ) {
1769 						rio_dprintk (RIO_DEBUG_CTRL, "Minor for device 0x%x: modem %d\n", dv, mino);
1770 						arg = (caddr_t)(mino | RIO_DEV_MODEM);
1771 					}
1772 					else {
1773 						rio_dprintk (RIO_DEBUG_CTRL, "Minor for device 0x%x: direct %d\n", dv, mino);
1774 						arg = (caddr_t)(mino | RIO_DEV_DIRECT);
1775 					}
1776 					return (int)arg;
1777 				}
1778 	}
1779 	rio_dprintk (RIO_DEBUG_CTRL, "INVALID DAEMON IOCTL 0x%x\n",cmd);
1780 	p->RIOError.Error = IOCTL_COMMAND_UNKNOWN;
1781 
1782 	func_exit ();
1783 	return EINVAL;
1784 }
1785 
1786 /*
1787 ** Pre-emptive commands go on RUPs and are only one byte long.
1788 */
1789 int
RIOPreemptiveCmd(p,PortP,Cmd)1790 RIOPreemptiveCmd(p, PortP, Cmd)
1791 struct rio_info *	p;
1792 struct Port *PortP;
1793 uchar Cmd;
1794 {
1795 	struct CmdBlk *CmdBlkP;
1796 	struct PktCmd_M *PktCmdP;
1797 	int Ret;
1798 	ushort rup;
1799 	int port;
1800 
1801 #ifdef CHECK
1802 	CheckPortP( PortP );
1803 #endif
1804 
1805 	if ( PortP->State & RIO_DELETED ) {
1806 		rio_dprintk (RIO_DEBUG_CTRL, "Preemptive command to deleted RTA ignored\n");
1807 		return RIO_FAIL;
1808 	}
1809 
1810 	if (((int)((char)PortP->InUse) == -1) || ! (CmdBlkP = RIOGetCmdBlk()) ) {
1811 		rio_dprintk (RIO_DEBUG_CTRL, "Cannot allocate command block for command %d on port %d\n",
1812 		       Cmd, PortP->PortNum);
1813 		return RIO_FAIL;
1814 	}
1815 
1816 	rio_dprintk (RIO_DEBUG_CTRL, "Command blk 0x%x - InUse now %d\n",
1817 	       (int)CmdBlkP,PortP->InUse);
1818 
1819 	PktCmdP = (struct PktCmd_M *)&CmdBlkP->Packet.data[0];
1820 
1821 	CmdBlkP->Packet.src_unit  = 0;
1822 	if (PortP->SecondBlock)
1823 		rup = PortP->ID2;
1824 	else
1825 		rup = PortP->RupNum;
1826 	CmdBlkP->Packet.dest_unit = rup;
1827 	CmdBlkP->Packet.src_port  = COMMAND_RUP;
1828 	CmdBlkP->Packet.dest_port = COMMAND_RUP;
1829 	CmdBlkP->Packet.len	  = PKT_CMD_BIT | 2;
1830 	CmdBlkP->PostFuncP	= RIOUnUse;
1831 	CmdBlkP->PostArg	= (int)PortP;
1832 	PktCmdP->Command	= Cmd;
1833 	port				= PortP->HostPort % (ushort)PORTS_PER_RTA;
1834 	/*
1835 	** Index ports 8-15 for 2nd block of 16 port RTA.
1836 	*/
1837 	if (PortP->SecondBlock)
1838 		port += (ushort) PORTS_PER_RTA;
1839 	PktCmdP->PhbNum	   = port;
1840 
1841 	switch ( Cmd ) {
1842 		case MEMDUMP:
1843 			rio_dprintk (RIO_DEBUG_CTRL, "Queue MEMDUMP command blk 0x%x (addr 0x%x)\n",
1844 			       (int)CmdBlkP, (int)SubCmd.Addr);
1845 			PktCmdP->SubCommand		= MEMDUMP;
1846 			PktCmdP->SubAddr		= SubCmd.Addr;
1847 			break;
1848 		case FCLOSE:
1849 			rio_dprintk (RIO_DEBUG_CTRL, "Queue FCLOSE command blk 0x%x\n",(int)CmdBlkP);
1850 			break;
1851 		case READ_REGISTER:
1852 			rio_dprintk (RIO_DEBUG_CTRL, "Queue READ_REGISTER (0x%x) command blk 0x%x\n",
1853 		 		(int)SubCmd.Addr, (int)CmdBlkP);
1854 			PktCmdP->SubCommand		= READ_REGISTER;
1855 			PktCmdP->SubAddr		= SubCmd.Addr;
1856 			break;
1857 		case RESUME:
1858 			rio_dprintk (RIO_DEBUG_CTRL, "Queue RESUME command blk 0x%x\n",(int)CmdBlkP);
1859 			break;
1860 		case RFLUSH:
1861 			rio_dprintk (RIO_DEBUG_CTRL, "Queue RFLUSH command blk 0x%x\n",(int)CmdBlkP);
1862 			CmdBlkP->PostFuncP = RIORFlushEnable;
1863 			break;
1864 		case SUSPEND:
1865 			rio_dprintk (RIO_DEBUG_CTRL, "Queue SUSPEND command blk 0x%x\n",(int)CmdBlkP);
1866 			break;
1867 
1868 		case MGET :
1869 			rio_dprintk (RIO_DEBUG_CTRL, "Queue MGET command blk 0x%x\n", (int)CmdBlkP);
1870 			break;
1871 
1872 		case MSET :
1873 		case MBIC :
1874 		case MBIS :
1875 			CmdBlkP->Packet.data[4] = (char) PortP->ModemLines;
1876 			rio_dprintk (RIO_DEBUG_CTRL, "Queue MSET/MBIC/MBIS command blk 0x%x\n", (int)CmdBlkP);
1877 			break;
1878 
1879 		case WFLUSH:
1880 			/*
1881 			** If we have queued up the maximum number of Write flushes
1882 			** allowed then we should not bother sending any more to the
1883 			** RTA.
1884 			*/
1885 			if ((int)((char)PortP->WflushFlag) == (int)-1) {
1886 				rio_dprintk (RIO_DEBUG_CTRL, "Trashed WFLUSH, WflushFlag about to wrap!");
1887 				RIOFreeCmdBlk(CmdBlkP);
1888 				return(RIO_FAIL);
1889 			} else {
1890 				rio_dprintk (RIO_DEBUG_CTRL, "Queue WFLUSH command blk 0x%x\n",
1891 				       (int)CmdBlkP);
1892 				CmdBlkP->PostFuncP = RIOWFlushMark;
1893 			}
1894 			break;
1895 	}
1896 
1897 	PortP->InUse++;
1898 
1899 	Ret = RIOQueueCmdBlk( PortP->HostP, rup, CmdBlkP );
1900 
1901 	return Ret;
1902 }
1903