summaryrefslogtreecommitdiff
path: root/spb/SkeletonI2C/device.h
blob: 06d993d2742503b9c1e2b4168ab35d7d49efb7be (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*++

Copyright (c) Microsoft Corporation.  All rights reserved.

Module Name:

    device.h

Abstract:

    This module contains the function definitions for the
    WDF device.

Environment:

    kernel-mode only

Revision History:

--*/

#ifndef _DEVICE_H_
#define _DEVICE_H_

//
// WDF event callbacks.
//

EVT_WDF_DEVICE_PREPARE_HARDWARE         OnPrepareHardware;
EVT_WDF_DEVICE_RELEASE_HARDWARE         OnReleaseHardware;
EVT_WDF_DEVICE_D0_ENTRY                 OnD0Entry;
EVT_WDF_DEVICE_D0_EXIT                  OnD0Exit;
EVT_WDF_DEVICE_SELF_MANAGED_IO_INIT     OnSelfManagedIoInit;
EVT_WDF_DEVICE_SELF_MANAGED_IO_CLEANUP  OnSelfManagedIoCleanup;

EVT_WDF_INTERRUPT_ISR                   OnInterruptIsr;
EVT_WDF_INTERRUPT_DPC                   OnInterruptDpc;

EVT_WDF_REQUEST_CANCEL                  OnCancel;

//
// Power framework event callbacks.
//

__drv_functionClass(POWER_SETTING_CALLBACK)
_IRQL_requires_same_
NTSTATUS
OnMonitorPowerSettingCallback(
    _In_                          LPCGUID SettingGuid,
    _In_reads_bytes_(ValueLength) PVOID   Value,
    _In_                          ULONG   ValueLength,
    _Inout_opt_                   PVOID   Context
   );

//
// SPBCx event callbacks.
//

EVT_SPB_TARGET_CONNECT               OnTargetConnect;
EVT_SPB_CONTROLLER_LOCK              OnControllerLock;
EVT_SPB_CONTROLLER_UNLOCK            OnControllerUnlock;
EVT_SPB_CONTROLLER_READ              OnRead;
EVT_SPB_CONTROLLER_WRITE             OnWrite;
EVT_SPB_CONTROLLER_SEQUENCE          OnSequence;

EVT_WDF_IO_IN_CALLER_CONTEXT         OnOtherInCallerContext;
EVT_SPB_CONTROLLER_OTHER             OnOther;

//
// PBC function prototypes.
//

NTSTATUS
PbcTargetGetSettings(
    _In_     PPBC_DEVICE             pDevice,
    _In_     PVOID                   ConnectionParameters,
    _Out_    PPBC_TARGET_SETTINGS    pSettings);

NTSTATUS
PbcRequestValidate(
    _In_     PPBC_REQUEST            pRequest);

VOID
PbcRequestConfigureForNonSequence(
    _In_  WDFDEVICE                  SpbController,
    _In_  SPBTARGET                  SpbTarget,
    _In_  SPBREQUEST                 SpbRequest,
    _In_  size_t                     Length);

NTSTATUS
PbcRequestConfigureForIndex(
    _Inout_  PPBC_REQUEST            pRequest,
    _In_     ULONG                   Index);

VOID
PbcRequestDoTransfer(
    _In_     PPBC_DEVICE             pDevice,
    _In_     PPBC_REQUEST            pRequest);

VOID
PbcRequestComplete(
    _In_     PPBC_REQUEST            pRequest);

EVT_WDF_TIMER                        OnDelayTimerExpired;

ULONG
FORCEINLINE
PbcDeviceGetInterruptMask(
    _In_  PPBC_DEVICE                pDevice
    )
/*++

  Routine Description:

    This routine returns the device context's current
    interrupt mask.

  Arguments:

    pDevice - a pointer to the PBC device context

  Return Value:

    Interrupt mask

--*/
{
    return (ULONG)InterlockedOr((PLONG)&pDevice->InterruptMask, 0);
}

VOID
FORCEINLINE
PbcDeviceSetInterruptMask(
    _In_  PPBC_DEVICE                pDevice,
    _In_  ULONG                      InterruptMask
    )
/*++

  Routine Description:

    This routine sets the device context's current
    interrupt mask.

  Arguments:

    pDevice - a pointer to the PBC device context
    InterruptMask - new interrupt mask value

  Return Value:

    None.

--*/
{
    InterlockedExchange(
        (PLONG)&pDevice->InterruptMask,
        (LONG)InterruptMask);
}

VOID
FORCEINLINE
PbcDeviceAndInterruptMask(
    _In_  PPBC_DEVICE                pDevice,
    _In_  ULONG                      InterruptMask
    )
/*++

  Routine Description:

    This routine performs a logical and between the device
    context's current interrupt mask and the input parameter.

  Arguments:

    pDevice - a pointer to the PBC device context
    InterruptMask - new interrupt mask value to and

  Return Value:

    None.

--*/
{
    InterlockedAnd(
        (PLONG)&pDevice->InterruptMask,
        (LONG)InterruptMask);
}

size_t
FORCEINLINE
PbcRequestGetInfoRemaining(
   _In_  PPBC_REQUEST  pRequest
   )
/*++

  Routine Description:

    This is a helper routine used to retrieve the
    number of bytes remaining in the current transfer.

  Arguments:

    pRequest - a pointer to the PBC request context

  Return Value:

    Bytes remaining in request

--*/
{
    return (pRequest->Length - pRequest->Information);
}

NTSTATUS
FORCEINLINE
PbcRequestGetByte(
   _In_  PPBC_REQUEST  pRequest,
   _In_  size_t        Index,
   _Out_ UCHAR*        pByte
   )
/*++

  Routine Description:

    This is a helper routine used to retrieve the
    specified byte of the current transfer descriptor buffer.

  Arguments:

    pRequest - a pointer to the PBC request context

    Index - index of desired byte in current transfer descriptor buffer

    pByte - pointer to the location for the specified byte

  Return Value:

    STATUS_INFO_LENGTH_MISMATCH if invalid index,
    otherwise STATUS_SUCCESS

--*/
{
    PMDL mdl = pRequest->pMdlChain;
    size_t mdlByteCount;
    size_t currentOffset = Index;
    PUCHAR pBuffer;
    NTSTATUS status = STATUS_INFO_LENGTH_MISMATCH;

    //
    // Check for out-of-bounds index
    //

    if (Index < pRequest->Length)
    {
        while (mdl != NULL)
        {
            mdlByteCount = MmGetMdlByteCount(mdl);

            if (currentOffset < mdlByteCount)
            {
                pBuffer = (PUCHAR) MmGetSystemAddressForMdlSafe(
                    mdl,
                    NormalPagePriority | MdlMappingNoExecute);

                if (pBuffer != NULL)
                {
                    //
                    // Byte found, mark successful
                    //

                    *pByte = pBuffer[currentOffset];
                    status = STATUS_SUCCESS;
                }

                break;
            }

            currentOffset -= mdlByteCount;
            mdl = mdl->Next;
        }

        //
        // If after walking the MDL the byte hasn't been found,
        // status will still be STATUS_INFO_LENGTH_MISMATCH
        //
    }

    return status;
}

NTSTATUS
FORCEINLINE
PbcRequestSetByte(
   _In_  PPBC_REQUEST  pRequest,
   _In_  size_t        Index,
   _In_  UCHAR         Byte
   )
/*++

  Routine Description:

    This is a helper routine used to set the
    specified byte of the current transfer descriptor buffer.

  Arguments:

    pRequest - a pointer to the PBC request context

    Index - index of desired byte in current transfer descriptor buffer

    Byte - the byte

  Return Value:

    STATUS_INFO_LENGTH_MISMATCH if invalid index,
    otherwise STATUS_SUCCESS

--*/
{
    PMDL mdl = pRequest->pMdlChain;
    size_t mdlByteCount;
    size_t currentOffset = Index;
    PUCHAR pBuffer;
    NTSTATUS status = STATUS_INFO_LENGTH_MISMATCH;

    //
    // Check for out-of-bounds index
    //

    if (Index < pRequest->Length)
    {
        while (mdl != NULL)
        {
            mdlByteCount = MmGetMdlByteCount(mdl);

            if (currentOffset < mdlByteCount)
            {
                pBuffer = (PUCHAR) MmGetSystemAddressForMdlSafe(
                    mdl,
                    NormalPagePriority | MdlMappingNoExecute);

                if (pBuffer != NULL)
                {
                    //
                    // Byte found, mark successful
                    //

                    pBuffer[currentOffset] = Byte;
                    status = STATUS_SUCCESS;
                }

                break;
            }

            currentOffset -= mdlByteCount;
            mdl = mdl->Next;
        }

        //
        // If after walking the MDL the byte hasn't been found,
        // status will still be STATUS_INFO_LENGTH_MISMATCH
        //
    }

    return status;
}

#endif