summaryrefslogtreecommitdiff
path: root/usb/ufxclientsample/ufxendpoint.c
blob: fbc45e56f7eccb5015039b652f071e1a1e157c71 (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*++

Copyright (c) Microsoft Corporation. All rights reserved.

Module Name:

    ufxendpoint.c

Abstract:

    Defines the functions needed for UFXENDPOINT object

Environment:

    Kernel mode

--*/

#include "ufxendpoint.h"
#include "ufxdevice.h"
#include "transfer.h"
#include <usbfnioctl.h>
#include "registers.h"
#include "trace.h"
#include "device.h"
#include "ufxendpoint.tmh"

EVT_WDF_IO_QUEUE_IO_CANCELED_ON_QUEUE EvtIoCanceledOnQueue;
EVT_WDF_IO_QUEUE_IO_STOP EndpointQueue_EvtIoStop;
EVT_WDF_IO_QUEUE_IO_INTERNAL_DEVICE_CONTROL EvtEndpointCommandQueue;

EVT_WDF_OBJECT_CONTEXT_CLEANUP UfxEndpoint_Cleanup;

_Must_inspect_result_
_IRQL_requires_max_(DISPATCH_LEVEL)
NTSTATUS
UfxEndpointDescriptorUpdate (
    _In_ UFXENDPOINT Endpoint,
    _In_ PUSB_ENDPOINT_DESCRIPTOR Descriptor
    );

_Must_inspect_result_
_IRQL_requires_(PASSIVE_LEVEL)
NTSTATUS
UfxEndpointAdd (
    _In_ UFXDEVICE Device,
    _In_ PUSB_ENDPOINT_DESCRIPTOR Descriptor,
    _Inout_ PUFXENDPOINT_INIT EndpointInit
    )
/*++
Routine Description:

    Creates a UFXENDPOINT object, and initializes its contexts.

Parameters Description:

    Device - UFXDEVICE associated with the endpoint.

    Descriptor - Endpoint descriptor for this endpoint.

    EndpointInit - Opaque structure from UFX.

Return Value:

    STATUS_SUCCESS if successful, appropriate NTSTATUS message otherwise.
--*/
{
    NTSTATUS Status;
    WDF_OBJECT_ATTRIBUTES Attributes;
    WDF_IO_QUEUE_CONFIG TransferQueueConfig;
    WDF_OBJECT_ATTRIBUTES TransferQueueAttributes;
    WDF_IO_QUEUE_CONFIG CommandQueueConfig;
    WDF_OBJECT_ATTRIBUTES CommandQueueAttributes;
    UFXENDPOINT Endpoint;
    PUFXENDPOINT_CONTEXT EpContext;
    PUFXDEVICE_CONTEXT DeviceContext;
    UFX_ENDPOINT_CALLBACKS Callbacks;
    PENDPOINT_QUEUE_CONTEXT QueueContext;
    WDFQUEUE Queue;

    TraceEntry();

    DeviceContext = UfxDeviceGetContext(Device);

    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&Attributes, UFXENDPOINT_CONTEXT);
    Attributes.ExecutionLevel = WdfExecutionLevelPassive;
    Attributes.EvtCleanupCallback = UfxEndpoint_Cleanup;

    //
    // Note: Execution level needs to be passive to avoid deadlocks with WdfRequestComplete.
    //
    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&TransferQueueAttributes, ENDPOINT_QUEUE_CONTEXT);
    TransferQueueAttributes.ExecutionLevel = WdfExecutionLevelPassive;

    WDF_IO_QUEUE_CONFIG_INIT(&TransferQueueConfig, WdfIoQueueDispatchManual);
    TransferQueueConfig.AllowZeroLengthRequests = TRUE;
    TransferQueueConfig.EvtIoStop = EndpointQueue_EvtIoStop;

    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&CommandQueueAttributes, ENDPOINT_QUEUE_CONTEXT);
    CommandQueueAttributes.ExecutionLevel = WdfExecutionLevelPassive;

    WDF_IO_QUEUE_CONFIG_INIT(&CommandQueueConfig, WdfIoQueueDispatchSequential);
    CommandQueueConfig.EvtIoInternalDeviceControl = EvtEndpointCommandQueue;

    UFX_ENDPOINT_CALLBACKS_INIT(&Callbacks);
    UfxEndpointInitSetEventCallbacks(EndpointInit, &Callbacks);

    Status = UfxEndpointCreate(
                 Device,
                 EndpointInit,
                 &Attributes,
                 &TransferQueueConfig,
                 &TransferQueueAttributes,
                 &CommandQueueConfig,
                 &CommandQueueAttributes,
                 &Endpoint);
    CHK_NT_MSG(Status, "Failed to create ufxendpoint!");

    Status = WdfCollectionAdd(DeviceContext->Endpoints, Endpoint);
    CHK_NT_MSG(Status, "Failed to add endpoint to collection!");

    EpContext = UfxEndpointGetContext(Endpoint);
    EpContext->UfxDevice = Device;
    EpContext->WdfDevice = DeviceContext->FdoWdfDevice;
    RtlCopyMemory(&EpContext->Descriptor, Descriptor, sizeof(*Descriptor));

    Queue = UfxEndpointGetTransferQueue(Endpoint);
    QueueContext = EndpointQueueGetContext(Queue);
    QueueContext->Endpoint = Endpoint;

    Queue = UfxEndpointGetCommandQueue(Endpoint);
    QueueContext = EndpointQueueGetContext(Queue);
    QueueContext->Endpoint = Endpoint;

    Status = TransferInitialize(Endpoint);
    CHK_NT_MSG(Status, "Failed to initialize endpoint transfers");

    //
    // This can happen if we're handling a SetInterface command.
    //
    if (DeviceContext->UsbState == UsbfnDeviceStateConfigured) {
        UfxEndpointConfigure(Endpoint);
    }

    Status = WdfIoQueueReadyNotify(
                 UfxEndpointGetTransferQueue(Endpoint),
                 TransferReadyNotify,
                 Endpoint);
    CHK_NT_MSG(Status, "Failed to register ready notify");

