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
|
/* 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"
static NTSTATUS DDKAPI
on_start_complete(DEVICE_OBJECT *device_object, IRP *irp,
void *context);
static NTSTATUS DDKAPI
on_device_usage_notification_complete(DEVICE_OBJECT *device_object,
IRP *irp, void *context);
static NTSTATUS DDKAPI
on_query_capabilities_complete(DEVICE_OBJECT *device_object,
IRP *irp, void *context);
NTSTATUS dispatch_pnp(libusb_device_t *dev, IRP *irp)
{
NTSTATUS status = STATUS_SUCCESS;
IO_STACK_LOCATION *stack_location = IoGetCurrentIrpStackLocation(irp);
UNICODE_STRING symbolic_link_name;
WCHAR tmp_name[128];
status = remove_lock_acquire(dev);
if (!NT_SUCCESS(status))
{
USBDBG("device %s is pending removal\n",dev->device_id);
return complete_irp(irp, status, 0);
}
switch (stack_location->MinorFunction)
{
case IRP_MN_REMOVE_DEVICE:
USBMSG("IRP_MN_REMOVE_DEVICE: is-filter=%c %s\n",
dev->is_filter ? 'Y' : 'N',
dev->device_id);
if (dev->device_interface_in_use && dev->is_started)
{
set_filter_interface_key(dev,(ULONG)-1);
status = IoSetDeviceInterfaceState(&dev->device_interface_name, FALSE);
if (!NT_SUCCESS(status))
{
USBERR0("IRP_MN_REMOVE_DEVICE: disabling device interface failed\n");
}
}
dev->is_started = FALSE;
/* wait until all outstanding requests are finished */
remove_lock_release_and_wait(dev);
status = pass_irp_down(dev, irp, NULL, NULL);
USBMSG("deleting device #%d %s\n", dev->id, dev->device_id);
_snwprintf(tmp_name, sizeof(tmp_name)/sizeof(WCHAR), L"%s%04d",
LIBUSB_SYMBOLIC_LINK_NAME, dev->id);
/* delete the symbolic link */
RtlInitUnicodeString(&symbolic_link_name, tmp_name);
IoDeleteSymbolicLink(&symbolic_link_name);
if (dev->device_interface_in_use && dev->device_interface_name.Buffer)
{
RtlFreeUnicodeString(&dev->device_interface_name);
}
UpdateContextConfigDescriptor(dev,NULL,0,0,-1);
/* delete the device object */
IoDetachDevice(dev->next_stack_device);
IoDeleteDevice(dev->self);
return status;
case IRP_MN_SURPRISE_REMOVAL:
dev->is_started = FALSE;
USBMSG("IRP_MN_SURPRISE_REMOVAL: is-filter=%c %s\n",
dev->is_filter ? 'Y' : 'N',
dev->device_id);
if (dev->device_interface_in_use)
{
set_filter_interface_key(dev,(ULONG)-1);
IoSetDeviceInterfaceState(&dev->device_interface_name, FALSE);
dev->device_interface_in_use=FALSE;
if (dev->device_interface_name.Buffer)
{
RtlFreeUnicodeString(&dev->device_interface_name);
dev->device_interface_name.Buffer = NULL;
}
}
UpdateContextConfigDescriptor(dev,NULL,0,0,-1);
status = STATUS_SUCCESS;
break;
case IRP_MN_START_DEVICE:
USBMSG("IRP_MN_START_DEVICE: is-filter=%c %s\n",
dev->is_filter ? 'Y' : 'N',
dev->device_id);
// A driver calls this routine after receiving a device set-power
// request and before calling PoStartNextPowerIrp. When handling a
// PnP IRP_MN_START_DEVICE request, the driver should call
// PoSetPowerState to notify the power manager that the device is
// in the D0 state.
//
PoSetPowerState(dev->self, DevicePowerState, dev->power_state);
if (dev->device_interface_in_use)
{
status = IoSetDeviceInterfaceState(&dev->device_interface_name, TRUE);
if (!NT_SUCCESS(status))
{
USBERR0("IRP_MN_START_DEVICE: enabling device interface failed\n");
}
}
return pass_irp_down(dev, irp, on_start_complete, NULL);
case IRP_MN_STOP_DEVICE:
dev->is_started = FALSE;
USBDBG("IRP_MN_STOP_DEVICE: is-filter=%c %s\n",
dev->is_filter ? 'Y' : 'N',
dev->device_id);
break;
case IRP_MN_DEVICE_USAGE_NOTIFICATION:
USBDBG("IRP_MN_DEVICE_USAGE_NOTIFICATION: is-filter=%c %s\n",
dev->is_filter ? 'Y' : 'N',
dev->device_id);
if (!dev->self->AttachedDevice
|| (dev->self->AttachedDevice->Flags & DO_POWER_PAGABLE))
{
dev->self->Flags |= DO_POWER_PAGABLE;
}
return pass_irp_down(dev, irp, on_device_usage_notification_complete, NULL);
case IRP_MN_QUERY_CAPABILITIES:
USBDBG("IRP_MN_QUERY_CAPABILITIES: is-filter=%c %s\n",
dev->is_filter ? 'Y' : 'N',
dev->device_id);
if (!dev->is_filter)
{
/* apply registry setting */
stack_location->Parameters.DeviceCapabilities.Capabilities->SurpriseRemovalOK = dev->surprise_removal_ok;
}
return pass_irp_down(dev, irp, on_query_capabilities_complete, NULL);
default:
break;
}
remove_lock_release(dev);
return pass_irp_down(dev, irp, NULL, NULL);
}
static NTSTATUS DDKAPI
on_start_complete(DEVICE_OBJECT *device_object, IRP *irp, void *context)
{
libusb_device_t *dev = device_object->DeviceExtension;
if (irp->PendingReturned)
{
IoMarkIrpPending(irp);
}
USBDBG("is-filter=%c %s\n",
dev->is_filter ? 'Y' : 'N',
dev->device_id);
if (dev->next_stack_device->Characteristics & FILE_REMOVABLE_MEDIA)
{
device_object->Characteristics |= FILE_REMOVABLE_MEDIA;
}
#ifndef SKIP_CONFIGURE_NORMAL_DEVICES
// select initial configuration if not a filter
if (!dev->is_filter && !dev->is_started)
{
// optionally, the initial configuration value can be specified
// in the inf file. See reg_get_properties()
// HKR,,"InitialConfigValue",0x00010001,<your config value>
// If initial_config_value is negative, the configuration will
// only be set if the device is not already configured.
if (dev->initial_config_value)
{
if (dev->initial_config_value == SET_CONFIG_ACTIVE_CONFIG)
{
USBDBG("applying active configuration for %s\n",
dev->device_id);
}
else
{
USBDBG("applying InitialConfigValue %d for %s\n",
dev->initial_config_value, dev->device_id);
}
if(!NT_SUCCESS(set_configuration(dev, dev->initial_config_value, LIBUSB_DEFAULT_TIMEOUT)))
{
// we should always be able to apply the active configuration,
// even in the case of composite devices.
if (dev->initial_config_value == SET_CONFIG_ACTIVE_CONFIG)
{
USBERR("failed applying active configuration for %s\n",
dev->device_id);
}
else
{
USBERR("failed applying InitialConfigValue %d for %s\n",
dev->initial_config_value, dev->device_id);
}
}
}
}
#endif
dev->is_started = TRUE;
remove_lock_release(dev);
return STATUS_SUCCESS;
}
static NTSTATUS DDKAPI
on_device_usage_notification_complete(DEVICE_OBJECT *device_object,
IRP *irp, void *context)
{
libusb_device_t *dev = device_object->DeviceExtension;
if (irp->PendingReturned)
{
IoMarkIrpPending(irp);
}
if (!(dev->next_stack_device->Flags & DO_POWER_PAGABLE))
{
device_object->Flags &= ~DO_POWER_PAGABLE;
}
remove_lock_release(dev);
return STATUS_SUCCESS;
}
static NTSTATUS DDKAPI
on_query_capabilities_complete(DEVICE_OBJECT *device_object,
IRP *irp, void *context)
{
libusb_device_t *dev = device_object->DeviceExtension;
IO_STACK_LOCATION *stack_location = IoGetCurrentIrpStackLocation(irp);
if (irp->PendingReturned)
{
IoMarkIrpPending(irp);
}
if (NT_SUCCESS(irp->IoStatus.Status))
{
if (!dev->is_filter)
{
/* apply registry setting */
stack_location->Parameters.DeviceCapabilities.Capabilities
->SurpriseRemovalOK = dev->surprise_removal_ok;
}
/* save supported device power states */
memcpy(dev->device_power_states, stack_location
->Parameters.DeviceCapabilities.Capabilities->DeviceState,
sizeof(dev->device_power_states));
}
remove_lock_release(dev);
return STATUS_SUCCESS;
}
|