forked from cezanne/usbip-win
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stub_pnp.c
117 lines (95 loc) · 3.25 KB
/
stub_pnp.c
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
/* libusb-win32, Generic Windows USB Library
* Copyright (c) 2002-2005 Stephan Meyer <[email protected]>
*
* 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 "stub_driver.h"
#include "stub_dbg.h"
#include "stub_irp.h"
static NTSTATUS
on_start_complete(DEVICE_OBJECT *devobj, IRP *irp, void *context)
{
usbip_stub_dev_t *devstub = (usbip_stub_dev_t *)devobj->DeviceExtension;
UNREFERENCED_PARAMETER(context);
if (irp->PendingReturned) {
IoMarkIrpPending(irp);
}
devstub->is_started = TRUE;
unlock_dev_removal(devstub);
return STATUS_SUCCESS;
}
static void
disable_interface(usbip_stub_dev_t *devstub)
{
NTSTATUS status;
if (devstub->interface_name.Buffer == NULL)
return;
status = IoSetDeviceInterfaceState(&devstub->interface_name, FALSE);
if (NT_ERROR(status)) {
DBGE(DBG_PNP, "failed to disable interface: err: %s\n", dbg_ntstatus(status));
}
if (devstub->interface_name.Buffer) {
RtlFreeUnicodeString(&devstub->interface_name);
devstub->interface_name.Buffer = NULL;
}
}
NTSTATUS
stub_dispatch_pnp(usbip_stub_dev_t *devstub, IRP *irp)
{
IO_STACK_LOCATION *irpstack = IoGetCurrentIrpStackLocation(irp);
NTSTATUS status;
DBGI(DBG_DISPATCH, "dispatch_pnp: minor: %s\n", dbg_pnp_minor(irpstack->MinorFunction));
status = lock_dev_removal(devstub);
if (NT_ERROR(status)) {
DBGI(DBG_PNP, "device is pending removal: %s\n", dbg_devstub(devstub));
return complete_irp(irp, status, 0);
}
switch (irpstack->MinorFunction) {
case IRP_MN_START_DEVICE:
status = IoSetDeviceInterfaceState(&devstub->interface_name, TRUE);
if (NT_ERROR(status)) {
DBGE(DBG_PNP, "failed to enable interface: err: %s\n", dbg_ntstatus(status));
}
return pass_irp_down(devstub, irp, on_start_complete, NULL);
case IRP_MN_REMOVE_DEVICE:
disable_interface(devstub);
devstub->is_started = FALSE;
/* wait until all outstanding requests are finished */
unlock_wait_dev_removal(devstub);
/* USBD_CloseHandle should be ahead of pass_irp_down */
USBD_CloseHandle(devstub->hUSBD);
status = pass_irp_down(devstub, irp, NULL, NULL);
DBGI(DBG_PNP, "deleting device: %s\n", dbg_devstub(devstub));
remove_devlink(devstub);
free_devconf(devstub->devconf);
devstub->devconf = NULL;
/* delete the device object */
IoDetachDevice(devstub->next_stack_dev);
IoDeleteDevice(devstub->self);
return status;
case IRP_MN_SURPRISE_REMOVAL:
devstub->is_started = FALSE;
disable_interface(devstub);
status = STATUS_SUCCESS;
break;
case IRP_MN_STOP_DEVICE:
devstub->is_started = FALSE;
break;
default:
break;
}
unlock_dev_removal(devstub);
return pass_irp_down(devstub, irp, NULL, NULL);
}