End:
    TraceExit();
    return Status;
}

_IRQL_requires_max_(DISPATCH_LEVEL)
VOID
UfxEndpointConfigureHardware (
    _In_ UFXENDPOINT Endpoint,
    _In_ BOOLEAN Modify
    )
/*++
Routine Description:

    Configures the endpoint on the controller based on the
    endpoint descriptor. If it is a control endpoint, configures
    for both IN and OUT endpoints.

Parameters Description:

    Endpoint - Endpoint to configure

    Modify - This is only TRUE for endpoint 0 on soft reset.

--*/
{
    PUFXENDPOINT_CONTEXT EpContext;

    TraceEntry();

    UNREFERENCED_PARAMETER(Modify);

    EpContext = UfxEndpointGetContext(Endpoint);

    TraceInformation("CONFIGURE ENDPOINT: 0x%p Endpoint (%d)", Endpoint, EpContext->PhysicalEndpoint);

    //
    // ControllerContext = DeviceGetControllerContext(EpContext->WdfDevice);
    //
    // The USB address is:
    //      EpContext->Descriptor.bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK
    //
    // The maxPacketSize is:
    //      (Address == 0 && !Modify) ? 512 : EpContext->Descriptor.wMaxPacketSize;
    //
    // The Interval is:
    //      ((EpContext->Descriptor.bInterval > 0) && (ControllerContext->Speed != UsbFullSpeed)) ?
    //        EpContext->Descriptor.bInterval :
    //        0;
    //

    if (CONTROL_ENDPOINT(Endpoint)) {
        //
        // #### TODO: Configure both IN and OUT endpoints ####
        //
    } else {
        if (DIRECTION_IN(Endpoint)) {
            //
            // #### TODO: Configure the IN endpoint ####
            //
        } else {
            //
            // #### TODO: Configure the OUT endpoint ####
            //
        }
    }

    TraceExit();
}

_IRQL_requires_max_(DISPATCH_LEVEL)
VOID
UfxEndpointConfigure (
    _In_ UFXENDPOINT Endpoint
    )
/*++
Routine Description:

    Enables and configures the endpoint on the controller.

Parameters Description:

    Endpoint - Endpoint to configure

--*/
{
    PUFXENDPOINT_CONTEXT EpContext;
    ULONG EndpointMask;
    ULONG Address;

    TraceEntry();

    EpContext = UfxEndpointGetContext(Endpoint);

    EndpointMask = 1 << (EpContext->PhysicalEndpoint);
    if (CONTROL_ENDPOINT(Endpoint)) {
        EndpointMask |= 1 << (EpContext->PhysicalEndpoint + 1);
    }

    Address = EpContext->Descriptor.bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK;

    //
    // Configure the endpoint
    //
    UfxEndpointConfigureHardware(Endpoint, Address == 0);

    //
    // #### TODO: Insert code to enable the endpoint on the controller ####
    //

    if (Address != 0) {
        TransferStart(Endpoint);
    }

    TraceExit();
}

_Must_inspect_result_
_IRQL_requires_max_(DISPATCH_LEVEL)
NTSTATUS
UfxEndpointDescriptorUpdate (
    _In_ UFXENDPOINT Endpoint,
    _In_ PUSB_ENDPOINT_DESCRIPTOR Descriptor
    )
/*++
Routine Description:

    Updates the descriptor and re-programs the endpoint.
    If endpoint 0 is passed, starts a new configuration.

Parameters Description:

    Endpoint - Endpoint to update.

    Descriptor - Endpoint descriptor for this endpoint.

Return Value:

    STATUS_SUCCESS if successful, appropriate NTSTATUS message otherwise.

--*/
{
    PUFXENDPOINT_CONTEXT EpContext;

    TraceEntry();

    EpContext = UfxEndpointGetContext(Endpoint);

    RtlCopyMemory(&EpContext->Descriptor, Descriptor, sizeof(USB_ENDPOINT_DESCRIPTOR));

    if ((Descriptor->bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK) == 0) {
        UfxEndpointConfigure(Endpoint);
    }

    TraceExit();
    return STATUS_SUCCESS;
}

