aboutsummaryrefslogtreecommitdiffstats
path: root/control.c
blob: 8130ec50798bd9a5a8898c8528bd7119fbaec849 (plain) (blame)
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/*****************************************************************************

Copyright (c) 2022-2023 Jaroslav Hensl <emulator@emulace.cz>

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.

*****************************************************************************/

/* WINAPI Escape/ExtEscape calls ends there */

#include "winhack.h"
#include <gdidefs.h>
#include <dibeng.h>
#include "minidrv.h"
#include "drvlib.h"
#include "ddrawi.h"
#include <wchar.h> /* wchar_t */
#include <string.h> /* _fmemset */

#include "version.h"

#ifdef SVGA
# include "svga_all.h"
# include "control_vxd.h"
#endif

/*
 * MS supported functions
 */

#define QUERYESCSUPPORT 8
#define OPENGL_GETINFO 0x1101
/* list of neighbor 'known' functions */
#define OPENGL_CMD     0x1100
#define WNDOBJ_SETUP   0x1102

#ifndef DCICOMMAND
#define DCICOMMAND     0x0C03 // 3075
// ^ defined in gfidefs.h
#endif

/* DCICOMMAND */
#define DDCREATEDRIVEROBJECT    10 // create an object
#define DDGET32BITDRIVERNAME    11 // get a 32-bit driver name
#define DDNEWCALLBACKFNS        12 // new callback fns coming
#define DDVERSIONINFO           13 // tells driver the ddraw version

#define MOUSETRAILS             39

/*
 * new escape codes
 */

/* all frame buffer devices */
#define FBHDA_REQ            0x110A

/* debug output from ring-3 application */
#define SVGA_DBG             0x110B

/* update buffer if 'need_call_update' is set */
#define FBHDA_UPDATE         0x110C

/* check for drv <-> vxd <-> dll match */
#define SVGA_API             0x110F

/* VMWare SVGA II codes */
#define SVGA_READ_REG        0x1110
#define SVGA_HDA_REQ         0x1112
#define SVGA_REGION_CREATE   0x1114
#define SVGA_REGION_FREE     0x1115
#define SVGA_SYNC            0x1116
#define SVGA_RING            0x1117

#define SVGA_HWINFO_REGS   0x1121
#define SVGA_HWINFO_FIFO   0x1122
#define SVGA_HWINFO_CAPS   0x1123

typedef struct _longRECT {
  LONG left;
  LONG top;
  LONG right;
  LONG bottom;
} longRECT;

/**
 * OpenGL ICD driver name (0x1101):
 * -------------------------------------
 * Dump from real nvidia driver:
 * 0x0000: 0x02
 * 0x0006: 0x01
 * 0x0008: 0x82 (R)
 * 0x0009: 0x73 (I)
 * 0x000a: 0x86 (V)
 * 0x000b: 0x65 (A)
 * 0x000c: 0x84 (T)
 * 0x000d: 0x78 (N)
 * 0x000e: 0x84 (T)
 * ------------------------------------
 * Buffer length in opengl32.dll is 532 bytes (0x214)
 *
 * Struct info is here:
 * https://community.khronos.org/t/how-to-get-icd-driver-name/50988
 *
 **/
typedef struct _opengl_icd_t
{
	long Version;
	long DriverVersion;
	char DLL[262]; /* wchat_t DLL */
} opengl_icd_t;

#define OPENGL_ICD_SIZE sizeof(opengl_icd_t)

/* software OpenGL (Mesa3D softpipe/llvmpipe) */
const static opengl_icd_t software_icd = {
	0x2,
	0x1,
	"SOFTWARE"
};

#ifdef SVGA
/* Mesa3D SVGA3D OpengGL */
const static opengl_icd_t vmwsvga_icd = {
	0x2,
	0x1,
	"VMWSVGA"
};

#pragma pack(push)
#pragma pack(1)

