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
/*
Copyright (c) 2006-2008 dogbert <dogber1@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#define PUT_GUIDS_HERE

#include "adapter.hpp"

#ifdef _MSC_VER
#pragma code_seg("PAGE")
#endif

static
NTSTATUS
InstallSubdevice(PDEVICE_OBJECT DeviceObject, PIRP Irp, PWCHAR Name, REFGUID PortClassId, REFGUID MiniportClassId, PFNCREATEINSTANCE MiniportCreate, PUNKNOWN UnknownAdapter, PRESOURCELIST ResourceList, REFGUID PortInterfaceId, PUNKNOWN* OutPortUnknown)
{
	PAGED_CODE();
	DBGPRINT(("InstallSubdevice()"));

	NTSTATUS	ntStatus;
	PPORT	   	Port;
	PMINIPORT   MiniPort;

	ntStatus = PcNewPort(&Port, PortClassId);
	if (NT_SUCCESS(ntStatus)) {
		if (MiniportCreate) {
			ntStatus = MiniportCreate((PUNKNOWN*)&MiniPort, MiniportClassId, NULL, NonPagedPool);
		} else {
			ntStatus = PcNewMiniport(&MiniPort, MiniportClassId);
		}
	}

	if (!NT_SUCCESS(ntStatus)) {
		Port->Release();
		return ntStatus;
	}

	ntStatus = Port->Init(DeviceObject, Irp, MiniPort, UnknownAdapter, ResourceList);
	if (NT_SUCCESS(ntStatus)) {
		ntStatus = PcRegisterSubdevice(DeviceObject, Name, Port);

		if (OutPortUnknown && NT_SUCCESS (ntStatus)) {
			ntStatus = Port->QueryInterface(IID_IUnknown, (PVOID *)OutPortUnknown);
		}
	}

	if (MiniPort) {
		MiniPort->Release();
	}

	if (Port) {
		Port->Release();
	}

	return ntStatus;
}

static
NTSTATUS
ProcessResources(PRESOURCELIST ResourceList, PRESOURCELIST* UartResourceList)
{
	PAGED_CODE();
	ASSERT(ResourceList);
	ASSERT(UartResourceList);
	DBGPRINT(("ProcessResources()"));
	DBGPRINT(("NumberOfPorts: %d, NumberOfInterrupts: %d, NumberOfDmas: %d", ResourceList->NumberOfPorts(), ResourceList->NumberOfInterrupts(), ResourceList->NumberOfDmas()));
#ifdef UART
   	NTSTATUS ntStatus;

	(*UartResourceList) = NULL;
#endif

	if ((ResourceList->NumberOfPorts() == 0) || (ResourceList->NumberOfPorts() > 2) || (ResourceList->NumberOfInterrupts() != 1) || (ResourceList->NumberOfDmas() != 0)) {
		DBGPRINT(("Unexpected configuration"));
		return STATUS_DEVICE_CONFIGURATION_ERROR;
	}

#ifdef UART
	ntStatus = PcNewResourceSublist(UartResourceList, NULL, PagedPool, ResourceList, 2);
	if (NT_SUCCESS(ntStatus)) {
		(*UartResourceList)->AddPortFromParent(ResourceList, 1);
		(*UartResourceList)->AddInterruptFromParent(ResourceList, 0);
	}
#endif

	return STATUS_SUCCESS;
}


NTSTATUS
NTAPI
StartDevice(PDEVICE_OBJECT DeviceObject, PIRP Irp, PRESOURCELIST ResourceList)
{
	PAGED_CODE();
	ASSERT(DeviceObject);
	ASSERT(Irp);
	ASSERT(ResourceList);
	DBGPRINT(("StartDevice()"));

	NTSTATUS ntStatus;
	PPORT    pPort = 0;
#ifdef UART
	ULONG*   MPUBase;
#endif

	ntStatus = PcNewPort(&pPort,CLSID_PortWaveCyclic);
	if (NT_SUCCESS(ntStatus)) {
		// not supported in the first edition of win98
		PPORTEVENTS pPortEvents = 0;
		ntStatus = pPort->QueryInterface(IID_IPortEvents, (PVOID *)&pPortEvents);
		if (!NT_SUCCESS(ntStatus)) {
			DBGPRINT(("ERROR: This driver doesn't work under Win98!"));
			ntStatus = STATUS_UNSUCCESSFUL;<--- ntStatus is assigned
		}
		else
		{
			pPortEvents->Release();
		}
		pPort->Release ();
	} else {
		return ntStatus;
	}

	// resource validation
	PRESOURCELIST UartResourceList = NULL;
	ntStatus = ProcessResources(ResourceList, &UartResourceList);<--- ntStatus is overwritten
	if (!NT_SUCCESS(ntStatus)) {
		DBGPRINT(("ProcessResources() failed"));
		return ntStatus;
	}

	PCMIADAPTER	pCMIAdapter	= NULL;
	PUNKNOWN	pUnknownCommon = NULL;

	// create the CMIAdapter object
	ntStatus = NewCMIAdapter(&pUnknownCommon, IID_ICMIAdapter, NULL, NonPagedPool);
	if (!NT_SUCCESS(ntStatus)) {
		DBGPRINT(("NewCMIAdapter() failed"));
		return ntStatus;
	}

	ntStatus = pUnknownCommon->QueryInterface(IID_ICMIAdapter, (PVOID *)&pCMIAdapter);
	if (!NT_SUCCESS(ntStatus)) {
		DBGPRINT(("QueryInterface() for ICMIAdapter failed"));
		return ntStatus;
	}
	ntStatus = pCMIAdapter->init(ResourceList, DeviceObject);
	if (!NT_SUCCESS(ntStatus)) {
		DBGPRINT(("CMIAdapter->init() failed"));
		return ntStatus;
	}

	ntStatus = PcRegisterAdapterPowerManagement((PUNKNOWN)pCMIAdapter, DeviceObject);<--- ntStatus is assigned

	pUnknownCommon->Release();

	PUNKNOWN unknownWave = NULL;
	PUNKNOWN unknownTopology = NULL;

	// install the topology miniport.
	ntStatus = InstallSubdevice(DeviceObject, Irp, L"Topology", CLSID_PortTopology, CLSID_PortTopology, CreateMiniportTopologyCMI, pCMIAdapter, NULL, GUID_NULL, &unknownTopology);<--- ntStatus is overwritten
	if (!NT_SUCCESS (ntStatus)) {
		DBGPRINT(("Topology miniport installation failed"));
		return ntStatus;
	}

#ifdef UART
	// install the UART miniport - execution order important
	ntStatus = STATUS_UNSUCCESSFUL;
	MPUBase = 0;
	for (int i=0;i<ResourceList->NumberOfPorts();i++) {
		if (ResourceList->FindTranslatedPort(i)->u.Port.Length == 2) {
			MPUBase = (UInt32*)ResourceList->FindTranslatedPort(i)->u.Port.Start.QuadPart;
		}
	}
	if (MPUBase != 0) {
		ntStatus = pCMIAdapter->activateMPU(MPUBase);
		if (NT_SUCCESS(ntStatus)) {
			ntStatus = InstallSubdevice(DeviceObject, Irp, L"Uart", CLSID_PortDMus, CLSID_MiniportDriverDMusUART, NULL, pCMIAdapter->getInterruptSync(), UartResourceList, IID_IPortDMus, NULL);
		}
	}
	if (!NT_SUCCESS(ntStatus)) {
		MPUBase = 0;
		pCMIAdapter->activateMPU(0);
		DBGPRINT(("UART miniport installation failed"));
	}
	if (UartResourceList) {
		UartResourceList->Release();
	}
#endif

	// install the wave miniport - the order matters here
#ifdef WAVERT
	ntStatus = InstallSubdevice(DeviceObject, Irp, L"Wave", CLSID_PortWaveRT, CLSID_PortWaveRT, CreateMiniportWaveCMI, pCMIAdapter, ResourceList, IID_IPortWaveRT, &unknownWave);
#else
	ntStatus = InstallSubdevice(DeviceObject, Irp, L"Wave", CLSID_PortWaveCyclic, CLSID_PortWaveCyclic, CreateMiniportWaveCMI, pCMIAdapter, ResourceList, IID_IPortWaveCyclic, &unknownWave);
#endif
	if (!NT_SUCCESS(ntStatus)) {
		DBGPRINT(("Wave miniport installation failed"));
		return ntStatus;
	}

	// connect wave and topology pins
	ntStatus = PcRegisterPhysicalConnection(DeviceObject, unknownWave, PIN_WAVE_RENDER_SOURCE, unknownTopology, PIN_WAVEOUT_SOURCE);
	if (!NT_SUCCESS(ntStatus)) {
		DBGPRINT(("Cannot connect topology and wave miniport (render)!"));
		return ntStatus;
	}
	ntStatus = PcRegisterPhysicalConnection(DeviceObject, unknownTopology, PIN_WAVEIN_DEST, unknownWave, PIN_WAVE_CAPTURE_SOURCE);
	if (!NT_SUCCESS(ntStatus)) {
		DBGPRINT(("Cannot connect topology and wave miniport (capture)!"));
		return ntStatus;
	}
	if (!IoIsWdmVersionAvailable(6,0)) {
		// this shit fixes the fucking XP mixer and breaks the vista mixer, so we have to check for vista here
		ntStatus = PcRegisterPhysicalConnection(DeviceObject, unknownWave, PIN_WAVE_AC3_RENDER_SOURCE, unknownTopology, PIN_SPDIF_AC3_SOURCE);
		if (!NT_SUCCESS(ntStatus)) {
			DBGPRINT(("Cannot connect topology and wave miniport (ac3)!"));
		}
	}

	// clean up
	if (pCMIAdapter) {
		pCMIAdapter->Release();
	}
	if (unknownTopology) {
		unknownTopology->Release();
	}
	if (unknownWave) {
		unknownWave->Release();
	}

	return ntStatus;
}

extern "C" NTSTATUS NTAPI AddDevice(PDRIVER_OBJECT DriverObject, PDEVICE_OBJECT PhysicalDeviceObject)
{
	PAGED_CODE();
	DBGPRINT(("AddDevice()"));

	return PcAddAdapterDevice(DriverObject, PhysicalDeviceObject, StartDevice, MAX_MINIPORTS, 0);
}

static
bool
CopyResourceDescriptor(PIO_RESOURCE_DESCRIPTOR pInResDescriptor, PIO_RESOURCE_DESCRIPTOR pOutResDescriptor)
{
	PAGED_CODE();
	ASSERT(pInResDescriptor);
	ASSERT(pOutResDescriptor);
	DBGPRINT(("CopyResourceDescriptor()"));

#if 0
	RtlCopyMemory(pOutResDescriptor, pInResDescriptor, sizeof(IO_RESOURCE_DESCRIPTOR));
#else
	pOutResDescriptor->Type             = pInResDescriptor->Type;
	pOutResDescriptor->ShareDisposition = pInResDescriptor->ShareDisposition;
	pOutResDescriptor->Flags            = pInResDescriptor->Flags;
	pOutResDescriptor->Option           = pInResDescriptor->Option;

	switch (pInResDescriptor->Type) {
		case CmResourceTypePort:
		case CmResourceTypePort | CmResourceTypeNonArbitrated:  // huh?
/*			// filter crap
			if ((pInResDescriptor->u.Port.Length == 0) ||
			    ( (pInResDescriptor->u.Port.MinimumAddress.HighPart == pInResDescriptor->u.Port.MaximumAddress.HighPart) && (pInResDescriptor->u.Port.MinimumAddress.LowPart == pInResDescriptor->u.Port.MaximumAddress.LowPart) ) ) {
				return FALSE;
			}
*/			pOutResDescriptor->u.Port.MinimumAddress = pInResDescriptor->u.Port.MinimumAddress;
			pOutResDescriptor->u.Port.MaximumAddress = pInResDescriptor->u.Port.MaximumAddress;
			pOutResDescriptor->u.Port.Length         = pInResDescriptor->u.Port.Length;
			pOutResDescriptor->u.Port.Alignment	     = pInResDescriptor->u.Port.Alignment;
			DBGPRINT((" Port: min %08x.%08x max %08x.%08x, Length: %x, Option: %x", pOutResDescriptor->u.Port.MinimumAddress.HighPart, pOutResDescriptor->u.Port.MinimumAddress.LowPart,
			                                                            pOutResDescriptor->u.Port.MaximumAddress.HighPart, pOutResDescriptor->u.Port.MaximumAddress.LowPart,
			                                                            pOutResDescriptor->u.Port.Length, pOutResDescriptor->Option));
			break;
		case CmResourceTypeInterrupt:
			pOutResDescriptor->u.Interrupt.MinimumVector = pInResDescriptor->u.Interrupt.MinimumVector;
			pOutResDescriptor->u.Interrupt.MaximumVector = pInResDescriptor->u.Interrupt.MaximumVector;
			DBGPRINT((" IRQ:  min %x max %x, Option: %d", pOutResDescriptor->u.Interrupt.MinimumVector, pOutResDescriptor->u.Interrupt.MaximumVector, pOutResDescriptor->Option));
			break;
		default:
			return FALSE;
	}
	return TRUE;
