summaryrefslogtreecommitdiff
path: root/usb/UcmTcpciCxClientSample/Device.cpp
blob: fc2330ad8329069ad569ee9a5edeffae3f8f6894 (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
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
/*++

Module Name:

    Device.c - Device handling events.

Abstract:

   This file contains the device definitions.

Environment:

    Kernel-mode Driver Framework

--*/

#include "Driver.h"
#include "device.tmh"

#ifdef ALLOC_PRAGMA
#pragma alloc_text (PAGE, EvtCreateDevice)
#pragma alloc_text (PAGE, EvtPrepareHardware)
#pragma alloc_text (PAGE, EvtDeviceD0Entry)
#pragma alloc_text (PAGE, EvtReleaseHardware)
#endif

NTSTATUS
EvtCreateDevice(
    _Inout_ PWDFDEVICE_INIT DeviceInit
)
/*++

Routine Description:

    Worker routine called to create a device and its software resources.

Arguments:

    DeviceInit - Pointer to an opaque init structure. Memory for this
                    structure will be freed by the framework when the WdfDeviceCreate
                    succeeds. Don't access the structure after that point.

Return Value:

    NTSTATUS

--*/
{
    TRACE_FUNC_ENTRY(TRACE_DEVICE);

    PAGED_CODE();

    NTSTATUS status;
    WDFDEVICE device;
    PDEVICE_CONTEXT deviceContext;
    UCMTCPCI_DEVICE_CONFIG config;
    WDF_PNPPOWER_EVENT_CALLBACKS pnpPowerCallbacks;
    WDF_OBJECT_ATTRIBUTES attributes;

    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, DEVICE_CONTEXT);

    WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpPowerCallbacks);
    pnpPowerCallbacks.EvtDevicePrepareHardware = EvtPrepareHardware;
    pnpPowerCallbacks.EvtDeviceReleaseHardware = EvtReleaseHardware;
    pnpPowerCallbacks.EvtDeviceD0Entry = EvtDeviceD0Entry;
    WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, &pnpPowerCallbacks);

    status = UcmTcpciDeviceInitInitialize(DeviceInit);
    if (!NT_SUCCESS(status))
    {
        TraceEvents(TRACE_LEVEL_ERROR, TRACE_DEVICE,
            "[PWDFDEVICE_INIT: 0x%p] UcmTcpciDeviceInitInitialize failed - %!STATUS!",
            DeviceInit, status);
        goto Exit;
    }

    status = WdfDeviceCreate(&DeviceInit, &attributes, &device);
    if (!NT_SUCCESS(status))
    {
        TraceEvents(TRACE_LEVEL_ERROR, TRACE_DEVICE,
            "[PWDFDEVICE_INIT: 0x%p] WdfDeviceCreate failed - %!STATUS!", DeviceInit, status);
        goto Exit;
    }

    deviceContext = DeviceGetContext(device);

    // Save the device in the context so we can access it later.
    deviceContext->Device = device;

    // Initialize platform-level device reset.
    deviceContext->ResetAttempts = 0;

    RtlZeroMemory(&deviceContext->ResetInterface, sizeof(deviceContext->ResetInterface));
    deviceContext->ResetInterface.Size = sizeof(deviceContext->ResetInterface);
    deviceContext->ResetInterface.Version = 1;

    status = WdfFdoQueryForInterface(
        deviceContext->Device,
        &GUID_DEVICE_RESET_INTERFACE_STANDARD,
        (PINTERFACE)&deviceContext->ResetInterface,
        sizeof(deviceContext->ResetInterface),
        1,
        NULL);

    // The reset interface may not exist on certain environments.
    // In this case, we will fall back to using a different reset method.
    // Zero the reset interface and ignore the error.
    if (!NT_SUCCESS(status))
    {
        TraceEvents(TRACE_LEVEL_ERROR, TRACE_DEVICE,
            "[WDFDEVICE: 0x%p] WdfFdoQueryForInterface for GUID_DEVICE_RESET_INTERFACE_STANDARD failed. Status: %!STATUS!",
            device, status);
        RtlZeroMemory(&deviceContext->ResetInterface, sizeof(deviceContext->ResetInterface));
        status = STATUS_SUCCESS;
    }
    else
    {
        TraceEvents(TRACE_LEVEL_ERROR, TRACE_DEVICE,
            "[WDFDEVICE: 0x%p] Successfully initialized platform-level device reset.", deviceContext->Device);
    }

    // Register our device with UcmTcpciCx.
    UCMTCPCI_DEVICE_CONFIG_INIT(&config);
    status = UcmTcpciDeviceInitialize(device, &config);
    if (!NT_SUCCESS(status))
    {
        TraceEvents(TRACE_LEVEL_ERROR, TRACE_DEVICE,
            "[WDFDEVICE: 0x%p] UcmTcpciDeviceInitialize failed - %!STATUS!",
            device, status);
        goto Exit;
    }

