1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
/* libusb-win32, Generic Windows USB Library
* Copyright (c) 2002-2005 Stephan Meyer <ste_meyer@web.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "libusb_driver.h"
#include "lusb_defdi_guids.h"
/* missing in mingw's ddk headers */
#ifndef PLUGPLAY_REGKEY_DEVICE
#define PLUGPLAY_REGKEY_DEVICE 1
#endif
#define LIBUSB_REG_SURPRISE_REMOVAL_OK L"SurpriseRemovalOK"
#define LIBUSB_REG_INITIAL_CONFIG_VALUE L"InitialConfigValue"
#define LIBUSB_REG_DEVICE_INTERFACE_GUIDS L"DeviceInterfaceGUIDs"
static bool_t reg_get_property(DEVICE_OBJECT *physical_device_object,
int property, char *data, int size);
static bool_t reg_get_property(DEVICE_OBJECT *physical_device_object,
int property, char *data, int size)
{
WCHAR tmp[512];
ULONG ret;
ULONG i;
if (!physical_device_object || !data || !size)
{
return FALSE;
}
memset(data, 0, size);
memset(tmp, 0, sizeof(tmp));
if (NT_SUCCESS(IoGetDeviceProperty(physical_device_object,
property,
sizeof(tmp) - 2,
tmp,
&ret)) && ret)
{
/* convert unicode string to normal character string */
for (i = 0; (i < ret/2) && (i < ((ULONG)size - 1)); i++)
{
data[i] = (char)tmp[i];
}
_strlwr(data);
return TRUE;
}
return FALSE;
}
bool_t reg_get_properties(libusb_device_t *dev)
{
HANDLE key = NULL;
PVOID keyObject = NULL;
NTSTATUS status;
UNICODE_STRING surprise_removal_ok_name;
UNICODE_STRING initial_config_value_name;
UNICODE_STRING device_interface_guids;
UNICODE_STRING device_interface_guid_value;
KEY_VALUE_FULL_INFORMATION *info;
ULONG pool_length;
ULONG length;
ULONG val;
LPWSTR valW;
if (!dev->physical_device_object)
{
return FALSE;
}
/* default settings */
dev->surprise_removal_ok = FALSE;
dev->is_filter = TRUE;
dev->initial_config_value = SET_CONFIG_ACTIVE_CONFIG;
status = IoOpenDeviceRegistryKey(dev->physical_device_object,
PLUGPLAY_REGKEY_DEVICE,
STANDARD_RIGHTS_ALL,
&key);
if (NT_SUCCESS(status))
{
RtlInitUnicodeString(&surprise_removal_ok_name,
LIBUSB_REG_SURPRISE_REMOVAL_OK);
RtlInitUnicodeString(&initial_config_value_name,
LIBUSB_REG_INITIAL_CONFIG_VALUE);
RtlInitUnicodeString(&device_interface_guids,
LIBUSB_REG_DEVICE_INTERFACE_GUIDS);
pool_length = sizeof(KEY_VALUE_FULL_INFORMATION) + 512;
info = ExAllocatePool(NonPagedPool, pool_length);
if (!info)
{
ZwClose(key);
USBERR("ExAllocatePool failed allocating %d bytes\n", pool_length);
return FALSE;
}
// get surprise_removal_ok
// get is_filter
length = pool_length;
memset(info, 0, length);
status = ZwQueryValueKey(key, &surprise_removal_ok_name,
KeyValueFullInformation, info, length, &length);
if (NT_SUCCESS(status) && (info->Type == REG_DWORD))
{
val = *((ULONG *)(((char *)info) + info->DataOffset));
dev->surprise_removal_ok = val ? TRUE : FALSE;
dev->is_filter = FALSE;
}
if (!dev->is_filter)
{
// get device interface guid
length = pool_length;
memset(info, 0, length);
status = ZwQueryValueKey(key, &device_interface_guids,
KeyValueFullInformation, info, length, &length);
if (NT_SUCCESS(status) && (info->Type == REG_MULTI_SZ))
{
valW = ((LPWSTR)(((char *)info) + info->DataOffset));
RtlInitUnicodeString(&device_interface_guid_value,valW);
status = RtlGUIDFromString(&device_interface_guid_value, &dev->device_interface_guid);
if (NT_SUCCESS(status))
{
if (IsEqualGUID(&dev->device_interface_guid, &LibusbKDeviceGuid))
{
USBWRN0("libusbK default device DeviceInterfaceGUID found. skippng..\n");
RtlInitUnicodeString(&device_interface_guid_value,Libusb0DeviceGuidW);
}
else if (IsEqualGUID(&dev->device_interface_guid, &Libusb0FilterGuid))
{
USBWRN0("libusb0 filter DeviceInterfaceGUID found. skippng..\n");
RtlInitUnicodeString(&device_interface_guid_value,Libusb0DeviceGuidW);
}
else if (IsEqualGUID(&dev->device_interface_guid, &Libusb0DeviceGuid))
{
USBWRN0("libusb0 device DeviceInterfaceGUID found. skippng..\n");
RtlInitUnicodeString(&device_interface_guid_value,Libusb0DeviceGuidW);
}
else
{
USBMSG0("found user specified device interface guid.\n");
dev->device_interface_in_use = TRUE;
}
}
else
{
USBERR0("invalid user specified device interface guid.");
}
}
if (!dev->device_interface_in_use)
{
RtlInitUnicodeString(&device_interface_guid_value,Libusb0DeviceGuidW);
}
}
else
{
RtlInitUnicodeString(&device_interface_guid_value,Libusb0FilterGuidW);
}
if (!dev->device_interface_in_use)
{
status = RtlGUIDFromString(&device_interface_guid_value, &dev->device_interface_guid);
if (NT_SUCCESS(status))
{
USBMSG0("using default device interface guid.\n");
dev->device_interface_in_use = TRUE;
}
else
{
USBERR0("failed using default device interface guid.\n");
}
}
// get initial_config_value
length = pool_length;
memset(info, 0, length);
status = ZwQueryValueKey(key, &initial_config_value_name,
KeyValueFullInformation, info, length, &length);
if (NT_SUCCESS(status) && (info->Type == REG_DWORD))
{
val = *((ULONG *)(((char *)info) + info->DataOffset));
dev->initial_config_value = (int)val;
}
status = ObReferenceObjectByHandle(key, KEY_READ, NULL, KernelMode, &keyObject, NULL);
if (NT_SUCCESS(status))
{
length = pool_length;
memset(info, 0, length);
status = ObQueryNameString(keyObject, (POBJECT_NAME_INFORMATION)info, length, &length);
if (NT_SUCCESS(status))
{
PWSTR nameW =((POBJECT_NAME_INFORMATION)info)->Name.Buffer;
PSTR nameA = dev->objname_plugplay_registry_key;
val=0;
while (nameW[val] && val < (length/2))
{
*nameA=(char)nameW[val];
nameA++;
val++;
}
*nameA='\0';
USBDBG("reg-key-name=%s\n",dev->objname_plugplay_registry_key);
}
else
{
USBERR("ObQueryNameString failed. status=%Xh\n",status);
}
ObDereferenceObject(keyObject);
}
else
{
USBERR("ObReferenceObjectByHandle failed. status=%Xh\n",status);
}
ZwClose(key);
ExFreePool(info);
}
return TRUE;
}
bool_t reg_get_hardware_id(DEVICE_OBJECT *physical_device_object,
char *data, int size)
{
if (!physical_device_object || !data || !size)
{
return FALSE;
}
return reg_get_property(physical_device_object, DevicePropertyHardwareID,
data, size);
}
bool_t reg_get_compatible_id(DEVICE_OBJECT *physical_device_object,
char *data, int size)
{
if (!physical_device_object || !data || !size)
{
return FALSE;
}
return reg_get_property(physical_device_object, DevicePropertyCompatibleIDs,
data, size);
}
/*
Gets a device property for the device_object.
Returns: NTSTATUS code from IoGetDeviceProperty
STATUS_INVALID_PARAMETER
*/
NTSTATUS reg_get_device_property(PDEVICE_OBJECT device_object,
int property,
char* data_buffer,
int data_length,
int* actual_length)
{
NTSTATUS status = STATUS_INVALID_PARAMETER;
if (!device_object || !data_buffer || !data_length || !actual_length)
return status;
// clear data
memset(data_buffer, 0, data_length);
*actual_length=0;
// get device property
status = IoGetDeviceProperty(
device_object, property, data_length, data_buffer, (PULONG)actual_length);
return status;
}
/*
Gets a property from the device registry key of the device_object.
Returns: NTSTATUS from ZwQueryValueKey
NTSTATUS from IoOpenDeviceRegistryKey if the device registry
key could not be opened.
*/
NTSTATUS reg_get_custom_property(PDEVICE_OBJECT device_object,
char *data_buffer,
unsigned int data_length,
unsigned int name_offset,
int* actual_length)
{
HANDLE key = NULL;
NTSTATUS status;
UNICODE_STRING name;
KEY_VALUE_FULL_INFORMATION *info;
ULONG length;
status = IoOpenDeviceRegistryKey(device_object, PLUGPLAY_REGKEY_DEVICE, KEY_READ, &key);
if (NT_SUCCESS(status))
{
RtlInitUnicodeString(&name, (WCHAR*)(&data_buffer[name_offset]));
length = sizeof(KEY_VALUE_FULL_INFORMATION) + name.MaximumLength + data_length;
info = ExAllocatePool(NonPagedPool, length);
if (info)
{
memset(info, 0, length);
status = ZwQueryValueKey(key, &name, KeyValueFullInformation, info, length, &length);
if (NT_SUCCESS(status))
{
data_length = (info->DataLength > data_length) ? data_length : info->DataLength;
memcpy(data_buffer, (((char *)info) + info->DataOffset),data_length);
*actual_length=data_length;
}
ExFreePool(info);
}
ZwClose(key);
}
return status;
}
VOID set_filter_interface_key(libusb_device_t *dev, ULONG id)
{
if (dev->device_interface_in_use)
{
HANDLE hKey=NULL;
if (NT_SUCCESS(IoOpenDeviceInterfaceRegistryKey(&dev->device_interface_name,KEY_ALL_ACCESS,&hKey)))
{
UNICODE_STRING valueName;
RtlInitUnicodeString(&valueName, L"LUsb0");
if (NT_SUCCESS(ZwSetValueKey(hKey,&valueName, 0, REG_DWORD, &id,sizeof(ULONG))))
{
USBMSG("updated interface registry with LUsb0 direct-access symbolic link. id=%d\n",id);
}
else
{
USBERR0("IoOpenDeviceInterfaceRegistryKey:ZwSetValueKey failed\n");
}
ZwClose(hKey);
}
else
{
USBERR0("IoOpenDeviceInterfaceRegistryKey failed\n");
}
}
}
|