summaryrefslogtreecommitdiff
path: root/wpd/WpdBasicHardwareDriver/Driver.cpp
blob: ca0697a160bf4dfe9ea44e251ec5526d1d178fd9 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "stdafx.h"

#include "Driver.tmh"

CDriver::CDriver()
{


}

HRESULT
CDriver::OnDeviceAdd(
    _In_ IWDFDriver* pDriver,
    _In_ IWDFDeviceInitialize* pDeviceInit
    )
/*++

Routine Description:

    The framework calls this function when a device is being added to
    the driver stack.

Arguments:

    IWDFDriver           - Framework interface.  The driver uses this
                           interface to create device objects.
    IWDFDeviceInitialize - Framework interface.  The driver uses this
                           interface to set device parameters before
                           creating the device obeject.

Return Value:

   HRESULT S_OK - Device added successfully

--*/
{
    TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_FLAG_DRIVER, "%!FUNC! Entry");

    HRESULT hr = S_OK;
    CComPtr<IUnknown> pDeviceCallback;

    WpdBaseDriver *pWpdBaseDriver = NULL;

    //
    // Create the WPD driver object that handles all WPD messages for this device
    //
    pWpdBaseDriver = new WpdBaseDriver();
    if(pWpdBaseDriver == NULL)
    {
        hr = E_OUTOFMEMORY;
    }

    if(SUCCEEDED(hr))
    {
        //
        // Create device callback object
        //
        hr = CDevice::CreateInstance(pDeviceInit, pWpdBaseDriver, &pDeviceCallback);
    }

    //
    // This driver has no special power management requirements and so
    // we set power policy ownership to UMDF to indicate that UMDF should
    // handle powermanagement for us.
    //
    pDeviceInit->SetPowerPolicyOwnership(FALSE);

    //
    // Create WDFDevice.
    //
    CComPtr<IWDFDevice> pIWDFDevice;
    if(SUCCEEDED(hr))
    {
        hr = pDriver->CreateDevice(
                pDeviceInit,
                pDeviceCallback,
                &pIWDFDevice);
    }

    //
    // Assign pWpdBaseDriver to the device object.  Each UMDF device requires its own instance of
    // a WpdBaseDriver to handle WPD messages.
    //
    if(SUCCEEDED(hr))
    {
        hr = pIWDFDevice->AssignContext(this, (void*)pWpdBaseDriver);
        if(SUCCEEDED(hr))
        {
            //  AddRef the WpdBaseDriver object since it is not stored with the
            //  device context.
            pWpdBaseDriver->AddRef();
        }
    }

    //
    // Create queue callback object
    //
    CComPtr<IUnknown> pIUnknown;
    if(S_OK == hr)
    {
        hr = CQueue::CreateInstance(&pIUnknown);
    }

    //
    // Configure the default queue.
    //
    if(S_OK == hr)
    {
        CComPtr<IWDFIoQueue> pDefaultQueue;
        hr = pIWDFDevice->CreateIoQueue(
                            pIUnknown,
                            TRUE,                         // bDefaultQueue
                            WdfIoQueueDispatchSequential,
                            TRUE,                         // bPowerManaged
                            FALSE,                        // bAllowZeroLengthRequests
                            &pDefaultQueue);
    }

    pDeviceCallback = NULL;
    pIWDFDevice = NULL;

    //
    // It is fine to release the interface on the callback object.
    // The framework has its own refcount on this object and will
    // provide an interface when calling into the driver.
    //
    pIUnknown = NULL;

    // Release the WpdBaseDriver object.  If it was successfully added to the device context,
    // it was already addref'd above. Releasing it here ensures it will be destroyed if
    // an error occured and it could not be added to the device context.
    SAFE_RELEASE(pWpdBaseDriver);

    return hr;
}

void
CDriver::OnDeinitialize(
    _In_ IWDFDriver* pDriver
    )
/*++

Routine Description:

    The framework calls this function just before de-initializing itself. All
    WDF framework resources should be released by driver before returning from this call.

Arguments:

Return Value:

--*/
{
    UNREFERENCED_PARAMETER(pDriver);
    return;
}

HRESULT
CDriver::OnInitialize(
    _In_ IWDFDriver* pDriver
    )
/*++

Routine Description:

    The framework calls this function just after loading the driver. The driver can
    perform any global, device independent intialization in this routine.

Arguments:

Return Value:

--*/
{
    UNREFERENCED_PARAMETER(pDriver);
    return S_OK;
}

STDMETHODIMP_ (void)
CDriver::OnCleanup(
    _In_ IWDFObject* pWdfObject
    )
{
    TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_FLAG_DRIVER, "%!FUNC! Entry");

    // Release the base driver object
    HRESULT         hr              = S_OK;
    WpdBaseDriver*  pWpdBaseDriver  = NULL;

    hr = pWdfObject->RetrieveContext((void**)&pWpdBaseDriver);
    if((hr == S_OK) && (pWpdBaseDriver != NULL))
    {
        pWpdBaseDriver->Release();
        pWpdBaseDriver = NULL;
    }
}