-
Notifications
You must be signed in to change notification settings - Fork 5
/
pcdevice.cpp
326 lines (267 loc) · 9.9 KB
/
pcdevice.cpp
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
/* 1010 PCI/PCIe Audio Driver
Copyright (c) Eugene Gavrilov, 2002-2016
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "common.h"
#include "debug.h"
#include "pcdriver.h"
#include "pcdevice.h"
#include "guids.h"
#include "adapter.h"
#pragma code_seg()
PcDevice *PcDevice::GetObject(DEVICE_OBJECT *fdo)
{
if(fdo)
{
if(fdo->DeviceExtension)
{
PcDevice **extension = (PcDevice **)((PCHAR)fdo->DeviceExtension + PORT_CLASS_DEVICE_EXTENSION_SIZE);
if(extension && *extension)
return *extension;
}
}
return NULL;
}
#pragma code_seg("PAGE")
NTSTATUS PcDevice::Add(PDRIVER_OBJECT DriverObject, PDEVICE_OBJECT PhysicalDeviceObject)
{
PAGED_CODE();
debug("PcDevice::Add: drvo: %p pdo: %p; next: %p\n",DriverObject,PhysicalDeviceObject,DriverObject->DeviceObject);
NTSTATUS ret = PcAddAdapterDevice(DriverObject, PhysicalDeviceObject, PcDevice::Start, MAX_MINIPORTS, sizeof(void *) + PORT_CLASS_DEVICE_EXTENSION_SIZE);
PDEVICE_OBJECT FunctionalDeviceObject=DriverObject->DeviceObject;
while(FunctionalDeviceObject)
{
PcDevice **extension = (PcDevice **)((PCHAR)FunctionalDeviceObject->DeviceExtension + PORT_CLASS_DEVICE_EXTENSION_SIZE);
if(extension && *extension==NULL)
{
PcDevice *device = new (NonPagedPool) PcDevice(FunctionalDeviceObject,PhysicalDeviceObject);
if(device)
{
*extension = device;
break;
}
}
FunctionalDeviceObject=FunctionalDeviceObject->NextDevice;
}
return ret;
}
#pragma code_seg("PAGE")
NTSTATUS PcDevice::InstallSubdevice(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp,IN PWCHAR Name,IN REFGUID PortClassId,IN REFGUID MiniportClassId,
IN PFNCREATEINSTANCE MiniportCreate,IN PUNKNOWN UnknownAdapter,
IN PRESOURCELIST ResourceList,OUT PUNKNOWN *OutPortUnknown OPTIONAL)
{
PAGED_CODE();
ASSERT(DeviceObject);
ASSERT(Irp);
ASSERT(Name);
ASSERT(ResourceList);
PPORT port=NULL;
NTSTATUS ntStatus=PcNewPort(&port,PortClassId);
if (NT_SUCCESS(ntStatus))
{
PUNKNOWN miniport=NULL;
if (MiniportCreate) // a function to create a proprietary miniport
{
ntStatus = MiniportCreate((PUNKNOWN *)&miniport, MiniportClassId, NULL, NonPagedPool);
}
else // Ask PortCls for one of its built-in miniports.
{
ntStatus = PcNewMiniport((PMINIPORT*)&miniport,MiniportClassId);
}
if (NT_SUCCESS(ntStatus))
{
//
// Bind the port, miniport, and resources.
//
ntStatus = port->Init( DeviceObject,Irp,miniport,UnknownAdapter,ResourceList);
if (NT_SUCCESS(ntStatus))
{
//
// Register the subdevice (port/miniport combination).
//
ntStatus = PcRegisterSubdevice( DeviceObject, Name, port );
if ( !NT_SUCCESS(ntStatus) )
{
debug("!! PcDevice::InstallSubdevice: PcRegisterSubdevice failed\n");
}
else
{
if (OutPortUnknown)
{
// FIXME: strange, IID_IUnknown does not work...
NTSTATUS result=port->QueryInterface(IID_IPort,(PVOID *) OutPortUnknown);
if (!NT_SUCCESS(result))
debug("!! PcDevice::InstallSubdevice: failed to query for unknown interface: %p, %p [%x]\n",port,miniport,result);
else
debug("PcDevice::InstallSubdevice: port: %p, mini: %p, out: %p\n",port,miniport,*OutPortUnknown);
}
}
}
else
{
debug("!! PcDevice::InstallSubdevice: port->Init failed [%x]\n",ntStatus);
}
}
else
{
debug("!! PcDevice::InstallSubdevice: PcNewMiniport failed\n");
}
if(miniport)
{
miniport->Release();
miniport=NULL;
}
port->Release();
}
else
{
debug("!! PcDevice::InstallSubdevice: PcNewPort failed\n");
}
return ntStatus;
}
#pragma code_seg("PAGE")
NTSTATUS PcDevice::Start(PDEVICE_OBJECT DeviceObject,PIRP Irp,PRESOURCELIST ResourceList)
{
PAGED_CODE();
PcDevice *device=PcDevice::GetObject(DeviceObject);
if(valid_object(device,PcDevice))
return device->Start(Irp,ResourceList);
debug("!! PcDevice::Start: invalid object\n");
return STATUS_UNSUCCESSFUL;
}
#pragma code_seg("PAGE")
NTSTATUS PcDevice::Start(PIRP Irp,PRESOURCELIST ResourceList)
{
PAGED_CODE();
debug("PcDevice::Start [do: %p]\n",DeviceObject);
PUNKNOWN u_adapter = NULL;
PADAPTERCOMMON padapter = NULL;
// create a new adapter common object
NTSTATUS ntStatus = Adapter::Create( &u_adapter,IID_IAdapterCommon,NULL,NonPagedPool );
if (NT_SUCCESS(ntStatus))
{
ASSERT( u_adapter );
// query for the IAdapterCommon interface
ntStatus = u_adapter->QueryInterface( IID_IAdapterCommon,(PVOID *)&padapter );
if (NT_SUCCESS(ntStatus))
{
// Initialize the object
ntStatus = padapter->Init(ResourceList, this, Irp);
}
else
debug("!! PcDevice::Start: failed to query adapter's interface [%x]\n",ntStatus);
u_adapter->Release();
}
if(padapter)
padapter->Release();
return ntStatus;
}
#pragma code_seg("PAGE")
PcDevice::PcDevice(DEVICE_OBJECT *fdo,DEVICE_OBJECT *pdo)
{
PAGED_CODE();
magic=object_magic;
DeviceObject=fdo;
PhysicalDeviceObject=pdo;
debug("PcDevice::PcDevice(): fdo: %p, pdo: %p [PcDevice: %p]\n",fdo,pdo,this);
}
#pragma code_seg("PAGE")
PcDevice::~PcDevice()
{
PAGED_CODE();
ASSERT(valid_object(this,PcDevice));
DeviceObject=NULL;
PhysicalDeviceObject=NULL;
magic=0;
debug("PcDevice::~PcDevice()\n");
}
#pragma code_seg()
int PcDevice::set_device_param(char *param,int type,void *data,int size)
{
HANDLE reg;
int ret=STATUS_UNSUCCESSFUL;
if(IoOpenDeviceRegistryKey(PhysicalDeviceObject,PLUGPLAY_REGKEY_DEVICE,KEY_SET_VALUE,®)==STATUS_SUCCESS)
{
UNICODE_STRING value_name;
ANSI_STRING ansi_value;
RtlInitAnsiString(&ansi_value,param);
RtlAnsiStringToUnicodeString(&value_name,&ansi_value,TRUE);
// debug("-- set dev param [%s]\n",param);
void *buff=data;
UNICODE_STRING string_data; memset(&string_data,0,sizeof(string_data));
ULONG type_=(ULONG)-1;
switch(type)
{
case CE_PARAM_DWORD: type_=REG_DWORD; break;
case CE_PARAM_BIN: type_=REG_BINARY; break;
case CE_PARAM_CHAR:
type_=REG_SZ;
// transform ansi->unicode
ANSI_STRING value;
RtlInitAnsiString(&value,(PCSZ)data);
RtlAnsiStringToUnicodeString(&string_data,&value,TRUE);
buff=string_data.Buffer;
break;
}
if(type!=(ULONG)-1)
ret=ZwSetValueKey(reg,&value_name,NULL,type_,buff,(ULONG)size);
else
ret=STATUS_INVALID_PARAMETER;
ZwClose(reg);
if(string_data.Buffer) // free
RtlFreeUnicodeString(&string_data);
RtlFreeUnicodeString(&value_name);
return ret;
}
return ret;
}
#pragma code_seg()
int PcDevice::get_device_param(char *param,int /*type*/,void *data,int size,int *out_type,int *out_size)
{
HANDLE reg;
int ret=STATUS_UNSUCCESSFUL;
if(IoOpenDeviceRegistryKey(PhysicalDeviceObject,PLUGPLAY_REGKEY_DEVICE,KEY_QUERY_VALUE,®)==STATUS_SUCCESS)
{
UNICODE_STRING value_name;
ANSI_STRING ansi_value;
RtlInitAnsiString(&ansi_value,param);
RtlAnsiStringToUnicodeString(&value_name,&ansi_value,TRUE);
// debug("-- get dev param [%s]\n",param);
ULONG result_length=0;
ULONG result[40]; // KEY_VALUE_PARTIAL_INFORMATION + 4 -> 16 bytes
ret=ZwQueryValueKey(reg,&value_name,KeyValuePartialInformation,result,sizeof(result),&result_length);
if(ret==STATUS_SUCCESS)
{
switch(result[1])
{
default:
case REG_BINARY: *out_type=CE_PARAM_BIN; break;
case REG_DWORD: *out_type=CE_PARAM_DWORD; break;
case REG_SZ:
case REG_EXPAND_SZ:
*out_type=CE_PARAM_CHAR; break;
}
*out_size=result[2];
if(size>=*out_size)
{
memcpy(data,&result[3],*out_size);
}
}
ZwClose(reg);
RtlFreeUnicodeString(&value_name);
return ret;
}
return ret;
}