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
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.
Module Name:
stream.c
Abstract:
This files contain routines for Streams.
Environment:
Kernel mode only
Notes:
--*/
#include "private.h"
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, GetStackCapability)
#if (NTDDI_VERSION >= NTDDI_WIN8)
#pragma alloc_text(PAGE, InitializePipeContextForSuperSpeedBulkPipe)
#endif
#endif
NTSTATUS
GetStackCapability(
_In_ WDFDEVICE Device,
_In_ const GUID* CapabilityType,
_In_ ULONG OutputBufferLength,
_When_(OutputBufferLength == 0, _Pre_null_)
_When_(OutputBufferLength != 0, _Out_writes_bytes_(OutputBufferLength))
PUCHAR OutputBuffer
)
/*++
Routine Description:
The helper routine gets stack's capability.
Arguments:
Device - WDF Device Object
CapabilityType - Pointer to capability type GUID
OutputBufferLength - Length of output buffer
OutPutBuffer - Output buffer
Return Value:
NTSTATUS
--*/
{
PDEVICE_CONTEXT pDevContext;
NTSTATUS status;
PAGED_CODE();
pDevContext = GetDeviceContext(Device);
//
// Note: All super speed bulk stream I/O transfers use USBD Handle obtained in
// UsbSamp_EvtDeviceAdd. If you call WdfUsbTargetDeviceQueryUsbCapability
// method instead of USBD_QueryUsbCapability here, it will not set stream
// capabilities for USBD Handle used by stream transfer in which case
// the open streams request will fail in this example.
//
status = USBD_QueryUsbCapability(pDevContext->UsbdHandle,
CapabilityType,
OutputBufferLength,
OutputBuffer,
NULL);
if (NT_SUCCESS(status)) {
UsbSamp_DbgPrint(1, ("USBD_QueryUsbCapability %x\n", status));
}
return status;
}
#if (NTDDI_VERSION >= NTDDI_WIN8)
NTSTATUS
InitializePipeContextForSuperSpeedBulkPipe(
_In_ PDEVICE_CONTEXT DeviceContext,
_In_ UCHAR InterfaceNumber,
_In_ WDFUSBPIPE Pipe
)
/*++
Routine Description:
This routine initialize pipe's streams context.
Arguments:
DeviceContext - pointer to device context
InterfaceNumber - InterfaceNumber of selected interface
Pipe - Bulk Pipe
Return Value:
NTSTATUS
--*/
{
WDF_USB_PIPE_INFORMATION pipeInfo;
PPIPE_CONTEXT pipeContext;
PUSB_ENDPOINT_DESCRIPTOR pEndpointDescriptor;
PUSB_SUPERSPEED_ENDPOINT_COMPANION_DESCRIPTOR pEndpointCompanionDescriptor;
UCHAR endpointAddress;
ULONG maxStreams;
ULONG supportedStreams;
PUSBSAMP_STREAM_INFO pStreamInfo;
NTSTATUS status;
PURB pUrb = NULL;
ULONG i;
PAGED_CODE();
WDF_USB_PIPE_INFORMATION_INIT(&pipeInfo);
WdfUsbTargetPipeGetInformation(Pipe, &pipeInfo);
pipeContext = GetPipeContext(Pipe);
pStreamInfo = &pipeContext->StreamInfo;
pStreamInfo->NumberOfStreams = 0;
pStreamInfo->StreamList = NULL;
pipeContext->StreamConfigured = FALSE;
//
// Validate that the endpoint/pipe is of type BULK.
// Streams are only allowed on a SS BULK endpoint.
// Also ensure that the bus actually supports streams
// before proceeding
//
if (pipeInfo.PipeType != WdfUsbPipeTypeBulk ||
DeviceContext->NumberOfStreamsSupportedByController == 0) {
status = STATUS_SUCCESS;
goto End;
}
endpointAddress = pipeInfo.EndpointAddress;
pEndpointDescriptor = GetEndpointDescriptorForEndpointAddress(
DeviceContext,
InterfaceNumber,
endpointAddress,
&pEndpointCompanionDescriptor);
if (pEndpointDescriptor != NULL &&
pEndpointCompanionDescriptor != NULL) {
maxStreams = pEndpointCompanionDescriptor->bmAttributes.Bulk.MaxStreams;
if (maxStreams == 0) {
supportedStreams = 0;
} else {
supportedStreams = 1 << maxStreams;
}
} else {
UsbSamp_DbgPrint(1, ("Endpoint Descriptor or Endpoint Companion Descriptor is NULL.\n"));
status = STATUS_INVALID_PARAMETER;
goto End;
}
if (supportedStreams == 0) {
status = STATUS_SUCCESS;
goto End;
}
if (supportedStreams > DeviceContext->NumberOfStreamsSupportedByController) {
supportedStreams = DeviceContext->NumberOfStreamsSupportedByController;
}
pStreamInfo->NumberOfStreams = supportedStreams;
pStreamInfo->StreamList = ExAllocatePool2(
POOL_FLAG_NON_PAGED,
supportedStreams * sizeof(USBD_STREAM_INFORMATION),
POOL_TAG);
if (pStreamInfo->StreamList == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto End;
}
for(i = 0; i < supportedStreams; i++)
{
pStreamInfo->StreamList[i].StreamID = i + 1;
}
status = USBD_UrbAllocate(DeviceContext->UsbdHandle, &pUrb);
if (!NT_SUCCESS(status)){
pUrb = NULL;
status = STATUS_INSUFFICIENT_RESOURCES;
goto End;
}
pUrb->UrbOpenStaticStreams.Hdr.Length = sizeof(struct _URB_OPEN_STATIC_STREAMS);
pUrb->UrbOpenStaticStreams.Hdr.Function = URB_FUNCTION_OPEN_STATIC_STREAMS;
pUrb->UrbOpenStaticStreams.PipeHandle = WdfUsbTargetPipeWdmGetPipeHandle(Pipe);
pUrb->UrbOpenStaticStreams.NumberOfStreams = pStreamInfo->NumberOfStreams;
pUrb->UrbOpenStaticStreams.StreamInfoSize = sizeof(USBD_STREAM_INFORMATION);
pUrb->UrbOpenStaticStreams.Streams = pStreamInfo->StreamList;
// Send the URB down the stack
#pragma prefast(suppress:__WARNING_WRITE_OVERRUN, "SAL noise")
status = WdfUsbTargetPipeSendUrbSynchronously(
Pipe,
NULL,
NULL,
pUrb
);
if (NT_SUCCESS(status)) {
pipeContext->StreamConfigured = TRUE;
}
End:
if (!NT_SUCCESS(status)) {
pipeContext->StreamConfigured = FALSE;
pStreamInfo->NumberOfStreams = 0;
if (pStreamInfo->StreamList != NULL) {
ExFreePool(pStreamInfo->StreamList);
pStreamInfo->StreamList = NULL;
}
}
if(pUrb != NULL) {
USBD_UrbFree(DeviceContext->UsbdHandle, pUrb);
}
return status;
}
USBD_PIPE_HANDLE
GetStreamPipeHandleFromBulkPipe(
_In_ WDFUSBPIPE Pipe
)
/*++
Routine Description:
This routine gets a stream USBD_PIPE_HANDLE from a super speed bulk pipe
Arguments:
Pipe - Bulk Pipe
Return Value:
A stream's USBD_PIPE_HANDLE
--*/
{
PPIPE_CONTEXT pipeContext;
PUSBSAMP_STREAM_INFO pStreamInfo;
USBD_PIPE_HANDLE streamPipeHandle;
ULONG index;
pipeContext = GetPipeContext(Pipe);
if (pipeContext->StreamConfigured == FALSE)
{
streamPipeHandle = NULL;
goto End;
}
pStreamInfo = &pipeContext->StreamInfo;
if (pStreamInfo->NumberOfStreams == 0 ||
pStreamInfo->StreamList == NULL)
{
streamPipeHandle = NULL;
goto End;
}
//
// Specify one associate stream's PipeHandle as the super speed bulk endpoint's PipeHandle
//
index = 1;
streamPipeHandle = pStreamInfo->StreamList[index].PipeHandle;
End:
return streamPipeHandle;
}
VOID
ConfigureStreamPipeHandleForRequest(
_In_ WDFREQUEST Request,
_In_ WDFUSBPIPE Pipe
)
/*++
Routine Description:
The framework has formated request for super speed bulk pipe.
For stream transfer, use the associated stream's PipeHandle for transfer.
Arguments:
Request - Read/Write Request.
Pipe - Bulk Pipe
Return Value:
NULL
--*/
{
PIRP irp;
PIO_STACK_LOCATION irpSp;
PURB urb;
//
// Get the IRP that is associated with the framework request object.
//
irp = WdfRequestWdmGetIrp(Request);
//
// Obtain the next-lower driver's I/O stack location in the IRP
//
irpSp = IoGetNextIrpStackLocation(irp);
//
// The framework uses pipe's Pipehandle for data transfer by default.
// For stream transfer, we should use the associated stream's PipeHandle of
// the super speed bulk pipe for transfer. Replace the PipeHandle with
// its associated stream's PipeHandle .
//
urb = irpSp->Parameters.Others.Argument1;
urb->UrbBulkOrInterruptTransfer.PipeHandle = GetStreamPipeHandleFromBulkPipe(Pipe);
}
#endif
ULONG
GetMaxPacketSize(
_In_ WDFUSBPIPE Pipe,
_In_ PDEVICE_CONTEXT DeviceContext
)
/*++
Routine Description:
This routine returns maximum packet size of a bulk pipe
Arguments:
Pipe - Bulk Pipe
Return Value:
Maximum Packet Size
--*/
{
ULONG maxPacketSize;
PPIPE_CONTEXT pipeContext;
pipeContext = GetPipeContext(Pipe);
if (pipeContext->StreamConfigured == TRUE) {
//
// For super speed bulk stream pipe, the max transfer size is
// MAX_STREAM_VALID_PACKET_SIZE which depends on the implementation
// of super speed bulk stream endpoint
//
maxPacketSize = MAX_STREAM_VALID_PACKET_SIZE;
}
else{
if (DeviceContext->IsDeviceSuperSpeed == TRUE)
{
maxPacketSize = MAX_SUPER_SPEED_PACKET_SIZE;
}
else if (DeviceContext->IsDeviceHighSpeed == TRUE)
{
maxPacketSize = MAX_HIGH_SPEED_PACKET_SIZE;
}
else
{
maxPacketSize = MAX_FULL_SPEED_PACKET_SIZE;
}
}
return maxPacketSize;
}
|