/* SVGA HDA = hardware direct access */
typedef struct _svga_hda_t
{
	uint32_t       vram_linear;   /* frame buffer address from PM32 (shared memory region, accesable with user space programs too) */
	uint8_t __far *vram_pm16;     /* access from THIS code */
	uint32_t       vram_physical; /* physical address only for drivers */
	uint32_t       vram_size;     /* video ram size */

	uint32_t        fifo_linear;
	uint32_t __far *fifo_pm16;
	uint32_t        fifo_physical;
	uint32_t        fifo_size;

  uint32_t        ul_flags_index;
  uint32_t        ul_fence_index;
  uint32_t        ul_gmr_start;
  uint32_t        ul_gmr_count;
  uint32_t        ul_ctx_start;
  uint32_t        ul_ctx_count;
  uint32_t        ul_surf_start;
  uint32_t        ul_surf_count;
  uint32_t __far *userlist_pm16;
  uint32_t        userlist_linear;
  uint32_t        userlist_length;
} svga_hda_t;

#pragma pack(pop)

#define ULF_DIRTY  0
#define ULF_WIDTH  1
#define ULF_HEIGHT 2
#define ULF_BPP    3
#define ULF_PITCH  4
#define ULF_LOCK_UL   5
#define ULF_LOCK_FIFO 6

#define GMR_INDEX_CNT 6

static svga_hda_t SVGAHDA;

#endif /* SVGA only */

#pragma code_seg( _INIT )

#ifdef SVGA
uint32_t GetDevCap(uint32_t search_id);

/*
 * Fix SVGA caps which are known as bad:
 *  - VirtualBox returning A4R4G4B4 instead of R5G6B5,
 *    try to guess it's value from X8R8G8B8.
 *
 */
uint32_t FixDevCap(uint32_t cap_id, uint32_t cap_val)
{
	switch(cap_id)
	{
		case SVGA3D_DEVCAP_SURFACEFMT_R5G6B5:
#ifdef CAP_R5G6B5_ALWAYS_WRONG
		{
			uint32_t xrgb8888 = GetDevCap(SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8);
			xrgb8888 &= ~SVGA3DFORMAT_OP_SRGBWRITE; /* nvidia */
			return xrgb8888;
		}
#else
			if(cap_val & SVGA3DFORMAT_OP_SAME_FORMAT_UP_TO_ALPHA_RENDERTARGET) /* VirtualBox BUG */
			{
				uint32_t xrgb8888 = GetDevCap(SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8);
				xrgb8888 &= ~SVGA3DFORMAT_OP_SRGBWRITE; /* nvidia */
				return xrgb8888;
			}
			
			if((cap_val & SVGA3DFORMAT_OP_3DACCELERATION) == 0) /* VirtualBox BUG, older drivers */
			{
				uint32_t xrgb8888 = GetDevCap(SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8);
				xrgb8888 &= ~SVGA3DFORMAT_OP_SRGBWRITE; /* nvidia */
				if(xrgb8888 & SVGA3DFORMAT_OP_3DACCELERATION)
				{
					return xrgb8888;
				}
			}
#endif
			
			break;
	}
	
	return cap_val;
}

/**
 * Read HW cap, supported old way (GPU gen9):
 *   struct SVGA_FIFO_3D_CAPS in FIFO,
 * and new way (GPU gen10):
 *   SVGA_REG_DEV_CAP HW register
 *
 **/
uint32_t GetDevCap(uint32_t search_id)
{
	if (gSVGA.capabilities & SVGA_CAP_GBOBJECTS)
	{
		/* new way to read device CAPS */
		SVGA_WriteReg(SVGA_REG_DEV_CAP, search_id);
		return FixDevCap(search_id, SVGA_ReadReg(SVGA_REG_DEV_CAP));
	}
	else
	{
		SVGA3dCapsRecord  __far *pCaps = (SVGA3dCapsRecord  __far *)&(gSVGA.fifoMem[SVGA_FIFO_3D_CAPS]);  
		while(pCaps->header.length != 0)
		{
		  if(pCaps->header.type == SVGA3DCAPS_RECORD_DEVCAPS)
		  {
		   	uint32_t datalen = (pCaps->header.length - 2)/2;
	    	SVGA3dCapPair __far *pData = (SVGA3dCapPair __far *)(&pCaps->data);
	    	uint32_t i;
	    	
		 		for(i = 0; i < datalen; i++)
		 		{
		  		uint32_t id  = pData[i][0];
		  		uint32_t val = pData[i][1];
		  			
		  		if(id == search_id)
					{
						return FixDevCap(search_id, val);
					}
				}
			}
			pCaps = (SVGA3dCapsRecord __far *)((uint32_t __far *)pCaps + pCaps->header.length);
		}
	}
	
	return 0;
}

