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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/* $Id: driver.cpp $ */
/** @file
* VBoxVideo driver, Haiku Guest Additions, implementation.
*/
/*
* Copyright (C) 2012-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*
* This code is based on:
*
* VirtualBox Guest Additions for Haiku.
* Copyright (c) 2011 Mike Smith <mike@scgtrp.net>
* Fran�ois Revol <revol@free.fr>
*
* 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.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <KernelExport.h>
#include <PCI.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <graphic_driver.h>
#include <VBoxGuest-haiku.h>
#include <VBoxVideoGuest.h>
#include "../common/VBoxVideo_common.h"
#define VENDOR_ID 0x80ee
#define DEVICE_ID 0xbeef
#define DRIVER_NAME "VBoxVideoDriver"
#define DEVICE_FORMAT "vd_%04X_%04X_%02X%02X%02X"
/** @todo r=ramshankar: pretty sure IPRT has something for page rounding,
* replace with IPRT version later. */
#define ROUND_TO_PAGE_SIZE(x) (((x) + (B_PAGE_SIZE) - 1) & ~((B_PAGE_SIZE) - 1))
#define ENABLE_DEBUG_TRACE
#undef TRACE
#ifdef ENABLE_DEBUG_TRACE
#define TRACE(x...) dprintf("VBoxVideo: " x)
#else
#define TRACE(x...) ;
#endif
int32 api_version = B_CUR_DRIVER_API_VERSION; // revision of driver API we support
extern "C" status_t vm_set_area_memory_type(area_id id, phys_addr_t physicalBase, uint32 type);
struct Benaphore
{
sem_id sem;
int32 count;
status_t Init(const char *name)
{
count = 0;
sem = create_sem(0, name);
return sem < 0 ? sem : B_OK;
}
status_t Acquire()
{
if (atomic_add(&count, 1) > 0)
return acquire_sem(sem);
return B_OK;
}
status_t Release()
{
if (atomic_add(&count, -1) > 1)
return release_sem(sem);
return B_OK;
}
void Delete()
{
delete_sem(sem);
}
};
struct DeviceInfo
{
uint32 openCount; /* Count of how many times device has been opened */
uint32 flags; /* Device flags */
area_id sharedArea; /* Area shared between driver and all accelerants */
SharedInfo *sharedInfo; /* Pointer to shared info area memory */
pci_info pciInfo; /* Copy of pci info for this device */
char name[B_OS_NAME_LENGTH]; /* Name of device */
};
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
status_t device_open(const char *name, uint32 flags, void **cookie);
status_t device_close(void *dev);
status_t device_free(void *dev);
status_t device_read(void *dev, off_t pos, void *buf, size_t *len);
status_t device_write(void *dev, off_t pos, const void *buf, size_t *len);
status_t device_ioctl(void *dev, uint32 msg, void *buf, size_t len);
static uint32 get_color_space_for_depth(uint32 depth);
/*********************************************************************************************************************************
* Globals *
*********************************************************************************************************************************/
/* At most one virtual video card ever appears, no reason for this to be an array */
static DeviceInfo gDeviceInfo;
static char *gDeviceNames[2] = { gDeviceInfo.name, NULL };
static bool gCanHasDevice = false; /* is the device present? */
static Benaphore gLock;
static pci_module_info *gPCI;
static device_hooks gDeviceHooks =
{
device_open,
device_close,
device_free,
device_ioctl,
device_read,
device_write,
NULL, /* select */
NULL, /* deselect */
NULL, /* read_pages */
NULL /* write_pages */
};
status_t init_hardware()
{
LogFlowFunc(("init_hardware\n"));
status_t err = get_module(VBOXGUEST_MODULE_NAME, (module_info **)&g_VBoxGuest);
if (err == B_OK)
{
err = get_module(B_PCI_MODULE_NAME, (module_info **)&gPCI);
if (err == B_OK)
return B_OK;
LogRel((DRIVER_NAME ":_init_hardware() get_module(%s) failed. err=%08lx\n", B_PCI_MODULE_NAME));
}
else
LogRel((DRIVER_NAME ":_init_hardware() get_module(%s) failed. err=%08lx\n", VBOXGUEST_MODULE_NAME, err));
return B_ERROR;
}
status_t init_driver()
{
LogFlowFunc(("init_driver\n"));
gLock.Init("VBoxVideo driver lock");
uint32 pciIndex = 0;
while (gPCI->get_nth_pci_info(pciIndex, &gDeviceInfo.pciInfo) == B_OK)
{
if (gDeviceInfo.pciInfo.vendor_id == VENDOR_ID && gDeviceInfo.pciInfo.device_id == DEVICE_ID)
{
sprintf(gDeviceInfo.name, "graphics/" DEVICE_FORMAT,
gDeviceInfo.pciInfo.vendor_id, gDeviceInfo.pciInfo.device_id,
gDeviceInfo.pciInfo.bus, gDeviceInfo.pciInfo.device, gDeviceInfo.pciInfo.function);
TRACE("found device %s\n", gDeviceInfo.name);
gCanHasDevice = true;
gDeviceInfo.openCount = 0;
size_t sharedSize = (sizeof(SharedInfo) + 7) & ~7;
gDeviceInfo.sharedArea = create_area("vboxvideo shared info",
(void **)&gDeviceInfo.sharedInfo, B_ANY_KERNEL_ADDRESS,
ROUND_TO_PAGE_SIZE(sharedSize), B_FULL_LOCK,
B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA | B_USER_CLONEABLE_AREA);
uint16_t width, height, vwidth, bpp, flags;
VBoxVideoGetModeRegisters(&width, &height, &vwidth, &bpp, &flags);
gDeviceInfo.sharedInfo->currentMode.space = get_color_space_for_depth(bpp);
gDeviceInfo.sharedInfo->currentMode.virtual_width = width;
gDeviceInfo.sharedInfo->currentMode.virtual_height = height;
gDeviceInfo.sharedInfo->currentMode.h_display_start = 0;
gDeviceInfo.sharedInfo->currentMode.v_display_start = 0;
gDeviceInfo.sharedInfo->currentMode.flags = 0;
gDeviceInfo.sharedInfo->currentMode.timing.h_display = width;
gDeviceInfo.sharedInfo->currentMode.timing.v_display = height;
/* Not used, but this makes a reasonable-sounding refresh rate show in screen prefs: */
gDeviceInfo.sharedInfo->currentMode.timing.h_total = 1000;
gDeviceInfo.sharedInfo->currentMode.timing.v_total = 1;
gDeviceInfo.sharedInfo->currentMode.timing.pixel_clock = 850;
/* Map the PCI memory space */
uint32 command_reg = gPCI->read_pci_config(gDeviceInfo.pciInfo.bus,
gDeviceInfo.pciInfo.device, gDeviceInfo.pciInfo.function, PCI_command, 2);
command_reg |= PCI_command_io | PCI_command_memory | PCI_command_master;
gPCI->write_pci_config(gDeviceInfo.pciInfo.bus, gDeviceInfo.pciInfo.device,
gDeviceInfo.pciInfo.function, PCI_command, 2, command_reg);
gDeviceInfo.sharedInfo->framebufferArea = map_physical_memory("vboxvideo framebuffer",
(phys_addr_t)gDeviceInfo.pciInfo.u.h0.base_registers[0],
gDeviceInfo.pciInfo.u.h0.base_register_sizes[0], B_ANY_KERNEL_BLOCK_ADDRESS,
B_READ_AREA | B_WRITE_AREA, &(gDeviceInfo.sharedInfo->framebuffer));
vm_set_area_memory_type(gDeviceInfo.sharedInfo->framebufferArea,
(phys_addr_t)gDeviceInfo.pciInfo.u.h0.base_registers[0], B_MTR_WC);
break;
}
pciIndex++;
}
return B_OK;
}
const char** publish_devices()
{
LogFlowFunc(("publish_devices\n"));
if (gCanHasDevice)
return (const char **)gDeviceNames;
return NULL;
}
device_hooks* find_device(const char *name)
{
LogFlowFunc(("find_device\n"));
if (gCanHasDevice && strcmp(name, gDeviceInfo.name) == 0)
return &gDeviceHooks;
return NULL;
}
void uninit_driver()
{
LogFlowFunc(("uninit_driver\n"));
gLock.Delete();
put_module(VBOXGUEST_MODULE_NAME);
}
status_t device_open(const char *name, uint32 flags, void **cookie)
{
LogFlowFunc(("device_open\n"));
if (!gCanHasDevice || strcmp(name, gDeviceInfo.name) != 0)
return B_BAD_VALUE;
/** @todo init device! */
*cookie = (void *)&gDeviceInfo;
return B_OK;
}
status_t device_close(void *dev)
{
LogFlowFunc(("device_close\n"));
return B_ERROR;
}
status_t device_free(void *dev)
{
LogFlowFunc(("device_free\n"));
DeviceInfo& di = *(DeviceInfo *)dev;
gLock.Acquire();
if (di.openCount <= 1)
{
/// @todo deinit device!
delete_area(di.sharedArea);
di.sharedArea = -1;
di.sharedInfo = NULL;
}
if (di.openCount > 0)
di.openCount--;
gLock.Release();
return B_OK;
}
status_t device_read(void *dev, off_t pos, void *buf, size_t *len)
{
LogFlowFunc(("device_read\n"));
return B_NOT_ALLOWED;
}
status_t device_write(void *dev, off_t pos, const void *buf, size_t *len)
{
LogFlowFunc(("device_write\n"));
return B_NOT_ALLOWED;
}
status_t device_ioctl(void *cookie, uint32 msg, void *buf, size_t len)
{
LogFlowFunc(("device_ioctl\n"));
DeviceInfo *dev = (DeviceInfo *)cookie;
switch (msg)
{
case B_GET_ACCELERANT_SIGNATURE:
{
strcpy((char *)buf, "vboxvideo.accelerant");
return B_OK;
}
case VBOXVIDEO_GET_PRIVATE_DATA:
{
/** @todo r=ramshankar: implement RTR0MemUserCopyFrom for haiku. */
return user_memcpy(buf, &dev->sharedArea, sizeof(area_id));
}
case VBOXVIDEO_GET_DEVICE_NAME:
{
/** @todo r=ramshankar: implement RTR0MemUserCopyFrom for haiku. */
if (user_strlcpy((char *)buf, gDeviceInfo.name, len) < B_OK)
return B_BAD_ADDRESS;
return B_OK;
}
case VBOXVIDEO_SET_DISPLAY_MODE:
{
display_mode *mode = (display_mode *)buf;
VBoxVideoSetModeRegisters(mode->timing.h_display, mode->timing.v_display,
mode->timing.h_display, get_depth_for_color_space(mode->space), 0, 0, 0);
gDeviceInfo.sharedInfo->currentMode = *mode;
return B_OK;
}
default:
return B_BAD_VALUE;
}
}
|