VOID
UfxEndpoint_Cleanup (
    _In_ WDFOBJECT Object
    )
/*++
Routine Description:

    Cleanup routine for the UFX endpoint object.

Parameters Description:

    Object - UFX endpoint object

--*/
{
    UFXENDPOINT Endpoint;
    PUFXENDPOINT_CONTEXT EpContext;
    ULONG EndpointMask;
    PUFXDEVICE_CONTEXT DeviceContext;
    NTSTATUS Status;

    TraceEntry();

    Endpoint = (UFXENDPOINT) Object;
    EpContext = UfxEndpointGetContext(Endpoint);
    DeviceContext = UfxDeviceGetContext(EpContext->UfxDevice);

    //
    // Release all references
    //
    WdfCollectionRemove(DeviceContext->Endpoints, Endpoint);

    //
    // Disable the endpoint. Note queue should already be purged by UFX.
    //
    Status = WdfDeviceStopIdle(DeviceContext->FdoWdfDevice, TRUE);
    CHK_NT_MSG(Status, "Failed to stop idle to disable endpoint");

    EndpointMask = 1 << (EpContext->PhysicalEndpoint);
    if (CONTROL_ENDPOINT(Endpoint)) {
        EndpointMask |= 1 << (EpContext->PhysicalEndpoint + 1);
    }

    //
    // #### TODO: Add code to disable the endpoint on the controller ####
    //

    WdfDeviceResumeIdle(DeviceContext->FdoWdfDevice);

    //
    // Delete allocated stuff
    //
    TransferDestroy(Endpoint);

End:
    TraceExit();
}

_Use_decl_annotations_
VOID
EndpointQueue_EvtIoStop (
    WDFQUEUE Queue,
    WDFREQUEST Request,
    ULONG ActionFlags
    )
/*++
Routine Description:

    EvtIoStop event handler

Parameters Description:

    Queue - Handle to the queue object

    Request - The request to be completed, requeued, or suspended.

    ActionFlags - Bitmask indicating action to take and if request is cancelable.

--*/
{
    UNREFERENCED_PARAMETER(Queue);
    UNREFERENCED_PARAMETER(ActionFlags);

    TraceEntry();

    TransferRequestCancel(Request, TRUE);

    TraceExit();
}

VOID
EvtEndpointCommandQueue (
    _In_ WDFQUEUE Queue,
    _In_ WDFREQUEST Request,
    _In_ size_t OutputBufferLength,
    _In_ size_t InputBufferLength,
    _In_ ULONG IoControlCode
    )
/*++
Routine Description:

    EvtIoInternalDeviceControl event handler

Parameters Description:

    Queue - Handle to the queue object

    Request - Internal device control request.

    OutputBufferLength - size of the output buffer.

    InputBufferLength - size of the input buffer.

    IoControlCode - IOCTL for the request.

--*/

{
    PVOID OutputBuffer;
    PENDPOINT_QUEUE_CONTEXT QueueContext;
    NTSTATUS Status;
    PUFXENDPOINT_CONTEXT EpContext;

    UNREFERENCED_PARAMETER(OutputBufferLength);
    UNREFERENCED_PARAMETER(InputBufferLength);

    TraceEntry();

    QueueContext = EndpointQueueGetContext(Queue);
    EpContext = UfxEndpointGetContext(QueueContext->Endpoint);

    Status = WdfRequestRetrieveOutputBuffer(Request, 0, &OutputBuffer, NULL);
    CHK_NT_MSG(Status, "Failed to retrieve command output buffer");

    switch(IoControlCode) {
        case IOCTL_INTERNAL_USBFN_GET_PIPE_STATE:
            TransferGetStall(
                QueueContext->Endpoint,
                (PBOOLEAN) OutputBuffer);

            WdfRequestComplete(Request, STATUS_SUCCESS);
            break;

        case IOCTL_INTERNAL_USBFN_SET_PIPE_STATE:
            if (*((PBOOLEAN) OutputBuffer)) {
                EpContext->StallRequest = Request;
                TransferCommandStallSet(QueueContext->Endpoint);

            } else {
                EpContext->ClearRequest = Request;
                TransferCommandStallClear(QueueContext->Endpoint);
            }
            break;

        case IOCTL_INTERNAL_USBFN_DESCRIPTOR_UPDATE:
            Status = UfxEndpointDescriptorUpdate(
                         QueueContext->Endpoint,
                         (PUSB_ENDPOINT_DESCRIPTOR) OutputBuffer);
            CHK_NT_MSG(Status, "Failed to update endpoint descriptor");
            WdfRequestComplete(Request, STATUS_SUCCESS);
            break;

        default:
            Status = STATUS_INVALID_DEVICE_REQUEST;
            goto End;
    }

End:
    if (!NT_SUCCESS(Status)) {
        WdfRequestComplete(Request, Status);
    }
    TraceExit();
}