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