summaryrefslogtreecommitdiff
path: root/QemuVGADriver/src/DriverDoDriverIO.c
blob: d72fd9cdfe0c1bc9e0d106edb79e1bbbd2b67e50 (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
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
/* Simple PCI video driver for use with Mac-On-Linux emulator
 *
 * Basically, this driver forward Apple video driver calls to
 * the emulator via a fake HW (and later, a "sc" based API).
*/

#include "VideoDriverPrivate.h"
#include "VideoDriverPrototypes.h"
#include "DriverQDCalls.h"
#include "QemuVga.h"

DriverDescription TheDriverDescription = {
	/*
	 * Signature info
	 */
	kTheDescriptionSignature,		/* OSType driverDescSignature */
	kInitialDriverDescriptor,		/* DriverDescVersion driverDescVersion */
	QEMU_PCI_VIDEO_NAME,
	0x01, 0x01,
	0, 0,
	/*
	 * DriverOSRuntime driverOSRuntimeInfo
	 */
	0					/* RuntimeOptions driverRuntime */
	| (0 * kDriverIsLoadedUponDiscovery)	/* Loader runtime options */
	| (1 * kDriverIsOpenedUponLoad)		/* Opened when loaded */
	| (1 * kDriverIsUnderExpertControl)	/* I/O expert handles loads/opens */
	| (0 * kDriverIsConcurrent)		/* concurrent */
	| (0 * kDriverQueuesIOPB),		/* Internally queued */
	QEMU_PCI_VIDEO_PNAME,			/* Str31 driverName (OpenDriver param) */
	0, 0, 0, 0, 0, 0, 0, 0,			/* UInt32 driverDescReserved[8] */
	/*
	 * DriverOSService Information. This section contains a vector count followed by
	 * a vector of structures, each defining a driver service.
	 */
	1,					/* ServiceCount nServices */
	/*
	 * DriverServiceInfo service[0]
	 */
	kServiceCategoryNdrvDriver,		/* OSType serviceCategory */
	kNdrvTypeIsVideo,				/* OSType serviceType */
	1, 0, 0, 0
};

/*
 * All driver-global information is in a structure defined in NCRDriverPrivate.
 * Note that "modern" drivers do not have access to their dce. In native Power PC
 * environments, the global world is created by the Code Fragment Manager (hmm,
 * perhaps it is created by CFMInitialize).
 */
DriverGlobal	gDriverGlobal;

/*
 * DoDriverIO
 *
 * In the new driver environment, DoDriverIO performs all driver
 * functions. It is called with the following parameters:
 *	IOCommandID		A unique reference for this driver request. In
 *				the emulated environment, this will be the ParamBlkPtr
 *				passed in from the Device Manager.
 *
 *	IOCommandContents	A union structure that contains information for the
 *				specific request. For the emulated environment, this
 *				will contain the following:
 *		Initialize	Driver RefNum and the name registry id for this driver.
 *		Finalize	Driver RefNum and the name registry id for this driver. 
 *		Others		The ParamBlkPtr
 *
 *	IOCommandCode		A switch value that specifies the required function.
 *
 *	IOCommandKind		A bit-mask indicating Synchronous, Asynchronous, and Immediate
 *
 * For Synchronous and Immediate commands, DoDriverIO returns the final status to
 * the Device Manager. For Asynchronous commands, DoDriverIO may return kIOBusyStatus.
 * If it returns busy status, the driver promises to call IOCommandIsComplete when
 * the transaction has completed.
 */
OSStatus
DoDriverIO( AddressSpaceID addressSpaceID, IOCommandID ioCommandID, IOCommandContents ioCommandContents,
	    IOCommandCode ioCommandCode, IOCommandKind ioCommandKind )
{
	OSStatus status;

	/*
	 * Note: Initialize, Open, KillIO, Close, and Finalize are either synchronous
	 * or immediate. Read, Write, Control, and Status may be immediate,
	 * synchronous, or asynchronous.
	 */

	lprintf("DoDriverIO cmdCode=%d\n", ioCommandCode);

	switch( ioCommandCode ) {
	case kInitializeCommand:			/* Always immediate */
		status = DriverInitializeCmd(addressSpaceID, ioCommandContents.initialInfo);
		CheckStatus(status, "Initialize failed");
		break;
	case kFinalizeCommand:				/* Always immediate */
		status = DriverFinalizeCmd(ioCommandContents.finalInfo);
		break;
	case kSupersededCommand:
		status = DriverSupersededCmd(ioCommandContents.supersededInfo, FALSE);
		break;
	case kReplaceCommand:				/* replace an old driver */
		status = DriverReplaceCmd(addressSpaceID, ioCommandContents.replaceInfo);
		break;
	case kOpenCommand:				/* Always immediate */
		status = DriverOpenCmd(addressSpaceID, ioCommandContents.pb);
		break;
	case kCloseCommand:				/* Always immediate */
		status = DriverCloseCmd(ioCommandContents.pb);
		break;
	case kControlCommand:
		/* lprintf("kControlCommand\n"); */				
		status = DriverControlCmd( addressSpaceID, ioCommandID, ioCommandKind,
					   (CntrlParam*)ioCommandContents.pb );
		break;
	case kStatusCommand:
		status = DriverStatusCmd( ioCommandID, ioCommandKind, 
					  (CntrlParam *)ioCommandContents.pb );
		break;
	case kReadCommand:
		status = DriverReadCmd(	addressSpaceID, ioCommandID, ioCommandKind,
					ioCommandContents.pb );
		break;
	case kWriteCommand:
		status = DriverWriteCmd( addressSpaceID, ioCommandID, ioCommandKind,
					 ioCommandContents.pb);
		break;
	case kKillIOCommand:				/* Always immediate */
		status = DriverKillIOCmd(ioCommandContents.pb);
		break;
	default:
		status = paramErr;
		break;
	}
	lprintf("Completing with status=%d (kind: %x)\n", status, ioCommandKind);

	/*
	 * Force a valid result for immediate commands -- they must return a valid
	 * status to the Driver Manager: returning kIOBusyStatus would be a bug..
	 * Non-immediate commands return a status from the lower-level routine. If the
	 * status is kIOBusyStatus, we just return -- an asynchronous I/O completion
	 * routine will eventually complete the request. If it's some other status, the
	 * lower-level routine has completed a non-immediate task, so we call
	 * IOCommandIsComplete and return its (presumably noErr) status.
	 */
	if( (ioCommandKind & kImmediateIOCommandKind) != 0 ) {
		;	/* Immediate commands return the operation status */
	}
	else if (status == ioInProgress) {
		/*
		 * An asynchronous operation is in progress. The driver handler promises
		 * to call IOCommandIsComplete when the operation concludes.
		 */
		status = noErr;
	} else {
		/*
		 * Normal command that completed synchronously. Dequeue the user's
		 * parameter block.
		 */
		status = (OSStatus)IOCommandIsComplete(ioCommandID, (OSErr)status);
	}
	return status;
}

/*
 * DriverInitializeCmd
 *
 * The New Driver Manager calls this when the driver is first opened.
 */
OSStatus
DriverInitializeCmd( AddressSpaceID addressSpaceID, DriverInitInfoPtr driverInitInfoPtr )
{
	OSStatus status;
		
	Trace(DriverInitializeCmd);

	lprintf("** First call:\n");
	lprintf("   DoDriverIO       @ %p\n", DoDriverIO);
	lprintf("   DriverStatusCmd  @ %p\n", DriverStatusCmd);
	lprintf("   DriverControlCmd @ %p\n", DriverControlCmd);
	
	GLOBAL.refNum = driverInitInfoPtr->refNum;
	GLOBAL.openCount = 0;
	GLOBAL.inInterrupt = false;
	GLOBAL.hasTimer = false;
	
	RegistryEntryIDInit( &GLOBAL.deviceEntry );
	status = RegistryEntryIDCopy( &driverInitInfoPtr->deviceEntry, &GLOBAL.deviceEntry );
	if( status != noErr )
		return status;

	GLOBAL.isOpen = false;
	GLOBAL.qdInterruptsEnable = false;
	GLOBAL.qdVBLInterrupt = NULL;

	GLOBAL.boardFBAddress = GetDeviceBARAddress(&GLOBAL.deviceEntry,
												QEMU_PCI_VIDEO_BASE_REG,
												&GLOBAL.boardFBMappedSize,
												NULL);
	if (GLOBAL.boardFBAddress == NULL) {
		status = paramErr;
		goto bail;
	}
	lprintf("boardFBAddress %08lX boardFBMappedSize %08lX\n", 
			GLOBAL.boardFBAddress, GLOBAL.boardFBMappedSize);

	GLOBAL.boardRegAddress = GetDeviceBARAddress(&GLOBAL.deviceEntry,
												 QEMU_PCI_VIDEO_MMIO_REG,
												 &GLOBAL.boardRegMappedSize,
												 NULL);
	if (GLOBAL.boardRegAddress == NULL) {
		status = paramErr;
		goto bail;
	}
	lprintf("boardRegAddress %08lX boardRegMappedSize %08lX\n", 
			GLOBAL.boardRegAddress, GLOBAL.boardRegMappedSize);


	lprintf("Enabling memory space..\n");
	status = EnablePCIMemorySpace(&GLOBAL.deviceEntry);
	if (status != noErr) {
		lprintf("EnablePCIMemorySpace returned %d\n", status);
		goto bail;
	}

	status = QemuVga_Init();
	if (status != noErr)
		goto bail;
	
bail:
	DBG(lprintf("Driver init result: %d\n", status));
	
	return status;
}

/*
 * DriverReplaceCmd
 *
 * We are replacing an existing driver -- or are completing an initialization sequence.
 * Retrieve any state information from the Name Registry (we have none), install
 * our interrupt handlers, and activate the device.
 *
 * We don't use the calledFromInitialize parameter, but it's here so that a driver can
 * distinguish between initialization (fetch only the NVRAM parameter) and replacement
 * (fetch state information that may be left-over from the previous incantation).
 */
OSStatus
DriverReplaceCmd( AddressSpaceID addressSpaceID, DriverReplaceInfoPtr driverReplaceInfoPtr )
{
	OSStatus status;
	
	Trace(DriverReplaceCmd);

	GLOBAL.refNum = driverReplaceInfoPtr->refNum;
	GLOBAL.deviceEntry = driverReplaceInfoPtr->deviceEntry;

	status = DriverInitializeCmd(addressSpaceID, driverReplaceInfoPtr);

	return status;
}

/*
 * DriverFinalizeCmd
 *
 * Process a DoDriverIO finalize command.
 */
OSStatus
DriverFinalizeCmd( DriverFinalInfoPtr driverFinalInfoPtr )
{
	Trace(DriverFinializeCmd);
	(void) DriverSupersededCmd((DriverSupersededInfoPtr) driverFinalInfoPtr, TRUE);
	return noErr;
}

/*
 * DriverSupersededCmd
 *
 * We are shutting down, or being replaced by a later driver. Wait for all I/O to
 * complete and store volatile state in the Name Registry whree it will be retrieved
 * by our replacement.
 */
OSStatus
DriverSupersededCmd( DriverSupersededInfoPtr driverSupersededInfoPtr, Boolean calledFromFinalize )
{
	Trace(DriverSupersededCmd);

	/*
	 * This duplicates DriverKillIOCmd, the correct algorithm would wait for
	 * concurrent I/O to complete. Hmm, what about "infinite wait" I/O, such
	 * as would be posted by a modem server or socket listener? Note that
	 * this section needs to be extended to handle all pending requests.
	 *
	 * It's safe to call CompleteThisRequest, as that routine uses an atomic
	 * operation that allows it to be called when no request is pending without
	 * any possible problems. Since it's a secondary interrupt handler, we
	 * need to call it through the Driver Services Library.
	 *
	 * Warning: GLOBAL.perRequestDataPtr will be NULL if initialization fails
	 * and the Driver Manager tries to terminate us. When we permit concurrent
	 * requests, this will loop on all per-request records.
	 */
	 
	QemuVga_Exit();

	RegistryEntryIDDispose( &GLOBAL.deviceEntry );
	
	return noErr;
}

/*
 * DriverControlCmd
 *
 * Process a PBControl command.
 */
OSStatus
DriverControlCmd( AddressSpaceID addressSpaceID, IOCommandID ioCommandID,
		  IOCommandKind ioCommandKind, CntrlParam *pb )
{
	OSStatus status;
	void *genericPtr;
	
	/* The 'csParam' field of the 'CntrlParam' stucture is defined as 'short csParam[11]'.  This is
	 * meant for 'operation defined parameters.' For the graphics driver, only the first 4 bytes are
	 * used.  They are used as a pointer to another structure.
	 * To help code readability, the pointer will be extracted as a generic 'void *' and then cast as
	 * appropriate.
	 */

	genericPtr = (void *) *((UInt32 *) &(pb->csParam[0]));

	Trace(DriverControlCmd);
	
	switch( pb->csCode ) {
	case cscReset:			// Old obsolete call..return a 'controlErr'
		return controlErr;
		break;
			
	case cscKillIO:			// Old obsolete call..do nothing
		return noErr;

	case cscSetMode:
		status = GraphicsCoreSetMode((VDPageInfo *) genericPtr);
		break;
			
	case cscSetEntries:
		status = GraphicsCoreSetEntries((VDSetEntryRecord *) genericPtr);
		// if ((status == noErr)&&(GLOBAL.qdDeskServiceCreated)&&(ioCommandKind == kSynchronousIOCommandKind))
		// VSLWaitOnInterruptService(GLOBAL.qdVBLInterrupt, 1000);
		break;
			
	case cscSetGamma:
		status = GraphicsCoreSetGamma((VDGammaRecord *) genericPtr);
		break;
			
	case cscGrayPage:
		status = GraphicsCoreGrayPage((VDPageInfo *) genericPtr);
		break;
			
	case cscSetGray:
		status = GraphicsCoreSetGray((VDGrayRecord *) genericPtr);
		break;
			
	case cscSetInterrupt:
		status = GraphicsCoreSetInterrupt((VDFlagRecord *) genericPtr);
		break;
			
	case cscDirectSetEntries:
		status = GraphicsCoreDirectSetEntries((VDSetEntryRecord *) genericPtr);
		break;
			
	case cscSetDefaultMode:
		return controlErr;
		
	case cscSwitchMode:
		status = GraphicsCoreSwitchMode((VDSwitchInfoRec *) genericPtr);
		break;
		
	case cscSetSync:
		status = GraphicsCoreSetSync((VDSyncInfoRec *) genericPtr);
		break;
		
	case cscSavePreferredConfiguration:
		status = GraphicsCoreSetPreferredConfiguration((VDSwitchInfoRec *) genericPtr);
		break;
		
	case cscSetHardwareCursor:
		status = GraphicsCoreSetHardwareCursor((VDSetHardwareCursorRec *) genericPtr);
		break;
		
	case cscDrawHardwareCursor:
		status = GraphicsCoreDrawHardwareCursor((VDDrawHardwareCursorRec *) genericPtr);
		break;
	case cscSetPowerState:
		status = GraphicsCoreSetPowerState((VDPowerStateRec *) genericPtr);
		break;	
	default:
		break;
	}
	if (status)
		status = paramErr;

	return status;
}

/*
 * DriverStatusCmd
 *
 * Process a PBStatus command. We support the driver gestalt call and our private
 * debugging commands.
 */
OSStatus
DriverStatusCmd( IOCommandID ioCommandID, IOCommandKind ioCommandKind, CntrlParam *pb )
{
	OSStatus status;
	void *genericPtr;

	/* The 'csParam' field of the 'CntrlParam' stucture is defined as 'short csParam[11]'.  This is
	 * meant for 'operation defined parameters.' For the graphics driver, only the first 4 bytes are
	 * used.  They are used as a pointer to another structure.
	 * To help code readability, the pointer will be extracted as a generic 'void *' and then cast as
	 * appropriate.
	 */

	genericPtr = (void *) *((UInt32 *) &(pb->csParam[0]));

	Trace(DriverStatusCmd);
	lprintf("csCode=%d\n", pb->csCode);
	switch( pb->csCode ) {
	case cscGetMode:
		status = GraphicsCoreGetMode((VDPageInfo *) genericPtr);
		break;
		
	case cscGetEntries:
		status = GraphicsCoreGetEntries((VDSetEntryRecord *) genericPtr);
		break;
		
	case cscGetPages:
		status = GraphicsCoreGetPages((VDPageInfo *) genericPtr);
		break;
		
	case cscGetBaseAddr:
		status = GraphicsCoreGetBaseAddress((VDPageInfo *) genericPtr);
		break;
		
	case cscGetGray:
		status = GraphicsCoreGetGray((VDGrayRecord *) genericPtr);
		break;
		
	case cscGetInterrupt:
		status = GraphicsCoreGetInterrupt((VDFlagRecord *) genericPtr);
		break;
		
	case cscGetGamma:
		status = GraphicsCoreGetGamma((VDGammaRecord *) genericPtr);
		break;
		
	case cscGetDefaultMode:
		status = statusErr;
		break;
		
	case cscGetCurMode:
		status = GraphicsCoreGetCurrentMode((VDSwitchInfoRec *) genericPtr);
		break;
		
	case cscGetSync:
		status = GraphicsCoreGetSync((VDSyncInfoRec *) genericPtr);
		break;
		
	case cscGetConnection:
		status = GraphicsCoreGetConnection((VDDisplayConnectInfoRec *) genericPtr);
		break;
		
	case cscGetModeTiming:
		status = GraphicsCoreGetModeTiming((VDTimingInfoRec *) genericPtr);
		break;
		
	case cscGetPreferredConfiguration:
		status = GraphicsCoreGetPreferredConfiguration((VDSwitchInfoRec *) genericPtr);
		break;
		
	case cscGetNextResolution:
		status = GraphicsCoreGetNextResolution((VDResolutionInfoRec *) genericPtr);
		break;
		
	case cscGetVideoParameters:
		status = GraphicsCoreGetVideoParams((VDVideoParametersInfoRec *) genericPtr);
		break;
		
	case cscGetGammaInfoList:
		status = GraphicsCoreGetGammaInfoList((VDGetGammaListRec *) genericPtr);
		break;
		
	case cscRetrieveGammaTable:
		status = GraphicsCoreRetrieveGammaTable((VDRetrieveGammaRec *) genericPtr);
		break;
		
	case cscSupportsHardwareCursor:
		status = GraphicsCoreSupportsHardwareCursor((VDSupportsHardwareCursorRec *) genericPtr);
		break;
		
	case cscGetHardwareCursorDrawState:
		status = GraphicsCoreGetHardwareCursorDrawState((VDHardwareCursorDrawStateRec *) genericPtr);
		break;
		
	case kDriverGestaltCode:
		status = DriverGestaltHandler(pb);
		break;
		
	case cscGetPowerState:
		status = GraphicsCoreGetPowerState((VDPowerStateRec *) genericPtr);
		break;
	case cscGetClutBehavior:
		*(VDClutBehaviorPtr)genericPtr = kSetClutAtSetEntries;
		status = noErr;
		break;
	default:
		return statusErr;
	}
	if (status)
		status = paramErr;

	return status;
}

/*
 * DriverKillIOCmd stops all I/O for this chip. It's a big hammer, use it wisely.
 * This will need revision when we support concurrent I/O as we must stop all
 * pending requests.
 */
OSStatus
DriverKillIOCmd( ParmBlkPtr pb )
{
#define REQUEST	(GLOBAL.perRequestData)

	Trace(DriverKillIOCmd);
	return noErr;
#undef REQUEST
}

/*
 * DriverReadCmd
 *
 * The caller passes the data buffer and buffer length in the IOParam record and
 * a pointer to a SCSI NCRSCSIParam in the ioMisc field.
 */
OSStatus
DriverReadCmd( AddressSpaceID addressSpaceID, IOCommandID ioCommandID,
	       IOCommandKind ioCommandKind, ParmBlkPtr pb )
{
	Trace(DriverReadCmd);
	return paramErr;
}


/*
 * DriverWriteCmd
 *
 * The caller passes the data buffer and buffer length in the IOParam record and
 * a pointer to a SCSI NCRSCSIParam in the ioMisc field.
 */
OSStatus
DriverWriteCmd( AddressSpaceID addressSpaceID, IOCommandID ioCommandID,
		IOCommandKind ioCommandKind, ParmBlkPtr pb )
{
	Trace(DriverWriteCmd);
	return paramErr;
}

/*
 * DriverCloseCmd does nothing..
 */
OSStatus
DriverCloseCmd(	ParmBlkPtr pb )
{
	Trace(DriverCloseCmd);
	
	if( !GLOBAL.openCount )
		return notOpenErr;
		
	GLOBAL.openCount--;

	if (!GLOBAL.openCount)
		QemuVga_Close();

	return noErr;
}

/*
 * DriverOpenCmd does nothing: remember that many applications will open a device, but
 * never close it..
 */
OSStatus
DriverOpenCmd( AddressSpaceID addressSpaceID, ParmBlkPtr pb )
{
	Trace(DriverOpenCmd);
	
	GLOBAL.openCount++;
	if (GLOBAL.openCount == 1)
		QemuVga_Open();

	return noErr;
}