summaryrefslogtreecommitdiff
path: root/src/vmwaremodule.c
blob: 0da2440c89f829bd44473795a6a6185baa8d078c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**********************************************************
 * Copyright 2010 VMware, Inc.  All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 **********************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_LIBDRM
#include <xf86drm.h>
#endif

#include <xorg-server.h>
#include <xf86.h>

#ifndef HAVE_XORG_SERVER_1_5_0
#include <xf86_ansic.h>
#include <xf86_libc.h>
#endif

/*
 * Defines and exported module info.
 */

#define VMWARE_DRIVER_NAME    "vmware"
#define VMWGFX_DRIVER_NAME    "vmwgfx"
#define VMWGFX_MODULE_NAME    "vmwgfx"
#define VMWGFX_COMPAT_MAJOR   11
#define VMWGFX_REQUIRED_MAJOR 11
#define VMWGFX_REQUIRED_MINOR 0
#define VMWLEGACY_DRIVER_NAME "vmwlegacy"

#define VMW_STRING_INNER(s) #s
#define VMW_STRING(str) VMW_STRING_INNER(str)

#define VMWARE_DRIVER_VERSION \
   (PACKAGE_VERSION_MAJOR * 65536 + PACKAGE_VERSION_MINOR * 256 + PACKAGE_VERSION_PATCHLEVEL)
#define VMWARE_DRIVER_VERSION_STRING \
    VMW_STRING(PACKAGE_VERSION_MAJOR) "." VMW_STRING(PACKAGE_VERSION_MINOR) \
    "." VMW_STRING(PACKAGE_VERSION_PATCHLEVEL)

/*
 * Standard four digit version string expected by VMware Tools installer.
 * As the driver's version is only  {major, minor, patchlevel},
 * the fourth digit may describe the commit number relative to the
 * last version tag as output from `git describe`
 */

#ifdef __GNUC__
#ifdef VMW_SUBPATCH
const char vmware_drv_modinfo[] __attribute__((section(".modinfo"),unused)) =
  "version=" VMWARE_DRIVER_VERSION_STRING "." VMW_STRING(VMW_SUBPATCH);
#else
const char vmware_drv_modinfo[] __attribute__((section(".modinfo"),unused)) =
  "version=" VMWARE_DRIVER_VERSION_STRING ".0";
#endif /*VMW_SUBPATCH*/
#endif

static XF86ModuleVersionInfo vmware_version;
static MODULESETUPPROTO(vmware_setup);

_X_EXPORT XF86ModuleData vmwareModuleData = {
    &vmware_version,
    vmware_setup,
    NULL
};

extern XF86ModuleData *VMWGFX_MODULE_DATA;

/*
 * Chain loading functions
 */

static Bool
vmware_check_kernel_module()
{
#ifdef HAVE_LIBDRM
    /* Super simple way of knowing if the kernel driver is loaded */
    int ret = drmOpen(VMWGFX_MODULE_NAME, NULL);
    if (ret < 0) {
	/* This shouldn't go in the log as the original message does not */
	fprintf(stderr, "%s: Please ignore above \"FATAL: Module %s not found."
		"\"\n", VMWARE_DRIVER_NAME, VMWGFX_MODULE_NAME);
	/* This is what goes into the log on the other hand */
	xf86DrvMsg(-1, X_PROBED, "%s: Please ignore above \"[drm] failed to "
		   "load kernel  module \"%s\"\"\n", VMWARE_DRIVER_NAME,
		   VMWGFX_MODULE_NAME);
	return FALSE;
    }

    drmClose(ret);

    return TRUE;
#else
    return FALSE;
#endif /* HAVE_LIBDRM */
}