/**
 * Check for 3D support:
 * - we need SVGA_FIFO_CAP_FENCE ans SVGA_FIFO_CAP_SCREEN_OBJECT
 * - SVGA3D_DEVCAP_3D is none zero
 * - at last SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8 is accelerated (GPU gen9 feature,
 *   GPU gen10 - eg. DirectX surfaces jet aren't supported)
 *
 **/
WORD SVGA_3DSupport()
{
	if(SVGA_HasFIFOCap(SVGA_FIFO_CAP_FENCE) && (SVGA_HasFIFOCap(SVGA_FIFO_CAP_SCREEN_OBJECT) || SVGA_HasFIFOCap(SVGA_FIFO_CAP_SCREEN_OBJECT_2)))
	{
		if(GetDevCap(SVGA3D_DEVCAP_3D)) /* is 3D enabled */
		{
			uint32_t surfcap = GetDevCap(SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8);
			if((surfcap & SVGA3DFORMAT_OP_3DACCELERATION) != 0) /* at last X8R8G8B8 needs to be accelerated! */
			{
				return 1;
			}
		}
		else
		{
			dbg_printf("!GetDevCap(SVGA3D_DEVCAP_3D)\n");
		}
	}
	else
	{
		dbg_printf("no SVGA_FIFO_CAP_FENCE || SVGA_FIFO_CAP_SCREEN_OBJECT\n");
	}
	
	return 0;
}

/**
 * init SVGAHDA -> memory map between user space and (virtual) hardware memory
 *
 **/
void SVGAHDA_init()
{
	_fmemset(&SVGAHDA, 0, sizeof(svga_hda_t));
  
  SVGAHDA.ul_flags_index = 0; // dirty, width, height, bpp, pitch, fifo_lock, ul_lock, fb_lock
  SVGAHDA.ul_fence_index = SVGAHDA.ul_flags_index + 8;
  SVGAHDA.ul_gmr_start   = SVGAHDA.ul_fence_index + 1;
  SVGAHDA.ul_gmr_count   = SVGA_ReadReg(SVGA_REG_GMR_MAX_IDS);
  SVGAHDA.ul_ctx_start   = SVGAHDA.ul_gmr_start + SVGAHDA.ul_gmr_count*GMR_INDEX_CNT;
  SVGAHDA.ul_ctx_count   = GetDevCap(SVGA3D_DEVCAP_MAX_CONTEXT_IDS);
  SVGAHDA.ul_surf_start  = SVGAHDA.ul_ctx_start + SVGAHDA.ul_ctx_count;
  SVGAHDA.ul_surf_count  = GetDevCap(SVGA3D_DEVCAP_MAX_SURFACE_IDS);
	SVGAHDA.userlist_length = SVGAHDA.ul_surf_start + SVGAHDA.ul_surf_count;
	
	SVGAHDA.userlist_pm16  = drv_malloc(SVGAHDA.userlist_length * sizeof(uint32_t), &SVGAHDA.userlist_linear);
	
	if(SVGAHDA.userlist_pm16)
	{
		SVGAHDA.userlist_pm16[ULF_DIRTY] = 0xFFFFFFFFUL;
		
		/* zero the memory, because is large than 64k, is much easier do it in PM32 */
		VXD_zeromem(SVGAHDA.userlist_linear, SVGAHDA.userlist_length * sizeof(uint32_t));
		
		SVGAHDA.userlist_pm16[ULF_LOCK_UL] = 0;
		SVGAHDA.userlist_pm16[ULF_LOCK_FIFO] = 0;
	}
	
	dbg_printf("SVGAHDA_init\n");
}

/**
 * update frame buffer and fifo addresses in SVGAHDA from gSVGA
 *
 **/
