summaryrefslogtreecommitdiff
path: root/usb/ufxclientsample/driver.c
blob: 1a1f5e1ec7814fad4b86021b5cb8e998fccddd00 (plain)
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
/*++

Copyright (c) Microsoft Corporation. All rights reserved.

Module Name:

    driver.c

Abstract:

    This file contains the driver entry points and callbacks.

Environment:

    Kernel-mode Driver Framework

--*/

#include "driver.h"
#include "device.h" 
#include "driver.tmh"


DRIVER_INITIALIZE DriverEntry;

//
// WDFDRIVER object Callbacks
//
EVT_WDF_DRIVER_DEVICE_ADD OnEvtDriverDeviceAdd;
EVT_WDF_DRIVER_UNLOAD OnEvtDriverUnload;


#ifdef ALLOC_PRAGMA
#pragma alloc_text (INIT, DriverEntry)
#pragma alloc_text (PAGE, OnEvtDriverDeviceAdd)
#pragma alloc_text (PAGE, OnEvtDriverUnload)
#endif


NTSTATUS
DriverEntry(
    _In_ PDRIVER_OBJECT  DriverObject,
    _In_ PUNICODE_STRING RegistryPath
    )
/*++

Routine Description:
    DriverEntry initializes the driver and is the first routine called by the
    system after the driver is loaded. DriverEntry specifies the other entry
    points in the function driver, such as EvtDevice and DriverUnload.

Parameters Description:

    DriverObject - represents the instance of the function driver that is loaded
    into memory. 

    RegistryPath - represents the driver specific path in the Registry.

Return Value:

    STATUS_SUCCESS if successful, appropriate NTSTATUS message otherwise.

--*/
{
    NTSTATUS Status;
    WDF_DRIVER_CONFIG DriverConfig;
    WDF_OBJECT_ATTRIBUTES DriverAttributes;
    
    //
    // Enable ETW tracing
    //
    EventRegisterUfxClientSample();
    //
    // Initialize WPP Tracing
    //
    WPP_INIT_TRACING(DriverObject, RegistryPath);

    TraceEntry();

    //
    // Register 'EvtDriverUnload' to de-register WPP and ETW.
    //
    WDF_OBJECT_ATTRIBUTES_INIT(&DriverAttributes);

    WDF_DRIVER_CONFIG_INIT(&DriverConfig, OnEvtDriverDeviceAdd);
    DriverConfig.DriverPoolTag = UFX_CLIENT_TAG;
    DriverConfig.EvtDriverUnload = OnEvtDriverUnload;

    Status = WdfDriverCreate(
                    DriverObject,
                    RegistryPath,
                    &DriverAttributes,
                    &DriverConfig,
                    WDF_NO_HANDLE);
    CHK_NT_MSG(Status, "Failed to create WDFDRIVER object");

End:

    if (NT_SUCCESS(Status) != TRUE) {

        //
        // Stop WPP Tracing
        //
        WPP_CLEANUP(DriverObject);

        //
        // Unregister ETW tracing
        //
        EventUnregisterUfxClientSample();
        goto Return;
    }

    TraceExit();
Return:

    return Status;
}

NTSTATUS
OnEvtDriverDeviceAdd(
    WDFDRIVER       Driver,
    PWDFDEVICE_INIT DeviceInit
    )
/*++
Routine Description:

    EvtDriverDeviceAdd is called by the framework in response to AddDevice
    call from the PnP manager. We create and initialize a device object to
    represent a new instance of the device.

Arguments:

    Driver - Handle to a framework driver object created in DriverEntry

    DeviceInit - Pointer to a framework-allocated WDFDEVICE_INIT structure.

Return Value:

    NTSTATUS

--*/
{
    NTSTATUS Status;

    PAGED_CODE();

    TraceEntry();

    Status = UfxClientDeviceCreate(Driver, DeviceInit);
    CHK_NT_MSG(Status, "Failed to create wdf device");

End:
    TraceExit();
    return Status;
}

VOID
OnEvtDriverUnload(
    _In_ WDFDRIVER WdfDriver
    )
/*++
Routine Description:

    Free-up all the driver wide common resources like WPP and ETW.

Arguments:

    WdfDriver - Driver object being unload.

--*/
{
    PAGED_CODE ();

    TraceEntry();

    //
    // Stop WPP Tracing
    //
    WPP_CLEANUP(WdfDriverWdmGetDriverObject(WdfDriver));
    //
    // Unregister ETW tracing
    //
    EventUnregisterUfxClientSample();
}