Copy wininet to branch
[reactos.git] / reactos / drivers / bus / serenum / pdo.c
1 /* $Id:
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS Serial enumerator driver
5 * FILE: drivers/bus/serenum/pdo.c
6 * PURPOSE: IRP_MJ_PNP operations for PDOs
7 *
8 * PROGRAMMERS: Hervé Poussineau (hpoussin@reactos.com)
9 */
10
11 #define NDEBUG
12 #include "serenum.h"
13
14 static NTSTATUS
15 SerenumPdoStartDevice(
16 IN PDEVICE_OBJECT DeviceObject)
17 {
18 PPDO_DEVICE_EXTENSION DeviceExtension;
19
20 DeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
21
22 ASSERT(DeviceExtension->Common.PnpState == dsStopped);
23
24 DeviceExtension->Common.PnpState = dsStarted;
25 return STATUS_SUCCESS;
26 }
27
28 static NTSTATUS
29 SerenumPdoQueryId(
30 IN PDEVICE_OBJECT DeviceObject,
31 IN PIRP Irp,
32 OUT ULONG_PTR* Information)
33 {
34 PPDO_DEVICE_EXTENSION DeviceExtension;
35 ULONG IdType;
36 PUNICODE_STRING SourceString;
37 UNICODE_STRING String;
38 NTSTATUS Status;
39
40 IdType = IoGetCurrentIrpStackLocation(Irp)->Parameters.QueryId.IdType;
41 DeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
42 RtlInitUnicodeString(&String, NULL);
43
44 switch (IdType)
45 {
46 case BusQueryDeviceID:
47 {
48 DPRINT("Serenum: IRP_MJ_PNP / IRP_MN_QUERY_ID / BusQueryDeviceID\n");
49 SourceString = &DeviceExtension->DeviceId;
50 break;
51 }
52 case BusQueryHardwareIDs:
53 {
54 DPRINT("Serenum: IRP_MJ_PNP / IRP_MN_QUERY_ID / BusQueryHardwareIDs\n");
55 SourceString = &DeviceExtension->HardwareIds;
56 break;
57 }
58 case BusQueryCompatibleIDs:
59 DPRINT("Serenum: IRP_MJ_PNP / IRP_MN_QUERY_ID / BusQueryCompatibleIDs\n");
60 SourceString = &DeviceExtension->CompatibleIds;
61 break;
62 case BusQueryInstanceID:
63 {
64 /* We don't have any instance id to report, and
65 * this query is optional, so ignore it.
66 */
67 *Information = Irp->IoStatus.Information;
68 return Irp->IoStatus.Status;
69 }
70 default:
71 DPRINT1("Serenum: IRP_MJ_PNP / IRP_MN_QUERY_ID / unknown query id type 0x%lx\n", IdType);
72 return STATUS_NOT_SUPPORTED;
73 }
74
75 Status = SerenumDuplicateUnicodeString(
76 &String,
77 SourceString,
78 PagedPool);
79 *Information = (ULONG_PTR)String.Buffer;
80 return Status;
81 }
82
83 static NTSTATUS
84 SerenumPdoQueryDeviceRelations(
85 IN PDEVICE_OBJECT DeviceObject,
86 OUT PDEVICE_RELATIONS* pDeviceRelations)
87 {
88 PFDO_DEVICE_EXTENSION DeviceExtension;
89 PDEVICE_RELATIONS DeviceRelations;
90
91 DeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
92 ASSERT(DeviceExtension->Common.IsFDO);
93
94 DeviceRelations = (PDEVICE_RELATIONS)ExAllocatePoolWithTag(
95 PagedPool,
96 sizeof(DEVICE_RELATIONS),
97 SERENUM_TAG);
98 if (!DeviceRelations)
99 return STATUS_INSUFFICIENT_RESOURCES;
100
101 ObReferenceObject(DeviceObject);
102 DeviceRelations->Objects[0] = DeviceObject;
103
104 *pDeviceRelations = DeviceRelations;
105 return STATUS_SUCCESS;
106 }
107
108 NTSTATUS
109 SerenumPdoPnp(
110 IN PDEVICE_OBJECT DeviceObject,
111 IN PIRP Irp)
112 {
113 ULONG MinorFunction;
114 PIO_STACK_LOCATION Stack;
115 ULONG_PTR Information = 0;
116 NTSTATUS Status;
117
118 Stack = IoGetCurrentIrpStackLocation(Irp);
119 MinorFunction = Stack->MinorFunction;
120
121 switch (MinorFunction)
122 {
123 /* FIXME: do all these minor functions
124 IRP_MN_QUERY_REMOVE_DEVICE 0x1
125 IRP_MN_REMOVE_DEVICE 0x2
126 IRP_MN_CANCEL_REMOVE_DEVICE 0x3
127 IRP_MN_STOP_DEVICE 0x4
128 IRP_MN_QUERY_STOP_DEVICE 0x5
129 IRP_MN_CANCEL_STOP_DEVICE 0x6
130 IRP_MN_QUERY_DEVICE_RELATIONS / EjectionRelations (optional) 0x7
131 IRP_MN_QUERY_INTERFACE (required or optional) 0x8
132 IRP_MN_READ_CONFIG (required or optional) 0xf
133 IRP_MN_WRITE_CONFIG (required or optional) 0x10
134 IRP_MN_EJECT (required or optional) 0x11
135 IRP_MN_SET_LOCK (required or optional) 0x12
136 IRP_MN_QUERY_ID / BusQueryDeviceID 0x13
137 IRP_MN_QUERY_ID / BusQueryCompatibleIDs (optional) 0x13
138 IRP_MN_QUERY_ID / BusQueryInstanceID (optional) 0x13
139 IRP_MN_QUERY_PNP_DEVICE_STATE (optional) 0x14
140 IRP_MN_DEVICE_USAGE_NOTIFICATION (required or optional) 0x16
141 IRP_MN_SURPRISE_REMOVAL 0x17
142 */
143 case IRP_MN_START_DEVICE: /* 0x0 */
144 {
145 DPRINT("Serenum: IRP_MJ_PNP / IRP_MN_START_DEVICE\n");
146 Status = SerenumPdoStartDevice(DeviceObject);
147 break;
148 }
149 case IRP_MN_QUERY_DEVICE_RELATIONS: /* 0x7 */
150 {
151 switch (Stack->Parameters.QueryDeviceRelations.Type)
152 {
153 case RemovalRelations:
154 {
155 return ForwardIrpToAttachedFdoAndForget(DeviceObject, Irp);
156 }
157 case TargetDeviceRelation:
158 {
159 PDEVICE_RELATIONS DeviceRelations;
160 DPRINT("Serenum: IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / TargetDeviceRelation\n");
161 Status = SerenumPdoQueryDeviceRelations(DeviceObject, &DeviceRelations);
162 Information = (ULONG_PTR)DeviceRelations;
163 break;
164 }
165 default:
166 {
167 DPRINT1("Serenum: IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / Unknown type 0x%lx\n",
168 Stack->Parameters.QueryDeviceRelations.Type);
169 Status = STATUS_NOT_IMPLEMENTED;
170 break;
171 }
172 }
173 break;
174 }
175 case IRP_MN_QUERY_CAPABILITIES: /* 0x9 */
176 {
177 PDEVICE_CAPABILITIES DeviceCapabilities;
178 ULONG i;
179 DPRINT("Serenum: IRP_MJ_PNP / IRP_MN_QUERY_CAPABILITIES\n");
180
181 DeviceCapabilities = (PDEVICE_CAPABILITIES)Stack->Parameters.DeviceCapabilities.Capabilities;
182 /* FIXME: capabilities can change with connected device */
183 DeviceCapabilities->LockSupported = FALSE;
184 DeviceCapabilities->EjectSupported = FALSE;
185 DeviceCapabilities->Removable = TRUE;
186 DeviceCapabilities->DockDevice = FALSE;
187 DeviceCapabilities->UniqueID = FALSE;
188 DeviceCapabilities->SilentInstall = FALSE;
189 DeviceCapabilities->RawDeviceOK = TRUE;
190 DeviceCapabilities->SurpriseRemovalOK = TRUE;
191 DeviceCapabilities->HardwareDisabled = FALSE; /* FIXME */
192 //DeviceCapabilities->NoDisplayInUI = FALSE; /* FIXME */
193 DeviceCapabilities->DeviceState[0] = PowerDeviceD0; /* FIXME */
194 for (i = 0; i < PowerSystemMaximum; i++)
195 DeviceCapabilities->DeviceState[i] = PowerDeviceD3; /* FIXME */
196 //DeviceCapabilities->DeviceWake = PowerDeviceUndefined; /* FIXME */
197 DeviceCapabilities->D1Latency = 0; /* FIXME */
198 DeviceCapabilities->D2Latency = 0; /* FIXME */
199 DeviceCapabilities->D3Latency = 0; /* FIXME */
200 Status = STATUS_SUCCESS;
201 break;
202 }
203 case IRP_MN_QUERY_RESOURCES: /* 0xa */
204 {
205 DPRINT("Serenum: IRP_MJ_PNP / IRP_MN_QUERY_RESOURCES\n");
206 /* Serial devices don't need resources, except the ones of
207 * the serial port. This PDO is the serial device PDO, so
208 * report no resource by not changing Information and
209 * Status
210 */
211 Information = Irp->IoStatus.Information;
212 Status = Irp->IoStatus.Status;
213 break;
214 }
215 case IRP_MN_QUERY_RESOURCE_REQUIREMENTS: /* 0xb */
216 {
217 DPRINT("Serenum: IRP_MJ_PNP / IRP_MN_QUERY_RESOURCE_REQUIREMENTS\n");
218 /* Serial devices don't need resources, except the ones of
219 * the serial port. This PDO is the serial device PDO, so
220 * report no resource by not changing Information and
221 * Status
222 */
223 Information = Irp->IoStatus.Information;
224 Status = Irp->IoStatus.Status;
225 break;
226 }
227 case IRP_MN_QUERY_DEVICE_TEXT: /* 0xc */
228 {
229 switch (Stack->Parameters.QueryDeviceText.DeviceTextType)
230 {
231 case DeviceTextDescription:
232 {
233 PUNICODE_STRING Source;
234 PWSTR Description;
235 DPRINT("Serenum: IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_TEXT / DeviceTextDescription\n");
236
237 Source = &((PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->DeviceDescription;
238 Description = ExAllocatePool(PagedPool, Source->Length + sizeof(WCHAR));
239 if (!Description)
240 Status = STATUS_INSUFFICIENT_RESOURCES;
241 else
242 {
243 RtlCopyMemory(Description, Source->Buffer, Source->Length);
244 Description[Source->Length / sizeof(WCHAR)] = L'\0';
245 Information = (ULONG_PTR)Description;
246 Status = STATUS_SUCCESS;
247 }
248 break;
249 }
250 case DeviceTextLocationInformation:
251 {
252 /* We don't have any text location to report,
253 * and this query is optional, so ignore it.
254 */
255 Information = Irp->IoStatus.Information;
256 Status = Irp->IoStatus.Status;
257 break;
258 }
259 default:
260 {
261 DPRINT1("Serenum: IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_TEXT / unknown type 0x%lx\n",
262 Stack->Parameters.QueryDeviceText.DeviceTextType);
263 Status = STATUS_NOT_SUPPORTED;
264 }
265 }
266 break;
267 }
268 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS: /* 0xd */
269 {
270 return ForwardIrpToAttachedFdoAndForget(DeviceObject, Irp);
271 }
272 case IRP_MN_QUERY_ID: /* 0x13 */
273 {
274 Status = SerenumPdoQueryId(DeviceObject, Irp, &Information);
275 break;
276 }
277 case IRP_MN_QUERY_BUS_INFORMATION: /* 0x15 */
278 {
279 PPNP_BUS_INFORMATION BusInfo;
280 DPRINT("Serenum: IRP_MJ_PNP / IRP_MN_QUERY_BUS_INFORMATION\n");
281
282 BusInfo = (PPNP_BUS_INFORMATION)ExAllocatePool(PagedPool, sizeof(PNP_BUS_INFORMATION));
283 if (!BusInfo)
284 Status = STATUS_INSUFFICIENT_RESOURCES;
285 else
286 {
287 BusInfo->BusTypeGuid = GUID_BUS_TYPE_SERENUM;
288 /* FIXME: real value should be PNPBus, but PNPBus seems to be
289 * the only value in INTERFACE_TYPE enum that doesn't work...
290 */
291 BusInfo->LegacyBusType = PNPISABus;
292 /* We're the only serial bus enumerator on the computer */
293 BusInfo->BusNumber = 0;
294 Information = (ULONG_PTR)BusInfo;
295 Status = STATUS_SUCCESS;
296 }
297 break;
298 }
299 default:
300 {
301 /* We can't forward request to the lower driver, because
302 * we are a Pdo, so we don't have lower driver... */
303 DPRINT1("Serenum: IRP_MJ_PNP / unknown minor function 0x%lx\n", MinorFunction);
304 Information = Irp->IoStatus.Information;
305 Status = Irp->IoStatus.Status;
306 }
307 }
308
309 Irp->IoStatus.Information = Information;
310 Irp->IoStatus.Status = Status;
311 IoCompleteRequest(Irp, IO_NO_INCREMENT);
312 return Status;
313 }