VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxUSB/win/mon/VBoxUsbMon.cpp@ 38268

Last change on this file since 38268 was 38268, checked in by vboxsync, 13 years ago

usb/win: logging fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.8 KB
Line 
1/* $Id: VBoxUsbMon.cpp 38268 2011-08-01 17:03:03Z vboxsync $ */
2/** @file
3 * VBox USB Monitor
4 */
5/*
6 * Copyright (C) 2011 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 */
16
17#include "VBoxUsbMon.h"
18#include "../cmn/VBoxUsbIdc.h"
19#include <vbox/err.h>
20#include <VBox/usblib.h>
21#include <excpt.h>
22#include <stdio.h>
23
24/*
25 * Note: Must match the VID & PID in the USB driver .inf file!!
26 */
27/*
28 BusQueryDeviceID USB\Vid_80EE&Pid_CAFE
29 BusQueryInstanceID 2
30 BusQueryHardwareIDs USB\Vid_80EE&Pid_CAFE&Rev_0100
31 BusQueryHardwareIDs USB\Vid_80EE&Pid_CAFE
32 BusQueryCompatibleIDs USB\Class_ff&SubClass_00&Prot_00
33 BusQueryCompatibleIDs USB\Class_ff&SubClass_00
34 BusQueryCompatibleIDs USB\Class_ff
35*/
36
37#define szBusQueryDeviceId L"USB\\Vid_80EE&Pid_CAFE"
38#define szBusQueryHardwareIDs L"USB\\Vid_80EE&Pid_CAFE&Rev_0100\0USB\\Vid_80EE&Pid_CAFE\0\0"
39#define szBusQueryCompatibleIDs L"USB\\Class_ff&SubClass_00&Prot_00\0USB\\Class_ff&SubClass_00\0USB\\Class_ff\0\0"
40
41#define szDeviceTextDescription L"VirtualBox USB"
42
43typedef struct VBOXUSBMONINS
44{
45 void * pvDummy;
46} VBOXUSBMONINS, *PVBOXUSBMONINS;
47
48typedef struct VBOXUSBMONCTX
49{
50 VBOXUSBFLTCTX FltCtx;
51} VBOXUSBMONCTX, *PVBOXUSBMONCTX;
52
53typedef struct VBOXUSBHUB_PNPHOOK
54{
55 VBOXUSBHOOK_ENTRY Hook;
56 bool fUninitFailed;
57} VBOXUSBHUB_PNPHOOK, *PVBOXUSBHUB_PNPHOOK;
58
59typedef struct VBOXUSBHUB_PNPHOOK_COMPLETION
60{
61 VBOXUSBHOOK_REQUEST Rq;
62} VBOXUSBHUB_PNPHOOK_COMPLETION, *PVBOXUSBHUB_PNPHOOK_COMPLETION;
63
64typedef struct VBOXUSBMONGLOBALS
65{
66 PDEVICE_OBJECT pDevObj;
67 VBOXUSBHUB_PNPHOOK UsbHubPnPHook;
68 KEVENT OpenSynchEvent;
69 IO_REMOVE_LOCK RmLock;
70 uint32_t cOpens;
71 volatile LONG ulPreventUnloadOn;
72 PFILE_OBJECT pPreventUnloadFileObj;
73} VBOXUSBMONGLOBALS, *PVBOXUSBMONGLOBALS;
74
75static VBOXUSBMONGLOBALS g_VBoxUsbMonGlobals;
76
77#define VBOXUSBMON_MEMTAG 'MUBV'
78
79PVOID VBoxUsbMonMemAlloc(SIZE_T cbBytes)
80{
81 PVOID pvMem = ExAllocatePoolWithTag(NonPagedPool, cbBytes, VBOXUSBMON_MEMTAG);
82 Assert(pvMem);
83 return pvMem;
84}
85
86PVOID VBoxUsbMonMemAllocZ(SIZE_T cbBytes)
87{
88 PVOID pvMem = VBoxUsbMonMemAlloc(cbBytes);
89 if (pvMem)
90 {
91 RtlZeroMemory(pvMem, cbBytes);
92 }
93 return pvMem;
94}
95
96VOID VBoxUsbMonMemFree(PVOID pvMem)
97{
98 ExFreePoolWithTag(pvMem, VBOXUSBMON_MEMTAG);
99}
100
101#define VBOXUSBDBG_STRCASE(_t) \
102 case _t: return #_t
103#define VBOXUSBDBG_STRCASE_UNKNOWN(_v) \
104 default: LOG((__FUNCTION__": Unknown Value (0n%d), (0x%x)\n", _v, _v)); return "Unknown"
105
106static const char* vboxUsbDbgStrPnPMn(UCHAR uMn)
107{
108 switch (uMn)
109 {
110 VBOXUSBDBG_STRCASE(IRP_MN_START_DEVICE);
111 VBOXUSBDBG_STRCASE(IRP_MN_QUERY_REMOVE_DEVICE);
112 VBOXUSBDBG_STRCASE(IRP_MN_REMOVE_DEVICE);
113 VBOXUSBDBG_STRCASE(IRP_MN_CANCEL_REMOVE_DEVICE);
114 VBOXUSBDBG_STRCASE(IRP_MN_STOP_DEVICE);
115 VBOXUSBDBG_STRCASE(IRP_MN_QUERY_STOP_DEVICE);
116 VBOXUSBDBG_STRCASE(IRP_MN_CANCEL_STOP_DEVICE);
117 VBOXUSBDBG_STRCASE(IRP_MN_QUERY_DEVICE_RELATIONS);
118 VBOXUSBDBG_STRCASE(IRP_MN_QUERY_INTERFACE);
119 VBOXUSBDBG_STRCASE(IRP_MN_QUERY_CAPABILITIES);
120 VBOXUSBDBG_STRCASE(IRP_MN_QUERY_RESOURCES);
121 VBOXUSBDBG_STRCASE(IRP_MN_QUERY_RESOURCE_REQUIREMENTS);
122 VBOXUSBDBG_STRCASE(IRP_MN_QUERY_DEVICE_TEXT);
123 VBOXUSBDBG_STRCASE(IRP_MN_FILTER_RESOURCE_REQUIREMENTS);
124 VBOXUSBDBG_STRCASE(IRP_MN_READ_CONFIG);
125 VBOXUSBDBG_STRCASE(IRP_MN_WRITE_CONFIG);
126 VBOXUSBDBG_STRCASE(IRP_MN_EJECT);
127 VBOXUSBDBG_STRCASE(IRP_MN_SET_LOCK);
128 VBOXUSBDBG_STRCASE(IRP_MN_QUERY_ID);
129 VBOXUSBDBG_STRCASE(IRP_MN_QUERY_PNP_DEVICE_STATE);
130 VBOXUSBDBG_STRCASE(IRP_MN_QUERY_BUS_INFORMATION);
131 VBOXUSBDBG_STRCASE(IRP_MN_DEVICE_USAGE_NOTIFICATION);
132 VBOXUSBDBG_STRCASE(IRP_MN_SURPRISE_REMOVAL);
133 VBOXUSBDBG_STRCASE_UNKNOWN(uMn);
134 }
135}
136
137void vboxUsbDbgPrintUnicodeString(PUNICODE_STRING pUnicodeString)
138{
139 PWSTR pStr = pUnicodeString->Buffer;
140 for (int i = 0; i < pUnicodeString->Length/2; ++i)
141 {
142 LOG(("%c", *pStr++));
143 }
144}
145
146/**
147 * Send IRP_MN_QUERY_DEVICE_RELATIONS
148 *
149 * @returns NT Status
150 * @param pDevObj USB device pointer
151 * @param pFileObj Valid file object pointer
152 * @param pDevRelations Pointer to DEVICE_RELATIONS pointer (out)
153 */
154NTSTATUS VBoxUsbMonQueryBusRelations(PDEVICE_OBJECT pDevObj, PFILE_OBJECT pFileObj, PDEVICE_RELATIONS *pDevRelations)
155{
156 IO_STATUS_BLOCK IoStatus;
157 KEVENT Event;
158 NTSTATUS Status;
159 PIRP pIrp;
160 PIO_STACK_LOCATION pSl;
161
162 KeInitializeEvent(&Event, NotificationEvent, FALSE);
163
164 Assert(pDevRelations);
165 *pDevRelations = NULL;
166
167 pIrp = IoBuildSynchronousFsdRequest(IRP_MJ_PNP, pDevObj, NULL, 0, NULL, &Event, &IoStatus);
168 if (!pIrp)
169 {
170 AssertMsgFailed(("IoBuildDeviceIoControlRequest failed!!\n"));
171 return STATUS_INSUFFICIENT_RESOURCES;
172 }
173 IoStatus.Status = STATUS_NOT_SUPPORTED;
174
175 pSl = IoGetNextIrpStackLocation(pIrp);
176 pSl->MajorFunction = IRP_MJ_PNP;
177 pSl->MinorFunction = IRP_MN_QUERY_DEVICE_RELATIONS;
178 pSl->Parameters.QueryDeviceRelations.Type = BusRelations;
179 pSl->FileObject = pFileObj;
180
181 Status = IoCallDriver(pDevObj, pIrp);
182 if (Status == STATUS_PENDING)
183 {
184 LOG(("IoCallDriver returned STATUS_PENDING!!\n"));
185 KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
186 Status = IoStatus.Status;
187 }
188
189 if (Status == STATUS_SUCCESS)
190 {
191 PDEVICE_RELATIONS pRel = (PDEVICE_RELATIONS)IoStatus.Information;
192 LOG(("pRel = %p\n", pRel));
193 if (VALID_PTR(pRel))
194 {
195 *pDevRelations = pRel;
196 }
197 else
198 LOG(("Invalid pointer %p\n", pRel));
199 }
200
201 LOG(("IoCallDriver returned %x\n", Status));
202 return Status;
203}
204
205static PDRIVER_OBJECT vboxUsbMonHookFindHubDrvObj()
206{
207 NTSTATUS Status = STATUS_UNSUCCESSFUL;
208 UNICODE_STRING szStandardHubName;
209 PDRIVER_OBJECT pDrvObj = NULL;
210 szStandardHubName.Length = 0;
211 szStandardHubName.MaximumLength = 0;
212 szStandardHubName.Buffer = 0;
213 RtlInitUnicodeString(&szStandardHubName, L"\\Driver\\usbhub");
214
215 LOG(("Search USB hub\n"));
216 for (int i = 0; i < 16; i++)
217 {
218 WCHAR szwHubName[32] = {};
219 char szHubName[32] = {};
220 ANSI_STRING AnsiName;
221 UNICODE_STRING UnicodeName;
222 PDEVICE_OBJECT pHubDevObj;
223 PFILE_OBJECT pHubFileObj;
224
225 sprintf(szHubName, "\\Device\\USBPDO-%d", i);
226
227 RtlInitAnsiString(&AnsiName, szHubName);
228
229 UnicodeName.Length = 0;
230 UnicodeName.MaximumLength = sizeof (szwHubName);
231 UnicodeName.Buffer = szwHubName;
232
233 RtlInitAnsiString(&AnsiName, szHubName);
234 Status = RtlAnsiStringToUnicodeString(&UnicodeName, &AnsiName, FALSE);
235 if (Status == STATUS_SUCCESS)
236 {
237 Status = IoGetDeviceObjectPointer(&UnicodeName, FILE_READ_DATA, &pHubFileObj, &pHubDevObj);
238 if (Status == STATUS_SUCCESS)
239 {
240 LOG(("IoGetDeviceObjectPointer for %S returned %p %p\n", szwHubName, pHubDevObj, pHubFileObj));
241
242 if (pHubDevObj->DriverObject
243 && pHubDevObj->DriverObject->DriverName.Buffer
244 && pHubDevObj->DriverObject->DriverName.Length
245 && !RtlCompareUnicodeString(&szStandardHubName, &pHubDevObj->DriverObject->DriverName, TRUE /* case insensitive */))
246 {
247 LOG(("Associated driver"));
248 LOG_USTR(&pHubDevObj->DriverObject->DriverName);
249 LOG(("pnp handler %p\n", pHubDevObj->DriverObject->MajorFunction[IRP_MJ_PNP]));
250
251 pDrvObj = pHubDevObj->DriverObject;
252 break;
253 }
254 ObDereferenceObject(pHubFileObj);
255 }
256 else
257 {
258 WARN(("IoGetDeviceObjectPointer returned Status (0x%x) for (%S)", Status, szwHubName));
259 }
260 }
261 else
262 {
263 WARN(("RtlAnsiStringToUnicodeString failed, Status (0x%x) for Ansu name (%s)", Status, szHubName));
264 }
265 }
266
267 return pDrvObj;
268}
269
270/* NOTE: the stack location data is not the "actual" IRP stack location,
271 * but a copy being preserved on the IRP way down.
272 * See the note in VBoxUsbPnPCompletion for detail */
273static NTSTATUS vboxUsbMonHandlePnPIoctl(PDEVICE_OBJECT pDevObj, PIO_STACK_LOCATION pSl, PIO_STATUS_BLOCK pIoStatus)
274{
275 LOG(("VBoxUSBMonHandlePnPIoctl IRQL = %d\n", KeGetCurrentIrql()));
276 switch(pSl->MinorFunction)
277 {
278 case IRP_MN_QUERY_DEVICE_TEXT:
279 {
280 LOG(("IRP_MN_QUERY_DEVICE_TEXT: pIoStatus->Status = %x\n", pIoStatus->Status));
281 if (pIoStatus->Status == STATUS_SUCCESS)
282 {
283 WCHAR *pId = (WCHAR *)pIoStatus->Information;
284 if (VALID_PTR(pId))
285 {
286 KIRQL Iqrl = KeGetCurrentIrql();
287 /* IRQL should be always passive here */
288 Assert(Iqrl == PASSIVE_LEVEL);
289 switch(pSl->Parameters.QueryDeviceText.DeviceTextType)
290 {
291 case DeviceTextLocationInformation:
292 LOG(("DeviceTextLocationInformation"));
293 LOG_STRW(pId);
294 break;
295
296 case DeviceTextDescription:
297 LOG(("DeviceTextDescription"));
298 LOG_STRW(pId);
299 if (VBoxUsbFltPdoIsFiltered(pDevObj))
300 {
301 WCHAR *pId = (WCHAR *)ExAllocatePool(PagedPool, sizeof(szDeviceTextDescription));
302 if (!pId)
303 {
304 AssertFailed();
305 break;
306 }
307 memcpy(pId, szDeviceTextDescription, sizeof(szDeviceTextDescription));
308 LOG(("NEW szDeviceTextDescription"));
309 LOG_STRW(pId);
310 ExFreePool((PVOID)pIoStatus->Information);
311 pIoStatus->Information = (ULONG_PTR)pId;
312 }
313 break;
314 default:
315 break;
316 }
317 }
318 else
319 LOG(("Invalid pointer %p\n", pId));
320 }
321 break;
322 }
323
324 case IRP_MN_QUERY_ID:
325 {
326 LOG(("IRP_MN_QUERY_ID: Irp->pIoStatus->Status = %x\n", pIoStatus->Status));
327 if (pIoStatus->Status == STATUS_SUCCESS && pDevObj)
328 {
329 WCHAR *pId = (WCHAR *)pIoStatus->Information;
330#ifdef DEBUG
331 WCHAR *pTmp;
332#endif
333 if (VALID_PTR(pId))
334 {
335 KIRQL Iqrl = KeGetCurrentIrql();
336 /* IRQL should be always passive here */
337 Assert(Iqrl == PASSIVE_LEVEL);
338
339 switch (pSl->Parameters.QueryDeviceRelations.Type)
340 {
341 case BusQueryInstanceID:
342 LOG(("BusQueryInstanceID"));
343 LOG_STRW(pId);
344 break;
345
346 case BusQueryDeviceID:
347 {
348 pId = (WCHAR *)ExAllocatePool(PagedPool, sizeof(szBusQueryDeviceId));
349 if (!pId)
350 {
351 AssertFailed();
352 break;
353 }
354
355 BOOLEAN bFiltered = FALSE;
356 NTSTATUS Status = VBoxUsbFltPdoAdd(pDevObj, &bFiltered);
357 if (Status != STATUS_SUCCESS || !bFiltered)
358 {
359 Assert(Status == STATUS_SUCCESS);
360 ExFreePool(pId);
361 break;
362 }
363
364 ExFreePool((PVOID)pIoStatus->Information);
365 memcpy(pId, szBusQueryDeviceId, sizeof(szBusQueryDeviceId));
366 pIoStatus->Information = (ULONG_PTR)pId;
367 break;
368 }
369 case BusQueryHardwareIDs:
370 {
371#ifdef DEBUG
372 while(*pId) //MULTI_SZ
373 {
374 LOG(("BusQueryHardwareIDs"));
375 LOG_STRW(pId);
376 while(*pId) pId++;
377 pId++;
378 }
379#endif
380 pId = (WCHAR *)ExAllocatePool(PagedPool, sizeof(szBusQueryHardwareIDs));
381 if (!pId)
382 {
383 AssertFailed();
384 break;
385 }
386
387 BOOLEAN bFiltered = FALSE;
388 NTSTATUS Status = VBoxUsbFltPdoAdd(pDevObj, &bFiltered);
389 if (Status != STATUS_SUCCESS || !bFiltered)
390 {
391 Assert(Status == STATUS_SUCCESS);
392 ExFreePool(pId);
393 break;
394 }
395
396 memcpy(pId, szBusQueryHardwareIDs, sizeof(szBusQueryHardwareIDs));
397#ifdef DEBUG
398 pTmp = pId;
399 while(*pTmp) //MULTI_SZ
400 {
401 LOG(("NEW BusQueryHardwareIDs"));
402 LOG_STRW(pTmp);
403 while(*pTmp) pTmp++;
404 pTmp++;
405 }
406#endif
407 ExFreePool((PVOID)pIoStatus->Information);
408 pIoStatus->Information = (ULONG_PTR)pId;
409 break;
410 }
411 case BusQueryCompatibleIDs:
412#ifdef DEBUG
413 while(*pId) //MULTI_SZ
414 {
415 LOG(("BusQueryCompatibleIDs"));
416 LOG_STRW(pId);
417 while(*pId) pId++;
418 pId++;
419 }
420#endif
421 if (VBoxUsbFltPdoIsFiltered(pDevObj))
422 {
423 pId = (WCHAR *)ExAllocatePool(PagedPool, sizeof(szBusQueryCompatibleIDs));
424 if (!pId)
425 {
426 AssertFailed();
427 break;
428 }
429 memcpy(pId, szBusQueryCompatibleIDs, sizeof(szBusQueryCompatibleIDs));
430#ifdef DEBUG
431 pTmp = pId;
432 while(*pTmp) //MULTI_SZ
433 {
434 LOG(("NEW BusQueryCompatibleIDs"));
435 LOG_STRW(pTmp);
436 while(*pTmp) pTmp++;
437 pTmp++;
438 }
439#endif
440 ExFreePool((PVOID)pIoStatus->Information);
441 pIoStatus->Information = (ULONG_PTR)pId;
442 }
443 break;
444 }
445 }
446 else
447 LOG(("Invalid pointer %p\n", pId));
448 }
449 break;
450 }
451
452#ifdef DEBUG
453 case IRP_MN_QUERY_DEVICE_RELATIONS:
454 {
455 switch(pSl->Parameters.QueryDeviceRelations.Type)
456 {
457 case BusRelations:
458 {
459 LOG(("BusRelations\n"));
460
461 if (pIoStatus->Status == STATUS_SUCCESS)
462 {
463 PDEVICE_RELATIONS pRel = (PDEVICE_RELATIONS)pIoStatus->Information;
464 LOG(("pRel = %p\n", pRel));
465 if (VALID_PTR(pRel))
466 {
467 for (unsigned i=0;i<pRel->Count;i++)
468 {
469 if (VBoxUsbFltPdoIsFiltered(pDevObj))
470 LOG(("New PDO %p\n", pRel->Objects[i]));
471 }
472 }
473 else
474 LOG(("Invalid pointer %p\n", pRel));
475 }
476 break;
477 }
478 case TargetDeviceRelation:
479 LOG(("TargetDeviceRelation\n"));
480 break;
481 case RemovalRelations:
482 LOG(("RemovalRelations\n"));
483 break;
484 case EjectionRelations:
485 LOG(("EjectionRelations\n"));
486 break;
487 }
488 break;
489 }
490
491 case IRP_MN_QUERY_CAPABILITIES:
492 {
493 LOG(("IRP_MN_QUERY_CAPABILITIES: pIoStatus->Status = %x\n", pIoStatus->Status));
494 if (pIoStatus->Status == STATUS_SUCCESS)
495 {
496 PDEVICE_CAPABILITIES pCaps = pSl->Parameters.DeviceCapabilities.Capabilities;
497 if (VALID_PTR(pCaps))
498 {
499 LOG(("Caps.SilentInstall = %d\n", pCaps->SilentInstall));
500 LOG(("Caps.UniqueID = %d\n", pCaps->UniqueID ));
501 LOG(("Caps.Address = %d\n", pCaps->Address ));
502 LOG(("Caps.UINumber = %d\n", pCaps->UINumber ));
503 }
504 else
505 LOG(("Invalid pointer %p\n", pCaps));
506 }
507 break;
508 }
509
510 default:
511 break;
512#endif
513 } /*switch */
514
515 LOG(("VBoxUSBMonHandlePnPIoctl returns %x (IRQL = %d)\n", pIoStatus->Status, KeGetCurrentIrql()));
516 return pIoStatus->Status;
517}
518
519NTSTATUS _stdcall VBoxUsbPnPCompletion(DEVICE_OBJECT *pDevObj, IRP *pIrp, void *pvContext)
520{
521 Assert(pvContext);
522
523 PVBOXUSBHOOK_REQUEST pRequest = (PVBOXUSBHOOK_REQUEST)pvContext;
524 /* NOTE: despite a regular IRP processing the stack location in our completion
525 * differs from those of the PnP hook since the hook is invoked in the "context" of the calle,
526 * while the completion is in the "coller" context in terms of IRP,
527 * so the completion stack location is one level "up" here.
528 *
529 * Moreover we CAN NOT access irp stack location in the completion because we might not have one at all
530 * in case the hooked driver is at the top of the irp call stack
531 *
532 * This is why we use the stack location we saved on IRP way down.
533 * */
534 PIO_STACK_LOCATION pSl = &pRequest->OldLocation;
535 Assert(pIrp == pRequest->pIrp);
536 /* NOTE: we can not rely on pDevObj passed in IoCompletion since it may be zero
537 * in case IRP was created with extra stack locations and the caller did not initialize
538 * the IO_STACK_LOCATION::DeviceObject */
539 DEVICE_OBJECT *pRealDevObj = pRequest->pDevObj;
540// Assert(!pDevObj || pDevObj == pRealDevObj);
541// Assert(pSl->DeviceObject == pDevObj);
542
543 switch(pSl->MinorFunction)
544 {
545 case IRP_MN_QUERY_DEVICE_TEXT:
546 case IRP_MN_QUERY_ID:
547#ifdef DEBUG
548 case IRP_MN_QUERY_DEVICE_RELATIONS:
549 case IRP_MN_QUERY_CAPABILITIES:
550#endif
551 if (NT_SUCCESS(pIrp->IoStatus.Status))
552 {
553 vboxUsbMonHandlePnPIoctl(pRealDevObj, pSl, &pIrp->IoStatus);
554 }
555 else
556 {
557 Assert(pIrp->IoStatus.Status == STATUS_NOT_SUPPORTED);
558 }
559 break;
560
561 case IRP_MN_SURPRISE_REMOVAL:
562 case IRP_MN_REMOVE_DEVICE:
563 if (NT_SUCCESS(pIrp->IoStatus.Status))
564 {
565 VBoxUsbFltPdoRemove(pRealDevObj);
566 }
567 else
568 {
569 AssertFailed();
570 }
571 break;
572
573 /* These two IRPs are received when the PnP subsystem has determined the id of the newly arrived device */
574 /* IRP_MN_START_DEVICE only arrives if it's a USB device of a known class or with a present host driver */
575 case IRP_MN_QUERY_RESOURCE_REQUIREMENTS:
576 case IRP_MN_QUERY_RESOURCES:
577 if (NT_SUCCESS(pIrp->IoStatus.Status) || pIrp->IoStatus.Status == STATUS_NOT_SUPPORTED)
578 {
579 VBoxUsbFltPdoAddCompleted(pRealDevObj);
580 }
581 else
582 {
583 AssertFailed();
584 }
585 break;
586
587 default:
588 break;
589 }
590
591 LOG(("<==PnP: Mn(%s), PDO(0x%p), IRP(0x%p), Status(0x%x), Sl PDO(0x%p), Compl PDO(0x%p)\n",
592 vboxUsbDbgStrPnPMn(pSl->MinorFunction),
593 pRealDevObj, pIrp, pIrp->IoStatus.Status,
594 pSl->DeviceObject, pDevObj));
595#ifdef DEBUG_misha
596 NTSTATUS tmpStatus = pIrp->IoStatus.Status;
597#endif
598 NTSTATUS Status = VBoxUsbHookRequestComplete(&g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook, pDevObj, pIrp, pRequest);
599 VBoxUsbMonMemFree(pRequest);
600#ifdef DEBUG_misha
601 if (Status != STATUS_MORE_PROCESSING_REQUIRED)
602 {
603 Assert(pIrp->IoStatus.Status == tmpStatus);
604 }
605#endif
606 VBoxUsbHookRelease(&g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook);
607 return Status;
608}
609
610/**
611 * Device PnP hook
612 *
613 * @param pDevObj Device object.
614 * @param pIrp Request packet.
615 */
616NTSTATUS _stdcall VBoxUsbMonPnPHook(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp)
617{
618 if(!VBoxUsbHookRetain(&g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook))
619 {
620 return VBoxUsbHookRequestPassDownHookSkip(&g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook, pDevObj, pIrp);
621 }
622
623 PVBOXUSBHUB_PNPHOOK_COMPLETION pCompletion = (PVBOXUSBHUB_PNPHOOK_COMPLETION)VBoxUsbMonMemAlloc(sizeof (*pCompletion));
624 if (!pCompletion)
625 {
626 AssertFailed();
627 VBoxUsbHookRelease(&g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook);
628 pIrp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
629 pIrp->IoStatus.Information = 0;
630 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
631 return STATUS_INSUFFICIENT_RESOURCES;
632 }
633
634 LOG(("==>PnP: Mn(%s), PDO(0x%p), IRP(0x%p), Status(0x%x)\n", vboxUsbDbgStrPnPMn(IoGetCurrentIrpStackLocation(pIrp)->MinorFunction), pDevObj, pIrp, pIrp->IoStatus.Status));
635
636 NTSTATUS Status = VBoxUsbHookRequestPassDownHookCompletion(&g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook, pDevObj, pIrp, VBoxUsbPnPCompletion, &pCompletion->Rq);
637#ifdef DEBUG
638 if (Status != STATUS_PENDING)
639 {
640 VBoxUsbHookVerifyCompletion(&g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook, &pCompletion->Rq, pIrp);
641 }
642#endif
643 return Status;
644}
645
646
647static NTSTATUS vboxUsbMonHookCheckInit()
648{
649 static bool fIsHookInited = false;
650 if (fIsHookInited)
651 {
652 LOG(("hook inited already, success"));
653 return STATUS_SUCCESS;
654 }
655 PDRIVER_OBJECT pDrvObj = vboxUsbMonHookFindHubDrvObj();
656 if (pDrvObj)
657 {
658 VBoxUsbHookInit(&g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook, pDrvObj, IRP_MJ_PNP, VBoxUsbMonPnPHook);
659 fIsHookInited = true;
660 LOG(("SUCCESS"));
661 return STATUS_SUCCESS;
662 }
663 WARN(("hub drv obj not found, fail"));
664 return STATUS_UNSUCCESSFUL;
665}
666
667static NTSTATUS vboxUsbMonHookInstall()
668{
669#ifdef VBOXUSBMON_DBG_NO_PNPHOOK
670 return STATUS_SUCCESS;
671#else
672 if (g_VBoxUsbMonGlobals.UsbHubPnPHook.fUninitFailed)
673 {
674 WARN(("trying to hook usbhub pnp after the unhook failed, do nothing & pretend success"));
675 return STATUS_SUCCESS;
676 }
677 return VBoxUsbHookInstall(&g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook);
678#endif
679}
680
681static NTSTATUS vboxUsbMonHookUninstall()
682{
683#ifdef VBOXUSBMON_DBG_NO_PNPHOOK
684 return STATUS_SUCCESS;
685#else
686 NTSTATUS Status = VBoxUsbHookUninstall(&g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook);
687 if (!NT_SUCCESS(Status))
688 {
689 AssertMsgFailed(("usbhub pnp unhook failed, setting the fUninitFailed flag, the current value of fUninitFailed (%d)\n", g_VBoxUsbMonGlobals.UsbHubPnPHook.fUninitFailed));
690 g_VBoxUsbMonGlobals.UsbHubPnPHook.fUninitFailed = true;
691 }
692 return Status;
693#endif
694}
695
696
697static NTSTATUS vboxUsbMonCheckTermStuff()
698{
699 NTSTATUS Status = KeWaitForSingleObject(&g_VBoxUsbMonGlobals.OpenSynchEvent,
700 Executive, KernelMode,
701 FALSE, /* BOOLEAN Alertable */
702 NULL /* IN PLARGE_INTEGER Timeout */
703 );
704 AssertRelease(Status == STATUS_SUCCESS);
705
706 do
707 {
708 if (--g_VBoxUsbMonGlobals.cOpens)
709 break;
710
711 Status = vboxUsbMonHookUninstall();
712
713 NTSTATUS tmpStatus = VBoxUsbFltTerm();
714 if (!NT_SUCCESS(tmpStatus))
715 {
716 /* this means a driver state is screwed up, KeBugCheckEx here ? */
717 AssertReleaseFailed();
718 }
719 } while (0);
720
721 KeSetEvent(&g_VBoxUsbMonGlobals.OpenSynchEvent, 0, FALSE);
722
723 return Status;
724}
725
726static NTSTATUS vboxUsbMonCheckInitStuff()
727{
728 NTSTATUS Status = KeWaitForSingleObject(&g_VBoxUsbMonGlobals.OpenSynchEvent,
729 Executive, KernelMode,
730 FALSE, /* BOOLEAN Alertable */
731 NULL /* IN PLARGE_INTEGER Timeout */
732 );
733 if (Status == STATUS_SUCCESS)
734 {
735 do
736 {
737 if (g_VBoxUsbMonGlobals.cOpens++)
738 {
739 LOG(("opens: %d, success", g_VBoxUsbMonGlobals.cOpens));
740 break;
741 }
742
743 Status = VBoxUsbFltInit();
744 if (NT_SUCCESS(Status))
745 {
746 Status = vboxUsbMonHookCheckInit();
747 if (NT_SUCCESS(Status))
748 {
749 Status = vboxUsbMonHookInstall();
750 if (NT_SUCCESS(Status))
751 {
752 Status = STATUS_SUCCESS;
753 LOG(("succeded!!"));
754 break;
755 }
756 else
757 {
758 WARN(("vboxUsbMonHookInstall failed, Status (0x%x)", Status));
759 }
760 }
761 else
762 {
763 WARN(("vboxUsbMonHookCheckInit failed, Status (0x%x)", Status));
764 }
765 VBoxUsbFltTerm();
766 }
767 else
768 {
769 WARN(("VBoxUsbFltInit failed, Status (0x%x)", Status));
770 }
771
772 --g_VBoxUsbMonGlobals.cOpens;
773 Assert(!g_VBoxUsbMonGlobals.cOpens);
774 } while (0);
775
776 KeSetEvent(&g_VBoxUsbMonGlobals.OpenSynchEvent, 0, FALSE);
777 }
778 else
779 {
780 WARN(("KeWaitForSingleObject failed, Status (0x%x)", Status));
781 }
782 return Status;
783}
784
785static NTSTATUS vboxUsbMonContextCreate(PVBOXUSBMONCTX *ppCtx)
786{
787 NTSTATUS Status;
788 *ppCtx = NULL;
789 PVBOXUSBMONCTX pFileCtx = (PVBOXUSBMONCTX)VBoxUsbMonMemAllocZ(sizeof (*pFileCtx));
790 if (pFileCtx)
791 {
792 Status = vboxUsbMonCheckInitStuff();
793 if (Status == STATUS_SUCCESS)
794 {
795 Status = VBoxUsbFltCreate(&pFileCtx->FltCtx);
796 if (Status == STATUS_SUCCESS)
797 {
798 *ppCtx = pFileCtx;
799 LOG(("succeeded!!"));
800 return STATUS_SUCCESS;
801 }
802 else
803 {
804 WARN(("VBoxUsbFltCreate failed"));
805 }
806 vboxUsbMonCheckTermStuff();
807 }
808 else
809 {
810 WARN(("vboxUsbMonCheckInitStuff failed"));
811 }
812 VBoxUsbMonMemFree(pFileCtx);
813 }
814 else
815 {
816 WARN(("VBoxUsbMonMemAllocZ failed"));
817 Status = STATUS_NO_MEMORY;
818 }
819
820 return Status;
821}
822
823static NTSTATUS vboxUsbMonContextClose(PVBOXUSBMONCTX pCtx)
824{
825 NTSTATUS Status = VBoxUsbFltClose(&pCtx->FltCtx);
826 if (Status == STATUS_SUCCESS)
827 {
828 Status = vboxUsbMonCheckTermStuff();
829 Assert(Status == STATUS_SUCCESS);
830 /* ignore the failure */
831 VBoxUsbMonMemFree(pCtx);
832 }
833
834 return Status;
835}
836
837static NTSTATUS _stdcall VBoxUsbMonClose(PDEVICE_OBJECT pDevObj, PIRP pIrp)
838{
839 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
840 PFILE_OBJECT pFileObj = pStack->FileObject;
841 Assert(pFileObj->FsContext);
842 PVBOXUSBMONCTX pCtx = (PVBOXUSBMONCTX)pFileObj->FsContext;
843
844 LOG(("VBoxUsbMonClose"));
845
846 NTSTATUS Status = vboxUsbMonContextClose(pCtx);
847 if (Status != STATUS_SUCCESS)
848 {
849 WARN(("vboxUsbMonContextClose failed, Status (0x%x), prefent unload", Status));
850 if (!InterlockedExchange(&g_VBoxUsbMonGlobals.ulPreventUnloadOn, 1))
851 {
852 LOGREL(("ulPreventUnloadOn not set, preventing unload\n"));
853 UNICODE_STRING UniName;
854 PDEVICE_OBJECT pTmpDevObj;
855 RtlInitUnicodeString(&UniName, USBMON_DEVICE_NAME_NT);
856 NTSTATUS tmpStatus = IoGetDeviceObjectPointer(&UniName, FILE_ALL_ACCESS, &g_VBoxUsbMonGlobals.pPreventUnloadFileObj, &pTmpDevObj);
857 AssertRelease(NT_SUCCESS(tmpStatus));
858 AssertRelease(pTmpDevObj == pDevObj);
859 }
860 else
861 {
862 WARN(("ulPreventUnloadOn already set"));
863 }
864 LOG(("success!!"));
865 Status = STATUS_SUCCESS;
866 }
867 pFileObj->FsContext = NULL;
868 pIrp->IoStatus.Status = Status;
869 pIrp->IoStatus.Information = 0;
870 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
871 return Status;
872}
873
874
875static NTSTATUS _stdcall VBoxUsbMonCreate(PDEVICE_OBJECT pDevObj, PIRP pIrp)
876{
877 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
878 PFILE_OBJECT pFileObj = pStack->FileObject;
879 NTSTATUS Status;
880
881 LOG(("VBoxUSBMonCreate"));
882
883 if (pStack->Parameters.Create.Options & FILE_DIRECTORY_FILE)
884 {
885 WARN(("trying to open as a directory"));
886 pIrp->IoStatus.Status = STATUS_NOT_A_DIRECTORY;
887 pIrp->IoStatus.Information = 0;
888 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
889 return STATUS_NOT_A_DIRECTORY;
890 }
891
892 pFileObj->FsContext = NULL;
893 PVBOXUSBMONCTX pCtx = NULL;
894 Status = vboxUsbMonContextCreate(&pCtx);
895 if (Status == STATUS_SUCCESS)
896 {
897 Assert(pCtx);
898 pFileObj->FsContext = pCtx;
899 }
900 else
901 {
902 WARN(("vboxUsbMonContextCreate failed Status (0x%x)", Status));
903 }
904
905 pIrp->IoStatus.Status = Status;
906 pIrp->IoStatus.Information = 0;
907 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
908 return Status;
909}
910
911static int VBoxUsbMonSetNotifyEvent(PVBOXUSBMONCTX pContext, HANDLE hEvent)
912{
913 int rc = VBoxUsbFltSetNotifyEvent(&pContext->FltCtx, hEvent);
914 return rc;
915}
916
917static int VBoxUsbMonFltAdd(PVBOXUSBMONCTX pContext, PUSBFILTER pFilter, uintptr_t *pId)
918{
919#ifdef VBOXUSBMON_DBG_NO_FILTERS
920 static uintptr_t idDummy = 1;
921 *pId = idDummy;
922 ++idDummy;
923 return VINF_SUCCESS;
924#else
925 int rc = VBoxUsbFltAdd(&pContext->FltCtx, pFilter, pId);
926 return rc;
927#endif
928}
929
930static int VBoxUsbMonFltRemove(PVBOXUSBMONCTX pContext, uintptr_t uId)
931{
932#ifdef VBOXUSBMON_DBG_NO_FILTERS
933 return VINF_SUCCESS;
934#else
935 int rc = VBoxUsbFltRemove(&pContext->FltCtx, uId);
936 return rc;
937#endif
938}
939
940static NTSTATUS VBoxUsbMonRunFilters(PVBOXUSBMONCTX pContext)
941{
942 NTSTATUS Status = VBoxUsbFltFilterCheck(&pContext->FltCtx);
943 return Status;
944}
945
946static NTSTATUS VBoxUsbMonGetDevice(PVBOXUSBMONCTX pContext, HVBOXUSBDEVUSR hDevice, PUSBSUP_GETDEV_MON pInfo)
947{
948 NTSTATUS Status = VBoxUsbFltGetDevice(&pContext->FltCtx, hDevice, pInfo);
949 return Status;
950}
951
952static NTSTATUS vboxUsbMonIoctlDispatch(PVBOXUSBMONCTX pContext, ULONG Ctl, PVOID pvBuffer, ULONG cbInBuffer, ULONG cbOutBuffer, ULONG_PTR* pInfo)
953{
954 NTSTATUS Status = STATUS_SUCCESS;
955 ULONG_PTR Info = 0;
956 switch (Ctl)
957 {
958 case SUPUSBFLT_IOCTL_GET_VERSION:
959 {
960 PUSBSUP_VERSION pOut = (PUSBSUP_VERSION)pvBuffer;
961
962 LOG(("SUPUSBFLT_IOCTL_GET_VERSION\n"));
963 if (!pvBuffer || cbOutBuffer != sizeof(*pOut) || cbInBuffer != 0)
964 {
965 AssertMsgFailed(("SUPUSBFLT_IOCTL_GET_VERSION: Invalid input/output sizes. cbIn=%d expected %d. cbOut=%d expected %d.\n",
966 cbInBuffer, 0, cbOutBuffer, sizeof (*pOut)));
967 Status = STATUS_INVALID_PARAMETER;
968 break;
969 }
970 pOut->u32Major = USBMON_MAJOR_VERSION;
971 pOut->u32Minor = USBMON_MINOR_VERSION;
972 Info = sizeof (*pOut);
973 break;
974 }
975
976 case SUPUSBFLT_IOCTL_ADD_FILTER:
977 {
978 PUSBFILTER pFilter = (PUSBFILTER)pvBuffer;
979 PUSBSUP_FLTADDOUT pOut = (PUSBSUP_FLTADDOUT)pvBuffer;
980 uintptr_t uId = 0;
981 int rc;
982 if (RT_UNLIKELY(!pvBuffer || cbInBuffer != sizeof (*pFilter) || cbOutBuffer != sizeof (*pOut)))
983 {
984 AssertMsgFailed(("SUPUSBFLT_IOCTL_ADD_FILTER: Invalid input/output sizes. cbIn=%d expected %d. cbOut=%d expected %d.\n",
985 cbInBuffer, sizeof (*pFilter), cbOutBuffer, sizeof (*pOut)));
986 Status = STATUS_INVALID_PARAMETER;
987 break;
988 }
989
990 rc = VBoxUsbMonFltAdd(pContext, pFilter, &uId);
991 pOut->rc = rc;
992 pOut->uId = uId;
993 Info = sizeof (*pOut);
994 break;
995 }
996
997 case SUPUSBFLT_IOCTL_REMOVE_FILTER:
998 {
999 uintptr_t *pIn = (uintptr_t *)pvBuffer;
1000 int *pRc = (int *)pvBuffer;
1001
1002 if (!pvBuffer || cbInBuffer != sizeof (*pIn) || (cbOutBuffer && cbOutBuffer != sizeof (*pRc)))
1003 {
1004 AssertMsgFailed(("SUPUSBFLT_IOCTL_REMOVE_FILTER: Invalid input/output sizes. cbIn=%d expected %d. cbOut=%d expected %d.\n",
1005 cbInBuffer, sizeof (*pIn), cbOutBuffer, 0));
1006 Status = STATUS_INVALID_PARAMETER;
1007 break;
1008 }
1009 LOG(("SUPUSBFLT_IOCTL_REMOVE_FILTER %x\n", *pIn));
1010 int rc = VBoxUsbMonFltRemove(pContext, *pIn);
1011 if (cbOutBuffer)
1012 {
1013 /* we've validated that already */
1014 Assert(cbOutBuffer == *pRc);
1015 *pRc = rc;
1016 Info = sizeof (*pRc);
1017 }
1018 break;
1019 }
1020
1021 case SUPUSBFLT_IOCTL_RUN_FILTERS:
1022 {
1023 if (pvBuffer || cbInBuffer || cbOutBuffer)
1024 {
1025 AssertMsgFailed(("SUPUSBFLT_IOCTL_RUN_FILTERS: Invalid input/output sizes. cbIn=%d expected %d. cbOut=%d expected %d.\n",
1026 cbInBuffer, 0, cbOutBuffer, 0));
1027 Status = STATUS_INVALID_PARAMETER;
1028 break;
1029 }
1030 LOG(("SUPUSBFLT_IOCTL_RUN_FILTERS \n"));
1031 Status = VBoxUsbMonRunFilters(pContext);
1032 Assert(Status == STATUS_SUCCESS);
1033 Assert(Status != STATUS_PENDING);
1034 break;
1035 }
1036
1037 case SUPUSBFLT_IOCTL_GET_DEVICE:
1038 {
1039 HVBOXUSBDEVUSR hDevice = *((HVBOXUSBDEVUSR*)pvBuffer);
1040 PUSBSUP_GETDEV_MON pOut = (PUSBSUP_GETDEV_MON)pvBuffer;
1041 if (!pvBuffer || cbInBuffer != sizeof (hDevice) || cbOutBuffer < sizeof (*pOut))
1042 {
1043 AssertMsgFailed(("SUPUSBFLT_IOCTL_GET_DEVICE: Invalid input/output sizes. cbIn=%d expected %d. cbOut=%d expected >= %d.\n",
1044 cbInBuffer, sizeof (hDevice), cbOutBuffer, sizeof (*pOut)));
1045 Status = STATUS_INVALID_PARAMETER;
1046 break;
1047 }
1048
1049 Status = VBoxUsbMonGetDevice(pContext, hDevice, pOut);
1050
1051 if (NT_SUCCESS(Status))
1052 {
1053 Info = sizeof (*pOut);
1054 }
1055 else
1056 {
1057 AssertFailed();
1058 }
1059 break;
1060 }
1061
1062 case SUPUSBFLT_IOCTL_SET_NOTIFY_EVENT:
1063 {
1064 PUSBSUP_SET_NOTIFY_EVENT pSne = (PUSBSUP_SET_NOTIFY_EVENT)pvBuffer;
1065 if (!pvBuffer || cbInBuffer != sizeof (*pSne) || cbOutBuffer != sizeof (*pSne))
1066 {
1067 AssertMsgFailed(("SUPUSBFLT_IOCTL_SET_NOTIFY_EVENT: Invalid input/output sizes. cbIn=%d expected %d. cbOut=%d expected %d.\n",
1068 cbInBuffer, sizeof (*pSne), cbOutBuffer, sizeof (*pSne)));
1069 Status = STATUS_INVALID_PARAMETER;
1070 break;
1071 }
1072
1073 pSne->u.rc = VBoxUsbMonSetNotifyEvent(pContext, pSne->u.hEvent);
1074 Info = sizeof (*pSne);
1075 break;
1076 }
1077
1078 default:
1079 Status = STATUS_INVALID_PARAMETER;
1080 break;
1081 }
1082
1083 Assert(Status != STATUS_PENDING);
1084
1085 *pInfo = Info;
1086 return Status;
1087}
1088
1089static NTSTATUS _stdcall VBoxUsbMonDeviceControl(PDEVICE_OBJECT pDevObj, PIRP pIrp)
1090{
1091 ULONG_PTR Info = 0;
1092 NTSTATUS Status = IoAcquireRemoveLock(&g_VBoxUsbMonGlobals.RmLock, pDevObj);
1093 if (NT_SUCCESS(Status))
1094 {
1095 PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
1096 PFILE_OBJECT pFileObj = pSl->FileObject;
1097 Assert(pFileObj);
1098 Assert(pFileObj->FsContext);
1099 PVBOXUSBMONCTX pCtx = (PVBOXUSBMONCTX)pFileObj->FsContext;
1100 Assert(pCtx);
1101 Status = vboxUsbMonIoctlDispatch(pCtx,
1102 pSl->Parameters.DeviceIoControl.IoControlCode,
1103 pIrp->AssociatedIrp.SystemBuffer,
1104 pSl->Parameters.DeviceIoControl.InputBufferLength,
1105 pSl->Parameters.DeviceIoControl.OutputBufferLength,
1106 &Info);
1107 Assert(Status != STATUS_PENDING);
1108
1109 IoReleaseRemoveLock(&g_VBoxUsbMonGlobals.RmLock, pDevObj);
1110 }
1111
1112 pIrp->IoStatus.Information = Info;
1113 pIrp->IoStatus.Status = Status;
1114 IoCompleteRequest (pIrp, IO_NO_INCREMENT);
1115 return Status;
1116}
1117
1118static NTSTATUS vboxUsbMonInternalIoctlDispatch(ULONG Ctl, PVOID pvBuffer, ULONG_PTR *pInfo)
1119{
1120 NTSTATUS Status = STATUS_SUCCESS;
1121 *pInfo = 0;
1122 switch (Ctl)
1123 {
1124 case VBOXUSBIDC_INTERNAL_IOCTL_GET_VERSION:
1125 {
1126 PVBOXUSBIDC_VERSION pOut = (PVBOXUSBIDC_VERSION)pvBuffer;
1127
1128 LOG(("VBOXUSBIDC_INTERNAL_IOCTL_GET_VERSION\n"));
1129 if (!pvBuffer)
1130 {
1131 AssertMsgFailed(("VBOXUSBIDC_INTERNAL_IOCTL_GET_VERSION: Buffer is NULL\n"));
1132 Status = STATUS_INVALID_PARAMETER;
1133 break;
1134 }
1135 pOut->u32Major = VBOXUSBIDC_VERSION_MAJOR;
1136 pOut->u32Minor = VBOXUSBIDC_VERSION_MINOR;
1137 break;
1138 }
1139
1140 case VBOXUSBIDC_INTERNAL_IOCTL_PROXY_STARTUP:
1141 {
1142 PVBOXUSBIDC_PROXY_STARTUP pOut = (PVBOXUSBIDC_PROXY_STARTUP)pvBuffer;
1143
1144 LOG(("VBOXUSBIDC_INTERNAL_IOCTL_PROXY_STARTUP\n"));
1145 if (!pvBuffer)
1146 {
1147 AssertMsgFailed(("VBOXUSBIDC_INTERNAL_IOCTL_GET_VERSION: Buffer is NULL\n"));
1148 Status = STATUS_INVALID_PARAMETER;
1149 break;
1150 }
1151
1152 pOut->u.hDev = VBoxUsbFltProxyStarted(pOut->u.pPDO);
1153 break;
1154 }
1155
1156 case VBOXUSBIDC_INTERNAL_IOCTL_PROXY_TEARDOWN:
1157 {
1158 PVBOXUSBIDC_PROXY_TEARDOWN pOut = (PVBOXUSBIDC_PROXY_TEARDOWN)pvBuffer;
1159
1160 LOG(("VBOXUSBIDC_INTERNAL_IOCTL_PROXY_TEARDOWN\n"));
1161 if (!pvBuffer)
1162 {
1163 AssertMsgFailed(("VBOXUSBIDC_INTERNAL_IOCTL_PROXY_TEARDOWN: Buffer is NULL\n"));
1164 Status = STATUS_INVALID_PARAMETER;
1165 break;
1166 }
1167
1168 VBoxUsbFltProxyStopped(pOut->hDev);
1169 break;
1170 }
1171
1172 default:
1173 {
1174 AssertFailed();
1175 Status = STATUS_INVALID_PARAMETER;
1176 break;
1177 }
1178 }
1179
1180 return Status;
1181}
1182
1183static NTSTATUS _stdcall VBoxUsbMonInternalDeviceControl(PDEVICE_OBJECT pDevObj, PIRP pIrp)
1184{
1185 ULONG_PTR Info = 0;
1186 NTSTATUS Status = IoAcquireRemoveLock(&g_VBoxUsbMonGlobals.RmLock, pDevObj);
1187 if (NT_SUCCESS(Status))
1188 {
1189 PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
1190 Status = vboxUsbMonInternalIoctlDispatch(pSl->Parameters.DeviceIoControl.IoControlCode,
1191 pSl->Parameters.Others.Argument1,
1192 &Info);
1193 Assert(Status != STATUS_PENDING);
1194
1195 IoReleaseRemoveLock(&g_VBoxUsbMonGlobals.RmLock, pDevObj);
1196 }
1197
1198 pIrp->IoStatus.Information = Info;
1199 pIrp->IoStatus.Status = Status;
1200 IoCompleteRequest (pIrp, IO_NO_INCREMENT);
1201 return Status;
1202}
1203
1204/**
1205 * Unload the driver.
1206 *
1207 * @param pDrvObj Driver object.
1208 */
1209static void _stdcall VBoxUsbMonUnload(PDRIVER_OBJECT pDrvObj)
1210{
1211 LOG(("VBoxUSBMonUnload pDrvObj (0x%p)\n", pDrvObj));
1212
1213 IoReleaseRemoveLockAndWait(&g_VBoxUsbMonGlobals.RmLock, &g_VBoxUsbMonGlobals);
1214
1215 Assert(!g_VBoxUsbMonGlobals.cOpens);
1216
1217 UNICODE_STRING DosName;
1218 RtlInitUnicodeString(&DosName, USBMON_DEVICE_NAME_DOS);
1219 NTSTATUS rc = IoDeleteSymbolicLink(&DosName);
1220
1221 IoDeleteDevice(g_VBoxUsbMonGlobals.pDevObj);
1222
1223 /* cleanup the logger */
1224 PRTLOGGER pLogger = RTLogRelSetDefaultInstance(NULL);
1225 if (pLogger)
1226 {
1227 RTLogDestroy(pLogger);
1228 }
1229 pLogger = RTLogSetDefaultInstance(NULL);
1230 if (pLogger)
1231 {
1232 RTLogDestroy(pLogger);
1233 }
1234}
1235
1236RT_C_DECLS_BEGIN
1237NTSTATUS _stdcall DriverEntry(PDRIVER_OBJECT pDrvObj, PUNICODE_STRING pRegPath);
1238RT_C_DECLS_END
1239
1240/**
1241 * Driver entry point.
1242 *
1243 * @returns appropriate status code.
1244 * @param pDrvObj Pointer to driver object.
1245 * @param pRegPath Registry base path.
1246 */
1247NTSTATUS _stdcall DriverEntry(PDRIVER_OBJECT pDrvObj, PUNICODE_STRING pRegPath)
1248{
1249#ifdef DEBUG_misha
1250 RTLogGroupSettings(0, "+default.e.l.f.l2.l3");
1251 RTLogDestinations(0, "debugger");
1252#endif
1253
1254 LOG(("VBoxUSBMon::DriverEntry\n"));
1255
1256 memset (&g_VBoxUsbMonGlobals, 0, sizeof (g_VBoxUsbMonGlobals));
1257 KeInitializeEvent(&g_VBoxUsbMonGlobals.OpenSynchEvent, SynchronizationEvent, TRUE /* signaled */);
1258 IoInitializeRemoveLock(&g_VBoxUsbMonGlobals.RmLock, VBOXUSBMON_MEMTAG, 1, 100);
1259 UNICODE_STRING DevName;
1260 PDEVICE_OBJECT pDevObj;
1261 /* create the device */
1262 RtlInitUnicodeString(&DevName, USBMON_DEVICE_NAME_NT);
1263 NTSTATUS Status = IoAcquireRemoveLock(&g_VBoxUsbMonGlobals.RmLock, &g_VBoxUsbMonGlobals);
1264 if (NT_SUCCESS(Status))
1265 {
1266 Status = IoCreateDevice(pDrvObj, sizeof (VBOXUSBMONINS), &DevName, FILE_DEVICE_UNKNOWN, 0, FALSE, &pDevObj);
1267 if (NT_SUCCESS(Status))
1268 {
1269 UNICODE_STRING DosName;
1270 RtlInitUnicodeString(&DosName, USBMON_DEVICE_NAME_DOS);
1271 Status = IoCreateSymbolicLink(&DosName, &DevName);
1272 if (NT_SUCCESS(Status))
1273 {
1274 PVBOXUSBMONINS pDevExt = (PVBOXUSBMONINS)pDevObj->DeviceExtension;
1275 memset(pDevExt, 0, sizeof(*pDevExt));
1276
1277 pDrvObj->DriverUnload = VBoxUsbMonUnload;
1278 pDrvObj->MajorFunction[IRP_MJ_CREATE] = VBoxUsbMonCreate;
1279 pDrvObj->MajorFunction[IRP_MJ_CLOSE] = VBoxUsbMonClose;
1280 pDrvObj->MajorFunction[IRP_MJ_DEVICE_CONTROL] = VBoxUsbMonDeviceControl;
1281 pDrvObj->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = VBoxUsbMonInternalDeviceControl;
1282
1283 g_VBoxUsbMonGlobals.pDevObj = pDevObj;
1284 LOG(("VBoxUSBMon::DriverEntry returning STATUS_SUCCESS\n"));
1285 return STATUS_SUCCESS;
1286 }
1287 IoDeleteDevice(pDevObj);
1288 }
1289 IoReleaseRemoveLockAndWait(&g_VBoxUsbMonGlobals.RmLock, &g_VBoxUsbMonGlobals);
1290 }
1291
1292 return Status;
1293}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette