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