[CMAKE]
[reactos.git] / drivers / usb / usbehci / misc.c
1 /*
2 * PROJECT: ReactOS Universal Serial Bus Bulk Enhanced Host Controller Interface
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: drivers/usb/usbehci/misc.c
5 * PURPOSE: Misceallenous operations.
6 * PROGRAMMERS:
7 * Michael Martin
8 */
9
10 #include "usbehci.h"
11 #include <hubbusif.h>
12 #include <usbbusif.h>
13
14 /*
15 Get SymblicName from Parameters in Registry Key
16 Caller is responsible for freeing pool of returned pointer
17 */
18 PWSTR
19 GetSymbolicName(PDEVICE_OBJECT DeviceObject)
20 {
21 NTSTATUS Status;
22 HANDLE DevInstRegKey;
23 UNICODE_STRING SymbolicName;
24 PKEY_VALUE_PARTIAL_INFORMATION KeyPartInfo;
25 ULONG SizeNeeded;
26 PWCHAR SymbolicNameString = NULL;
27
28 Status = IoOpenDeviceRegistryKey(DeviceObject,
29 PLUGPLAY_REGKEY_DEVICE,
30 STANDARD_RIGHTS_ALL,
31 &DevInstRegKey);
32
33 DPRINT("IoOpenDeviceRegistryKey PLUGPLAY_REGKEY_DEVICE Status %x\n", Status);
34
35 if (NT_SUCCESS(Status))
36 {
37 RtlInitUnicodeString(&SymbolicName, L"SymbolicName");
38 Status = ZwQueryValueKey(DevInstRegKey,
39 &SymbolicName,
40 KeyValuePartialInformation,
41 NULL,
42 0,
43 &SizeNeeded);
44
45 DPRINT("ZwQueryValueKey status %x, %d\n", Status, SizeNeeded);
46
47 if (Status == STATUS_BUFFER_TOO_SMALL)
48 {
49 KeyPartInfo = (PKEY_VALUE_PARTIAL_INFORMATION ) ExAllocatePool(PagedPool, SizeNeeded);
50 if (!KeyPartInfo)
51 {
52 DPRINT1("OUT OF MEMORY\n");
53 return NULL;
54 }
55 else
56 {
57 Status = ZwQueryValueKey(DevInstRegKey,
58 &SymbolicName,
59 KeyValuePartialInformation,
60 KeyPartInfo,
61 SizeNeeded,
62 &SizeNeeded);
63
64 SymbolicNameString = ExAllocatePool(PagedPool, (KeyPartInfo->DataLength + sizeof(WCHAR)));
65 if (!SymbolicNameString)
66 {
67 return NULL;
68 }
69 RtlZeroMemory(SymbolicNameString, KeyPartInfo->DataLength + 2);
70 RtlCopyMemory(SymbolicNameString, KeyPartInfo->Data, KeyPartInfo->DataLength);
71 }
72
73 ExFreePool(KeyPartInfo);
74 }
75
76 ZwClose(DevInstRegKey);
77 }
78
79 return SymbolicNameString;
80 }
81
82 /*
83 Get Physical Device Object Name from registry
84 Caller is responsible for freeing pool
85 */
86 PWSTR
87 GetPhysicalDeviceObjectName(PDEVICE_OBJECT DeviceObject)
88 {
89 NTSTATUS Status;
90 PWSTR ObjectName = NULL;
91 ULONG SizeNeeded;
92
93 Status = IoGetDeviceProperty(DeviceObject,
94 DevicePropertyPhysicalDeviceObjectName,
95 0,
96 NULL,
97 &SizeNeeded);
98
99 if (Status != STATUS_BUFFER_TOO_SMALL)
100 {
101 DPRINT1("Expected STATUS_BUFFER_TOO_SMALL, got %x!\n", Status);
102 return NULL;
103 }
104
105 ObjectName = (PWSTR) ExAllocatePool(PagedPool, SizeNeeded + sizeof(WCHAR));
106 if (!ObjectName)
107 {
108 DPRINT1("Out of memory\n");
109 return NULL;
110 }
111
112 Status = IoGetDeviceProperty(DeviceObject,
113 DevicePropertyPhysicalDeviceObjectName,
114 SizeNeeded,
115 ObjectName,
116 &SizeNeeded);
117 if (!NT_SUCCESS(Status))
118 {
119 DPRINT1("Failed to Get Property\n");
120 return NULL;
121 }
122
123 return ObjectName;
124 }
125
126 PUSB_DEVICE DeviceHandleToUsbDevice(PPDO_DEVICE_EXTENSION PdoDeviceExtension, PUSB_DEVICE_HANDLE DeviceHandle)
127 {
128 LONG i;
129
130 for (i=0; i<127; i++)
131 {
132 if (PdoDeviceExtension->UsbDevices[i] == (PUSB_DEVICE)DeviceHandle)
133 return (PUSB_DEVICE)DeviceHandle;
134 }
135 return NULL;
136 }