[bus/pnp] Use LowerDeviceObject in WvBusPnpQueryCapabilities_
[people/sha0/winvblock.git] / src / winvblock / bus / pnp.c
1 /**
2  * Copyright (C) 2009-2010, Shao Miller <shao.miller@yrdsb.edu.on.ca>.
3  * Copyright 2006-2008, V.
4  * For WinAoE contact information, see http://winaoe.org/
5  *
6  * This file is part of WinVBlock, derived from WinAoE.
7  *
8  * WinVBlock is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * WinVBlock is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with WinVBlock.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 /**
23  * @file
24  *
25  * Bus PnP IRP handling.
26  */
27
28 #include <stdio.h>
29 #include <ntddk.h>
30 #include <initguid.h>
31
32 #include "winvblock.h"
33 #include "wv_stdlib.h"
34 #include "portable.h"
35 #include "driver.h"
36 #include "device.h"
37 #include "disk.h"
38 #include "bus.h"
39 #include "debug.h"
40 #include "probe.h"
41
42 static NTSTATUS STDCALL WvBusPnpIoCompletion_(
43     IN PDEVICE_OBJECT dev_obj,
44     IN PIRP irp,
45     IN PKEVENT event
46   ) {
47     KeSetEvent(event, 0, FALSE);
48     return STATUS_MORE_PROCESSING_REQUIRED;
49   }
50
51 static NTSTATUS STDCALL WvBusPnpStartDev_(IN WV_SP_BUS_T bus, IN PIRP irp) {
52     NTSTATUS status;
53     KEVENT event;
54     PDEVICE_OBJECT lower = bus->LowerDeviceObject;
55
56     if (!lower)
57       return driver__complete_irp(irp, 0, STATUS_SUCCESS);
58     KeInitializeEvent(&event, NotificationEvent, FALSE);
59     IoCopyCurrentIrpStackLocationToNext(irp);
60     IoSetCompletionRoutine(
61         irp,
62         (PIO_COMPLETION_ROUTINE) WvBusPnpIoCompletion_,
63         (void *) &event,
64         TRUE,
65         TRUE,
66         TRUE
67       );
68     status = IoCallDriver(lower, irp);
69     if (status == STATUS_PENDING) {
70         DBG("Locked\n");
71         KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
72       }
73     if (NT_SUCCESS(status = irp->IoStatus.Status)) {
74         bus->Dev.OldState = bus->Dev.State;
75         bus->Dev.State = WvDevStateStarted;
76       }
77     return driver__complete_irp(
78         irp,
79         irp->IoStatus.Information,
80         STATUS_SUCCESS
81       );
82   }
83
84 static NTSTATUS STDCALL WvBusPnpRemoveDev_(IN WV_SP_BUS_T bus, IN PIRP irp) {
85     PIO_STACK_LOCATION io_stack_loc = IoGetCurrentIrpStackLocation(irp);
86     NTSTATUS status;
87     PDEVICE_OBJECT lower;
88     WV_SP_DEV_T dev = &bus->Dev;
89     WV_SP_DEV_T walker, next;
90     PLIST_ENTRY node_link;
91
92     if (!(io_stack_loc->Control & SL_PENDING_RETURNED)) {
93         /* Enqueue the IRP. */
94         status = WvBusEnqueueIrp(bus, irp);
95         if (status != STATUS_PENDING)
96           /* Problem. */
97           return driver__complete_irp(irp, 0, status);
98         /* Ok. */
99         return status;
100       }
101     /* If we get here, we should be called by WvBusProcessWorkItems() */
102     status = STATUS_SUCCESS;
103     lower = bus->LowerDeviceObject;
104     dev->OldState = dev->State;
105     dev->State = WvDevStateDeleted;
106     /* Pass the IRP on to any lower DEVICE_OBJECT */
107     if (lower) {
108         irp->IoStatus.Information = 0;
109         irp->IoStatus.Status = STATUS_SUCCESS;
110         IoSkipCurrentIrpStackLocation(irp);
111         status = IoCallDriver(lower, irp);
112       }
113     /* Remove all children. */
114     walker = bus->first_child;
115     while (walker != NULL) {
116         next = walker->next_sibling_ptr;
117         WvDevClose(walker);
118         IoDeleteDevice(walker->Self);
119         WvDevFree(walker);
120         walker = next;
121       }
122     node_link = &bus->BusPrivate_.Nodes;
123     while ((node_link = node_link->Flink) != &bus->BusPrivate_.Nodes) {
124         WV_SP_BUS_NODE node = CONTAINING_RECORD(
125             node_link,
126             WV_S_BUS_NODE,
127             BusPrivate_.Link
128           );
129
130         DBG("Removing PDO from bus...\n");
131         RemoveEntryList(&node->BusPrivate_.Link);
132         ObDereferenceObject(node->BusPrivate_.Pdo);
133         bus->BusPrivate_.NodeCount--;
134       }
135     /* Somewhat redundant, since the bus will be freed shortly. */
136     bus->Children = 0;
137     bus->first_child = NULL;
138     /* Detach from any lower DEVICE_OBJECT */
139     if (lower)
140       IoDetachDevice(lower);
141     /* Delete and free. */
142     IoDeleteDevice(dev->Self);
143     WvDevFree(dev);
144     return status;
145   }
146
147 static NTSTATUS STDCALL WvBusPnpQueryDevRelations_(
148     IN WV_SP_BUS_T bus,
149     IN PIRP irp
150   ) {
151     NTSTATUS status;
152     WV_SP_DEV_T dev = &bus->Dev;
153     PDEVICE_OBJECT lower = bus->LowerDeviceObject;
154     PIO_STACK_LOCATION io_stack_loc = IoGetCurrentIrpStackLocation(irp);
155     winvblock__uint32 count;
156     WV_SP_DEV_T walker;
157     PDEVICE_RELATIONS dev_relations;
158     PLIST_ENTRY node_link;
159
160     if (!(io_stack_loc->Control & SL_PENDING_RETURNED)) {
161         /* Enqueue the IRP. */
162         status = WvBusEnqueueIrp(bus, irp);
163         if (status != STATUS_PENDING)
164           /* Problem. */
165           return driver__complete_irp(irp, 0, status);
166         /* Ok. */
167         return status;
168       }
169     /* If we get here, we should be called by WvBusProcessWorkItems() */
170     if (
171         io_stack_loc->Parameters.QueryDeviceRelations.Type != BusRelations ||
172         irp->IoStatus.Information
173       ) {
174         if (lower) {
175             IoSkipCurrentIrpStackLocation(irp);
176             return IoCallDriver(lower, irp);
177           }
178         return driver__complete_irp(
179             irp,
180             irp->IoStatus.Information,
181             irp->IoStatus.Status
182           );
183       }
184     WvProbeDisks();
185     count = 0;
186     walker = bus->first_child;
187     while (walker != NULL) {
188         count++;
189         walker = walker->next_sibling_ptr;
190       }
191     count += bus->BusPrivate_.NodeCount;
192     dev_relations = wv_malloc(
193         sizeof *dev_relations + (sizeof (PDEVICE_OBJECT) * count)
194       );
195     if (dev_relations == NULL) {
196         /* Couldn't allocate dev_relations, but silently succeed. */
197         if (lower) {
198             IoSkipCurrentIrpStackLocation(irp);
199             return IoCallDriver(lower, irp);
200           }
201         return driver__complete_irp(irp, 0, STATUS_SUCCESS);
202       }
203     dev_relations->Count = count;
204
205     count = 0;
206     walker = bus->first_child;
207     while (walker != NULL) {
208         ObReferenceObject(walker->Self);
209         dev_relations->Objects[count] = walker->Self;
210         count++;
211         walker = walker->next_sibling_ptr;
212       }
213     node_link = &bus->BusPrivate_.Nodes;
214     while ((node_link = node_link->Flink) != &bus->BusPrivate_.Nodes) {
215         WV_SP_BUS_NODE node = CONTAINING_RECORD(
216             node_link,
217             WV_S_BUS_NODE,
218             BusPrivate_.Link
219           );
220
221         dev_relations->Objects[count] = node->BusPrivate_.Pdo;
222         count++;
223       }
224     irp->IoStatus.Information = (ULONG_PTR) dev_relations;
225     irp->IoStatus.Status = status = STATUS_SUCCESS;
226     if (lower) {
227         IoSkipCurrentIrpStackLocation(irp);
228         return IoCallDriver(lower, irp);
229       }
230     return driver__complete_irp(irp, irp->IoStatus.Information, status);
231   }
232
233 static NTSTATUS STDCALL WvBusPnpQueryCapabilities_(
234     IN WV_SP_BUS_T bus,
235     IN PIRP irp
236   ) {
237     PIO_STACK_LOCATION io_stack_loc = IoGetCurrentIrpStackLocation(irp);
238     PDEVICE_CAPABILITIES DeviceCapabilities =
239       io_stack_loc->Parameters.DeviceCapabilities.Capabilities;
240     NTSTATUS status;
241     DEVICE_CAPABILITIES ParentDeviceCapabilities;
242     PDEVICE_OBJECT lower;
243
244     if (DeviceCapabilities->Version != 1 ||
245         DeviceCapabilities->Size < sizeof (DEVICE_CAPABILITIES)
246       )
247       return driver__complete_irp(irp, 0, STATUS_UNSUCCESSFUL);
248     /* Let the lower DEVICE_OBJECT handle the IRP. */
249     lower = bus->LowerDeviceObject;
250     IoSkipCurrentIrpStackLocation(irp);
251     return IoCallDriver(lower, irp);
252   }
253
254 static NTSTATUS STDCALL WvBusPnpQueryDevText_(IN WV_SP_BUS_T bus, IN PIRP irp) {
255     WV_SP_DEV_T dev = &bus->Dev;
256     WCHAR (*str)[512];
257     PIO_STACK_LOCATION io_stack_loc = IoGetCurrentIrpStackLocation(irp);
258     NTSTATUS status;
259     winvblock__uint32 str_len;
260
261     /* Allocate a string buffer. */
262     str = wv_mallocz(sizeof *str);
263     if (str == NULL) {
264         DBG("wv_malloc IRP_MN_QUERY_DEVICE_TEXT\n");
265         status = STATUS_INSUFFICIENT_RESOURCES;
266         goto alloc_str;
267       }
268     /* Determine the query type. */
269     switch (io_stack_loc->Parameters.QueryDeviceText.DeviceTextType) {
270         case DeviceTextDescription:
271           str_len = swprintf(*str, winvblock__literal_w L" Bus") + 1;
272           irp->IoStatus.Information =
273             (ULONG_PTR) wv_palloc(str_len * sizeof *str);
274           if (irp->IoStatus.Information == 0) {
275               DBG("wv_palloc DeviceTextDescription\n");
276               status = STATUS_INSUFFICIENT_RESOURCES;
277               goto alloc_info;
278             }
279           RtlCopyMemory(
280               (PWCHAR) irp->IoStatus.Information,
281               str,
282               str_len * sizeof (WCHAR)
283             );
284           status = STATUS_SUCCESS;
285           goto alloc_info;
286
287         case DeviceTextLocationInformation:
288           str_len = WvDevPnpId(
289               dev,
290               BusQueryInstanceID,
291               str
292             );
293           irp->IoStatus.Information =
294             (ULONG_PTR) wv_palloc(str_len * sizeof *str);
295           if (irp->IoStatus.Information == 0) {
296               DBG("wv_palloc DeviceTextLocationInformation\n");
297               status = STATUS_INSUFFICIENT_RESOURCES;
298               goto alloc_info;
299             }
300           RtlCopyMemory(
301               (PWCHAR) irp->IoStatus.Information,
302               str,
303               str_len * sizeof (WCHAR)
304             );
305           status = STATUS_SUCCESS;
306           goto alloc_info;
307
308         default:
309           irp->IoStatus.Information = 0;
310           status = STATUS_NOT_SUPPORTED;
311       }
312     /* irp->IoStatus.Information not freed. */
313     alloc_info:
314
315     wv_free(str);
316     alloc_str:
317
318     return driver__complete_irp(irp, irp->IoStatus.Information, status);
319   }
320
321 DEFINE_GUID(
322     GUID_BUS_TYPE_INTERNAL,
323     0x2530ea73L,
324     0x086b,
325     0x11d1,
326     0xa0,
327     0x9f,
328     0x00,
329     0xc0,
330     0x4f,
331     0xc3,
332     0x40,
333     0xb1
334   );
335
336 static NTSTATUS STDCALL WvBusPnpQueryBusInfo_(IN WV_SP_BUS_T bus, IN PIRP irp) {
337     PPNP_BUS_INFORMATION pnp_bus_info;
338     NTSTATUS status;
339
340     pnp_bus_info = wv_palloc(sizeof *pnp_bus_info);
341     if (pnp_bus_info == NULL) {
342         DBG("wv_palloc IRP_MN_QUERY_BUS_INFORMATION\n");
343         status = STATUS_INSUFFICIENT_RESOURCES;
344         goto alloc_pnp_bus_info;
345       }
346     pnp_bus_info->BusTypeGuid = GUID_BUS_TYPE_INTERNAL;
347     pnp_bus_info->LegacyBusType = PNPBus;
348     pnp_bus_info->BusNumber = 0;
349     irp->IoStatus.Information = (ULONG_PTR) pnp_bus_info;
350     status = STATUS_SUCCESS;
351
352     /* irp-IoStatus.Information (pnp_bus_info) not freed. */
353     alloc_pnp_bus_info:
354
355     irp->IoStatus.Status = status;
356     IoCompleteRequest(irp, IO_NO_INCREMENT);
357     return status;
358   }
359
360 static NTSTATUS STDCALL WvBusPnpSimple_(
361     IN WV_SP_BUS_T bus,
362     IN PIRP irp,
363     IN UCHAR code
364   ) {
365     NTSTATUS status;
366     WV_SP_DEV_T dev = &bus->Dev;
367     PDEVICE_OBJECT lower = bus->LowerDeviceObject;
368
369     switch (code) {
370         case IRP_MN_QUERY_PNP_DEVICE_STATE:
371           DBG("bus_pnp: IRP_MN_QUERY_PNP_DEVICE_STATE\n");
372           irp->IoStatus.Information = 0;
373           status = STATUS_SUCCESS;
374           break;
375
376         case IRP_MN_QUERY_STOP_DEVICE:
377           DBG("bus_pnp: IRP_MN_QUERY_STOP_DEVICE\n");
378           dev->OldState = dev->State;
379           dev->State = WvDevStateStopPending;
380           status = STATUS_SUCCESS;
381           break;
382
383         case IRP_MN_CANCEL_STOP_DEVICE:
384           DBG("bus_pnp: IRP_MN_CANCEL_STOP_DEVICE\n");
385           dev->State = dev->OldState;
386           status = STATUS_SUCCESS;
387           break;
388
389         case IRP_MN_STOP_DEVICE:
390           DBG("bus_pnp: IRP_MN_STOP_DEVICE\n");
391           dev->OldState = dev->State;
392           dev->State = WvDevStateStopped;
393           status = STATUS_SUCCESS;
394           break;
395
396         case IRP_MN_QUERY_REMOVE_DEVICE:
397           DBG("bus_pnp: IRP_MN_QUERY_REMOVE_DEVICE\n");
398           dev->OldState = dev->State;
399           dev->State = WvDevStateRemovePending;
400           status = STATUS_SUCCESS;
401           break;
402
403         case IRP_MN_CANCEL_REMOVE_DEVICE:
404           DBG("bus_pnp: IRP_MN_CANCEL_REMOVE_DEVICE\n");
405           dev->State = dev->OldState;
406           status = STATUS_SUCCESS;
407           break;
408
409         case IRP_MN_SURPRISE_REMOVAL:
410           DBG("bus_pnp: IRP_MN_SURPRISE_REMOVAL\n");
411           dev->OldState = dev->State;
412           dev->State = WvDevStateSurpriseRemovePending;
413           status = STATUS_SUCCESS;
414           break;
415
416         case IRP_MN_QUERY_RESOURCES:
417         case IRP_MN_QUERY_RESOURCE_REQUIREMENTS:
418           DBG("bus_pnp: IRP_MN_QUERY_RESOURCE*\n");
419           IoCompleteRequest(irp, IO_NO_INCREMENT);
420           return STATUS_SUCCESS;
421           
422         default:
423           DBG("bus_pnp: Unhandled IRP_MN_*: %d\n", code);
424           status = irp->IoStatus.Status;
425       }
426
427     irp->IoStatus.Status = status;
428     if (lower) {
429         IoSkipCurrentIrpStackLocation(irp);
430         return IoCallDriver(lower, irp);
431       }
432     return driver__complete_irp(irp, irp->IoStatus.Information, status);
433   }
434
435 /* Bus PnP dispatch routine. */
436 winvblock__lib_func NTSTATUS STDCALL WvBusPnp(
437     IN WV_SP_BUS_T bus,
438     IN PIRP irp,
439     IN UCHAR code
440   ) {
441     WV_SP_DEV_T dev = &bus->Dev;
442
443     switch (code) {
444         case IRP_MN_QUERY_ID:
445           DBG("bus_pnp: IRP_MN_QUERY_ID\n");
446           return WvDevPnpQueryId(dev, irp);
447
448         case IRP_MN_QUERY_DEVICE_TEXT:
449           DBG("bus_pnp: IRP_MN_QUERY_DEVICE_TEXT\n");
450           return WvBusPnpQueryDevText_(bus, irp);
451
452         case IRP_MN_QUERY_BUS_INFORMATION:
453           DBG("bus_pnp: IRP_MN_QUERY_BUS_INFORMATION\n");
454           return WvBusPnpQueryBusInfo_(bus, irp);
455
456         case IRP_MN_QUERY_DEVICE_RELATIONS:
457           DBG("bus_pnp: IRP_MN_QUERY_DEVICE_RELATIONS\n");
458           return WvBusPnpQueryDevRelations_(bus, irp);
459
460         case IRP_MN_QUERY_CAPABILITIES:
461           DBG("bus_pnp: IRP_MN_QUERY_CAPABILITIES\n");
462           return WvBusPnpQueryCapabilities_(bus, irp);
463
464         case IRP_MN_REMOVE_DEVICE:
465           DBG("bus_pnp: IRP_MN_REMOVE_DEVICE\n");
466           return WvBusPnpRemoveDev_(bus, irp);
467
468         case IRP_MN_START_DEVICE:
469           DBG("bus_pnp: IRP_MN_START_DEVICE\n");
470           return WvBusPnpStartDev_(bus, irp);
471
472         default:
473           return WvBusPnpSimple_(bus, irp, code);
474       }
475   }