1 | /* $Id: VBoxUsbPnP.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * USB PnP Handling
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | #include "VBoxUsbCmn.h"
|
---|
28 |
|
---|
29 | static NTSTATUS vboxUsbPnPMnStartDevice(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
|
---|
30 | {
|
---|
31 | IoCopyCurrentIrpStackLocationToNext(pIrp);
|
---|
32 | NTSTATUS Status = VBoxDrvToolIoPostSync(pDevExt->pLowerDO, pIrp);
|
---|
33 | Assert(NT_SUCCESS(Status) || Status == STATUS_NOT_SUPPORTED);
|
---|
34 | if (NT_SUCCESS(Status))
|
---|
35 | {
|
---|
36 | Status = vboxUsbRtStart(pDevExt);
|
---|
37 | Assert(Status == STATUS_SUCCESS);
|
---|
38 | if (NT_SUCCESS(Status))
|
---|
39 | {
|
---|
40 | vboxUsbPnPStateSet(pDevExt, ENMVBOXUSB_PNPSTATE_STARTED);
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | VBoxDrvToolIoComplete(pIrp, Status, 0);
|
---|
45 | vboxUsbDdiStateRelease(pDevExt);
|
---|
46 | return Status;
|
---|
47 | }
|
---|
48 |
|
---|
49 | static NTSTATUS vboxUsbPnPMnQueryStopDevice(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
|
---|
50 | {
|
---|
51 | vboxUsbPnPStateSet(pDevExt, ENMVBOXUSB_PNPSTATE_STOP_PENDING);
|
---|
52 |
|
---|
53 | vboxUsbDdiStateReleaseAndWaitCompleted(pDevExt);
|
---|
54 |
|
---|
55 | pIrp->IoStatus.Status = STATUS_SUCCESS;
|
---|
56 | pIrp->IoStatus.Information = 0;
|
---|
57 | IoSkipCurrentIrpStackLocation(pIrp);
|
---|
58 | return IoCallDriver(pDevExt->pLowerDO, pIrp);
|
---|
59 | }
|
---|
60 |
|
---|
61 | static NTSTATUS vboxUsbPnPMnStopDevice(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
|
---|
62 | {
|
---|
63 | vboxUsbPnPStateSet(pDevExt, ENMVBOXUSB_PNPSTATE_STOPPED);
|
---|
64 |
|
---|
65 | vboxUsbRtClear(pDevExt);
|
---|
66 |
|
---|
67 | NTSTATUS Status = VBoxUsbToolDevUnconfigure(pDevExt->pLowerDO);
|
---|
68 | Assert(NT_SUCCESS(Status));
|
---|
69 |
|
---|
70 | pIrp->IoStatus.Status = Status;
|
---|
71 | pIrp->IoStatus.Information = 0;
|
---|
72 | IoSkipCurrentIrpStackLocation(pIrp);
|
---|
73 | Status = IoCallDriver(pDevExt->pLowerDO, pIrp);
|
---|
74 |
|
---|
75 | vboxUsbDdiStateRelease(pDevExt);
|
---|
76 | return Status;
|
---|
77 | }
|
---|
78 |
|
---|
79 | static NTSTATUS vboxUsbPnPMnCancelStopDevice(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
|
---|
80 | {
|
---|
81 | ENMVBOXUSB_PNPSTATE enmState = vboxUsbPnPStateGet(pDevExt);
|
---|
82 | NTSTATUS Status = STATUS_SUCCESS;
|
---|
83 |
|
---|
84 | IoCopyCurrentIrpStackLocationToNext(pIrp);
|
---|
85 | Status = VBoxDrvToolIoPostSync(pDevExt->pLowerDO, pIrp);
|
---|
86 | if (NT_SUCCESS(Status) && enmState == ENMVBOXUSB_PNPSTATE_STOP_PENDING)
|
---|
87 | {
|
---|
88 | vboxUsbPnPStateRestore(pDevExt);
|
---|
89 | }
|
---|
90 |
|
---|
91 | Status = STATUS_SUCCESS;
|
---|
92 | VBoxDrvToolIoComplete(pIrp, Status, 0);
|
---|
93 | vboxUsbDdiStateRelease(pDevExt);
|
---|
94 |
|
---|
95 | return Status;
|
---|
96 | }
|
---|
97 |
|
---|
98 | static NTSTATUS vboxUsbPnPMnQueryRemoveDevice(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
|
---|
99 | {
|
---|
100 | vboxUsbPnPStateSet(pDevExt, ENMVBOXUSB_PNPSTATE_REMOVE_PENDING);
|
---|
101 |
|
---|
102 | vboxUsbDdiStateReleaseAndWaitCompleted(pDevExt);
|
---|
103 |
|
---|
104 | pIrp->IoStatus.Status = STATUS_SUCCESS;
|
---|
105 | pIrp->IoStatus.Information = 0;
|
---|
106 | IoSkipCurrentIrpStackLocation(pIrp);
|
---|
107 | return IoCallDriver(pDevExt->pLowerDO, pIrp);
|
---|
108 | }
|
---|
109 |
|
---|
110 | static NTSTATUS vboxUsbPnPRmDev(PVBOXUSBDEV_EXT pDevExt)
|
---|
111 | {
|
---|
112 | NTSTATUS Status = vboxUsbRtRm(pDevExt);
|
---|
113 | Assert(Status == STATUS_SUCCESS);
|
---|
114 |
|
---|
115 | return Status;
|
---|
116 | }
|
---|
117 |
|
---|
118 | static NTSTATUS vboxUsbPnPMnRemoveDevice(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
|
---|
119 | {
|
---|
120 | ENMVBOXUSB_PNPSTATE enmState = vboxUsbPnPStateGet(pDevExt);
|
---|
121 | NTSTATUS Status = STATUS_SUCCESS;
|
---|
122 | if (enmState != ENMVBOXUSB_PNPSTATE_SURPRISE_REMOVED)
|
---|
123 | {
|
---|
124 | Status = vboxUsbPnPRmDev(pDevExt);
|
---|
125 | Assert(Status == STATUS_SUCCESS);
|
---|
126 | }
|
---|
127 |
|
---|
128 | vboxUsbPnPStateSet(pDevExt, ENMVBOXUSB_PNPSTATE_REMOVED);
|
---|
129 |
|
---|
130 | vboxUsbDdiStateRelease(pDevExt);
|
---|
131 |
|
---|
132 | vboxUsbDdiStateReleaseAndWaitRemoved(pDevExt);
|
---|
133 |
|
---|
134 | vboxUsbRtClear(pDevExt);
|
---|
135 |
|
---|
136 | pIrp->IoStatus.Status = STATUS_SUCCESS;
|
---|
137 | pIrp->IoStatus.Information = 0;
|
---|
138 | IoSkipCurrentIrpStackLocation(pIrp);
|
---|
139 | Status = IoCallDriver(pDevExt->pLowerDO, pIrp);
|
---|
140 |
|
---|
141 | IoDetachDevice(pDevExt->pLowerDO);
|
---|
142 | IoDeleteDevice(pDevExt->pFDO);
|
---|
143 |
|
---|
144 | return Status;
|
---|
145 | }
|
---|
146 |
|
---|
147 | static NTSTATUS vboxUsbPnPMnCancelRemoveDevice(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
|
---|
148 | {
|
---|
149 | ENMVBOXUSB_PNPSTATE enmState = vboxUsbPnPStateGet(pDevExt);
|
---|
150 | NTSTATUS Status = STATUS_SUCCESS;
|
---|
151 | IoCopyCurrentIrpStackLocationToNext(pIrp);
|
---|
152 |
|
---|
153 | Status = VBoxDrvToolIoPostSync(pDevExt->pLowerDO, pIrp);
|
---|
154 |
|
---|
155 | if (NT_SUCCESS(Status) &&
|
---|
156 | enmState == ENMVBOXUSB_PNPSTATE_REMOVE_PENDING)
|
---|
157 | {
|
---|
158 | vboxUsbPnPStateRestore(pDevExt);
|
---|
159 | }
|
---|
160 |
|
---|
161 | Status = STATUS_SUCCESS;
|
---|
162 | VBoxDrvToolIoComplete(pIrp, Status, 0);
|
---|
163 | vboxUsbDdiStateRelease(pDevExt);
|
---|
164 |
|
---|
165 | return Status;
|
---|
166 | }
|
---|
167 |
|
---|
168 | static NTSTATUS vboxUsbPnPMnSurpriseRemoval(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
|
---|
169 | {
|
---|
170 | vboxUsbPnPStateSet(pDevExt, ENMVBOXUSB_PNPSTATE_SURPRISE_REMOVED);
|
---|
171 |
|
---|
172 | NTSTATUS Status = vboxUsbPnPRmDev(pDevExt);
|
---|
173 | Assert(Status == STATUS_SUCCESS);
|
---|
174 |
|
---|
175 | pIrp->IoStatus.Status = STATUS_SUCCESS;
|
---|
176 | pIrp->IoStatus.Information = 0;
|
---|
177 | IoSkipCurrentIrpStackLocation(pIrp);
|
---|
178 | Status = IoCallDriver(pDevExt->pLowerDO, pIrp);
|
---|
179 |
|
---|
180 | vboxUsbDdiStateRelease(pDevExt);
|
---|
181 |
|
---|
182 | return Status;
|
---|
183 | }
|
---|
184 |
|
---|
185 | static NTSTATUS vboxUsbPnPMnQueryCapabilities(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
|
---|
186 | {
|
---|
187 | PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
|
---|
188 | PDEVICE_CAPABILITIES pDevCaps = pSl->Parameters.DeviceCapabilities.Capabilities;
|
---|
189 |
|
---|
190 | if (pDevCaps->Version < 1 || pDevCaps->Size < sizeof (*pDevCaps))
|
---|
191 | {
|
---|
192 | AssertFailed();
|
---|
193 | /** @todo return more appropriate status ?? */
|
---|
194 | return STATUS_UNSUCCESSFUL;
|
---|
195 | }
|
---|
196 |
|
---|
197 | pDevCaps->SurpriseRemovalOK = TRUE;
|
---|
198 | pIrp->IoStatus.Status = STATUS_SUCCESS;
|
---|
199 |
|
---|
200 | IoCopyCurrentIrpStackLocationToNext(pIrp);
|
---|
201 | NTSTATUS Status = VBoxDrvToolIoPostSync(pDevExt->pLowerDO, pIrp);
|
---|
202 | Assert(NT_SUCCESS(Status));
|
---|
203 | if (NT_SUCCESS(Status))
|
---|
204 | {
|
---|
205 | pDevCaps->SurpriseRemovalOK = 1;
|
---|
206 | pDevExt->DdiState.DevCaps = *pDevCaps;
|
---|
207 | }
|
---|
208 |
|
---|
209 | VBoxDrvToolIoComplete(pIrp, Status, 0);
|
---|
210 | vboxUsbDdiStateRelease(pDevExt);
|
---|
211 |
|
---|
212 | return Status;
|
---|
213 | }
|
---|
214 |
|
---|
215 | static NTSTATUS vboxUsbPnPMnDefault(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
|
---|
216 | {
|
---|
217 | NTSTATUS Status;
|
---|
218 | IoSkipCurrentIrpStackLocation(pIrp);
|
---|
219 | Status = IoCallDriver(pDevExt->pLowerDO, pIrp);
|
---|
220 | vboxUsbDdiStateRelease(pDevExt);
|
---|
221 | return Status;
|
---|
222 | }
|
---|
223 |
|
---|
224 | DECLHIDDEN(NTSTATUS) vboxUsbDispatchPnP(IN PDEVICE_OBJECT pDeviceObject, IN PIRP pIrp)
|
---|
225 | {
|
---|
226 | PVBOXUSBDEV_EXT pDevExt = (PVBOXUSBDEV_EXT)pDeviceObject->DeviceExtension;
|
---|
227 | if (!vboxUsbDdiStateRetainIfNotRemoved(pDevExt))
|
---|
228 | return VBoxDrvToolIoComplete(pIrp, STATUS_DELETE_PENDING, 0);
|
---|
229 |
|
---|
230 | PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
|
---|
231 | switch (pSl->MinorFunction)
|
---|
232 | {
|
---|
233 | case IRP_MN_START_DEVICE:
|
---|
234 | return vboxUsbPnPMnStartDevice(pDevExt, pIrp);
|
---|
235 |
|
---|
236 | case IRP_MN_QUERY_STOP_DEVICE:
|
---|
237 | return vboxUsbPnPMnQueryStopDevice(pDevExt, pIrp);
|
---|
238 |
|
---|
239 | case IRP_MN_STOP_DEVICE:
|
---|
240 | return vboxUsbPnPMnStopDevice(pDevExt, pIrp);
|
---|
241 |
|
---|
242 | case IRP_MN_CANCEL_STOP_DEVICE:
|
---|
243 | return vboxUsbPnPMnCancelStopDevice(pDevExt, pIrp);
|
---|
244 |
|
---|
245 | case IRP_MN_QUERY_REMOVE_DEVICE:
|
---|
246 | return vboxUsbPnPMnQueryRemoveDevice(pDevExt, pIrp);
|
---|
247 |
|
---|
248 | case IRP_MN_REMOVE_DEVICE:
|
---|
249 | return vboxUsbPnPMnRemoveDevice(pDevExt, pIrp);
|
---|
250 |
|
---|
251 | case IRP_MN_CANCEL_REMOVE_DEVICE:
|
---|
252 | return vboxUsbPnPMnCancelRemoveDevice(pDevExt, pIrp);
|
---|
253 |
|
---|
254 | case IRP_MN_SURPRISE_REMOVAL:
|
---|
255 | return vboxUsbPnPMnSurpriseRemoval(pDevExt, pIrp);
|
---|
256 |
|
---|
257 | case IRP_MN_QUERY_CAPABILITIES:
|
---|
258 | return vboxUsbPnPMnQueryCapabilities(pDevExt, pIrp);
|
---|
259 |
|
---|
260 | default:
|
---|
261 | return vboxUsbPnPMnDefault(pDevExt, pIrp);
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|