Exit:
    TRACE_FUNC_EXIT(TRACE_DEVICE);
    return status;
}

NTSTATUS
EvtPrepareHardware(
    _In_ WDFDEVICE Device,
    _In_ WDFCMRESLIST ResourcesRaw,
    _In_ WDFCMRESLIST ResourcesTranslated
)
/*++

Routine Description:

    A driver's EvtDevicePrepareHardware event callback function performs any operations
    that are needed to make a device accessible to the driver.

Arguments:

    Device - A handle to a framework device object.

    ResourcesRaw - A handle to a framework resource-list object that identifies the raw hardware
    resources that the Plug and Play manager has assigned to the device.

    ResourcesTranslated - A handle to a framework resource-list object that identifies the
    translated hardware resources that the Plug and Play manager has assigned to the device.

Return Value:

    NTSTATUS

--*/
{
    TRACE_FUNC_ENTRY(TRACE_DEVICE);

    PAGED_CODE();

    NTSTATUS status;
    PDEVICE_CONTEXT deviceContext;

    deviceContext = DeviceGetContext(Device);

    //// Initialize the I2C communication channel to read from/write to the hardware.
    status = I2CInitialize(deviceContext, ResourcesRaw, ResourcesTranslated);
    if (!NT_SUCCESS(status))
    {
        goto Exit;
    }

    status = I2COpen(deviceContext);
    if (!NT_SUCCESS(status))
    {
        goto Exit;
    }

Exit:
    return status;
}

