1/*
2 * Copyright 1990, 1991 by Thomas Roell, Dinkelscherben, Germany
3 * Copyright 1992 by David Dawes <dawes@XFree86.org>
4 * Copyright 1992 by Jim Tsillas <jtsilla@damon.ccs.northeastern.edu>
5 * Copyright 1992 by Rich Murphey <Rich@Rice.edu>
6 * Copyright 1992 by Robert Baron <Robert.Baron@ernst.mach.cs.cmu.edu>
7 * Copyright 1992 by Orest Zborowski <obz@eskimo.com>
8 * Copyright 1993 by Vrije Universiteit, The Netherlands
9 * Copyright 1993 by David Wexelblat <dwex@XFree86.org>
10 * Copyright 1994, 1996 by Holger Veit <Holger.Veit@gmd.de>
11 * Copyright 1997 by Takis Psarogiannakopoulos <takis@dpmms.cam.ac.uk>
12 * Copyright 1994-2003 by The XFree86 Project, Inc
13 * Copyright 1999 by David Holland <davidh@iquest.net>
14 * Copyright 2015 by VMware Inc.
15 *
16 * Permission to use, copy, modify, distribute, and sell this software and its
17 * documentation for any purpose is hereby granted without fee, provided that
18 * the above copyright notice appear in all copies and that both that
19 * copyright notice and this permission notice appear in supporting
20 * documentation, and that the names of the above listed copyright holders
21 * not be used in advertising or publicity pertaining to distribution of
22 * the software without specific, written prior permission.  The above listed
23 * copyright holders make no representations about the suitability of this
24 * software for any purpose.  It is provided "as is" without express or
25 * implied warranty.
26 *
27 * THE ABOVE LISTED COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD
28 * TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS, IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDERS BE
30 * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
31 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
32 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
33 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 *
35 */
36#ifdef HAVE_CONFIG_H
37#include "config.h"
38#endif
39#ifdef HAVE_XORG_CONFIG_H
40#include <xorg-config.h>
41#endif
42
43#include <X11/Xdefs.h>
44#include <stdbool.h>
45
46#if defined(VMMOUSE_OS_BSD)
47#include <sys/types.h>
48#if defined(USE_I386_IOPL) || defined(USE_AMD64_IOPL) || defined(USE_X86_64_IOPL)
49#include <machine/sysarch.h>
50#if defined(USE_I386_IOPL)
51#define IOPL_NAME i386_iopl
52#elif defined(USE_AMD64_IOPL)
53#define IOPL_NAME amd64_iopl
54#elif defined(USE_X86_64_IOPL)
55#define IOPL_NAME x86_64_iopl
56#endif
57/***************************************************************************/
58/* I/O Permissions section                                                 */
59/***************************************************************************/
60static Bool ExtendedEnabled = false;
61
62Bool
63xf86EnableIO()
64{
65    if (ExtendedEnabled)
66	return false;
67
68    if (IOPL_NAME(1) < 0)
69	return false;
70
71    ExtendedEnabled = true;
72    return true;
73}
74
75void
76xf86DisableIO()
77{
78    if (!ExtendedEnabled)
79	return;
80
81    IOPL_NAME(0);
82
83    ExtendedEnabled = false;
84    return;
85}
86
87#endif /* defined(USE_I386_IOPL) || defined(USE_AMD64_IOPL) || defined(USE_X86_64_IOPL) */
88
89#ifdef USE_DEV_IO
90#include <sys/stat.h>
91#include <fcntl.h>
92#include <unistd.h>
93static int IoFd = -1;
94
95Bool
96xf86EnableIO()
97{
98    if (IoFd >= 0)
99	return true;
100
101    if ((IoFd = open("/dev/io", O_RDWR)) == -1)
102	return false;
103
104    return true;
105}
106
107void
108xf86DisableIO()
109{
110    if (IoFd < 0)
111	return;
112
113    close(IoFd);
114    IoFd = -1;
115    return;
116}
117#endif
118
119#elif defined(VMMOUSE_OS_GENERIC)
120
121static Bool ExtendedEnabled = false;
122
123extern int ioperm(unsigned long __from, unsigned long __num, int __turn_on);
124extern int iopl(int __level);
125
126Bool xf86EnableIO(void)
127{
128    if (ExtendedEnabled)
129	return true;
130
131    if (ioperm(0, 1024, 1) || iopl(3))
132	return false;
133
134    ExtendedEnabled = true;
135    return true;
136}
137
138void
139xf86DisableIO(void)
140{
141    if (!ExtendedEnabled)
142	return;
143
144    iopl(0);
145    ioperm(0, 1024, 0);
146    ExtendedEnabled = false;
147
148    return;
149}
150
151#elif defined(VMMOUSE_OS_SOLARIS)
152
153#ifdef __GNUC__
154#if defined(__sun) && !defined(sun)
155#define sun 1
156#endif
157#if defined(__SVR4) && !defined(SVR4)
158#define SVR4 1
159#endif
160#endif
161/*
162 * The below sequence of includes is stolen from Xserver. If it doesn't work
163 * for your setup, please propose a patch to fix it.
164 */
165#include <sys/types.h>
166#include <errno.h>
167#if !(defined (sun) && defined (SVR4))
168#include <sys/immu.h>
169#include <sys/region.h>
170#include <sys/proc.h>
171#endif
172#include <sys/tss.h>
173#include <sys/sysi86.h>
174#if defined(SVR4) && !defined(sun)
175#include <sys/seg.h>
176#endif                          /* SVR4 && !sun */
177/* V86SC_IOPL was moved to <sys/sysi86.h> on Solaris 7 and later */
178#if !defined(V86SC_IOPL)        /* Solaris 7 or later? */
179#include <sys/v86.h>            /* Nope */
180#endif
181#if defined(sun) && (defined (__i386__) || defined(__i386) || defined(__x86))  && defined (SVR4)
182#include <sys/psw.h>
183#endif
184
185static Bool ExtendedEnabled = false;
186
187Bool
188xf86EnableIO(void)
189{
190    if (ExtendedEnabled)
191	return true;
192
193    if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) < 0)
194	return false;
195
196    ExtendedEnabled = true;
197
198    return true;
199}
200
201void
202xf86DisableIO(void)
203{
204    if(!ExtendedEnabled)
205	return;
206
207    sysi86(SI86V86, V86SC_IOPL, 0);
208
209    ExtendedEnabled = false;
210}
211
212#endif
213