static Bool
vmware_check_vmwgfx_driver(int matched, pointer opts)
{
#ifdef HAVE_LIBDRM
    int major; int minor;
    pointer module;
    CARD32 version;

    if (matched) {
	xf86DrvMsg(-1, X_PROBED, "%s: X configured to use %s X driver assume "
		   "who ever did that knows what they are doing\n",
		   VMWARE_DRIVER_NAME, VMWGFX_DRIVER_NAME);
	/* Also how did they end up here, if the configured X to use vmwgfx and
	 * X should load that driver for that hardware. And since there is only
	 * one SVGA device this driver shouldn't be loaded. Weird...
	 */
	return TRUE;
    }

    module = xf86LoadOneModule(VMWGFX_DRIVER_NAME, opts);
    if (!module) {
	xf86DrvMsg(-1, X_ERROR, "%s: Please ignore the above warnings about "
		   "not being able to to load module/driver %s\n",
		   VMWARE_DRIVER_NAME, VMWGFX_DRIVER_NAME);
	return FALSE;
    }

    version = xf86GetModuleVersion(module);
    major = GET_MODULE_MAJOR_VERSION(version);
    minor = GET_MODULE_MINOR_VERSION(version);

    if (major > VMWGFX_COMPAT_MAJOR ||
	major < VMWGFX_REQUIRED_MAJOR ||
	(major == VMWGFX_REQUIRED_MAJOR && minor < VMWGFX_REQUIRED_MINOR)) {
	xf86DrvMsg(-1, X_PROBED, "%s: The %s X driver failed version "
		   "checking.\n", VMWARE_DRIVER_NAME, VMWGFX_DRIVER_NAME);
	goto err;
    }

    return TRUE;

err:
    /* XXX We should drop the reference on the module here */
#endif /* HAVE_LIBDRM */
    return FALSE;
}

static Bool
vmware_chain_module(pointer opts)
{
    int vmwlegacy_devices;
    int vmwgfx_devices;
    int vmware_devices;
    int matched;
    char *driver_name;
    GDevPtr *gdevs;
    GDevPtr gdev;
    int i;

    vmware_devices = xf86MatchDevice(VMWARE_DRIVER_NAME, &gdevs);
    vmwgfx_devices = xf86MatchDevice(VMWGFX_DRIVER_NAME, NULL);
    vmwlegacy_devices = xf86MatchDevice(VMWLEGACY_DRIVER_NAME, NULL);

    if (vmware_check_vmwgfx_driver(vmwgfx_devices, opts) &&
	vmware_check_kernel_module()) {
	xf86DrvMsg(-1, X_INFO, "%s: Using %s X driver.\n",
		   VMWARE_DRIVER_NAME, VMWGFX_DRIVER_NAME);
	driver_name = VMWGFX_DRIVER_NAME;
	matched = 1;
    } else {
	xf86DrvMsg(-1, X_INFO, "%s: Using %s driver everything is fine.\n",
		   VMWARE_DRIVER_NAME, VMWLEGACY_DRIVER_NAME);
	driver_name = VMWLEGACY_DRIVER_NAME;
	matched = vmwlegacy_devices;
    }

    /* Xorg -configure returns 1 from xf86MatchDevice with NULL gdevs */
    for (i = 0; gdevs && i < vmware_devices; i++) {
	gdev = gdevs[i];
	gdev->driver = driver_name;
    }

    free(gdevs);

    if (!matched) {
	if (xf86LoadOneModule(driver_name, opts) == NULL) {
	    xf86DrvMsg(-1, X_ERROR, "%s: Unexpected failure while "
		       "loading the \"%s\" driver. Giving up.\n",
		       VMWARE_DRIVER_NAME, driver_name);
	    return FALSE;
	}
    }

    return TRUE;
}


/*
 * Module info
 */

static XF86ModuleVersionInfo vmware_version = {
    VMWARE_DRIVER_NAME,
    MODULEVENDORSTRING,
    MODINFOSTRING1,
    MODINFOSTRING2,
    XORG_VERSION_CURRENT,
    PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCHLEVEL,
    ABI_CLASS_VIDEODRV,
    ABI_VIDEODRV_VERSION,
    MOD_CLASS_VIDEODRV,
    {0, 0, 0, 0}
};

static pointer
vmware_setup(pointer module, pointer opts, int *errmaj, int *errmin)
{
    static Bool setupDone = 0;

    /* This module should be loaded only once, but check to be sure. */
    if (!setupDone) {
	setupDone = 1;

	/* Chain load the real driver */
	if (vmware_chain_module(opts))
	    return (pointer) 1;
	else {
	    if (errmaj)
		*errmaj = LDR_NOSUBENT;
	    return NULL;
	}
    } else {
	if (errmaj)
	    *errmaj = LDR_ONCEONLY;
	return NULL;
    }
}