void SVGAHDA_setmode()
{
	if(SVGAHDA.userlist_pm16)
	{
		SVGAHDA.vram_linear   = gSVGA.fbLinear;
		SVGAHDA.vram_pm16     = gSVGA.fbMem;
		SVGAHDA.vram_physical = gSVGA.fbPhy;
		SVGAHDA.vram_size     = gSVGA.vramSize;
		
		SVGAHDA.fifo_linear   = gSVGA.fifoLinear;
	  SVGAHDA.fifo_pm16     = gSVGA.fifoMem;
	  SVGAHDA.fifo_physical = gSVGA.fifoPhy;
		SVGAHDA.fifo_size     = gSVGA.fifoSize;
	}
}

/**
 * Lock resource
 *
 * Note: this is simple spinlock implementation because multitasking isn't
 * preemptive in Win9x this can lead to deadlock. Please use SVGAHDA_trylock
 * instead.
 *
 * Note: please call SVGAHDA_unlock every time after this function
 *
 **/
BOOL SVGAHDA_lock(uint32_t lockid)
{
	volatile uint32_t __far * ptr_lock = SVGAHDA.userlist_pm16 + lockid;
	
	if(SVGAHDA.userlist_pm16)
	{
		_asm
		{
			.386
			push eax
			push ebx
			
			spin_lock:
				mov  eax, 1
				les   bx, ptr_lock
				lock xchg eax, es:[bx]
				test eax, eax
				jnz  spin_lock
			
			pop ebx
			pop eax
			
		};
		
		return TRUE;
	}
	
	return FALSE;
}

/**
 * Try to lock resource and return TRUE is success.
 *
 * Note: if is function successfull you must call SVGAHDA_unlock!
 *
 **/
BOOL SVGAHDA_trylock(uint32_t lockid)
{
	volatile uint32_t __far * ptr_lock = SVGAHDA.userlist_pm16 + lockid;
	BOOL locked = FALSE;
	
	if(SVGAHDA.userlist_pm16)
	{
		_asm
		{
			.386
			push eax
			push ebx
			
			mov  eax, 1
			les   bx, ptr_lock
			lock xchg eax, es:[bx]
			test eax, eax
			jnz lock_false
			mov locked, 1
			lock_false:
			
			pop ebx
			pop eax
			
		};
	}
	
	return locked;
}

/**
 * Unlock resource
 *
 **/
void SVGAHDA_unlock(uint32_t lockid)
{
	volatile uint32_t __far * ptr_lock = SVGAHDA.userlist_pm16 + lockid;
	
	if(SVGAHDA.userlist_pm16)
	{
		_asm
		{
			.386
			push eax
			push ebx
			
			xor  eax, eax
			les   bx, ptr_lock
			lock xchg eax, es:[bx]
			
			pop ebx
			pop eax
			
		};
	}
}

/**
 * This is called after every screen change
 **/
void SVGAHDA_update(DWORD width, DWORD height, DWORD bpp, DWORD pitch)
{
	if(SVGAHDA.userlist_pm16)
	{
		SVGAHDA.userlist_pm16[ULF_WIDTH]  = width;
		SVGAHDA.userlist_pm16[ULF_HEIGHT] = height;
		SVGAHDA.userlist_pm16[ULF_BPP]    = bpp;
		SVGAHDA.userlist_pm16[ULF_PITCH]  = pitch;
	}
}

#endif /* SVGA only */

/**
 * 2022:
 * Control now serve system QUERYESCSUPPORT (0x8), OPENGL_ICD_DRIVER (0x1101),
 *
 * For second one there is no documentation known to me, so functionality was
 * reverse-enginered from exists drivers and system's opengl32.dll.
 * (update: some note here: https://community.khronos.org/t/how-to-get-icd-driver-name/50988)
 * 
 * Retuned driver names is 'SOFTWARE' and OpenGL library name could be
 * located here:
 *   HKLM\Software\Microsoft\Windows\CurrentVersion\OpenGLDrivers
 * For example:
 *   "SOFTWARE"="mesa3d.dll"
 *
 * 2023:
 * Added driver specific call to:
 *   1) accelerate drawing to access framebuffer directly
        FBHDA_REQ
 *   2) access VMWare SVGA II registers and memory to real GPU acceleration
 *
 * 2023-II:
 * Added DCICOMMAND to query DirectDraw/DirectX interface
 *
 **/
