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