summaryrefslogtreecommitdiff
path: root/usb/UcmCxUcsi/Ppm.h
blob: e83399bf6aef89a06745e3946b9bd17f167b597b (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*++

Copyright (c) Microsoft Corporation. All rights reserved.

Module Name:

    Ppm.h

Abstract:

    Type-C Platform Policy Manager. Main interface to talk to the hardware.

Environment:

    Kernel-mode only.

--*/

#pragma once

#define MAKEWORD(a, b) ((WORD)(((BYTE)(((DWORD_PTR)(a)) & 0xff)) | ((WORD)((BYTE)(((DWORD_PTR)(b)) & 0xff))) << 8))

#define UCSI_EXPECTED_NOTIFY_CODE 0x80

#define FILE_DEVICE_PPM 32769

#define IOCTL_INTERNAL_UCSI_SEND_COMMAND \
    (DWORD) CTL_CODE(FILE_DEVICE_PPM, \
                     0x900, \
                     METHOD_BUFFERED, \
                     FILE_ANY_ACCESS)

EXTERN_C_START

typedef struct _PPM_COMMAND_ACK_PARAMS
{
    BOOLEAN AckConnectorChange;
} PPM_COMMAND_ACK_PARAMS, *PPPM_COMMAND_ACK_PARAMS;

typedef
_Function_class_(EVT_PPM_COMMAND_COMPLETION_ROUTINE)
_IRQL_requires_max_(PASSIVE_LEVEL)
_IRQL_requires_same_
VOID
EVT_PPM_COMMAND_COMPLETION_ROUTINE (
    _In_ UCSI_CONTROL Command,
    _In_ PVOID Context,
    _Inout_ PPPM_COMMAND_ACK_PARAMS CommandAckParams
    );

typedef EVT_PPM_COMMAND_COMPLETION_ROUTINE *PFN_PPM_COMMAND_COMPLETION_ROUTINE;

typedef ULONG CONNECTOR_INDEX;

typedef struct _PPM_ACTIVE_COMMAND_CONTEXT
{
    WDFREQUEST Request;
    NTSTATUS Status;
    UCSI_CONTROL Command;
    PFN_PPM_COMMAND_COMPLETION_ROUTINE CompletionRoutine;
    PVOID CompletionContext;
    BOOLEAN CompletionRoutineInvoked;
    LONG CompletionAcked;
} PPM_ACTIVE_COMMAND_CONTEXT, *PPPM_ACTIVE_COMMAND_CONTEXT;

typedef struct _PPM_CONNECTOR_CHANGE_CONTEXT
{
    LONG InProgress;
    UCM_CHARGING_STATE ChargingState;
    UCM_PD_REQUEST_DATA_OBJECT Rdo;
    UCM_PD_POWER_DATA_OBJECT Pdos[UCSI_MAX_NUM_PDOS];
    UCHAR PdoCount;
} PPM_CONNECTOR_CHANGE_CONTEXT, *PPPM_CONNECTOR_CHANGE_CONTEXT;

typedef struct _PPM_CONTEXT
{
    PUCSI_DATA_BLOCK UcsiDataBlock;
    ULONG MappedMemoryLength;
    BOOLEAN IsUsbDeviceControllerEnabled;
    WDFIOTARGET SelfIoTarget;
    WDFQUEUE CommandQueue;
    WDFWORKITEM CommandCompletionWorkItem;
    BOOLEAN CommandCompleteNotificationEnabled;
    WDFCOLLECTION Connectors;

    //
    // State associated with the current command.
    //
    PPM_ACTIVE_COMMAND_CONTEXT ActiveCommandCtx;

    //
    // Temporary state associated with a reported connector
    // change. Since multiple commands may need to be sent,
    // all the data is temporarily staged in here, before reporting
    // it all to the Cx.
    //
    PPM_CONNECTOR_CHANGE_CONTEXT ConnectorChangeCtx;
} PPM_CONTEXT, *PPPM_CONTEXT;

_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Ppm_Initialize(
    _In_ PPPM_CONTEXT PpmCtx
);

_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Ppm_PrepareHardware (
    _In_ PPPM_CONTEXT PpmCtx,
    _In_ PHYSICAL_ADDRESS MemoryAddress,
    _In_ ULONG MemoryLength
    );

_IRQL_requires_max_(PASSIVE_LEVEL)
VOID
Ppm_ReleaseHardware (
    _In_ PPPM_CONTEXT PpmCtx
    );

_IRQL_requires_max_(DISPATCH_LEVEL)
NTSTATUS
Ppm_SendCommand (
    _In_ PPPM_CONTEXT PpmCtx,
    _In_ UCSI_CONTROL Command,
    _In_opt_ PFN_PPM_COMMAND_COMPLETION_ROUTINE CompletionRoutine,
    _In_opt_ PVOID Context
    );

_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Ppm_SendCommandSynchronously (
    _In_ PPPM_CONTEXT PpmCtx,
    _In_ UCSI_CONTROL Command,
    _Out_opt_ PUCSI_MESSAGE_IN MessageIn,
    _Out_opt_ PULONG_PTR BytesReturned
    );

_IRQL_requires_max_(HIGH_LEVEL)
NTSTATUS
Ppm_GetCci (
    _In_ PPPM_CONTEXT PpmCtx,
    _Out_ PUCSI_CCI UcsiCci
    );

_IRQL_requires_max_(HIGH_LEVEL)
NTSTATUS
Ppm_GetMessage (
    _In_ PPPM_CONTEXT PpmCtx,
    _Out_ UINT8 (&Message)[UCSI_MAX_DATA_LENGTH]
    );

_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Ppm_PowerOn (
    _In_ PPPM_CONTEXT PpmCtx
    );

_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Ppm_EnableNotifications (
    _In_ PPPM_CONTEXT PpmCtx
    );

_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Ppm_PowerOff (
    _In_ PPPM_CONTEXT PpmCtx
    );

_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Ppm_ConnectorSetUor (
    _In_ PPPM_CONTEXT PpmCtx,
    _In_ CONNECTOR_INDEX ConnectorIndex,
    _In_ UCSI_USB_OPERATION_ROLE Uor
    );

_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Ppm_ConnectorSetPdr (
    _In_ PPPM_CONTEXT PpmCtx,
    _In_ CONNECTOR_INDEX ConnectorIndex,
    _In_ UCSI_POWER_DIRECTION_ROLE Pdr
    );

EVT_ACPI_NOTIFY_CALLBACK Ppm_NotificationHandler;

_IRQL_requires_max_(HIGH_LEVEL)
ULONG64
FORCEINLINE
UCM_CONNECTOR_ID_FROM_ACPI_PLD(
    _In_ PACPI_PLD_BUFFER PldBuffer
)
{
    ULONG64 connectorId;

    connectorId = 0;
    connectorId |= (PldBuffer->GroupToken & 0xFF);
    connectorId <<= 8;
    connectorId |= (PldBuffer->GroupPosition & 0xFF);

    return connectorId;
}


typedef struct _PPM_SEND_COMMAND_PARAMS
{
    UCSI_CONTROL Command;
    PFN_PPM_COMMAND_COMPLETION_ROUTINE CompletionRoutine;
    PVOID Context;
} PPM_SEND_COMMAND_PARAMS, *PPPM_SEND_COMMAND_PARAMS;

typedef struct _PPM_REQUEST_CONTEXT
{
    UCSI_CONTROL Command;
} PPM_REQUEST_CONTEXT, *PPPM_REQUEST_CONTEXT;

WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(PPM_REQUEST_CONTEXT, PpmRequest_GetContext);

typedef struct _PPM_CONNECTOR
{
    WDFDEVICE WdfDevice;
    ULONGLONG Id;
    CONNECTOR_INDEX Index;
    UCMCONNECTOR Handle;
    BOOLEAN PerformRoleCorrectionOnNextPdContract;
} PPM_CONNECTOR, *PPPM_CONNECTOR;

typedef struct _PPM_UCM_CONNECTOR_CONTEXT
{
    PPPM_CONNECTOR PpmConnector;
} PPM_UCM_CONNECTOR_CONTEXT, *PPPM_UCM_CONNECTOR_CONTEXT;

WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(PPM_UCM_CONNECTOR_CONTEXT, PpmUcmConnector_GetContext);

EVT_WDF_IO_QUEUE_IO_INTERNAL_DEVICE_CONTROL Ppm_EvtIoInternalDeviceControl;
EVT_WDF_REQUEST_COMPLETION_ROUTINE Ppm_CommandRequestCompletionRoutine;
EVT_WDF_WORKITEM Ppm_CommandCompletionWorkItem;
EVT_PPM_COMMAND_COMPLETION_ROUTINE Ppm_EvtGetConnectorStatusCompleted;

_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Ppm_WaitForResetComplete(
    _In_ PPPM_CONTEXT PpmCtx
);

_IRQL_requires_max_(DISPATCH_LEVEL)
VOID
Ppm_ProcessNotifications(
    _In_ PPPM_CONTEXT PpmCtx
);

_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Ppm_ExecuteCommand(
    _In_ PPPM_CONTEXT PpmCtx,
    _In_ UCSI_CONTROL Command
);

_IRQL_requires_max_(DISPATCH_LEVEL)
VOID
Ppm_CompleteActiveRequest(
    _In_ PPPM_CONTEXT PpmCtx
);

_IRQL_requires_max_(DISPATCH_LEVEL)
VOID
Ppm_SaveMessageInContentsInRequest(
    _In_ PPPM_CONTEXT PpmCtx
);

_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Ppm_QueryConnectors(
    _In_ PPPM_CONTEXT PpmCtx
);

_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Ppm_CommandCompletionHandler(
    _In_ PPPM_CONTEXT PpmCtx
);

_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Ppm_GetCapability(
    _In_ PPPM_CONTEXT PpmCtx,
    _Out_ PUCSI_GET_CAPABILITY_IN Caps
);

_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Ppm_GetConnectorCapability(
    _In_ PPPM_CONTEXT PpmCtx,
    _In_ CONNECTOR_INDEX ConnectorIndex,
    _Out_ PUCSI_GET_CONNECTOR_CAPABILITY_IN ConnCaps
);

_IRQL_requires_max_(PASSIVE_LEVEL)
_Must_inspect_result_
_Success_(return != 0)
PPPM_CONNECTOR
Ppm_AddConnector(
    _In_ PPPM_CONTEXT PpmCtx
);

_IRQL_requires_max_(DISPATCH_LEVEL)
PPPM_CONNECTOR
Ppm_GetConnector(
    _In_ PPPM_CONTEXT PpmCtx,
    _In_ CONNECTOR_INDEX Index
);

_IRQL_requires_max_(DISPATCH_LEVEL)
VOID
Ppm_HandleCommandCompletionNotification(
    _In_ PPPM_CONTEXT PpmCtx,
    _In_ UCSI_CCI Cci
);

_IRQL_requires_max_(DISPATCH_LEVEL)
VOID
Ppm_HandleConnectorChangeNotification(
    _In_ PPPM_CONTEXT PpmCtx,
    _In_ UCSI_CCI Cci
);

_IRQL_requires_max_(PASSIVE_LEVEL)
VOID
Ppm_ReportNegotiatedPowerLevelChanged(
    _In_ PPPM_CONTEXT PpmCtx,
    _In_ PPPM_CONNECTOR Connector,
    _Inout_ PUCSI_GET_CONNECTOR_STATUS_IN ConnStatus,
    _Inout_ PPPM_COMMAND_ACK_PARAMS CommandAckParams
);

_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Ppm_PerformRoleCorrection(
    _In_ PPPM_CONTEXT PpmCtx,
    _In_ PPPM_CONNECTOR Connector
);

_IRQL_requires_max_(DISPATCH_LEVEL)
VOID
Ppm_PrettyDebugPrintMessageIn(
    _In_ PPPM_CONTEXT PpmCtx
);


EXTERN_C_END