LONG WINAPI __loadds Control(LPVOID lpDevice, UINT function,
  LPVOID lpInput, LPVOID lpOutput)
{
	LONG rc = -1;
	
	dbg_printf("Control (16bit): %d (0x%x)\n", function, function);
	
  if(function == QUERYESCSUPPORT)
  {
  	WORD function_code = 0;
  	function_code =  *((LPWORD)lpInput);
  	switch(function_code)
  	{
  		case OPENGL_GETINFO:
  		case FBHDA_REQ:
  		case FBHDA_UPDATE:
#ifdef SVGA
  		case SVGA_API:	
  		/*
  		 * allow read HW registry/fifo registry and caps even if 3D is 
  	   * disabled for diagnostic tool to check why is disabled
  		 */
  		case SVGA_HWINFO_REGS:
  		case SVGA_HWINFO_FIFO:
  		case SVGA_HWINFO_CAPS:
#endif
  			rc = 1;
  			break;
#ifdef SVGA
  		case SVGA_READ_REG:
  		case SVGA_HDA_REQ:
  		case SVGA_REGION_CREATE:
  		case SVGA_REGION_FREE:
  		case SVGA_SYNC:
			case SVGA_RING:
  			if(wMesa3DEnabled)
  			{
  				rc = 1;
  			}
  			break;
#endif
  		case DCICOMMAND:
  			rc = DD_HAL_VERSION;
  			break;
  		case QUERYESCSUPPORT:
  			rc = 1;
  			break;
  		default:
  			dbg_printf("Control: function check for unknown: %x\n", function_code);
  			break;
  	}
  	
  }
  else if(function == OPENGL_GETINFO) /* input: NULL, output: opengl_icd_t */
  {
#ifdef SVGA  	
  	if(wMesa3DEnabled == 0)
  	{
  		_fmemcpy(lpOutput, &software_icd, OPENGL_ICD_SIZE); /* no 3D use software OPENGL */
  	}
  	else
  	{
  		_fmemcpy(lpOutput, &vmwsvga_icd, OPENGL_ICD_SIZE); /* accelerated OPENGL */
  	}
#else
    _fmemcpy(lpOutput, &software_icd, OPENGL_ICD_SIZE);
#endif
  	
  	rc = 1;
  }
  else if(function == DCICOMMAND) /* input ptr DCICMD */
  {
  	DCICMD_t __far *lpDCICMD = lpInput;
  	if(lpDCICMD != NULL)
  	{
  		if(lpDCICMD->dwVersion == DD_VERSION)
  		{
  			switch(lpDCICMD->dwCommand)
  			{
  				case DDCREATEDRIVEROBJECT:
  					dbg_printf("DDCREATEDRIVEROBJECT: ");
  					if(DDCreateDriverObject(0))
  					{
  						uint32_t __far *lpOut = lpOutput;
  						DWORD hInst = DDHinstance();
  						*lpOut = hInst;
  						
  						dbg_printf("success\n");
  						rc = 1;
  					}
  					else
  					{
  						dbg_printf("failure\n");
  						rc = 0;
  					}
  					break;
  				case DDGET32BITDRIVERNAME:
  					dbg_printf("DDGET32BITDRIVERNAME: ");
  					if(DDGet32BitDriverName(lpOutput))
  					{
  						dbg_printf("success\n");
  						rc = 1;
  					}
  					else
  					{
  						dbg_printf("failure\n");
  						rc = 0;
  					}
  					break;
  				case DDNEWCALLBACKFNS:
  					dbg_printf("DDNEWCALLBACKFNS: ");
  					if(DDNewCallbackFns(lpInput))
  					{
  						dbg_printf("success\n");
  						rc = 1;
  					}
  					else
  					{
  						dbg_printf("failure\n");
  						rc = 0;
  					}
  					break;
  				case DDVERSIONINFO:
  					dbg_printf("DDGetVersion\n");
  					DDGetVersion(lpOutput);
  					rc = 1;
  					break;
  				default:
  					dbg_printf("UNK DD code: %lX\n", lpDCICMD->dwCommand);
  					break;
  			}
  		}
  		else if(lpDCICMD->dwVersion == DCI_VERSION)
  		{
  			dbg_printf("DCI(%d) -> failed!\n", lpDCICMD->dwCommand);
  		}
  		else
  		{
  			dbg_printf("hal required: %lX\n", lpDCICMD->dwVersion);
  		}
  	}
  }
  else if(function == MOUSETRAILS)
  {
  	if(lpOutput)
  	{
  		//...
  	}
  	rc = 1;
  }
  else if(function == FBHDA_REQ) /* input: NULL, output: uint32  */
  {
  	uint32_t __far *lpOut = lpOutput;
  	lpOut[0] = FBHDA_linear;
  	dbg_printf("FBHDA request: %lX\n", FBHDA_linear);
  	
  	rc = 1;
  }
  else if(function == FBHDA_UPDATE) /* input RECT, output: NULL */
  {
#ifdef SVGA
		longRECT __far *lpRECT = lpInput;
		SVGA_UpdateRect(lpRECT->left, lpRECT->top, lpRECT->right - lpRECT->left, lpRECT->bottom - lpRECT->top);
#endif
  }
#ifdef SVGA
  else if(function == SVGA_READ_REG) /* input: uint32_t, output: uint32_t */
  {
  	unsigned long val;
  	unsigned long regname = *((unsigned long __far *)lpInput);
  	
  	if(regname == SVGA_REG_ID)
  	{
  		val = gSVGA.deviceVersionId;
  	}
  	else if(regname == SVGA_REG_CAPABILITIES)
  	{
  		val = gSVGA.capabilities;
  	}
  	else
  	{
  		val = SVGA_ReadReg(regname);
  	}
  	
  	*((unsigned long __far *)lpOutput) = val;
  	
  	rc = 1;
  }
  else if(function == SVGA_HDA_REQ) /* input: NULL, output: svga_hda_t  */
  {
  	svga_hda_t __far *lpHDA  = lpOutput;
  	_fmemcpy(lpHDA, &SVGAHDA, sizeof(svga_hda_t));
  	rc = 1;
  }
  else if(function == SVGA_REGION_CREATE) /* input: 2*uint32_t, output: 2*uint32_t */
  {
  	uint32_t __far *lpIn  = lpInput;
  	uint32_t __far *lpOut = lpOutput;
  	uint32_t rid = lpIn[0];
  	
  	dbg_printf("Region id = %ld, max desc = %ld\n", rid, SVGA_ReadReg(SVGA_REG_GMR_MAX_DESCRIPTOR_LENGTH));	
  	
	  if(rid)
	  {
	  	uint32_t lAddr;
	  	uint32_t ppn;
	  	uint32_t pgblk;
	  	
	  	if(VXD_CreateRegion(lpIn[1], &lAddr, &ppn, &pgblk)) /* allocate physical memory */
	  	{
	  		dbg_printf("Region address = %lX, PPN = %lX, GMRBLK = %lX\n", lAddr, ppn, pgblk);
	  		
		    SVGA_WriteReg(SVGA_REG_GMR_ID, rid);
		    SVGA_WriteReg(SVGA_REG_GMR_DESCRIPTOR, ppn);
		    
		    /* refresh all register, so make sure that new commands will accepts this region */
		    SVGA_Flush();
		    
		    lpOut[0] = rid;
		    lpOut[1] = lAddr;
		    lpOut[2] = pgblk;
	  	}
	  	else
	  	{
	  		lpOut[0] = 0;
	  		lpOut[1] = 0;
	  		lpOut[2] = 0;
	  	}
	  }
	  
	  rc = 1;
  }
  else if(function == SVGA_REGION_FREE) /* input: 2*uint32_t, output: NULL */
  {
  	uint32_t __far *lpin = lpInput;
    uint32_t id     = lpin[0];
    uint32_t linear = lpin[1];
    uint32_t pgblk  = lpin[2];
    
    /* flush all register inc. fifo so make sure, that all commands are processed */
    SVGA_Flush();
    
    SVGA_WriteReg(SVGA_REG_GMR_ID, id);
    SVGA_WriteReg(SVGA_REG_GMR_DESCRIPTOR, 0);
    
    /* sync again */
    SVGA_Flush();
    
    /* region physical delete */
    VXD_FreeRegion(linear, pgblk);
    
    rc = 1;
  }
  else if(function == SVGA_HWINFO_REGS) /* input: NULL, output: 256*uint32_t */
  {
  	int i;
  	uint32_t __far *lpreg = lpOutput;
  	for(i = 0; i < 256; i++)
  	{
  		*lpreg = SVGA_ReadReg(i);
  		lpreg++;
  	}
  	
  	rc = 1;
  }
  else if(function == SVGA_HWINFO_FIFO) /* input: NULL, output: 1024*uint32_t */
  {
  	int i;
  	uint32_t __far *lpreg = lpOutput;
  	
  	for(i = 0; i < 1024; i++)
  	{
  		*lpreg = gSVGA.fifoMem[i];
  		lpreg++;
  	}
  	
  	rc = 1;
  }
  else if(function == SVGA_HWINFO_CAPS) /* input: NULL, output: 512*uint32_t */
  {
  	int i;
  	uint32_t         __far *lpreg = lpOutput;
    SVGA3dCapsRecord __far *pCaps;
    SVGA3dCapPair    __far *pData;
    
    
    if (gSVGA.capabilities & SVGA_CAP_GBOBJECTS)
    {
    	/* new way to read device CAPS */
			for (i = 0; i < 512; i++)
			{
				SVGA_WriteReg(SVGA_REG_DEV_CAP, i);
				lpreg[i] = FixDevCap(i, SVGA_ReadReg(SVGA_REG_DEV_CAP));
				dbg_printf("VMSVGA3d_3: cap[%d]=0x%lX\n", i, lpreg[i]);
			}
		}
    else
    {
	    /* old way to read device CAPS */
	    pCaps = (SVGA3dCapsRecord  __far *)&(gSVGA.fifoMem[SVGA_FIFO_3D_CAPS]);
	    
	    while(pCaps->header.length != 0)
	    {
	    	dbg_printf("SVGA_FIFO_3D_CAPS: %ld, %ld\n", pCaps->header.type, pCaps->header.length);
		    if(pCaps->header.type == SVGA3DCAPS_RECORD_DEVCAPS)
		    {
		    	uint32_t datalen = (pCaps->header.length - 2)/2;
		    	pData = (SVGA3dCapPair __far *)(&pCaps->data);
		  		for(i = 0; i < datalen; i++)
		  		{
		  			uint32_t id  = pData[i][0];
		  			uint32_t val = pData[i][1];
		  			
		  			if(id < 512)
		  			{
		  				lpreg[id] = FixDevCap(id, val);
		  			}
		  			
		  			dbg_printf("VMSVGA3d_2: cap[%ld]=0x%lX\n", id, val);
		  		}
		  	}
		  	dbg_printf("SVGA_FIFO_3D_CAPS: end\n");
		  	
		  	pCaps = (SVGA3dCapsRecord __far *)((uint32_t __far *)pCaps + pCaps->header.length);
		  }
		}
  	
  	rc = 1;
  }
  else if(function == SVGA_SYNC) /* input: NULL, output: NULL */
  {
		SVGA_Flush();
		
		rc = 1;
  }
  else if(function == SVGA_RING) /* input: NULL, output: NULL */
  {
		SVGA_WriteReg(SVGA_REG_SYNC, 1);
		
		rc = 1;
  }
  else if(function == SVGA_API) /* input: NULL, output: 2x DWORD */
  {
  	uint32_t __far *lpver = lpOutput;
  	
  	lpver[0] = DRV_API_LEVEL;
  	
    lpver[1] = VXD_apiver();
    
    rc = 1;
  }
#endif /* SVGA only */
  
  /* if command accepted, return */
  if(rc >= 0)
  {
  	return rc;
  }
  
  /* else, call DIB_Control handle */
  dbg_printf("Control: unknown code: %d\n", function);
  return DIB_Control(lpDevice, function, lpInput, lpOutput);
}