#endif
}

extern "C" NTSTATUS NTAPI AdapterDispatchPnp(PDEVICE_OBJECT pDeviceObject, PIRP pIrp)
{
	PAGED_CODE();
	ASSERT(pDeviceObject);
	ASSERT(pIrp);
	DBGPRINT(("AdapterDispatchPnp()"));

	NTSTATUS                       ntStatus = STATUS_SUCCESS;
	ULONG                          resourceListSize;
	PIO_RESOURCE_REQUIREMENTS_LIST resourceList, list;
	PIO_RESOURCE_DESCRIPTOR        descriptor;
	PIO_STACK_LOCATION             pIrpStack = IoGetCurrentIrpStackLocation(pIrp);

	if (pIrpStack->MinorFunction == IRP_MN_FILTER_RESOURCE_REQUIREMENTS) {
		DBGPRINT(("[AdapterDispatchPnp] - IRP_MN_FILTER_RESOURCE_REQUIREMENTS"));

		list = (PIO_RESOURCE_REQUIREMENTS_LIST)pIrp->IoStatus.Information;

		// IO_RESOURCE_REQUIREMENTS_LIST has 1 IO_RESOURCE_LIST, IO_RESOURCE_LIST has 1 IO_RESOURCE_DESCRIPTOR and we want 2 more
		resourceListSize = sizeof(IO_RESOURCE_REQUIREMENTS_LIST) + sizeof(IO_RESOURCE_DESCRIPTOR)*(list->List[0].Count+2) ;
		resourceList = (PIO_RESOURCE_REQUIREMENTS_LIST)ExAllocatePoolWithTag(PagedPool, resourceListSize, 'LRDV');

		if (!resourceList) {
			ntStatus = STATUS_INSUFFICIENT_RESOURCES;
			return ntStatus;
		}


		RtlZeroMemory(resourceList, resourceListSize);

		// initialize the list header
		resourceList->AlternativeLists = 1; // number of IO_RESOURCE_LISTs
		resourceList->ListSize = resourceListSize;

		resourceList->List[0].Version  = 1;
		resourceList->List[0].Revision = 1;
		resourceList->List[0].Count    = 0;

		// copy the resources which have already been assigned
		for (unsigned int i=0;i<list->List[0].Count;i++) {
			if (CopyResourceDescriptor(&list->List[0].Descriptors[i], &resourceList->List[0].Descriptors[resourceList->List[0].Count])) {
				resourceList->List[0].Count++;
			}
		}
		ExFreePool(list);

		// an additional port for mpu401
		resourceList->List[0].Count++;
		descriptor = &resourceList->List[0].Descriptors[resourceList->List[0].Count-1];
		descriptor->Option                = IO_RESOURCE_PREFERRED;
		descriptor->Type                  = CmResourceTypePort;
		descriptor->ShareDisposition      = CmResourceShareDeviceExclusive;
		descriptor->Flags                 = CM_RESOURCE_PORT_IO;
		descriptor->u.Port.MinimumAddress.LowPart  = 0x300;
		descriptor->u.Port.MinimumAddress.HighPart = 0;
		descriptor->u.Port.MaximumAddress.LowPart  = 0x330;
		descriptor->u.Port.MaximumAddress.HighPart = 0;
		descriptor->u.Port.Length         = 2;
		descriptor->u.Port.Alignment      = 0x10;

		// mpu401 port should be optional. yes, this is severely braindamaged.
		resourceList->List[0].Count++;
		descriptor = &resourceList->List[0].Descriptors[resourceList->List[0].Count-1];
		descriptor->Option                = IO_RESOURCE_ALTERNATIVE;
		descriptor->Type                  = CmResourceTypePort;
		descriptor->ShareDisposition      = CmResourceShareDeviceExclusive;
		descriptor->Flags                 = CM_RESOURCE_PORT_IO;
		descriptor->u.Port.MinimumAddress.LowPart  = 0x0;
		descriptor->u.Port.MinimumAddress.HighPart = 0;
		descriptor->u.Port.MaximumAddress.LowPart  = 0xFFFF;
		descriptor->u.Port.MaximumAddress.HighPart = 0;
		descriptor->u.Port.Length         = 1;
		descriptor->u.Port.Alignment      = 0x10;

		DBGPRINT(("number of resource list descriptors: %d", resourceList->List[0].Count));

		pIrp->IoStatus.Information = (ULONG_PTR)resourceList;

		// set the return status
		pIrp->IoStatus.Status = ntStatus;
	}

	// Pass the IRPs on to PortCls
	ntStatus = PcDispatchIrp(pDeviceObject, pIrp);

	return ntStatus;
}

extern "C"
NTSTATUS
NTAPI
DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPathName)
{
	PAGED_CODE();
	DBGPRINT(("DriverEntry()"));

	NTSTATUS ntStatus;

	//bind the adapter driver to the portclass driver
	ntStatus = PcInitializeAdapterDriver(DriverObject, RegistryPathName, AddDevice);
#ifdef UART
	if(NT_SUCCESS(ntStatus)) {
		DriverObject->MajorFunction[IRP_MJ_PNP] = AdapterDispatchPnp;
	}
#endif
#ifdef WAVERT
	if (!IoIsWdmVersionAvailable(6,0)) {
		ntStatus = STATUS_UNSUCCESSFUL;
	}
#endif

	return ntStatus;
}

#ifdef _MSC_VER
#pragma code_seg()
#endif
int __cdecl _purecall (void)
{
	return 0;
}