NTSTATUS EvtDeviceD0Entry(
    _In_ WDFDEVICE              Device,
    _In_ WDF_POWER_DEVICE_STATE PreviousState
)
{
    TRACE_FUNC_ENTRY(TRACE_DEVICE);

    UNREFERENCED_PARAMETER(PreviousState);

    PAGED_CODE();

    NTSTATUS status;
    PDEVICE_CONTEXT deviceContext;
    UCMTCPCIPORTCONTROLLER portController = WDF_NO_HANDLE;
    UCMTCPCI_PORT_CONTROLLER_CONFIG config;
    UCMTCPCI_PORT_CONTROLLER_IDENTIFICATION ident;
    UCMTCPCI_PORT_CONTROLLER_CAPABILITIES capabilities;

    deviceContext = DeviceGetContext(Device);

    UCMTCPCI_PORT_CONTROLLER_IDENTIFICATION_INIT(&ident);
    UCMTCPCI_PORT_CONTROLLER_CAPABILITIES_INIT(&capabilities);

    // Read device identification and capabilities from the registers.
    REGISTER_ITEM items[] = {
        GEN_REGISTER_ITEM(VENDOR_ID,                    ident.VendorId),
        GEN_REGISTER_ITEM(PRODUCT_ID,                   ident.ProductId),
        GEN_REGISTER_ITEM(DEVICE_ID,                    ident.DeviceId),
        GEN_REGISTER_ITEM(USBTYPEC_REV,                 ident.TypeCRevisionInBcd),
        GEN_REGISTER_ITEM(USBPD_REV_VER,                ident.PDRevisionAndVersionInBcd),
        GEN_REGISTER_ITEM(PD_INTERFACE_REV,             ident.PDInterfaceRevisionAndVersionInBcd),
        GEN_REGISTER_ITEM(DEVICE_CAPABILITIES_1,        capabilities.DeviceCapabilities1),
        GEN_REGISTER_ITEM(DEVICE_CAPABILITIES_2,        capabilities.DeviceCapabilities2),
        GEN_REGISTER_ITEM(STANDARD_INPUT_CAPABILITIES,  capabilities.StandardInputCapabilities),
        GEN_REGISTER_ITEM(STANDARD_OUTPUT_CAPABILITIES, capabilities.StandardOutputCapabilities),
    };

    status = I2CReadSynchronouslyMultiple(deviceContext,
        I2CRequestSourceClient,
        items,
        _countof(items));
    if (!NT_SUCCESS(status))
    {
        goto Exit;
    }


    capabilities.IsPowerDeliveryCapable = TRUE;

    UCMTCPCI_PORT_CONTROLLER_CONFIG_INIT(&config, &ident, &capabilities);

    // Create a UCMTCPCIPORTCONTROLLER framework object.
    status = UcmTcpciPortControllerCreate(Device, &config, WDF_NO_OBJECT_ATTRIBUTES, &portController);
    if (!NT_SUCCESS(status))
    {
        TraceEvents(TRACE_LEVEL_ERROR, TRACE_DEVICE, "[WDFDEVICE: 0x%p] UcmTcpciPortControllerCreate "
            "failed - %!STATUS!", Device, status);
        goto Exit;
    }

    // Save the UCMTCPCIPORTCONTROLLER in our device context.
    deviceContext = DeviceGetContext(Device);
    deviceContext->PortController = portController;

    // Set the hardware request queue for our device.
    status = HardwareRequestQueueInitialize(Device);
    if (!NT_SUCCESS(status))
    {
        goto Exit;
    }

    // Direct UcmTcpciCx to start the port controller.
    // At this point, UcmTcpciCx will assume control of USB Type-C and Power Delivery.
    // After the port controller is started, UcmTcpciCx may start putting requests into the
    // hardware request queue.
    status = UcmTcpciPortControllerStart(portController);
    if (!NT_SUCCESS(status))
    {
        TraceEvents(TRACE_LEVEL_ERROR, TRACE_DEVICE, "[UCMTCPCIPORTCONTROLLER: 0x%p]"
            "UcmTcpciPortControllerStart failed - %!STATUS!", portController, status);
        goto Exit;
    }

Exit:
    if (!NT_SUCCESS(status) && (portController != WDF_NO_HANDLE))
    {
        WdfObjectDelete(portController);
        deviceContext->PortController = WDF_NO_HANDLE;
    }

    TRACE_FUNC_EXIT(TRACE_DEVICE);
    return status;
}

NTSTATUS
EvtReleaseHardware(
    _In_ WDFDEVICE Device,
    _In_ WDFCMRESLIST ResourcesTranslated
)
/*++

Routine Description:

    A driver's EvtDeviceReleaseHardware event callback function performs operations
    that are needed when a device is no longer accessible.

Arguments:

    Device - A handle to a framework device object.

    ResourcesTranslated - A handle to a resource list object that identifies the translated
    hardware resources that the Plug and Play manager has assigned to the device.

Return Value:

    NTSTATUS

--*/
{
    TRACE_FUNC_ENTRY(TRACE_DEVICE);

    UNREFERENCED_PARAMETER(ResourcesTranslated);
    PAGED_CODE();

    NTSTATUS status = STATUS_SUCCESS;
    PDEVICE_CONTEXT deviceContext;

    deviceContext = DeviceGetContext(Device);

    if (deviceContext->PortController != WDF_NO_HANDLE)
    {
        // Direct UcmTcpciCx to stop the port controller and then delete the backing object.
        UcmTcpciPortControllerStop(deviceContext->PortController);
        WdfObjectDelete(deviceContext->PortController);
        deviceContext->PortController = WDF_NO_HANDLE;
    }

    // Close the I2C controller.
    I2CClose(deviceContext);

    TRACE_FUNC_EXIT(TRACE_DEVICE);
    return status;
}