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
/*! \file drv_xfer.h
*/
/* handle pipe reset scenarios: ResetPipeOnResume, AutoClearStall */ \
if \
\
} while
if \
{ \
if \
\
} \
}while
// STATUS_IO_TIMEOUT is set by the driver when a timeout policy expires.
// STATUS_CANCELLED is set as a result of the user cancelling the request.
switch \
{ \
case STATUS_SUCCESS: \
break; \
case STATUS_TIMEOUT: \
case STATUS_IO_TIMEOUT: \
; \
break; \
case STATUS_CANCELLED: \
; \
break; \
default: \
; \
break; \
}
VOID ;
VOID ;
VOID ;
VOID ;
VOID ;
VOID ;
VOID ;
VOID ;
FORCEINLINE NTSTATUS
{
NTSTATUS status = STATUS_SUCCESS;
UINT timeoutMS = requestContext->Timeout;
if
return status;
}
FORCEINLINE NTSTATUS
{
NTSTATUS status = STATUS_SUCCESS;
;
if
return status;
}
FORCEINLINE NTSTATUS UrbFormatBulkRequestContext(
__in WDFREQUEST Request,
__in PREQUEST_CONTEXT requestContext,
__out_opt PULONG stageTransferSize,
__out_opt PULONG remainingTransferSize)
{
NTSTATUS status;
PURB urb = NULL;
ULONG remaining = requestContext->Length - requestContext->TotalTransferred;
ULONG stageSize = GetRequestStageSize(requestContext);
ULONG nextStageSize = remaining > stageSize ? stageSize : remaining;
UCHAR pipeID = GetRequestPipeID(requestContext);
// Return the length of the next stage transfer (bytes we are sending for this one) to the caller.
if (stageTransferSize)
*stageTransferSize = nextStageSize;
// Return the the 'projected' remaing bytes (after a successful completion of this stage) to the caller.
if (remainingTransferSize)
*remainingTransferSize = (remaining - nextStageSize);
if (requestContext->UrbMemory == WDF_NO_HANDLE)
{
// This is the first stage of transfer(s).
// Create the URB and set all the initial requestContext members.
WDF_OBJECT_ATTRIBUTES urbAttributes;
USBD_PIPE_HANDLE usbdPipeHandle;
ULONG urbFlags = (requestContext->RequestType == WdfRequestTypeRead) ?
USBD_TRANSFER_DIRECTION_IN | USBD_SHORT_TRANSFER_OK : USBD_TRANSFER_DIRECTION_OUT;
CONST PCHAR dirString = GetEndpointDirString(pipeID);
CONST PCHAR requestTypeString = GetRequestTypeString(requestContext->RequestType);
if (dirString != requestTypeString)
{
USBERR("cannot %s to a %s pipe pipeID=%02Xh\n",
requestTypeString, dirString, pipeID);
status = STATUS_BAD_DEVICE_TYPE;
goto Exit;
}
if (!requestContext->OriginalTransferMDL)
{
if (nextStageSize != 0)
{
USBERR("nextStageSize=%u and OriginalTransferMDL is NULL. pipeID=%02Xh\n",
nextStageSize,
pipeID);
status = STATUS_INSUFFICIENT_RESOURCES;
goto Exit;
}
if (USB_ENDPOINT_DIRECTION_IN(pipeID))
{
// Can only happen if the completion event submits a zero length transfer to a read (out) pipe. (never).
USBERR("Zero-length buffer. pipeID=%02Xh\n", pipeID);
status = STATUS_BUFFER_TOO_SMALL;
goto Exit;
}
USBWRN("zero-length transfer. pipeID=%02Xh\n", pipeID);
}
else
{
requestContext->VirtualAddress = (ULONG_PTR) MmGetMdlVirtualAddress(requestContext->OriginalTransferMDL);
requestContext->Mdl = IoAllocateMdl((PVOID) requestContext->VirtualAddress, requestContext->Length, FALSE, FALSE, NULL);
if (requestContext->Mdl == NULL)
{
USBERR("IoAllocateMdl failed for requestContext->Mdl\n");
status = STATUS_INSUFFICIENT_RESOURCES;
goto Exit;
}
//
// Build an mdl that will describe stage sections of the buffer. (VirtualAddress=Start offset) (nextStageSize=transfer length).
// As Transfers complete successfully, the VirtualAddress is increased by the number of bytes transferred.
// (usually nextStageSize depending on pipe policy settings).
//
IoBuildPartialMdl(requestContext->OriginalTransferMDL,
requestContext->Mdl,
(PVOID) requestContext->VirtualAddress,
nextStageSize);
}
// When the framework deletrs the request, let it delete urbMemory too.
WDF_OBJECT_ATTRIBUTES_INIT(&urbAttributes);
urbAttributes.ParentObject = Request;
status = WdfMemoryCreate(&urbAttributes,
NonPagedPool,
POOL_TAG,
sizeof(struct _URB_BULK_OR_INTERRUPT_TRANSFER),
&requestContext->UrbMemory,
(PVOID*) &urb);
if (!NT_SUCCESS(status))
{
USBERR("WdfMemoryCreate failed for urbMemory.\n");
goto Exit;
}
// get the wdm pipe handle. It is needed to FormatRequestForUrb.
usbdPipeHandle = WdfUsbTargetPipeWdmGetPipeHandle(requestContext->PipeContext->Pipe);
if (!usbdPipeHandle)
{
USBERR("WdfUsbTargetPipeWdmGetPipeHandle failed for requestContext->PipeContext->Pipe\n");
status = STATUS_INSUFFICIENT_RESOURCES;
goto Exit;
}
// Fill the URB structure.
UsbBuildInterruptOrBulkTransferRequest(urb,
sizeof(struct _URB_BULK_OR_INTERRUPT_TRANSFER),
usbdPipeHandle,
NULL,
requestContext->Mdl,
nextStageSize,
urbFlags,
NULL);
}
else // (stages) URB has already been created; this is a continuation of a larger transfer.
{
if (requestContext->Length == 0)
{
// Special case when a final zlp request is submitted for the SHORT_PACKET_TERMINATE policy.
remaining = 0;
nextStageSize = 0;
}
if (nextStageSize == 0 && USB_ENDPOINT_DIRECTION_IN(pipeID))
{
// Can only happen if the completion event submits a zero length transfer to a read (out) pipe. (never).
USBERR("zero-length buffer. pipeID=%02Xh\n", pipeID);
status = STATUS_BUFFER_TOO_SMALL;
goto Exit;
}
// NOTE: requestContext->TotalTransferred and requestContext->VirtualAddress must be updated in the
// completion event before calling this function from the completion routine.
// Fetch the urb structure for updates.
urb = (PURB) WdfMemoryGetBuffer(requestContext->UrbMemory, NULL);
if (!urb)
{
USBERR("WdfMemoryGetBuffer failed for requestContext->UrbMemory\n");
status = STATUS_INSUFFICIENT_RESOURCES;
goto Exit;
}
// Update the URB transfer length.
urb->UrbBulkOrInterruptTransfer.TransferBufferLength = nextStageSize;
if (nextStageSize > 0)
{
// Continue reusing the Mdl created in the first stage transfer.
MmPrepareMdlForReuse(requestContext->Mdl);
// Update the mdl start offset and length.
// requestContext->VirtualAddress must be updated in the complete event before calling this function again!
IoBuildPartialMdl(requestContext->OriginalTransferMDL, requestContext->Mdl, (PVOID) requestContext->VirtualAddress, nextStageSize);
}
else
{
// Occurs when SHORT_PACKET_TERMINATE policy is TRUE.
USBDBG("sending Zero-length-packet. pipeID=%02Xh\n", pipeID);
urb->UrbBulkOrInterruptTransfer.TransferBufferMDL = NULL;
}
}
// Assign the new urb to the request and prepare it for WdfRequestSend.
status = WdfUsbTargetPipeFormatRequestForUrb(requestContext->PipeContext->Pipe, Request, requestContext->UrbMemory, NULL);
if (!NT_SUCCESS(status))
{
USBERR("WdfUsbTargetPipeFormatRequestForUrb failed. status=%Xh\n", status);
goto Exit;
}
Exit:
return status;
}